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".
options = new ScriptFetchOptions(
mozilla::CORS_NONE, /* aNonce = */ u""_ns, RequestPriority::Auto,
ParserMetadata::NotParserInserted, principal, nullptr);
ParserMetadata::NotParserInserted, principal);
referrerPolicy = document->GetReferrerPolicy();
baseURL = document->GetDocBaseURI();
}

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

@ -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<nsIScriptElement> 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<nsIContent> scriptContent(do_QueryInterface(GetScriptElement()));
nsCOMPtr<nsIContent> 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;
}

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

@ -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<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
// request is used.
nsresult mUnreportedPreloadError;

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

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

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

@ -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<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

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

@ -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;

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

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