From 195cb08d192e8edd927182230fd2847cf0d3a2ac Mon Sep 17 00:00:00 2001 From: Tim Huang Date: Fri, 17 Apr 2020 22:09:23 +0000 Subject: [PATCH] Bug 1624268 - Part 1: Add checking the HasStorageAccessGranted() for the ContentBlocking::ShouldAllowAccessFor(channel). r=dimi,baku We need to check the HasStorageAccessGranted() in the channel version of the ContentBlocking::ShouldAllowAccessFor(). It's because we will move to use the flag 'HasStoragePermission' to check the permission. But, this flag won't get updated if the storage is allowed by the heuristic. So, we need to rely on the HasStorageAccessGranted() to check the storage access in the channel check as well. Differential Revision: https://phabricator.services.mozilla.com/D71031 --- .../antitracking/ContentBlocking.cpp | 43 +++++++++++++++++-- 1 file changed, 40 insertions(+), 3 deletions(-) diff --git a/toolkit/components/antitracking/ContentBlocking.cpp b/toolkit/components/antitracking/ContentBlocking.cpp index 5550b1490902..402c1ff11acf 100644 --- a/toolkit/components/antitracking/ContentBlocking.cpp +++ b/toolkit/components/antitracking/ContentBlocking.cpp @@ -1016,9 +1016,46 @@ bool ContentBlocking::ShouldAllowAccessFor(nsIChannel* aChannel, nsIURI* aURI, return false; } - return AntiTrackingUtils::CheckStoragePermission( - parentPrincipal, type, !!privateBrowsingId, aRejectedReason, - blockedReason); + auto checkPermission = [parentPrincipal, type, privateBrowsingId, + aRejectedReason, blockedReason]() -> bool { + return AntiTrackingUtils::CheckStoragePermission( + parentPrincipal, type, !!privateBrowsingId, aRejectedReason, + blockedReason); + }; + + // Call HasStorageAccessGranted() in the top-level inner window to check + // if the storage permission has been granted by the heuristic or the + // StorageAccessAPI. Note that calling the HasStorageAccessGranted() is still + // not fission-compatible. This would be modified in Bug 1612376. + RefPtr bc; + loadInfo->GetBrowsingContext(getter_AddRefs(bc)); + if (!bc) { + return checkPermission(); + } + + bc = bc->Top(); + if (!bc || !bc->IsInProcess()) { + return checkPermission(); + } + + nsGlobalWindowOuter* topWindow = + nsGlobalWindowOuter::Cast(bc->GetDOMWindow()); + + if (!topWindow) { + return checkPermission(); + } + + nsPIDOMWindowInner* topInnerWindow = topWindow->GetCurrentInnerWindow(); + // We use the 'hasStoragePermission' flag to check the storage permission. + // However, this flag won't get updated once the permission is granted by + // the heuristic or the StorageAccessAPI. So, we need to check the + // HasStorageAccessGranted() in order to get the correct storage access before + // we check the 'hasStoragePermission' flag. + if (topInnerWindow && topInnerWindow->HasStorageAccessGranted(type)) { + return true; + } + + return checkPermission(); } bool ContentBlocking::ShouldAllowAccessFor(