diff --git a/dom/script/ScriptLoader.cpp b/dom/script/ScriptLoader.cpp index 6e385dd9df..e84ebcf151 100644 --- a/dom/script/ScriptLoader.cpp +++ b/dom/script/ScriptLoader.cpp @@ -89,6 +89,7 @@ ScriptFetchOptions::ScriptFetchOptions(mozilla::CORSMode aCORSMode, nsIPrincipal* aTriggeringPrincipal) : mCORSMode(aCORSMode) , mReferrerPolicy(aReferrerPolicy) + , mIsPreload(false) , mElement(aElement) , mTriggeringPrincipal(aTriggeringPrincipal) { @@ -1110,7 +1111,7 @@ ScriptLoader::ProcessLoadedModuleTree(ModuleLoadRequest* aRequest) RefPtr req = mDynamicImportRequests.Steal(aRequest); RunScriptWhenSafe(req); } else if (aRequest->mIsInline && - aRequest->Element()->GetParserCreated() == NOT_FROM_PARSER) { + aRequest->GetParserCreated() == NOT_FROM_PARSER) { RunScriptWhenSafe(aRequest); } else { MaybeMoveToLoadedList(aRequest); @@ -1510,7 +1511,7 @@ ScriptLoader::ProcessScriptElement(nsIScriptElement *aElement) // preloaded // note that a script-inserted script can steal a preload! request = mPreloads[i].mRequest; - request->SetElement(aElement); + request->SetIsLoadRequest(aElement); nsString preloadCharset(mPreloads[i].mCharset); mPreloads.RemoveElementAt(i); @@ -1981,7 +1982,7 @@ ScriptLoader::ProcessRequest(ScriptLoadRequest* aRequest) } nsCOMPtr oldParserInsertedScript; - uint32_t parserCreated = aRequest->Element()->GetParserCreated(); + uint32_t parserCreated = aRequest->GetParserCreated(); if (parserCreated) { oldParserInsertedScript = mCurrentParserInsertedScript; mCurrentParserInsertedScript = aRequest->Element(); @@ -2192,15 +2193,15 @@ ScriptLoader::EvaluateScript(ScriptLoadRequest* aRequest) } nsCOMPtr scriptContent(do_QueryInterface(aRequest->Element())); - nsIDocument* ownerDoc = scriptContent->OwnerDoc(); - if (ownerDoc != mDocument) { - // Willful violation of HTML5 as of 2010-12-01 - return NS_ERROR_FAILURE; + MOZ_ASSERT_IF(!scriptContent, aRequest->AsModuleRequest()->IsDynamicImport()); + if (scriptContent) { + nsIDocument* ownerDoc = scriptContent->OwnerDoc(); + if (ownerDoc != mDocument) { + // Willful violation of HTML5 as of 2010-12-01 + return NS_ERROR_FAILURE; + } } - // Get the script-type to be used by this element. - NS_ASSERTION(scriptContent, "no content - what is default script-type?"); - nsCOMPtr globalObject = GetScriptGlobalObject(); if (!globalObject) { return NS_ERROR_FAILURE; @@ -2955,6 +2956,7 @@ ScriptLoader::PreloadURI(nsIURI *aURI, aReferrerPolicy); request->mIsInline = false; request->SetScriptMode(aDefer, aAsync); + request->SetIsPreloadRequest(); nsresult rv = StartLoad(request, aType, aScriptFromHead); if (NS_FAILED(rv)) { diff --git a/dom/script/ScriptLoader.h b/dom/script/ScriptLoader.h index 2e2b1592cd..da39d0a630 100644 --- a/dom/script/ScriptLoader.h +++ b/dom/script/ScriptLoader.h @@ -70,6 +70,7 @@ public: const mozilla::CORSMode mCORSMode; const mozilla::net::ReferrerPolicy mReferrerPolicy; + bool mIsPreload; nsCOMPtr mElement; nsCOMPtr mTriggeringPrincipal; }; @@ -113,9 +114,9 @@ public: Element()->ScriptEvaluated(aResult, Element(), mIsInline); } - bool IsPreload() - { - return Element() == nullptr; + bool IsPreload() const { + MOZ_ASSERT_IF(mFetchOptions->mIsPreload, !Element()); + return mFetchOptions->mIsPreload; } virtual void Cancel(); @@ -195,13 +196,32 @@ public: return mFetchOptions->mTriggeringPrincipal; } - void SetElement(nsIScriptElement* aElement) + // Make this request a preload (speculative) request. + void SetIsPreloadRequest() + { + MOZ_ASSERT(!Element()); + MOZ_ASSERT(!IsPreload()); + mFetchOptions->mIsPreload = true; + } + + // Make a preload request into an actual load request for the given element. + void SetIsLoadRequest(nsIScriptElement* aElement) { - // Called when a preload request is later used for an actual request. MOZ_ASSERT(aElement); MOZ_ASSERT(!Element()); + MOZ_ASSERT(IsPreload()); mFetchOptions->mElement = aElement; - } + mFetchOptions->mIsPreload = false; + } + + FromParser GetParserCreated() const + { + nsIScriptElement* element = Element(); + if (!element) { + return NOT_FROM_PARSER; + } + return element->GetParserCreated(); + } void SetScript(JSScript* aScript);