Skip to content

Commit 9163fab

Browse files
committed
Bug 1835886 - Cancel outstanding load requests when a document is detached from a global r=smaug
Also cancel module load requests when cancelling requests generally. What was happening here was that a previous load was completing and calling into the wrong module loader, because the loader to use is determined via the current global, and this was now associated with a different document / script loader / module loader. Differential Revision: https://phabricator.services.mozilla.com/D179787
1 parent 5432788 commit 9163fab

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

dom/script/ScriptLoader.cpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -258,19 +258,21 @@ ScriptLoader::~ScriptLoader() {
258258
void ScriptLoader::SetGlobalObject(nsIGlobalObject* aGlobalObject) {
259259
if (!aGlobalObject) {
260260
// The document is being detached.
261+
CancelAndClearScriptLoadRequests();
261262
return;
262263
}
263264

264265
MOZ_ASSERT(!HasPendingRequests());
265266

266-
if (mModuleLoader) {
267-
MOZ_ASSERT(mModuleLoader->GetGlobalObject() == aGlobalObject);
268-
return;
267+
if (!mModuleLoader) {
268+
// The module loader is associated with a global object, so don't create it
269+
// until we have a global set.
270+
mModuleLoader = new ModuleLoader(this, aGlobalObject, ModuleLoader::Normal);
269271
}
270272

271-
// The module loader is associated with a global object, so don't create it
272-
// until we have a global set.
273-
mModuleLoader = new ModuleLoader(this, aGlobalObject, ModuleLoader::Normal);
273+
MOZ_ASSERT(mModuleLoader->GetGlobalObject() == aGlobalObject);
274+
MOZ_ASSERT(aGlobalObject->GetModuleLoader(dom::danger::GetJSContext()) ==
275+
mModuleLoader);
274276
}
275277

276278
void ScriptLoader::RegisterContentScriptModuleLoader(ModuleLoader* aLoader) {
@@ -1428,6 +1430,10 @@ void ScriptLoader::CancelAndClearScriptLoadRequests() {
14281430
mXSLTRequests.CancelRequestsAndClear();
14291431
mOffThreadCompilingRequests.CancelRequestsAndClear();
14301432

1433+
if (mModuleLoader) {
1434+
mModuleLoader->CancelAndClearDynamicImports();
1435+
}
1436+
14311437
for (ModuleLoader* loader : mWebExtModuleLoaders) {
14321438
loader->CancelAndClearDynamicImports();
14331439
}

js/loader/ModuleLoaderBase.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -934,15 +934,19 @@ void ModuleLoaderBase::FinishDynamicImportAndReject(ModuleLoadRequest* aRequest,
934934
FinishDynamicImport(jsapi.cx(), aRequest, aResult, nullptr);
935935
}
936936

937+
/* static */
937938
void ModuleLoaderBase::FinishDynamicImport(
938939
JSContext* aCx, ModuleLoadRequest* aRequest, nsresult aResult,
939940
JS::Handle<JSObject*> aEvaluationPromise) {
941+
LOG(("ScriptLoadRequest (%p): Finish dynamic import %x %d", aRequest,
942+
unsigned(aResult), JS_IsExceptionPending(aCx)));
943+
944+
MOZ_ASSERT(GetCurrentModuleLoader(aCx) == aRequest->mLoader);
945+
940946
// If aResult is a failed result, we don't have an EvaluationPromise. If it
941947
// succeeded, evaluationPromise may still be null, but in this case it will
942948
// be handled by rejecting the dynamic module import promise in the JSAPI.
943949
MOZ_ASSERT_IF(NS_FAILED(aResult), !aEvaluationPromise);
944-
LOG(("ScriptLoadRequest (%p): Finish dynamic import %x %d", aRequest,
945-
unsigned(aResult), JS_IsExceptionPending(aCx)));
946950

947951
// Complete the dynamic import, report failures indicated by aResult or as a
948952
// pending exception on the context.
@@ -1182,6 +1186,7 @@ nsresult ModuleLoaderBase::EvaluateModuleInContext(
11821186
JSContext* aCx, ModuleLoadRequest* aRequest,
11831187
JS::ModuleErrorBehaviour errorBehaviour) {
11841188
MOZ_ASSERT(aRequest->mLoader == this);
1189+
MOZ_ASSERT(mGlobalObject->GetModuleLoader(aCx) == this);
11851190

11861191
AUTO_PROFILER_LABEL("ModuleLoaderBase::EvaluateModule", JS);
11871192

@@ -1215,6 +1220,7 @@ nsresult ModuleLoaderBase::EvaluateModuleInContext(
12151220

12161221
JS::Rooted<JSObject*> module(aCx, moduleScript->ModuleRecord());
12171222
MOZ_ASSERT(module);
1223+
MOZ_ASSERT(CurrentGlobalOrNull(aCx) == GetNonCCWObjectGlobal(module));
12181224

12191225
if (!xpc::Scriptability::AllowedIfExists(module)) {
12201226
return NS_OK;

0 commit comments

Comments
 (0)