Bug 1742437 - move mIsPreload from ScriptFetchOptions to DOMScriptLoadContext, and introduce GetRootModule; r=jonco,smaug

Differential Revision: https://phabricator.services.mozilla.com/D136743
This commit is contained in:
Yulia Startsev 2022-02-17 15:57:48 +00:00
Родитель 09f0828968
Коммит c413fa305d
6 изменённых файлов: 74 добавлений и 47 удалений

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

@ -25,13 +25,14 @@ NS_IMPL_CYCLE_COLLECTION_MULTI_ZONE_JSHOLDER_CLASS(ModuleLoadRequest)
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(ModuleLoadRequest,
ScriptLoadRequest)
NS_IMPL_CYCLE_COLLECTION_UNLINK(mLoader, mModuleScript, mImports)
NS_IMPL_CYCLE_COLLECTION_UNLINK(mLoader, mModuleScript, mImports, mRootModule)
tmp->ClearDynamicImport();
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(ModuleLoadRequest,
ScriptLoadRequest)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mLoader, mModuleScript, mImports)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mLoader, mModuleScript, mImports,
mRootModule)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN_INHERITED(ModuleLoadRequest,
@ -51,38 +52,39 @@ static VisitedURLSet* NewVisitedSetForTopLevelImport(nsIURI* aURI) {
}
/* static */
ModuleLoadRequest* ModuleLoadRequest::CreateTopLevel(
already_AddRefed<ModuleLoadRequest> ModuleLoadRequest::CreateTopLevel(
nsIURI* aURI, ScriptFetchOptions* aFetchOptions, Element* aElement,
const SRIMetadata& aIntegrity, nsIURI* aReferrer, ScriptLoader* aLoader) {
auto* request = new ModuleLoadRequest(
RefPtr<ModuleLoadRequest> request = new ModuleLoadRequest(
aURI, aFetchOptions, aIntegrity, aReferrer, true, /* is top level */
false, /* is dynamic import */
aLoader->GetModuleLoader(), NewVisitedSetForTopLevelImport(aURI));
aLoader->GetModuleLoader(), NewVisitedSetForTopLevelImport(aURI),
nullptr);
DOMScriptLoadContext* context = new DOMScriptLoadContext(aElement, request);
request->mLoadContext = context;
return request;
return request.forget();
}
/* static */
ModuleLoadRequest* ModuleLoadRequest::CreateStaticImport(
already_AddRefed<ModuleLoadRequest> ModuleLoadRequest::CreateStaticImport(
nsIURI* aURI, ModuleLoadRequest* aParent) {
auto request =
new ModuleLoadRequest(aURI, aParent->mFetchOptions, SRIMetadata(),
aParent->mURI, false, /* is top level */
false, /* is dynamic import */
aParent->mLoader, aParent->mVisitedSet);
DOMScriptLoadContext* context =
new DOMScriptLoadContext(aParent->mLoadContext->mElement, request);
RefPtr<ModuleLoadRequest> request = new ModuleLoadRequest(
aURI, aParent->mFetchOptions, SRIMetadata(), aParent->mURI,
false, /* is top level */
false, /* is dynamic import */
aParent->mLoader, aParent->mVisitedSet, aParent->GetRootModule());
DOMScriptLoadContext* context = new DOMScriptLoadContext(nullptr, request);
context->mIsInline = false;
// Propagated Parent values. TODO: allow child modules to use root module's
// script mode.
context->mScriptMode = aParent->GetLoadContext()->mScriptMode;
request->mLoadContext = context;
return request;
return request.forget();
}
/* static */
ModuleLoadRequest* ModuleLoadRequest::CreateDynamicImport(
already_AddRefed<ModuleLoadRequest> ModuleLoadRequest::CreateDynamicImport(
nsIURI* aURI, ScriptFetchOptions* aFetchOptions, nsIURI* aBaseURL,
Element* aElement, ScriptLoader* aLoader,
JS::Handle<JS::Value> aReferencingPrivate, JS::Handle<JSString*> aSpecifier,
@ -90,10 +92,11 @@ ModuleLoadRequest* ModuleLoadRequest::CreateDynamicImport(
MOZ_ASSERT(aSpecifier);
MOZ_ASSERT(aPromise);
auto request = new ModuleLoadRequest(
RefPtr<ModuleLoadRequest> request = new ModuleLoadRequest(
aURI, aFetchOptions, SRIMetadata(), aBaseURL, true, /* is top level */
true, /* is dynamic import */
aLoader->GetModuleLoader(), NewVisitedSetForTopLevelImport(aURI));
aLoader->GetModuleLoader(), NewVisitedSetForTopLevelImport(aURI),
nullptr);
DOMScriptLoadContext* context = new DOMScriptLoadContext(aElement, request);
context->mIsInline = false;
@ -103,20 +106,22 @@ ModuleLoadRequest* ModuleLoadRequest::CreateDynamicImport(
request->mDynamicSpecifier = aSpecifier;
request->mDynamicPromise = aPromise;
HoldJSObjects(request);
HoldJSObjects(request.get());
return request;
return request.forget();
}
ModuleLoadRequest::ModuleLoadRequest(
nsIURI* aURI, ScriptFetchOptions* aFetchOptions,
const SRIMetadata& aIntegrity, nsIURI* aReferrer, bool aIsTopLevel,
bool aIsDynamicImport, ModuleLoader* aLoader, VisitedURLSet* aVisitedSet)
bool aIsDynamicImport, ModuleLoader* aLoader, VisitedURLSet* aVisitedSet,
ModuleLoadRequest* aRootModule)
: ScriptLoadRequest(ScriptKind::eModule, aURI, aFetchOptions, aIntegrity,
aReferrer),
mIsTopLevel(aIsTopLevel),
mIsDynamicImport(aIsDynamicImport),
mLoader(aLoader),
mRootModule(aRootModule),
mVisitedSet(aVisitedSet) {}
void ModuleLoadRequest::Cancel() {

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

@ -43,7 +43,8 @@ class ModuleLoadRequest final : public ScriptLoadRequest {
ModuleLoadRequest(nsIURI* aURI, ScriptFetchOptions* aFetchOptions,
const SRIMetadata& aIntegrity, nsIURI* aReferrer,
bool aIsTopLevel, bool aIsDynamicImport,
ModuleLoader* aLoader, VisitedURLSet* aVisitedSet);
ModuleLoader* aLoader, VisitedURLSet* aVisitedSet,
ModuleLoadRequest* aRootModule);
public:
NS_DECL_ISUPPORTS_INHERITED
@ -51,16 +52,16 @@ class ModuleLoadRequest final : public ScriptLoadRequest {
ScriptLoadRequest)
// Create a top-level module load request.
static ModuleLoadRequest* CreateTopLevel(
static already_AddRefed<ModuleLoadRequest> CreateTopLevel(
nsIURI* aURI, ScriptFetchOptions* aFetchOptions, Element* aElement,
const SRIMetadata& aIntegrity, nsIURI* aReferrer, ScriptLoader* aLoader);
// Create a module load request for a static module import.
static ModuleLoadRequest* CreateStaticImport(nsIURI* aURI,
ModuleLoadRequest* aParent);
static already_AddRefed<ModuleLoadRequest> CreateStaticImport(
nsIURI* aURI, ModuleLoadRequest* aParent);
// Create a module load request for dynamic module import.
static ModuleLoadRequest* CreateDynamicImport(
static already_AddRefed<ModuleLoadRequest> CreateDynamicImport(
nsIURI* aURI, ScriptFetchOptions* aFetchOptions, nsIURI* aBaseURL,
Element* aElement, ScriptLoader* aLoader,
JS::Handle<JS::Value> aReferencingPrivate,
@ -79,6 +80,13 @@ class ModuleLoadRequest final : public ScriptLoadRequest {
void DependenciesLoaded();
void LoadFailed();
ModuleLoadRequest* GetRootModule() {
if (!mRootModule) {
return this;
}
return mRootModule;
}
private:
void LoadFinished();
void CancelImports();
@ -95,6 +103,10 @@ class ModuleLoadRequest final : public ScriptLoadRequest {
// finishes.
RefPtr<ModuleLoader> mLoader;
// Pointer to the top level module of this module tree, nullptr if this is
// a top level module
RefPtr<ModuleLoadRequest> mRootModule;
// Set to a module script object after a successful load or nullptr on
// failure.
RefPtr<ModuleScript> mModuleScript;

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

@ -44,7 +44,6 @@ ScriptFetchOptions::ScriptFetchOptions(mozilla::CORSMode aCORSMode,
nsIGlobalObject* aWebExtGlobal)
: mCORSMode(aCORSMode),
mReferrerPolicy(aReferrerPolicy),
mIsPreload(false),
mTriggeringPrincipal(aTriggeringPrincipal),
mWebExtGlobal(aWebExtGlobal) {
MOZ_ASSERT(mTriggeringPrincipal);
@ -139,6 +138,7 @@ DOMScriptLoadContext::DOMScriptLoadContext(Element* aElement,
mWasCompiledOMT(false),
mOffThreadToken(nullptr),
mRunnable(nullptr),
mIsPreload(false),
mElement(aElement),
mRequest(aRequest),
mUnreportedPreloadError(NS_OK) {}
@ -277,7 +277,21 @@ void DOMScriptLoadContext::PrioritizeAsPreload() {
}
}
bool DOMScriptLoadContext::IsPreload() const {
if (mRequest->IsModuleRequest() && !mRequest->IsTopLevel()) {
ModuleLoadRequest* root = mRequest->AsModuleRequest()->GetRootModule();
return root->mLoadContext->IsPreload();
}
MOZ_ASSERT_IF(mIsPreload, !GetScriptElement());
return mIsPreload;
}
nsIScriptElement* DOMScriptLoadContext::GetScriptElement() const {
if (mRequest->IsModuleRequest() && !mRequest->IsTopLevel()) {
ModuleLoadRequest* root = mRequest->AsModuleRequest()->GetRootModule();
return root->mLoadContext->GetScriptElement();
}
nsCOMPtr<nsIScriptElement> scriptElement = do_QueryInterface(mElement);
return scriptElement;
}
@ -288,7 +302,7 @@ void DOMScriptLoadContext::SetIsLoadRequest(nsIScriptElement* aElement) {
MOZ_ASSERT(IsPreload());
// TODO: How to allow both to access fetch options
mElement = do_QueryInterface(aElement);
mRequest->mFetchOptions->mIsPreload = false;
mIsPreload = false;
}
nsresult ScriptLoadRequest::GetScriptSource(JSContext* aCx,

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

@ -65,8 +65,6 @@ class ScriptFetchOptions {
const mozilla::CORSMode mCORSMode;
const enum ReferrerPolicy mReferrerPolicy;
bool mIsPreload;
nsCOMPtr<Element> mElement;
nsCOMPtr<nsIPrincipal> mTriggeringPrincipal;
// Global that initiated this request, when using a WebExtension
// content-script.
@ -363,10 +361,7 @@ class DOMScriptLoadContext : public PreloaderBase {
scriptElement->ScriptEvaluated(aResult, scriptElement, mIsInline);
}
bool IsPreload() const {
MOZ_ASSERT_IF(mRequest->mFetchOptions->mIsPreload, !GetScriptElement());
return mRequest->mFetchOptions->mIsPreload;
}
bool IsPreload() const;
bool CompileStarted() const {
return mRequest->InCompilingStage() ||
@ -413,7 +408,7 @@ class DOMScriptLoadContext : public PreloaderBase {
void SetIsPreloadRequest() {
MOZ_ASSERT(!GetScriptElement());
MOZ_ASSERT(!IsPreload());
mRequest->mFetchOptions->mIsPreload = true;
mIsPreload = true;
}
// Make a preload request into an actual load request for the given element.
@ -457,6 +452,8 @@ class DOMScriptLoadContext : public PreloaderBase {
// compile. Tracked here so that it can be
// properly released during cancellation.
// Set on scripts and top level modules.
bool mIsPreload;
nsCOMPtr<Element> mElement;
RefPtr<ScriptLoadRequest> mRequest;

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

@ -1137,7 +1137,7 @@ static bool CSPAllowsInlineScript(nsIScriptElement* aElement,
return NS_SUCCEEDED(rv) && allowInlineScript;
}
ScriptLoadRequest* ScriptLoader::CreateLoadRequest(
already_AddRefed<ScriptLoadRequest> ScriptLoader::CreateLoadRequest(
ScriptKind aKind, nsIURI* aURI, nsIScriptElement* aElement,
nsIPrincipal* aTriggeringPrincipal, CORSMode aCORSMode,
const SRIMetadata& aIntegrity, ReferrerPolicy aReferrerPolicy) {
@ -1147,17 +1147,18 @@ ScriptLoadRequest* ScriptLoader::CreateLoadRequest(
aCORSMode, aReferrerPolicy, aTriggeringPrincipal, nullptr);
if (aKind == ScriptKind::eClassic) {
ScriptLoadRequest* aRequest =
RefPtr<ScriptLoadRequest> aRequest =
new ScriptLoadRequest(aKind, aURI, fetchOptions, aIntegrity, referrer);
DOMScriptLoadContext* aContext =
new DOMScriptLoadContext(domElement, aRequest);
aRequest->mLoadContext = aContext;
return aRequest;
return aRequest.forget();
}
MOZ_ASSERT(aKind == ScriptKind::eModule);
return ModuleLoadRequest::CreateTopLevel(aURI, fetchOptions, domElement,
aIntegrity, referrer, this);
RefPtr<ModuleLoadRequest> aRequest = ModuleLoadRequest::CreateTopLevel(
aURI, fetchOptions, domElement, aIntegrity, referrer, this);
return aRequest.forget();
}
bool ScriptLoader::ProcessScriptElement(nsIScriptElement* aElement) {

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

@ -416,12 +416,10 @@ class ScriptLoader final : public ScriptLoaderInterface {
private:
virtual ~ScriptLoader();
ScriptLoadRequest* CreateLoadRequest(ScriptKind aKind, nsIURI* aURI,
nsIScriptElement* aElement,
nsIPrincipal* aTriggeringPrincipal,
mozilla::CORSMode aCORSMode,
const SRIMetadata& aIntegrity,
ReferrerPolicy aReferrerPolicy);
already_AddRefed<ScriptLoadRequest> CreateLoadRequest(
ScriptKind aKind, nsIURI* aURI, nsIScriptElement* aElement,
nsIPrincipal* aTriggeringPrincipal, mozilla::CORSMode aCORSMode,
const SRIMetadata& aIntegrity, ReferrerPolicy aReferrerPolicy);
/**
* Unblocks the creator parser of the parser-blocking scripts.