From e48cee9f1da317beb08445da1dd4aca5dded8f34 Mon Sep 17 00:00:00 2001 From: Yulia Startsev Date: Wed, 24 Aug 2022 13:38:34 +0000 Subject: [PATCH] Bug 1784476 - Move ClientInfo to WorkerLoadContext; r=asuth Previously, we had the client info for main scripts on the ScriptLoadInfo, and in the ScriptLoader for non-main scripts (ImportScripts case). This was a bit confusing, so I moved it all to the ScriptLoader as in the importScripts case, it is always recreated with each new load. However, for modules this is not true. In order to make it persistant for main scripts and modules, as well as ensure that it has the correct data, it should always live on the WorkerLoadContext. Differential Revision: https://phabricator.services.mozilla.com/D147317 --- dom/workers/ScriptLoader.cpp | 19 ++++++++++++------- dom/workers/ScriptLoader.h | 1 - dom/workers/loader/WorkerLoadContext.cpp | 6 ++++-- dom/workers/loader/WorkerLoadContext.h | 3 ++- 4 files changed, 18 insertions(+), 11 deletions(-) diff --git a/dom/workers/ScriptLoader.cpp b/dom/workers/ScriptLoader.cpp index d3a3c5e9086b..604db5b79a53 100644 --- a/dom/workers/ScriptLoader.cpp +++ b/dom/workers/ScriptLoader.cpp @@ -452,13 +452,15 @@ WorkerScriptLoader::WorkerScriptLoader( nsIGlobalObject* global = GetGlobal(); - mClientInfo = global->GetClientInfo(); + Maybe clientInfo = global->GetClientInfo(); mController = global->GetController(); for (const nsString& aScriptURL : aScriptURLs) { WorkerLoadContext::Kind kind = WorkerLoadContext::GetKind(aIsMainScript, IsDebuggerScript()); - RefPtr loadContext = new WorkerLoadContext(kind); + + RefPtr loadContext = + new WorkerLoadContext(kind, clientInfo); // Create ScriptLoadRequests for this WorkerScriptLoader ReferrerPolicy aReferrerPolicy = mWorkerRef->Private()->GetReferrerPolicy(); @@ -782,9 +784,10 @@ nsresult WorkerScriptLoader::LoadScript(ScriptLoadRequest* aRequest) { rv = ChannelFromScriptURL( principal, parentDoc, mWorkerRef->Private(), loadGroup, ios, secMan, - aRequest->mURI, mClientInfo, mController, loadContext->IsTopLevel(), - mWorkerScriptType, mWorkerRef->Private()->ContentPolicyType(), - loadFlags, mWorkerRef->Private()->CookieJarSettings(), referrerInfo, + aRequest->mURI, loadContext->mClientInfo, mController, + loadContext->IsTopLevel(), mWorkerScriptType, + mWorkerRef->Private()->ContentPolicyType(), loadFlags, + mWorkerRef->Private()->CookieJarSettings(), referrerInfo, getter_AddRefs(channel)); if (NS_WARN_IF(NS_FAILED(rv))) { return rv; @@ -816,7 +819,7 @@ nsresult WorkerScriptLoader::LoadScript(ScriptLoadRequest* aRequest) { } if (loadContext->IsTopLevel()) { - MOZ_DIAGNOSTIC_ASSERT(mClientInfo.isSome()); + MOZ_DIAGNOSTIC_ASSERT(loadContext->mClientInfo.isSome()); // In order to get the correct foreign partitioned prinicpal, we need to // set the `IsThirdPartyContextToTopWindow` to the channel's loadInfo. @@ -826,7 +829,9 @@ nsresult WorkerScriptLoader::LoadScript(ScriptLoadRequest* aRequest) { loadInfo->SetIsThirdPartyContextToTopWindow( mWorkerRef->Private()->IsThirdPartyContextToTopWindow()); - rv = AddClientChannelHelper(channel, std::move(mClientInfo), + Maybe clientInfo; + clientInfo.emplace(loadContext->mClientInfo.ref()); + rv = AddClientChannelHelper(channel, std::move(clientInfo), Maybe(), mWorkerRef->Private()->HybridEventTarget()); if (NS_WARN_IF(NS_FAILED(rv))) { diff --git a/dom/workers/ScriptLoader.h b/dom/workers/ScriptLoader.h index 6f55e12575c8..47459090da71 100644 --- a/dom/workers/ScriptLoader.h +++ b/dom/workers/ScriptLoader.h @@ -134,7 +134,6 @@ class WorkerScriptLoader final : public nsINamed { nsCOMPtr mSyncLoopTarget; JS::loader::ScriptLoadRequestList mLoadingRequests; JS::loader::ScriptLoadRequestList mLoadedRequests; - Maybe mClientInfo; Maybe mController; WorkerScriptType mWorkerScriptType; Maybe mCancelMainThread; diff --git a/dom/workers/loader/WorkerLoadContext.cpp b/dom/workers/loader/WorkerLoadContext.cpp index 44655dca035f..63b1ce36e574 100644 --- a/dom/workers/loader/WorkerLoadContext.cpp +++ b/dom/workers/loader/WorkerLoadContext.cpp @@ -10,9 +10,11 @@ namespace mozilla { namespace dom { -WorkerLoadContext::WorkerLoadContext(WorkerLoadContext::Kind aKind) +WorkerLoadContext::WorkerLoadContext(Kind aKind, + const Maybe& aClientInfo) : JS::loader::LoadContextBase(JS::loader::ContextKind::Worker), - mKind(aKind){}; + mKind(aKind), + mClientInfo(aClientInfo){}; void WorkerLoadContext::SetCacheCreator( RefPtr aCacheCreator) { diff --git a/dom/workers/loader/WorkerLoadContext.h b/dom/workers/loader/WorkerLoadContext.h index daf8fa90f56e..ffdbddb18846 100644 --- a/dom/workers/loader/WorkerLoadContext.h +++ b/dom/workers/loader/WorkerLoadContext.h @@ -78,7 +78,7 @@ class WorkerLoadContext : public JS::loader::LoadContextBase { DebuggerScript }; - explicit WorkerLoadContext(WorkerLoadContext::Kind aKind); + explicit WorkerLoadContext(Kind aKind, const Maybe& aClientInfo); ~WorkerLoadContext() = default; @@ -102,6 +102,7 @@ class WorkerLoadContext : public JS::loader::LoadContextBase { nsresult mLoadResult = NS_ERROR_NOT_INITIALIZED; bool mLoadingFinished = false; Kind mKind; + Maybe mClientInfo; /* These fields are only used by service workers */ /* TODO: Split out a ServiceWorkerLoadContext */