mirror of
https://github.com/mozilla/gecko-dev.git
synced 2024-11-26 06:11:37 +00:00
Bug 1572526 - Move a bunch of code that's only called by HTMLLinkElement out of Link.cpp. r=bzbarsky
Seems like a more logical place for it to live. Differential Revision: https://phabricator.services.mozilla.com/D41270 --HG-- extra : moz-landing-system : lando
This commit is contained in:
parent
66c0a4fa9b
commit
d47ef2a1b7
@ -6,7 +6,6 @@
|
||||
|
||||
#include "Link.h"
|
||||
|
||||
#include "mozilla/Components.h"
|
||||
#include "mozilla/EventStates.h"
|
||||
#include "mozilla/MemoryReporting.h"
|
||||
#include "mozilla/dom/Element.h"
|
||||
@ -95,236 +94,6 @@ void Link::CancelDNSPrefetch(nsWrapperCache::FlagsType aDeferredFlag,
|
||||
}
|
||||
}
|
||||
|
||||
void Link::GetContentPolicyMimeTypeMedia(nsAttrValue& aAsAttr,
|
||||
nsContentPolicyType& aPolicyType,
|
||||
nsString& aMimeType,
|
||||
nsAString& aMedia) {
|
||||
nsAutoString as;
|
||||
mElement->GetAttr(kNameSpaceID_None, nsGkAtoms::as, as);
|
||||
Link::ParseAsValue(as, aAsAttr);
|
||||
aPolicyType = AsValueToContentPolicy(aAsAttr);
|
||||
|
||||
nsAutoString type;
|
||||
mElement->GetAttr(kNameSpaceID_None, nsGkAtoms::type, type);
|
||||
nsAutoString notUsed;
|
||||
nsContentUtils::SplitMimeType(type, aMimeType, notUsed);
|
||||
|
||||
mElement->GetAttr(kNameSpaceID_None, nsGkAtoms::media, aMedia);
|
||||
}
|
||||
|
||||
void Link::TryDNSPrefetchOrPreconnectOrPrefetchOrPreloadOrPrerender() {
|
||||
MOZ_ASSERT(mElement->IsInComposedDoc());
|
||||
if (!ElementHasHref()) {
|
||||
return;
|
||||
}
|
||||
|
||||
nsAutoString rel;
|
||||
if (!mElement->GetAttr(kNameSpaceID_None, nsGkAtoms::rel, rel)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!nsContentUtils::PrefetchPreloadEnabled(
|
||||
mElement->OwnerDoc()->GetDocShell())) {
|
||||
return;
|
||||
}
|
||||
|
||||
uint32_t linkTypes = nsStyleLinkElement::ParseLinkTypes(rel);
|
||||
|
||||
if ((linkTypes & nsStyleLinkElement::ePREFETCH) ||
|
||||
(linkTypes & nsStyleLinkElement::eNEXT) ||
|
||||
(linkTypes & nsStyleLinkElement::ePRELOAD)) {
|
||||
nsCOMPtr<nsIPrefetchService> prefetchService(
|
||||
components::Prefetch::Service());
|
||||
if (prefetchService) {
|
||||
nsCOMPtr<nsIURI> uri(GetURI());
|
||||
if (uri) {
|
||||
bool preload = !!(linkTypes & nsStyleLinkElement::ePRELOAD);
|
||||
nsContentPolicyType policyType;
|
||||
|
||||
if (preload) {
|
||||
nsAttrValue asAttr;
|
||||
nsAutoString mimeType;
|
||||
nsAutoString media;
|
||||
GetContentPolicyMimeTypeMedia(asAttr, policyType, mimeType, media);
|
||||
|
||||
if (policyType == nsIContentPolicy::TYPE_INVALID) {
|
||||
// Ignore preload with a wrong or empty as attribute.
|
||||
return;
|
||||
}
|
||||
|
||||
if (!HTMLLinkElement::CheckPreloadAttrs(asAttr, mimeType, media,
|
||||
mElement->OwnerDoc())) {
|
||||
policyType = nsIContentPolicy::TYPE_INVALID;
|
||||
}
|
||||
}
|
||||
|
||||
nsCOMPtr<nsIReferrerInfo> referrerInfo = new ReferrerInfo();
|
||||
referrerInfo->InitWithNode(mElement);
|
||||
if (preload) {
|
||||
prefetchService->PreloadURI(uri, referrerInfo, mElement, policyType);
|
||||
} else {
|
||||
prefetchService->PrefetchURI(
|
||||
uri, referrerInfo, mElement,
|
||||
linkTypes & nsStyleLinkElement::ePREFETCH);
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (linkTypes & nsStyleLinkElement::ePRECONNECT) {
|
||||
nsCOMPtr<nsIURI> uri(GetURI());
|
||||
if (uri && mElement->OwnerDoc()) {
|
||||
mElement->OwnerDoc()->MaybePreconnect(
|
||||
uri, Element::AttrValueToCORSMode(
|
||||
mElement->GetParsedAttr(nsGkAtoms::crossorigin)));
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (linkTypes & nsStyleLinkElement::eDNS_PREFETCH) {
|
||||
if (nsHTMLDNSPrefetch::IsAllowed(mElement->OwnerDoc())) {
|
||||
nsHTMLDNSPrefetch::PrefetchLow(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Link::UpdatePreload(nsAtom* aName, const nsAttrValue* aValue,
|
||||
const nsAttrValue* aOldValue) {
|
||||
MOZ_ASSERT(mElement->IsInComposedDoc());
|
||||
|
||||
if (!ElementHasHref()) {
|
||||
return;
|
||||
}
|
||||
|
||||
nsAutoString rel;
|
||||
if (!mElement->GetAttr(kNameSpaceID_None, nsGkAtoms::rel, rel)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!nsContentUtils::PrefetchPreloadEnabled(
|
||||
mElement->OwnerDoc()->GetDocShell())) {
|
||||
return;
|
||||
}
|
||||
|
||||
uint32_t linkTypes = nsStyleLinkElement::ParseLinkTypes(rel);
|
||||
|
||||
if (!(linkTypes & nsStyleLinkElement::ePRELOAD)) {
|
||||
return;
|
||||
}
|
||||
|
||||
nsCOMPtr<nsIPrefetchService> prefetchService(components::Prefetch::Service());
|
||||
if (!prefetchService) {
|
||||
return;
|
||||
}
|
||||
|
||||
nsCOMPtr<nsIURI> uri(GetURI());
|
||||
if (!uri) {
|
||||
return;
|
||||
}
|
||||
|
||||
nsAttrValue asAttr;
|
||||
nsContentPolicyType asPolicyType;
|
||||
nsAutoString mimeType;
|
||||
nsAutoString media;
|
||||
GetContentPolicyMimeTypeMedia(asAttr, asPolicyType, mimeType, media);
|
||||
|
||||
if (asPolicyType == nsIContentPolicy::TYPE_INVALID) {
|
||||
// Ignore preload with a wrong or empty as attribute, but be sure to cancel
|
||||
// the old one.
|
||||
prefetchService->CancelPrefetchPreloadURI(uri, mElement);
|
||||
return;
|
||||
}
|
||||
|
||||
nsContentPolicyType policyType = asPolicyType;
|
||||
if (!HTMLLinkElement::CheckPreloadAttrs(asAttr, mimeType, media,
|
||||
mElement->OwnerDoc())) {
|
||||
policyType = nsIContentPolicy::TYPE_INVALID;
|
||||
}
|
||||
|
||||
if (aName == nsGkAtoms::crossorigin) {
|
||||
CORSMode corsMode = Element::AttrValueToCORSMode(aValue);
|
||||
CORSMode oldCorsMode = Element::AttrValueToCORSMode(aOldValue);
|
||||
if (corsMode != oldCorsMode) {
|
||||
prefetchService->CancelPrefetchPreloadURI(uri, mElement);
|
||||
|
||||
nsCOMPtr<nsIReferrerInfo> referrerInfo = new ReferrerInfo();
|
||||
referrerInfo->InitWithNode(mElement);
|
||||
prefetchService->PreloadURI(uri, referrerInfo, mElement, policyType);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
nsContentPolicyType oldPolicyType;
|
||||
|
||||
if (aName == nsGkAtoms::as) {
|
||||
if (aOldValue) {
|
||||
oldPolicyType = AsValueToContentPolicy(*aOldValue);
|
||||
if (!HTMLLinkElement::CheckPreloadAttrs(*aOldValue, mimeType, media,
|
||||
mElement->OwnerDoc())) {
|
||||
oldPolicyType = nsIContentPolicy::TYPE_INVALID;
|
||||
}
|
||||
} else {
|
||||
oldPolicyType = nsIContentPolicy::TYPE_INVALID;
|
||||
}
|
||||
} else if (aName == nsGkAtoms::type) {
|
||||
nsAutoString oldType;
|
||||
nsAutoString notUsed;
|
||||
if (aOldValue) {
|
||||
aOldValue->ToString(oldType);
|
||||
} else {
|
||||
oldType = EmptyString();
|
||||
}
|
||||
nsAutoString oldMimeType;
|
||||
nsContentUtils::SplitMimeType(oldType, oldMimeType, notUsed);
|
||||
if (HTMLLinkElement::CheckPreloadAttrs(asAttr, oldMimeType, media,
|
||||
mElement->OwnerDoc())) {
|
||||
oldPolicyType = asPolicyType;
|
||||
} else {
|
||||
oldPolicyType = nsIContentPolicy::TYPE_INVALID;
|
||||
}
|
||||
} else {
|
||||
MOZ_ASSERT(aName == nsGkAtoms::media);
|
||||
nsAutoString oldMedia;
|
||||
if (aOldValue) {
|
||||
aOldValue->ToString(oldMedia);
|
||||
} else {
|
||||
oldMedia = EmptyString();
|
||||
}
|
||||
if (HTMLLinkElement::CheckPreloadAttrs(asAttr, mimeType, oldMedia,
|
||||
mElement->OwnerDoc())) {
|
||||
oldPolicyType = asPolicyType;
|
||||
} else {
|
||||
oldPolicyType = nsIContentPolicy::TYPE_INVALID;
|
||||
}
|
||||
}
|
||||
|
||||
if ((policyType != oldPolicyType) &&
|
||||
(oldPolicyType != nsIContentPolicy::TYPE_INVALID)) {
|
||||
prefetchService->CancelPrefetchPreloadURI(uri, mElement);
|
||||
}
|
||||
|
||||
// Trigger a new preload if the policy type has changed.
|
||||
// Also trigger load if the new policy type is invalid, this will only
|
||||
// trigger an error event.
|
||||
if ((policyType != oldPolicyType) ||
|
||||
(policyType == nsIContentPolicy::TYPE_INVALID)) {
|
||||
nsCOMPtr<nsIReferrerInfo> referrerInfo = new ReferrerInfo();
|
||||
referrerInfo->InitWithNode(mElement);
|
||||
prefetchService->PreloadURI(uri, referrerInfo, mElement, policyType);
|
||||
}
|
||||
}
|
||||
|
||||
void Link::CancelPrefetchOrPreload() {
|
||||
nsCOMPtr<nsIPrefetchService> prefetchService(components::Prefetch::Service());
|
||||
if (prefetchService) {
|
||||
nsCOMPtr<nsIURI> uri(GetURI());
|
||||
if (uri) {
|
||||
prefetchService->CancelPrefetchPreloadURI(uri, mElement);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Link::SetLinkState(nsLinkState aState) {
|
||||
MOZ_ASSERT(mRegistered, "Setting the link state of an unregistered Link!");
|
||||
MOZ_ASSERT(mLinkState != aState, "Setting state to the currently set state!");
|
||||
@ -835,48 +604,5 @@ size_t Link::SizeOfExcludingThis(mozilla::SizeOfState& aState) const {
|
||||
return n;
|
||||
}
|
||||
|
||||
static const nsAttrValue::EnumTable kAsAttributeTable[] = {
|
||||
{"", DESTINATION_INVALID}, {"audio", DESTINATION_AUDIO},
|
||||
{"font", DESTINATION_FONT}, {"image", DESTINATION_IMAGE},
|
||||
{"script", DESTINATION_SCRIPT}, {"style", DESTINATION_STYLE},
|
||||
{"track", DESTINATION_TRACK}, {"video", DESTINATION_VIDEO},
|
||||
{"fetch", DESTINATION_FETCH}, {nullptr, 0}};
|
||||
|
||||
/* static */
|
||||
void Link::ParseAsValue(const nsAString& aValue, nsAttrValue& aResult) {
|
||||
DebugOnly<bool> success =
|
||||
aResult.ParseEnumValue(aValue, kAsAttributeTable, false,
|
||||
// default value is a empty string
|
||||
// if aValue is not a value we
|
||||
// understand
|
||||
&kAsAttributeTable[0]);
|
||||
MOZ_ASSERT(success);
|
||||
}
|
||||
|
||||
/* static */
|
||||
nsContentPolicyType Link::AsValueToContentPolicy(const nsAttrValue& aValue) {
|
||||
switch (aValue.GetEnumValue()) {
|
||||
case DESTINATION_INVALID:
|
||||
return nsIContentPolicy::TYPE_INVALID;
|
||||
case DESTINATION_AUDIO:
|
||||
return nsIContentPolicy::TYPE_INTERNAL_AUDIO;
|
||||
case DESTINATION_TRACK:
|
||||
return nsIContentPolicy::TYPE_INTERNAL_TRACK;
|
||||
case DESTINATION_VIDEO:
|
||||
return nsIContentPolicy::TYPE_INTERNAL_VIDEO;
|
||||
case DESTINATION_FONT:
|
||||
return nsIContentPolicy::TYPE_FONT;
|
||||
case DESTINATION_IMAGE:
|
||||
return nsIContentPolicy::TYPE_IMAGE;
|
||||
case DESTINATION_SCRIPT:
|
||||
return nsIContentPolicy::TYPE_SCRIPT;
|
||||
case DESTINATION_STYLE:
|
||||
return nsIContentPolicy::TYPE_STYLESHEET;
|
||||
case DESTINATION_FETCH:
|
||||
return nsIContentPolicy::TYPE_OTHER;
|
||||
}
|
||||
return nsIContentPolicy::TYPE_INVALID;
|
||||
}
|
||||
|
||||
} // namespace dom
|
||||
} // namespace mozilla
|
||||
|
@ -119,17 +119,11 @@ class Link : public nsISupports {
|
||||
|
||||
virtual bool ElementHasHref() const;
|
||||
|
||||
// This is called by HTMLAnchorElement.
|
||||
// This is called by HTMLAnchorElement and HTMLLinkElement.
|
||||
void TryDNSPrefetch();
|
||||
void CancelDNSPrefetch(nsWrapperCache::FlagsType aDeferredFlag,
|
||||
nsWrapperCache::FlagsType aRequestedFlag);
|
||||
|
||||
// This is called by HTMLLinkElement.
|
||||
void TryDNSPrefetchOrPreconnectOrPrefetchOrPreloadOrPrerender();
|
||||
void UpdatePreload(nsAtom* aName, const nsAttrValue* aValue,
|
||||
const nsAttrValue* aOldValue);
|
||||
void CancelPrefetchOrPreload();
|
||||
|
||||
bool HasPendingLinkUpdate() const { return mHasPendingLinkUpdate; }
|
||||
void SetHasPendingLinkUpdate() { mHasPendingLinkUpdate = true; }
|
||||
void ClearHasPendingLinkUpdate() { mHasPendingLinkUpdate = false; }
|
||||
@ -144,9 +138,6 @@ class Link : public nsISupports {
|
||||
void SetIsInDNSPrefetch() { mInDNSPrefetch = true; }
|
||||
void ClearIsInDNSPrefetch() { mInDNSPrefetch = false; }
|
||||
|
||||
static void ParseAsValue(const nsAString& aValue, nsAttrValue& aResult);
|
||||
static nsContentPolicyType AsValueToContentPolicy(const nsAttrValue& aValue);
|
||||
|
||||
protected:
|
||||
virtual ~Link();
|
||||
|
||||
@ -173,10 +164,6 @@ class Link : public nsISupports {
|
||||
|
||||
void SetHrefAttribute(nsIURI* aURI);
|
||||
|
||||
void GetContentPolicyMimeTypeMedia(nsAttrValue& aAsAttr,
|
||||
nsContentPolicyType& aPolicyType,
|
||||
nsString& aMimeType, nsAString& aMedia);
|
||||
|
||||
mutable nsCOMPtr<nsIURI> mCachedURI;
|
||||
|
||||
Element* const mElement;
|
||||
|
@ -800,8 +800,8 @@ void nsContentSink::PrefetchPreloadHref(const nsAString& aHref,
|
||||
|
||||
if (preload) {
|
||||
nsAttrValue asAttr;
|
||||
Link::ParseAsValue(aAs, asAttr);
|
||||
policyType = Link::AsValueToContentPolicy(asAttr);
|
||||
HTMLLinkElement::ParseAsValue(aAs, asAttr);
|
||||
policyType = HTMLLinkElement::AsValueToContentPolicy(asAttr);
|
||||
|
||||
if (policyType == nsIContentPolicy::TYPE_INVALID) {
|
||||
// Ignore preload with a wrong or empty as attribute.
|
||||
|
@ -8,6 +8,7 @@
|
||||
|
||||
#include "mozilla/AsyncEventDispatcher.h"
|
||||
#include "mozilla/Attributes.h"
|
||||
#include "mozilla/Components.h"
|
||||
#include "mozilla/EventDispatcher.h"
|
||||
#include "mozilla/EventStates.h"
|
||||
#include "mozilla/MemoryReporting.h"
|
||||
@ -16,12 +17,14 @@
|
||||
#include "mozilla/dom/DocumentInlines.h"
|
||||
#include "mozilla/dom/HTMLLinkElementBinding.h"
|
||||
#include "nsContentUtils.h"
|
||||
#include "nsDOMTokenList.h"
|
||||
#include "nsGenericHTMLElement.h"
|
||||
#include "nsGkAtoms.h"
|
||||
#include "nsDOMTokenList.h"
|
||||
#include "nsHTMLDNSPrefetch.h"
|
||||
#include "nsIContentInlines.h"
|
||||
#include "mozilla/dom/Document.h"
|
||||
#include "nsINode.h"
|
||||
#include "nsIPrefetchService.h"
|
||||
#include "nsIStyleSheetLinkingElement.h"
|
||||
#include "nsIURL.h"
|
||||
#include "nsPIDOMWindow.h"
|
||||
@ -326,9 +329,9 @@ nsresult HTMLLinkElement::AfterSetAttr(int32_t aNameSpaceID, nsAtom* aName,
|
||||
if (aName == nsGkAtoms::rel) {
|
||||
nsAutoString value;
|
||||
aValue->ToString(value);
|
||||
uint32_t linkTypes = nsStyleLinkElement::ParseLinkTypes(value);
|
||||
uint32_t linkTypes = ParseLinkTypes(value);
|
||||
if (GetSheet()) {
|
||||
dropSheet = !(linkTypes & nsStyleLinkElement::eSTYLESHEET);
|
||||
dropSheet = !(linkTypes & eSTYLESHEET);
|
||||
}
|
||||
}
|
||||
|
||||
@ -419,8 +422,8 @@ already_AddRefed<nsIURI> HTMLLinkElement::GetHrefURI() const {
|
||||
Maybe<nsStyleLinkElement::SheetInfo> HTMLLinkElement::GetStyleSheetInfo() {
|
||||
nsAutoString rel;
|
||||
GetAttr(kNameSpaceID_None, nsGkAtoms::rel, rel);
|
||||
uint32_t linkTypes = nsStyleLinkElement::ParseLinkTypes(rel);
|
||||
if (!(linkTypes & nsStyleLinkElement::eSTYLESHEET)) {
|
||||
uint32_t linkTypes = ParseLinkTypes(rel);
|
||||
if (!(linkTypes & eSTYLESHEET)) {
|
||||
return Nothing();
|
||||
}
|
||||
|
||||
@ -436,7 +439,7 @@ Maybe<nsStyleLinkElement::SheetInfo> HTMLLinkElement::GetStyleSheetInfo() {
|
||||
nsAutoString media;
|
||||
GetTitleAndMediaForElement(*this, title, media);
|
||||
|
||||
bool alternate = linkTypes & nsStyleLinkElement::eALTERNATE;
|
||||
bool alternate = linkTypes & eALTERNATE;
|
||||
if (alternate && title.IsEmpty()) {
|
||||
// alternates must have title.
|
||||
return Nothing();
|
||||
@ -486,6 +489,269 @@ void HTMLLinkElement::GetAs(nsAString& aResult) {
|
||||
GetEnumAttr(nsGkAtoms::as, EmptyCString().get(), aResult);
|
||||
}
|
||||
|
||||
static const nsAttrValue::EnumTable kAsAttributeTable[] = {
|
||||
{"", DESTINATION_INVALID}, {"audio", DESTINATION_AUDIO},
|
||||
{"font", DESTINATION_FONT}, {"image", DESTINATION_IMAGE},
|
||||
{"script", DESTINATION_SCRIPT}, {"style", DESTINATION_STYLE},
|
||||
{"track", DESTINATION_TRACK}, {"video", DESTINATION_VIDEO},
|
||||
{"fetch", DESTINATION_FETCH}, {nullptr, 0}};
|
||||
|
||||
/* static */
|
||||
void HTMLLinkElement::ParseAsValue(const nsAString& aValue,
|
||||
nsAttrValue& aResult) {
|
||||
DebugOnly<bool> success =
|
||||
aResult.ParseEnumValue(aValue, kAsAttributeTable, false,
|
||||
// default value is a empty string
|
||||
// if aValue is not a value we
|
||||
// understand
|
||||
&kAsAttributeTable[0]);
|
||||
MOZ_ASSERT(success);
|
||||
}
|
||||
|
||||
/* static */
|
||||
nsContentPolicyType HTMLLinkElement::AsValueToContentPolicy(
|
||||
const nsAttrValue& aValue) {
|
||||
switch (aValue.GetEnumValue()) {
|
||||
case DESTINATION_INVALID:
|
||||
return nsIContentPolicy::TYPE_INVALID;
|
||||
case DESTINATION_AUDIO:
|
||||
return nsIContentPolicy::TYPE_INTERNAL_AUDIO;
|
||||
case DESTINATION_TRACK:
|
||||
return nsIContentPolicy::TYPE_INTERNAL_TRACK;
|
||||
case DESTINATION_VIDEO:
|
||||
return nsIContentPolicy::TYPE_INTERNAL_VIDEO;
|
||||
case DESTINATION_FONT:
|
||||
return nsIContentPolicy::TYPE_FONT;
|
||||
case DESTINATION_IMAGE:
|
||||
return nsIContentPolicy::TYPE_IMAGE;
|
||||
case DESTINATION_SCRIPT:
|
||||
return nsIContentPolicy::TYPE_SCRIPT;
|
||||
case DESTINATION_STYLE:
|
||||
return nsIContentPolicy::TYPE_STYLESHEET;
|
||||
case DESTINATION_FETCH:
|
||||
return nsIContentPolicy::TYPE_OTHER;
|
||||
}
|
||||
return nsIContentPolicy::TYPE_INVALID;
|
||||
}
|
||||
|
||||
void HTMLLinkElement::GetContentPolicyMimeTypeMedia(
|
||||
nsAttrValue& aAsAttr, nsContentPolicyType& aPolicyType, nsString& aMimeType,
|
||||
nsAString& aMedia) {
|
||||
nsAutoString as;
|
||||
GetAttr(kNameSpaceID_None, nsGkAtoms::as, as);
|
||||
ParseAsValue(as, aAsAttr);
|
||||
aPolicyType = AsValueToContentPolicy(aAsAttr);
|
||||
|
||||
nsAutoString type;
|
||||
GetAttr(kNameSpaceID_None, nsGkAtoms::type, type);
|
||||
nsAutoString notUsed;
|
||||
nsContentUtils::SplitMimeType(type, aMimeType, notUsed);
|
||||
|
||||
GetAttr(kNameSpaceID_None, nsGkAtoms::media, aMedia);
|
||||
}
|
||||
|
||||
void HTMLLinkElement::
|
||||
TryDNSPrefetchOrPreconnectOrPrefetchOrPreloadOrPrerender() {
|
||||
MOZ_ASSERT(IsInComposedDoc());
|
||||
if (!ElementHasHref()) {
|
||||
return;
|
||||
}
|
||||
|
||||
nsAutoString rel;
|
||||
if (!GetAttr(kNameSpaceID_None, nsGkAtoms::rel, rel)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!nsContentUtils::PrefetchPreloadEnabled(OwnerDoc()->GetDocShell())) {
|
||||
return;
|
||||
}
|
||||
|
||||
uint32_t linkTypes = ParseLinkTypes(rel);
|
||||
|
||||
if ((linkTypes & ePREFETCH) || (linkTypes & eNEXT) ||
|
||||
(linkTypes & ePRELOAD)) {
|
||||
nsCOMPtr<nsIPrefetchService> prefetchService(
|
||||
components::Prefetch::Service());
|
||||
if (prefetchService) {
|
||||
nsCOMPtr<nsIURI> uri(GetURI());
|
||||
if (uri) {
|
||||
bool preload = !!(linkTypes & ePRELOAD);
|
||||
nsContentPolicyType policyType;
|
||||
|
||||
if (preload) {
|
||||
nsAttrValue asAttr;
|
||||
nsAutoString mimeType;
|
||||
nsAutoString media;
|
||||
GetContentPolicyMimeTypeMedia(asAttr, policyType, mimeType, media);
|
||||
|
||||
if (policyType == nsIContentPolicy::TYPE_INVALID) {
|
||||
// Ignore preload with a wrong or empty as attribute.
|
||||
return;
|
||||
}
|
||||
|
||||
if (!CheckPreloadAttrs(asAttr, mimeType, media, OwnerDoc())) {
|
||||
policyType = nsIContentPolicy::TYPE_INVALID;
|
||||
}
|
||||
}
|
||||
|
||||
nsCOMPtr<nsIReferrerInfo> referrerInfo = new ReferrerInfo();
|
||||
referrerInfo->InitWithNode(this);
|
||||
if (preload) {
|
||||
prefetchService->PreloadURI(uri, referrerInfo, this, policyType);
|
||||
} else {
|
||||
prefetchService->PrefetchURI(uri, referrerInfo, this,
|
||||
linkTypes & ePREFETCH);
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (linkTypes & ePRECONNECT) {
|
||||
if (nsCOMPtr<nsIURI> uri = GetURI()) {
|
||||
OwnerDoc()->MaybePreconnect(
|
||||
uri, AttrValueToCORSMode(GetParsedAttr(nsGkAtoms::crossorigin)));
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (linkTypes & eDNS_PREFETCH) {
|
||||
if (nsHTMLDNSPrefetch::IsAllowed(OwnerDoc())) {
|
||||
nsHTMLDNSPrefetch::PrefetchLow(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void HTMLLinkElement::UpdatePreload(nsAtom* aName, const nsAttrValue* aValue,
|
||||
const nsAttrValue* aOldValue) {
|
||||
MOZ_ASSERT(IsInComposedDoc());
|
||||
|
||||
if (!ElementHasHref()) {
|
||||
return;
|
||||
}
|
||||
|
||||
nsAutoString rel;
|
||||
if (!GetAttr(kNameSpaceID_None, nsGkAtoms::rel, rel)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!nsContentUtils::PrefetchPreloadEnabled(OwnerDoc()->GetDocShell())) {
|
||||
return;
|
||||
}
|
||||
|
||||
uint32_t linkTypes = ParseLinkTypes(rel);
|
||||
|
||||
if (!(linkTypes & ePRELOAD)) {
|
||||
return;
|
||||
}
|
||||
|
||||
nsCOMPtr<nsIPrefetchService> prefetchService(components::Prefetch::Service());
|
||||
if (!prefetchService) {
|
||||
return;
|
||||
}
|
||||
|
||||
nsCOMPtr<nsIURI> uri(GetURI());
|
||||
if (!uri) {
|
||||
return;
|
||||
}
|
||||
|
||||
nsAttrValue asAttr;
|
||||
nsContentPolicyType asPolicyType;
|
||||
nsAutoString mimeType;
|
||||
nsAutoString media;
|
||||
GetContentPolicyMimeTypeMedia(asAttr, asPolicyType, mimeType, media);
|
||||
|
||||
if (asPolicyType == nsIContentPolicy::TYPE_INVALID) {
|
||||
// Ignore preload with a wrong or empty as attribute, but be sure to cancel
|
||||
// the old one.
|
||||
prefetchService->CancelPrefetchPreloadURI(uri, this);
|
||||
return;
|
||||
}
|
||||
|
||||
nsContentPolicyType policyType = asPolicyType;
|
||||
if (!CheckPreloadAttrs(asAttr, mimeType, media, OwnerDoc())) {
|
||||
policyType = nsIContentPolicy::TYPE_INVALID;
|
||||
}
|
||||
|
||||
if (aName == nsGkAtoms::crossorigin) {
|
||||
CORSMode corsMode = AttrValueToCORSMode(aValue);
|
||||
CORSMode oldCorsMode = AttrValueToCORSMode(aOldValue);
|
||||
if (corsMode != oldCorsMode) {
|
||||
prefetchService->CancelPrefetchPreloadURI(uri, this);
|
||||
|
||||
nsCOMPtr<nsIReferrerInfo> referrerInfo = new ReferrerInfo();
|
||||
referrerInfo->InitWithNode(this);
|
||||
prefetchService->PreloadURI(uri, referrerInfo, this, policyType);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
nsContentPolicyType oldPolicyType;
|
||||
|
||||
if (aName == nsGkAtoms::as) {
|
||||
if (aOldValue) {
|
||||
oldPolicyType = AsValueToContentPolicy(*aOldValue);
|
||||
if (!CheckPreloadAttrs(*aOldValue, mimeType, media, OwnerDoc())) {
|
||||
oldPolicyType = nsIContentPolicy::TYPE_INVALID;
|
||||
}
|
||||
} else {
|
||||
oldPolicyType = nsIContentPolicy::TYPE_INVALID;
|
||||
}
|
||||
} else if (aName == nsGkAtoms::type) {
|
||||
nsAutoString oldType;
|
||||
nsAutoString notUsed;
|
||||
if (aOldValue) {
|
||||
aOldValue->ToString(oldType);
|
||||
} else {
|
||||
oldType = EmptyString();
|
||||
}
|
||||
nsAutoString oldMimeType;
|
||||
nsContentUtils::SplitMimeType(oldType, oldMimeType, notUsed);
|
||||
if (CheckPreloadAttrs(asAttr, oldMimeType, media, OwnerDoc())) {
|
||||
oldPolicyType = asPolicyType;
|
||||
} else {
|
||||
oldPolicyType = nsIContentPolicy::TYPE_INVALID;
|
||||
}
|
||||
} else {
|
||||
MOZ_ASSERT(aName == nsGkAtoms::media);
|
||||
nsAutoString oldMedia;
|
||||
if (aOldValue) {
|
||||
aOldValue->ToString(oldMedia);
|
||||
} else {
|
||||
oldMedia = EmptyString();
|
||||
}
|
||||
if (CheckPreloadAttrs(asAttr, mimeType, oldMedia, OwnerDoc())) {
|
||||
oldPolicyType = asPolicyType;
|
||||
} else {
|
||||
oldPolicyType = nsIContentPolicy::TYPE_INVALID;
|
||||
}
|
||||
}
|
||||
|
||||
if ((policyType != oldPolicyType) &&
|
||||
(oldPolicyType != nsIContentPolicy::TYPE_INVALID)) {
|
||||
prefetchService->CancelPrefetchPreloadURI(uri, this);
|
||||
}
|
||||
|
||||
// Trigger a new preload if the policy type has changed.
|
||||
// Also trigger load if the new policy type is invalid, this will only
|
||||
// trigger an error event.
|
||||
if ((policyType != oldPolicyType) ||
|
||||
(policyType == nsIContentPolicy::TYPE_INVALID)) {
|
||||
nsCOMPtr<nsIReferrerInfo> referrerInfo = new ReferrerInfo();
|
||||
referrerInfo->InitWithNode(this);
|
||||
prefetchService->PreloadURI(uri, referrerInfo, this, policyType);
|
||||
}
|
||||
}
|
||||
|
||||
void HTMLLinkElement::CancelPrefetchOrPreload() {
|
||||
nsCOMPtr<nsIPrefetchService> prefetchService(components::Prefetch::Service());
|
||||
if (prefetchService) {
|
||||
if (nsCOMPtr<nsIURI> uri = GetURI()) {
|
||||
prefetchService->CancelPrefetchPreloadURI(uri, this);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// We will use official mime-types from:
|
||||
// https://www.iana.org/assignments/media-types/media-types.xhtml#font
|
||||
// We do not support old deprecated mime-types for preload feature.
|
||||
@ -510,7 +776,7 @@ bool HTMLLinkElement::CheckPreloadAttrs(const nsAttrValue& aAs,
|
||||
const nsAString& aType,
|
||||
const nsAString& aMedia,
|
||||
Document* aDocument) {
|
||||
nsContentPolicyType policyType = Link::AsValueToContentPolicy(aAs);
|
||||
nsContentPolicyType policyType = AsValueToContentPolicy(aAs);
|
||||
if (policyType == nsIContentPolicy::TYPE_INVALID) {
|
||||
return false;
|
||||
}
|
||||
|
@ -120,6 +120,10 @@ class HTMLLinkElement final : public nsGenericHTMLElement,
|
||||
void SetAs(const nsAString& aAs, ErrorResult& aRv) {
|
||||
SetAttr(nsGkAtoms::as, aAs, aRv);
|
||||
}
|
||||
|
||||
static void ParseAsValue(const nsAString& aValue, nsAttrValue& aResult);
|
||||
static nsContentPolicyType AsValueToContentPolicy(const nsAttrValue& aValue);
|
||||
|
||||
nsDOMTokenList* Sizes() { return GetTokenList(nsGkAtoms::sizes); }
|
||||
void GetType(DOMString& aValue) { GetHTMLAttr(nsGkAtoms::type, aValue); }
|
||||
void SetType(const nsAString& aType, ErrorResult& aRv) {
|
||||
@ -167,6 +171,14 @@ class HTMLLinkElement final : public nsGenericHTMLElement,
|
||||
protected:
|
||||
virtual ~HTMLLinkElement();
|
||||
|
||||
void GetContentPolicyMimeTypeMedia(nsAttrValue& aAsAttr,
|
||||
nsContentPolicyType& aPolicyType,
|
||||
nsString& aMimeType, nsAString& aMedia);
|
||||
void TryDNSPrefetchOrPreconnectOrPrefetchOrPreloadOrPrerender();
|
||||
void UpdatePreload(nsAtom* aName, const nsAttrValue* aValue,
|
||||
const nsAttrValue* aOldValue);
|
||||
void CancelPrefetchOrPreload();
|
||||
|
||||
// nsStyleLinkElement
|
||||
Maybe<SheetInfo> GetStyleSheetInfo() final;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user