Bug 1497707, part 4 - Only support loading JS files in the component manager. r=froydnj
authorAndrew McCreight <continuation@gmail.com>
Tue, 23 Oct 2018 00:47:38 +0000
changeset 490835 e44f3f8cde9c63cc407b959f816d2999a0200eec
parent 490834 3db6926e3e4441e726a4163f2a60da58584f76b6
child 490836 ded047a09c690d95cbe1a2c8babdfa8d917cf29b
push id247
push userfmarier@mozilla.com
push dateSat, 27 Oct 2018 01:06:44 +0000
reviewersfroydnj
bugs1497707
milestone65.0a1
Bug 1497707, part 4 - Only support loading JS files in the component manager. r=froydnj JS is the only file extension actually supported, and there are a few layers of cruft that can be eliminated if we specialize it. This eliminates one XPCOM registration of the JS component loader. Depends on D8170 Differential Revision: https://phabricator.services.mozilla.com/D8171
js/xpconnect/src/XPCModule.h
layout/build/nsLayoutModule.cpp
xpcom/build/XPCOMInit.cpp
xpcom/components/moz.build
xpcom/components/nsComponentManager.cpp
xpcom/components/nsComponentManager.h
--- a/js/xpconnect/src/XPCModule.h
+++ b/js/xpconnect/src/XPCModule.h
@@ -34,13 +34,10 @@ NS_DEFINE_NAMED_CID(MOZ_JSSUBSCRIPTLOADE
   { &kNS_JS_ID_CID, false, nullptr,  nsJSIDConstructor },                     \
   { &kMOZJSCOMPONENTLOADER_CID, false, nullptr, mozJSComponentLoaderConstructor },\
   { &kMOZ_JSSUBSCRIPTLOADER_CID, false, nullptr, mozJSSubScriptLoaderConstructor },
 
 #define XPCONNECT_CONTRACTS                                                   \
   { MOZJSCOMPONENTLOADER_CONTRACTID, &kMOZJSCOMPONENTLOADER_CID },            \
   { MOZJSSUBSCRIPTLOADER_CONTRACTID, &kMOZ_JSSUBSCRIPTLOADER_CID },
 
-#define XPCONNECT_CATEGORIES \
-  { "module-loader", "js", MOZJSCOMPONENTLOADER_CONTRACTID },
-
 nsresult xpcModuleCtor();
 void xpcModuleDtor();
--- a/layout/build/nsLayoutModule.cpp
+++ b/layout/build/nsLayoutModule.cpp
@@ -663,17 +663,16 @@ static const mozilla::Module::ContractID
   { PRESENTATION_DEVICE_MANAGER_CONTRACTID, &kPRESENTATION_DEVICE_MANAGER_CID },
   { PRESENTATION_TCP_SESSION_TRANSPORT_CONTRACTID, &kPRESENTATION_TCP_SESSION_TRANSPORT_CID },
   { "@mozilla.org/text-input-processor;1", &kTEXT_INPUT_PROCESSOR_CID },
   { NS_SCRIPTERROR_CONTRACTID, &kNS_SCRIPTERROR_CID },
   { nullptr }
 };
 
 static const mozilla::Module::CategoryEntry kLayoutCategories[] = {
-  XPCONNECT_CATEGORIES
   { "content-policy", NS_DATADOCUMENTCONTENTPOLICY_CONTRACTID, NS_DATADOCUMENTCONTENTPOLICY_CONTRACTID },
   { "content-policy", NS_NODATAPROTOCOLCONTENTPOLICY_CONTRACTID, NS_NODATAPROTOCOLCONTENTPOLICY_CONTRACTID },
   { "content-policy", "CSPService", CSPSERVICE_CONTRACTID },
   { "content-policy", NS_MIXEDCONTENTBLOCKER_CONTRACTID, NS_MIXEDCONTENTBLOCKER_CONTRACTID },
   { "net-channel-event-sinks", "CSPService", CSPSERVICE_CONTRACTID },
   { "net-channel-event-sinks", NS_MIXEDCONTENTBLOCKER_CONTRACTID, NS_MIXEDCONTENTBLOCKER_CONTRACTID },
   { "app-startup", "Script Security Manager", "service," NS_SCRIPTSECURITYMANAGER_CONTRACTID },
   { "app-startup", "Push Notifier", "service," PUSHNOTIFIER_CONTRACTID },
--- a/xpcom/build/XPCOMInit.cpp
+++ b/xpcom/build/XPCOMInit.cpp
@@ -697,29 +697,30 @@ NS_InitXPCOM2(nsIServiceManager** aResul
     NS_RELEASE(nsComponentManagerImpl::gComponentManager);
     return rv;
   }
 
   if (aResult) {
     NS_ADDREF(*aResult = nsComponentManagerImpl::gComponentManager);
   }
 
+  // Ensure that XPConnect etc. is started up before we might call
+  // into JS. For instance, on Android nsIDirectoryServiceProvider is
+  // written in JS.
+  nsCOMPtr<nsISupports> componentLoader =
+    do_GetService("@mozilla.org/moz/jsloader;1");
+
   // After autoreg, but before we actually instantiate any components,
   // add any services listed in the "xpcom-directory-providers" category
   // to the directory service.
   nsDirectoryService::gService->RegisterCategoryProviders();
 
   // Init SharedThreadPool (which needs the service manager).
   SharedThreadPool::InitStatics();
 
-  // Force layout to spin up so that nsContentUtils is available for cx stack
-  // munging.
-  nsCOMPtr<nsISupports> componentLoader =
-    do_GetService("@mozilla.org/moz/jsloader;1");
-
   mozilla::ScriptPreloader::GetSingleton();
   mozilla::scache::StartupCache::GetSingleton();
   mozilla::AvailableMemoryTracker::Init();
 
   // Notify observers of xpcom autoregistration start
   NS_CreateServicesFromCategory(NS_XPCOM_STARTUP_CATEGORY,
                                 nullptr,
                                 NS_XPCOM_STARTUP_OBSERVER_ID);
--- a/xpcom/components/moz.build
+++ b/xpcom/components/moz.build
@@ -42,13 +42,14 @@ UNIFIED_SOURCES += [
 FINAL_LIBRARY = 'xul'
 
 LOCAL_INCLUDES += [
     '!..',
     '../base',
     '../build',
     '../ds',
     '/chrome',
+    '/js/xpconnect/loader',
     '/modules/libjar',
 ]
 
 if 'gtk' in CONFIG['MOZ_WIDGET_TOOLKIT']:
     CXXFLAGS += CONFIG['TK_CFLAGS']
--- a/xpcom/components/nsComponentManager.cpp
+++ b/xpcom/components/nsComponentManager.cpp
@@ -33,16 +33,17 @@
 #include "nsThreadUtils.h"
 #include "prthread.h"
 #include "private/pprthred.h"
 #include "nsTArray.h"
 #include "prio.h"
 #include "ManifestParser.h"
 #include "nsNetUtil.h"
 #include "mozilla/Services.h"
+#include "mozJSComponentLoader.h"
 
 #include "mozilla/GenericFactory.h"
 #include "nsSupportsPrimitives.h"
 #include "nsArray.h"
 #include "nsIMutableArray.h"
 #include "nsArrayEnumerator.h"
 #include "nsStringEnumerator.h"
 #include "mozilla/FileUtils.h"
@@ -769,40 +770,31 @@ nsComponentManagerImpl::RereadChromeMani
 
   nsCOMPtr<nsIObserverService> obs = services::GetObserverService();
   if (obs) {
     obs->NotifyObservers(nullptr, "chrome-manifests-loaded", nullptr);
   }
 }
 
 bool
-nsComponentManagerImpl::KnownModule::EnsureLoader()
-{
-  if (!mLoader) {
-    nsCString extension;
-    mFile.GetURIString(extension);
-    CutExtension(extension);
-    mLoader =
-      nsComponentManagerImpl::gComponentManager->LoaderForExtension(extension);
-  }
-  return !!mLoader;
-}
-
-bool
 nsComponentManagerImpl::KnownModule::Load()
 {
   if (mFailed) {
     return false;
   }
   if (!mModule) {
-    if (!EnsureLoader()) {
+    nsCString extension;
+    mFile.GetURIString(extension);
+    CutExtension(extension);
+    if (!extension.Equals("js")) {
       return false;
     }
 
-    mModule = mLoader->LoadModule(mFile);
+    RefPtr<mozJSComponentLoader> loader = mozJSComponentLoader::GetOrCreate();
+    mModule = loader->LoadModule(mFile);
 
     if (!mModule) {
       mFailed = true;
       return false;
     }
   }
   if (!mLoaded) {
     if (mModule->loadProc) {
@@ -839,17 +831,16 @@ nsresult nsComponentManagerImpl::Shutdow
   MOZ_LOG(nsComponentManagerLog, LogLevel::Debug,
          ("nsComponentManager: Beginning Shutdown."));
 
   UnregisterWeakMemoryReporter(this);
 
   // Release all cached factories
   mContractIDs.Clear();
   mFactories.Clear(); // XXX release the objects, don't just clear
-  mLoaderMap.Clear();
   mKnownModules.Clear();
   mKnownStaticModules.Clear();
 
   delete sExtraStaticModules;
   delete sModuleLocations;
 
   mStatus = SHUTDOWN_COMPLETE;
 
@@ -1516,33 +1507,16 @@ nsComponentManagerImpl::GetServiceByCont
 
   nsISupports** sresult = reinterpret_cast<nsISupports**>(aResult);
   *sresult = entry->mServiceObject;
   (*sresult)->AddRef();
 
   return NS_OK;
 }
 
-already_AddRefed<mozilla::ModuleLoader>
-nsComponentManagerImpl::LoaderForExtension(const nsACString& aExt)
-{
-  nsCOMPtr<mozilla::ModuleLoader> loader = mLoaderMap.Get(aExt);
-  if (!loader) {
-    loader = do_GetServiceFromCategory(NS_LITERAL_CSTRING("module-loader"),
-                                       aExt);
-    if (!loader) {
-      return nullptr;
-    }
-
-    mLoaderMap.Put(aExt, loader);
-  }
-
-  return loader.forget();
-}
-
 NS_IMETHODIMP
 nsComponentManagerImpl::RegisterFactory(const nsCID& aClass,
                                         const char* aName,
                                         const char* aContractID,
                                         nsIFactory* aFactory)
 {
   if (!aFactory) {
     // If a null factory is passed in, this call just wants to reset
@@ -1741,18 +1715,16 @@ nsComponentManagerImpl::CollectReports(n
 }
 
 size_t
 nsComponentManagerImpl::SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf)
   const
 {
   size_t n = aMallocSizeOf(this);
 
-  n += mLoaderMap.ShallowSizeOfExcludingThis(aMallocSizeOf);
-
   n += mFactories.ShallowSizeOfExcludingThis(aMallocSizeOf);
   for (auto iter = mFactories.ConstIter(); !iter.Done(); iter.Next()) {
     n += iter.Data()->SizeOfIncludingThis(aMallocSizeOf);
   }
 
   n += mContractIDs.ShallowSizeOfExcludingThis(aMallocSizeOf);
   for (auto iter = mContractIDs.ConstIter(); !iter.Done(); iter.Next()) {
     // We don't measure the nsFactoryEntry data because it's owned by
@@ -1769,17 +1741,16 @@ nsComponentManagerImpl::SizeOfIncludingT
   n += mKnownModules.ShallowSizeOfExcludingThis(aMallocSizeOf);
 
   n += mArena.SizeOfExcludingThis(aMallocSizeOf);
 
   n += mPendingServices.ShallowSizeOfExcludingThis(aMallocSizeOf);
 
   // Measurement of the following members may be added later if DMD finds it is
   // worthwhile:
-  // - mLoaderMap's keys and values
   // - mMon
   // - sModuleLocations' entries
   // - mKnownStaticModules' entries?
   // - mKnownModules' keys and values?
 
   return n;
 }
 
--- a/xpcom/components/nsComponentManager.h
+++ b/xpcom/components/nsComponentManager.h
@@ -137,19 +137,16 @@ public:
 
   static nsComponentManagerImpl* gComponentManager;
   nsresult Init();
 
   nsresult Shutdown(void);
 
   nsresult FreeServices();
 
-  already_AddRefed<mozilla::ModuleLoader> LoaderForExtension(const nsACString& aExt);
-  nsInterfaceHashtable<nsCStringHashKey, mozilla::ModuleLoader> mLoaderMap;
-
   already_AddRefed<nsIFactory> FindFactory(const nsCID& aClass);
   already_AddRefed<nsIFactory> FindFactory(const char* aContractID,
                                            uint32_t aContractIDLen);
 
   already_AddRefed<nsIFactory> LoadFactory(nsFactoryEntry* aEntry);
 
   nsFactoryEntry* GetFactoryEntry(const char* aContractID,
                                   uint32_t aContractIDLen);
@@ -191,44 +188,41 @@ public:
       , mLoaded(false)
       , mFailed(false)
     {
     }
 
     explicit KnownModule(mozilla::FileLocation& aFile)
       : mModule(nullptr)
       , mFile(aFile)
-      , mLoader(nullptr)
       , mLoaded(false)
       , mFailed(false)
     {
     }
 
     ~KnownModule()
     {
       if (mLoaded && mModule->unloadProc) {
         mModule->unloadProc();
       }
     }
 
-    bool EnsureLoader();
     bool Load();
 
     const mozilla::Module* Module() const { return mModule; }
 
     /**
      * For error logging, get a description of this module, either the
      * file path, or <static module>.
      */
     nsCString Description() const;
 
   private:
     const mozilla::Module* mModule;
     mozilla::FileLocation mFile;
-    nsCOMPtr<mozilla::ModuleLoader> mLoader;
     bool mLoaded;
     bool mFailed;
   };
 
   // The KnownModule is kept alive by these members, it is
   // referenced by pointer from the factory entries.
   nsTArray<nsAutoPtr<KnownModule>> mKnownStaticModules;
   // The key is the URI string of the module