Bug 1484876 - Part 3: Refactor the code responsible for checking whether the top window URI is on the content blocking allow list into AntiTrackingCommon; r=francois
☠☠ backed out by 41ebcb085bb0 ☠ ☠
authorEhsan Akhgari <ehsan@mozilla.com>
Mon, 20 Aug 2018 20:09:52 -0400
changeset 432826 d31e39a477046ed8a574e43dc6bbd4192829680d
parent 432825 babf6abc7f4cc189f17f73c7ddff8987da69a43a
child 432827 cd2ced68989568aaadd21e09d7d68664cf43e91d
push id34488
push usernerli@mozilla.com
push dateWed, 22 Aug 2018 16:28:54 +0000
treeherdermozilla-central@d6e4d3e69d4c [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersfrancois
bugs1484876
milestone63.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 1484876 - Part 3: Refactor the code responsible for checking whether the top window URI is on the content blocking allow list into AntiTrackingCommon; r=francois
netwerk/base/nsChannelClassifier.cpp
toolkit/components/antitracking/AntiTrackingCommon.cpp
toolkit/components/antitracking/AntiTrackingCommon.h
--- a/netwerk/base/nsChannelClassifier.cpp
+++ b/netwerk/base/nsChannelClassifier.cpp
@@ -32,16 +32,17 @@
 #include "nsIWebProgressListener.h"
 #include "nsNetUtil.h"
 #include "nsPIDOMWindow.h"
 #include "nsXULAppAPI.h"
 #include "nsQueryObject.h"
 #include "nsIUrlClassifierDBService.h"
 #include "nsIURLFormatter.h"
 
+#include "mozilla/AntiTrackingCommon.h"
 #include "mozilla/ErrorNames.h"
 #include "mozilla/Logging.h"
 #include "mozilla/Preferences.h"
 #include "mozilla/net/HttpBaseChannel.h"
 #include "mozilla/ClearOnShutdown.h"
 #include "mozilla/Services.h"
 #include "mozilla/StaticPrefs.h"
 #include "mozilla/Unused.h"
@@ -443,80 +444,47 @@ nsChannelClassifier::ShouldEnableTrackin
         return NS_OK;
       }
     }
 
     if (AddonMayLoad(aChannel, chanURI)) {
         return NS_OK;
     }
 
-    nsCOMPtr<nsIIOService> ios = do_GetService(NS_IOSERVICE_CONTRACTID, &rv);
-    NS_ENSURE_SUCCESS(rv, rv);
+    nsCOMPtr<nsIIOService> ios = services::GetIOService();
+    NS_ENSURE_TRUE(ios, NS_ERROR_FAILURE);
 
     nsCOMPtr<nsIURI> topWinURI;
     rv = chan->GetTopWindowURI(getter_AddRefs(topWinURI));
     if (NS_FAILED(rv)) {
       return rv;
     }
 
     if (!topWinURI && CachedPrefs::GetInstance()->IsAllowListExample()) {
       LOG(("nsChannelClassifier[%p]: Allowlisting test domain\n", this));
       rv = ios->NewURI(NS_LITERAL_CSTRING("http://allowlisted.example.com"),
                        nullptr, nullptr, getter_AddRefs(topWinURI));
       NS_ENSURE_SUCCESS(rv, rv);
     }
 
-    // Take the host/port portion so we can allowlist by site. Also ignore the
-    // scheme, since users who put sites on the allowlist probably don't expect
-    // allowlisting to depend on scheme.
-    nsCOMPtr<nsIURL> url = do_QueryInterface(topWinURI, &rv);
+    rv = AntiTrackingCommon::IsOnContentBlockingAllowList(topWinURI, mIsAllowListed);
     if (NS_FAILED(rv)) {
       return rv; // normal for some loads, no need to print a warning
     }
 
-    nsCString escaped(NS_LITERAL_CSTRING("https://"));
-    nsAutoCString temp;
-    rv = url->GetHostPort(temp);
-    NS_ENSURE_SUCCESS(rv, rv);
-    escaped.Append(temp);
-
-    // Stuff the whole thing back into a URI for the permission manager.
-    rv = ios->NewURI(escaped, nullptr, nullptr, getter_AddRefs(topWinURI));
-    NS_ENSURE_SUCCESS(rv, rv);
-
-    nsCOMPtr<nsIPermissionManager> permMgr =
-        do_GetService(NS_PERMISSIONMANAGER_CONTRACTID, &rv);
-    NS_ENSURE_SUCCESS(rv, rv);
-
-    // Check both the normal mode and private browsing mode user override permissions.
-    const char* types[] = {
-      "trackingprotection",
-      "trackingprotection-pb"
-    };
-
-    for (size_t i = 0; i < ArrayLength(types); ++i) {
-      uint32_t permissions = nsIPermissionManager::UNKNOWN_ACTION;
-      rv = permMgr->TestPermission(topWinURI, types[i], &permissions);
-      NS_ENSURE_SUCCESS(rv, rv);
-
-      if (permissions == nsIPermissionManager::ALLOW_ACTION) {
-        if (LOG_ENABLED()) {
-          nsCString chanSpec = chanURI->GetSpecOrDefault();
-          chanSpec.Truncate(std::min(chanSpec.Length(), sMaxSpecLength));
-          LOG(("nsChannelClassifier[%p]: User override on channel[%p] (%s) for %s (%s)",
-               this, aChannel, chanSpec.get(), escaped.get(), types[i]));
-        }
-        mIsAllowListed = true;
-        *result = false;
-        // Stop checking the next permisson type if we decided to override.
-        break;
-      } else if (i == ArrayLength(types) - 1) {
-        // Do this on the last iteration.
-        *result = true;
+    if (mIsAllowListed) {
+      *result = false;
+      if (LOG_ENABLED()) {
+        nsCString chanSpec = chanURI->GetSpecOrDefault();
+        chanSpec.Truncate(std::min(chanSpec.Length(), sMaxSpecLength));
+        LOG(("nsChannelClassifier[%p]: User override on channel[%p] (%s)",
+             this, aChannel, chanSpec.get()));
       }
+    } else {
+      *result = true;
     }
 
     // Tracking protection will be enabled so return without updating
     // the security state. If any channels are subsequently cancelled
     // (page elements blocked) the state will be then updated.
     if (*result) {
       if (LOG_ENABLED()) {
         nsCString chanSpec = chanURI->GetSpecOrDefault();
--- a/toolkit/components/antitracking/AntiTrackingCommon.cpp
+++ b/toolkit/components/antitracking/AntiTrackingCommon.cpp
@@ -11,19 +11,21 @@
 #include "mozilla/AbstractThread.h"
 #include "mozilla/Logging.h"
 #include "mozilla/StaticPrefs.h"
 #include "mozIThirdPartyUtil.h"
 #include "nsContentUtils.h"
 #include "nsGlobalWindowInner.h"
 #include "nsICookiePermission.h"
 #include "nsICookieService.h"
+#include "nsIIOService.h"
 #include "nsIPermissionManager.h"
 #include "nsIPrincipal.h"
 #include "nsIURI.h"
+#include "nsIURL.h"
 #include "nsPIDOMWindow.h"
 #include "nsScriptSecurityManager.h"
 #include "prtime.h"
 
 #define ANTITRACKING_PERM_KEY "3rdPartyStorage"
 
 using namespace mozilla;
 using mozilla::dom::ContentChild;
@@ -637,8 +639,71 @@ AntiTrackingCommon::MaybeIsFirstPartySto
   Unused << parentPrincipal->GetURI(getter_AddRefs(parentPrincipalURI));
   LOG_SPEC(("Testing permission type %s for %s resulted in %d (%s)",
             type.get(), _spec, int(result),
             result == nsIPermissionManager::ALLOW_ACTION ?
               "success" : "failure"), parentPrincipalURI);
 
   return result == nsIPermissionManager::ALLOW_ACTION;
 }
+
+nsresult
+AntiTrackingCommon::IsOnContentBlockingAllowList(nsIURI* aTopWinURI,
+                                                 bool& aIsAllowListed)
+{
+  aIsAllowListed = false;
+
+  LOG_SPEC(("Deciding whether the user has overridden content blocking for %s",
+            _spec), aTopWinURI);
+
+  nsCOMPtr<nsIIOService> ios = services::GetIOService();
+  NS_ENSURE_TRUE(ios, NS_ERROR_FAILURE);
+
+  // Take the host/port portion so we can allowlist by site. Also ignore the
+  // scheme, since users who put sites on the allowlist probably don't expect
+  // allowlisting to depend on scheme.
+  nsresult rv = NS_ERROR_FAILURE;
+  nsCOMPtr<nsIURL> url = do_QueryInterface(aTopWinURI, &rv);
+  if (NS_FAILED(rv)) {
+    return rv; // normal for some loads, no need to print a warning
+  }
+
+  nsCString escaped(NS_LITERAL_CSTRING("https://"));
+  nsAutoCString temp;
+  rv = url->GetHostPort(temp);
+  NS_ENSURE_SUCCESS(rv, rv);
+  escaped.Append(temp);
+
+  // Stuff the whole thing back into a URI for the permission manager.
+  nsCOMPtr<nsIURI> topWinURI;
+  rv = ios->NewURI(escaped, nullptr, nullptr, getter_AddRefs(topWinURI));
+  NS_ENSURE_SUCCESS(rv, rv);
+
+  nsCOMPtr<nsIPermissionManager> permMgr =
+    do_GetService(NS_PERMISSIONMANAGER_CONTRACTID, &rv);
+  NS_ENSURE_SUCCESS(rv, rv);
+
+  // Check both the normal mode and private browsing mode user override permissions.
+  const char* types[] = {
+    "trackingprotection",
+    "trackingprotection-pb"
+  };
+
+  for (size_t i = 0; i < ArrayLength(types); ++i) {
+    uint32_t permissions = nsIPermissionManager::UNKNOWN_ACTION;
+    rv = permMgr->TestPermission(topWinURI, types[i], &permissions);
+    NS_ENSURE_SUCCESS(rv, rv);
+
+    if (permissions == nsIPermissionManager::ALLOW_ACTION) {
+      aIsAllowListed = true;
+      LOG_SPEC(("Found user override type %s for %s", types[i], _spec),
+               topWinURI);
+      // Stop checking the next permisson type if we decided to override.
+      break;
+    }
+  }
+
+  if (!aIsAllowListed) {
+    LOG(("No user override found"));
+  }
+
+  return NS_OK;
+}
--- a/toolkit/components/antitracking/AntiTrackingCommon.h
+++ b/toolkit/components/antitracking/AntiTrackingCommon.h
@@ -78,13 +78,18 @@ public:
 
   // For IPC only.
   static void
   SaveFirstPartyStorageAccessGrantedForOriginOnParentProcess(nsIPrincipal* aPrincipal,
                                                              const nsCString& aParentOrigin,
                                                              const nsCString& aGrantedOrigin,
                                                              FirstPartyStorageAccessGrantedForOriginResolver&& aResolver);
 
+
+  // Check whether a top window URI is on the content blocking allow list.
+  static nsresult
+  IsOnContentBlockingAllowList(nsIURI* aTopWinURI, bool& aIsAllowListed);
+
 };
 
 } // namespace mozilla
 
 #endif // mozilla_antitrackingservice_h