Bug 1416066 - Delay initialization of sProcessType for child processes in ScriptPreloader since remoteType in ContentChild is not ready yet. draft
authorimjching <jlim@mozilla.com>
Fri, 22 Jun 2018 17:07:12 -0400
changeset 816184 4cb4330e391e791d9fa0ac8dd03183277381d06c
parent 816183 1ffee79fd37be72c5933d3c7a4ca7503696b2d38
child 816185 4cf20395ea9742f0fedd2f92a1641fd3d66d9854
child 816195 f47da8c5ba63084eb0fc01a75c253fa217264ab9
push id115768
push userbmo:jlim@mozilla.com
push dateTue, 10 Jul 2018 17:22:49 +0000
bugs1416066
milestone63.0a1
Bug 1416066 - Delay initialization of sProcessType for child processes in ScriptPreloader since remoteType in ContentChild is not ready yet. MozReview-Commit-ID: FTmQMbKhlR
js/xpconnect/loader/ScriptPreloader.cpp
--- a/js/xpconnect/loader/ScriptPreloader.cpp
+++ b/js/xpconnect/loader/ScriptPreloader.cpp
@@ -235,37 +235,32 @@ ScriptPreloader::Trace(JSTracer* trc)
     }
 }
 
 
 ScriptPreloader::ScriptPreloader()
   : mMonitor("[ScriptPreloader.mMonitor]")
   , mSaveMonitor("[ScriptPreloader.mSaveMonitor]")
 {
+    // We do not set the process type for child processes here because the
+    // remoteType in ContentChild is not ready yet.
     if (XRE_IsParentProcess()) {
         sProcessType = ProcessType::Parent;
-    } else {
-        sProcessType = GetChildProcessType(dom::ContentChild::GetSingleton()->GetRemoteType());
     }
 
     nsCOMPtr<nsIObserverService> obs = services::GetObserverService();
     MOZ_RELEASE_ASSERT(obs);
 
     if (XRE_IsParentProcess()) {
         // In the parent process, we want to freeze the script cache as soon
         // as idle tasks for the first browser window have completed.
         obs->AddObserver(this, STARTUP_COMPLETE_TOPIC, false);
         obs->AddObserver(this, CACHE_WRITE_TOPIC, false);
-    } else {
-        // In the child process, we need to freeze the script cache before any
-        // untrusted code has been executed. The insertion of the first DOM
-        // document element may sometimes be earlier than is ideal, but at
-        // least it should always be safe.
-        obs->AddObserver(this, DOC_ELEM_INSERTED_TOPIC, false);
     }
+
     obs->AddObserver(this, SHUTDOWN_TOPIC, false);
     obs->AddObserver(this, CLEANUP_TOPIC, false);
     obs->AddObserver(this, CACHE_INVALIDATE_TOPIC, false);
 
     AutoSafeJSAPI jsapi;
     JS_AddExtraGCRootsTracer(jsapi.cx(), TraceOp, this);
 }
 
@@ -362,16 +357,17 @@ ScriptPreloader::Observe(nsISupports* su
     if (!strcmp(topic, STARTUP_COMPLETE_TOPIC)) {
         obs->RemoveObserver(this, STARTUP_COMPLETE_TOPIC);
 
         MOZ_ASSERT(XRE_IsParentProcess());
 
         mStartupFinished = true;
     } else if (!strcmp(topic, CACHE_WRITE_TOPIC)) {
         obs->RemoveObserver(this, CACHE_WRITE_TOPIC);
+
         MOZ_ASSERT(mStartupFinished);
         MOZ_ASSERT(XRE_IsParentProcess());
 
         if (mChildCache) {
             Unused << NS_NewNamedThread("SaveScripts",
                                         getter_AddRefs(mSaveThread), this);
         }
     } else if (!strcmp(topic, DOC_ELEM_INSERTED_TOPIC)) {
@@ -498,16 +494,26 @@ ScriptPreloader::InitCache(const nsAStri
 
 Result<Ok, nsresult>
 ScriptPreloader::InitCache(const Maybe<ipc::FileDescriptor>& cacheFile, ScriptCacheChild* cacheChild)
 {
     MOZ_ASSERT(XRE_IsContentProcess());
 
     mCacheInitialized = true;
     mChildActor = cacheChild;
+    sProcessType = GetChildProcessType(dom::ContentChild::GetSingleton()->GetRemoteType());
+
+    nsCOMPtr<nsIObserverService> obs = services::GetObserverService();
+    MOZ_RELEASE_ASSERT(obs);
+
+    // In the child process, we need to freeze the script cache before any
+    // untrusted code has been executed. The insertion of the first DOM
+    // document element may sometimes be earlier than is ideal, but at
+    // least it should always be safe.
+    obs->AddObserver(this, DOC_ELEM_INSERTED_TOPIC, false);
 
     RegisterWeakMemoryReporter(this);
 
     auto cleanup = MakeScopeExit([&] {
         // If the parent is expecting cache data from us, make sure we send it
         // before it writes out its cache file. For normal proceses, this isn't
         // a concern, since they begin loading documents quite early. For the
         // preloaded process, we may end up waiting a long time (or, indeed,