Bug 739780 - Switch Telephony.cpp to use nsTArrayHelpers.h implementation of nsTArrayToJSArray. r=khuey, r=mrbkap
authorGabriele Svelto <gsvelto@mozilla.com>
Fri, 28 Sep 2012 10:43:49 -0700
changeset 109291 e6bc6b88d3ba116be3c4e3096188f0c28f3dd9fa
parent 109290 de67d5a36369f34338ddb9d844849f47ed1bc0f6
child 109292 6eb4dda69796bc7d569a250c81e40d82146944f1
push id82
push usershu@rfrn.org
push dateFri, 05 Oct 2012 13:20:22 +0000
reviewerskhuey, mrbkap
bugs739780
milestone18.0a1
Bug 739780 - Switch Telephony.cpp to use nsTArrayHelpers.h implementation of nsTArrayToJSArray. r=khuey, r=mrbkap
dom/telephony/Telephony.cpp
js/xpconnect/public/nsTArrayHelpers.h
--- a/dom/telephony/Telephony.cpp
+++ b/dom/telephony/Telephony.cpp
@@ -15,72 +15,30 @@
 #include "nsCharSeparatedTokenizer.h"
 #include "nsContentUtils.h"
 #include "nsDOMClassInfo.h"
 #include "nsIInterfaceRequestorUtils.h"
 #include "nsNetUtil.h"
 #include "nsServiceManagerUtils.h"
 #include "SystemWorkerManager.h"
 #include "nsRadioInterfaceLayer.h"
+#include "nsTArrayHelpers.h"
 
 #include "CallEvent.h"
 #include "TelephonyCall.h"
 
 USING_TELEPHONY_NAMESPACE
 using namespace mozilla::dom::gonk;
 
 namespace {
 
 typedef nsAutoTArray<Telephony*, 2> TelephonyList;
 
 TelephonyList* gTelephonyList;
 
-template <class T>
-inline nsresult
-nsTArrayToJSArray(JSContext* aCx, JSObject* aGlobal,
-                  const nsTArray<nsRefPtr<T> >& aSourceArray,
-                  JSObject** aResultArray)
-{
-  NS_ASSERTION(aCx, "Null context!");
-  NS_ASSERTION(aGlobal, "Null global!");
-
-  JSAutoRequest ar(aCx);
-  JSAutoCompartment ac(aCx, aGlobal);
-
-  JSObject* arrayObj;
-
-  if (aSourceArray.IsEmpty()) {
-    arrayObj = JS_NewArrayObject(aCx, 0, nullptr);
-  } else {
-    uint32_t valLength = aSourceArray.Length();
-    mozilla::ScopedDeleteArray<jsval> valArray(new jsval[valLength]);
-    JS::AutoArrayRooter tvr(aCx, 0, valArray);
-    for (uint32_t index = 0; index < valLength; index++) {
-      nsISupports* obj = aSourceArray[index]->ToISupports();
-      nsresult rv =
-        nsContentUtils::WrapNative(aCx, aGlobal, obj, &valArray[index]);
-      NS_ENSURE_SUCCESS(rv, rv);
-      tvr.changeLength(index + 1);
-    }
-    arrayObj = JS_NewArrayObject(aCx, valLength, valArray);
-  }
-
-  if (!arrayObj) {
-    return NS_ERROR_OUT_OF_MEMORY;
-  }
-
-  // XXX This is not what Jonas wants. He wants it to be live.
-  if (!JS_FreezeObject(aCx, arrayObj)) {
-    return NS_ERROR_FAILURE;
-  }
-
-  *aResultArray = arrayObj;
-  return NS_OK;
-}
-
 } // anonymous namespace
 
 Telephony::Telephony()
 : mActiveCall(nullptr), mCallsArray(nullptr), mRooted(false)
 {
   if (!gTelephonyList) {
     gTelephonyList = new TelephonyList();
   }
@@ -329,19 +287,17 @@ NS_IMETHODIMP
 Telephony::GetCalls(jsval* aCalls)
 {
   JSObject* calls = mCallsArray;
   if (!calls) {
     nsresult rv;
     nsIScriptContext* sc = GetContextForEventHandlers(&rv);
     NS_ENSURE_SUCCESS(rv, rv);
     if (sc) {
-      rv =
-        nsTArrayToJSArray(sc->GetNativeContext(),
-                          sc->GetNativeGlobal(), mCalls, &calls);
+      rv = nsTArrayToJSArray(sc->GetNativeContext(), mCalls, &calls);
       NS_ENSURE_SUCCESS(rv, rv);
 
       if (!mRooted) {
         NS_HOLD_JS_OBJECTS(this, Telephony);
         mRooted = true;
       }
 
       mCallsArray = calls;
--- a/js/xpconnect/public/nsTArrayHelpers.h
+++ b/js/xpconnect/public/nsTArrayHelpers.h
@@ -1,15 +1,15 @@
-/* This Source Code Form is subject to the terms of the Mozilla Public
- * License, v. 2.0. If a copy of the MPL was not distributed with this file,
- * You can obtain one at http://mozilla.org/MPL/2.0/. */
-
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this file,
+ * You can obtain one at http://mozilla.org/MPL/2.0/. */
+
 #ifndef __NSTARRAYHELPERS_H__
-#define __NSTARRAYHELPERS_H__
-
+#define __NSTARRAYHELPERS_H__
+
 template <class T>
 inline nsresult
 nsTArrayToJSArray(JSContext* aCx, const nsTArray<T>& aSourceArray,
                   JSObject** aResultArray)
 {
   MOZ_ASSERT(aCx);
   JSAutoRequest ar(aCx);
 
@@ -19,17 +19,17 @@ nsTArrayToJSArray(JSContext* aCx, const 
     return NS_ERROR_OUT_OF_MEMORY;
   }
 
   JSObject* global = JS_GetGlobalForScopeChain(aCx);
   MOZ_ASSERT(global);
 
   for (uint32_t index = 0; index < aSourceArray.Length(); index++) {
     nsCOMPtr<nsISupports> obj;
-    nsresult rv = CallQueryInterface(aSourceArray[index], getter_AddRefs(obj));
+    nsresult rv = aSourceArray[index]->QueryInterface(NS_GET_IID(nsISupports), getter_AddRefs(obj));
     NS_ENSURE_SUCCESS(rv, rv);
 
     jsval wrappedVal;
     rv = nsContentUtils::WrapNative(aCx, global, obj, &wrappedVal, nullptr, true);
     NS_ENSURE_SUCCESS(rv, rv);
 
     if (!JS_SetElement(aCx, arrayObj, index, &wrappedVal)) {
       NS_WARNING("JS_SetElement failed!");
@@ -39,11 +39,11 @@ nsTArrayToJSArray(JSContext* aCx, const 
 
   if (!JS_FreezeObject(aCx, arrayObj)) {
     NS_WARNING("JS_FreezeObject failed!");
     return NS_ERROR_FAILURE;
   }
 
   *aResultArray = arrayObj;
   return NS_OK;
-}
-
-#endif /* __NSTARRAYHELPERS_H__ */
+}
+
+#endif /* __NSTARRAYHELPERS_H__ */