Bug 1116189 - Fix up the dom uses of nsAutoJSString::Init to use the cx-less interface where it makes sense. r=bholley
authorDave Huseby <dhuseby@mozilla.com>
Fri, 13 Feb 2015 16:53:00 -0500
changeset 229995 a30cdc2cc8d856aa2e06a7d2fd7bc03c7cf8e05e
parent 229994 a1463070ce7162fe452fb5e32ae27d2baac2f36a
child 229996 39354f436e2b7d5ecde0b7fbbdfbc1fe797c12be
push id55863
push userryanvm@gmail.com
push dateFri, 20 Feb 2015 15:14:27 +0000
treeherdermozilla-inbound@a30cdc2cc8d8 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbholley
bugs1116189
milestone38.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1116189 - Fix up the dom uses of nsAutoJSString::Init to use the cx-less interface where it makes sense. r=bholley
dom/geolocation/nsGeolocationSettings.cpp
--- a/dom/geolocation/nsGeolocationSettings.cpp
+++ b/dom/geolocation/nsGeolocationSettings.cpp
@@ -242,45 +242,39 @@ nsGeolocationSettings::HandleGeolocation
   // because the spec requires calling getters when enumerating the key of a
   // dictionary
   AutoEntryScript aes(global);
   aes.TakeOwnershipOfErrorReporting();
   JSContext *cx = aes.cx();
   JS::AutoIdArray ids(cx, JS_Enumerate(cx, obj));
 
   // if we get no ids then the exception list is empty and we can return here.
-  if (!ids)
+  if (!ids) {
       return;
+  }
 
   // go through all of the objects in the exceptions dictionary
   for (size_t i = 0; i < ids.length(); i++) {
     JS::RootedId id(cx);
     id = ids[i];
 
-    JS::RootedValue v(cx);
-    if (!JS_IdToValue(cx, id, &v) || !v.isString())
-      continue;
-
-    JS::RootedString str(cx, v.toString());
-    if (!str)
+    // if it is an app that is always precise, skip it
+    nsAutoJSString origin;
+    if (!origin.init(cx, id)) {
       continue;
-
-    // get the origin for the app
-    nsString origin;
-    if (!AssignJSString(cx, origin, str))
+    }
+    if (mAlwaysPreciseApps.Contains(origin)) {
       continue;
-
-    // if it is an app that is always precise, skip it
-    if (mAlwaysPreciseApps.Contains(origin))
-      continue;
+    }
 
     // get the app setting object
     JS::RootedValue propertyValue(cx);
-    if (!JS_GetPropertyById(cx, obj, id, &propertyValue) || !propertyValue.isObject())
+    if (!JS_GetPropertyById(cx, obj, id, &propertyValue) || !propertyValue.isObject()) {
       continue;
+    }
     JS::RootedObject settingObj(cx, &propertyValue.toObject());
 
     GeolocationSetting *settings = new GeolocationSetting(origin);
     GPSLOG("adding exception for %s", NS_ConvertUTF16toUTF8(origin).get());
 
     // get the geolocation type
     JS::RootedValue fm(cx);
     if (JS_GetProperty(cx, settingObj, "type", &fm)) {
@@ -311,20 +305,27 @@ nsGeolocationSettings::HandleGeolocation
 {
   if (!aVal.isObject()) {
     return;
   }
 
   // clear the list of apps that are always precise
   mAlwaysPreciseApps.Clear();
 
-  AutoJSAPI jsapi;
-  jsapi.Init();
-  JSContext* cx = jsapi.cx();
-  JS::Rooted<JSObject*> obj(cx, &aVal.toObject());
+  // root the object and get the global
+  JS::Rooted<JSObject*> obj(nsContentUtils::RootingCx(), &aVal.toObject());
+  MOZ_ASSERT(obj);
+  nsIGlobalObject* global = xpc::NativeGlobal(obj);
+  NS_ENSURE_TRUE_VOID(global && global->GetGlobalJSObject());
+
+  // the spec requires calling getters when accessing array by index
+  AutoEntryScript aes(global);
+  aes.TakeOwnershipOfErrorReporting();
+  JSContext *cx = aes.cx();
+
   if (!JS_IsArrayObject(cx, obj)) {
     return;
   }
 
   uint32_t length;
   if (!JS_GetArrayLength(cx, obj, &length)) {
     return;
   }
@@ -332,39 +333,35 @@ nsGeolocationSettings::HandleGeolocation
   // process the list of apps...
   for (uint32_t i = 0; i < length; ++i) {
     JS::RootedValue value(cx);
 
     if (!JS_GetElement(cx, obj, i, &value) || !value.isString()) {
       continue;
     }
 
-    nsString origin;
-    if (!AssignJSString(cx, origin, value.toString())) {
+    nsAutoJSString origin;
+    if (!origin.init(cx, value)) {
       continue;
     }
 
     GPSLOG("adding always precise for %s", NS_ConvertUTF16toUTF8(origin).get());
 
     // add the origin to the list of apps that will always receive
     // precise location information
     mAlwaysPreciseApps.AppendElement(origin);
   }
 }
 
 
-
 void
 GeolocationSetting::HandleTypeChange(const JS::Value& aVal)
 {
-  AutoJSAPI jsapi;
-  jsapi.Init();
-  JSContext* cx = jsapi.cx();
-  nsString str;
-  if (!aVal.isString() || !AssignJSString(cx, str, aVal.toString())) {
+  nsAutoJSString str;
+  if (!str.init(aVal)) {
     return;
   }
 
   GeolocationFuzzMethod fm = GEO_ALA_TYPE_DEFAULT;
   if (str.EqualsASCII(GEO_ALA_TYPE_VALUE_PRECISE)) {
     fm = GEO_ALA_TYPE_PRECISE;
   } else if (str.EqualsASCII(GEO_ALA_TYPE_VALUE_APPROX)) {
 #ifdef MOZ_APPROX_LOCATION
@@ -428,38 +425,39 @@ GeolocationSetting::HandleApproxDistance
   mDistance = aVal.toInt32();
 }
 #endif
 
 
 void
 GeolocationSetting::HandleFixedCoordsChange(const JS::Value& aVal)
 {
-  AutoJSAPI jsapi;
-  jsapi.Init();
-  JSContext* cx = jsapi.cx();
-  nsString str;
-  if (!aVal.isString() || !AssignJSString(cx, str, aVal.toString()) || str.IsEmpty()) {
-    return;
+  nsAutoJSString str;
+  if (!str.init(aVal)) {
+      return;
   }
 
   // parse the string and store the global lat/lon
-  // the @ character is present in the GPS coord strings we receive
+  // the string is in the form of @1.2345,6.7890
+  // check for leading '@' and a comma in the middle
   int32_t const comma = str.Find(",");
   if ( (str.CharAt(0) != '@') || (comma == -1) ) {
     return;
   }
 
+  // pull the lat and lon out of the string and convert to doubles
   nsresult rv;
-  nsString slat(Substring(str, 1, comma));
+  nsString slat(Substring(str, 1, comma - 1));
   nsString slon(Substring(str, comma + 1));
   double lat = slat.ToDouble(&rv);
   NS_ENSURE_SUCCESS(rv,);
   double lon = slon.ToDouble(&rv);
   NS_ENSURE_SUCCESS(rv,);
+
+  // store the values
   mLatitude = lat;
   mLongitude = lon;
 
   GPSLOG("changing coords for %s to %s (%f, %f)",
          (mOrigin.IsEmpty() ? "global" : NS_ConvertUTF16toUTF8(mOrigin).get()),
          NS_ConvertUTF16toUTF8(str).get(),
          mLatitude, mLongitude);
 }