Bug 1176342, part 1 - De-COM InitClassesWithNewWrappedGlobal. r=mrbkap draft
authorAndrew McCreight <continuation@gmail.com>
Mon, 18 Sep 2017 16:35:39 -0700
changeset 667203 eb0939fe0600b114869cec8804dff72ac8f8889e
parent 666274 ffe6cc09ccf38cca6f0e727837bbc6cb722d1e71
child 667204 23542da687ee57943b48c6a4efb8d8d2a003fdec
child 667694 020db2a299ca134919d9344496edda5c7f6c2328
push id80650
push userbmo:continuation@gmail.com
push dateTue, 19 Sep 2017 20:55:26 +0000
reviewersmrbkap
bugs1176342
milestone57.0a1
Bug 1176342, part 1 - De-COM InitClassesWithNewWrappedGlobal. r=mrbkap MozReview-Commit-ID: EfRQW3QUXCG
dom/base/nsFrameMessageManager.cpp
ipc/testshell/XPCShellEnvironment.cpp
js/xpconnect/idl/nsIXPConnect.idl
js/xpconnect/loader/mozJSComponentLoader.cpp
js/xpconnect/src/XPCShellImpl.cpp
js/xpconnect/src/nsXPConnect.cpp
js/xpconnect/src/xpcpublic.h
--- a/dom/base/nsFrameMessageManager.cpp
+++ b/dom/base/nsFrameMessageManager.cpp
@@ -1714,35 +1714,33 @@ nsMessageManagerScriptExecutor::Unlink()
 bool
 nsMessageManagerScriptExecutor::InitChildGlobalInternal(
   nsISupports* aScope,
   const nsACString& aID)
 {
   AutoSafeJSContext cx;
   nsContentUtils::GetSecurityManager()->GetSystemPrincipal(getter_AddRefs(mPrincipal));
 
-  nsIXPConnect* xpc = nsContentUtils::XPConnect();
   const uint32_t flags = nsIXPConnect::INIT_JS_STANDARD_CLASSES;
 
   JS::CompartmentOptions options;
   options.creationOptions().setSystemZone();
   options.behaviors().setVersion(JSVERSION_DEFAULT);
 
   if (xpc::SharedMemoryEnabled()) {
     options.creationOptions().setSharedMemoryAndAtomicsEnabled(true);
   }
 
-  nsCOMPtr<nsIXPConnectJSObjectHolder> globalHolder;
-  nsresult rv =
-    xpc->InitClassesWithNewWrappedGlobal(cx, aScope, mPrincipal,
-                                         flags, options,
-                                         getter_AddRefs(globalHolder));
+  JS::Rooted<JSObject*> global(cx);
+  nsresult rv = xpc::InitClassesWithNewWrappedGlobal(cx, aScope, mPrincipal,
+                                                     flags, options,
+                                                     &global);
   NS_ENSURE_SUCCESS(rv, false);
 
-  mGlobal = globalHolder->GetJSObject();
+  mGlobal = global;
   NS_ENSURE_TRUE(mGlobal, false);
 
   // Set the location information for the new global, so that tools like
   // about:memory may use that information.
   xpc::SetLocationForGlobal(mGlobal, aID);
 
   DidCreateGlobal();
   return true;
--- a/ipc/testshell/XPCShellEnvironment.cpp
+++ b/ipc/testshell/XPCShellEnvironment.cpp
@@ -418,23 +418,16 @@ XPCShellEnvironment::Init()
     // unbuffer stdout so that output is in the correct order; note that stderr
     // is unbuffered by default
     setbuf(stdout, 0);
 
     AutoSafeJSContext cx;
 
     mGlobalHolder.init(cx);
 
-    nsCOMPtr<nsIXPConnect> xpc =
-      do_GetService(nsIXPConnect::GetCID());
-    if (!xpc) {
-        NS_ERROR("failed to get nsXPConnect service!");
-        return false;
-    }
-
     nsCOMPtr<nsIPrincipal> principal;
     nsCOMPtr<nsIScriptSecurityManager> securityManager =
         do_GetService(NS_SCRIPTSECURITYMANAGER_CONTRACTID, &rv);
     if (NS_SUCCEEDED(rv) && securityManager) {
         rv = securityManager->GetSystemPrincipal(getter_AddRefs(principal));
         if (NS_FAILED(rv)) {
             fprintf(stderr, "+++ Failed to obtain SystemPrincipal from ScriptSecurityManager service.\n");
         }
@@ -450,28 +443,27 @@ XPCShellEnvironment::Init()
     }
 
     JS::CompartmentOptions options;
     options.creationOptions().setSystemZone();
     options.behaviors().setVersion(JSVERSION_DEFAULT);
     if (xpc::SharedMemoryEnabled())
         options.creationOptions().setSharedMemoryAndAtomicsEnabled(true);
 
-    nsCOMPtr<nsIXPConnectJSObjectHolder> holder;
-    rv = xpc->InitClassesWithNewWrappedGlobal(cx,
+    JS::Rooted<JSObject*> globalObj(cx);
+    rv = xpc::InitClassesWithNewWrappedGlobal(cx,
                                               static_cast<nsIGlobalObject *>(backstagePass),
                                               principal, 0,
                                               options,
-                                              getter_AddRefs(holder));
+                                              &globalObj);
     if (NS_FAILED(rv)) {
         NS_ERROR("InitClassesWithNewWrappedGlobal failed!");
         return false;
     }
 
-    JS::Rooted<JSObject*> globalObj(cx, holder->GetJSObject());
     if (!globalObj) {
         NS_ERROR("Failed to get global JSObject!");
         return false;
     }
     JSAutoCompartment ac(cx, globalObj);
 
     backstagePass->SetGlobalObject(globalObj);
 
--- a/js/xpconnect/idl/nsIXPConnect.idl
+++ b/js/xpconnect/idl/nsIXPConnect.idl
@@ -274,39 +274,16 @@ interface nsIXPCFunctionThisTranslator :
 
 [noscript, uuid(768507b5-b981-40c7-8276-f6a1da502a24)]
 interface nsIXPConnect : nsISupports
 {
 %{ C++
   NS_DEFINE_STATIC_CID_ACCESSOR(NS_XPCONNECT_CID)
 %}
 
-    /**
-     * Creates a new global object using the given aCOMObj as the global
-     * object. The object will be set up according to the flags (defined
-     * below). If you do not pass INIT_JS_STANDARD_CLASSES, then aCOMObj
-     * must implement nsIXPCScriptable so it can resolve the standard
-     * classes when asked by the JS engine.
-     *
-     * @param aJSContext the context to use while creating the global object.
-     * @param aCOMObj the native object that represents the global object.
-     * @param aPrincipal the principal of the code that will run in this
-     *                   compartment. Can be null if not on the main thread.
-     * @param aFlags one of the flags below specifying what options this
-     *               global object wants.
-     * @param aOptions JSAPI-specific options for the new compartment.
-     */
-    nsIXPConnectJSObjectHolder
-    initClassesWithNewWrappedGlobal(
-                  in JSContextPtr         aJSContext,
-                  in nsISupports          aCOMObj,
-                  in nsIPrincipal         aPrincipal,
-                  in uint32_t             aFlags,
-                  in JSCompartmentOptions aOptions);
-
     const uint32_t INIT_JS_STANDARD_CLASSES  = 1 << 0;
     const uint32_t DONT_FIRE_ONNEWGLOBALHOOK = 1 << 1;
     const uint32_t OMIT_COMPONENTS_OBJECT    = 1 << 2;
 
     /**
     * wrapNative will create a new JSObject or return an existing one.
     *
     * This method now correctly deals with cases where the passed in xpcom
--- a/js/xpconnect/loader/mozJSComponentLoader.cpp
+++ b/js/xpconnect/loader/mozJSComponentLoader.cpp
@@ -508,27 +508,25 @@ mozJSComponentLoader::CreateLoaderGlobal
     options.behaviors().setVersion(JSVERSION_DEFAULT);
 
     if (xpc::SharedMemoryEnabled())
         options.creationOptions().setSharedMemoryAndAtomicsEnabled(true);
 
     // Defer firing OnNewGlobalObject until after the __URI__ property has
     // been defined so the JS debugger can tell what module the global is
     // for
-    nsCOMPtr<nsIXPConnectJSObjectHolder> holder;
-    rv = nsXPConnect::XPConnect()->
-        InitClassesWithNewWrappedGlobal(aCx,
-                                        static_cast<nsIGlobalObject*>(backstagePass),
-                                        nsContentUtils::GetSystemPrincipal(),
-                                        nsIXPConnect::DONT_FIRE_ONNEWGLOBALHOOK,
-                                        options,
-                                        getter_AddRefs(holder));
+    RootedObject global(aCx);
+    rv = xpc::InitClassesWithNewWrappedGlobal(aCx,
+                                              static_cast<nsIGlobalObject*>(backstagePass),
+                                              nsContentUtils::GetSystemPrincipal(),
+                                              nsIXPConnect::DONT_FIRE_ONNEWGLOBALHOOK,
+                                              options,
+                                              &global);
     NS_ENSURE_SUCCESS_VOID(rv);
 
-    RootedObject global(aCx, holder->GetJSObject());
     NS_ENSURE_TRUE_VOID(global);
 
     backstagePass->SetGlobalObject(global);
 
     JSAutoCompartment ac(aCx, global);
     if (!JS_DefineFunctions(aCx, global, gGlobalFun) ||
         !JS_DefineProfilingFunctions(aCx, global)) {
         return;
--- a/js/xpconnect/src/XPCShellImpl.cpp
+++ b/js/xpconnect/src/XPCShellImpl.cpp
@@ -1301,24 +1301,23 @@ XRE_XPCShellMain(int argc, char** argv, 
 
         // Make the default XPCShell global use a fresh zone (rather than the
         // System Zone) to improve cross-zone test coverage.
         JS::CompartmentOptions options;
         options.creationOptions().setNewZoneInSystemZoneGroup();
         if (xpc::SharedMemoryEnabled())
             options.creationOptions().setSharedMemoryAndAtomicsEnabled(true);
         options.behaviors().setVersion(JSVERSION_DEFAULT);
-        nsCOMPtr<nsIXPConnectJSObjectHolder> holder;
-        rv = nsXPConnect::XPConnect()->
-            InitClassesWithNewWrappedGlobal(cx,
-                                            static_cast<nsIGlobalObject*>(backstagePass),
-                                            systemprincipal,
-                                            0,
-                                            options,
-                                            getter_AddRefs(holder));
+        JS::Rooted<JSObject*> glob(cx);
+        rv = xpc::InitClassesWithNewWrappedGlobal(cx,
+                                                  static_cast<nsIGlobalObject*>(backstagePass),
+                                                  systemprincipal,
+                                                  0,
+                                                  options,
+                                                  &glob);
         if (NS_FAILED(rv))
             return 1;
 
         // Initialize graphics prefs on the main thread, if not already done
         gfxPrefs::GetSingleton();
         // Initialize e10s check on the main thread, if not already done
         BrowserTabsRemoteAutostart();
 #ifdef XP_WIN
@@ -1338,17 +1337,16 @@ XRE_XPCShellMain(int argc, char** argv, 
 #endif
 #endif
 
 #ifdef MOZ_CODE_COVERAGE
         CodeCoverageHandler::Init();
 #endif
 
         {
-            JS::Rooted<JSObject*> glob(cx, holder->GetJSObject());
             if (!glob) {
                 return 1;
             }
 
             // Even if we're building in a configuration where source is
             // discarded, there's no reason to do that on XPCShell, and doing so
             // might break various automation scripts.
             JS::CompartmentBehaviorsRef(glob).setDiscardSource(false);
--- a/js/xpconnect/src/nsXPConnect.cpp
+++ b/js/xpconnect/src/nsXPConnect.cpp
@@ -555,29 +555,26 @@ InitGlobalObject(JSContext* aJSContext, 
     }
 
     if (!(aFlags & nsIXPConnect::DONT_FIRE_ONNEWGLOBALHOOK))
         JS_FireOnNewGlobalObject(aJSContext, aGlobal);
 
     return true;
 }
 
-} // namespace xpc
-
-NS_IMETHODIMP
-nsXPConnect::InitClassesWithNewWrappedGlobal(JSContext * aJSContext,
-                                             nsISupports* aCOMObj,
-                                             nsIPrincipal * aPrincipal,
-                                             uint32_t aFlags,
-                                             JS::CompartmentOptions& aOptions,
-                                             nsIXPConnectJSObjectHolder** _retval)
+nsresult
+InitClassesWithNewWrappedGlobal(JSContext* aJSContext,
+                                nsISupports* aCOMObj,
+                                nsIPrincipal* aPrincipal,
+                                uint32_t aFlags,
+                                JS::CompartmentOptions& aOptions,
+                                MutableHandleObject aNewGlobal)
 {
     MOZ_ASSERT(aJSContext, "bad param");
     MOZ_ASSERT(aCOMObj, "bad param");
-    MOZ_ASSERT(_retval, "bad param");
 
     // We pass null for the 'extra' pointer during global object creation, so
     // we need to have a principal.
     MOZ_ASSERT(aPrincipal);
 
     InitGlobalObjectOptions(aOptions, aPrincipal);
 
     // Call into XPCWrappedNative to make a new global object, scope, and global
@@ -593,20 +590,22 @@ nsXPConnect::InitClassesWithNewWrappedGl
 
     // Grab a copy of the global and enter its compartment.
     RootedObject global(aJSContext, wrappedGlobal->GetFlatJSObject());
     MOZ_ASSERT(JS_IsGlobalObject(global));
 
     if (!InitGlobalObject(aJSContext, global, aFlags))
         return UnexpectedFailure(NS_ERROR_FAILURE);
 
-    wrappedGlobal.forget(_retval);
+    aNewGlobal.set(global);
     return NS_OK;
 }
 
+} // namespace xpc
+
 static nsresult
 NativeInterface2JSObject(HandleObject aScope,
                          nsISupports* aCOMObj,
                          nsWrapperCache* aCache,
                          const nsIID * aIID,
                          bool aAllowWrapping,
                          MutableHandleValue aVal,
                          nsIXPConnectJSObjectHolder** aHolder)
--- a/js/xpconnect/src/xpcpublic.h
+++ b/js/xpconnect/src/xpcpublic.h
@@ -170,16 +170,39 @@ bool
 XrayAwareCalleeGlobalForSpecializedGetters(JSContext* cx,
                                            JS::Handle<JSObject*> thisObj,
                                            JS::MutableHandle<JSObject*> global);
 
 
 void
 TraceXPCGlobal(JSTracer* trc, JSObject* obj);
 
+/**
+ * Creates a new global object using the given aCOMObj as the global
+ * object. The object will be set up according to the flags (defined
+ * below). If you do not pass INIT_JS_STANDARD_CLASSES, then aCOMObj
+ * must implement nsIXPCScriptable so it can resolve the standard
+ * classes when asked by the JS engine.
+ *
+ * @param aJSContext the context to use while creating the global object.
+ * @param aCOMObj the native object that represents the global object.
+ * @param aPrincipal the principal of the code that will run in this
+ *                   compartment. Can be null if not on the main thread.
+ * @param aFlags one of the flags below specifying what options this
+ *               global object wants.
+ * @param aOptions JSAPI-specific options for the new compartment.
+ */
+nsresult
+InitClassesWithNewWrappedGlobal(JSContext* aJSContext,
+                                nsISupports* aCOMObj,
+                                nsIPrincipal* aPrincipal,
+                                uint32_t aFlags,
+                                JS::CompartmentOptions& aOptions,
+                                JS::MutableHandleObject aNewGlobal);
+
 } /* namespace xpc */
 
 namespace JS {
 
 struct RuntimeStats;
 
 } // namespace JS