Bug 1612378 - Part 4: Remove the ContentBlockingAllowListCache. r=dimi,baku
authorTim Huang <tihuang@mozilla.com>
Wed, 25 Mar 2020 13:20:44 +0000
changeset 520369 b05dfc8127b45f115817082b2f046615d099ea1f
parent 520368 1b39850be0bee76c13ec627c7a3ae2a46c5320b7
child 520370 7f47663d9e027ccfad57500af25d8ac8ae411ec2
push id37249
push userdvarga@mozilla.com
push dateWed, 25 Mar 2020 21:39:06 +0000
treeherdermozilla-central@b3c3f7d0f044 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersdimi, baku
bugs1612378
milestone76.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 1612378 - Part 4: Remove the ContentBlockingAllowListCache. r=dimi,baku Due to the fact that we compute the IsOnContentBlockingAllowList in the parent process and pass it as a flag to the content processes, we don't need to do the cache anymore. Because the check would be just to read a bool from the WindowContext. The cost should be low enough that we don't need a cache anymore. Differential Revision: https://phabricator.services.mozilla.com/D66211
toolkit/components/antitracking/ContentBlockingAllowList.cpp
toolkit/components/antitracking/ContentBlockingAllowList.h
toolkit/components/antitracking/ContentBlockingAllowListCache.h
--- a/toolkit/components/antitracking/ContentBlockingAllowList.cpp
+++ b/toolkit/components/antitracking/ContentBlockingAllowList.cpp
@@ -1,42 +1,26 @@
 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
 /* 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 "AntiTrackingLog.h"
 #include "ContentBlockingAllowList.h"
-#include "ContentBlockingAllowListCache.h"
-#include "SettingsChangeObserver.h"
 
 #include "mozilla/dom/BrowsingContext.h"
 #include "mozilla/dom/Document.h"
 #include "mozilla/ScopeExit.h"
 #include "nsContentUtils.h"
 #include "nsIHttpChannelInternal.h"
 #include "nsPermissionManager.h"
 
 using namespace mozilla;
 
-/* static */ ContentBlockingAllowListCache& ContentBlockingAllowList::Cache() {
-  static bool initialized = false;
-  static ContentBlockingAllowListCache cache;
-  if (!initialized) {
-    SettingsChangeObserver::OnAntiTrackingSettingsChanged([&] {
-      // Drop everything in the cache, since the result of content blocking
-      // allow list checks may change past this point.
-      cache.Clear();
-    });
-    initialized = true;
-  }
-  return cache;
-}
-
 /* static */ bool ContentBlockingAllowList::Check(
     nsIPrincipal* aTopWinPrincipal, bool aIsPrivateBrowsing) {
   bool isAllowed = false;
   nsresult rv = Check(aTopWinPrincipal, aIsPrivateBrowsing, isAllowed);
   if (NS_SUCCEEDED(rv) && isAllowed) {
     LOG(
         ("The top-level window is on the content blocking allow list, "
          "bail out early"));
@@ -44,82 +28,55 @@ using namespace mozilla;
   }
   if (NS_FAILED(rv)) {
     LOG(("Checking the content blocking allow list for failed with %" PRIx32,
          static_cast<uint32_t>(rv)));
   }
   return false;
 }
 
-/* static */ bool ContentBlockingAllowList::Check(nsPIDOMWindowInner* aWindow) {
-  ContentBlockingAllowListKey cacheKey(aWindow);
-  auto entry = Cache().Lookup(cacheKey);
-  if (entry) {
-    // We've recently performed a content blocking allow list check for this
-    // window, so let's quickly return the answer instead of continuing with the
-    // rest of this potentially expensive computation.
-    return entry.Data().mResult;
+/* static */ bool ContentBlockingAllowList::Check(
+    nsICookieJarSettings* aCookieJarSettings) {
+  if (!aCookieJarSettings) {
+    LOG(
+        ("Could not check the content blocking allow list because the cookie "
+         "jar settings wasn't available"));
+    return false;
   }
 
+  return aCookieJarSettings->GetIsOnContentBlockingAllowList();
+}
+
+/* static */ bool ContentBlockingAllowList::Check(nsPIDOMWindowInner* aWindow) {
   // We can check the IsOnContentBlockingAllowList flag in the document's
   // CookieJarSettings. Because this flag represents the fact that whether the
   // top-level document is on the content blocking allow list. And this flag was
   // propagated from the top-level as the CookieJarSettings inherits from the
   // parent.
   RefPtr<dom::Document> doc = nsGlobalWindowInner::Cast(aWindow)->GetDocument();
 
   if (!doc) {
     LOG(
         ("Could not check the content blocking allow list because the document "
          "wasn't available"));
     return false;
   }
 
   nsCOMPtr<nsICookieJarSettings> cookieJarSettings = doc->CookieJarSettings();
 
-  if (!cookieJarSettings) {
-    LOG(
-        ("Could not check the content blocking allow list because the cookie "
-         "jar settings wasn't available"));
-    entry.Set(ContentBlockingAllowListEntry(aWindow, false));
-    return false;
-  }
-
-  const bool result = cookieJarSettings->GetIsOnContentBlockingAllowList();
-  entry.Set(ContentBlockingAllowListEntry(aWindow, result));
-
-  return result;
+  return ContentBlockingAllowList::Check(cookieJarSettings);
 }
 
 /* static */ bool ContentBlockingAllowList::Check(nsIHttpChannel* aChannel) {
-  ContentBlockingAllowListKey cacheKey(aChannel);
-  auto entry = Cache().Lookup(cacheKey);
-  if (entry) {
-    // We've recently performed a content blocking allow list check for this
-    // channel, so let's quickly return the answer instead of continuing with
-    // the rest of this potentially expensive computation.
-    return entry.Data().mResult;
-  }
-
   nsCOMPtr<nsILoadInfo> loadInfo = aChannel->LoadInfo();
   nsCOMPtr<nsICookieJarSettings> cookieJarSettings;
 
   Unused << loadInfo->GetCookieJarSettings(getter_AddRefs(cookieJarSettings));
 
-  if (!cookieJarSettings) {
-    LOG(
-        ("Could not check the content blocking allow list because the "
-         "CookieJarSettings of the channel wasn't available"));
-    entry.Set(ContentBlockingAllowListEntry(aChannel, false));
-    return false;
-  }
-
-  const bool result = cookieJarSettings->GetIsOnContentBlockingAllowList();
-  entry.Set(ContentBlockingAllowListEntry(aChannel, result));
-  return result;
+  return ContentBlockingAllowList::Check(cookieJarSettings);
 }
 
 nsresult ContentBlockingAllowList::Check(
     nsIPrincipal* aContentBlockingAllowListPrincipal, bool aIsPrivateBrowsing,
     bool& aIsAllowListed) {
   aIsAllowListed = false;
 
   if (!aContentBlockingAllowListPrincipal) {
--- a/toolkit/components/antitracking/ContentBlockingAllowList.h
+++ b/toolkit/components/antitracking/ContentBlockingAllowList.h
@@ -2,16 +2,17 @@
 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
 /* 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/. */
 
 #ifndef mozilla_contentblockingallowlist_h
 #define mozilla_contentblockingallowlist_h
 
+class nsICookieJarSettings;
 class nsIHttpChannel;
 class nsIPrincipal;
 class nsIURI;
 class nsPIDOMWindowInner;
 
 namespace mozilla {
 
 class OriginAttributes;
@@ -34,22 +35,19 @@ class ContentBlockingAllowList final {
   static void ComputePrincipal(nsIPrincipal* aDocumentPrincipal,
                                nsIPrincipal** aPrincipal);
 
   static void RecomputePrincipal(nsIURI* aURIBeingLoaded,
                                  const OriginAttributes& aAttrs,
                                  nsIPrincipal** aPrincipal);
 
  private:
-  // Get the cache of allow list lookups for recent window/channels passed to
-  // Check().
-  static ContentBlockingAllowListCache& Cache();
-
   // Utility APIs for ContentBlocking.
   static bool Check(nsIPrincipal* aTopWinPrincipal, bool aIsPrivateBrowsing);
   static bool Check(nsPIDOMWindowInner* aWindow);
+  static bool Check(nsICookieJarSettings* aCookieJarSettings);
 
   friend class ContentBlocking;
 };
 
 }  // namespace mozilla
 
 #endif  // mozilla_contentblockingallowlist_h
deleted file mode 100644
--- a/toolkit/components/antitracking/ContentBlockingAllowListCache.h
+++ /dev/null
@@ -1,69 +0,0 @@
-/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
-/* vim: set ts=8 sts=2 et sw=2 tw=80: */
-/* 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/. */
-
-#ifndef mozilla_contentblockingallowlistcache_h
-#define mozilla_contentblockingallowlistcache_h
-
-#include "mozilla/HashFunctions.h"
-#include "mozilla/MruCache.h"
-#include "nsPIDOMWindow.h"
-#include "nsIHttpChannel.h"
-
-namespace mozilla {
-
-struct ContentBlockingAllowListKey {
-  ContentBlockingAllowListKey() : mHash(mozilla::HashGeneric(uintptr_t(0))) {}
-
-  // Ensure that we compute a different hash for window and channel pointers of
-  // the same numeric value, in the off chance that we get unlucky and encounter
-  // a case where the allocator reallocates a window object where a channel used
-  // to live and vice versa.
-  explicit ContentBlockingAllowListKey(nsPIDOMWindowInner* aWindow)
-      : mHash(mozilla::AddToHash(aWindow->WindowID(),
-                                 mozilla::HashString("window"))) {}
-  explicit ContentBlockingAllowListKey(nsIHttpChannel* aChannel)
-      : mHash(mozilla::AddToHash(aChannel->ChannelId(),
-                                 mozilla::HashString("channel"))) {}
-
-  ContentBlockingAllowListKey(const ContentBlockingAllowListKey& aRHS) =
-      default;
-
-  bool operator==(const ContentBlockingAllowListKey& aRHS) const {
-    return mHash == aRHS.mHash;
-  }
-
-  HashNumber GetHash() const { return mHash; }
-
- private:
-  HashNumber mHash;
-};
-
-struct ContentBlockingAllowListEntry {
-  ContentBlockingAllowListEntry() : mResult(false) {}
-  ContentBlockingAllowListEntry(nsPIDOMWindowInner* aWindow, bool aResult)
-      : mKey(aWindow), mResult(aResult) {}
-  ContentBlockingAllowListEntry(nsIHttpChannel* aChannel, bool aResult)
-      : mKey(aChannel), mResult(aResult) {}
-
-  ContentBlockingAllowListKey mKey;
-  bool mResult;
-};
-
-struct ContentBlockingAllowListCache
-    : MruCache<ContentBlockingAllowListKey, ContentBlockingAllowListEntry,
-               ContentBlockingAllowListCache> {
-  static HashNumber Hash(const ContentBlockingAllowListKey& aKey) {
-    return aKey.GetHash();
-  }
-  static bool Match(const ContentBlockingAllowListKey& aKey,
-                    const ContentBlockingAllowListEntry& aValue) {
-    return aValue.mKey == aKey;
-  }
-};
-
-}  // namespace mozilla
-
-#endif  // mozilla_contentblockingallowlistcache_h