diff --git a/dom/script/ModuleLoader.cpp b/dom/script/ModuleLoader.cpp index 0e325b23b122..821552629925 100644 --- a/dom/script/ModuleLoader.cpp +++ b/dom/script/ModuleLoader.cpp @@ -346,7 +346,7 @@ already_AddRefed ModuleLoader::CreateDynamicImport( // "auto". options = new ScriptFetchOptions( mozilla::CORS_NONE, /* aNonce = */ u""_ns, RequestPriority::Auto, - ParserMetadata::NotParserInserted, principal, nullptr); + ParserMetadata::NotParserInserted, principal); referrerPolicy = document->GetReferrerPolicy(); baseURL = document->GetDocBaseURI(); } diff --git a/dom/script/ScriptLoadContext.cpp b/dom/script/ScriptLoadContext.cpp index 781ba183b173..2cf2533bbdb1 100644 --- a/dom/script/ScriptLoadContext.cpp +++ b/dom/script/ScriptLoadContext.cpp @@ -38,17 +38,20 @@ NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(ScriptLoadContext, JS::loader::LoadContextBase) MOZ_ASSERT(!tmp->mCompileOrDecodeTask); tmp->MaybeUnblockOnload(); + NS_IMPL_CYCLE_COLLECTION_UNLINK(mScriptElement); NS_IMPL_CYCLE_COLLECTION_UNLINK_END NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(ScriptLoadContext, JS::loader::LoadContextBase) NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mLoadBlockedDocument) + NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mScriptElement); NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END NS_IMPL_ADDREF_INHERITED(ScriptLoadContext, JS::loader::LoadContextBase) NS_IMPL_RELEASE_INHERITED(ScriptLoadContext, JS::loader::LoadContextBase) -ScriptLoadContext::ScriptLoadContext() +ScriptLoadContext::ScriptLoadContext( + nsIScriptElement* aScriptElement /* = nullptr */) : JS::loader::LoadContextBase(JS::loader::ContextKind::Window), mScriptMode(ScriptMode::eBlocking), mScriptFromHead(false), @@ -63,6 +66,7 @@ ScriptLoadContext::ScriptLoadContext() mLineNo(1), mColumnNo(0), mIsPreload(false), + mScriptElement(aScriptElement), mUnreportedPreloadError(NS_OK) {} ScriptLoadContext::~ScriptLoadContext() { @@ -141,59 +145,54 @@ bool ScriptLoadContext::CompileStarted() const { return mRequest->IsCompiling() || (mRequest->IsFinished() && mWasCompiledOMT); } -nsIScriptElement* ScriptLoadContext::GetScriptElement() const { - nsCOMPtr scriptElement = - do_QueryInterface(mRequest->mFetchOptions->mElement); - return scriptElement; -} - -bool ScriptLoadContext::HasScriptElement() const { - return !!GetScriptElement(); -} +bool ScriptLoadContext::HasScriptElement() const { return !!mScriptElement; } void ScriptLoadContext::GetInlineScriptText(nsAString& aText) const { MOZ_ASSERT(mIsInline); - GetScriptElement()->GetScriptText(aText); + mScriptElement->GetScriptText(aText); } void ScriptLoadContext::GetHintCharset(nsAString& aCharset) const { - GetScriptElement()->GetScriptCharset(aCharset); + MOZ_ASSERT(mScriptElement); + mScriptElement->GetScriptCharset(aCharset); } uint32_t ScriptLoadContext::GetScriptLineNumber() const { - nsIScriptElement* element = GetScriptElement(); - if (element) { - return element->GetScriptLineNumber(); + if (mScriptElement) { + return mScriptElement->GetScriptLineNumber(); } return 0; } JS::ColumnNumberOneOrigin ScriptLoadContext::GetScriptColumnNumber() const { - nsIScriptElement* element = GetScriptElement(); - if (element) { - return element->GetScriptColumnNumber(); + if (mScriptElement) { + return mScriptElement->GetScriptColumnNumber(); } return JS::ColumnNumberOneOrigin(); } void ScriptLoadContext::BeginEvaluatingTopLevel() const { - GetScriptElement()->BeginEvaluating(); + MOZ_ASSERT(mScriptElement); + mScriptElement->BeginEvaluating(); } void ScriptLoadContext::EndEvaluatingTopLevel() const { - GetScriptElement()->EndEvaluating(); + MOZ_ASSERT(mScriptElement); + mScriptElement->EndEvaluating(); } void ScriptLoadContext::UnblockParser() const { - GetScriptElement()->UnblockParser(); + MOZ_ASSERT(mScriptElement); + mScriptElement->UnblockParser(); } void ScriptLoadContext::ContinueParserAsync() const { - GetScriptElement()->ContinueParserAsync(); + MOZ_ASSERT(mScriptElement); + mScriptElement->ContinueParserAsync(); } Document* ScriptLoadContext::GetScriptOwnerDocument() const { - nsCOMPtr scriptContent(do_QueryInterface(GetScriptElement())); + nsCOMPtr scriptContent(do_QueryInterface(mScriptElement)); MOZ_ASSERT(scriptContent); return scriptContent->OwnerDoc(); } @@ -202,9 +201,7 @@ void ScriptLoadContext::SetIsLoadRequest(nsIScriptElement* aElement) { MOZ_ASSERT(aElement); MOZ_ASSERT(!HasScriptElement()); MOZ_ASSERT(IsPreload()); - // We are not tracking our own element, and are relying on the one in - // FetchOptions. - mRequest->mFetchOptions->mElement = do_QueryInterface(aElement); + mScriptElement = aElement; mIsPreload = false; } diff --git a/dom/script/ScriptLoadContext.h b/dom/script/ScriptLoadContext.h index 1ec37f066b21..b92294cef56b 100644 --- a/dom/script/ScriptLoadContext.h +++ b/dom/script/ScriptLoadContext.h @@ -139,7 +139,7 @@ class ScriptLoadContext : public JS::loader::LoadContextBase, virtual ~ScriptLoadContext(); public: - explicit ScriptLoadContext(); + explicit ScriptLoadContext(nsIScriptElement* aScriptElement = nullptr); NS_DECL_ISUPPORTS_INHERITED NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(ScriptLoadContext, @@ -181,10 +181,6 @@ class ScriptLoadContext : public JS::loader::LoadContextBase, bool IsAsyncScript() const { return mScriptMode == ScriptMode::eAsync; } - private: - nsIScriptElement* GetScriptElement() const; - - public: // Accessors for the script element, for each purpose. // // The script element reference is guaranteed to be available only for: @@ -197,41 +193,46 @@ class ScriptLoadContext : public JS::loader::LoadContextBase, // TODO: This is basically unnecessary and a document can be used instead. // Remove this. inline nsIScriptElement* GetScriptElementForLoadingNode() const { - return GetScriptElement(); + MOZ_ASSERT(mScriptElement); + return mScriptElement; } // For TRACE_FOR_TEST macros. // NOTE: This is called also for imported modules. // The consumer allows nullptr. inline nsIScriptElement* GetScriptElementForTrace() const { - return GetScriptElement(); + return mScriptElement; } // Event target for beforescriptexecute/afterscriptexecute events. inline nsIScriptElement* GetScriptElementForExecuteEvents() const { - return GetScriptElement(); + MOZ_ASSERT(mScriptElement); + return mScriptElement; } // For ScriptLoader::mCurrentParserInsertedScript. inline nsIScriptElement* GetScriptElementForCurrentParserInsertedScript() const { - return GetScriptElement(); + MOZ_ASSERT(mScriptElement); + return mScriptElement; } // For nsIScriptLoaderObserver. inline nsIScriptElement* GetScriptElementForObserver() const { - return GetScriptElement(); + MOZ_ASSERT(mScriptElement); + return mScriptElement; } // For URL classifier. inline nsIScriptElement* GetScriptElementForUrlClassifier() const { - return GetScriptElement(); + return mScriptElement; } // For AutoCurrentScriptUpdater. // This is valid only for classic script. inline nsIScriptElement* GetScriptElementForCurrentScript() const { - return GetScriptElement(); + MOZ_ASSERT(mScriptElement); + return mScriptElement; } bool HasScriptElement() const; @@ -263,11 +264,10 @@ class ScriptLoadContext : public JS::loader::LoadContextBase, void SetIsLoadRequest(nsIScriptElement* aElement); FromParser GetParserCreated() const { - nsIScriptElement* element = GetScriptElement(); - if (!element) { + if (!mScriptElement) { return NOT_FROM_PARSER; } - return element->GetParserCreated(); + return mScriptElement->GetParserCreated(); } // Used to output a string for the Gecko Profiler. @@ -312,6 +312,10 @@ class ScriptLoadContext : public JS::loader::LoadContextBase, // Non-null if there is a document that this request is blocking from loading. RefPtr mLoadBlockedDocument; + // The script element which trigerred this script load. + // This is valid only for classic script and top-level module script. + nsCOMPtr mScriptElement; + // For preload requests, we defer reporting errors to the console until the // request is used. nsresult mUnreportedPreloadError; diff --git a/dom/script/ScriptLoader.cpp b/dom/script/ScriptLoader.cpp index e96c887edaf2..e65533af04ea 100644 --- a/dom/script/ScriptLoader.cpp +++ b/dom/script/ScriptLoader.cpp @@ -972,11 +972,10 @@ already_AddRefed ScriptLoader::CreateLoadRequest( const SRIMetadata& aIntegrity, ReferrerPolicy aReferrerPolicy, ParserMetadata aParserMetadata) { nsIURI* referrer = mDocument->GetDocumentURIAsReferrer(); - nsCOMPtr domElement = do_QueryInterface(aElement); RefPtr fetchOptions = new ScriptFetchOptions(aCORSMode, aNonce, aRequestPriority, - aParserMetadata, aTriggeringPrincipal, domElement); - RefPtr context = new ScriptLoadContext(); + aParserMetadata, aTriggeringPrincipal); + RefPtr context = new ScriptLoadContext(aElement); if (aKind == ScriptKind::eClassic || aKind == ScriptKind::eImportMap) { RefPtr aRequest = diff --git a/js/loader/ScriptFetchOptions.h b/js/loader/ScriptFetchOptions.h index 3275cbfc03ca..844527da1ca1 100644 --- a/js/loader/ScriptFetchOptions.h +++ b/js/loader/ScriptFetchOptions.h @@ -8,7 +8,6 @@ #define js_loader_ScriptFecthOptions_h #include "mozilla/CORSMode.h" -#include "mozilla/dom/Element.h" #include "mozilla/dom/ReferrerPolicyBinding.h" #include "mozilla/dom/RequestBinding.h" // RequestPriority #include "nsCOMPtr.h" @@ -55,8 +54,7 @@ class ScriptFetchOptions { ScriptFetchOptions(mozilla::CORSMode aCORSMode, const nsAString& aNonce, mozilla::dom::RequestPriority aFetchPriority, const ParserMetadata aParserMetadata, - nsIPrincipal* aTriggeringPrincipal, - mozilla::dom::Element* aElement = nullptr); + nsIPrincipal* aTriggeringPrincipal); /* * The credentials mode used for the initial fetch (for module scripts) @@ -88,14 +86,6 @@ class ScriptFetchOptions { * TODO: Move to ScriptLoadContext */ nsCOMPtr mTriggeringPrincipal; - /* - * Represents fields populated by DOM elements (nonce, parser metadata) - * Leave this field as a nullptr for any fetch that requires the - * default classic script options. - * (https://html.spec.whatwg.org/multipage/webappapis.html#default-classic-script-fetch-options) - * TODO: extract necessary fields rather than passing this object - */ - nsCOMPtr mElement; }; } // namespace JS::loader diff --git a/js/loader/ScriptLoadRequest.cpp b/js/loader/ScriptLoadRequest.cpp index 5c7a5a55f767..e0be0d1599b5 100644 --- a/js/loader/ScriptLoadRequest.cpp +++ b/js/loader/ScriptLoadRequest.cpp @@ -32,19 +32,17 @@ namespace JS::loader { // ScriptFetchOptions ////////////////////////////////////////////////////////////// -NS_IMPL_CYCLE_COLLECTION(ScriptFetchOptions, mTriggeringPrincipal, mElement) +NS_IMPL_CYCLE_COLLECTION(ScriptFetchOptions, mTriggeringPrincipal) ScriptFetchOptions::ScriptFetchOptions( mozilla::CORSMode aCORSMode, const nsAString& aNonce, mozilla::dom::RequestPriority aFetchPriority, - const ParserMetadata aParserMetadata, nsIPrincipal* aTriggeringPrincipal, - mozilla::dom::Element* aElement) + const ParserMetadata aParserMetadata, nsIPrincipal* aTriggeringPrincipal) : mCORSMode(aCORSMode), mNonce(aNonce), mFetchPriority(aFetchPriority), mParserMetadata(aParserMetadata), - mTriggeringPrincipal(aTriggeringPrincipal), - mElement(aElement) {} + mTriggeringPrincipal(aTriggeringPrincipal) {} ScriptFetchOptions::~ScriptFetchOptions() = default; diff --git a/js/loader/ScriptLoadRequest.h b/js/loader/ScriptLoadRequest.h index b9150f420970..f2d95d85dfa0 100644 --- a/js/loader/ScriptLoadRequest.h +++ b/js/loader/ScriptLoadRequest.h @@ -24,7 +24,6 @@ #include "LoadedScript.h" #include "ScriptKind.h" #include "ScriptFetchOptions.h" -#include "nsIScriptElement.h" class nsICacheInfoChannel;