Bug 1477432 - Part 7: Stop using nsIJSID in xpconnect outside of XPCJSID.cpp, r=mccr8
authorNika Layzell <nika@thelayzells.com>
Wed, 18 Jul 2018 00:33:43 -0400
changeset 503338 a68bee9d2168c514ea485deba6c2624934a1c79f
parent 503337 ab47ae5672ac00f6525f0d109af570a771d97add
child 503339 1ddcdfc06526ffa9a997987ae7a2cfff320c00e9
push id10290
push userffxbld-merge
push dateMon, 03 Dec 2018 16:23:23 +0000
treeherdermozilla-beta@700bed2445e6 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmccr8
bugs1477432
milestone65.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 1477432 - Part 7: Stop using nsIJSID in xpconnect outside of XPCJSID.cpp, r=mccr8 This mostly consists of changes to the XPCComponents objects to avoid using the implementation details, and instead use the API defined in part 1. Differential Revision: https://phabricator.services.mozilla.com/D2284
js/ipc/WrapperOwner.cpp
js/xpconnect/src/XPCComponents.cpp
--- a/js/ipc/WrapperOwner.cpp
+++ b/js/ipc/WrapperOwner.cpp
@@ -406,34 +406,25 @@ WrapperOwner::toString(JSContext* cx, Ha
     return true;
 }
 
 bool
 WrapperOwner::DOMQI(JSContext* cx, JS::HandleObject proxy, JS::CallArgs& args)
 {
     // Someone's calling us, handle nsISupports specially to avoid unnecessary
     // CPOW traffic.
-    HandleValue id = args[0];
-    if (id.isObject()) {
-        RootedObject idobj(cx, &id.toObject());
-        nsCOMPtr<nsIJSID> jsid;
+    if (Maybe<nsID> id = xpc::JSValue2ID(cx, args[0])) {
+        if (id->Equals(NS_GET_IID(nsISupports))) {
+            args.rval().set(args.thisv());
+            return true;
+        }
 
-        nsresult rv = UnwrapArg<nsIJSID>(cx, idobj, getter_AddRefs(jsid));
-        if (NS_SUCCEEDED(rv)) {
-            MOZ_ASSERT(jsid, "bad wrapJS");
-            const nsID* idptr = jsid->GetID();
-            if (idptr->Equals(NS_GET_IID(nsISupports))) {
-                args.rval().set(args.thisv());
-                return true;
-            }
-
-            // Webidl-implemented DOM objects never have nsIClassInfo.
-            if (idptr->Equals(NS_GET_IID(nsIClassInfo))) {
-                return Throw(cx, NS_ERROR_NO_INTERFACE);
-            }
+        // Webidl-implemented DOM objects never have nsIClassInfo.
+        if (id->Equals(NS_GET_IID(nsIClassInfo))) {
+            return Throw(cx, NS_ERROR_NO_INTERFACE);
         }
     }
 
     // It wasn't nsISupports, call into the other process to do the QI for us
     // (since we don't know what other interfaces our object supports). Note
     // that we have to use JS_GetPropertyDescriptor here to avoid infinite
     // recursion back into CPOWDOMQI via WrapperOwner::get().
     // We could stash the actual QI function on our own function object to avoid
--- a/js/xpconnect/src/XPCComponents.cpp
+++ b/js/xpconnect/src/XPCComponents.cpp
@@ -264,34 +264,24 @@ nsXPCComponents_Interfaces::Resolve(nsIX
 
     // we only allow interfaces by name here
     if (name && name[0] != '{') {
         const nsXPTInterfaceInfo* info = nsXPTInterfaceInfo::ByName(name.get());
         if (!info) {
             return NS_OK;
         }
 
-        nsCOMPtr<nsIJSIID> nsid = nsJSIID::NewID(info);
-
-        if (nsid) {
-            nsIXPConnect* xpc = nsIXPConnect::XPConnect();
-            RootedObject idobj(cx);
-            if (NS_SUCCEEDED(xpc->WrapNative(cx, obj,
-                                             static_cast<nsIJSIID*>(nsid),
-                                             NS_GET_IID(nsIJSIID),
-                                             idobj.address()))) {
-                if (idobj) {
-                    *resolvedp = true;
-                    *_retval = JS_DefinePropertyById(cx, obj, id, idobj,
-                                                     JSPROP_ENUMERATE |
-                                                     JSPROP_READONLY |
-                                                     JSPROP_PERMANENT |
-                                                     JSPROP_RESOLVING);
-                }
-            }
+        RootedValue iidv(cx);
+        if (xpc::IfaceID2JSValue(cx, *info, &iidv)) {
+            *resolvedp = true;
+            *_retval = JS_DefinePropertyById(cx, obj, id, iidv,
+                                             JSPROP_ENUMERATE |
+                                             JSPROP_READONLY |
+                                             JSPROP_PERMANENT |
+                                             JSPROP_RESOLVING);
         }
     }
     return NS_OK;
 }
 
 /***************************************************************************/
 /***************************************************************************/
 /***************************************************************************/
@@ -456,37 +446,24 @@ nsXPCComponents_InterfacesByID::Resolve(
             return NS_OK;
         }
 
         const nsXPTInterfaceInfo* info = nsXPTInterfaceInfo::ByIID(iid);
         if (!info) {
             return NS_OK;
         }
 
-        nsCOMPtr<nsIJSIID> nsid = nsJSIID::NewID(info);
-
-        if (!nsid) {
-            return NS_ERROR_OUT_OF_MEMORY;
-        }
-
-        nsIXPConnect* xpc = nsIXPConnect::XPConnect();
-        RootedObject idobj(cx);
-        if (NS_SUCCEEDED(xpc->WrapNative(cx, obj,
-                                         static_cast<nsIJSIID*>(nsid),
-                                         NS_GET_IID(nsIJSIID),
-                                         idobj.address()))) {
-            if (idobj) {
-                *resolvedp = true;
-                *_retval =
-                    JS_DefinePropertyById(cx, obj, id, idobj,
-                                          JSPROP_ENUMERATE |
-                                          JSPROP_READONLY |
-                                          JSPROP_PERMANENT |
-                                          JSPROP_RESOLVING);
-            }
+        RootedValue iidv(cx);
+        if (xpc::IfaceID2JSValue(cx, *info, &iidv)) {
+            *resolvedp = true;
+            *_retval = JS_DefinePropertyById(cx, obj, id, iidv,
+                                             JSPROP_ENUMERATE |
+                                             JSPROP_READONLY |
+                                             JSPROP_PERMANENT |
+                                             JSPROP_RESOLVING);
         }
     }
     return NS_OK;
 }
 
 /***************************************************************************/
 /***************************************************************************/
 /***************************************************************************/
@@ -643,40 +620,29 @@ nsXPCComponents_Classes::Resolve(nsIXPCo
                                  JSContext* cx, JSObject* objArg,
                                  jsid idArg, bool* resolvedp,
                                  bool* _retval)
 
 {
     RootedId id(cx, idArg);
     RootedObject obj(cx, objArg);
 
-    if (!JSID_IS_STRING(id)) {
-        return NS_OK;
-    }
-
-    JS::UniqueChars name = JS_EncodeStringToLatin1(cx, JSID_TO_STRING(id));
-    if (name &&
-        name[0] != '{') { // we only allow contractids here
-        nsCOMPtr<nsIJSCID> nsid = nsJSCID::NewID(name.get());
-        if (nsid) {
-            nsIXPConnect* xpc = nsIXPConnect::XPConnect();
-            RootedObject idobj(cx);
-            if (NS_SUCCEEDED(xpc->WrapNative(cx, obj,
-                                             static_cast<nsIJSCID*>(nsid),
-                                             NS_GET_IID(nsIJSCID),
-                                             idobj.address()))) {
-                if (idobj) {
-                    *resolvedp = true;
-                    *_retval = JS_DefinePropertyById(cx, obj, id, idobj,
-                                                     JSPROP_ENUMERATE |
-                                                     JSPROP_READONLY |
-                                                     JSPROP_PERMANENT |
-                                                     JSPROP_RESOLVING);
-                }
-            }
+    JSAutoByteString name;
+    if (JSID_IS_STRING(id) &&
+        name.encodeLatin1(cx, JSID_TO_STRING(id)) &&
+        name.ptr()[0] != '{') { // we only allow contractids here
+        RootedValue cidv(cx);
+        if (xpc::ContractID2JSValue(cx, nsDependentCString(name.ptr()),
+                                    &cidv)) {
+            *resolvedp = true;
+            *_retval = JS_DefinePropertyById(cx, obj, id, cidv,
+                                             JSPROP_ENUMERATE |
+                                             JSPROP_READONLY |
+                                             JSPROP_PERMANENT |
+                                             JSPROP_RESOLVING);
         }
     }
     return NS_OK;
 }
 
 
 /***************************************************************************/
 /***************************************************************************/
@@ -824,28 +790,22 @@ nsXPCComponents_ClassesByID::NewEnumerat
             }
         }
     }
 
     return NS_OK;
 }
 
 static bool
-IsRegisteredCLSID(const char* str)
+IsRegisteredCLSID(const nsCID& aCID)
 {
-    bool registered;
-    nsID id;
-
-    if (!id.Parse(str)) {
-        return false;
-    }
-
+    bool registered = false;
     nsCOMPtr<nsIComponentRegistrar> compMgr;
     if (NS_FAILED(NS_GetComponentRegistrar(getter_AddRefs(compMgr))) || !compMgr ||
-        NS_FAILED(compMgr->IsCIDRegistered(id, &registered)))
+        NS_FAILED(compMgr->IsCIDRegistered(aCID, &registered)))
         return false;
 
     return registered;
 }
 
 NS_IMETHODIMP
 nsXPCComponents_ClassesByID::Resolve(nsIXPConnectWrappedNative* wrapper,
                                      JSContext* cx, JSObject* objArg,
@@ -854,38 +814,31 @@ nsXPCComponents_ClassesByID::Resolve(nsI
 {
     RootedObject obj(cx, objArg);
     RootedId id(cx, idArg);
 
     if (!JSID_IS_STRING(id)) {
         return NS_OK;
     }
 
+    // we only allow canonical CLSIDs here
+    nsCID cid;
+    JSAutoByteString name;
     RootedString str(cx, JSID_TO_STRING(id));
-    JS::UniqueChars name = JS_EncodeStringToLatin1(cx, str);
-    if (name && name[0] == '{' &&
-        IsRegisteredCLSID(name.get())) // we only allow canonical CLSIDs here
+    if (name.encodeLatin1(cx, str) && name.ptr()[0] == '{' &&
+        cid.Parse(name.ptr()) && IsRegisteredCLSID(cid))
     {
-        nsCOMPtr<nsIJSCID> nsid = nsJSCID::NewID(name.get());
-        if (nsid) {
-            nsIXPConnect* xpc = nsIXPConnect::XPConnect();
-            RootedObject idobj(cx);
-            if (NS_SUCCEEDED(xpc->WrapNative(cx, obj,
-                                             static_cast<nsIJSCID*>(nsid),
-                                             NS_GET_IID(nsIJSCID),
-                                             idobj.address()))) {
-                if (idobj) {
-                    *resolvedp = true;
-                    *_retval = JS_DefinePropertyById(cx, obj, id, idobj,
-                                                     JSPROP_ENUMERATE |
-                                                     JSPROP_READONLY |
-                                                     JSPROP_PERMANENT |
-                                                     JSPROP_RESOLVING);
-                }
-            }
+        RootedValue cidv(cx);
+        if (xpc::ClassID2JSValue(cx, cid, &cidv)) {
+            *resolvedp = true;
+            *_retval = JS_DefinePropertyById(cx, obj, id, cidv,
+                                             JSPROP_ENUMERATE |
+                                             JSPROP_READONLY |
+                                             JSPROP_PERMANENT |
+                                             JSPROP_RESOLVING);
         }
     }
     return NS_OK;
 }
 
 
 /***************************************************************************/
 
@@ -1214,17 +1167,17 @@ nsXPCComponents_ID::CallOrConstruct(nsIX
 }
 
 NS_IMETHODIMP
 nsXPCComponents_ID::HasInstance(nsIXPConnectWrappedNative* wrapper,
                                 JSContext* cx, JSObject* obj,
                                 HandleValue val, bool* bp, bool* _retval)
 {
     if (bp) {
-        *bp = JSValIsInterfaceOfType(cx, val, NS_GET_IID(nsIJSID));
+        *bp = xpc::JSValue2ID(cx, val).isSome();
     }
     return NS_OK;
 }
 
 /***************************************************************************/
 // JavaScript Constructor for Exception objects (Components.Exception)
 
 class nsXPCComponents_Exception final :