Bug 1416066 - Delay initialization of sProcessType for child processes in ScriptPreloader since remoteType in ContentChild is not ready yet.
MozReview-Commit-ID: FTmQMbKhlR
--- 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,