Bug 1546334 - Add hash and equals to make ReferrerInfo becomes key of hashtable. r=heycam

Differential Revision: https://phabricator.services.mozilla.com/D37577

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Thomas Nguyen 2019-07-16 15:03:56 +00:00
Родитель a4dabe848c
Коммит 268b26c194
3 изменённых файлов: 64 добавлений и 0 удалений

Просмотреть файл

@ -39,6 +39,11 @@ interface nsIReferrerInfo : nsISerializable
[must_use, noscript, nostdcall, notxpcom]
URIRef GetComputedReferrer();
/**
* Returns whether the other referrerInfo is equivalent to this referrerInfo.
*/
boolean equals(in nsIReferrerInfo other);
/**
* Initialize method.
* @param aReferrerPolicy referrer policy of the created object

Просмотреть файл

@ -708,6 +708,42 @@ ReferrerInfo::GetSendReferrer(bool* aSendReferrer) {
return NS_OK;
}
NS_IMETHODIMP
ReferrerInfo::Equals(nsIReferrerInfo* aOther, bool* aResult) {
NS_ENSURE_TRUE(aOther, NS_ERROR_INVALID_ARG);
MOZ_ASSERT(mInitialized);
if (aOther == this) {
*aResult = true;
return NS_OK;
}
*aResult = false;
ReferrerInfo* other = static_cast<ReferrerInfo*>(aOther);
MOZ_ASSERT(other->mInitialized);
if (mPolicy != other->mPolicy || mSendReferrer != other->mSendReferrer ||
mOverridePolicyByDefault != other->mOverridePolicyByDefault ||
mComputedReferrer != other->mComputedReferrer) {
return NS_OK;
}
if (!mOriginalReferrer != !other->mOriginalReferrer) {
// One or the other has mOriginalReferrer, but not both... not equal
return NS_OK;
}
bool originalReferrerEquals;
if (mOriginalReferrer &&
(NS_FAILED(mOriginalReferrer->Equals(other->mOriginalReferrer,
&originalReferrerEquals)) ||
!originalReferrerEquals)) {
return NS_OK;
}
*aResult = true;
return NS_OK;
}
already_AddRefed<nsIURI> ReferrerInfo::GetComputedReferrer() {
if (!mComputedReferrer.isSome() || mComputedReferrer.value().IsEmpty()) {
return nullptr;
@ -722,6 +758,20 @@ already_AddRefed<nsIURI> ReferrerInfo::GetComputedReferrer() {
return result.forget();
}
PLDHashNumber ReferrerInfo::Hash() const {
MOZ_ASSERT(mInitialized);
nsAutoCString originalReferrerSpec;
if (mOriginalReferrer) {
Unused << mOriginalReferrer->GetSpec(originalReferrerSpec);
}
return mozilla::AddToHash(
mPolicy, mSendReferrer, mOverridePolicyByDefault,
mozilla::HashString(originalReferrerSpec),
mozilla::HashString(mComputedReferrer.isSome() ? mComputedReferrer.value()
: EmptyCString()));
}
NS_IMETHODIMP
ReferrerInfo::Init(uint32_t aReferrerPolicy, bool aSendReferrer,
nsIURI* aOriginalReferrer) {

Просмотреть файл

@ -13,6 +13,7 @@
#include "mozilla/net/ReferrerPolicy.h"
#include "nsReadableUtils.h"
#include "mozilla/Maybe.h"
#include "mozilla/HashFunctions.h"
#define REFERRERINFOF_CONTRACTID "@mozilla.org/referrer-info;1"
// 041a129f-10ce-4bda-a60d-e027a26d5ed0
@ -31,6 +32,7 @@ class nsIPrincipal;
namespace mozilla {
class StyleSheet;
class URLAndReferrerInfo;
namespace net {
class HttpBaseChannel;
@ -187,6 +189,11 @@ class ReferrerInfo : public nsIReferrerInfo {
nsIURI* aURI = nullptr,
bool privateBrowsing = false);
/**
* Hash function for this object
*/
PLDHashNumber Hash() const;
NS_DECL_THREADSAFE_ISUPPORTS
NS_DECL_NSIREFERRERINFO
NS_DECL_NSISERIALIZABLE
@ -348,6 +355,8 @@ class ReferrerInfo : public nsIReferrerInfo {
void LogMessageToConsole(nsIHttpChannel* aChannel, const char* aMsg,
const nsTArray<nsString>& aParams) const;
friend class mozilla::URLAndReferrerInfo;
nsCOMPtr<nsIURI> mOriginalReferrer;
uint32_t mPolicy;