Bug 1067345 - Part 3 - Implement speculative load operations for <picture> and <img srcset>

This commit is contained in:
John Schoenick
2014-12-10 18:54:00 -05:00
parent 73294b7f47
commit 303bd3cb54
8 changed files with 356 additions and 41 deletions

View File

@@ -859,24 +859,31 @@ nsHtml5TreeOpExecutor::IsExternalViewSource()
// Speculative loading
nsIURI*
nsHtml5TreeOpExecutor::BaseURIForPreload()
{
// The URL of the document without <base>
nsIURI* documentURI = mDocument->GetDocumentURI();
// The URL of the document with non-speculative <base>
nsIURI* documentBaseURI = mDocument->GetDocBaseURI();
// If the two above are different, use documentBaseURI. If they are the same,
// the document object isn't aware of a <base>, so attempt to use the
// mSpeculationBaseURI or, failing, that, documentURI.
return (documentURI == documentBaseURI) ?
(mSpeculationBaseURI ?
mSpeculationBaseURI.get() : documentURI)
: documentBaseURI;
}
already_AddRefed<nsIURI>
nsHtml5TreeOpExecutor::ConvertIfNotPreloadedYet(const nsAString& aURL)
{
if (aURL.IsEmpty()) {
return nullptr;
}
// The URL of the document without <base>
nsIURI* documentURI = mDocument->GetDocumentURI();
// The URL of the document with non-speculative <base>
nsIURI* documentBaseURI = mDocument->GetDocBaseURI();
// If the two above are different, use documentBaseURI. If they are the
// same, the document object isn't aware of a <base>, so attempt to use the
// mSpeculationBaseURI or, failing, that, documentURI.
nsIURI* base = (documentURI == documentBaseURI) ?
(mSpeculationBaseURI ?
mSpeculationBaseURI.get() : documentURI)
: documentBaseURI;
nsIURI* base = BaseURIForPreload();
const nsCString& charset = mDocument->GetDocumentCharacterSet();
nsCOMPtr<nsIURI> uri;
nsresult rv = NS_NewURI(getter_AddRefs(uri), aURL, charset.get(), base);
@@ -884,13 +891,24 @@ nsHtml5TreeOpExecutor::ConvertIfNotPreloadedYet(const nsAString& aURL)
NS_WARNING("Failed to create a URI");
return nullptr;
}
if (ShouldPreloadURI(uri)) {
return uri.forget();
}
return nullptr;
}
bool
nsHtml5TreeOpExecutor::ShouldPreloadURI(nsIURI *aURI)
{
nsAutoCString spec;
uri->GetSpec(spec);
aURI->GetSpec(spec);
if (mPreloadedURLs.Contains(spec)) {
return nullptr;
return false;
}
mPreloadedURLs.PutEntry(spec);
return uri.forget();
return true;
}
void
@@ -924,13 +942,39 @@ nsHtml5TreeOpExecutor::PreloadStyle(const nsAString& aURL,
void
nsHtml5TreeOpExecutor::PreloadImage(const nsAString& aURL,
const nsAString& aCrossOrigin)
const nsAString& aCrossOrigin,
const nsAString& aSrcset,
const nsAString& aSizes)
{
nsCOMPtr<nsIURI> uri = ConvertIfNotPreloadedYet(aURL);
if (!uri) {
return;
nsCOMPtr<nsIURI> baseURI = BaseURIForPreload();
nsCOMPtr<nsIURI> uri = mDocument->ResolvePreloadImage(baseURI, aURL, aSrcset,
aSizes);
if (uri && ShouldPreloadURI(uri)) {
mDocument->MaybePreLoadImage(uri, aCrossOrigin, mSpeculationReferrerPolicy);
}
mDocument->MaybePreLoadImage(uri, aCrossOrigin, mSpeculationReferrerPolicy);
}
// These calls inform the document of picture state and seen sources, such that
// it can use them to inform ResolvePreLoadImage as necessary
void
nsHtml5TreeOpExecutor::PreloadPictureSource(const nsAString& aSrcset,
const nsAString& aSizes,
const nsAString& aType,
const nsAString& aMedia)
{
mDocument->PreloadPictureImageSource(aSrcset, aSizes, aType, aMedia);
}
void
nsHtml5TreeOpExecutor::PreloadOpenPicture()
{
mDocument->PreloadPictureOpened();
}
void
nsHtml5TreeOpExecutor::PreloadEndPicture()
{
mDocument->PreloadPictureClosed();
}
void