Bug 1784482 - Move creation of ScriptLoadRequests into own functions; r=asuth

Differential Revision: https://phabricator.services.mozilla.com/D147318
This commit is contained in:
Yulia Startsev 2022-09-07 09:37:38 +00:00
Родитель 461fbf1994
Коммит 0d822c68c2
2 изменённых файлов: 64 добавлений и 46 удалений

Просмотреть файл

@ -250,14 +250,16 @@ void LoadAllScripts(WorkerPrivate* aWorkerPrivate,
return;
}
RefPtr<loader::WorkerScriptLoader> loader = new loader::WorkerScriptLoader(
aWorkerPrivate, std::move(aOriginStack), syncLoopTarget, aScriptURLs,
aDocumentEncoding, aIsMainScript, aWorkerScriptType, aRv);
RefPtr<loader::WorkerScriptLoader> loader =
new loader::WorkerScriptLoader(aWorkerPrivate, std::move(aOriginStack),
syncLoopTarget, aWorkerScriptType, aRv);
if (NS_WARN_IF(aRv.Failed())) {
return;
}
loader->CreateScriptRequests(aScriptURLs, aDocumentEncoding, aIsMainScript);
if (loader->DispatchLoadScripts()) {
syncLoop.Run();
}
@ -422,9 +424,8 @@ static bool EvaluateSourceBuffer(JSContext* aCx,
WorkerScriptLoader::WorkerScriptLoader(
WorkerPrivate* aWorkerPrivate,
UniquePtr<SerializedStackHolder> aOriginStack,
nsIEventTarget* aSyncLoopTarget, const nsTArray<nsString>& aScriptURLs,
const mozilla::Encoding* aDocumentEncoding, bool aIsMainScript,
WorkerScriptType aWorkerScriptType, ErrorResult& aRv)
nsIEventTarget* aSyncLoopTarget, WorkerScriptType aWorkerScriptType,
ErrorResult& aRv)
: mOriginStack(std::move(aOriginStack)),
mSyncLoopTarget(aSyncLoopTarget),
mWorkerScriptType(aWorkerScriptType),
@ -432,7 +433,6 @@ WorkerScriptLoader::WorkerScriptLoader(
mRv(aRv) {
aWorkerPrivate->AssertIsOnWorkerThread();
MOZ_ASSERT(aSyncLoopTarget);
MOZ_ASSERT_IF(aIsMainScript, aScriptURLs.Length() == 1);
RefPtr<WorkerScriptLoader> self = this;
@ -452,28 +452,41 @@ WorkerScriptLoader::WorkerScriptLoader(
nsIGlobalObject* global = GetGlobal();
Maybe<ClientInfo> clientInfo = global->GetClientInfo();
mController = global->GetController();
}
for (const nsString& aScriptURL : aScriptURLs) {
void WorkerScriptLoader::CreateScriptRequests(
const nsTArray<nsString>& aScriptURLs,
const mozilla::Encoding* aDocumentEncoding, bool aIsMainScript) {
for (const nsString& scriptURL : aScriptURLs) {
RefPtr<ScriptLoadRequest> request =
CreateScriptLoadRequest(scriptURL, aDocumentEncoding, aIsMainScript);
mLoadingRequests.AppendElement(request);
}
}
already_AddRefed<ScriptLoadRequest> WorkerScriptLoader::CreateScriptLoadRequest(
const nsString& aScriptURL, const mozilla::Encoding* aDocumentEncoding,
bool aIsMainScript) {
WorkerLoadContext::Kind kind =
WorkerLoadContext::GetKind(aIsMainScript, IsDebuggerScript());
Maybe<ClientInfo> clientInfo = GetGlobal()->GetClientInfo();
RefPtr<WorkerLoadContext> loadContext =
new WorkerLoadContext(kind, clientInfo);
// Create ScriptLoadRequests for this WorkerScriptLoader
ReferrerPolicy aReferrerPolicy = mWorkerRef->Private()->GetReferrerPolicy();
ReferrerPolicy referrerPolicy = mWorkerRef->Private()->GetReferrerPolicy();
// Only top level workers' main script use the document charset for the
// script uri encoding. Otherwise, default encoding (UTF-8) is applied.
MOZ_ASSERT_IF(bool(aDocumentEncoding),
aIsMainScript && !aWorkerPrivate->GetParent());
nsCOMPtr<nsIURI> baseURI =
aIsMainScript ? GetInitialBaseURI() : GetBaseURI();
nsCOMPtr<nsIURI> aURI;
nsresult rv = ConstructURI(aScriptURL, baseURI, aDocumentEncoding,
getter_AddRefs(aURI));
aIsMainScript && !mWorkerRef->Private()->GetParent());
nsCOMPtr<nsIURI> baseURI = aIsMainScript ? GetInitialBaseURI() : GetBaseURI();
nsCOMPtr<nsIURI> uri;
nsresult rv =
ConstructURI(aScriptURL, baseURI, aDocumentEncoding, getter_AddRefs(uri));
// If we failed to construct the URI, handle it in the LoadContext so it is
// thrown in the right order.
if (NS_WARN_IF(NS_FAILED(rv))) {
@ -481,17 +494,17 @@ WorkerScriptLoader::WorkerScriptLoader(
}
RefPtr<ScriptFetchOptions> fetchOptions =
new ScriptFetchOptions(CORSMode::CORS_NONE, aReferrerPolicy, nullptr);
new ScriptFetchOptions(CORSMode::CORS_NONE, referrerPolicy, nullptr);
RefPtr<ScriptLoadRequest> request =
new ScriptLoadRequest(ScriptKind::eClassic, aURI, fetchOptions,
new ScriptLoadRequest(ScriptKind::eClassic, uri, fetchOptions,
SRIMetadata(), nullptr, /* = aReferrer */
loadContext);
// Set the mURL, it will be used for error handling and debugging.
request->mURL = NS_ConvertUTF16toUTF8(aScriptURL);
mLoadingRequests.AppendElement(request);
}
return request.forget();
}
bool WorkerScriptLoader::DispatchLoadScripts() {

Просмотреть файл

@ -147,15 +147,20 @@ class WorkerScriptLoader final : public nsINamed {
WorkerScriptLoader(WorkerPrivate* aWorkerPrivate,
UniquePtr<SerializedStackHolder> aOriginStack,
nsIEventTarget* aSyncLoopTarget,
const nsTArray<nsString>& aScriptURLs,
const mozilla::Encoding* aDocumentEncoding,
bool aIsMainScript, WorkerScriptType aWorkerScriptType,
ErrorResult& aRv);
WorkerScriptType aWorkerScriptType, ErrorResult& aRv);
void CancelMainThreadWithBindingAborted() {
CancelMainThread(NS_BINDING_ABORTED);
}
void CreateScriptRequests(const nsTArray<nsString>& aScriptURLs,
const mozilla::Encoding* aDocumentEncoding,
bool aIsMainScript);
already_AddRefed<ScriptLoadRequest> CreateScriptLoadRequest(
const nsString& aScriptURL, const mozilla::Encoding* aDocumentEncoding,
bool aIsMainScript);
bool DispatchLoadScripts();
protected: