Bug 1497707, part 1 - Get rid of xpcIJSModuleLoader. r=kmag
authorAndrew McCreight <continuation@gmail.com>
Tue, 23 Oct 2018 00:47:16 +0000
changeset 490832 26d5c8531a081dda749a6ddbcec3cb86fac39c78
parent 490831 9140cba9a8f80c392dd57d7a89aa85baca30547d
child 490833 bd13c578aceb65c8af0e760a2279db486b91e578
push id247
push userfmarier@mozilla.com
push dateSat, 27 Oct 2018 01:06:44 +0000
reviewerskmag
bugs1497707
milestone65.0a1
Bug 1497707, part 1 - Get rid of xpcIJSModuleLoader. r=kmag This interface is only used for a few testing functions. Just move them to Cu. Differential Revision: https://phabricator.services.mozilla.com/D8168
browser/base/content/test/performance/browser_startup.js
browser/base/content/test/performance/browser_startup_content.js
browser/components/tests/startupRecorder.js
js/xpconnect/idl/moz.build
js/xpconnect/idl/xpcIJSModuleLoader.idl
js/xpconnect/idl/xpccomponents.idl
js/xpconnect/loader/mozJSComponentLoader.cpp
js/xpconnect/loader/mozJSComponentLoader.h
js/xpconnect/src/XPCComponents.cpp
toolkit/components/extensions/test/xpcshell/test_ext_startup_perf.js
--- a/browser/base/content/test/performance/browser_startup.js
+++ b/browser/base/content/test/performance/browser_startup.js
@@ -152,33 +152,32 @@ add_task(async function() {
        "the startup recorder component shouldn't exist in this non-nightly/non-devedition/" +
        "non-debug build.");
     return;
   }
 
   let startupRecorder = Cc["@mozilla.org/test/startuprecorder;1"].getService().wrappedJSObject;
   await startupRecorder.done;
 
-  let loader = Cc["@mozilla.org/moz/jsloader;1"].getService(Ci.xpcIJSModuleLoader);
   let componentStacks = new Map();
   let data = Cu.cloneInto(startupRecorder.data.code, {});
   // Keep only the file name for components, as the path is an absolute file
   // URL rather than a resource:// URL like for modules.
   for (let phase in data) {
     data[phase].components =
       data[phase].components.map(uri => {
         let fileName = uri.replace(/.*\//, "");
-        componentStacks.set(fileName, loader.getComponentLoadStack(uri));
+        componentStacks.set(fileName, Cu.getComponentLoadStack(uri));
         return fileName;
       }).filter(c => c != "startupRecorder.js");
   }
 
   function printStack(scriptType, name) {
     if (scriptType == "modules")
-      info(loader.getModuleImportStack(name));
+      info(Cu.getModuleImportStack(name));
     else if (scriptType == "components")
       info(componentStacks.get(name));
   }
 
   // This block only adds debug output to help find the next bugs to file,
   // it doesn't contribute to the actual test.
   SimpleTest.requestCompleteLog();
   let previous;
--- a/browser/base/content/test/performance/browser_startup_content.js
+++ b/browser/base/content/test/performance/browser_startup_content.js
@@ -105,27 +105,26 @@ add_task(async function() {
 
   // Load a custom frame script to avoid using ContentTask which loads Task.jsm
   mm.loadFrameScript("data:text/javascript,(" + function() {
     /* eslint-env mozilla/frame-script */
     const Cm = Components.manager;
     Cm.QueryInterface(Ci.nsIServiceManager);
     ChromeUtils.import("resource://gre/modules/AppConstants.jsm");
     let collectStacks = AppConstants.NIGHTLY_BUILD || AppConstants.DEBUG;
-    let loader = Cc["@mozilla.org/moz/jsloader;1"].getService(Ci.xpcIJSModuleLoader);
     let components = {};
-    for (let component of loader.loadedComponents()) {
+    for (let component of Cu.loadedComponents()) {
       /* Keep only the file name for components, as the path is an absolute file
          URL rather than a resource:// URL like for modules. */
       components[component.replace(/.*\//, "")] =
-        collectStacks ? loader.getComponentLoadStack(component) : "";
+        collectStacks ? Cu.getComponentLoadStack(component) : "";
     }
     let modules = {};
-    for (let module of loader.loadedModules()) {
-      modules[module] = collectStacks ? loader.getModuleImportStack(module) : "";
+    for (let module of Cu.loadedModules()) {
+      modules[module] = collectStacks ? Cu.getModuleImportStack(module) : "";
     }
     let services = {};
     for (let contractID of Object.keys(Cc)) {
       try {
         if (Cm.isServiceInstantiatedByContractID(contractID, Ci.nsISupports)) {
           services[contractID] = "";
         }
       } catch (e) {}
--- a/browser/components/tests/startupRecorder.js
+++ b/browser/components/tests/startupRecorder.js
@@ -38,17 +38,16 @@ let afterPaintListener = () => {
   * loaded at each of these points.
   * The records are meant to be used by startup tests in
   * browser/base/content/test/performance
   * This component only exists in nightly and debug builds, it doesn't ship in
   * our release builds.
   */
 function startupRecorder() {
   this.wrappedJSObject = this;
-  this.loader = Cc["@mozilla.org/moz/jsloader;1"].getService(Ci.xpcIJSModuleLoader);
   this.data = {
     images: {
       "image-drawing": new Set(),
       "image-loading": new Set(),
     },
     code: {},
     prefStats: {},
   };
@@ -59,18 +58,18 @@ startupRecorder.prototype = {
 
   QueryInterface: ChromeUtils.generateQI([Ci.nsIObserver]),
 
   record(name) {
     if (!Services.prefs.getBoolPref("browser.startup.record", false))
       return;
 
     this.data.code[name] = {
-      components: this.loader.loadedComponents(),
-      modules: this.loader.loadedModules(),
+      components: Cu.loadedComponents(),
+      modules: Cu.loadedModules(),
       services: Object.keys(Cc).filter(c => {
         try {
           return Cm.isServiceInstantiatedByContractID(c, Ci.nsISupports);
         } catch (e) {
           return false;
         }
       }),
     };
--- a/js/xpconnect/idl/moz.build
+++ b/js/xpconnect/idl/moz.build
@@ -5,15 +5,14 @@
 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
 
 XPIDL_SOURCES += [
     'mozIJSSubScriptLoader.idl',
     'nsIXPConnect.idl',
     'nsIXPCScriptable.idl',
     'xpccomponents.idl',
     'xpcIJSGetFactory.idl',
-    'xpcIJSModuleLoader.idl',
     'xpcIJSWeakReference.idl',
     'xpcjsid.idl',
 ]
 
 XPIDL_MODULE = 'xpconnect'
 
deleted file mode 100644
--- a/js/xpconnect/idl/xpcIJSModuleLoader.idl
+++ /dev/null
@@ -1,24 +0,0 @@
-/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
-/* This Source Code Form is subject to the terms of the Mozilla Public
- * License, v. 2.0. If a copy of the MPL was not distributed with this
- * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
-
-#include "nsISupports.idl"
-
-[scriptable, builtinclass, uuid(4f94b21f-2920-4bd9-8251-5fb60fb054b2)]
-interface xpcIJSModuleLoader : nsISupports
-{
-  // These functions are for startup testing purposes. They are not expected
-  // to be used for production code.
-  void loadedModules([optional] out unsigned long length,
-                     [retval, array, size_is(length)] out string aModules);
-
-  void loadedComponents([optional] out unsigned long length,
-                        [retval, array, size_is(length)] out string aComponents);
-
-  // These 2 functions will only return useful values if the
-  // "browser.startup.record" preference was true at the time the JS file
-  // was loaded.
-  ACString getModuleImportStack(in AUTF8String aLocation);
-  ACString getComponentLoadStack(in AUTF8String aLocation);
-};
--- a/js/xpconnect/idl/xpccomponents.idl
+++ b/js/xpconnect/idl/xpccomponents.idl
@@ -733,16 +733,30 @@ interface nsIXPCComponents_Utils : nsISu
     /* Create a loadcontext object. */
     nsILoadContext createLoadContext();
 
     /* Create a private loadcontext object. */
     nsILoadContext createPrivateLoadContext();
 
     /* Create a persistent property object. */
     nsIPersistentProperties createPersistentProperties();
+
+    // These functions are for startup testing purposes. They are not expected
+    // to be used for production code.
+    void loadedModules([optional] out unsigned long length,
+                       [retval, array, size_is(length)] out string aModules);
+
+    void loadedComponents([optional] out unsigned long length,
+                          [retval, array, size_is(length)] out string aComponents);
+
+    // These 2 functions will only return useful values if the
+    // "browser.startup.record" preference was true at the time the JS file
+    // was loaded.
+    ACString getModuleImportStack(in AUTF8String aLocation);
+    ACString getComponentLoadStack(in AUTF8String aLocation);
 };
 
 /**
 * Interface for the 'Components' object.
 *
 * The first interface contains things that are available to non-chrome XBL code
 * that runs in a scope with an ExpandedPrincipal. The second interface
 * includes members that are only exposed to chrome.
--- a/js/xpconnect/loader/mozJSComponentLoader.cpp
+++ b/js/xpconnect/loader/mozJSComponentLoader.cpp
@@ -311,17 +311,16 @@ mozJSComponentLoader::~mozJSComponentLoa
     sSelf = nullptr;
 }
 
 mozJSComponentLoader*
 mozJSComponentLoader::sSelf;
 
 NS_IMPL_ISUPPORTS(mozJSComponentLoader,
                   mozilla::ModuleLoader,
-                  xpcIJSModuleLoader,
                   nsIObserver)
 
 nsresult
 mozJSComponentLoader::ReallyInit()
 {
     MOZ_ASSERT(!mInitialized);
 
     const char* shareGlobal = PR_GetEnv("MOZ_LOADER_SHARE_GLOBAL");
@@ -1059,47 +1058,45 @@ mozJSComponentLoader::IsModuleLoaded(con
     ComponentLoaderInfo info(aLocation);
     rv = info.EnsureKey();
     NS_ENSURE_SUCCESS(rv, rv);
 
     *retval = !!mImports.Get(info.Key());
     return NS_OK;
 }
 
-NS_IMETHODIMP mozJSComponentLoader::LoadedModules(uint32_t* length,
-                                                  char*** aModules)
+void
+mozJSComponentLoader::LoadedModules(uint32_t* length,
+                                    char*** aModules)
 {
     char** modules = new char*[mImports.Count()];
     *length = mImports.Count();
     *aModules = modules;
 
     for (auto iter = mImports.Iter(); !iter.Done(); iter.Next()) {
         *modules = NS_xstrdup(iter.Data()->location);
         modules++;
     }
-
-    return NS_OK;
 }
 
-NS_IMETHODIMP mozJSComponentLoader::LoadedComponents(uint32_t* length,
-                                                     char*** aComponents)
+void
+mozJSComponentLoader::LoadedComponents(uint32_t* length,
+                                       char*** aComponents)
 {
     char** comp = new char*[mModules.Count()];
     *length = mModules.Count();
     *aComponents = comp;
 
     for (auto iter = mModules.Iter(); !iter.Done(); iter.Next()) {
         *comp = NS_xstrdup(iter.Data()->location);
         comp++;
     }
-
-    return NS_OK;
 }
 
-NS_IMETHODIMP
+nsresult
 mozJSComponentLoader::GetModuleImportStack(const nsACString& aLocation,
                                            nsACString& retval)
 {
 #ifdef STARTUP_RECORDER_ENABLED
     MOZ_ASSERT(nsContentUtils::IsCallerChrome());
     MOZ_ASSERT(mInitialized);
 
     ComponentLoaderInfo info(aLocation);
@@ -1113,17 +1110,17 @@ mozJSComponentLoader::GetModuleImportSta
 
     retval = mod->importStack;
     return NS_OK;
 #else
     return NS_ERROR_NOT_IMPLEMENTED;
 #endif
 }
 
-NS_IMETHODIMP
+nsresult
 mozJSComponentLoader::GetComponentLoadStack(const nsACString& aLocation,
                                             nsACString& retval)
 {
 #ifdef STARTUP_RECORDER_ENABLED
     MOZ_ASSERT(nsContentUtils::IsCallerChrome());
     MOZ_ASSERT(mInitialized);
 
     ComponentLoaderInfo info(aLocation);
--- a/js/xpconnect/loader/mozJSComponentLoader.h
+++ b/js/xpconnect/loader/mozJSComponentLoader.h
@@ -9,17 +9,16 @@
 
 #include "mozilla/dom/ScriptSettings.h"
 #include "mozilla/MemoryReporting.h"
 #include "mozilla/ModuleLoader.h"
 #include "nsAutoPtr.h"
 #include "nsISupports.h"
 #include "nsIObserver.h"
 #include "nsIURI.h"
-#include "xpcIJSModuleLoader.h"
 #include "nsClassHashtable.h"
 #include "nsDataHashtable.h"
 #include "jsapi.h"
 
 #include "xpcIJSGetFactory.h"
 #include "xpcpublic.h"
 
 class nsIFile;
@@ -37,26 +36,29 @@ namespace mozilla {
     { 0xbb, 0xef, 0xf0, 0xcc, 0xb5, 0xfa, 0x64, 0xb6 }}
 #define MOZJSCOMPONENTLOADER_CONTRACTID "@mozilla.org/moz/jsloader;1"
 
 #if defined(NIGHTLY_BUILD) || defined(MOZ_DEV_EDITION) || defined(DEBUG)
 #define STARTUP_RECORDER_ENABLED
 #endif
 
 class mozJSComponentLoader final : public mozilla::ModuleLoader,
-                                   public xpcIJSModuleLoader,
                                    public nsIObserver
 {
  public:
     NS_DECL_ISUPPORTS
-    NS_DECL_XPCIJSMODULELOADER
     NS_DECL_NSIOBSERVER
 
     mozJSComponentLoader();
 
+    void LoadedModules(uint32_t* aLength, char*** aModules);
+    void LoadedComponents(uint32_t* aLength, char*** aComponents);
+    nsresult GetModuleImportStack(const nsACString& aLocation, nsACString& aRetval);
+    nsresult GetComponentLoadStack(const nsACString& aLocation, nsACString& aRetval);
+
     // ModuleLoader
     const mozilla::Module* LoadModule(mozilla::FileLocation& aFile) override;
 
     void FindTargetObject(JSContext* aCx,
                           JS::MutableHandleObject aTargetObject);
 
     static already_AddRefed<mozJSComponentLoader> GetOrCreate();
 
--- a/js/xpconnect/src/XPCComponents.cpp
+++ b/js/xpconnect/src/XPCComponents.cpp
@@ -3265,16 +3265,46 @@ NS_IMETHODIMP
 nsXPCComponents_Utils::CreatePersistentProperties(nsIPersistentProperties** aPersistentProperties)
 {
     NS_ENSURE_ARG_POINTER(aPersistentProperties);
     nsCOMPtr<nsIPersistentProperties> props = new nsPersistentProperties();
     props.forget(aPersistentProperties);
     return NS_OK;
 }
 
+NS_IMETHODIMP
+nsXPCComponents_Utils::LoadedModules(uint32_t* aLength,
+                                     char*** aModules)
+{
+    mozJSComponentLoader::Get()->LoadedModules(aLength, aModules);
+    return NS_OK;
+}
+
+NS_IMETHODIMP
+nsXPCComponents_Utils::LoadedComponents(uint32_t* aLength,
+                                        char*** aComponents)
+{
+    mozJSComponentLoader::Get()->LoadedComponents(aLength, aComponents);
+    return NS_OK;
+}
+
+NS_IMETHODIMP
+nsXPCComponents_Utils::GetModuleImportStack(const nsACString& aLocation,
+                                            nsACString& aRetval)
+{
+    return mozJSComponentLoader::Get()->GetModuleImportStack(aLocation, aRetval);
+}
+
+NS_IMETHODIMP
+nsXPCComponents_Utils::GetComponentLoadStack(const nsACString& aLocation,
+                                             nsACString& aRetval)
+{
+    return mozJSComponentLoader::Get()->GetComponentLoadStack(aLocation, aRetval);
+}
+
 /***************************************************************************/
 /***************************************************************************/
 /***************************************************************************/
 
 
 nsXPCComponentsBase::nsXPCComponentsBase(XPCWrappedNativeScope* aScope)
     :   mScope(aScope)
 {
--- a/toolkit/components/extensions/test/xpcshell/test_ext_startup_perf.js
+++ b/toolkit/components/extensions/test/xpcshell/test_ext_startup_perf.js
@@ -42,17 +42,16 @@ add_task(async function test_loaded_scri
   const loadedAPIs = Array.from(apiManager.modules.values())
                           .filter(m => m.loaded || m.asyncLoaded)
                           .map(m => m.namespaceName);
 
   deepEqual(loadedAPIs.sort(), STARTUP_APIS,
             "No extra APIs should be loaded at startup for a simple extension");
 
 
-  const loader = Cc["@mozilla.org/moz/jsloader;1"].getService(Ci.xpcIJSModuleLoader);
-  let loadedModules = loader.loadedModules()
-                            .filter(url => url.startsWith("resource://gre/modules/Extension"));
+  let loadedModules = Cu.loadedModules()
+                        .filter(url => url.startsWith("resource://gre/modules/Extension"));
 
   deepEqual(loadedModules.sort(), STARTUP_MODULES.sort(),
             "No extra extension modules should be loaded at startup for a simple extension");
 
   await extension.unload();
 });