Bug 798123 - Replace the array conversion functions in BluetoothUtils.cpp with those in nsTArrayHelpers.h. r=mrbkap
authorGabriele Svelto <gsvelto@mozilla.com>
Thu, 18 Oct 2012 15:20:54 +0200
changeset 110871 811bf58c47f2d8ac629a1b805fa97670e4c626fb
parent 110870 977d1735104901918e054f035755364662a73d70
child 110872 fe90dfc8a30a1fce0cc2cf8078ac871a3f300399
push id93
push usernmatsakis@mozilla.com
push dateWed, 31 Oct 2012 21:26:57 +0000
reviewersmrbkap
bugs798123
milestone19.0a1
Bug 798123 - Replace the array conversion functions in BluetoothUtils.cpp with those in nsTArrayHelpers.h. r=mrbkap
dom/bluetooth/BluetoothAdapter.cpp
dom/bluetooth/BluetoothDevice.cpp
dom/bluetooth/BluetoothUtils.cpp
dom/bluetooth/BluetoothUtils.h
js/xpconnect/public/nsTArrayHelpers.h
--- a/dom/bluetooth/BluetoothAdapter.cpp
+++ b/dom/bluetooth/BluetoothAdapter.cpp
@@ -15,16 +15,17 @@
 #include "GeneratedEvents.h"
 
 #include "nsContentUtils.h"
 #include "nsDOMClassInfo.h"
 #include "nsDOMEvent.h"
 #include "nsIDOMBluetoothDeviceAddressEvent.h"
 #include "nsIDOMBluetoothDeviceEvent.h"
 #include "nsIDOMDOMRequest.h"
+#include "nsTArrayHelpers.h"
 #include "nsThreadUtils.h"
 #include "nsXPCOMCIDInternal.h"
 
 #include "mozilla/dom/bluetooth/BluetoothTypes.h"
 #include "mozilla/dom/ContentChild.h"
 #include "mozilla/LazyIdleThread.h"
 #include "mozilla/Util.h"
 
@@ -97,18 +98,17 @@ public:
 
     nsresult rv;
     nsIScriptContext* sc = mAdapterPtr->GetContextForEventHandlers(&rv);
     if (!sc) {
       NS_WARNING("Cannot create script context!");
       return false;
     }
 
-    rv = BluetoothDeviceArrayToJSArray(sc->GetNativeContext(),
-                                       sc->GetNativeGlobal(), devices, &JsDevices);
+    rv = nsTArrayToJSArray(sc->GetNativeContext(), devices, &JsDevices);
 
     if (JsDevices) {
       aValue->setObject(*JsDevices);
     }
     else {
       NS_WARNING("Paird not yet set!");
       return false;
     }
@@ -206,34 +206,33 @@ BluetoothAdapter::SetPropertyByValue(con
   } else if (name.EqualsLiteral("Class")) {
     mClass = value.get_uint32_t();
   } else if (name.EqualsLiteral("UUIDs")) {
     mUuids = value.get_ArrayOfnsString();
     nsresult rv;
     nsIScriptContext* sc = GetContextForEventHandlers(&rv);
     if (sc) {
       rv =
-        StringArrayToJSArray(sc->GetNativeContext(),
-                             sc->GetNativeGlobal(), mUuids, &mJsUuids);
+        nsTArrayToJSArray(sc->GetNativeContext(), mUuids, &mJsUuids);
       if (NS_FAILED(rv)) {
         NS_WARNING("Cannot set JS UUIDs object!");
         return;
       }
       Root();
     } else {
       NS_WARNING("Could not get context!");
     }
   } else if (name.EqualsLiteral("Devices")) {
     mDeviceAddresses = value.get_ArrayOfnsString();
     nsresult rv;
     nsIScriptContext* sc = GetContextForEventHandlers(&rv);
     if (sc) {
       rv =
-        StringArrayToJSArray(sc->GetNativeContext(),
-                             sc->GetNativeGlobal(), mDeviceAddresses, &mJsDeviceAddresses);
+        nsTArrayToJSArray(sc->GetNativeContext(), mDeviceAddresses,
+                          &mJsDeviceAddresses);
       if (NS_FAILED(rv)) {
         NS_WARNING("Cannot set JS Device Addresses object!");
         return;
       }
       Root();
     } else {
       NS_WARNING("Could not get context!");
     }
--- a/dom/bluetooth/BluetoothDevice.cpp
+++ b/dom/bluetooth/BluetoothDevice.cpp
@@ -10,16 +10,17 @@
 #include "BluetoothReplyRunnable.h"
 #include "BluetoothService.h"
 #include "BluetoothUtils.h"
 #include "BluetoothServiceUuid.h"
 
 #include "nsIDOMDOMRequest.h"
 #include "nsDOMClassInfo.h"
 #include "nsContentUtils.h"
+#include "nsTArrayHelpers.h"
 #include "mozilla/dom/bluetooth/BluetoothTypes.h"
 
 USING_BLUETOOTH_NAMESPACE
 
 DOMCI_DATA(BluetoothDevice, BluetoothDevice)
 
 NS_IMPL_CYCLE_COLLECTION_CLASS(BluetoothDevice)
 
@@ -113,34 +114,32 @@ BluetoothDevice::SetPropertyByValue(cons
   } else if (name.EqualsLiteral("Paired")) {
     mPaired = value.get_bool();
   } else if (name.EqualsLiteral("UUIDs")) {
     mUuids = value.get_ArrayOfnsString();
     nsresult rv;
     nsIScriptContext* sc = GetContextForEventHandlers(&rv);
     if (sc) {
       rv =
-        StringArrayToJSArray(sc->GetNativeContext(),
-                             sc->GetNativeGlobal(), mUuids, &mJsUuids);
+        nsTArrayToJSArray(sc->GetNativeContext(), mUuids, &mJsUuids);
       if (NS_FAILED(rv)) {
         NS_WARNING("Cannot set JS UUIDs object!");
         return;
       }
       Root();
     } else {
       NS_WARNING("Could not get context!");
     }
   } else if (name.EqualsLiteral("Services")) {
     mServices = value.get_ArrayOfnsString();
     nsresult rv;
     nsIScriptContext* sc = GetContextForEventHandlers(&rv);
     if (sc) {
       rv =
-        StringArrayToJSArray(sc->GetNativeContext(),
-                             sc->GetNativeGlobal(), mServices, &mJsServices);
+        nsTArrayToJSArray(sc->GetNativeContext(), mServices, &mJsServices);
       if (NS_FAILED(rv)) {
         NS_WARNING("Cannot set JS Services object!");
         return;
       }
       Root();
     } else {
       NS_WARNING("Could not get context!");
     }
--- a/dom/bluetooth/BluetoothUtils.cpp
+++ b/dom/bluetooth/BluetoothUtils.cpp
@@ -13,103 +13,16 @@
 #include "mozilla/dom/bluetooth/BluetoothTypes.h"
 #include "nsContentUtils.h"
 #include "nsISystemMessagesInternal.h"
 #include "nsTArray.h"
 #include "nsString.h"
 
 USING_BLUETOOTH_NAMESPACE
 
-nsresult
-mozilla::dom::bluetooth::StringArrayToJSArray(JSContext* aCx, JSObject* aGlobal,
-                                              const nsTArray<nsString>& 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++) {
-      JSString* s = JS_NewUCStringCopyN(aCx, aSourceArray[index].BeginReading(),
-                                        aSourceArray[index].Length());
-      if(!s) {
-        NS_WARNING("Memory allocation error!");
-        return NS_ERROR_OUT_OF_MEMORY;
-      }
-      valArray[index] = STRING_TO_JSVAL(s);
-      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.
-  // Followup at bug 717414
-  if (!JS_FreezeObject(aCx, arrayObj)) {
-    return NS_ERROR_FAILURE;
-  }
-
-  *aResultArray = arrayObj;
-  return NS_OK;
-}
-
-nsresult
-mozilla::dom::bluetooth::BluetoothDeviceArrayToJSArray(JSContext* aCx, JSObject* aGlobal,
-                                                       const nsTArray<nsRefPtr<BluetoothDevice> >& 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;
-  }
-
-  if (!JS_FreezeObject(aCx, arrayObj)) {
-    return NS_ERROR_FAILURE;
-  }
-
-  *aResultArray = arrayObj;
-  return NS_OK;
-}
-
 bool
 mozilla::dom::bluetooth::SetJsObject(JSContext* aContext,
                                      JSObject* aObj,
                                      const InfallibleTArray<BluetoothNamedValue>& aData)
 {
   for (uint32_t i = 0; i < aData.Length(); i++) {
     jsval v;
     if (aData[i].value().type() == BluetoothValue::TnsString) {
--- a/dom/bluetooth/BluetoothUtils.h
+++ b/dom/bluetooth/BluetoothUtils.h
@@ -12,26 +12,16 @@
 class JSContext;
 class JSObject;
 
 BEGIN_BLUETOOTH_NAMESPACE
 
 class BluetoothDevice;
 class BluetoothNamedValue;
 
-nsresult
-StringArrayToJSArray(JSContext* aCx, JSObject* aGlobal,
-                     const nsTArray<nsString>& aSourceArray,
-                     JSObject** aResultArray);
-
-nsresult
-BluetoothDeviceArrayToJSArray(JSContext* aCx, JSObject* aGlobal,
-                              const nsTArray<nsRefPtr<BluetoothDevice> >& aSourceArray,
-                              JSObject** aResultArray);
-
 bool
 SetJsObject(JSContext* aContext,
             JSObject* aObj,
             const InfallibleTArray<BluetoothNamedValue>& aData);
 
 nsString
 GetObjectPathFromAddress(const nsAString& aAdapterPath,
                          const nsAString& aDeviceAddress);
--- a/js/xpconnect/public/nsTArrayHelpers.h
+++ b/js/xpconnect/public/nsTArrayHelpers.h
@@ -41,9 +41,53 @@ nsTArrayToJSArray(JSContext* aCx, const 
     NS_WARNING("JS_FreezeObject failed!");
     return NS_ERROR_FAILURE;
   }
 
   *aResultArray = arrayObj;
   return NS_OK;
 }
 
+template <>
+inline nsresult
+nsTArrayToJSArray<nsString>(JSContext* aCx,
+                            const nsTArray<nsString>& aSourceArray,
+                            JSObject** aResultArray)
+{
+  MOZ_ASSERT(aCx);
+  JSAutoRequest ar(aCx);
+
+  JSObject* arrayObj = JS_NewArrayObject(aCx, aSourceArray.Length(), nullptr);
+  if (!arrayObj) {
+    NS_WARNING("JS_NewArrayObject failed!");
+    return NS_ERROR_OUT_OF_MEMORY;
+  }
+
+  JSObject* global = JS_GetGlobalForScopeChain(aCx);
+  MOZ_ASSERT(global);
+
+  for (uint32_t index = 0; index < aSourceArray.Length(); index++) {
+    JSString* s = JS_NewUCStringCopyN(aCx, aSourceArray[index].BeginReading(),
+                                      aSourceArray[index].Length());
+
+    if(!s) {
+      NS_WARNING("Memory allocation error!");
+      return NS_ERROR_OUT_OF_MEMORY;
+    }
+
+    jsval wrappedVal = STRING_TO_JSVAL(s);
+
+    if (!JS_SetElement(aCx, arrayObj, index, &wrappedVal)) {
+      NS_WARNING("JS_SetElement failed!");
+      return NS_ERROR_FAILURE;
+    }
+  }
+
+  if (!JS_FreezeObject(aCx, arrayObj)) {
+    NS_WARNING("JS_FreezeObject failed!");
+    return NS_ERROR_FAILURE;
+  }
+
+  *aResultArray = arrayObj;
+  return NS_OK;
+}
+
 #endif /* __NSTARRAYHELPERS_H__ */