From 64fd29f182c1fb4cfc8771d6a825164a7fff35b9 Mon Sep 17 00:00:00 2001 From: Nika Layzell Date: Tue, 9 Mar 2021 15:29:41 +0000 Subject: [PATCH] Bug 1663757 - Part 4: Track current remote URI on CanonicalBrowsingContext, r=annyG,farre This URI is intended to reflect the currentURI field on the content nsIDocShell, and is the value used for getters like `window.location.href`. For most documents, this generally matches the Document URI on WindowGlobalParent, it does not match in specific cases such as error pages or when performing a session restore. The field is kept up-to-date by listening to `OnLocationChange` notifications from the content process, and is ignored when the BrowsingContext is loaded in the parent process. Differential Revision: https://phabricator.services.mozilla.com/D105559 --- docshell/base/CanonicalBrowsingContext.cpp | 20 ++++++++++++++++++++ docshell/base/CanonicalBrowsingContext.h | 8 ++++++++ dom/chrome-webidl/BrowsingContext.webidl | 5 +++++ dom/ipc/BrowserParent.cpp | 2 ++ 4 files changed, 35 insertions(+) diff --git a/docshell/base/CanonicalBrowsingContext.cpp b/docshell/base/CanonicalBrowsingContext.cpp index a3893f6681dc..bb20a37d8a7a 100644 --- a/docshell/base/CanonicalBrowsingContext.cpp +++ b/docshell/base/CanonicalBrowsingContext.cpp @@ -70,6 +70,10 @@ CanonicalBrowsingContext::CanonicalBrowsingContext(WindowContext* aParentWindow, // You are only ever allowed to create CanonicalBrowsingContexts in the // parent process. MOZ_RELEASE_ASSERT(XRE_IsParentProcess()); + + // The initial URI in a BrowsingContext is always "about:blank". + MOZ_ALWAYS_SUCCEEDS( + NS_NewURI(getter_AddRefs(mCurrentRemoteURI), "about:blank")); } /* static */ @@ -1656,6 +1660,22 @@ void CanonicalBrowsingContext::EndDocumentLoad(bool aForProcessSwitch) { } } +already_AddRefed CanonicalBrowsingContext::GetCurrentURI() const { + nsCOMPtr currentURI; + if (nsIDocShell* docShell = GetDocShell()) { + MOZ_ALWAYS_SUCCEEDS( + nsDocShell::Cast(docShell)->GetCurrentURI(getter_AddRefs(currentURI))); + } else { + currentURI = mCurrentRemoteURI; + } + return currentURI.forget(); +} + +void CanonicalBrowsingContext::SetCurrentRemoteURI(nsIURI* aCurrentRemoteURI) { + MOZ_ASSERT(!GetDocShell()); + mCurrentRemoteURI = aCurrentRemoteURI; +} + void CanonicalBrowsingContext::ResetSHEntryHasUserInteractionCache() { WindowContext* topWc = GetTopWindowContext(); if (topWc && !topWc->IsDiscarded()) { diff --git a/docshell/base/CanonicalBrowsingContext.h b/docshell/base/CanonicalBrowsingContext.h index 78a2bf6a12d1..2659100b3258 100644 --- a/docshell/base/CanonicalBrowsingContext.h +++ b/docshell/base/CanonicalBrowsingContext.h @@ -208,6 +208,10 @@ class CanonicalBrowsingContext final : public BrowsingContext { void Reload(uint32_t aReloadFlags); void Stop(uint32_t aStopFlags); + // Get the publicly exposed current URI loaded in this BrowsingContext. + already_AddRefed GetCurrentURI() const; + void SetCurrentRemoteURI(nsIURI* aCurrentRemoteURI); + BrowserParent* GetBrowserParent() const; // Internal method to change which process a BrowsingContext is being loaded @@ -360,6 +364,10 @@ class CanonicalBrowsingContext final : public BrowsingContext { // entries are added or replaced. void ResetSHEntryHasUserInteractionCache(); + // The current URI loaded in this BrowsingContext. This value is only set for + // BrowsingContexts loaded in content processes. + nsCOMPtr mCurrentRemoteURI; + // The current remoteness change which is in a pending state. RefPtr mPendingRemotenessChange; diff --git a/dom/chrome-webidl/BrowsingContext.webidl b/dom/chrome-webidl/BrowsingContext.webidl index ecc5bcfb1cc2..13ed77568768 100644 --- a/dom/chrome-webidl/BrowsingContext.webidl +++ b/dom/chrome-webidl/BrowsingContext.webidl @@ -3,6 +3,7 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ +interface URI; interface nsIDocShell; interface nsISecureBrowserUI; interface nsIWebProgress; @@ -265,6 +266,10 @@ interface CanonicalBrowsingContext : BrowsingContext { readonly attribute MediaController? mediaController; void resetScalingZoom(); + + // The current URI loaded in this BrowsingContext according to nsDocShell. + // This may not match the current window global's document URI in some cases. + readonly attribute URI? currentURI; }; [Exposed=Window, ChromeOnly] diff --git a/dom/ipc/BrowserParent.cpp b/dom/ipc/BrowserParent.cpp index e62de5705939..62fc4c499ad5 100644 --- a/dom/ipc/BrowserParent.cpp +++ b/dom/ipc/BrowserParent.cpp @@ -2686,6 +2686,8 @@ mozilla::ipc::IPCResult BrowserParent::RecvOnLocationChange( return IPC_OK(); } + browsingContext->SetCurrentRemoteURI(aLocation); + nsCOMPtr browser = GetBrowser(); if (!mozilla::SessionHistoryInParent() && browser) { Unused << browser->UpdateWebNavigationForLocationChange(aCanGoBack,