Bug 1687587 - Cleanup a bit the nsHTMLDNSPrefetch code. r=smaug

Introduce a Priority parameter that we use rather than duplicating functions.

Depends on D102366

Differential Revision: https://phabricator.services.mozilla.com/D102367
This commit is contained in:
Emilio Cobos Álvarez 2021-01-20 09:34:27 +00:00
Родитель e1271b87c9
Коммит a70873c588
5 изменённых файлов: 56 добавлений и 78 удалений

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

@ -66,7 +66,7 @@ bool Link::ElementHasHref() const {
void Link::TryDNSPrefetch() {
MOZ_ASSERT(mElement->IsInComposedDoc());
if (ElementHasHref() && nsHTMLDNSPrefetch::IsAllowed(mElement->OwnerDoc())) {
nsHTMLDNSPrefetch::PrefetchLow(this);
nsHTMLDNSPrefetch::Prefetch(this, nsHTMLDNSPrefetch::Priority::Low);
}
}
@ -80,7 +80,8 @@ void Link::CancelDNSPrefetch(nsWrapperCache::FlagsType aDeferredFlag,
mElement->UnsetFlags(aRequestedFlag);
// Possible that hostname could have changed since binding, but since this
// covers common cases, most DNS prefetch requests will be canceled
nsHTMLDNSPrefetch::CancelPrefetchLow(this, NS_ERROR_ABORT);
nsHTMLDNSPrefetch::CancelPrefetch(this, nsHTMLDNSPrefetch::Priority::Low,
NS_ERROR_ABORT);
}
}

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

@ -803,8 +803,9 @@ void nsContentSink::PrefetchDNS(const nsAString& aHref) {
OriginAttributes oa;
StoragePrincipalHelper::GetOriginAttributesForNetworkState(mDocument, oa);
nsHTMLDNSPrefetch::PrefetchLow(hostname, isHttps, oa,
mDocument->GetChannel()->GetTRRMode());
nsHTMLDNSPrefetch::Prefetch(hostname, isHttps, oa,
mDocument->GetChannel()->GetTRRMode(),
nsHTMLDNSPrefetch::Priority::Low);
}
}

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

@ -638,7 +638,7 @@ void HTMLLinkElement::
if (linkTypes & eDNS_PREFETCH) {
if (nsHTMLDNSPrefetch::IsAllowed(OwnerDoc())) {
nsHTMLDNSPrefetch::PrefetchLow(this);
nsHTMLDNSPrefetch::Prefetch(this, nsHTMLDNSPrefetch::Priority::Low);
}
}
}

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

@ -10,6 +10,7 @@
#include "mozilla/dom/Element.h"
#include "mozilla/net/NeckoCommon.h"
#include "mozilla/net/NeckoChild.h"
#include "mozilla/OriginAttributes.h"
#include "mozilla/StoragePrincipalHelper.h"
#include "nsURLHelper.h"
@ -110,24 +111,26 @@ static uint32_t GetDNSFlagsFromLink(Link* aElement) {
return nsIDNSService::GetFlagsFromTRRMode(mode);
}
nsresult nsHTMLDNSPrefetch::Prefetch(Link* aElement, uint32_t flags) {
if (!(sInitialized && sPrefetches && sDNSListener) || !EnsureDNSService())
uint32_t nsHTMLDNSPrefetch::PriorityToDNSServiceFlags(Priority aPriority) {
switch (aPriority) {
case Priority::Low:
return uint32_t(nsIDNSService::RESOLVE_PRIORITY_LOW);
case Priority::Medium:
return uint32_t(nsIDNSService::RESOLVE_PRIORITY_MEDIUM);
case Priority::High:
return 0u;
}
MOZ_ASSERT_UNREACHABLE("Unknown priority");
return 0u;
}
nsresult nsHTMLDNSPrefetch::Prefetch(Link* aElement, Priority aPriority) {
if (!(sInitialized && sPrefetches && sDNSListener) || !EnsureDNSService()) {
return NS_ERROR_NOT_AVAILABLE;
flags |= GetDNSFlagsFromLink(aElement);
return sPrefetches->Add(flags, aElement);
}
nsresult nsHTMLDNSPrefetch::PrefetchLow(Link* aElement) {
return Prefetch(aElement, nsIDNSService::RESOLVE_PRIORITY_LOW);
}
nsresult nsHTMLDNSPrefetch::PrefetchMedium(Link* aElement) {
return Prefetch(aElement, nsIDNSService::RESOLVE_PRIORITY_MEDIUM);
}
nsresult nsHTMLDNSPrefetch::PrefetchHigh(Link* aElement) {
return Prefetch(aElement, 0);
}
return sPrefetches->Add(
GetDNSFlagsFromLink(aElement) | PriorityToDNSServiceFlags(aPriority),
aElement);
}
nsresult nsHTMLDNSPrefetch::Prefetch(
@ -173,36 +176,23 @@ nsresult nsHTMLDNSPrefetch::Prefetch(
return NS_OK;
}
nsresult nsHTMLDNSPrefetch::PrefetchLow(
nsresult nsHTMLDNSPrefetch::Prefetch(
const nsAString& hostname, bool isHttps,
const OriginAttributes& aPartitionedPrincipalOriginAttributes,
nsIRequest::TRRMode aMode) {
nsIRequest::TRRMode aMode, Priority aPriority) {
return Prefetch(hostname, isHttps, aPartitionedPrincipalOriginAttributes,
nsIDNSService::GetFlagsFromTRRMode(aMode) |
nsIDNSService::RESOLVE_PRIORITY_LOW);
PriorityToDNSServiceFlags(aPriority));
}
nsresult nsHTMLDNSPrefetch::PrefetchMedium(
const nsAString& hostname, bool isHttps,
const OriginAttributes& aPartitionedPrincipalOriginAttributes,
nsIRequest::TRRMode aMode) {
return Prefetch(hostname, isHttps, aPartitionedPrincipalOriginAttributes,
nsIDNSService::GetFlagsFromTRRMode(aMode) |
nsIDNSService::RESOLVE_PRIORITY_MEDIUM);
}
nsresult nsHTMLDNSPrefetch::PrefetchHigh(
const nsAString& hostname, bool isHttps,
const OriginAttributes& aPartitionedPrincipalOriginAttributes,
nsIRequest::TRRMode aMode) {
return Prefetch(hostname, isHttps, aPartitionedPrincipalOriginAttributes,
nsIDNSService::GetFlagsFromTRRMode(aMode));
}
nsresult nsHTMLDNSPrefetch::CancelPrefetch(Link* aElement, uint32_t flags,
nsresult nsHTMLDNSPrefetch::CancelPrefetch(Link* aElement, Priority aPriority,
nsresult aReason) {
if (!(sInitialized && sPrefetches && sDNSListener) || !EnsureDNSService())
if (!(sInitialized && sPrefetches && sDNSListener) || !EnsureDNSService()) {
return NS_ERROR_NOT_AVAILABLE;
}
uint32_t flags =
GetDNSFlagsFromLink(aElement) | PriorityToDNSServiceFlags(aPriority);
nsAutoString hostname;
aElement->GetHostname(hostname);
@ -265,22 +255,14 @@ nsresult nsHTMLDNSPrefetch::CancelPrefetch(
return rv;
}
nsresult nsHTMLDNSPrefetch::CancelPrefetchLow(Link* aElement,
nsresult aReason) {
return CancelPrefetch(
aElement,
GetDNSFlagsFromLink(aElement) | nsIDNSService::RESOLVE_PRIORITY_LOW,
aReason);
}
nsresult nsHTMLDNSPrefetch::CancelPrefetchLow(
nsresult nsHTMLDNSPrefetch::CancelPrefetch(
const nsAString& hostname, bool isHttps,
const OriginAttributes& aPartitionedPrincipalOriginAttributes,
nsIRequest::TRRMode aTRRMode, nsresult aReason) {
nsIRequest::TRRMode aTRRMode, Priority aPriority, nsresult aReason) {
return CancelPrefetch(hostname, isHttps,
aPartitionedPrincipalOriginAttributes,
nsIDNSService::GetFlagsFromTRRMode(aTRRMode) |
nsIDNSService::RESOLVE_PRIORITY_LOW,
PriorityToDNSServiceFlags(aPriority),
aReason);
}

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

@ -7,7 +7,6 @@
#ifndef nsHTMLDNSPrefetch_h___
#define nsHTMLDNSPrefetch_h___
#include "mozilla/OriginAttributes.h"
#include "nsCOMPtr.h"
#include "nsIDNSListener.h"
#include "nsIObserver.h"
@ -18,13 +17,14 @@
class nsITimer;
namespace mozilla {
class OriginAttributes;
namespace dom {
class Document;
class Link;
} // namespace dom
} // namespace mozilla
namespace mozilla {
namespace net {
class NeckoParent;
} // namespace net
@ -52,42 +52,36 @@ class nsHTMLDNSPrefetch {
// sure that you pass a partitioned one. See StoragePrincipalHelper.h to know
// more.
static nsresult PrefetchHigh(mozilla::dom::Link* aElement);
static nsresult PrefetchMedium(mozilla::dom::Link* aElement);
static nsresult PrefetchLow(mozilla::dom::Link* aElement);
static nsresult PrefetchLow(
enum class Priority {
Low,
Medium,
High,
};
static nsresult Prefetch(mozilla::dom::Link* aElement, Priority);
static nsresult Prefetch(
const nsAString& host, bool isHttps,
const mozilla::OriginAttributes& aPartitionedPrincipalOriginAttributes,
nsIRequest::TRRMode aTRRMode);
static nsresult PrefetchMedium(
nsIRequest::TRRMode aTRRMode, Priority);
static nsresult CancelPrefetch(
const nsAString& host, bool isHttps,
const mozilla::OriginAttributes& aPartitionedPrincipalOriginAttributes,
nsIRequest::TRRMode aTRRMode);
static nsresult PrefetchHigh(
const nsAString& host, bool isHttps,
const mozilla::OriginAttributes& aPartitionedPrincipalOriginAttributes,
nsIRequest::TRRMode aTRRMode);
static nsresult CancelPrefetchLow(
const nsAString& host, bool isHttps,
const mozilla::OriginAttributes& aPartitionedPrincipalOriginAttributes,
nsIRequest::TRRMode aTRRMode, nsresult aReason);
static nsresult CancelPrefetchLow(mozilla::dom::Link* aElement,
nsresult aReason);
nsIRequest::TRRMode aTRRMode, Priority, nsresult aReason);
static nsresult CancelPrefetch(mozilla::dom::Link* aElement,
Priority, nsresult aReason);
static void LinkDestroyed(mozilla::dom::Link* aLink);
private:
static uint32_t PriorityToDNSServiceFlags(Priority);
static nsresult Prefetch(
const nsAString& host, bool isHttps,
const mozilla::OriginAttributes& aPartitionedPrincipalOriginAttributes,
uint32_t flags);
static nsresult Prefetch(mozilla::dom::Link* aElement, uint32_t flags);
static nsresult CancelPrefetch(
const nsAString& hostname, bool isHttps,
const mozilla::OriginAttributes& aPartitionedPrincipalOriginAttributes,
uint32_t flags, nsresult aReason);
static nsresult CancelPrefetch(mozilla::dom::Link* aElement, uint32_t flags,
nsresult aReason);
public:
class nsListener final : public nsIDNSListener {