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"
13 #include "nsHashKeys.h"
14 #include "nsHashtablesFwd.h"
15 #include "nsTHashMap.h"
17 #include "nsIObserver.h"
19 #include "PLDHashTable.h"
22 class TemporaryAccessGrantCacheKey
;
26 class PermissionManager
;
28 class TemporaryAccessGrantCacheKey
: public PrincipalHashKey
{
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
) {
50 return HashGeneric(PrincipalHashKey::HashKey(aKey
->first
),
51 HashString(aKey
->second
));
54 enum { ALLOW_MEMMOVE
= true };
60 class TemporaryAccessGrantObserver final
: public nsIObserver
, public nsINamed
{
66 static void Create(PermissionManager
* aPM
, nsIPrincipal
* aPrincipal
,
67 const nsACString
& aType
);
69 void SetTimer(nsITimer
* aTimer
);
72 TemporaryAccessGrantObserver(PermissionManager
* aPM
, nsIPrincipal
* aPrincipal
,
73 const nsACString
& aType
);
74 ~TemporaryAccessGrantObserver() = default;
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
;
86 } // namespace mozilla
88 #endif // mozilla_temporaryaccessgrantobserver_h