Bug 1620322 - Part 7: Move TemporaryAccessGrantObserver out of AntiTrackingCommon.cpp; r=baku
authorEhsan Akhgari <ehsan@mozilla.com>
Mon, 09 Mar 2020 23:36:36 +0000
changeset 517702 da1235d7cba83d5adf1730242e5921aae3153263
parent 517701 3caedfb95834dd93c39e974c1b5afaca3e686488
child 517703 3634b502a4cf2f1afd7b44c4e9a8c20601d33ac0
push id37199
push useropoprus@mozilla.com
push dateTue, 10 Mar 2020 03:43:44 +0000
treeherdermozilla-central@6f21f98dcfcd [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbaku
bugs1620322
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 1620322 - Part 7: Move TemporaryAccessGrantObserver out of AntiTrackingCommon.cpp; r=baku Differential Revision: https://phabricator.services.mozilla.com/D65820
toolkit/components/antitracking/AntiTrackingCommon.cpp
toolkit/components/antitracking/TemporaryAccessGrantObserver.cpp
toolkit/components/antitracking/TemporaryAccessGrantObserver.h
toolkit/components/antitracking/moz.build
--- a/toolkit/components/antitracking/AntiTrackingCommon.cpp
+++ b/toolkit/components/antitracking/AntiTrackingCommon.cpp
@@ -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/. */
 
 #include "AntiTrackingLog.h"
 #include "AntiTrackingCommon.h"
 #include "AntiTrackingUtils.h"
+#include "TemporaryAccessGrantObserver.h"
 
 #include "mozilla/ContentBlockingAllowList.h"
 #include "mozilla/ContentBlockingUserInteraction.h"
 #include "mozilla/dom/BrowsingContext.h"
 #include "mozilla/dom/CanonicalBrowsingContext.h"
 #include "mozilla/dom/ContentChild.h"
 #include "mozilla/dom/WindowGlobalParent.h"
 #include "mozilla/net/UrlClassifierCommon.h"
@@ -158,147 +159,16 @@ int32_t CookiesBehavior(nsIPrincipal* aP
   // (See Bug 1406675 for rationale).
   if (BasePrincipal::Cast(aPrincipal)->AddonPolicy()) {
     return nsICookieService::BEHAVIOR_ACCEPT;
   }
 
   return aCookieJarSettings->GetCookieBehavior();
 }
 
-class TemporaryAccessGrantCacheKey : public PLDHashEntryHdr {
- public:
-  typedef Pair<nsCOMPtr<nsIPrincipal>, nsCString> KeyType;
-  typedef const KeyType* KeyTypePointer;
-
-  explicit TemporaryAccessGrantCacheKey(KeyTypePointer aKey)
-      : mPrincipal(aKey->first()), mType(aKey->second()) {}
-  TemporaryAccessGrantCacheKey(TemporaryAccessGrantCacheKey&& aOther) = default;
-
-  ~TemporaryAccessGrantCacheKey() = default;
-
-  KeyType GetKey() const { return MakePair(mPrincipal, mType); }
-  bool KeyEquals(KeyTypePointer aKey) const {
-    return !!mPrincipal == !!aKey->first() && mType == aKey->second() &&
-           (mPrincipal ? (mPrincipal->Equals(aKey->first())) : true);
-  }
-
-  static KeyTypePointer KeyToPointer(KeyType& aKey) { return &aKey; }
-  static PLDHashNumber HashKey(KeyTypePointer aKey) {
-    if (!aKey) {
-      return 0;
-    }
-
-    BasePrincipal* bp = BasePrincipal::Cast(aKey->first());
-    return HashGeneric(bp->GetOriginNoSuffixHash(), bp->GetOriginSuffixHash(),
-                       HashString(aKey->second()));
-  }
-
-  enum { ALLOW_MEMMOVE = true };
-
- private:
-  nsCOMPtr<nsIPrincipal> mPrincipal;
-  nsCString mType;
-};
-
-class TemporaryAccessGrantObserver final : public nsIObserver {
- public:
-  NS_DECL_ISUPPORTS
-  NS_DECL_NSIOBSERVER
-
-  static void Create(nsPermissionManager* aPM, nsIPrincipal* aPrincipal,
-                     const nsACString& aType) {
-    MOZ_ASSERT(XRE_IsParentProcess());
-
-    if (!sObservers) {
-      sObservers = MakeUnique<ObserversTable>();
-    }
-    Unused << sObservers
-                  ->LookupForAdd(MakePair(nsCOMPtr<nsIPrincipal>(aPrincipal),
-                                          nsCString(aType)))
-                  .OrInsert([&]() -> nsITimer* {
-                    // Only create a new observer if we don't have a matching
-                    // entry in our hashtable.
-                    nsCOMPtr<nsITimer> timer;
-                    RefPtr<TemporaryAccessGrantObserver> observer =
-                        new TemporaryAccessGrantObserver(aPM, aPrincipal,
-                                                         aType);
-                    nsresult rv = NS_NewTimerWithObserver(
-                        getter_AddRefs(timer), observer,
-                        24 * 60 * 60 * 1000,  // 24 hours
-                        nsITimer::TYPE_ONE_SHOT);
-
-                    if (NS_SUCCEEDED(rv)) {
-                      observer->SetTimer(timer);
-                      return timer;
-                    }
-                    timer->Cancel();
-                    return nullptr;
-                  });
-  }
-
-  void SetTimer(nsITimer* aTimer) {
-    mTimer = aTimer;
-    nsCOMPtr<nsIObserverService> observerService =
-        mozilla::services::GetObserverService();
-    if (observerService) {
-      observerService->AddObserver(this, NS_XPCOM_SHUTDOWN_OBSERVER_ID, false);
-    }
-  }
-
- private:
-  TemporaryAccessGrantObserver(nsPermissionManager* aPM,
-                               nsIPrincipal* aPrincipal,
-                               const nsACString& aType)
-      : mPM(aPM), mPrincipal(aPrincipal), mType(aType) {
-    MOZ_ASSERT(XRE_IsParentProcess(),
-               "Enforcing temporary access grant lifetimes can only be done in "
-               "the parent process");
-  }
-
-  ~TemporaryAccessGrantObserver() = default;
-
- private:
-  typedef nsDataHashtable<TemporaryAccessGrantCacheKey, nsCOMPtr<nsITimer>>
-      ObserversTable;
-  static UniquePtr<ObserversTable> sObservers;
-  nsCOMPtr<nsITimer> mTimer;
-  RefPtr<nsPermissionManager> mPM;
-  nsCOMPtr<nsIPrincipal> mPrincipal;
-  nsCString mType;
-};
-
-UniquePtr<TemporaryAccessGrantObserver::ObserversTable>
-    TemporaryAccessGrantObserver::sObservers;
-
-NS_IMPL_ISUPPORTS(TemporaryAccessGrantObserver, nsIObserver)
-
-NS_IMETHODIMP
-TemporaryAccessGrantObserver::Observe(nsISupports* aSubject, const char* aTopic,
-                                      const char16_t* aData) {
-  if (strcmp(aTopic, NS_TIMER_CALLBACK_TOPIC) == 0) {
-    Unused << mPM->RemoveFromPrincipal(mPrincipal, mType);
-
-    MOZ_ASSERT(sObservers);
-    sObservers->Remove(MakePair(mPrincipal, mType));
-  } else if (strcmp(aTopic, NS_XPCOM_SHUTDOWN_OBSERVER_ID) == 0) {
-    nsCOMPtr<nsIObserverService> observerService =
-        mozilla::services::GetObserverService();
-    if (observerService) {
-      observerService->RemoveObserver(this, NS_XPCOM_SHUTDOWN_OBSERVER_ID);
-    }
-    if (mTimer) {
-      mTimer->Cancel();
-      mTimer = nullptr;
-    }
-    sObservers.reset();
-  }
-
-  return NS_OK;
-}
-
 bool CheckAntiTrackingPermission(nsIPrincipal* aPrincipal,
                                  const nsAutoCString& aType,
                                  bool aIsInPrivateBrowsing,
                                  uint32_t* aRejectedReason,
                                  uint32_t aBlockedReason) {
   nsPermissionManager* permManager = nsPermissionManager::GetInstance();
   if (NS_WARN_IF(!permManager)) {
     LOG(("Failed to obtain the permission manager"));
new file mode 100644
--- /dev/null
+++ b/toolkit/components/antitracking/TemporaryAccessGrantObserver.cpp
@@ -0,0 +1,92 @@
+/* -*- 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 "TemporaryAccessGrantObserver.h"
+#include "AntiTrackingCommon.h"
+
+#include "nsIObserverService.h"
+#include "nsPermissionManager.h"
+#include "nsTHashtable.h"
+
+using namespace mozilla;
+
+UniquePtr<TemporaryAccessGrantObserver::ObserversTable>
+    TemporaryAccessGrantObserver::sObservers;
+
+TemporaryAccessGrantObserver::TemporaryAccessGrantObserver(
+    nsPermissionManager* aPM, nsIPrincipal* aPrincipal, const nsACString& aType)
+    : mPM(aPM), mPrincipal(aPrincipal), mType(aType) {
+  MOZ_ASSERT(XRE_IsParentProcess(),
+             "Enforcing temporary access grant lifetimes can only be done in "
+             "the parent process");
+}
+
+NS_IMPL_ISUPPORTS(TemporaryAccessGrantObserver, nsIObserver)
+
+// static
+void TemporaryAccessGrantObserver::Create(nsPermissionManager* aPM,
+                                          nsIPrincipal* aPrincipal,
+                                          const nsACString& aType) {
+  MOZ_ASSERT(XRE_IsParentProcess());
+
+  if (!sObservers) {
+    sObservers = MakeUnique<ObserversTable>();
+  }
+  Unused << sObservers
+                ->LookupForAdd(MakePair(nsCOMPtr<nsIPrincipal>(aPrincipal),
+                                        nsCString(aType)))
+                .OrInsert([&]() -> nsITimer* {
+                  // Only create a new observer if we don't have a matching
+                  // entry in our hashtable.
+                  nsCOMPtr<nsITimer> timer;
+                  RefPtr<TemporaryAccessGrantObserver> observer =
+                      new TemporaryAccessGrantObserver(aPM, aPrincipal, aType);
+                  nsresult rv =
+                      NS_NewTimerWithObserver(getter_AddRefs(timer), observer,
+                                              24 * 60 * 60 * 1000,  // 24 hours
+                                              nsITimer::TYPE_ONE_SHOT);
+
+                  if (NS_SUCCEEDED(rv)) {
+                    observer->SetTimer(timer);
+                    return timer;
+                  }
+                  timer->Cancel();
+                  return nullptr;
+                });
+}
+
+void TemporaryAccessGrantObserver::SetTimer(nsITimer* aTimer) {
+  mTimer = aTimer;
+  nsCOMPtr<nsIObserverService> observerService =
+      mozilla::services::GetObserverService();
+  if (observerService) {
+    observerService->AddObserver(this, NS_XPCOM_SHUTDOWN_OBSERVER_ID, false);
+  }
+}
+
+NS_IMETHODIMP
+TemporaryAccessGrantObserver::Observe(nsISupports* aSubject, const char* aTopic,
+                                      const char16_t* aData) {
+  if (strcmp(aTopic, NS_TIMER_CALLBACK_TOPIC) == 0) {
+    Unused << mPM->RemoveFromPrincipal(mPrincipal, mType);
+
+    MOZ_ASSERT(sObservers);
+    sObservers->Remove(MakePair(mPrincipal, mType));
+  } else if (strcmp(aTopic, NS_XPCOM_SHUTDOWN_OBSERVER_ID) == 0) {
+    nsCOMPtr<nsIObserverService> observerService =
+        mozilla::services::GetObserverService();
+    if (observerService) {
+      observerService->RemoveObserver(this, NS_XPCOM_SHUTDOWN_OBSERVER_ID);
+    }
+    if (mTimer) {
+      mTimer->Cancel();
+      mTimer = nullptr;
+    }
+    sObservers.reset();
+  }
+
+  return NS_OK;
+}
new file mode 100644
--- /dev/null
+++ b/toolkit/components/antitracking/TemporaryAccessGrantObserver.h
@@ -0,0 +1,88 @@
+/* -*- 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_temporaryaccessgrantobserver_h
+#define mozilla_temporaryaccessgrantobserver_h
+
+#include "mozilla/BasePrincipal.h"
+#include "nsCOMPtr.h"
+#include "nsHashKeys.h"
+#include "nsIObserver.h"
+#include "nsString.h"
+#include "PLDHashTable.h"
+
+template <class, class>
+class nsDataHashtable;
+class nsITimer;
+class nsPermissionManager;
+class TemporaryAccessGrantCacheKey;
+
+namespace mozilla {
+
+class TemporaryAccessGrantCacheKey : public PLDHashEntryHdr {
+ public:
+  typedef Pair<nsCOMPtr<nsIPrincipal>, nsCString> KeyType;
+  typedef const KeyType* KeyTypePointer;
+
+  explicit TemporaryAccessGrantCacheKey(KeyTypePointer aKey)
+      : mPrincipal(aKey->first()), mType(aKey->second()) {}
+  TemporaryAccessGrantCacheKey(TemporaryAccessGrantCacheKey&& aOther) = default;
+
+  ~TemporaryAccessGrantCacheKey() = default;
+
+  KeyType GetKey() const { return MakePair(mPrincipal, mType); }
+  bool KeyEquals(KeyTypePointer aKey) const {
+    return !!mPrincipal == !!aKey->first() && mType == aKey->second() &&
+           (mPrincipal ? (mPrincipal->Equals(aKey->first())) : true);
+  }
+
+  static KeyTypePointer KeyToPointer(KeyType& aKey) { return &aKey; }
+  static PLDHashNumber HashKey(KeyTypePointer aKey) {
+    if (!aKey) {
+      return 0;
+    }
+
+    BasePrincipal* bp = BasePrincipal::Cast(aKey->first());
+    return HashGeneric(bp->GetOriginNoSuffixHash(), bp->GetOriginSuffixHash(),
+                       HashString(aKey->second()));
+  }
+
+  enum { ALLOW_MEMMOVE = true };
+
+ private:
+  nsCOMPtr<nsIPrincipal> mPrincipal;
+  nsCString mType;
+};
+
+class TemporaryAccessGrantObserver final : public nsIObserver {
+ public:
+  NS_DECL_ISUPPORTS
+  NS_DECL_NSIOBSERVER
+
+  static void Create(nsPermissionManager* aPM, nsIPrincipal* aPrincipal,
+                     const nsACString& aType);
+
+  void SetTimer(nsITimer* aTimer);
+
+ private:
+  TemporaryAccessGrantObserver(nsPermissionManager* aPM,
+                               nsIPrincipal* aPrincipal,
+                               const nsACString& aType);
+  ~TemporaryAccessGrantObserver() = default;
+
+ private:
+  typedef nsDataHashtable<TemporaryAccessGrantCacheKey, nsCOMPtr<nsITimer>>
+      ObserversTable;
+  static UniquePtr<ObserversTable> sObservers;
+  nsCOMPtr<nsITimer> mTimer;
+  RefPtr<nsPermissionManager> mPM;
+  nsCOMPtr<nsIPrincipal> mPrincipal;
+  nsCString mType;
+};
+
+}  // namespace mozilla
+
+#endif  // mozilla_temporaryaccessgrantobserver_h
--- a/toolkit/components/antitracking/moz.build
+++ b/toolkit/components/antitracking/moz.build
@@ -48,16 +48,17 @@ UNIFIED_SOURCES += [
     'AntiTrackingRedirectHeuristic.cpp',
     'AntiTrackingUtils.cpp',
     'ContentBlockingAllowList.cpp',
     'ContentBlockingNotifier.cpp',
     'ContentBlockingUserInteraction.cpp',
     'SettingsChangeObserver.cpp',
     'StorageAccess.cpp',
     'StoragePrincipalHelper.cpp',
+    'TemporaryAccessGrantObserver.cpp',
     'URLDecorationStripper.cpp',
 ]
 
 LOCAL_INCLUDES += [
     '/extensions/permissions',
 ]
 
 include('/ipc/chromium/chromium-config.mozbuild')