Bug 1633851 - Part 3: Compute the IsThirdPartyContextToTopWindow flag in LoadInfo when opening the channel. r=baku
authorTim Huang <tihuang@mozilla.com>
Wed, 13 May 2020 13:39:01 +0000
changeset 529629 46073712f17e191bf1c34f0d394e15546a8a57e4
parent 529628 aa5fe5344e64465b64d672122ff22c655a1a8687
child 529630 9b070149d2eea1ee0d53ab35f4f3e8d03814cdb5
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 3: Compute the IsThirdPartyContextToTopWindow flag in LoadInfo when opening the channel. r=baku Differential Revision: https://phabricator.services.mozilla.com/D73201
netwerk/protocol/http/nsHttpChannel.cpp
toolkit/components/antitracking/AntiTrackingUtils.cpp
toolkit/components/antitracking/AntiTrackingUtils.h
--- a/netwerk/protocol/http/nsHttpChannel.cpp
+++ b/netwerk/protocol/http/nsHttpChannel.cpp
@@ -6456,16 +6456,18 @@ nsHttpChannel::AsyncOpen(nsIStreamListen
     LOG(("  after HTTP shutdown..."));
     ReleaseListeners();
     return NS_ERROR_NOT_AVAILABLE;
   }
 
   Unused << mLoadInfo->SetHasStoragePermission(
       AntiTrackingUtils::HasStoragePermissionInParent(this));
 
+  AntiTrackingUtils::ComputeIsThirdPartyToTopWindow(this);
+
   static bool sRCWNInited = false;
   if (!sRCWNInited) {
     sRCWNInited = true;
     Preferences::AddBoolVarCache(&sRCWNEnabled, "network.http.rcwn.enabled");
     Preferences::AddUintVarCache(
         &sRCWNQueueSizeNormal,
         "network.http.rcwn.cache_queue_normal_threshold");
     Preferences::AddUintVarCache(
--- a/toolkit/components/antitracking/AntiTrackingUtils.cpp
+++ b/toolkit/components/antitracking/AntiTrackingUtils.cpp
@@ -526,8 +526,43 @@ AntiTrackingUtils::GetTopWindowExcluding
     bc = parentBC;
     prev = parent;
   }
   if (!prev) {
     prev = bc->GetCurrentWindowGlobal();
   }
   return prev.forget();
 }
+
+/* static */
+void AntiTrackingUtils::ComputeIsThirdPartyToTopWindow(nsIChannel* aChannel) {
+  MOZ_ASSERT(aChannel);
+  MOZ_ASSERT(XRE_IsParentProcess());
+
+  nsCOMPtr<nsILoadInfo> loadInfo = aChannel->LoadInfo();
+  RefPtr<BrowsingContext> bc;
+  loadInfo->GetBrowsingContext(getter_AddRefs(bc));
+
+  // In xpcshell-tests we don't always have a browsingContext.
+  if (!bc) {
+    return;
+  }
+
+  nsCOMPtr<nsIURI> uri;
+  Unused << aChannel->GetURI(getter_AddRefs(uri));
+
+  RefPtr<WindowGlobalParent> topWindow =
+      GetTopWindowExcludingExtensionAccessibleContentFrames(bc->Canonical(),
+                                                            uri);
+
+  if (NS_WARN_IF(!topWindow)) {
+    return;
+  }
+
+  nsCOMPtr<nsIPrincipal> topWindowPrincipal = topWindow->DocumentPrincipal();
+  if (topWindowPrincipal && !topWindowPrincipal->GetIsNullPrincipal()) {
+    auto* basePrin = BasePrincipal::Cast(topWindowPrincipal);
+    bool isThirdParty = true;
+    basePrin->IsThirdPartyURI(uri, &isThirdParty);
+
+    loadInfo->SetIsThirdPartyContextToTopWindow(isThirdParty);
+  }
+}
--- a/toolkit/components/antitracking/AntiTrackingUtils.h
+++ b/toolkit/components/antitracking/AntiTrackingUtils.h
@@ -96,13 +96,17 @@ class AntiTrackingUtils final {
   // 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);
+
+  // Given a channel, compute and set the IsThirdPartyContextToTopWindow for
+  // this channel. This function is supposed to be called in the parent process.
+  static void ComputeIsThirdPartyToTopWindow(nsIChannel* aChannel);
 };
 
 }  // namespace mozilla
 
 #endif  // mozilla_antitrackingutils_h