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
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 #ifndef mozilla_PrincipalHashKey_h
8 #define mozilla_PrincipalHashKey_h
9 
10 #include "BasePrincipal.h"
11 #include "PLDHashTable.h"
12 #include "mozilla/Unused.h"
13 #include "nsCOMPtr.h"
14 #include "nsHashKeys.h"
15 
16 namespace mozilla {
17 
18 class PrincipalHashKey : public PLDHashEntryHdr {
19  public:
20   using KeyType = nsIPrincipal*;
21   using KeyTypePointer = const nsIPrincipal*;
22 
PrincipalHashKey(const nsIPrincipal * aKey)23   explicit PrincipalHashKey(const nsIPrincipal* aKey)
24       : mPrincipal(const_cast<nsIPrincipal*>(aKey)) {
25     MOZ_ASSERT(aKey);
26     MOZ_COUNT_CTOR(PrincipalHashKey);
27   }
PrincipalHashKey(PrincipalHashKey && aKey)28   PrincipalHashKey(PrincipalHashKey&& aKey)
29       : mPrincipal(std::move(aKey.mPrincipal)) {
30     MOZ_COUNT_CTOR(PrincipalHashKey);
31   }
32 
MOZ_COUNTED_DTOR(PrincipalHashKey)33   MOZ_COUNTED_DTOR(PrincipalHashKey)
34 
35   nsIPrincipal* GetKey() const { return mPrincipal; }
36 
KeyEquals(const nsIPrincipal * aKey)37   bool KeyEquals(const nsIPrincipal* aKey) const {
38     return BasePrincipal::Cast(mPrincipal)
39         ->FastEquals(const_cast<nsIPrincipal*>(aKey));
40   }
41 
KeyToPointer(const nsIPrincipal * aKey)42   static const nsIPrincipal* KeyToPointer(const nsIPrincipal* aKey) {
43     return aKey;
44   }
HashKey(const nsIPrincipal * aKey)45   static PLDHashNumber HashKey(const nsIPrincipal* aKey) {
46     auto* bp = BasePrincipal::Cast(aKey);
47     return HashGeneric(bp->GetOriginNoSuffixHash(), bp->GetOriginSuffixHash());
48   }
49 
50   enum { ALLOW_MEMMOVE = true };
51 
52  protected:
53   nsCOMPtr<nsIPrincipal> mPrincipal;
54 };
55 
56 }  // namespace mozilla
57 
58 #endif
59