Bug 1646056 - Use const references as keys instead of raw pointers for PreloadHashKey. r=mayhemer

Feels a bit more natural for the callers this way. This should have no
behavior change.

Differential Revision: https://phabricator.services.mozilla.com/D79831
This commit is contained in:
Emilio Cobos Álvarez 2020-06-18 14:06:34 +00:00
Родитель 939c7c466b
Коммит 6bb08bd490
15 изменённых файлов: 66 добавлений и 69 удалений

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

@ -11415,7 +11415,7 @@ void Document::MaybePreLoadImage(nsIURI* aUri,
PreloadHashKey key = PreloadHashKey::CreateAsImage(
aUri, NodePrincipal(),
dom::Element::StringToCORSMode(aCrossOriginAttr));
if (!mPreloadService.PreloadExists(&key)) {
if (!mPreloadService.PreloadExists(key)) {
PreLoadImage(aUri, aCrossOriginAttr, aReferrerPolicy, aIsImgSet,
aLinkPreload);
}

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

@ -411,7 +411,7 @@ already_AddRefed<PreloaderBase> FetchDriver::FindPreload(nsIURI* aURI) {
// OK, this request can be satisfied by a preloaded response, try to find one.
auto preloadKey = PreloadHashKey::CreateAsFetch(aURI, cors);
return mDocument->Preloads().LookupPreload(&preloadKey);
return mDocument->Preloads().LookupPreload(preloadKey);
}
void FetchDriver::UpdateReferrerInfoFromNewChannel(nsIChannel* aChannel) {

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

@ -1523,7 +1523,7 @@ nsresult ScriptLoader::StartLoad(ScriptLoadRequest* aRequest) {
auto key = PreloadHashKey::CreateAsScript(
aRequest->mURI, aRequest->CORSMode(), aRequest->mKind);
aRequest->NotifyOpen(&key, channel, mDocument,
aRequest->NotifyOpen(key, channel, mDocument,
aRequest->IsLinkPreloadScript());
rv = channel->AsyncOpen(loader);

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

@ -2744,7 +2744,7 @@ already_AddRefed<PreloaderBase> XMLHttpRequestMainThread::FindPreload() {
nsCOMPtr<nsIReferrerInfo> referrerInfo =
ReferrerInfo::CreateForFetch(mPrincipal, doc);
auto key = PreloadHashKey::CreateAsFetch(mRequestURL, cors);
RefPtr<PreloaderBase> preload = doc->Preloads().LookupPreload(&key);
RefPtr<PreloaderBase> preload = doc->Preloads().LookupPreload(key);
if (!preload) {
return nullptr;
}

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

@ -1689,7 +1689,7 @@ bool imgLoader::ValidateRequestWithNewChannel(
proxy->PrioritizeAsPreload();
auto preloadKey = PreloadHashKey::CreateAsImage(
aURI, aTriggeringPrincipal, ConvertToCORSMode(aCORSMode));
proxy->NotifyOpen(&preloadKey, aLoadingDocument, true);
proxy->NotifyOpen(preloadKey, aLoadingDocument, true);
}
// Attach the proxy without notifying
@ -1756,7 +1756,7 @@ bool imgLoader::ValidateRequestWithNewChannel(
req->PrioritizeAsPreload();
auto preloadKey = PreloadHashKey::CreateAsImage(
aURI, aTriggeringPrincipal, ConvertToCORSMode(aCORSMode));
req->NotifyOpen(&preloadKey, aLoadingDocument, true);
req->NotifyOpen(preloadKey, aLoadingDocument, true);
}
// Add the proxy without notifying
@ -2220,7 +2220,7 @@ nsresult imgLoader::LoadImage(
auto key = PreloadHashKey::CreateAsImage(aURI, aTriggeringPrincipal,
ConvertToCORSMode(corsmode));
if (RefPtr<PreloaderBase> preload =
aLoadingDocument->Preloads().LookupPreload(&key)) {
aLoadingDocument->Preloads().LookupPreload(key)) {
RefPtr<imgRequestProxy> proxy = do_QueryObject(preload);
MOZ_ASSERT(proxy);
@ -2449,7 +2449,7 @@ nsresult imgLoader::LoadImage(
proxy->PrioritizeAsPreload();
auto preloadKey = PreloadHashKey::CreateAsImage(
aURI, aTriggeringPrincipal, ConvertToCORSMode(corsmode));
proxy->NotifyOpen(&preloadKey, aLoadingDocument, true);
proxy->NotifyOpen(preloadKey, aLoadingDocument, true);
}
// Note that it's OK to add here even if the request is done. If it is,

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

@ -577,7 +577,7 @@ nsresult FontFaceSet::StartLoad(gfxUserFontEntry* aUserFontEntry,
auto preloadKey =
PreloadHashKey::CreateAsFont(aFontFaceSrc->mURI->get(), CORS_ANONYMOUS);
RefPtr<PreloaderBase> preload =
mDocument->Preloads().LookupPreload(&preloadKey);
mDocument->Preloads().LookupPreload(preloadKey);
if (preload) {
fontLoader = new nsFontFaceLoader(aUserFontEntry, aFontFaceSrc->mURI->get(),

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

@ -1351,7 +1351,7 @@ nsresult Loader::LoadSheet(SheetLoadData& aLoadData, SheetState aSheetState) {
}
auto preloadKey = PreloadHashKey::CreateAsStyle(aLoadData);
streamLoader->NotifyOpen(&preloadKey, channel, mDocument,
streamLoader->NotifyOpen(preloadKey, channel, mDocument,
aLoadData.mIsPreload == IsPreload::FromLink);
rv = channel->AsyncOpen(streamLoader);

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

@ -32,7 +32,7 @@ FetchPreloader::FetchPreloader()
FetchPreloader::FetchPreloader(nsContentPolicyType aContentPolicyType)
: mContentPolicyType(aContentPolicyType) {}
nsresult FetchPreloader::OpenChannel(PreloadHashKey* aKey, nsIURI* aURI,
nsresult FetchPreloader::OpenChannel(const PreloadHashKey& aKey, nsIURI* aURI,
const CORSMode aCORSMode,
const dom::ReferrerPolicy& aReferrerPolicy,
dom::Document* aDocument) {

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

@ -24,7 +24,7 @@ class FetchPreloader : public PreloaderBase, public nsIStreamListener {
NS_DECL_NSISTREAMLISTENER
FetchPreloader();
nsresult OpenChannel(PreloadHashKey* aKey, nsIURI* aURI,
nsresult OpenChannel(const PreloadHashKey& aKey, nsIURI* aURI,
const CORSMode aCORSMode,
const dom::ReferrerPolicy& aReferrerPolicy,
dom::Document* aDocument);

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

@ -32,8 +32,8 @@ class PreloadHashKey : public nsURIHashKey {
public:
enum class ResourceType : uint8_t { NONE, SCRIPT, STYLE, IMAGE, FONT, FETCH };
typedef PreloadHashKey* KeyType;
typedef const PreloadHashKey* KeyTypePointer;
using KeyType = const PreloadHashKey&;
using KeyTypePointer = const PreloadHashKey*;
PreloadHashKey() = default;
PreloadHashKey(const nsIURI* aKey, ResourceType aAs);
@ -67,9 +67,9 @@ class PreloadHashKey : public nsURIHashKey {
// Construct key for "font"
static PreloadHashKey CreateAsFont(nsIURI* aURI, CORSMode aCORSMode);
KeyType GetKey() const { return const_cast<PreloadHashKey*>(this); }
KeyType GetKey() const { return *this; }
KeyTypePointer GetKeyPointer() const { return this; }
static KeyTypePointer KeyToPointer(KeyType aKey) { return aKey; }
static KeyTypePointer KeyToPointer(KeyType aKey) { return &aKey; }
bool KeyEquals(KeyTypePointer aOther) const;
static PLDHashNumber HashKey(KeyTypePointer aKey);

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

@ -16,7 +16,7 @@
namespace mozilla {
bool PreloadService::RegisterPreload(PreloadHashKey* aKey,
bool PreloadService::RegisterPreload(const PreloadHashKey& aKey,
PreloaderBase* aPreload) {
if (PreloadExists(aKey)) {
return false;
@ -26,20 +26,20 @@ bool PreloadService::RegisterPreload(PreloadHashKey* aKey,
return true;
}
void PreloadService::DeregisterPreload(PreloadHashKey* aKey) {
void PreloadService::DeregisterPreload(const PreloadHashKey& aKey) {
mPreloads.Remove(aKey);
}
void PreloadService::ClearAllPreloads() { mPreloads.Clear(); }
bool PreloadService::PreloadExists(PreloadHashKey* aKey) {
bool PreloadService::PreloadExists(const PreloadHashKey& aKey) {
bool found;
mPreloads.GetWeak(aKey, &found);
return found;
}
already_AddRefed<PreloaderBase> PreloadService::LookupPreload(
PreloadHashKey* aKey) const {
const PreloadHashKey& aKey) const {
return mPreloads.Get(aKey);
}
@ -155,28 +155,24 @@ already_AddRefed<PreloaderBase> PreloadService::PreloadOrCoalesce(
return nullptr;
}
RefPtr<PreloaderBase> preload = LookupPreload(&preloadKey);
if (!preload) {
if (aAs.LowerCaseEqualsASCII("script")) {
PreloadScript(uri, aType, aCharset, aCORS, aReferrerPolicy, aIntegrity,
true /* isInHead - TODO */);
} else if (aAs.LowerCaseEqualsASCII("style")) {
PreloadStyle(uri, aCharset, aCORS, aReferrerPolicy, aIntegrity);
} else if (aAs.LowerCaseEqualsASCII("image")) {
PreloadImage(uri, aCORS, aReferrerPolicy, isImgSet);
} else if (aAs.LowerCaseEqualsASCII("font")) {
PreloadFont(uri, aCORS, aReferrerPolicy);
} else if (aAs.LowerCaseEqualsASCII("fetch")) {
PreloadFetch(uri, aCORS, aReferrerPolicy);
}
preload = LookupPreload(&preloadKey);
if (!preload) {
return nullptr;
}
if (RefPtr<PreloaderBase> preload = LookupPreload(preloadKey)) {
return preload.forget();
}
return preload.forget();
if (aAs.LowerCaseEqualsASCII("script")) {
PreloadScript(uri, aType, aCharset, aCORS, aReferrerPolicy, aIntegrity,
true /* isInHead - TODO */);
} else if (aAs.LowerCaseEqualsASCII("style")) {
PreloadStyle(uri, aCharset, aCORS, aReferrerPolicy, aIntegrity);
} else if (aAs.LowerCaseEqualsASCII("image")) {
PreloadImage(uri, aCORS, aReferrerPolicy, isImgSet);
} else if (aAs.LowerCaseEqualsASCII("font")) {
PreloadFont(uri, aCORS, aReferrerPolicy);
} else if (aAs.LowerCaseEqualsASCII("fetch")) {
PreloadFetch(uri, aCORS, aReferrerPolicy);
}
return LookupPreload(preloadKey);
}
void PreloadService::PreloadScript(nsIURI* aURI, const nsAString& aType,
@ -217,7 +213,7 @@ void PreloadService::PreloadFont(nsIURI* aURI, const nsAString& aCrossOrigin,
RefPtr<FontPreloader> preloader = new FontPreloader();
dom::ReferrerPolicy referrerPolicy = PreloadReferrerPolicy(aReferrerPolicy);
preloader->OpenChannel(&key, aURI, cors, referrerPolicy, mDocument);
preloader->OpenChannel(key, aURI, cors, referrerPolicy, mDocument);
}
void PreloadService::PreloadFetch(nsIURI* aURI, const nsAString& aCrossOrigin,
@ -230,7 +226,7 @@ void PreloadService::PreloadFetch(nsIURI* aURI, const nsAString& aCrossOrigin,
RefPtr<FetchPreloader> preloader = new FetchPreloader();
dom::ReferrerPolicy referrerPolicy = PreloadReferrerPolicy(aReferrerPolicy);
preloader->OpenChannel(&key, aURI, cors, referrerPolicy, mDocument);
preloader->OpenChannel(key, aURI, cors, referrerPolicy, mDocument);
}
// static

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

@ -39,21 +39,22 @@ class PreloadService {
//
// Returns false and does nothing if a preload is already registered under
// this key, true otherwise.
bool RegisterPreload(PreloadHashKey* aKey, PreloaderBase* aPreload);
bool RegisterPreload(const PreloadHashKey& aKey, PreloaderBase* aPreload);
// Called when the load is about to be cancelled. Exact behavior is to be
// determined yet.
void DeregisterPreload(PreloadHashKey* aKey);
void DeregisterPreload(const PreloadHashKey& aKey);
// Called when the scope is to go away.
void ClearAllPreloads();
// True when there is a preload registered under the key.
bool PreloadExists(PreloadHashKey* aKey);
bool PreloadExists(const PreloadHashKey& aKey);
// Returns an existing preload under the key or null, when there is none
// registered under the key.
already_AddRefed<PreloaderBase> LookupPreload(PreloadHashKey* aKey) const;
already_AddRefed<PreloaderBase> LookupPreload(
const PreloadHashKey& aKey) const;
void SetSpeculationBase(nsIURI* aURI) { mSpeculationBaseURI = aURI; }
already_AddRefed<nsIURI> GetPreloadURI(const nsAString& aURL);

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

@ -91,8 +91,8 @@ void PreloaderBase::AddLoadBackgroundFlag(nsIChannel* aChannel) {
aChannel->SetLoadFlags(loadFlags | nsIRequest::LOAD_BACKGROUND);
}
void PreloaderBase::NotifyOpen(PreloadHashKey* aKey, dom::Document* aDocument,
bool aIsPreload) {
void PreloaderBase::NotifyOpen(const PreloadHashKey& aKey,
dom::Document* aDocument, bool aIsPreload) {
if (aDocument && !aDocument->Preloads().RegisterPreload(aKey, this)) {
// This means there is already a preload registered under this key in this
// document. We only allow replacement when this is a regular load.
@ -102,11 +102,11 @@ void PreloaderBase::NotifyOpen(PreloadHashKey* aKey, dom::Document* aDocument,
aDocument->Preloads().RegisterPreload(aKey, this);
}
mKey = *aKey;
mKey = aKey;
mIsUsed = !aIsPreload;
}
void PreloaderBase::NotifyOpen(PreloadHashKey* aKey, nsIChannel* aChannel,
void PreloaderBase::NotifyOpen(const PreloadHashKey& aKey, nsIChannel* aChannel,
dom::Document* aDocument, bool aIsPreload) {
NotifyOpen(aKey, aDocument, aIsPreload);
mChannel = aChannel;
@ -152,7 +152,7 @@ void PreloaderBase::NotifyUsage(LoadBackground aLoadBackground) {
void PreloaderBase::RemoveSelf(dom::Document* aDocument) {
if (aDocument) {
aDocument->Preloads().DeregisterPreload(&mKey);
aDocument->Preloads().DeregisterPreload(mKey);
}
}

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

@ -47,9 +47,9 @@ class PreloaderBase : public SupportsWeakPtr<PreloaderBase>,
// Called by resource loaders to register this preload in the document's
// preload service to provide coalescing, and access to the preload when it
// should be used for an actual load.
void NotifyOpen(PreloadHashKey* aKey, dom::Document* aDocument,
void NotifyOpen(const PreloadHashKey& aKey, dom::Document* aDocument,
bool aIsPreload);
void NotifyOpen(PreloadHashKey* aKey, nsIChannel* aChannel,
void NotifyOpen(const PreloadHashKey& aKey, nsIChannel* aChannel,
dom::Document* aDocument, bool aIsPreload);
// Called when the load is about to be started all over again and thus this

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

@ -272,7 +272,7 @@ TEST(TestFetchPreloader, CacheNoneBeforeConsume)
NS_NewXMLDocument(getter_AddRefs(doc));
EXPECT_TRUE(NS_SUCCEEDED(
preloader->OpenChannel(&key, uri, mozilla::CORS_NONE,
preloader->OpenChannel(key, uri, mozilla::CORS_NONE,
mozilla::dom::ReferrerPolicy::_empty, doc)));
RefPtr<FakeListener> listener = new FakeListener();
@ -310,7 +310,7 @@ TEST(TestFetchPreloader, CacheStartBeforeConsume)
NS_NewXMLDocument(getter_AddRefs(doc));
EXPECT_TRUE(NS_SUCCEEDED(
preloader->OpenChannel(&key, uri, mozilla::CORS_NONE,
preloader->OpenChannel(key, uri, mozilla::CORS_NONE,
mozilla::dom::ReferrerPolicy::_empty, doc)));
EXPECT_TRUE(NS_SUCCEEDED(channel->Start()));
@ -350,7 +350,7 @@ TEST(TestFetchPreloader, CachePartOfDataBeforeConsume)
NS_NewXMLDocument(getter_AddRefs(doc));
EXPECT_TRUE(NS_SUCCEEDED(
preloader->OpenChannel(&key, uri, mozilla::CORS_NONE,
preloader->OpenChannel(key, uri, mozilla::CORS_NONE,
mozilla::dom::ReferrerPolicy::_empty, doc)));
EXPECT_TRUE(NS_SUCCEEDED(channel->Start()));
@ -390,7 +390,7 @@ TEST(TestFetchPreloader, CacheAllDataBeforeConsume)
NS_NewXMLDocument(getter_AddRefs(doc));
EXPECT_TRUE(NS_SUCCEEDED(
preloader->OpenChannel(&key, uri, mozilla::CORS_NONE,
preloader->OpenChannel(key, uri, mozilla::CORS_NONE,
mozilla::dom::ReferrerPolicy::_empty, doc)));
EXPECT_TRUE(NS_SUCCEEDED(channel->Start()));
@ -430,7 +430,7 @@ TEST(TestFetchPreloader, CacheAllBeforeConsume)
NS_NewXMLDocument(getter_AddRefs(doc));
EXPECT_TRUE(NS_SUCCEEDED(
preloader->OpenChannel(&key, uri, mozilla::CORS_NONE,
preloader->OpenChannel(key, uri, mozilla::CORS_NONE,
mozilla::dom::ReferrerPolicy::_empty, doc)));
EXPECT_TRUE(NS_SUCCEEDED(channel->Start()));
@ -469,7 +469,7 @@ TEST(TestFetchPreloader, CacheAllBeforeConsumeWithChannelError)
NS_NewXMLDocument(getter_AddRefs(doc));
EXPECT_TRUE(NS_SUCCEEDED(
preloader->OpenChannel(&key, uri, mozilla::CORS_NONE,
preloader->OpenChannel(key, uri, mozilla::CORS_NONE,
mozilla::dom::ReferrerPolicy::_empty, doc)));
EXPECT_TRUE(NS_SUCCEEDED(channel->Start()));
@ -508,7 +508,7 @@ TEST(TestFetchPreloader, CacheAllBeforeConsumeWithChannelCancel)
NS_NewXMLDocument(getter_AddRefs(doc));
EXPECT_TRUE(NS_SUCCEEDED(
preloader->OpenChannel(&key, uri, mozilla::CORS_NONE,
preloader->OpenChannel(key, uri, mozilla::CORS_NONE,
mozilla::dom::ReferrerPolicy::_empty, doc)));
EXPECT_TRUE(NS_SUCCEEDED(channel->Start()));
@ -551,7 +551,7 @@ TEST(TestFetchPreloader, CacheAllBeforeConsumeThrowFromOnStartRequest)
NS_NewXMLDocument(getter_AddRefs(doc));
EXPECT_TRUE(NS_SUCCEEDED(
preloader->OpenChannel(&key, uri, mozilla::CORS_NONE,
preloader->OpenChannel(key, uri, mozilla::CORS_NONE,
mozilla::dom::ReferrerPolicy::_empty, doc)));
EXPECT_TRUE(NS_SUCCEEDED(channel->Start()));
@ -591,7 +591,7 @@ TEST(TestFetchPreloader, CacheAllBeforeConsumeThrowFromOnDataAvailable)
NS_NewXMLDocument(getter_AddRefs(doc));
EXPECT_TRUE(NS_SUCCEEDED(
preloader->OpenChannel(&key, uri, mozilla::CORS_NONE,
preloader->OpenChannel(key, uri, mozilla::CORS_NONE,
mozilla::dom::ReferrerPolicy::_empty, doc)));
EXPECT_TRUE(NS_SUCCEEDED(channel->Start()));
@ -631,7 +631,7 @@ TEST(TestFetchPreloader, CacheAllBeforeConsumeThrowFromOnStopRequest)
NS_NewXMLDocument(getter_AddRefs(doc));
EXPECT_TRUE(NS_SUCCEEDED(
preloader->OpenChannel(&key, uri, mozilla::CORS_NONE,
preloader->OpenChannel(key, uri, mozilla::CORS_NONE,
mozilla::dom::ReferrerPolicy::_empty, doc)));
EXPECT_TRUE(NS_SUCCEEDED(channel->Start()));
@ -673,7 +673,7 @@ TEST(TestFetchPreloader, CacheAllBeforeConsumeCancelInOnStartRequest)
NS_NewXMLDocument(getter_AddRefs(doc));
EXPECT_TRUE(NS_SUCCEEDED(
preloader->OpenChannel(&key, uri, mozilla::CORS_NONE,
preloader->OpenChannel(key, uri, mozilla::CORS_NONE,
mozilla::dom::ReferrerPolicy::_empty, doc)));
EXPECT_TRUE(NS_SUCCEEDED(channel->Start()));
@ -716,7 +716,7 @@ TEST(TestFetchPreloader, CacheAllBeforeConsumeCancelInOnDataAvailable)
NS_NewXMLDocument(getter_AddRefs(doc));
EXPECT_TRUE(NS_SUCCEEDED(
preloader->OpenChannel(&key, uri, mozilla::CORS_NONE,
preloader->OpenChannel(key, uri, mozilla::CORS_NONE,
mozilla::dom::ReferrerPolicy::_empty, doc)));
EXPECT_TRUE(NS_SUCCEEDED(channel->Start()));
@ -760,7 +760,7 @@ TEST(TestFetchPreloader, CachePartlyBeforeConsumeCancelInOnDataAvailable)
NS_NewXMLDocument(getter_AddRefs(doc));
EXPECT_TRUE(NS_SUCCEEDED(
preloader->OpenChannel(&key, uri, mozilla::CORS_NONE,
preloader->OpenChannel(key, uri, mozilla::CORS_NONE,
mozilla::dom::ReferrerPolicy::_empty, doc)));
EXPECT_TRUE(NS_SUCCEEDED(channel->Start()));
@ -801,7 +801,7 @@ TEST(TestFetchPreloader, CachePartlyBeforeConsumeCancelInOnStartRequestAndRace)
NS_NewXMLDocument(getter_AddRefs(doc));
EXPECT_TRUE(NS_SUCCEEDED(
preloader->OpenChannel(&key, uri, mozilla::CORS_NONE,
preloader->OpenChannel(key, uri, mozilla::CORS_NONE,
mozilla::dom::ReferrerPolicy::_empty, doc)));
EXPECT_TRUE(NS_SUCCEEDED(channel->Start()));
@ -850,7 +850,7 @@ TEST(TestFetchPreloader, CachePartlyBeforeConsumeCancelInOnDataAvailableAndRace)
NS_NewXMLDocument(getter_AddRefs(doc));
EXPECT_TRUE(NS_SUCCEEDED(
preloader->OpenChannel(&key, uri, mozilla::CORS_NONE,
preloader->OpenChannel(key, uri, mozilla::CORS_NONE,
mozilla::dom::ReferrerPolicy::_empty, doc)));
EXPECT_TRUE(NS_SUCCEEDED(channel->Start()));
@ -899,7 +899,7 @@ TEST(TestFetchPreloader, CachePartlyBeforeConsumeThrowFromOnStartRequestAndRace)
NS_NewXMLDocument(getter_AddRefs(doc));
EXPECT_TRUE(NS_SUCCEEDED(
preloader->OpenChannel(&key, uri, mozilla::CORS_NONE,
preloader->OpenChannel(key, uri, mozilla::CORS_NONE,
mozilla::dom::ReferrerPolicy::_empty, doc)));
EXPECT_TRUE(NS_SUCCEEDED(channel->Start()));