Bug 1742437 - move mElement from ScriptFetchOptions to DOMScriptLoadContext; r=jonco,smaug

Differential Revision: https://phabricator.services.mozilla.com/D136742
This commit is contained in:
Yulia Startsev 2022-02-21 15:39:05 +00:00
Родитель 7ded97bc1a
Коммит f1640d290a
10 изменённых файлов: 76 добавлений и 53 удалений

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

@ -1235,10 +1235,11 @@ nsresult EventListenerManager::CompileEventHandlerInternal(
}
RefPtr<ScriptFetchOptions> fetchOptions = new ScriptFetchOptions(
CORS_NONE, aElement->OwnerDoc()->GetReferrerPolicy(), aElement,
CORS_NONE, aElement->OwnerDoc()->GetReferrerPolicy(),
aElement->OwnerDoc()->NodePrincipal(), nullptr);
RefPtr<EventScript> eventScript = new EventScript(fetchOptions, uri);
RefPtr<EventScript> eventScript =
new EventScript(fetchOptions, uri, aElement);
JS::CompileOptions options(cx);
// Use line 0 to make the function body starts from line 1.

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

@ -7,6 +7,7 @@
#include "LoadedScript.h"
#include "mozilla/HoldDropJSObjects.h"
#include "mozilla/dom/Element.h"
#include "jsfriendapi.h"
#include "js/Modules.h" // JS::{Get,Set}ModulePrivate
@ -27,10 +28,11 @@ NS_IMPL_CYCLE_COLLECTION_CLASS(LoadedScript)
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(LoadedScript)
NS_IMPL_CYCLE_COLLECTION_UNLINK(mFetchOptions)
NS_IMPL_CYCLE_COLLECTION_UNLINK(mBaseURL)
NS_IMPL_CYCLE_COLLECTION_UNLINK(mElement)
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(LoadedScript)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mFetchOptions)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mFetchOptions, mElement)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(LoadedScript)
@ -40,8 +42,11 @@ NS_IMPL_CYCLE_COLLECTING_ADDREF(LoadedScript)
NS_IMPL_CYCLE_COLLECTING_RELEASE(LoadedScript)
LoadedScript::LoadedScript(ScriptKind aKind, ScriptFetchOptions* aFetchOptions,
nsIURI* aBaseURL)
: mKind(aKind), mFetchOptions(aFetchOptions), mBaseURL(aBaseURL) {
nsIURI* aBaseURL, Element* aElement)
: mKind(aKind),
mFetchOptions(aFetchOptions),
mBaseURL(aBaseURL),
mElement(aElement) {
MOZ_ASSERT(mFetchOptions);
MOZ_ASSERT(mBaseURL);
}
@ -91,16 +96,17 @@ void HostReleaseTopLevelScript(const JS::Value& aPrivate) {
// EventScript
//////////////////////////////////////////////////////////////
EventScript::EventScript(ScriptFetchOptions* aFetchOptions, nsIURI* aBaseURL)
: LoadedScript(ScriptKind::eEvent, aFetchOptions, aBaseURL) {}
EventScript::EventScript(ScriptFetchOptions* aFetchOptions, nsIURI* aBaseURL,
Element* aElement)
: LoadedScript(ScriptKind::eEvent, aFetchOptions, aBaseURL, aElement) {}
//////////////////////////////////////////////////////////////
// ClassicScript
//////////////////////////////////////////////////////////////
ClassicScript::ClassicScript(ScriptFetchOptions* aFetchOptions,
nsIURI* aBaseURL)
: LoadedScript(ScriptKind::eClassic, aFetchOptions, aBaseURL) {}
nsIURI* aBaseURL, Element* aElement)
: LoadedScript(ScriptKind::eClassic, aFetchOptions, aBaseURL, aElement) {}
//////////////////////////////////////////////////////////////
// ModuleScript
@ -129,8 +135,9 @@ NS_IMPL_CYCLE_COLLECTION_TRACE_END
NS_IMPL_ADDREF_INHERITED(ModuleScript, LoadedScript)
NS_IMPL_RELEASE_INHERITED(ModuleScript, LoadedScript)
ModuleScript::ModuleScript(ScriptFetchOptions* aFetchOptions, nsIURI* aBaseURL)
: LoadedScript(ScriptKind::eModule, aFetchOptions, aBaseURL),
ModuleScript::ModuleScript(ScriptFetchOptions* aFetchOptions, nsIURI* aBaseURL,
Element* aElement)
: LoadedScript(ScriptKind::eModule, aFetchOptions, aBaseURL, aElement),
mDebuggerDataInitialized(false) {
MOZ_ASSERT(!ModuleRecord());
MOZ_ASSERT(!HasParseError());

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

@ -17,6 +17,7 @@ class nsIURI;
namespace mozilla {
namespace dom {
class Element;
class ScriptLoader;
void HostAddRefTopLevelScript(const JS::Value& aPrivate);
@ -30,10 +31,11 @@ class LoadedScript : public nsISupports {
ScriptKind mKind;
RefPtr<ScriptFetchOptions> mFetchOptions;
nsCOMPtr<nsIURI> mBaseURL;
nsCOMPtr<Element> mElement;
protected:
LoadedScript(ScriptKind aKind, ScriptFetchOptions* aFetchOptions,
nsIURI* aBaseURL);
nsIURI* aBaseURL, Element* aElement);
virtual ~LoadedScript();
@ -48,7 +50,12 @@ class LoadedScript : public nsISupports {
inline ModuleScript* AsModuleScript();
inline EventScript* AsEventScript();
// Used to propagate Fetch Options to child modules
ScriptFetchOptions* GetFetchOptions() const { return mFetchOptions; }
// Used by the Debugger to get the associated Script Element
Element* GetScriptElement() const { return mElement; }
nsIURI* BaseURL() const { return mBaseURL; }
void AssociateWithScript(JSScript* aScript);
@ -58,14 +65,16 @@ class ClassicScript final : public LoadedScript {
~ClassicScript() = default;
public:
ClassicScript(ScriptFetchOptions* aFetchOptions, nsIURI* aBaseURL);
ClassicScript(ScriptFetchOptions* aFetchOptions, nsIURI* aBaseURL,
Element* aElement);
};
class EventScript final : public LoadedScript {
~EventScript() = default;
public:
EventScript(ScriptFetchOptions* aFetchOptions, nsIURI* aBaseURL);
EventScript(ScriptFetchOptions* aFetchOptions, nsIURI* aBaseURL,
Element* aElement);
};
// A single module script. May be used to satisfy multiple load requests.
@ -83,7 +92,8 @@ class ModuleScript final : public LoadedScript {
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_INHERITED(ModuleScript,
LoadedScript)
ModuleScript(ScriptFetchOptions* aFetchOptions, nsIURI* aBaseURL);
ModuleScript(ScriptFetchOptions* aFetchOptions, nsIURI* aBaseURL,
Element* aElement);
void SetModuleRecord(JS::Handle<JSObject*> aModuleRecord);
void SetParseError(const JS::Value& aError);

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

@ -52,13 +52,13 @@ static VisitedURLSet* NewVisitedSetForTopLevelImport(nsIURI* aURI) {
/* static */
ModuleLoadRequest* ModuleLoadRequest::CreateTopLevel(
nsIURI* aURI, ScriptFetchOptions* aFetchOptions,
nsIURI* aURI, ScriptFetchOptions* aFetchOptions, Element* aElement,
const SRIMetadata& aIntegrity, nsIURI* aReferrer, ScriptLoader* aLoader) {
auto* request = new ModuleLoadRequest(
aURI, aFetchOptions, aIntegrity, aReferrer, true, /* is top level */
false, /* is dynamic import */
aLoader->GetModuleLoader(), NewVisitedSetForTopLevelImport(aURI));
DOMScriptLoadContext* context = new DOMScriptLoadContext(request);
DOMScriptLoadContext* context = new DOMScriptLoadContext(aElement, request);
request->mLoadContext = context;
return request;
}
@ -71,7 +71,8 @@ ModuleLoadRequest* ModuleLoadRequest::CreateStaticImport(
aParent->mURI, false, /* is top level */
false, /* is dynamic import */
aParent->mLoader, aParent->mVisitedSet);
DOMScriptLoadContext* context = new DOMScriptLoadContext(request);
DOMScriptLoadContext* context =
new DOMScriptLoadContext(aParent->mLoadContext->mElement, request);
context->mIsInline = false;
context->mScriptMode = aParent->GetLoadContext()->mScriptMode;
@ -83,8 +84,9 @@ ModuleLoadRequest* ModuleLoadRequest::CreateStaticImport(
/* static */
ModuleLoadRequest* ModuleLoadRequest::CreateDynamicImport(
nsIURI* aURI, ScriptFetchOptions* aFetchOptions, nsIURI* aBaseURL,
ScriptLoader* aLoader, JS::Handle<JS::Value> aReferencingPrivate,
JS::Handle<JSString*> aSpecifier, JS::Handle<JSObject*> aPromise) {
Element* aElement, ScriptLoader* aLoader,
JS::Handle<JS::Value> aReferencingPrivate, JS::Handle<JSString*> aSpecifier,
JS::Handle<JSObject*> aPromise) {
MOZ_ASSERT(aSpecifier);
MOZ_ASSERT(aPromise);
@ -93,7 +95,7 @@ ModuleLoadRequest* ModuleLoadRequest::CreateDynamicImport(
true, /* is dynamic import */
aLoader->GetModuleLoader(), NewVisitedSetForTopLevelImport(aURI));
DOMScriptLoadContext* context = new DOMScriptLoadContext(request);
DOMScriptLoadContext* context = new DOMScriptLoadContext(aElement, request);
context->mIsInline = false;
context->mScriptMode = DOMScriptLoadContext::ScriptMode::eAsync;
request->mLoadContext = context;

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

@ -51,11 +51,9 @@ class ModuleLoadRequest final : public ScriptLoadRequest {
ScriptLoadRequest)
// Create a top-level module load request.
static ModuleLoadRequest* CreateTopLevel(nsIURI* aURI,
ScriptFetchOptions* aFetchOptions,
const SRIMetadata& aIntegrity,
nsIURI* aReferrer,
ScriptLoader* aLoader);
static 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,
@ -64,7 +62,8 @@ class ModuleLoadRequest final : public ScriptLoadRequest {
// Create a module load request for dynamic module import.
static ModuleLoadRequest* CreateDynamicImport(
nsIURI* aURI, ScriptFetchOptions* aFetchOptions, nsIURI* aBaseURL,
ScriptLoader* aLoader, JS::Handle<JS::Value> aReferencingPrivate,
Element* aElement, ScriptLoader* aLoader,
JS::Handle<JS::Value> aReferencingPrivate,
JS::Handle<JSString*> aSpecifier, JS::Handle<JSObject*> aPromise);
bool IsTopLevel() const override { return mIsTopLevel; }

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

@ -267,7 +267,8 @@ nsresult ModuleLoader::CreateModuleScript(ModuleLoadRequest* aRequest) {
}
RefPtr<ModuleScript> moduleScript =
new ModuleScript(aRequest->mFetchOptions, aRequest->mBaseURL);
new ModuleScript(aRequest->mFetchOptions, aRequest->mBaseURL,
aRequest->mLoadContext->mElement);
aRequest->mModuleScript = moduleScript;
if (!module) {

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

@ -32,7 +32,7 @@ namespace dom {
// ScriptFetchOptions
//////////////////////////////////////////////////////////////
NS_IMPL_CYCLE_COLLECTION(ScriptFetchOptions, mElement, mTriggeringPrincipal,
NS_IMPL_CYCLE_COLLECTION(ScriptFetchOptions, mTriggeringPrincipal,
mWebExtGlobal)
NS_IMPL_CYCLE_COLLECTION_ROOT_NATIVE(ScriptFetchOptions, AddRef)
@ -40,13 +40,11 @@ NS_IMPL_CYCLE_COLLECTION_UNROOT_NATIVE(ScriptFetchOptions, Release)
ScriptFetchOptions::ScriptFetchOptions(mozilla::CORSMode aCORSMode,
ReferrerPolicy aReferrerPolicy,
Element* aElement,
nsIPrincipal* aTriggeringPrincipal,
nsIGlobalObject* aWebExtGlobal)
: mCORSMode(aCORSMode),
mReferrerPolicy(aReferrerPolicy),
mIsPreload(false),
mElement(aElement),
mTriggeringPrincipal(aTriggeringPrincipal),
mWebExtGlobal(aWebExtGlobal) {
MOZ_ASSERT(mTriggeringPrincipal);
@ -112,6 +110,7 @@ NS_IMPL_CYCLE_COLLECTING_RELEASE(DOMScriptLoadContext)
NS_IMPL_CYCLE_COLLECTION_CLASS(DOMScriptLoadContext)
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(DOMScriptLoadContext)
NS_IMPL_CYCLE_COLLECTION_UNLINK(mElement)
// XXX missing mLoadBlockedDocument ?
if (Runnable* runnable = tmp->mRunnable.exchange(nullptr)) {
runnable->Release();
@ -120,13 +119,14 @@ NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(DOMScriptLoadContext)
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(DOMScriptLoadContext)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mLoadBlockedDocument, mRequest)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mLoadBlockedDocument, mRequest, mElement)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(DOMScriptLoadContext)
NS_IMPL_CYCLE_COLLECTION_TRACE_END
DOMScriptLoadContext::DOMScriptLoadContext(ScriptLoadRequest* aRequest)
DOMScriptLoadContext::DOMScriptLoadContext(Element* aElement,
ScriptLoadRequest* aRequest)
: mScriptMode(ScriptMode::eBlocking),
mScriptFromHead(false),
mIsInline(true),
@ -139,6 +139,7 @@ DOMScriptLoadContext::DOMScriptLoadContext(ScriptLoadRequest* aRequest)
mWasCompiledOMT(false),
mOffThreadToken(nullptr),
mRunnable(nullptr),
mElement(aElement),
mRequest(aRequest),
mUnreportedPreloadError(NS_OK) {}
@ -277,8 +278,7 @@ void DOMScriptLoadContext::PrioritizeAsPreload() {
}
nsIScriptElement* DOMScriptLoadContext::GetScriptElement() const {
nsCOMPtr<nsIScriptElement> scriptElement =
do_QueryInterface(mRequest->mFetchOptions->mElement);
nsCOMPtr<nsIScriptElement> scriptElement = do_QueryInterface(mElement);
return scriptElement;
}
@ -286,7 +286,8 @@ void DOMScriptLoadContext::SetIsLoadRequest(nsIScriptElement* aElement) {
MOZ_ASSERT(aElement);
MOZ_ASSERT(!GetScriptElement());
MOZ_ASSERT(IsPreload());
mRequest->mFetchOptions->mElement = do_QueryInterface(aElement);
// TODO: How to allow both to access fetch options
mElement = do_QueryInterface(aElement);
mRequest->mFetchOptions->mIsPreload = false;
}

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

@ -59,7 +59,7 @@ class ScriptFetchOptions {
NS_DECL_CYCLE_COLLECTION_NATIVE_CLASS(ScriptFetchOptions)
ScriptFetchOptions(mozilla::CORSMode aCORSMode,
enum ReferrerPolicy aReferrerPolicy, Element* aElement,
enum ReferrerPolicy aReferrerPolicy,
nsIPrincipal* aTriggeringPrincipal,
nsIGlobalObject* aWebExtGlobal);
@ -341,7 +341,7 @@ class DOMScriptLoadContext : public PreloaderBase {
virtual ~DOMScriptLoadContext();
public:
explicit DOMScriptLoadContext(ScriptLoadRequest* aRequest);
explicit DOMScriptLoadContext(Element* aElement, ScriptLoadRequest* aRequest);
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(DOMScriptLoadContext)
@ -456,6 +456,9 @@ class DOMScriptLoadContext : public PreloaderBase {
Atomic<Runnable*> mRunnable; // Runnable created when dispatching off thread
// compile. Tracked here so that it can be
// properly released during cancellation.
nsCOMPtr<Element> mElement;
RefPtr<ScriptLoadRequest> mRequest;
// Non-null if there is a document that this request is blocking from loading.

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

@ -655,9 +655,11 @@ bool HostImportModuleDynamically(JSContext* aCx,
// Create a new top-level load request.
ScriptFetchOptions* options;
nsIURI* baseURL;
nsCOMPtr<Element> element;
if (script) {
options = script->GetFetchOptions();
baseURL = script->BaseURL();
element = script->GetScriptElement();
} else {
// We don't have a referencing script so fall back on using
// options from the document. This can happen when the user
@ -679,15 +681,14 @@ bool HostImportModuleDynamically(JSContext* aCx,
xpc::IsWebExtensionContentScriptSandbox(global->GetGlobalJSObject()));
}
options = new ScriptFetchOptions(mozilla::CORS_NONE,
document->GetReferrerPolicy(), nullptr,
principal, global);
options = new ScriptFetchOptions(
mozilla::CORS_NONE, document->GetReferrerPolicy(), principal, global);
baseURL = document->GetDocBaseURI();
}
RefPtr<ModuleLoadRequest> request = ModuleLoadRequest::CreateDynamicImport(
uri, options, baseURL, loader, aReferencingPrivate, specifierString,
aPromise);
uri, options, baseURL, element, loader, aReferencingPrivate,
specifierString, aPromise);
loader->GetModuleLoader()->StartDynamicImport(request);
return true;
@ -1143,19 +1144,20 @@ ScriptLoadRequest* ScriptLoader::CreateLoadRequest(
nsIURI* referrer = mDocument->GetDocumentURIAsReferrer();
nsCOMPtr<Element> domElement = do_QueryInterface(aElement);
ScriptFetchOptions* fetchOptions = new ScriptFetchOptions(
aCORSMode, aReferrerPolicy, domElement, aTriggeringPrincipal, nullptr);
aCORSMode, aReferrerPolicy, aTriggeringPrincipal, nullptr);
if (aKind == ScriptKind::eClassic) {
ScriptLoadRequest* aRequest =
new ScriptLoadRequest(aKind, aURI, fetchOptions, aIntegrity, referrer);
DOMScriptLoadContext* aContext = new DOMScriptLoadContext(aRequest);
DOMScriptLoadContext* aContext =
new DOMScriptLoadContext(domElement, aRequest);
aRequest->mLoadContext = aContext;
return aRequest;
}
MOZ_ASSERT(aKind == ScriptKind::eModule);
return ModuleLoadRequest::CreateTopLevel(aURI, fetchOptions, aIntegrity,
referrer, this);
return ModuleLoadRequest::CreateTopLevel(aURI, fetchOptions, domElement,
aIntegrity, referrer, this);
}
bool ScriptLoader::ProcessScriptElement(nsIScriptElement* aElement) {
@ -2526,7 +2528,8 @@ nsresult ScriptLoader::EvaluateScript(nsIGlobalObject* aGlobalObject,
// Create a ClassicScript object and associate it with the JSScript.
RefPtr<ClassicScript> classicScript =
new ClassicScript(aRequest->mFetchOptions, aRequest->mBaseURL);
new ClassicScript(aRequest->mFetchOptions, aRequest->mBaseURL,
aRequest->mLoadContext->mElement);
JS::RootedValue classicScriptValue(cx, JS::PrivateValue(classicScript));
JS::CompileOptions options(cx);

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

@ -57,13 +57,9 @@ JSObject* SourceElementCallback(JSContext* aCx, JS::HandleValue aPrivateValue) {
LoadedScript* script = static_cast<LoadedScript*>(aPrivateValue.toPrivate());
if (!script->GetFetchOptions()) {
return nullptr;
}
JS::Rooted<JS::Value> elementValue(aCx);
{
nsCOMPtr<Element> domElement = script->GetFetchOptions()->mElement;
nsCOMPtr<Element> domElement = script->GetScriptElement();
if (!domElement) {
return nullptr;
}