From 0677a9ec85ba816454fe524ceff42adbad0ae065 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emilio=20Cobos=20=C3=81lvarez?= Date: Thu, 15 Apr 2021 12:11:02 +0000 Subject: [PATCH] Bug 1704651 - Wait for the right load in ClientOpenWindow. r=asuth With fission+BFCache, we get a notification for the initial document, and that fails the same-origin check (because about:blank isn't same-origin with the opener) so we resolve the promise with null rather than returning a client. Instead, wait for the right load to arrive if the top level window global is the initial document. Differential Revision: https://phabricator.services.mozilla.com/D112072 --- dom/clients/manager/ClientOpenWindowUtils.cpp | 17 ++++++++++++++--- dom/serviceworkers/test/openWindow_worker.js | 2 +- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/dom/clients/manager/ClientOpenWindowUtils.cpp b/dom/clients/manager/ClientOpenWindowUtils.cpp index 98f6c7e79d09..8dfb2ae3cfb2 100644 --- a/dom/clients/manager/ClientOpenWindowUtils.cpp +++ b/dom/clients/manager/ClientOpenWindowUtils.cpp @@ -83,8 +83,10 @@ class WebProgressListener final : public nsIWebProgressListener, // Our caller keeps a strong reference, so it is safe to remove the listener // from the BrowsingContext's nsIWebProgress. - nsCOMPtr webProgress = browsingContext->GetWebProgress(); - webProgress->RemoveProgressListener(this); + auto RemoveListener = [&] { + nsCOMPtr webProgress = browsingContext->GetWebProgress(); + webProgress->RemoveProgressListener(this); + }; RefPtr wgp = browsingContext->GetCurrentWindowGlobal(); @@ -93,9 +95,18 @@ class WebProgressListener final : public nsIWebProgressListener, rv.ThrowInvalidStateError("Unable to open window"); mPromise->Reject(rv, __func__); mPromise = nullptr; + RemoveListener(); return NS_OK; } + if (NS_WARN_IF(wgp->IsInitialDocument())) { + // This is the load of the initial document, which is not the document we + // care about for the purposes of checking same-originness of the URL. + return NS_OK; + } + + RemoveListener(); + // Check same origin. If the origins do not match, resolve with null (per // step 7.2.7.1 of the openWindow spec). nsCOMPtr securityManager = @@ -104,7 +115,7 @@ class WebProgressListener final : public nsIWebProgressListener, wgp->DocumentPrincipal()->OriginAttributesRef().mPrivateBrowsingId > 0; nsresult rv = securityManager->CheckSameOriginURI( wgp->GetDocumentURI(), mBaseURI, false, isPrivateWin); - if (NS_FAILED(rv)) { + if (NS_WARN_IF(NS_FAILED(rv))) { mPromise->Resolve(CopyableErrorResult(), __func__); mPromise = nullptr; return NS_OK; diff --git a/dom/serviceworkers/test/openWindow_worker.js b/dom/serviceworkers/test/openWindow_worker.js index 49db32c391f7..eb440da6e521 100644 --- a/dom/serviceworkers/test/openWindow_worker.js +++ b/dom/serviceworkers/test/openWindow_worker.js @@ -25,7 +25,7 @@ function testForUrl(url, throwType, clientProperties, resultsArray) { } else if (clientProperties) { resultsArray.push({ result: e instanceof WindowClient, - message: "openWindow should resolve to a WindowClient", + message: `openWindow should resolve to a WindowClient for url ${url}, got ${e}`, }); resultsArray.push({ result: e.url == clientProperties.url,