Bug 1540913 - Part 6-a: Fix BaseURL for importScripts; r=jonco

Differential Revision: https://phabricator.services.mozilla.com/D171875
This commit is contained in:
Yulia
2023-03-14 18:16:32 +00:00
parent ddbfcc4d9e
commit da459f7e4d
4 changed files with 47 additions and 18 deletions

View File

@@ -32,6 +32,7 @@
#include "js/CompilationAndEvaluation.h"
#include "js/Exception.h"
#include "js/SourceText.h"
#include "js/TypeDecls.h"
#include "nsError.h"
#include "nsComponentManagerUtils.h"
#include "nsContentSecurityManager.h"
@@ -456,13 +457,24 @@ class ScriptExecutorRunnable final : public MainThreadWorkerSyncRunnable {
template <typename Unit>
static bool EvaluateSourceBuffer(JSContext* aCx,
const JS::CompileOptions& aOptions,
JS::loader::ClassicScript* aClassicScript,
JS::SourceText<Unit>& aSourceBuffer) {
static_assert(std::is_same<Unit, char16_t>::value ||
std::is_same<Unit, Utf8Unit>::value,
"inferred units must be UTF-8 or UTF-16");
JS::Rooted<JSScript*> script(aCx, JS::Compile(aCx, aOptions, aSourceBuffer));
if (!script) {
return false;
}
if (aClassicScript) {
aClassicScript->AssociateWithScript(script);
}
JS::Rooted<JS::Value> unused(aCx);
return Evaluate(aCx, aOptions, aSourceBuffer, &unused);
return JS_ExecuteScript(aCx, script, &unused);
}
WorkerScriptLoader::WorkerScriptLoader(
@@ -1127,11 +1139,27 @@ bool WorkerScriptLoader::EvaluateScript(JSContext* aCx,
return false;
}
RefPtr<JS::loader::ClassicScript> classicScript = nullptr;
if (StaticPrefs::dom_workers_modules_enabled() &&
!mWorkerRef->Private()->IsServiceWorker()) {
// We need a LoadedScript to be associated with the JSScript in order to
// correctly resolve the referencing private for dynamic imports. In turn
// this allows us to correctly resolve the BaseURL.
//
// Dynamic import is disallowed on service workers. Additionally, causes
// crashes because the life cycle isn't completed for service workers. To
// keep things simple, we don't create a classic script for ServiceWorkers.
// If this changes then we will need to ensure that the reference that is
// held is released appropriately.
classicScript = new JS::loader::ClassicScript(aRequest->mFetchOptions,
aRequest->mBaseURL);
}
bool successfullyEvaluated =
aRequest->IsUTF8Text()
? EvaluateSourceBuffer(aCx, options,
? EvaluateSourceBuffer(aCx, options, classicScript,
maybeSource.ref<JS::SourceText<Utf8Unit>>())
: EvaluateSourceBuffer(aCx, options,
: EvaluateSourceBuffer(aCx, options, classicScript,
maybeSource.ref<JS::SourceText<char16_t>>());
if (aRequest->IsCanceled()) {

View File

@@ -567,9 +567,12 @@ nsresult CacheLoadHandler::DataReceivedFromCache(
"EmptyWorkerSourceWarning");
}
nsCOMPtr<nsIURI> finalURI;
rv = NS_NewURI(getter_AddRefs(finalURI), loadContext->mFullURL);
if (!loadContext->mRequest->mBaseURL) {
loadContext->mRequest->mBaseURL = finalURI;
}
if (loadContext->IsTopLevel()) {
nsCOMPtr<nsIURI> finalURI;
rv = NS_NewURI(getter_AddRefs(finalURI), loadContext->mFullURL);
if (NS_SUCCEEDED(rv)) {
mWorkerRef->Private()->SetBaseURI(finalURI);
}

View File

@@ -178,19 +178,17 @@ nsresult NetworkLoadHandler::DataReceivedFromNetwork(nsIStreamLoader* aLoader,
"EmptyWorkerSourceWarning");
}
if (loadContext->mRequest->IsModuleRequest()) {
// For modules, we need to store the base URI on the module request object,
// rather than on the worker private (as we do for classic scripts). This is
// because module loading is shared across multiple components, with
// ScriptLoadRequests being the common structure among them. This specific
// use of the base url is used when resolving the module specifier for child
// modules.
nsCOMPtr<nsIURI> uri;
rv = channel->GetOriginalURI(getter_AddRefs(uri));
NS_ENSURE_SUCCESS(rv, rv);
// For modules, we need to store the base URI on the module request object,
// rather than on the worker private (as we do for classic scripts). This is
// because module loading is shared across multiple components, with
// ScriptLoadRequests being the common structure among them. This specific
// use of the base url is used when resolving the module specifier for child
// modules.
nsCOMPtr<nsIURI> uri;
rv = channel->GetOriginalURI(getter_AddRefs(uri));
NS_ENSURE_SUCCESS(rv, rv);
channel->GetURI(getter_AddRefs(loadContext->mRequest->mBaseURL));
}
channel->GetURI(getter_AddRefs(loadContext->mRequest->mBaseURL));
// Figure out what we actually loaded.
nsCOMPtr<nsIURI> finalURI;

View File

@@ -98,7 +98,7 @@ already_AddRefed<ModuleLoadRequest> WorkerModuleLoader::CreateDynamicImport(
ReferrerPolicy referrerPolicy = workerPrivate->GetReferrerPolicy();
options =
new ScriptFetchOptions(CORSMode::CORS_NONE, referrerPolicy, nullptr);
baseURL = workerPrivate->GetBaseURI();
baseURL = GetBaseURI();
}
Maybe<ClientInfo> clientInfo = GetGlobalObject()->GetClientInfo();