Commit 8883f72d authored by Emilio Cobos Álvarez's avatar Emilio Cobos Álvarez
Browse files

Bug 1641936 - Add a generic principal hash key. r=bholley

Differential Revision: https://phabricator.services.mozilla.com/D77501
parent 98dd8642
/* -*- 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_PrincipalHashKey_h
#define mozilla_PrincipalHashKey_h
#include "BasePrincipal.h"
#include "PLDHashTable.h"
#include "mozilla/Unused.h"
#include "nsCOMPtr.h"
#include "nsHashKeys.h"
namespace mozilla {
class PrincipalHashKey : public PLDHashEntryHdr {
public:
using KeyType = nsIPrincipal*;
using KeyTypePointer = const nsIPrincipal*;
explicit PrincipalHashKey(const nsIPrincipal* aKey)
: mPrincipal(const_cast<nsIPrincipal*>(aKey)) {
MOZ_ASSERT(aKey);
MOZ_COUNT_CTOR(PrincipalHashKey);
}
PrincipalHashKey(PrincipalHashKey&& aKey)
: mPrincipal(std::move(aKey.mPrincipal)) {
MOZ_COUNT_CTOR(PrincipalHashKey);
}
MOZ_COUNTED_DTOR(PrincipalHashKey)
nsIPrincipal* GetKey() const { return mPrincipal; }
bool KeyEquals(const nsIPrincipal* aKey) const {
return BasePrincipal::Cast(mPrincipal)
->FastEquals(const_cast<nsIPrincipal*>(aKey));
}
static const nsIPrincipal* KeyToPointer(const nsIPrincipal* aKey) {
return aKey;
}
static PLDHashNumber HashKey(const nsIPrincipal* aKey) {
auto* bp = BasePrincipal::Cast(aKey);
return HashGeneric(bp->GetOriginNoSuffixHash(), bp->GetOriginSuffixHash());
}
enum { ALLOW_MEMMOVE = true };
protected:
nsCOMPtr<nsIPrincipal> mPrincipal;
};
} // namespace mozilla
#endif
......@@ -35,6 +35,7 @@ EXPORTS.mozilla = [
'NullPrincipal.h',
'NullPrincipalURI.h',
'OriginAttributes.h',
'PrincipalHashKey.h',
'SystemPrincipal.h',
]
......
......@@ -7,7 +7,7 @@
#ifndef mozilla_temporaryaccessgrantobserver_h
#define mozilla_temporaryaccessgrantobserver_h
#include "mozilla/BasePrincipal.h"
#include "mozilla/PrincipalHashKey.h"
#include "nsCOMPtr.h"
#include "nsHashKeys.h"
#include "nsIObserver.h"
......@@ -23,21 +23,20 @@ namespace mozilla {
class PermissionManager;
class TemporaryAccessGrantCacheKey : public PLDHashEntryHdr {
class TemporaryAccessGrantCacheKey : public PrincipalHashKey {
public:
typedef std::pair<nsCOMPtr<nsIPrincipal>, nsCString> KeyType;
typedef const KeyType* KeyTypePointer;
explicit TemporaryAccessGrantCacheKey(KeyTypePointer aKey)
: mPrincipal(aKey->first), mType(aKey->second) {}
: 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 !!mPrincipal == !!aKey->first && mType == aKey->second &&
(mPrincipal ? (mPrincipal->Equals(aKey->first)) : true);
return PrincipalHashKey::KeyEquals(aKey->first) && mType == aKey->second;
}
static KeyTypePointer KeyToPointer(KeyType& aKey) { return &aKey; }
......@@ -46,15 +45,13 @@ class TemporaryAccessGrantCacheKey : public PLDHashEntryHdr {
return 0;
}
BasePrincipal* bp = BasePrincipal::Cast(aKey->first);
return HashGeneric(bp->GetOriginNoSuffixHash(), bp->GetOriginSuffixHash(),
return HashGeneric(PrincipalHashKey::HashKey(aKey->first),
HashString(aKey->second));
}
enum { ALLOW_MEMMOVE = true };
private:
nsCOMPtr<nsIPrincipal> mPrincipal;
nsCString mType;
};
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment