Bug 1348981 - provide method to re-initialize URLPreloader for xpcshell tests r?kmag draft
authorRobert Helmer <rhelmer@mozilla.com>
Thu, 21 Sep 2017 11:24:14 -0700
changeset 668854 03b498be730b1f1341c4831c04db5a8381dc4808
parent 668853 b80e4bac92ec7ad2f6bbdf7a4ba548d064127c64
child 668855 83eac9c787f2d8bdaa4c7e81fa600699fe7dae49
push id81140
push userbmo:rhelmer@mozilla.com
push dateFri, 22 Sep 2017 03:38:54 +0000
reviewerskmag
bugs1348981
milestone58.0a1
Bug 1348981 - provide method to re-initialize URLPreloader for xpcshell tests r?kmag MozReview-Commit-ID: rnjEJc1Uir
js/xpconnect/loader/URLPreloader.cpp
js/xpconnect/loader/URLPreloader.h
toolkit/mozapps/extensions/AddonManagerStartup.cpp
toolkit/mozapps/extensions/amIAddonManagerStartup.idl
--- a/js/xpconnect/loader/URLPreloader.cpp
+++ b/js/xpconnect/loader/URLPreloader.cpp
@@ -78,32 +78,30 @@ URLPreloader::CollectReports(nsIHandleRe
             NS_LITERAL_CSTRING("Memory used to hold cache data for files which "
                                "have been read or pre-loaded during this session."),
             aData);
     }
 
     return NS_OK;
 }
 
-
 URLPreloader&
 URLPreloader::GetSingleton()
 {
-    static RefPtr<URLPreloader> singleton;
-
-    if (!singleton) {
-        singleton = new URLPreloader();
-        ClearOnShutdown(&singleton);
+    if (!sSingleton) {
+        sSingleton = new URLPreloader();
+        ClearOnShutdown(&sSingleton);
     }
 
-    return *singleton;
+    return *sSingleton;
 }
 
+bool URLPreloader::sInitialized = false;
 
-bool URLPreloader::sInitialized = false;
+StaticRefPtr<URLPreloader> URLPreloader::sSingleton;
 
 URLPreloader::URLPreloader()
 {
     if (InitInternal().isOk()) {
         sInitialized = true;
         RegisterWeakMemoryReporter(this);
     }
 }
@@ -151,16 +149,24 @@ URLPreloader::InitInternal()
     } else {
         mStartupFinished = true;
         mReaderInitialized = true;
     }
 
     return Ok();
 }
 
+URLPreloader&
+URLPreloader::ReInitialize()
+{
+    sSingleton = new URLPreloader();
+
+    return *sSingleton;
+}
+
 Result<nsCOMPtr<nsIFile>, nsresult>
 URLPreloader::GetCacheFile(const nsAString& suffix)
 {
     if (!mProfD) {
         return Err(NS_ERROR_NOT_INITIALIZED);
     }
 
     nsCOMPtr<nsIFile> cacheFile;
--- a/js/xpconnect/loader/URLPreloader.h
+++ b/js/xpconnect/loader/URLPreloader.h
@@ -91,23 +91,28 @@ private:
     Result<const nsCString, nsresult> ReadURIInternal(nsIURI* uri, ReadType readType);
 
     Result<const nsCString, nsresult> ReadFileInternal(nsIFile* file, ReadType readType);
 
     static Result<const nsCString, nsresult> Read(const CacheKey& key, ReadType readType);
 
     static bool sInitialized;
 
+    static mozilla::StaticRefPtr<URLPreloader> sSingleton;
+
 protected:
+    friend class AddonManagerStartup;
     friend class ScriptPreloader;
 
     virtual ~URLPreloader();
 
     Result<Ok, nsresult> WriteCache();
 
+    static URLPreloader& ReInitialize();
+
     // Clear leftover entries after the cache has been written.
     void Cleanup();
 
     // Begins reading files off-thread, and ensures that initialization has
     // completed before leaving the current scope. The caller *must* ensure that
     // no code on the main thread access Omnijar, either directly or indirectly,
     // for the lifetime of this guard object.
     struct MOZ_RAII AutoBeginReading final
--- a/toolkit/mozapps/extensions/AddonManagerStartup.cpp
+++ b/toolkit/mozapps/extensions/AddonManagerStartup.cpp
@@ -740,16 +740,25 @@ AddonManagerStartup::Reset()
   mInitialized = false;
 
   mExtensionPaths.Clear();
   mThemePaths.Clear();
 
   return NS_OK;
 }
 
+nsresult
+AddonManagerStartup::InitializeURLPreloader()
+{
+  MOZ_RELEASE_ASSERT(xpc::IsInAutomation());
+
+  URLPreloader::ReInitialize();
+
+  return NS_OK;
+}
 
 /******************************************************************************
  * RegisterChrome
  ******************************************************************************/
 
 namespace {
 static bool sObserverRegistered;
 
--- a/toolkit/mozapps/extensions/amIAddonManagerStartup.idl
+++ b/toolkit/mozapps/extensions/amIAddonManagerStartup.idl
@@ -70,10 +70,18 @@ interface amIAddonManagerStartup : nsISu
   /**
    * Resets the internal state of the startup service, and allows
    * initializeExtensions() to be called again. Does *not* fully unregister
    * chrome registry locations for previously registered add-ons.
    *
    * NOT FOR USE OUTSIDE OF UNIT TESTS.
    */
   void reset();
+
+  /**
+   * Initializes the URL Preloader.
+   *
+   * NOT FOR USE OUTSIDE OF UNIT TESTS.
+   */
+  void initializeURLPreloader();
+
 };