Skip to content

Commit

Permalink
Bug 1778289 - Part 2: Rename to 'import maps allowed' and add 'disall…
Browse files Browse the repository at this point in the history
  • Loading branch information
allstarschh committed Oct 7, 2022
1 parent 8671254 commit 77f6c4a
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 46 deletions.
8 changes: 3 additions & 5 deletions dom/base/nsContentSink.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -329,11 +329,9 @@ nsresult nsContentSink::ProcessLinkFromHeader(const net::LinkHeader& aHeader) {
}

if (linkTypes & LinkStyle::eMODULE_PRELOAD) {
// https://wicg.github.io/import-maps/#wait-for-import-maps
// Step 1.2: Set document’s acquiring import maps to false.
// When fetch a modulepreload module script graph.
mDocument->ScriptLoader()->GetModuleLoader()->SetAcquiringImportMaps(
false);
// https://whatpr.org/html/8075/webappapis.html#fetch-a-modulepreload-module-script-graph
// Step 1. Disallow further import maps given settings object.
mDocument->ScriptLoader()->GetModuleLoader()->DisallowImportMaps();
}
}

Expand Down
8 changes: 3 additions & 5 deletions dom/html/HTMLLinkElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -489,9 +489,6 @@ void HTMLLinkElement::
}

if (linkTypes & eMODULE_PRELOAD) {
// https://wicg.github.io/import-maps/#wait-for-import-maps
// Step 1.2: Set document’s acquiring import maps to false.
// When fetch a modulepreload module script graph.
if (!OwnerDoc()->ScriptLoader()->GetModuleLoader()) {
// For the print preview documents, at this moment it doesn't have module
// loader yet, as the (print preview) document is not attached to the
Expand All @@ -502,8 +499,9 @@ void HTMLLinkElement::
return;
}

OwnerDoc()->ScriptLoader()->GetModuleLoader()->SetAcquiringImportMaps(
false);
// https://whatpr.org/html/8075/webappapis.html#fetch-a-modulepreload-module-script-graph
// Step 1. Disallow further import maps given settings object.
OwnerDoc()->ScriptLoader()->GetModuleLoader()->DisallowImportMaps();
return;
}

Expand Down
12 changes: 4 additions & 8 deletions dom/script/ModuleLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,15 +110,11 @@ nsresult ModuleLoader::StartFetch(ModuleLoadRequest* aRequest) {
nsresult rv = GetScriptLoader()->StartLoadInternal(aRequest, securityFlags);
NS_ENSURE_SUCCESS(rv, rv);

// https://wicg.github.io/import-maps/#document-acquiring-import-maps
//
// An import map is accepted if and only if it is added (i.e., its
// corresponding script element is added) before the first module load is
// started, even if the loading of the import map file doesn’t finish before
// the first module load is started.
// https://whatpr.org/html/8075/webappapis.html#fetch-an-import()-module-script-graph
// Step 1. Disallow further import maps given settings object.
if (!aRequest->GetScriptLoadContext()->IsPreload()) {
LOG(("ScriptLoadRequest (%p): SetAcquiringImportMaps false", aRequest));
SetAcquiringImportMaps(false);
LOG(("ScriptLoadRequest (%p): Disallow further import maps.", aRequest));
DisallowImportMaps();
}

LOG(("ScriptLoadRequest (%p): Start fetching module", aRequest));
Expand Down
34 changes: 16 additions & 18 deletions dom/script/ScriptLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -953,13 +953,12 @@ bool ScriptLoader::ProcessExternalScript(nsIScriptElement* aElement,

LOG(("ScriptLoadRequest (%p): Using preload request", request.get()));

// https://wicg.github.io/import-maps/#document-acquiring-import-maps
// If this preload request is for a module load, set acquiring import maps
// to false.
// https://whatpr.org/html/8075/webappapis.html#fetch-a-module-script-tree
// Step 1. Disallow further import maps given settings object.
if (request->IsModuleRequest()) {
LOG(("ScriptLoadRequest (%p): Set acquiring import maps to false",
LOG(("ScriptLoadRequest (%p): Disallow further import maps.",
request.get()));
mModuleLoader->SetAcquiringImportMaps(false);
mModuleLoader->DisallowImportMaps();
}

// It's possible these attributes changed since we started the preload so
Expand Down Expand Up @@ -1153,9 +1152,9 @@ bool ScriptLoader::ProcessInlineScript(nsIScriptElement* aElement,
request->mBaseURL = mDocument->GetDocBaseURI();

if (request->IsModuleRequest()) {
// https://wicg.github.io/import-maps/#document-acquiring-import-maps
// Set acquiring import maps to false for inline modules.
mModuleLoader->SetAcquiringImportMaps(false);
// https://whatpr.org/html/8075/webappapis.html#fetch-an-inline-module-script-graph
// Step 1. Disallow further import maps given settings object.
mModuleLoader->DisallowImportMaps();

ModuleLoadRequest* modReq = request->AsModuleRequest();
if (aElement->GetParserCreated() != NOT_FROM_PARSER) {
Expand Down Expand Up @@ -1185,22 +1184,21 @@ bool ScriptLoader::ProcessInlineScript(nsIScriptElement* aElement,
}

if (request->IsImportMapRequest()) {
// https://wicg.github.io/import-maps/#integration-prepare-a-script
// If the script's type is "importmap":
//
// Step 1: If the element's node document's acquiring import maps is false,
// then queue a task to fire an event named error at the element, and
// return.
if (!mModuleLoader->GetAcquiringImportMaps()) {
NS_WARNING("ScriptLoader: acquiring import maps is false.");
// https://whatpr.org/html/8075/scripting.html#prepare-the-script-element
// Step 31.2 type is "importmap":
// Step 1. If el's relevant global object's import maps allowed is false,
// then queue an element task on the DOM manipulation task source given el
// to fire an event named error at el, and return.
if (!mModuleLoader->IsImportMapAllowed()) {
NS_WARNING("ScriptLoader: import maps allowed is false.");
NS_DispatchToCurrentThread(
NewRunnableMethod("nsIScriptElement::FireErrorEvent", aElement,
&nsIScriptElement::FireErrorEvent));
return false;
}

// Step 2: Set the element's node document's acquiring import maps to false.
mModuleLoader->SetAcquiringImportMaps(false);
// Step 2. Set el's relevant global object's import maps allowed to false.
mModuleLoader->DisallowImportMaps();

UniquePtr<ImportMap> importMap = mModuleLoader->ParseImportMap(request);

Expand Down
17 changes: 7 additions & 10 deletions js/loader/ModuleLoaderBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,10 @@ class ModuleLoaderBase : public nsISupports {
// for fetches to finish and for imports to become avilable.
nsCOMPtr<nsISerialEventTarget> mEventTarget;

// https://wicg.github.io/import-maps/#document-acquiring-import-maps
// https://whatpr.org/html/8075/webappapis.html#import-maps-allowed
//
// Each Document has an acquiring import maps boolean. It is initially true.
bool mAcquiringImportMaps = true;
// Each Window has an import maps allowed boolean, initially true.
bool mImportMapsAllowed = true;

protected:
RefPtr<ScriptLoaderInterface> mLoader;
Expand Down Expand Up @@ -287,13 +287,10 @@ class ModuleLoaderBase : public nsISupports {
// Implements https://wicg.github.io/import-maps/#register-an-import-map
void RegisterImportMap(mozilla::UniquePtr<ImportMap> aImportMap);

/**
* Getter and Setter for mAcquiringImportMaps.
*/
bool GetAcquiringImportMaps() const { return mAcquiringImportMaps; }
void SetAcquiringImportMaps(bool acquiring) {
mAcquiringImportMaps = acquiring;
}
// Getter for mImportMapsAllowed.
bool IsImportMapAllowed() const { return mImportMapsAllowed; }
// https://whatpr.org/html/8075/webappapis.html#disallow-further-import-maps
void DisallowImportMaps() { mImportMapsAllowed = false; }

// Returns true if the module for given URL is already fetched.
bool IsModuleFetched(nsIURI* aURL) const;
Expand Down

0 comments on commit 77f6c4a

Please sign in to comment.