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/. */
6 
7 #ifndef mozilla_temporaryaccessgrantobserver_h
8 #define mozilla_temporaryaccessgrantobserver_h
9 
10 #include "mozilla/PrincipalHashKey.h"
11 #include "nsCOMPtr.h"
12 #include "nsHashKeys.h"
13 #include "nsIObserver.h"
14 #include "nsString.h"
15 #include "PLDHashTable.h"
16 
17 template <class, class>
18 class nsDataHashtable;
19 class nsITimer;
20 class TemporaryAccessGrantCacheKey;
21 
22 namespace mozilla {
23 
24 class PermissionManager;
25 
26 class TemporaryAccessGrantCacheKey : public PrincipalHashKey {
27  public:
28   typedef std::pair<nsCOMPtr<nsIPrincipal>, nsCString> KeyType;
29   typedef const KeyType* KeyTypePointer;
30 
TemporaryAccessGrantCacheKey(KeyTypePointer aKey)31   explicit TemporaryAccessGrantCacheKey(KeyTypePointer aKey)
32       : PrincipalHashKey(aKey->first), mType(aKey->second) {}
33   TemporaryAccessGrantCacheKey(TemporaryAccessGrantCacheKey&& aOther) = default;
34 
35   ~TemporaryAccessGrantCacheKey() = default;
36 
GetKey()37   KeyType GetKey() const { return std::make_pair(mPrincipal, mType); }
KeyEquals(KeyTypePointer aKey)38   bool KeyEquals(KeyTypePointer aKey) const {
39     return PrincipalHashKey::KeyEquals(aKey->first) && mType == aKey->second;
40   }
41 
KeyToPointer(KeyType & aKey)42   static KeyTypePointer KeyToPointer(KeyType& aKey) { return &aKey; }
HashKey(KeyTypePointer aKey)43   static PLDHashNumber HashKey(KeyTypePointer aKey) {
44     if (!aKey) {
45       return 0;
46     }
47 
48     return HashGeneric(PrincipalHashKey::HashKey(aKey->first),
49                        HashString(aKey->second));
50   }
51 
52   enum { ALLOW_MEMMOVE = true };
53 
54  private:
55   nsCString mType;
56 };
57 
58 class TemporaryAccessGrantObserver final : public nsIObserver {
59  public:
60   NS_DECL_ISUPPORTS
61   NS_DECL_NSIOBSERVER
62 
63   static void Create(PermissionManager* aPM, nsIPrincipal* aPrincipal,
64                      const nsACString& aType);
65 
66   void SetTimer(nsITimer* aTimer);
67 
68  private:
69   TemporaryAccessGrantObserver(PermissionManager* aPM, nsIPrincipal* aPrincipal,
70                                const nsACString& aType);
71   ~TemporaryAccessGrantObserver() = default;
72 
73  private:
74   typedef nsDataHashtable<TemporaryAccessGrantCacheKey, nsCOMPtr<nsITimer>>
75       ObserversTable;
76   static UniquePtr<ObserversTable> sObservers;
77   nsCOMPtr<nsITimer> mTimer;
78   RefPtr<PermissionManager> mPM;
79   nsCOMPtr<nsIPrincipal> mPrincipal;
80   nsCString mType;
81 };
82 
83 }  // namespace mozilla
84 
85 #endif  // mozilla_temporaryaccessgrantobserver_h
86