Bug 1633851 - Part 2: Moving GetTopWindowExcludingExtensionAccessibleContentFrames() to AntiTrackingUtils. r=baku
authorTim Huang <tihuang@mozilla.com>
Wed, 13 May 2020 13:38:48 +0000
changeset 529628 aa5fe5344e64465b64d672122ff22c655a1a8687
parent 529627 6e725d8d275c96e2b41e4ad8073744a4dc416767
child 529629 46073712f17e191bf1c34f0d394e15546a8a57e4
push id115820
push usertihuang@mozilla.com
push dateWed, 13 May 2020 14:41:21 +0000
treeherderautoland@ecbd52188ae8 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbaku
bugs1633851
milestone78.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1633851 - Part 2: Moving GetTopWindowExcludingExtensionAccessibleContentFrames() to AntiTrackingUtils. r=baku Differential Revision: https://phabricator.services.mozilla.com/D73200
netwerk/ipc/DocumentLoadListener.cpp
toolkit/components/antitracking/AntiTrackingUtils.cpp
toolkit/components/antitracking/AntiTrackingUtils.h
--- a/netwerk/ipc/DocumentLoadListener.cpp
+++ b/netwerk/ipc/DocumentLoadListener.cpp
@@ -357,47 +357,16 @@ already_AddRefed<LoadInfo> DocumentLoadL
   }
 
   loadInfo->SetHasValidUserGestureActivation(
       aLoadState->HasValidUserGestureActivation());
 
   return loadInfo.forget();
 }
 
-// parent-process implementation of
-// nsGlobalWindowOuter::GetTopExcludingExtensionAccessibleContentFrames
-static already_AddRefed<WindowGlobalParent>
-GetTopWindowExcludingExtensionAccessibleContentFrames(
-    CanonicalBrowsingContext* aBrowsingContext, nsIURI* aURIBeingLoaded) {
-  CanonicalBrowsingContext* bc = aBrowsingContext;
-  RefPtr<WindowGlobalParent> prev;
-  while (RefPtr<WindowGlobalParent> parent = bc->GetParentWindowContext()) {
-    CanonicalBrowsingContext* parentBC = parent->BrowsingContext();
-
-    nsIPrincipal* parentPrincipal = parent->DocumentPrincipal();
-    nsIURI* uri = prev ? prev->GetDocumentURI() : aURIBeingLoaded;
-
-    // If the new parent has permission to load the current page, we're
-    // at a moz-extension:// frame which has a host permission that allows
-    // it to load the document that we've loaded.  In that case, stop at
-    // this frame and consider it the top-level frame.
-    if (uri &&
-        BasePrincipal::Cast(parentPrincipal)->AddonAllowsLoad(uri, true)) {
-      break;
-    }
-
-    bc = parentBC;
-    prev = parent;
-  }
-  if (!prev) {
-    prev = bc->GetCurrentWindowGlobal();
-  }
-  return prev.forget();
-}
-
 CanonicalBrowsingContext* DocumentLoadListener::GetBrowsingContext() {
   if (!mParentChannelListener) {
     return nullptr;
   }
   return mParentChannelListener->GetBrowsingContext();
 }
 
 bool DocumentLoadListener::Open(
@@ -448,17 +417,17 @@ bool DocumentLoadListener::Open(
       // Update the IsOnContentBlockingAllowList flag in the CookieJarSettings
       // if this is a top level loading. For sub-document loading, this flag
       // would inherit from the parent.
       nsCOMPtr<nsICookieJarSettings> cookieJarSettings;
       Unused << loadInfo->GetCookieJarSettings(
           getter_AddRefs(cookieJarSettings));
       net::CookieJarSettings::Cast(cookieJarSettings)
           ->UpdateIsOnContentBlockingAllowList(mChannel);
-    } else if (RefPtr<WindowGlobalParent> topWindow =
+    } else if (RefPtr<WindowGlobalParent> topWindow = AntiTrackingUtils::
                    GetTopWindowExcludingExtensionAccessibleContentFrames(
                        browsingContext, uriBeingLoaded)) {
       nsCOMPtr<nsIPrincipal> topWindowPrincipal =
           topWindow->DocumentPrincipal();
       if (topWindowPrincipal && !topWindowPrincipal->GetIsNullPrincipal()) {
         auto* basePrin = BasePrincipal::Cast(topWindowPrincipal);
         basePrin->GetURI(getter_AddRefs(topWindowURI));
       }
--- a/toolkit/components/antitracking/AntiTrackingUtils.cpp
+++ b/toolkit/components/antitracking/AntiTrackingUtils.cpp
@@ -3,16 +3,17 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * 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/. */
 
 #include "AntiTrackingUtils.h"
 
 #include "AntiTrackingLog.h"
 #include "mozilla/dom/BrowsingContext.h"
+#include "mozilla/dom/CanonicalBrowsingContext.h"
 #include "mozilla/net/CookieJarSettings.h"
 #include "mozilla/dom/Document.h"
 #include "mozilla/dom/WindowGlobalParent.h"
 #include "mozilla/dom/WindowContext.h"
 #include "mozilla/net/NeckoChannelParams.h"
 #include "mozilla/PermissionManager.h"
 #include "nsIChannel.h"
 #include "nsIPermission.h"
@@ -463,16 +464,17 @@ bool AntiTrackingUtils::GetPrincipalAndT
 
   if (aPrincipal) {
     principal.forget(aPrincipal);
   }
 
   return true;
 };
 
+/* static */
 bool AntiTrackingUtils::IsFirstLevelSubContext(
     BrowsingContext* aBrowsingContext) {
   MOZ_ASSERT(aBrowsingContext);
 
   RefPtr<BrowsingContext> parentBC = aBrowsingContext->GetParent();
 
   if (!parentBC) {
     // No parent means it is the top.
@@ -491,8 +493,41 @@ uint32_t AntiTrackingUtils::GetCookieBeh
 
   RefPtr<dom::WindowContext> win = aBrowsingContext->GetCurrentWindowContext();
   if (!win || win->GetCookieBehavior().isNothing()) {
     return nsICookieService::BEHAVIOR_REJECT;
   }
 
   return *win->GetCookieBehavior();
 }
+
+/* static */
+already_AddRefed<WindowGlobalParent>
+AntiTrackingUtils::GetTopWindowExcludingExtensionAccessibleContentFrames(
+    CanonicalBrowsingContext* aBrowsingContext, nsIURI* aURIBeingLoaded) {
+  MOZ_ASSERT(XRE_IsParentProcess());
+  MOZ_ASSERT(aBrowsingContext);
+
+  CanonicalBrowsingContext* bc = aBrowsingContext;
+  RefPtr<WindowGlobalParent> prev;
+  while (RefPtr<WindowGlobalParent> parent = bc->GetParentWindowContext()) {
+    CanonicalBrowsingContext* parentBC = parent->BrowsingContext();
+
+    nsIPrincipal* parentPrincipal = parent->DocumentPrincipal();
+    nsIURI* uri = prev ? prev->GetDocumentURI() : aURIBeingLoaded;
+
+    // If the new parent has permission to load the current page, we're
+    // at a moz-extension:// frame which has a host permission that allows
+    // it to load the document that we've loaded.  In that case, stop at
+    // this frame and consider it the top-level frame.
+    if (uri &&
+        BasePrincipal::Cast(parentPrincipal)->AddonAllowsLoad(uri, true)) {
+      break;
+    }
+
+    bc = parentBC;
+    prev = parent;
+  }
+  if (!prev) {
+    prev = bc->GetCurrentWindowGlobal();
+  }
+  return prev.forget();
+}
--- a/toolkit/components/antitracking/AntiTrackingUtils.h
+++ b/toolkit/components/antitracking/AntiTrackingUtils.h
@@ -15,16 +15,18 @@ class nsPIDOMWindowOuter;
 class nsIChannel;
 class nsIPermission;
 class nsIPrincipal;
 class nsIURI;
 
 namespace mozilla {
 namespace dom {
 class BrowsingContext;
+class CanonicalBrowsingContext;
+class WindowGlobalParent;
 }  // namespace dom
 
 class AntiTrackingUtils final {
  public:
   static already_AddRefed<nsPIDOMWindowInner> GetInnerWindow(
       dom::BrowsingContext* aBrowsingContext);
 
   static already_AddRefed<nsPIDOMWindowOuter> GetTopWindow(
@@ -85,13 +87,22 @@ class AntiTrackingUtils final {
 
   // Retruns true if the given browsingContext is a first-level sub context,
   // i.e. a first-level iframe.
   static bool IsFirstLevelSubContext(dom::BrowsingContext* aBrowsingContext);
 
   // Retruns the cookie behavior of the given browsingContext,
   // return BEHAVIOR_REJECT when fail.
   static uint32_t GetCookieBehavior(dom::BrowsingContext* aBrowsingContext);
+
+  // Returns the top-level global window parent. But we would stop at the
+  // content window which is loaded by addons and consider this window as a top.
+  //
+  // Note that this is the parent-process implementation of
+  // nsGlobalWindowOuter::GetTopExcludingExtensionAccessibleContentFrames
+  static already_AddRefed<dom::WindowGlobalParent>
+  GetTopWindowExcludingExtensionAccessibleContentFrames(
+      dom::CanonicalBrowsingContext* aBrowsingContext, nsIURI* aURIBeingLoaded);
 };
 
 }  // namespace mozilla
 
 #endif  // mozilla_antitrackingutils_h