Bug 1642591 - Don't make referrer policy a cache miss for sub-resource {pre,}loads. r=mayhemer,tnikkel

For preload we're already effectively not using it, I think, due to
bug 1642325.

For images, this matches the spec, see earlier comments in this bug and
https://bugzilla.mozilla.org/show_bug.cgi?id=1174921#c17.  I think it
makes sense for other sub-resources to align as well.

Differential Revision: https://phabricator.services.mozilla.com/D79812
This commit is contained in:
Emilio Cobos Álvarez 2020-06-18 14:06:20 +00:00
Родитель 17e54eff48
Коммит 939c7c466b
13 изменённых файлов: 93 добавлений и 210 удалений

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

@ -11413,8 +11413,8 @@ void Document::MaybePreLoadImage(nsIURI* aUri,
// Check if the image was already preloaded in this document to avoid
// duplicate preloading.
PreloadHashKey key = PreloadHashKey::CreateAsImage(
aUri, NodePrincipal(), dom::Element::StringToCORSMode(aCrossOriginAttr),
aReferrerPolicy);
aUri, NodePrincipal(),
dom::Element::StringToCORSMode(aCrossOriginAttr));
if (!mPreloadService.PreloadExists(&key)) {
PreLoadImage(aUri, aCrossOriginAttr, aReferrerPolicy, aIsImgSet,
aLinkPreload);

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

@ -410,10 +410,7 @@ already_AddRefed<PreloaderBase> FetchDriver::FindPreload(nsIURI* aURI) {
// OK, this request can be satisfied by a preloaded response, try to find one.
// TODO - check if we need to perform step 5 and 6 before using
// mRequest->ReferrerPolicy_() here.
auto preloadKey =
PreloadHashKey::CreateAsFetch(aURI, cors, mRequest->ReferrerPolicy_());
auto preloadKey = PreloadHashKey::CreateAsFetch(aURI, cors);
return mDocument->Preloads().LookupPreload(&preloadKey);
}

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

@ -1522,8 +1522,7 @@ nsresult ScriptLoader::StartLoad(ScriptLoadRequest* aRequest) {
NS_ENSURE_SUCCESS(rv, rv);
auto key = PreloadHashKey::CreateAsScript(
aRequest->mURI, aRequest->CORSMode(), aRequest->mKind,
aRequest->ReferrerPolicy());
aRequest->mURI, aRequest->CORSMode(), aRequest->mKind);
aRequest->NotifyOpen(&key, channel, mDocument,
aRequest->IsLinkPreloadScript());
@ -1976,11 +1975,9 @@ ScriptLoadRequest* ScriptLoader::LookupPreloadRequest(
// we have now.
nsAutoString elementCharset;
aElement->GetScriptCharset(elementCharset);
ReferrerPolicy referrerPolicy = GetReferrerPolicy(aElement);
if (!elementCharset.Equals(preloadCharset) ||
aElement->GetCORSMode() != request->CORSMode() ||
referrerPolicy != request->ReferrerPolicy() ||
aScriptKind != request->mKind) {
// Drop the preload.
request->Cancel();

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

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

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

@ -748,30 +748,7 @@ static bool ValidateSecurityInfo(imgRequest* request, bool forcePrincipalCheck,
int32_t corsmode,
nsIPrincipal* triggeringPrincipal,
Document* aLoadingDocument,
nsContentPolicyType aPolicyType,
nsIReferrerInfo* aReferrerInfo) {
// If the referrer policy doesn't match, we can't use this request.
// XXX: Note that we only validate referrer policy, not referrerInfo object.
// We should do with referrerInfo object, but it will cause us to use more
// resources in the common case (the same policies but different original
// referrers).
// XXX: this will return false if an image has different referrer attributes,
// i.e. we currently don't use the cached image but reload the image with
// the new referrer policy bug 1174921
ReferrerPolicy referrerPolicy = ReferrerPolicy::_empty;
if (aReferrerInfo) {
referrerPolicy = aReferrerInfo->ReferrerPolicy();
}
ReferrerPolicy requestReferrerPolicy = ReferrerPolicy::_empty;
if (request->GetReferrerInfo()) {
requestReferrerPolicy = request->GetReferrerInfo()->ReferrerPolicy();
}
if (referrerPolicy != requestReferrerPolicy) {
return false;
}
nsContentPolicyType aPolicyType) {
// If the entry's CORS mode doesn't match, or the CORS mode matches but the
// document principal isn't the same, we can't use this request.
if (request->GetCORSMode() != corsmode) {
@ -1709,11 +1686,9 @@ bool imgLoader::ValidateRequestWithNewChannel(
if (aLinkPreload) {
MOZ_ASSERT(aLoadingDocument);
MOZ_ASSERT(aReferrerInfo);
proxy->PrioritizeAsPreload();
auto preloadKey = PreloadHashKey::CreateAsImage(
aURI, aTriggeringPrincipal, ConvertToCORSMode(aCORSMode),
aReferrerInfo->ReferrerPolicy());
aURI, aTriggeringPrincipal, ConvertToCORSMode(aCORSMode));
proxy->NotifyOpen(&preloadKey, aLoadingDocument, true);
}
@ -1778,11 +1753,9 @@ bool imgLoader::ValidateRequestWithNewChannel(
if (aLinkPreload) {
MOZ_ASSERT(aLoadingDocument);
MOZ_ASSERT(aReferrerInfo);
req->PrioritizeAsPreload();
auto preloadKey = PreloadHashKey::CreateAsImage(
aURI, aTriggeringPrincipal, ConvertToCORSMode(aCORSMode),
aReferrerInfo->ReferrerPolicy());
aURI, aTriggeringPrincipal, ConvertToCORSMode(aCORSMode));
req->NotifyOpen(&preloadKey, aLoadingDocument, true);
}
@ -1853,7 +1826,7 @@ bool imgLoader::ValidateEntry(
if (!ValidateSecurityInfo(request, aEntry->ForcePrincipalCheck(), aCORSMode,
aTriggeringPrincipal, aLoadingDocument,
aLoadPolicyType, aReferrerInfo)) {
aLoadPolicyType)) {
return false;
}
@ -2244,10 +2217,8 @@ nsresult imgLoader::LoadImage(
// Look in the preloaded images of loading document first.
if (StaticPrefs::network_preload() && !aLinkPreload && aLoadingDocument) {
auto key = PreloadHashKey::CreateAsImage(
aURI, aTriggeringPrincipal, ConvertToCORSMode(corsmode),
aReferrerInfo ? aReferrerInfo->ReferrerPolicy()
: ReferrerPolicy::_empty);
auto key = PreloadHashKey::CreateAsImage(aURI, aTriggeringPrincipal,
ConvertToCORSMode(corsmode));
if (RefPtr<PreloaderBase> preload =
aLoadingDocument->Preloads().LookupPreload(&key)) {
RefPtr<imgRequestProxy> proxy = do_QueryObject(preload);
@ -2475,11 +2446,9 @@ nsresult imgLoader::LoadImage(
if (aLinkPreload) {
MOZ_ASSERT(aLoadingDocument);
MOZ_ASSERT(aReferrerInfo);
proxy->PrioritizeAsPreload();
auto preloadKey = PreloadHashKey::CreateAsImage(
aURI, aTriggeringPrincipal, ConvertToCORSMode(corsmode),
aReferrerInfo->ReferrerPolicy());
aURI, aTriggeringPrincipal, ConvertToCORSMode(corsmode));
proxy->NotifyOpen(&preloadKey, aLoadingDocument, true);
}

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

@ -574,9 +574,8 @@ nsresult FontFaceSet::StartLoad(gfxUserFontEntry* aUserFontEntry,
nsCOMPtr<nsIStreamLoader> streamLoader;
RefPtr<nsFontFaceLoader> fontLoader;
auto preloadKey = PreloadHashKey::CreateAsFont(
aFontFaceSrc->mURI->get(), CORS_ANONYMOUS,
aFontFaceSrc->mReferrerInfo->ReferrerPolicy());
auto preloadKey =
PreloadHashKey::CreateAsFont(aFontFaceSrc->mURI->get(), CORS_ANONYMOUS);
RefPtr<PreloaderBase> preload =
mDocument->Preloads().LookupPreload(&preloadKey);

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

@ -135,7 +135,6 @@ SheetLoadDataHashKey::SheetLoadDataHashKey(const css::SheetLoadData& aLoadData)
: mURI(aLoadData.mURI),
mPrincipal(aLoadData.mTriggeringPrincipal),
mLoaderPrincipal(aLoadData.mLoader->LoaderPrincipal()),
mReferrerInfo(aLoadData.ReferrerInfo()),
mEncodingGuess(aLoadData.mGuessedEncoding),
mCORSMode(aLoadData.mSheet->GetCORSMode()),
mParsingMode(aLoadData.mSheet->ParsingMode()),
@ -854,9 +853,8 @@ void Loader::DidHitCompleteSheetCache(const SheetLoadDataHashKey& aKey,
std::tuple<RefPtr<StyleSheet>, Loader::SheetState> Loader::CreateSheet(
nsIURI* aURI, nsIContent* aLinkingContent,
nsIPrincipal* aTriggeringPrincipal, css::SheetParsingMode aParsingMode,
CORSMode aCORSMode, nsIReferrerInfo* aLoadingReferrerInfo,
const Encoding* aPreloadOrParentDataEncoding, const nsAString& aIntegrity,
bool aSyncLoad, IsPreload aIsPreload) {
CORSMode aCORSMode, const Encoding* aPreloadOrParentDataEncoding,
const nsAString& aIntegrity, bool aSyncLoad, IsPreload aIsPreload) {
MOZ_ASSERT(aURI, "This path is not taken for inline stylesheets");
LOG(("css::Loader::CreateSheet(%s)", aURI->GetSpecOrDefault().get()));
@ -873,11 +871,11 @@ std::tuple<RefPtr<StyleSheet>, Loader::SheetState> Loader::CreateSheet(
}
if (mSheets) {
SheetLoadDataHashKey key(
aURI, aTriggeringPrincipal, LoaderPrincipal(), aLoadingReferrerInfo,
GetFallbackEncoding(*this, aLinkingContent,
aPreloadOrParentDataEncoding),
aCORSMode, aParsingMode, mCompatMode, sriMetadata, aIsPreload);
SheetLoadDataHashKey key(aURI, aTriggeringPrincipal, LoaderPrincipal(),
GetFallbackEncoding(*this, aLinkingContent,
aPreloadOrParentDataEncoding),
aCORSMode, aParsingMode, mCompatMode, sriMetadata,
aIsPreload);
auto cacheResult = mSheets->Lookup(*this, key, aSyncLoad);
if (const auto& [styleSheet, sheetState] = cacheResult; styleSheet) {
LOG((" Hit cache with state: %s", gStateStrings[size_t(sheetState)]));
@ -1838,8 +1836,7 @@ nsresult Loader::LoadChildSheet(StyleSheet& aParentSheet,
// For now, use CORS_NONE for child sheets
std::tie(sheet, state) =
CreateSheet(aURL, nullptr, principal, aParentSheet.ParsingMode(),
CORS_NONE, aParentSheet.GetReferrerInfo(),
aParentData ? aParentData->mEncoding : nullptr,
CORS_NONE, aParentData ? aParentData->mEncoding : nullptr,
EmptyString(), // integrity is only checked on main sheet
aParentData && aParentData->mSyncLoad, IsPreload::No);
PrepareSheet(*sheet, EmptyString(), EmptyString(), aMedia, IsAlternate::No,
@ -1931,9 +1928,9 @@ Result<RefPtr<StyleSheet>, nsresult> Loader::InternalLoadNonDocumentSheet(
}
bool syncLoad = !aObserver;
auto [sheet, state] = CreateSheet(
aURL, nullptr, triggeringPrincipal, aParsingMode, aCORSMode,
aReferrerInfo, aPreloadEncoding, aIntegrity, syncLoad, aIsPreload);
auto [sheet, state] =
CreateSheet(aURL, nullptr, triggeringPrincipal, aParsingMode, aCORSMode,
aPreloadEncoding, aIntegrity, syncLoad, aIsPreload);
PrepareSheet(*sheet, EmptyString(), EmptyString(), nullptr, IsAlternate::No,
IsExplicitlyEnabled::No);

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

@ -63,7 +63,6 @@ class SheetLoadDataHashKey : public PLDHashEntryHdr {
: mURI(aKey->mURI),
mPrincipal(aKey->mPrincipal),
mLoaderPrincipal(aKey->mLoaderPrincipal),
mReferrerInfo(aKey->mReferrerInfo),
mEncodingGuess(aKey->mEncodingGuess),
mCORSMode(aKey->mCORSMode),
mParsingMode(aKey->mParsingMode),
@ -75,7 +74,6 @@ class SheetLoadDataHashKey : public PLDHashEntryHdr {
SheetLoadDataHashKey(nsIURI* aURI, nsIPrincipal* aPrincipal,
nsIPrincipal* aLoaderPrincipal,
nsIReferrerInfo* aReferrerInfo,
NotNull<const Encoding*> aEncodingGuess,
CORSMode aCORSMode, css::SheetParsingMode aParsingMode,
nsCompatibility aCompatMode,
@ -84,7 +82,6 @@ class SheetLoadDataHashKey : public PLDHashEntryHdr {
: mURI(aURI),
mPrincipal(aPrincipal),
mLoaderPrincipal(aLoaderPrincipal),
mReferrerInfo(aReferrerInfo),
mEncodingGuess(aEncodingGuess),
mCORSMode(aCORSMode),
mParsingMode(aParsingMode),
@ -101,7 +98,6 @@ class SheetLoadDataHashKey : public PLDHashEntryHdr {
: mURI(std::move(toMove.mURI)),
mPrincipal(std::move(toMove.mPrincipal)),
mLoaderPrincipal(std::move(toMove.mLoaderPrincipal)),
mReferrerInfo(std::move(toMove.mReferrerInfo)),
mEncodingGuess(std::move(toMove.mEncodingGuess)),
mCORSMode(std::move(toMove.mCORSMode)),
mParsingMode(std::move(toMove.mParsingMode)),
@ -158,13 +154,6 @@ class SheetLoadDataHashKey : public PLDHashEntryHdr {
return false;
}
// FIXME: Should we _really_ miss the cache here for different referrer
// policies? Missing the cache for different referrers would be sad.
if (mReferrerInfo->ReferrerPolicy() !=
aKey.mReferrerInfo->ReferrerPolicy()) {
return false;
}
// Consuming stylesheet tags must never coalesce to <link preload> initiated
// speculative loads with a weaker SRI hash or its different value. This
// check makes sure that regular loads will never find such a weaker preload
@ -206,7 +195,6 @@ class SheetLoadDataHashKey : public PLDHashEntryHdr {
const nsCOMPtr<nsIURI> mURI;
const nsCOMPtr<nsIPrincipal> mPrincipal;
const nsCOMPtr<nsIPrincipal> mLoaderPrincipal;
const nsCOMPtr<nsIReferrerInfo> mReferrerInfo;
// The encoding guess is the encoding the sheet would get if the request
// didn't have any encoding information like @charset or a Content-Encoding
// header.
@ -533,7 +521,7 @@ class Loader final {
? aInfo.mTriggeringPrincipal.get()
: LoaderPrincipal();
return CreateSheet(aInfo.mURI, aInfo.mContent, triggeringPrincipal,
aParsingMode, aInfo.mCORSMode, aInfo.mReferrerInfo,
aParsingMode, aInfo.mCORSMode,
/* aPreloadOrParentDataEncoding = */ nullptr,
aInfo.mIntegrity, aSyncLoad, aIsPreload);
}
@ -544,7 +532,6 @@ class Loader final {
std::tuple<RefPtr<StyleSheet>, SheetState> CreateSheet(
nsIURI* aURI, nsIContent* aLinkingContent,
nsIPrincipal* aTriggeringPrincipal, css::SheetParsingMode, CORSMode,
nsIReferrerInfo* aLoadingReferrerInfo,
const Encoding* aPreloadOrParentDataEncoding, const nsAString& aIntegrity,
bool aSyncLoad, IsPreload aIsPreload);

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

@ -10,8 +10,6 @@
#include "nsIPrincipal.h"
#include "nsIReferrerInfo.h"
#define IGNORE_REFERRER_POLICY_FOR_PRELOAD_COALESCING true
namespace mozilla {
PreloadHashKey::PreloadHashKey(const nsIURI* aKey, ResourceType aAs)
@ -26,7 +24,6 @@ PreloadHashKey::PreloadHashKey(PreloadHashKey&& aToMove)
: nsURIHashKey(std::move(aToMove)) {
mAs = std::move(aToMove.mAs);
mCORSMode = std::move(aToMove.mCORSMode);
mReferrerPolicy = std::move(aToMove.mReferrerPolicy);
mPrincipal = std::move(aToMove.mPrincipal);
switch (mAs) {
@ -55,7 +52,6 @@ PreloadHashKey& PreloadHashKey::operator=(const PreloadHashKey& aOther) {
mAs = aOther.mAs;
mCORSMode = aOther.mCORSMode;
mReferrerPolicy = aOther.mReferrerPolicy;
mPrincipal = aOther.mPrincipal;
switch (mAs) {
@ -79,12 +75,10 @@ PreloadHashKey& PreloadHashKey::operator=(const PreloadHashKey& aOther) {
}
// static
PreloadHashKey PreloadHashKey::CreateAsScript(
nsIURI* aURI, const CORSMode& aCORSMode, const dom::ScriptKind& aScriptKind,
const dom::ReferrerPolicy& aReferrerPolicy) {
PreloadHashKey PreloadHashKey::CreateAsScript(nsIURI* aURI, CORSMode aCORSMode,
dom::ScriptKind aScriptKind) {
PreloadHashKey key(aURI, ResourceType::SCRIPT);
key.mCORSMode = aCORSMode;
key.mReferrerPolicy = aReferrerPolicy;
key.mScript.mScriptKind = aScriptKind;
@ -92,24 +86,22 @@ PreloadHashKey PreloadHashKey::CreateAsScript(
}
// static
PreloadHashKey PreloadHashKey::CreateAsScript(
nsIURI* aURI, const nsAString& aCrossOrigin, const nsAString& aType,
const dom::ReferrerPolicy& aReferrerPolicy) {
PreloadHashKey PreloadHashKey::CreateAsScript(nsIURI* aURI,
const nsAString& aCrossOrigin,
const nsAString& aType) {
dom::ScriptKind scriptKind = dom::ScriptKind::eClassic;
if (aType.LowerCaseEqualsASCII("module")) {
scriptKind = dom::ScriptKind::eModule;
}
CORSMode crossOrigin = dom::Element::StringToCORSMode(aCrossOrigin);
return CreateAsScript(aURI, crossOrigin, scriptKind, aReferrerPolicy);
return CreateAsScript(aURI, crossOrigin, scriptKind);
}
// static
PreloadHashKey PreloadHashKey::CreateAsStyle(
nsIURI* aURI, nsIPrincipal* aPrincipal, dom::ReferrerPolicy aReferrerPolicy,
CORSMode aCORSMode, css::SheetParsingMode aParsingMode) {
nsIURI* aURI, nsIPrincipal* aPrincipal, CORSMode aCORSMode,
css::SheetParsingMode aParsingMode) {
PreloadHashKey key(aURI, ResourceType::STYLE);
key.mReferrerPolicy = aReferrerPolicy;
key.mCORSMode = aCORSMode;
key.mPrincipal = aPrincipal;
@ -122,17 +114,15 @@ PreloadHashKey PreloadHashKey::CreateAsStyle(
PreloadHashKey PreloadHashKey::CreateAsStyle(
css::SheetLoadData& aSheetLoadData) {
return CreateAsStyle(aSheetLoadData.mURI, aSheetLoadData.mTriggeringPrincipal,
aSheetLoadData.ReferrerInfo()->ReferrerPolicy(),
aSheetLoadData.mSheet->GetCORSMode(),
aSheetLoadData.mSheet->ParsingMode());
}
// static
PreloadHashKey PreloadHashKey::CreateAsImage(
nsIURI* aURI, nsIPrincipal* aPrincipal, CORSMode aCORSMode,
dom::ReferrerPolicy const& aReferrerPolicy) {
PreloadHashKey PreloadHashKey::CreateAsImage(nsIURI* aURI,
nsIPrincipal* aPrincipal,
CORSMode aCORSMode) {
PreloadHashKey key(aURI, ResourceType::IMAGE);
key.mReferrerPolicy = aReferrerPolicy;
key.mCORSMode = aCORSMode;
key.mPrincipal = aPrincipal;
@ -140,29 +130,21 @@ PreloadHashKey PreloadHashKey::CreateAsImage(
}
// static
PreloadHashKey PreloadHashKey::CreateAsFetch(
nsIURI* aURI, const CORSMode aCORSMode,
const dom::ReferrerPolicy& aReferrerPolicy) {
PreloadHashKey PreloadHashKey::CreateAsFetch(nsIURI* aURI, CORSMode aCORSMode) {
PreloadHashKey key(aURI, ResourceType::FETCH);
key.mReferrerPolicy = aReferrerPolicy;
key.mCORSMode = aCORSMode;
return key;
}
// static
PreloadHashKey PreloadHashKey::CreateAsFetch(
nsIURI* aURI, const nsAString& aCrossOrigin,
const dom::ReferrerPolicy& aReferrerPolicy) {
return CreateAsFetch(aURI, dom::Element::StringToCORSMode(aCrossOrigin),
aReferrerPolicy);
PreloadHashKey PreloadHashKey::CreateAsFetch(nsIURI* aURI,
const nsAString& aCrossOrigin) {
return CreateAsFetch(aURI, dom::Element::StringToCORSMode(aCrossOrigin));
}
PreloadHashKey PreloadHashKey::CreateAsFont(
nsIURI* aURI, const CORSMode aCORSMode,
const dom::ReferrerPolicy& aReferrerPolicy) {
PreloadHashKey PreloadHashKey::CreateAsFont(nsIURI* aURI, CORSMode aCORSMode) {
PreloadHashKey key(aURI, ResourceType::FONT);
key.mReferrerPolicy = aReferrerPolicy;
key.mCORSMode = aCORSMode;
return key;
@ -173,12 +155,6 @@ bool PreloadHashKey::KeyEquals(KeyTypePointer aOther) const {
return false;
}
#if !IGNORE_REFERRER_POLICY_FOR_PRELOAD_COALESCING
if (mReferrerPolicy != aOther->mReferrerPolicy) {
return false;
}
#endif
if (!mPrincipal != !aOther->mPrincipal) {
// One or the other has a principal, but not both... not equal
return false;
@ -231,10 +207,6 @@ PLDHashNumber PreloadHashKey::HashKey(KeyTypePointer aKey) {
hash = AddToHash(hash, static_cast<uint32_t>(aKey->mAs));
hash = AddToHash(hash, static_cast<uint32_t>(aKey->mCORSMode));
#if !IGNORE_REFERRER_POLICY_FOR_PRELOAD_COALESCING
hash = AddToHash(hash, static_cast<uint32_t>(aKey->mReferrerPolicy));
#endif
return hash;
}

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

@ -7,7 +7,6 @@
#include "mozilla/CORSMode.h"
#include "mozilla/css/SheetParsingMode.h"
#include "mozilla/dom/ReferrerPolicyBinding.h"
#include "mozilla/dom/ScriptKind.h"
#include "nsURIHashKey.h"
@ -44,38 +43,29 @@ class PreloadHashKey : public nsURIHashKey {
PreloadHashKey& operator=(const PreloadHashKey& aOther);
// Construct key for "script"
static PreloadHashKey CreateAsScript(
nsIURI* aURI, const CORSMode& aCORSMode,
const dom::ScriptKind& aScriptKind,
const dom::ReferrerPolicy& aReferrerPolicy);
static PreloadHashKey CreateAsScript(
nsIURI* aURI, const nsAString& aCrossOrigin, const nsAString& aType,
const dom::ReferrerPolicy& aReferrerPolicy);
static PreloadHashKey CreateAsScript(nsIURI* aURI, CORSMode aCORSMode,
dom::ScriptKind aScriptKind);
static PreloadHashKey CreateAsScript(nsIURI* aURI,
const nsAString& aCrossOrigin,
const nsAString& aType);
// Construct key for "style"
static PreloadHashKey CreateAsStyle(nsIURI* aURI, nsIPrincipal* aPrincipal,
dom::ReferrerPolicy aReferrerPolicy,
CORSMode aCORSMode,
css::SheetParsingMode aParsingMode);
static PreloadHashKey CreateAsStyle(css::SheetLoadData&);
// Construct key for "image"
static PreloadHashKey CreateAsImage(
nsIURI* aURI, nsIPrincipal* aPrincipal, CORSMode aCORSMode,
dom::ReferrerPolicy const& aReferrerPolicy);
static PreloadHashKey CreateAsImage(nsIURI* aURI, nsIPrincipal* aPrincipal,
CORSMode aCORSMode);
// Construct key for "fetch"
static PreloadHashKey CreateAsFetch(
nsIURI* aURI, const CORSMode aCORSMode,
const dom::ReferrerPolicy& aReferrerPolicy);
static PreloadHashKey CreateAsFetch(
nsIURI* aURI, const nsAString& aCrossOrigin,
const dom::ReferrerPolicy& aReferrerPolicy);
static PreloadHashKey CreateAsFetch(nsIURI* aURI, CORSMode aCORSMode);
static PreloadHashKey CreateAsFetch(nsIURI* aURI,
const nsAString& aCrossOrigin);
// Construct key for "font"
static PreloadHashKey CreateAsFont(
nsIURI* aURI, const CORSMode aCORSMode,
const dom::ReferrerPolicy& aReferrerPolicy);
static PreloadHashKey CreateAsFont(nsIURI* aURI, CORSMode aCORSMode);
KeyType GetKey() const { return const_cast<PreloadHashKey*>(this); }
KeyTypePointer GetKeyPointer() const { return this; }
@ -98,7 +88,6 @@ class PreloadHashKey : public nsURIHashKey {
ResourceType mAs = ResourceType::NONE;
CORSMode mCORSMode = CORS_NONE;
enum dom::ReferrerPolicy mReferrerPolicy = dom::ReferrerPolicy::_empty;
nsCOMPtr<nsIPrincipal> mPrincipal;
struct {

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

@ -72,7 +72,7 @@ already_AddRefed<PreloaderBase> PreloadService::PreloadLinkElement(
return nullptr;
}
nsAutoString as, charset, crossOrigin, integrity, referrerPolicyAttr, srcset,
nsAutoString as, charset, crossOrigin, integrity, referrerPolicy, srcset,
sizes, type, url;
nsCOMPtr<nsIURI> uri = aLinkElement->GetURI();
@ -83,13 +83,12 @@ already_AddRefed<PreloaderBase> PreloadService::PreloadLinkElement(
aLinkElement->GetHref(url);
aLinkElement->GetCrossOrigin(crossOrigin);
aLinkElement->GetIntegrity(integrity);
aLinkElement->GetReferrerPolicy(referrerPolicyAttr);
auto referrerPolicy = PreloadReferrerPolicy(referrerPolicyAttr);
aLinkElement->GetReferrerPolicy(referrerPolicy);
aLinkElement->GetType(type);
RefPtr<PreloaderBase> preload = PreloadOrCoalesce(
uri, url, aPolicyType, as, type, charset, srcset, sizes, integrity,
crossOrigin, referrerPolicy, referrerPolicyAttr);
RefPtr<PreloaderBase> preload =
PreloadOrCoalesce(uri, url, aPolicyType, as, type, charset, srcset, sizes,
integrity, crossOrigin, referrerPolicy);
if (!preload) {
NotifyNodeEvent(aLinkElement, false);
@ -117,10 +116,8 @@ already_AddRefed<PreloaderBase> PreloadService::PreloadLinkHeader(
return nullptr;
}
auto referrerPolicy = PreloadReferrerPolicy(aReferrerPolicy);
return PreloadOrCoalesce(aURI, aURL, aPolicyType, aAs, aType, EmptyString(),
aSrcset, aSizes, aIntegrity, aCORS, referrerPolicy,
aReferrerPolicy);
aSrcset, aSizes, aIntegrity, aCORS, aReferrerPolicy);
}
already_AddRefed<PreloaderBase> PreloadService::PreloadOrCoalesce(
@ -128,18 +125,16 @@ already_AddRefed<PreloaderBase> PreloadService::PreloadOrCoalesce(
const nsAString& aAs, const nsAString& aType, const nsAString& aCharset,
const nsAString& aSrcset, const nsAString& aSizes,
const nsAString& aIntegrity, const nsAString& aCORS,
dom::ReferrerPolicy aReferrerPolicy, const nsAString& aReferrerPolicyAttr) {
const nsAString& aReferrerPolicy) {
bool isImgSet = false;
PreloadHashKey preloadKey;
nsCOMPtr<nsIURI> uri = aURI;
if (aAs.LowerCaseEqualsASCII("script")) {
preloadKey =
PreloadHashKey::CreateAsScript(uri, aCORS, aType, aReferrerPolicy);
preloadKey = PreloadHashKey::CreateAsScript(uri, aCORS, aType);
} else if (aAs.LowerCaseEqualsASCII("style")) {
preloadKey = PreloadHashKey::CreateAsStyle(
uri, mDocument->NodePrincipal(), aReferrerPolicy,
dom::Element::StringToCORSMode(aCORS),
uri, mDocument->NodePrincipal(), dom::Element::StringToCORSMode(aCORS),
css::eAuthorSheetFeatures /* see Loader::LoadSheet */);
} else if (aAs.LowerCaseEqualsASCII("image")) {
uri = mDocument->ResolvePreloadImage(BaseURIForPreload(), aURL, aSrcset,
@ -149,14 +144,13 @@ already_AddRefed<PreloaderBase> PreloadService::PreloadOrCoalesce(
}
preloadKey = PreloadHashKey::CreateAsImage(
uri, mDocument->NodePrincipal(), dom::Element::StringToCORSMode(aCORS),
aReferrerPolicy);
uri, mDocument->NodePrincipal(), dom::Element::StringToCORSMode(aCORS));
} else if (aAs.LowerCaseEqualsASCII("font")) {
preloadKey = PreloadHashKey::CreateAsFont(
uri, dom::Element::StringToCORSMode(aCORS), aReferrerPolicy);
uri, dom::Element::StringToCORSMode(aCORS));
} else if (aAs.LowerCaseEqualsASCII("fetch")) {
preloadKey = PreloadHashKey::CreateAsFetch(
uri, dom::Element::StringToCORSMode(aCORS), aReferrerPolicy);
uri, dom::Element::StringToCORSMode(aCORS));
} else {
return nullptr;
}
@ -164,16 +158,16 @@ already_AddRefed<PreloaderBase> PreloadService::PreloadOrCoalesce(
RefPtr<PreloaderBase> preload = LookupPreload(&preloadKey);
if (!preload) {
if (aAs.LowerCaseEqualsASCII("script")) {
PreloadScript(uri, aType, aCharset, aCORS, aReferrerPolicyAttr,
aIntegrity, true /* isInHead - TODO */);
PreloadScript(uri, aType, aCharset, aCORS, aReferrerPolicy, aIntegrity,
true /* isInHead - TODO */);
} else if (aAs.LowerCaseEqualsASCII("style")) {
PreloadStyle(uri, aCharset, aCORS, aReferrerPolicyAttr, aIntegrity);
PreloadStyle(uri, aCharset, aCORS, aReferrerPolicy, aIntegrity);
} else if (aAs.LowerCaseEqualsASCII("image")) {
PreloadImage(uri, aCORS, aReferrerPolicyAttr, isImgSet);
PreloadImage(uri, aCORS, aReferrerPolicy, isImgSet);
} else if (aAs.LowerCaseEqualsASCII("font")) {
PreloadFont(uri, aCORS, aReferrerPolicyAttr);
PreloadFont(uri, aCORS, aReferrerPolicy);
} else if (aAs.LowerCaseEqualsASCII("fetch")) {
PreloadFetch(uri, aCORS, aReferrerPolicyAttr);
PreloadFetch(uri, aCORS, aReferrerPolicy);
}
preload = LookupPreload(&preloadKey);
@ -216,26 +210,26 @@ void PreloadService::PreloadImage(nsIURI* aURI, const nsAString& aCrossOrigin,
void PreloadService::PreloadFont(nsIURI* aURI, const nsAString& aCrossOrigin,
const nsAString& aReferrerPolicy) {
CORSMode cors = dom::Element::StringToCORSMode(aCrossOrigin);
dom::ReferrerPolicy referrerPolicy = PreloadReferrerPolicy(aReferrerPolicy);
auto key = PreloadHashKey::CreateAsFont(aURI, cors, referrerPolicy);
auto key = PreloadHashKey::CreateAsFont(aURI, cors);
// * Bug 1618549: Depending on where we decide to do the deduplication, we may
// want to check if the font is already being preloaded here.
RefPtr<FontPreloader> preloader = new FontPreloader();
dom::ReferrerPolicy referrerPolicy = PreloadReferrerPolicy(aReferrerPolicy);
preloader->OpenChannel(&key, aURI, cors, referrerPolicy, mDocument);
}
void PreloadService::PreloadFetch(nsIURI* aURI, const nsAString& aCrossOrigin,
const nsAString& aReferrerPolicy) {
CORSMode cors = dom::Element::StringToCORSMode(aCrossOrigin);
dom::ReferrerPolicy referrerPolicy = PreloadReferrerPolicy(aReferrerPolicy);
auto key = PreloadHashKey::CreateAsFetch(aURI, cors, referrerPolicy);
auto key = PreloadHashKey::CreateAsFetch(aURI, cors);
// * Bug 1618549: Depending on where we decide to do the deduplication, we may
// want to check if a fetch is already being preloaded here.
RefPtr<FetchPreloader> preloader = new FetchPreloader();
dom::ReferrerPolicy referrerPolicy = PreloadReferrerPolicy(aReferrerPolicy);
preloader->OpenChannel(&key, aURI, cors, referrerPolicy, mDocument);
}

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

@ -59,7 +59,7 @@ class PreloadService {
already_AddRefed<nsIURI> GetPreloadURI(const nsAString& aURL);
already_AddRefed<PreloaderBase> PreloadLinkElement(
dom::HTMLLinkElement* aLinkElement, nsContentPolicyType aPolicyType,
dom::HTMLLinkElement* aLink, nsContentPolicyType aPolicyType,
nsIReferrerInfo* aReferrerInfo);
already_AddRefed<PreloaderBase> PreloadLinkHeader(
@ -99,8 +99,7 @@ class PreloadService {
const nsAString& aAs, const nsAString& aType, const nsAString& aCharset,
const nsAString& aSrcset, const nsAString& aSizes,
const nsAString& aIntegrity, const nsAString& aCORS,
dom::ReferrerPolicy aReferrerPolicy,
const nsAString& aReferrerPolicyAttr);
const nsAString& aReferrerPolicy);
private:
nsRefPtrHashtable<PreloadHashKey, PreloaderBase> mPreloads;

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

@ -264,8 +264,7 @@ TEST(TestFetchPreloader, CacheNoneBeforeConsume)
{
nsCOMPtr<nsIURI> uri;
NS_NewURI(getter_AddRefs(uri), NS_LITERAL_CSTRING("https://example.com"));
auto key = mozilla::PreloadHashKey::CreateAsFetch(
uri, mozilla::CORS_NONE, mozilla::dom::ReferrerPolicy::_empty);
auto key = mozilla::PreloadHashKey::CreateAsFetch(uri, mozilla::CORS_NONE);
RefPtr<FakeChannel> channel = new FakeChannel();
RefPtr<FakePreloader> preloader = new FakePreloader(channel);
@ -303,8 +302,7 @@ TEST(TestFetchPreloader, CacheStartBeforeConsume)
{
nsCOMPtr<nsIURI> uri;
NS_NewURI(getter_AddRefs(uri), NS_LITERAL_CSTRING("https://example.com"));
auto key = mozilla::PreloadHashKey::CreateAsFetch(
uri, mozilla::CORS_NONE, mozilla::dom::ReferrerPolicy::_empty);
auto key = mozilla::PreloadHashKey::CreateAsFetch(uri, mozilla::CORS_NONE);
RefPtr<FakeChannel> channel = new FakeChannel();
RefPtr<FakePreloader> preloader = new FakePreloader(channel);
@ -344,8 +342,7 @@ TEST(TestFetchPreloader, CachePartOfDataBeforeConsume)
{
nsCOMPtr<nsIURI> uri;
NS_NewURI(getter_AddRefs(uri), NS_LITERAL_CSTRING("https://example.com"));
auto key = mozilla::PreloadHashKey::CreateAsFetch(
uri, mozilla::CORS_NONE, mozilla::dom::ReferrerPolicy::_empty);
auto key = mozilla::PreloadHashKey::CreateAsFetch(uri, mozilla::CORS_NONE);
RefPtr<FakeChannel> channel = new FakeChannel();
RefPtr<FakePreloader> preloader = new FakePreloader(channel);
@ -385,8 +382,7 @@ TEST(TestFetchPreloader, CacheAllDataBeforeConsume)
{
nsCOMPtr<nsIURI> uri;
NS_NewURI(getter_AddRefs(uri), NS_LITERAL_CSTRING("https://example.com"));
auto key = mozilla::PreloadHashKey::CreateAsFetch(
uri, mozilla::CORS_NONE, mozilla::dom::ReferrerPolicy::_empty);
auto key = mozilla::PreloadHashKey::CreateAsFetch(uri, mozilla::CORS_NONE);
RefPtr<FakeChannel> channel = new FakeChannel();
RefPtr<FakePreloader> preloader = new FakePreloader(channel);
@ -426,8 +422,7 @@ TEST(TestFetchPreloader, CacheAllBeforeConsume)
{
nsCOMPtr<nsIURI> uri;
NS_NewURI(getter_AddRefs(uri), NS_LITERAL_CSTRING("https://example.com"));
auto key = mozilla::PreloadHashKey::CreateAsFetch(
uri, mozilla::CORS_NONE, mozilla::dom::ReferrerPolicy::_empty);
auto key = mozilla::PreloadHashKey::CreateAsFetch(uri, mozilla::CORS_NONE);
RefPtr<FakeChannel> channel = new FakeChannel();
RefPtr<FakePreloader> preloader = new FakePreloader(channel);
@ -466,8 +461,7 @@ TEST(TestFetchPreloader, CacheAllBeforeConsumeWithChannelError)
{
nsCOMPtr<nsIURI> uri;
NS_NewURI(getter_AddRefs(uri), NS_LITERAL_CSTRING("https://example.com"));
auto key = mozilla::PreloadHashKey::CreateAsFetch(
uri, mozilla::CORS_NONE, mozilla::dom::ReferrerPolicy::_empty);
auto key = mozilla::PreloadHashKey::CreateAsFetch(uri, mozilla::CORS_NONE);
RefPtr<FakeChannel> channel = new FakeChannel();
RefPtr<FakePreloader> preloader = new FakePreloader(channel);
@ -506,8 +500,7 @@ TEST(TestFetchPreloader, CacheAllBeforeConsumeWithChannelCancel)
{
nsCOMPtr<nsIURI> uri;
NS_NewURI(getter_AddRefs(uri), NS_LITERAL_CSTRING("https://example.com"));
auto key = mozilla::PreloadHashKey::CreateAsFetch(
uri, mozilla::CORS_NONE, mozilla::dom::ReferrerPolicy::_empty);
auto key = mozilla::PreloadHashKey::CreateAsFetch(uri, mozilla::CORS_NONE);
RefPtr<FakeChannel> channel = new FakeChannel();
RefPtr<FakePreloader> preloader = new FakePreloader(channel);
@ -550,8 +543,7 @@ TEST(TestFetchPreloader, CacheAllBeforeConsumeThrowFromOnStartRequest)
{
nsCOMPtr<nsIURI> uri;
NS_NewURI(getter_AddRefs(uri), NS_LITERAL_CSTRING("https://example.com"));
auto key = mozilla::PreloadHashKey::CreateAsFetch(
uri, mozilla::CORS_NONE, mozilla::dom::ReferrerPolicy::_empty);
auto key = mozilla::PreloadHashKey::CreateAsFetch(uri, mozilla::CORS_NONE);
RefPtr<FakeChannel> channel = new FakeChannel();
RefPtr<FakePreloader> preloader = new FakePreloader(channel);
@ -591,8 +583,7 @@ TEST(TestFetchPreloader, CacheAllBeforeConsumeThrowFromOnDataAvailable)
{
nsCOMPtr<nsIURI> uri;
NS_NewURI(getter_AddRefs(uri), NS_LITERAL_CSTRING("https://example.com"));
auto key = mozilla::PreloadHashKey::CreateAsFetch(
uri, mozilla::CORS_NONE, mozilla::dom::ReferrerPolicy::_empty);
auto key = mozilla::PreloadHashKey::CreateAsFetch(uri, mozilla::CORS_NONE);
RefPtr<FakeChannel> channel = new FakeChannel();
RefPtr<FakePreloader> preloader = new FakePreloader(channel);
@ -632,8 +623,7 @@ TEST(TestFetchPreloader, CacheAllBeforeConsumeThrowFromOnStopRequest)
{
nsCOMPtr<nsIURI> uri;
NS_NewURI(getter_AddRefs(uri), NS_LITERAL_CSTRING("https://example.com"));
auto key = mozilla::PreloadHashKey::CreateAsFetch(
uri, mozilla::CORS_NONE, mozilla::dom::ReferrerPolicy::_empty);
auto key = mozilla::PreloadHashKey::CreateAsFetch(uri, mozilla::CORS_NONE);
RefPtr<FakeChannel> channel = new FakeChannel();
RefPtr<FakePreloader> preloader = new FakePreloader(channel);
@ -675,8 +665,7 @@ TEST(TestFetchPreloader, CacheAllBeforeConsumeCancelInOnStartRequest)
{
nsCOMPtr<nsIURI> uri;
NS_NewURI(getter_AddRefs(uri), NS_LITERAL_CSTRING("https://example.com"));
auto key = mozilla::PreloadHashKey::CreateAsFetch(
uri, mozilla::CORS_NONE, mozilla::dom::ReferrerPolicy::_empty);
auto key = mozilla::PreloadHashKey::CreateAsFetch(uri, mozilla::CORS_NONE);
RefPtr<FakeChannel> channel = new FakeChannel();
RefPtr<FakePreloader> preloader = new FakePreloader(channel);
@ -719,8 +708,7 @@ TEST(TestFetchPreloader, CacheAllBeforeConsumeCancelInOnDataAvailable)
{
nsCOMPtr<nsIURI> uri;
NS_NewURI(getter_AddRefs(uri), NS_LITERAL_CSTRING("https://example.com"));
auto key = mozilla::PreloadHashKey::CreateAsFetch(
uri, mozilla::CORS_NONE, mozilla::dom::ReferrerPolicy::_empty);
auto key = mozilla::PreloadHashKey::CreateAsFetch(uri, mozilla::CORS_NONE);
RefPtr<FakeChannel> channel = new FakeChannel();
RefPtr<FakePreloader> preloader = new FakePreloader(channel);
@ -764,8 +752,7 @@ TEST(TestFetchPreloader, CachePartlyBeforeConsumeCancelInOnDataAvailable)
{
nsCOMPtr<nsIURI> uri;
NS_NewURI(getter_AddRefs(uri), NS_LITERAL_CSTRING("https://example.com"));
auto key = mozilla::PreloadHashKey::CreateAsFetch(
uri, mozilla::CORS_NONE, mozilla::dom::ReferrerPolicy::_empty);
auto key = mozilla::PreloadHashKey::CreateAsFetch(uri, mozilla::CORS_NONE);
RefPtr<FakeChannel> channel = new FakeChannel();
RefPtr<FakePreloader> preloader = new FakePreloader(channel);
@ -806,8 +793,7 @@ TEST(TestFetchPreloader, CachePartlyBeforeConsumeCancelInOnStartRequestAndRace)
{
nsCOMPtr<nsIURI> uri;
NS_NewURI(getter_AddRefs(uri), NS_LITERAL_CSTRING("https://example.com"));
auto key = mozilla::PreloadHashKey::CreateAsFetch(
uri, mozilla::CORS_NONE, mozilla::dom::ReferrerPolicy::_empty);
auto key = mozilla::PreloadHashKey::CreateAsFetch(uri, mozilla::CORS_NONE);
RefPtr<FakeChannel> channel = new FakeChannel();
RefPtr<FakePreloader> preloader = new FakePreloader(channel);
@ -856,8 +842,7 @@ TEST(TestFetchPreloader, CachePartlyBeforeConsumeCancelInOnDataAvailableAndRace)
{
nsCOMPtr<nsIURI> uri;
NS_NewURI(getter_AddRefs(uri), NS_LITERAL_CSTRING("https://example.com"));
auto key = mozilla::PreloadHashKey::CreateAsFetch(
uri, mozilla::CORS_NONE, mozilla::dom::ReferrerPolicy::_empty);
auto key = mozilla::PreloadHashKey::CreateAsFetch(uri, mozilla::CORS_NONE);
RefPtr<FakeChannel> channel = new FakeChannel();
RefPtr<FakePreloader> preloader = new FakePreloader(channel);
@ -906,8 +891,7 @@ TEST(TestFetchPreloader, CachePartlyBeforeConsumeThrowFromOnStartRequestAndRace)
{
nsCOMPtr<nsIURI> uri;
NS_NewURI(getter_AddRefs(uri), NS_LITERAL_CSTRING("https://example.com"));
auto key = mozilla::PreloadHashKey::CreateAsFetch(
uri, mozilla::CORS_NONE, mozilla::dom::ReferrerPolicy::_empty);
auto key = mozilla::PreloadHashKey::CreateAsFetch(uri, mozilla::CORS_NONE);
RefPtr<FakeChannel> channel = new FakeChannel();
RefPtr<FakePreloader> preloader = new FakePreloader(channel);