Bug 1899172 - Part 11: Move nsIScriptElement from ScriptFetchOptions to ScriptLoadContext. r=nbp

Differential Revision: https://phabricator.services.mozilla.com/D211912
This commit is contained in:
Tooru Fujisawa 2024-05-30 05:01:18 +00:00
Родитель 7e60adcb96
Коммит d56e00ca0e
7 изменённых файлов: 49 добавлений и 62 удалений

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

@ -346,7 +346,7 @@ already_AddRefed<ModuleLoadRequest> ModuleLoader::CreateDynamicImport(
// "auto". // "auto".
options = new ScriptFetchOptions( options = new ScriptFetchOptions(
mozilla::CORS_NONE, /* aNonce = */ u""_ns, RequestPriority::Auto, mozilla::CORS_NONE, /* aNonce = */ u""_ns, RequestPriority::Auto,
ParserMetadata::NotParserInserted, principal, nullptr); ParserMetadata::NotParserInserted, principal);
referrerPolicy = document->GetReferrerPolicy(); referrerPolicy = document->GetReferrerPolicy();
baseURL = document->GetDocBaseURI(); baseURL = document->GetDocBaseURI();
} }

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

@ -38,17 +38,20 @@ NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(ScriptLoadContext,
JS::loader::LoadContextBase) JS::loader::LoadContextBase)
MOZ_ASSERT(!tmp->mCompileOrDecodeTask); MOZ_ASSERT(!tmp->mCompileOrDecodeTask);
tmp->MaybeUnblockOnload(); tmp->MaybeUnblockOnload();
NS_IMPL_CYCLE_COLLECTION_UNLINK(mScriptElement);
NS_IMPL_CYCLE_COLLECTION_UNLINK_END NS_IMPL_CYCLE_COLLECTION_UNLINK_END
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(ScriptLoadContext, NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(ScriptLoadContext,
JS::loader::LoadContextBase) JS::loader::LoadContextBase)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mLoadBlockedDocument) NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mLoadBlockedDocument)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mScriptElement);
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
NS_IMPL_ADDREF_INHERITED(ScriptLoadContext, JS::loader::LoadContextBase) NS_IMPL_ADDREF_INHERITED(ScriptLoadContext, JS::loader::LoadContextBase)
NS_IMPL_RELEASE_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), : JS::loader::LoadContextBase(JS::loader::ContextKind::Window),
mScriptMode(ScriptMode::eBlocking), mScriptMode(ScriptMode::eBlocking),
mScriptFromHead(false), mScriptFromHead(false),
@ -63,6 +66,7 @@ ScriptLoadContext::ScriptLoadContext()
mLineNo(1), mLineNo(1),
mColumnNo(0), mColumnNo(0),
mIsPreload(false), mIsPreload(false),
mScriptElement(aScriptElement),
mUnreportedPreloadError(NS_OK) {} mUnreportedPreloadError(NS_OK) {}
ScriptLoadContext::~ScriptLoadContext() { ScriptLoadContext::~ScriptLoadContext() {
@ -141,59 +145,54 @@ bool ScriptLoadContext::CompileStarted() const {
return mRequest->IsCompiling() || (mRequest->IsFinished() && mWasCompiledOMT); return mRequest->IsCompiling() || (mRequest->IsFinished() && mWasCompiledOMT);
} }
nsIScriptElement* ScriptLoadContext::GetScriptElement() const { bool ScriptLoadContext::HasScriptElement() const { return !!mScriptElement; }
nsCOMPtr<nsIScriptElement> scriptElement =
do_QueryInterface(mRequest->mFetchOptions->mElement);
return scriptElement;
}
bool ScriptLoadContext::HasScriptElement() const {
return !!GetScriptElement();
}
void ScriptLoadContext::GetInlineScriptText(nsAString& aText) const { void ScriptLoadContext::GetInlineScriptText(nsAString& aText) const {
MOZ_ASSERT(mIsInline); MOZ_ASSERT(mIsInline);
GetScriptElement()->GetScriptText(aText); mScriptElement->GetScriptText(aText);
} }
void ScriptLoadContext::GetHintCharset(nsAString& aCharset) const { void ScriptLoadContext::GetHintCharset(nsAString& aCharset) const {
GetScriptElement()->GetScriptCharset(aCharset); MOZ_ASSERT(mScriptElement);
mScriptElement->GetScriptCharset(aCharset);
} }
uint32_t ScriptLoadContext::GetScriptLineNumber() const { uint32_t ScriptLoadContext::GetScriptLineNumber() const {
nsIScriptElement* element = GetScriptElement(); if (mScriptElement) {
if (element) { return mScriptElement->GetScriptLineNumber();
return element->GetScriptLineNumber();
} }
return 0; return 0;
} }
JS::ColumnNumberOneOrigin ScriptLoadContext::GetScriptColumnNumber() const { JS::ColumnNumberOneOrigin ScriptLoadContext::GetScriptColumnNumber() const {
nsIScriptElement* element = GetScriptElement(); if (mScriptElement) {
if (element) { return mScriptElement->GetScriptColumnNumber();
return element->GetScriptColumnNumber();
} }
return JS::ColumnNumberOneOrigin(); return JS::ColumnNumberOneOrigin();
} }
void ScriptLoadContext::BeginEvaluatingTopLevel() const { void ScriptLoadContext::BeginEvaluatingTopLevel() const {
GetScriptElement()->BeginEvaluating(); MOZ_ASSERT(mScriptElement);
mScriptElement->BeginEvaluating();
} }
void ScriptLoadContext::EndEvaluatingTopLevel() const { void ScriptLoadContext::EndEvaluatingTopLevel() const {
GetScriptElement()->EndEvaluating(); MOZ_ASSERT(mScriptElement);
mScriptElement->EndEvaluating();
} }
void ScriptLoadContext::UnblockParser() const { void ScriptLoadContext::UnblockParser() const {
GetScriptElement()->UnblockParser(); MOZ_ASSERT(mScriptElement);
mScriptElement->UnblockParser();
} }
void ScriptLoadContext::ContinueParserAsync() const { void ScriptLoadContext::ContinueParserAsync() const {
GetScriptElement()->ContinueParserAsync(); MOZ_ASSERT(mScriptElement);
mScriptElement->ContinueParserAsync();
} }
Document* ScriptLoadContext::GetScriptOwnerDocument() const { Document* ScriptLoadContext::GetScriptOwnerDocument() const {
nsCOMPtr<nsIContent> scriptContent(do_QueryInterface(GetScriptElement())); nsCOMPtr<nsIContent> scriptContent(do_QueryInterface(mScriptElement));
MOZ_ASSERT(scriptContent); MOZ_ASSERT(scriptContent);
return scriptContent->OwnerDoc(); return scriptContent->OwnerDoc();
} }
@ -202,9 +201,7 @@ void ScriptLoadContext::SetIsLoadRequest(nsIScriptElement* aElement) {
MOZ_ASSERT(aElement); MOZ_ASSERT(aElement);
MOZ_ASSERT(!HasScriptElement()); MOZ_ASSERT(!HasScriptElement());
MOZ_ASSERT(IsPreload()); MOZ_ASSERT(IsPreload());
// We are not tracking our own element, and are relying on the one in mScriptElement = aElement;
// FetchOptions.
mRequest->mFetchOptions->mElement = do_QueryInterface(aElement);
mIsPreload = false; mIsPreload = false;
} }

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

@ -139,7 +139,7 @@ class ScriptLoadContext : public JS::loader::LoadContextBase,
virtual ~ScriptLoadContext(); virtual ~ScriptLoadContext();
public: public:
explicit ScriptLoadContext(); explicit ScriptLoadContext(nsIScriptElement* aScriptElement = nullptr);
NS_DECL_ISUPPORTS_INHERITED NS_DECL_ISUPPORTS_INHERITED
NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(ScriptLoadContext, NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(ScriptLoadContext,
@ -181,10 +181,6 @@ class ScriptLoadContext : public JS::loader::LoadContextBase,
bool IsAsyncScript() const { return mScriptMode == ScriptMode::eAsync; } bool IsAsyncScript() const { return mScriptMode == ScriptMode::eAsync; }
private:
nsIScriptElement* GetScriptElement() const;
public:
// Accessors for the script element, for each purpose. // Accessors for the script element, for each purpose.
// //
// The script element reference is guaranteed to be available only for: // 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. // TODO: This is basically unnecessary and a document can be used instead.
// Remove this. // Remove this.
inline nsIScriptElement* GetScriptElementForLoadingNode() const { inline nsIScriptElement* GetScriptElementForLoadingNode() const {
return GetScriptElement(); MOZ_ASSERT(mScriptElement);
return mScriptElement;
} }
// For TRACE_FOR_TEST macros. // For TRACE_FOR_TEST macros.
// NOTE: This is called also for imported modules. // NOTE: This is called also for imported modules.
// The consumer allows nullptr. // The consumer allows nullptr.
inline nsIScriptElement* GetScriptElementForTrace() const { inline nsIScriptElement* GetScriptElementForTrace() const {
return GetScriptElement(); return mScriptElement;
} }
// Event target for beforescriptexecute/afterscriptexecute events. // Event target for beforescriptexecute/afterscriptexecute events.
inline nsIScriptElement* GetScriptElementForExecuteEvents() const { inline nsIScriptElement* GetScriptElementForExecuteEvents() const {
return GetScriptElement(); MOZ_ASSERT(mScriptElement);
return mScriptElement;
} }
// For ScriptLoader::mCurrentParserInsertedScript. // For ScriptLoader::mCurrentParserInsertedScript.
inline nsIScriptElement* GetScriptElementForCurrentParserInsertedScript() inline nsIScriptElement* GetScriptElementForCurrentParserInsertedScript()
const { const {
return GetScriptElement(); MOZ_ASSERT(mScriptElement);
return mScriptElement;
} }
// For nsIScriptLoaderObserver. // For nsIScriptLoaderObserver.
inline nsIScriptElement* GetScriptElementForObserver() const { inline nsIScriptElement* GetScriptElementForObserver() const {
return GetScriptElement(); MOZ_ASSERT(mScriptElement);
return mScriptElement;
} }
// For URL classifier. // For URL classifier.
inline nsIScriptElement* GetScriptElementForUrlClassifier() const { inline nsIScriptElement* GetScriptElementForUrlClassifier() const {
return GetScriptElement(); return mScriptElement;
} }
// For AutoCurrentScriptUpdater. // For AutoCurrentScriptUpdater.
// This is valid only for classic script. // This is valid only for classic script.
inline nsIScriptElement* GetScriptElementForCurrentScript() const { inline nsIScriptElement* GetScriptElementForCurrentScript() const {
return GetScriptElement(); MOZ_ASSERT(mScriptElement);
return mScriptElement;
} }
bool HasScriptElement() const; bool HasScriptElement() const;
@ -263,11 +264,10 @@ class ScriptLoadContext : public JS::loader::LoadContextBase,
void SetIsLoadRequest(nsIScriptElement* aElement); void SetIsLoadRequest(nsIScriptElement* aElement);
FromParser GetParserCreated() const { FromParser GetParserCreated() const {
nsIScriptElement* element = GetScriptElement(); if (!mScriptElement) {
if (!element) {
return NOT_FROM_PARSER; return NOT_FROM_PARSER;
} }
return element->GetParserCreated(); return mScriptElement->GetParserCreated();
} }
// Used to output a string for the Gecko Profiler. // 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. // Non-null if there is a document that this request is blocking from loading.
RefPtr<Document> mLoadBlockedDocument; RefPtr<Document> mLoadBlockedDocument;
// The script element which trigerred this script load.
// This is valid only for classic script and top-level module script.
nsCOMPtr<nsIScriptElement> mScriptElement;
// For preload requests, we defer reporting errors to the console until the // For preload requests, we defer reporting errors to the console until the
// request is used. // request is used.
nsresult mUnreportedPreloadError; nsresult mUnreportedPreloadError;

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

@ -972,11 +972,10 @@ already_AddRefed<ScriptLoadRequest> ScriptLoader::CreateLoadRequest(
const SRIMetadata& aIntegrity, ReferrerPolicy aReferrerPolicy, const SRIMetadata& aIntegrity, ReferrerPolicy aReferrerPolicy,
ParserMetadata aParserMetadata) { ParserMetadata aParserMetadata) {
nsIURI* referrer = mDocument->GetDocumentURIAsReferrer(); nsIURI* referrer = mDocument->GetDocumentURIAsReferrer();
nsCOMPtr<Element> domElement = do_QueryInterface(aElement);
RefPtr<ScriptFetchOptions> fetchOptions = RefPtr<ScriptFetchOptions> fetchOptions =
new ScriptFetchOptions(aCORSMode, aNonce, aRequestPriority, new ScriptFetchOptions(aCORSMode, aNonce, aRequestPriority,
aParserMetadata, aTriggeringPrincipal, domElement); aParserMetadata, aTriggeringPrincipal);
RefPtr<ScriptLoadContext> context = new ScriptLoadContext(); RefPtr<ScriptLoadContext> context = new ScriptLoadContext(aElement);
if (aKind == ScriptKind::eClassic || aKind == ScriptKind::eImportMap) { if (aKind == ScriptKind::eClassic || aKind == ScriptKind::eImportMap) {
RefPtr<ScriptLoadRequest> aRequest = RefPtr<ScriptLoadRequest> aRequest =

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

@ -8,7 +8,6 @@
#define js_loader_ScriptFecthOptions_h #define js_loader_ScriptFecthOptions_h
#include "mozilla/CORSMode.h" #include "mozilla/CORSMode.h"
#include "mozilla/dom/Element.h"
#include "mozilla/dom/ReferrerPolicyBinding.h" #include "mozilla/dom/ReferrerPolicyBinding.h"
#include "mozilla/dom/RequestBinding.h" // RequestPriority #include "mozilla/dom/RequestBinding.h" // RequestPriority
#include "nsCOMPtr.h" #include "nsCOMPtr.h"
@ -55,8 +54,7 @@ class ScriptFetchOptions {
ScriptFetchOptions(mozilla::CORSMode aCORSMode, const nsAString& aNonce, ScriptFetchOptions(mozilla::CORSMode aCORSMode, const nsAString& aNonce,
mozilla::dom::RequestPriority aFetchPriority, mozilla::dom::RequestPriority aFetchPriority,
const ParserMetadata aParserMetadata, const ParserMetadata aParserMetadata,
nsIPrincipal* aTriggeringPrincipal, nsIPrincipal* aTriggeringPrincipal);
mozilla::dom::Element* aElement = nullptr);
/* /*
* The credentials mode used for the initial fetch (for module scripts) * The credentials mode used for the initial fetch (for module scripts)
@ -88,14 +86,6 @@ class ScriptFetchOptions {
* TODO: Move to ScriptLoadContext * TODO: Move to ScriptLoadContext
*/ */
nsCOMPtr<nsIPrincipal> mTriggeringPrincipal; nsCOMPtr<nsIPrincipal> 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<mozilla::dom::Element> mElement;
}; };
} // namespace JS::loader } // namespace JS::loader

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

@ -32,19 +32,17 @@ namespace JS::loader {
// ScriptFetchOptions // ScriptFetchOptions
////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////
NS_IMPL_CYCLE_COLLECTION(ScriptFetchOptions, mTriggeringPrincipal, mElement) NS_IMPL_CYCLE_COLLECTION(ScriptFetchOptions, mTriggeringPrincipal)
ScriptFetchOptions::ScriptFetchOptions( ScriptFetchOptions::ScriptFetchOptions(
mozilla::CORSMode aCORSMode, const nsAString& aNonce, mozilla::CORSMode aCORSMode, const nsAString& aNonce,
mozilla::dom::RequestPriority aFetchPriority, mozilla::dom::RequestPriority aFetchPriority,
const ParserMetadata aParserMetadata, nsIPrincipal* aTriggeringPrincipal, const ParserMetadata aParserMetadata, nsIPrincipal* aTriggeringPrincipal)
mozilla::dom::Element* aElement)
: mCORSMode(aCORSMode), : mCORSMode(aCORSMode),
mNonce(aNonce), mNonce(aNonce),
mFetchPriority(aFetchPriority), mFetchPriority(aFetchPriority),
mParserMetadata(aParserMetadata), mParserMetadata(aParserMetadata),
mTriggeringPrincipal(aTriggeringPrincipal), mTriggeringPrincipal(aTriggeringPrincipal) {}
mElement(aElement) {}
ScriptFetchOptions::~ScriptFetchOptions() = default; ScriptFetchOptions::~ScriptFetchOptions() = default;

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

@ -24,7 +24,6 @@
#include "LoadedScript.h" #include "LoadedScript.h"
#include "ScriptKind.h" #include "ScriptKind.h"
#include "ScriptFetchOptions.h" #include "ScriptFetchOptions.h"
#include "nsIScriptElement.h"
class nsICacheInfoChannel; class nsICacheInfoChannel;