summaryrefslogtreecommitdiffstats
path: root/toolkit/components/antitracking/TemporaryAccessGrantObserver.h
blob: adb83f398f5fcda3d7f0956f162ef48d248e1e56 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
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/PrincipalHashKey.h"
#include "mozilla/StaticPtr.h"
#include "nsCOMPtr.h"
#include "nsHashKeys.h"
#include "nsHashtablesFwd.h"
#include "nsTHashMap.h"
#include "nsINamed.h"
#include "nsIObserver.h"
#include "nsString.h"
#include "PLDHashTable.h"

class nsITimer;
class TemporaryAccessGrantCacheKey;

namespace mozilla {

class PermissionManager;

class TemporaryAccessGrantCacheKey : public PrincipalHashKey {
 public:
  typedef std::pair<nsCOMPtr<nsIPrincipal>, nsCString> KeyType;
  typedef const KeyType* KeyTypePointer;

  explicit TemporaryAccessGrantCacheKey(KeyTypePointer aKey)
      : PrincipalHashKey(aKey->first), mType(aKey->second) {}
  TemporaryAccessGrantCacheKey(TemporaryAccessGrantCacheKey&& aOther) = default;

  ~TemporaryAccessGrantCacheKey() = default;

  KeyType GetKey() const { return std::make_pair(mPrincipal, mType); }
  bool KeyEquals(KeyTypePointer aKey) const {
    return PrincipalHashKey::KeyEquals(aKey->first) && mType == aKey->second;
  }

  static KeyTypePointer KeyToPointer(KeyType& aKey) { return &aKey; }
  static PLDHashNumber HashKey(KeyTypePointer aKey) {
    if (!aKey) {
      return 0;
    }

    return HashGeneric(PrincipalHashKey::HashKey(aKey->first),
                       HashString(aKey->second));
  }

  enum { ALLOW_MEMMOVE = true };

 private:
  nsCString mType;
};

class TemporaryAccessGrantObserver final : public nsIObserver, public nsINamed {
 public:
  NS_DECL_ISUPPORTS
  NS_DECL_NSIOBSERVER
  NS_DECL_NSINAMED

  static void Create(PermissionManager* aPM, nsIPrincipal* aPrincipal,
                     const nsACString& aType);

  void SetTimer(nsITimer* aTimer);

 private:
  TemporaryAccessGrantObserver(PermissionManager* aPM, nsIPrincipal* aPrincipal,
                               const nsACString& aType);
  ~TemporaryAccessGrantObserver() = default;

 private:
  using ObserversTable =
      nsTHashMap<TemporaryAccessGrantCacheKey, nsCOMPtr<nsITimer>>;
  static StaticAutoPtr<ObserversTable> sObservers;
  nsCOMPtr<nsITimer> mTimer;
  RefPtr<PermissionManager> mPM;
  nsCOMPtr<nsIPrincipal> mPrincipal;
  nsCString mType;
};

}  // namespace mozilla

#endif  // mozilla_temporaryaccessgrantobserver_h