Bug 1942239 - Add option to explicitly enable incremental origin initialization in...
[gecko.git] / toolkit / components / antitracking / TemporaryAccessGrantObserver.h
blobadb83f398f5fcda3d7f0956f162ef48d248e1e56
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef mozilla_temporaryaccessgrantobserver_h
8 #define mozilla_temporaryaccessgrantobserver_h
10 #include "mozilla/PrincipalHashKey.h"
11 #include "mozilla/StaticPtr.h"
12 #include "nsCOMPtr.h"
13 #include "nsHashKeys.h"
14 #include "nsHashtablesFwd.h"
15 #include "nsTHashMap.h"
16 #include "nsINamed.h"
17 #include "nsIObserver.h"
18 #include "nsString.h"
19 #include "PLDHashTable.h"
21 class nsITimer;
22 class TemporaryAccessGrantCacheKey;
24 namespace mozilla {
26 class PermissionManager;
28 class TemporaryAccessGrantCacheKey : public PrincipalHashKey {
29 public:
30 typedef std::pair<nsCOMPtr<nsIPrincipal>, nsCString> KeyType;
31 typedef const KeyType* KeyTypePointer;
33 explicit TemporaryAccessGrantCacheKey(KeyTypePointer aKey)
34 : PrincipalHashKey(aKey->first), mType(aKey->second) {}
35 TemporaryAccessGrantCacheKey(TemporaryAccessGrantCacheKey&& aOther) = default;
37 ~TemporaryAccessGrantCacheKey() = default;
39 KeyType GetKey() const { return std::make_pair(mPrincipal, mType); }
40 bool KeyEquals(KeyTypePointer aKey) const {
41 return PrincipalHashKey::KeyEquals(aKey->first) && mType == aKey->second;
44 static KeyTypePointer KeyToPointer(KeyType& aKey) { return &aKey; }
45 static PLDHashNumber HashKey(KeyTypePointer aKey) {
46 if (!aKey) {
47 return 0;
50 return HashGeneric(PrincipalHashKey::HashKey(aKey->first),
51 HashString(aKey->second));
54 enum { ALLOW_MEMMOVE = true };
56 private:
57 nsCString mType;
60 class TemporaryAccessGrantObserver final : public nsIObserver, public nsINamed {
61 public:
62 NS_DECL_ISUPPORTS
63 NS_DECL_NSIOBSERVER
64 NS_DECL_NSINAMED
66 static void Create(PermissionManager* aPM, nsIPrincipal* aPrincipal,
67 const nsACString& aType);
69 void SetTimer(nsITimer* aTimer);
71 private:
72 TemporaryAccessGrantObserver(PermissionManager* aPM, nsIPrincipal* aPrincipal,
73 const nsACString& aType);
74 ~TemporaryAccessGrantObserver() = default;
76 private:
77 using ObserversTable =
78 nsTHashMap<TemporaryAccessGrantCacheKey, nsCOMPtr<nsITimer>>;
79 static StaticAutoPtr<ObserversTable> sObservers;
80 nsCOMPtr<nsITimer> mTimer;
81 RefPtr<PermissionManager> mPM;
82 nsCOMPtr<nsIPrincipal> mPrincipal;
83 nsCString mType;
86 } // namespace mozilla
88 #endif // mozilla_temporaryaccessgrantobserver_h