From dd1ef62596e3b89c7d3071b4ca83697d9698add0 Mon Sep 17 00:00:00 2001 From: Nika Layzell Date: Tue, 7 Apr 2020 21:39:04 +0000 Subject: [PATCH] Bug 1616353 - Part 3: Use an attribute to set geckoViewUserContextId on a browser, r=geckoview-reviewers,esawin This is necessary to avoid the use of setOriginAttributesBeforeLoading, which is being removed in this patch set. Differential Revision: https://phabricator.services.mozilla.com/D67042 --HG-- extra : moz-landing-system : lando --- dom/base/nsFrameLoader.cpp | 49 ++++++++++++------- dom/base/nsFrameLoader.h | 3 +- .../modules/geckoview/GeckoViewNavigation.jsm | 32 ++++++------ xpcom/ds/StaticAtoms.py | 1 + 4 files changed, 48 insertions(+), 37 deletions(-) diff --git a/dom/base/nsFrameLoader.cpp b/dom/base/nsFrameLoader.cpp index 9a7b880c0d98..1c74ec6aeaae 100644 --- a/dom/base/nsFrameLoader.cpp +++ b/dom/base/nsFrameLoader.cpp @@ -1167,12 +1167,12 @@ nsresult nsFrameLoader::SwapWithOtherRemoteLoader( // usercontextid attribute before comparing our originAttributes with the // other one. OriginAttributes ourOriginAttributes = browserParent->OriginAttributesRef(); - rv = PopulateUserContextIdFromAttribute(ourOriginAttributes); + rv = PopulateOriginContextIdsFromAttributes(ourOriginAttributes); NS_ENSURE_SUCCESS(rv, rv); OriginAttributes otherOriginAttributes = otherBrowserParent->OriginAttributesRef(); - rv = aOther->PopulateUserContextIdFromAttribute(otherOriginAttributes); + rv = aOther->PopulateOriginContextIdsFromAttributes(otherOriginAttributes); NS_ENSURE_SUCCESS(rv, rv); if (ourOriginAttributes != otherOriginAttributes) { @@ -1597,11 +1597,11 @@ nsresult nsFrameLoader::SwapWithOtherLoader(nsFrameLoader* aOther, // usercontextid attribute before comparing our originAttributes with the // other one. OriginAttributes ourOriginAttributes = ourDocshell->GetOriginAttributes(); - rv = PopulateUserContextIdFromAttribute(ourOriginAttributes); + rv = PopulateOriginContextIdsFromAttributes(ourOriginAttributes); NS_ENSURE_SUCCESS(rv, rv); OriginAttributes otherOriginAttributes = otherDocshell->GetOriginAttributes(); - rv = aOther->PopulateUserContextIdFromAttribute(otherOriginAttributes); + rv = aOther->PopulateOriginContextIdsFromAttributes(otherOriginAttributes); NS_ENSURE_SUCCESS(rv, rv); if (ourOriginAttributes != otherOriginAttributes) { @@ -2186,7 +2186,7 @@ nsresult nsFrameLoader::MaybeCreateDocShell() { ApplySandboxFlags(sandboxFlags); // Grab the userContextId from owner - nsresult rv = PopulateUserContextIdFromAttribute(attrs); + nsresult rv = PopulateOriginContextIdsFromAttributes(attrs); if (NS_WARN_IF(NS_FAILED(rv))) { return rv; } @@ -3399,7 +3399,7 @@ nsresult nsFrameLoader::GetNewTabContext(MutableTabContext* aTabContext, // set the userContextId on the attrs before we pass them into // the tab context - rv = PopulateUserContextIdFromAttribute(attrs); + rv = PopulateOriginContextIdsFromAttributes(attrs); NS_ENSURE_SUCCESS(rv, rv); nsAutoString presentationURLStr; @@ -3440,21 +3440,32 @@ nsresult nsFrameLoader::GetNewTabContext(MutableTabContext* aTabContext, return NS_OK; } -nsresult nsFrameLoader::PopulateUserContextIdFromAttribute( +nsresult nsFrameLoader::PopulateOriginContextIdsFromAttributes( OriginAttributes& aAttr) { + // Only XUL or mozbrowser frames are allowed to set context IDs + uint32_t namespaceID = mOwnerContent->GetNameSpaceID(); + if (namespaceID != kNameSpaceID_XUL && !OwnerIsMozBrowserFrame()) { + return NS_OK; + } + + nsAutoString attributeValue; if (aAttr.mUserContextId == - nsIScriptSecurityManager::DEFAULT_USER_CONTEXT_ID) { - // Grab the userContextId from owner if XUL or mozbrowser frame - nsAutoString userContextIdStr; - int32_t namespaceID = mOwnerContent->GetNameSpaceID(); - if ((namespaceID == kNameSpaceID_XUL || OwnerIsMozBrowserFrame()) && - mOwnerContent->GetAttr(kNameSpaceID_None, nsGkAtoms::usercontextid, - userContextIdStr) && - !userContextIdStr.IsEmpty()) { - nsresult rv; - aAttr.mUserContextId = userContextIdStr.ToInteger(&rv); - NS_ENSURE_SUCCESS(rv, rv); - } + nsIScriptSecurityManager::DEFAULT_USER_CONTEXT_ID && + mOwnerContent->GetAttr(kNameSpaceID_None, nsGkAtoms::usercontextid, + attributeValue) && + !attributeValue.IsEmpty()) { + nsresult rv; + aAttr.mUserContextId = attributeValue.ToInteger(&rv); + NS_ENSURE_SUCCESS(rv, rv); + } + + if (aAttr.mGeckoViewSessionContextId.IsEmpty() && + mOwnerContent->GetAttr(kNameSpaceID_None, + nsGkAtoms::geckoViewSessionContextId, + attributeValue) && + !attributeValue.IsEmpty()) { + // XXX: Should we check the format from `GeckoViewNavigation.jsm` here? + aAttr.mGeckoViewSessionContextId = attributeValue; } return NS_OK; diff --git a/dom/base/nsFrameLoader.h b/dom/base/nsFrameLoader.h index 7201b3fed288..a1c73f9fa407 100644 --- a/dom/base/nsFrameLoader.h +++ b/dom/base/nsFrameLoader.h @@ -467,7 +467,8 @@ class nsFrameLoader final : public nsStubMutationObserver, enum BrowserParentChange { eBrowserParentRemoved, eBrowserParentChanged }; void MaybeUpdatePrimaryBrowserParent(BrowserParentChange aChange); - nsresult PopulateUserContextIdFromAttribute(mozilla::OriginAttributes& aAttr); + nsresult PopulateOriginContextIdsFromAttributes( + mozilla::OriginAttributes& aAttr); RefPtr mPendingBrowsingContext; nsCOMPtr mURIToLoad; diff --git a/mobile/android/modules/geckoview/GeckoViewNavigation.jsm b/mobile/android/modules/geckoview/GeckoViewNavigation.jsm index ef2fa82c03dd..f559192dcec4 100644 --- a/mobile/android/modules/geckoview/GeckoViewNavigation.jsm +++ b/mobile/android/modules/geckoview/GeckoViewNavigation.jsm @@ -17,7 +17,6 @@ XPCOMUtils.defineLazyModuleGetters(this, { E10SUtils: "resource://gre/modules/E10SUtils.jsm", LoadURIDelegate: "resource://gre/modules/LoadURIDelegate.jsm", Services: "resource://gre/modules/Services.jsm", - PrivateBrowsingUtils: "resource://gre/modules/PrivateBrowsingUtils.jsm", }); XPCOMUtils.defineLazyGetter(this, "ReferrerInfo", () => @@ -78,6 +77,21 @@ class GeckoViewNavigation extends GeckoViewModule { onInitBrowser() { this.window.browserDOMWindow = this; + debug`sessionContextId=${this.settings.sessionContextId}`; + + if (this.settings.sessionContextId !== null) { + // Gecko may have issues with strings containing special characters, + // so we restrict the string format to a specific pattern. + if (!/^gvctx(-)?([a-f0-9]+)$/.test(this.settings.sessionContextId)) { + throw new Error("sessionContextId has illegal format"); + } + + this.browser.setAttribute( + "geckoViewSessionContextId", + this.settings.sessionContextId + ); + } + // There may be a GeckoViewNavigation module in another window waiting for // us to create a browser so it can call presetOpenerWindow(), so allow them // to do that now. @@ -98,22 +112,6 @@ class GeckoViewNavigation extends GeckoViewModule { ]); this._initialAboutBlank = true; - - debug`sessionContextId=${this.settings.sessionContextId}`; - - if (this.settings.sessionContextId !== null) { - // Gecko may have issues with strings containing special characters, - // so we restrict the string format to a specific pattern. - if (!/^gvctx(-)?([a-f0-9]+)$/.test(this.settings.sessionContextId)) { - throw new Error("sessionContextId has illegal format"); - } - this.browser.webNavigation.setOriginAttributesBeforeLoading({ - geckoViewSessionContextId: this.settings.sessionContextId, - privateBrowsingId: PrivateBrowsingUtils.isBrowserPrivate(this.browser) - ? 1 - : 0, - }); - } } // Bundle event handler. diff --git a/xpcom/ds/StaticAtoms.py b/xpcom/ds/StaticAtoms.py index 875ec5b9eb54..e77dfc375856 100644 --- a/xpcom/ds/StaticAtoms.py +++ b/xpcom/ds/StaticAtoms.py @@ -2389,6 +2389,7 @@ STATIC_ATOMS = [ # Contextual Identity / Containers Atom("usercontextid", "usercontextid"), + Atom("geckoViewSessionContextId", "geckoViewSessionContextId"), # Namespaces Atom("nsuri_xmlns", "http://www.w3.org/2000/xmlns/"),