Bug 1363544 - Call into nsXPConnect directly in the loader and XPCShellImpl. r=mrbkap draft
authorAndrew McCreight <continuation@gmail.com>
Tue, 09 May 2017 14:08:15 -0700
changeset 576386 610cd6bb6794c3ef01d048737bb0c0f7d6e1d705
parent 576385 df5273354784a4e198f6e6c169f590a8056b00bb
child 576387 afe6cc4ebd2564dda31ede8aedf5ad16343015d8
push id58351
push userbmo:continuation@gmail.com
push dateThu, 11 May 2017 18:06:19 +0000
reviewersmrbkap
bugs1363544
milestone55.0a1
Bug 1363544 - Call into nsXPConnect directly in the loader and XPCShellImpl. r=mrbkap MozReview-Commit-ID: 7ZnHdAG1Jmk
js/xpconnect/loader/mozJSComponentLoader.cpp
js/xpconnect/src/XPCShellImpl.cpp
--- a/js/xpconnect/loader/mozJSComponentLoader.cpp
+++ b/js/xpconnect/loader/mozJSComponentLoader.cpp
@@ -54,17 +54,16 @@ using namespace mozilla::scache;
 using namespace xpc;
 using namespace JS;
 
 // This JSClass exists to trick silly code that expects toString()ing the
 // global in a component scope to return something with "BackstagePass" in it
 // to continue working.
 static const JSClass kFakeBackstagePassJSClass = { "FakeBackstagePass" };
 
-static const char kXPConnectServiceContractID[] = "@mozilla.org/js/xpc/XPConnect;1";
 static const char kObserverServiceContractID[] = "@mozilla.org/observer-service;1";
 static const char kJSCachePrefix[] = "jsloader";
 
 /**
  * Buffer sizes for serialization and deserialization of scripts.
  * FIXME: bug #411579 (tune this macro!) Last updated: Jan 2008
  */
 #define XPC_SERIALIZATION_BUFFER_SIZE   (64 * 1024)
@@ -376,21 +375,16 @@ mozJSComponentLoader::LoadModule(FileLoc
     nsAutoPtr<ModuleEntry> entry(new ModuleEntry(RootingContext::get(cx)));
     RootedValue dummy(cx);
     rv = ObjectForLocation(info, file, &entry->obj, &entry->thisObjectKey,
                            &entry->location, false, &dummy);
     if (NS_FAILED(rv)) {
         return nullptr;
     }
 
-    nsCOMPtr<nsIXPConnect> xpc = do_GetService(kXPConnectServiceContractID,
-                                               &rv);
-    if (NS_FAILED(rv))
-        return nullptr;
-
     nsCOMPtr<nsIComponentManager> cm;
     rv = NS_GetComponentManager(getter_AddRefs(cm));
     if (NS_FAILED(rv))
         return nullptr;
 
     JSAutoCompartment ac(cx, entry->obj);
     RootedObject entryObj(cx, entry->obj);
 
@@ -415,18 +409,19 @@ mozJSComponentLoader::LoadModule(FileLoc
 
     RootedObject jsGetFactoryObj(cx);
     if (!JS_ValueToObject(cx, NSGetFactory_val, &jsGetFactoryObj) ||
         !jsGetFactoryObj) {
         /* XXX report error properly */
         return nullptr;
     }
 
-    rv = xpc->WrapJS(cx, jsGetFactoryObj,
-                     NS_GET_IID(xpcIJSGetFactory), getter_AddRefs(entry->getfactoryobj));
+    rv = nsXPConnect::XPConnect()->WrapJS(cx, jsGetFactoryObj,
+                                          NS_GET_IID(xpcIJSGetFactory),
+                                          getter_AddRefs(entry->getfactoryobj));
     if (NS_FAILED(rv)) {
         /* XXX report error properly */
 #ifdef DEBUG
         fprintf(stderr, "mJCL: couldn't get nsIModule from jsval\n");
 #endif
         return nullptr;
     }
 
@@ -522,19 +517,16 @@ mozJSComponentLoader::PrepareObjectForLo
                                                bool* aRealFile)
 {
     nsCOMPtr<nsIXPConnectJSObjectHolder> holder;
     if (aReuseLoaderGlobal) {
         holder = mLoaderGlobal;
     }
 
     nsresult rv = NS_OK;
-    nsCOMPtr<nsIXPConnect> xpc =
-        do_GetService(kXPConnectServiceContractID, &rv);
-    NS_ENSURE_SUCCESS(rv, nullptr);
     bool createdNewGlobal = false;
 
     if (!mLoaderGlobal) {
         RefPtr<BackstagePass> backstagePass;
         rv = NS_NewBackstagePass(getter_AddRefs(backstagePass));
         NS_ENSURE_SUCCESS(rv, nullptr);
 
         CompartmentOptions options;
@@ -546,22 +538,23 @@ mozJSComponentLoader::PrepareObjectForLo
         options.behaviors().setVersion(JSVERSION_LATEST);
 
         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
-        rv = xpc->InitClassesWithNewWrappedGlobal(aCx,
-                                                  static_cast<nsIGlobalObject*>(backstagePass),
-                                                  mSystemPrincipal,
-                                                  nsIXPConnect::DONT_FIRE_ONNEWGLOBALHOOK,
-                                                  options,
-                                                  getter_AddRefs(holder));
+        rv = nsXPConnect::XPConnect()->
+            InitClassesWithNewWrappedGlobal(aCx,
+                                            static_cast<nsIGlobalObject*>(backstagePass),
+                                            mSystemPrincipal,
+                                            nsIXPConnect::DONT_FIRE_ONNEWGLOBALHOOK,
+                                            options,
+                                            getter_AddRefs(holder));
         NS_ENSURE_SUCCESS(rv, nullptr);
         createdNewGlobal = true;
 
         RootedObject global(aCx, holder->GetJSObject());
         NS_ENSURE_TRUE(global, nullptr);
 
         backstagePass->SetGlobalObject(global);
 
@@ -600,19 +593,19 @@ mozJSComponentLoader::PrepareObjectForLo
     }
 
     if (testFile) {
         *aRealFile = true;
 
         if (XRE_IsParentProcess()) {
             RootedObject locationObj(aCx);
 
-            rv = xpc->WrapNative(aCx, obj, aComponentFile,
-                                 NS_GET_IID(nsIFile),
-                                 locationObj.address());
+            rv = nsXPConnect::XPConnect()->WrapNative(aCx, obj, aComponentFile,
+                                                      NS_GET_IID(nsIFile),
+                                                      locationObj.address());
             NS_ENSURE_SUCCESS(rv, nullptr);
             NS_ENSURE_TRUE(locationObj, nullptr);
 
             if (!JS_DefineProperty(aCx, obj, "__LOCATION__", locationObj, 0))
                 return nullptr;
         }
     }
 
--- a/js/xpconnect/src/XPCShellImpl.cpp
+++ b/js/xpconnect/src/XPCShellImpl.cpp
@@ -8,17 +8,16 @@
 #include "jsapi.h"
 #include "jsfriendapi.h"
 #include "jsprf.h"
 #include "mozilla/ChaosMode.h"
 #include "mozilla/dom/ScriptSettings.h"
 #include "mozilla/Preferences.h"
 #include "nsServiceManagerUtils.h"
 #include "nsComponentManagerUtils.h"
-#include "nsIXPConnect.h"
 #include "nsIServiceManager.h"
 #include "nsIFile.h"
 #include "nsString.h"
 #include "nsIDirectoryService.h"
 #include "nsDirectoryServiceDefs.h"
 #include "nsAppDirectoryServiceDefs.h"
 #include "nscore.h"
 #include "nsArrayEnumerator.h"
@@ -115,18 +114,16 @@ public:
     }
 
     ~AutoAudioSession() {
         widget::StopAudioSession();
     }
 };
 #endif
 
-static const char kXPConnectServiceContractID[] = "@mozilla.org/js/xpc/XPConnect;1";
-
 #define EXITCODE_RUNTIME_ERROR 3
 #define EXITCODE_FILE_NOT_FOUND 4
 
 static FILE* gOutFile = nullptr;
 static FILE* gErrFile = nullptr;
 static FILE* gInFile = nullptr;
 
 static int gExitCode = 0;
@@ -146,20 +143,16 @@ GetLocationProperty(JSContext* cx, unsig
         return false;
     }
 #if !defined(XP_WIN) && !defined(XP_UNIX)
     //XXX: your platform should really implement this
     return false;
 #else
     JS::AutoFilename filename;
     if (JS::DescribeScriptedCaller(cx, &filename) && filename.get()) {
-        nsresult rv;
-        nsCOMPtr<nsIXPConnect> xpc =
-            do_GetService(kXPConnectServiceContractID, &rv);
-
 #if defined(XP_WIN)
         // convert from the system codepage to UTF-16
         int bufferSize = MultiByteToWideChar(CP_ACP, 0, filename.get(),
                                              -1, nullptr, 0);
         nsAutoString filenameString;
         filenameString.SetLength(bufferSize);
         MultiByteToWideChar(CP_ACP, 0, filename.get(),
                             -1, (LPWSTR)filenameString.BeginWriting(),
@@ -177,20 +170,18 @@ GetLocationProperty(JSContext* cx, unsig
                 *start = L'\\';
             start++;
         }
 #elif defined(XP_UNIX)
         NS_ConvertUTF8toUTF16 filenameString(filename.get());
 #endif
 
         nsCOMPtr<nsIFile> location;
-        if (NS_SUCCEEDED(rv)) {
-            rv = NS_NewLocalFile(filenameString,
-                                 false, getter_AddRefs(location));
-        }
+        nsresult rv = NS_NewLocalFile(filenameString,
+                                      false, getter_AddRefs(location));
 
         if (!location && gWorkingDirectory) {
             // could be a relative path, try appending it to the cwd
             // and then normalize
             nsAutoString absolutePath(*gWorkingDirectory);
             absolutePath.Append(filenameString);
 
             rv = NS_NewLocalFile(absolutePath,
@@ -199,18 +190,20 @@ GetLocationProperty(JSContext* cx, unsig
 
         if (location) {
             bool symlink;
             // don't normalize symlinks, because that's kind of confusing
             if (NS_SUCCEEDED(location->IsSymlink(&symlink)) &&
                 !symlink)
                 location->Normalize();
             RootedObject locationObj(cx);
-            rv = xpc->WrapNative(cx, &args.thisv().toObject(), location,
-                                 NS_GET_IID(nsIFile), locationObj.address());
+            rv = nsXPConnect::XPConnect()->WrapNative(cx, &args.thisv().toObject(),
+                                                      location,
+                                                      NS_GET_IID(nsIFile),
+                                                      locationObj.address());
             if (NS_SUCCEEDED(rv) && locationObj) {
                 args.rval().setObject(*locationObj);
             }
         }
     }
 
     return true;
 #endif
@@ -422,19 +415,17 @@ DumpXPC(JSContext* cx, unsigned argc, Va
     JS::CallArgs args = CallArgsFromVp(argc, vp);
 
     uint16_t depth = 2;
     if (args.length() > 0) {
         if (!JS::ToUint16(cx, args[0], &depth))
             return false;
     }
 
-    nsCOMPtr<nsIXPConnect> xpc = do_GetService(nsIXPConnect::GetCID());
-    if (xpc)
-        xpc->DebugDump(int16_t(depth));
+    nsXPConnect::XPConnect()->DebugDump(int16_t(depth));
     args.rval().setUndefined();
     return true;
 }
 
 static bool
 GC(JSContext* cx, unsigned argc, Value* vp)
 {
     CallArgs args = CallArgsFromVp(argc, vp);
@@ -1400,22 +1391,16 @@ XRE_XPCShellMain(int argc, char** argv, 
         sScriptedInterruptCallback->init(cx, UndefinedValue());
 
         JS_AddInterruptCallback(cx, XPCShellInterruptCallback);
 
         argc--;
         argv++;
         ProcessArgsForCompartment(cx, argv, argc);
 
-        nsCOMPtr<nsIXPConnect> xpc = do_GetService(nsIXPConnect::GetCID());
-        if (!xpc) {
-            printf("failed to get nsXPConnect service!\n");
-            return 1;
-        }
-
         nsCOMPtr<nsIPrincipal> systemprincipal;
         // Fetch the system principal and store it away in a global, to use for
         // script compilation in Load() and ProcessFile() (including interactive
         // eval loop)
         {
 
             nsCOMPtr<nsIScriptSecurityManager> securityManager =
                 do_GetService(NS_SCRIPTSECURITYMANAGER_CONTRACTID, &rv);
@@ -1449,22 +1434,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_LATEST);
         nsCOMPtr<nsIXPConnectJSObjectHolder> holder;
-        rv = xpc->InitClassesWithNewWrappedGlobal(cx,
-                                                  static_cast<nsIGlobalObject*>(backstagePass),
-                                                  systemprincipal,
-                                                  0,
-                                                  options,
-                                                  getter_AddRefs(holder));
+        rv = nsXPConnect::XPConnect()->
+            InitClassesWithNewWrappedGlobal(cx,
+                                            static_cast<nsIGlobalObject*>(backstagePass),
+                                            systemprincipal,
+                                            0,
+                                            options,
+                                            getter_AddRefs(holder));
         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