2015-05-03 22:32:37 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2008-11-04 18:22:04 +03:00
|
|
|
|
2010-07-26 22:49:09 +04:00
|
|
|
#include "base/basictypes.h"
|
|
|
|
#include "mozilla/net/NeckoCommon.h"
|
|
|
|
#include "mozilla/net/NeckoChild.h"
|
2010-08-21 07:06:53 +04:00
|
|
|
#include "nsURLHelper.h"
|
2010-07-26 22:49:09 +04:00
|
|
|
|
2008-11-04 18:22:04 +03:00
|
|
|
#include "nsHTMLDNSPrefetch.h"
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsString.h"
|
|
|
|
|
|
|
|
#include "nsNetUtil.h"
|
2015-07-07 05:17:00 +03:00
|
|
|
#include "nsNetCID.h"
|
|
|
|
#include "nsIProtocolHandler.h"
|
2008-11-04 18:22:04 +03:00
|
|
|
|
|
|
|
#include "nsIDNSListener.h"
|
2008-12-05 23:53:24 +03:00
|
|
|
#include "nsIWebProgressListener.h"
|
|
|
|
#include "nsIWebProgress.h"
|
2008-11-04 18:22:04 +03:00
|
|
|
#include "nsIDNSRecord.h"
|
|
|
|
#include "nsIDNSService.h"
|
|
|
|
#include "nsICancelable.h"
|
|
|
|
#include "nsGkAtoms.h"
|
2019-01-02 16:05:23 +03:00
|
|
|
#include "mozilla/dom/Document.h"
|
2008-12-05 23:53:24 +03:00
|
|
|
#include "nsThreadUtils.h"
|
2009-01-12 18:36:55 +03:00
|
|
|
#include "nsITimer.h"
|
|
|
|
#include "nsIObserverService.h"
|
2010-02-24 19:37:03 +03:00
|
|
|
#include "mozilla/dom/Link.h"
|
|
|
|
|
2018-12-17 04:27:58 +03:00
|
|
|
#include "mozilla/Components.h"
|
2011-05-25 10:31:59 +04:00
|
|
|
#include "mozilla/Preferences.h"
|
|
|
|
|
|
|
|
using namespace mozilla;
|
2010-02-24 19:37:03 +03:00
|
|
|
using namespace mozilla::dom;
|
2010-08-21 03:24:40 +04:00
|
|
|
using namespace mozilla::net;
|
2008-11-04 18:22:04 +03:00
|
|
|
|
|
|
|
static NS_DEFINE_CID(kDNSServiceCID, NS_DNSSERVICE_CID);
|
2011-09-29 10:19:26 +04:00
|
|
|
static bool sInitialized = false;
|
2019-05-01 11:47:10 +03:00
|
|
|
static nsIDNSService* sDNSService = nullptr;
|
|
|
|
static nsHTMLDNSPrefetch::nsDeferrals* sPrefetches = nullptr;
|
|
|
|
static nsHTMLDNSPrefetch::nsListener* sDNSListener = nullptr;
|
2018-09-22 23:54:11 +03:00
|
|
|
bool sEsniEnabled;
|
2008-11-04 18:22:04 +03:00
|
|
|
|
|
|
|
nsresult nsHTMLDNSPrefetch::Initialize() {
|
|
|
|
if (sInitialized) {
|
|
|
|
NS_WARNING("Initialize() called twice");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2008-12-05 23:53:24 +03:00
|
|
|
sPrefetches = new nsHTMLDNSPrefetch::nsDeferrals();
|
|
|
|
NS_ADDREF(sPrefetches);
|
2009-01-12 18:38:32 +03:00
|
|
|
|
|
|
|
sDNSListener = new nsHTMLDNSPrefetch::nsListener();
|
|
|
|
NS_ADDREF(sDNSListener);
|
|
|
|
|
2008-12-05 23:53:24 +03:00
|
|
|
sPrefetches->Activate();
|
|
|
|
|
2018-09-22 23:54:11 +03:00
|
|
|
Preferences::AddBoolVarCache(&sEsniEnabled, "network.security.esni.enabled");
|
|
|
|
|
|
|
|
sEsniEnabled = Preferences::GetBool("network.security.esni.enabled", false);
|
|
|
|
|
2008-11-04 18:22:04 +03:00
|
|
|
NS_IF_RELEASE(sDNSService);
|
|
|
|
nsresult rv;
|
|
|
|
rv = CallGetService(kDNSServiceCID, &sDNSService);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2010-08-21 03:24:40 +04:00
|
|
|
if (IsNeckoChild()) NeckoChild::InitNeckoChild();
|
2010-08-11 04:20:17 +04:00
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
sInitialized = true;
|
2008-11-04 18:22:04 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult nsHTMLDNSPrefetch::Shutdown() {
|
|
|
|
if (!sInitialized) {
|
|
|
|
NS_WARNING("Not Initialized");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2011-10-17 18:59:28 +04:00
|
|
|
sInitialized = false;
|
2008-11-04 18:22:04 +03:00
|
|
|
NS_IF_RELEASE(sDNSService);
|
2008-12-05 23:53:24 +03:00
|
|
|
NS_IF_RELEASE(sPrefetches);
|
2009-01-12 18:38:32 +03:00
|
|
|
NS_IF_RELEASE(sDNSListener);
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2008-12-05 23:53:24 +03:00
|
|
|
return NS_OK;
|
2008-11-04 18:22:04 +03:00
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
bool nsHTMLDNSPrefetch::IsAllowed(Document* aDocument) {
|
2008-11-04 18:22:04 +03:00
|
|
|
// There is no need to do prefetch on non UI scenarios such as XMLHttpRequest.
|
2009-05-17 18:22:54 +04:00
|
|
|
return aDocument->IsDNSPrefetchAllowed() && aDocument->GetWindow();
|
2008-11-04 18:22:04 +03:00
|
|
|
}
|
|
|
|
|
2019-11-11 13:11:36 +03:00
|
|
|
nsresult nsHTMLDNSPrefetch::Prefetch(Link* aElement, uint16_t flags) {
|
2009-01-12 18:38:32 +03:00
|
|
|
if (!(sInitialized && sPrefetches && sDNSService && sDNSListener))
|
2008-11-04 18:22:04 +03:00
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
|
2009-01-12 18:36:55 +03:00
|
|
|
return sPrefetches->Add(flags, aElement);
|
2008-12-05 23:53:24 +03:00
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
nsresult nsHTMLDNSPrefetch::PrefetchLow(Link* aElement) {
|
2009-01-12 18:36:55 +03:00
|
|
|
return Prefetch(aElement, nsIDNSService::RESOLVE_PRIORITY_LOW);
|
2008-12-05 23:53:24 +03:00
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
nsresult nsHTMLDNSPrefetch::PrefetchMedium(Link* aElement) {
|
2009-01-12 18:36:55 +03:00
|
|
|
return Prefetch(aElement, nsIDNSService::RESOLVE_PRIORITY_MEDIUM);
|
2008-12-05 23:53:24 +03:00
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
nsresult nsHTMLDNSPrefetch::PrefetchHigh(Link* aElement) {
|
2009-01-12 18:36:55 +03:00
|
|
|
return Prefetch(aElement, 0);
|
2008-12-05 23:53:24 +03:00
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
nsresult nsHTMLDNSPrefetch::Prefetch(const nsAString& hostname, bool isHttps,
|
|
|
|
const OriginAttributes& aOriginAttributes,
|
2019-11-11 13:11:36 +03:00
|
|
|
uint16_t flags) {
|
2010-08-21 03:24:40 +04:00
|
|
|
if (IsNeckoChild()) {
|
|
|
|
// We need to check IsEmpty() because net_IsValidHostName()
|
|
|
|
// considers empty strings to be valid hostnames
|
|
|
|
if (!hostname.IsEmpty() &&
|
|
|
|
net_IsValidHostName(NS_ConvertUTF16toUTF8(hostname))) {
|
2015-12-24 04:00:54 +03:00
|
|
|
// during shutdown gNeckoChild might be null
|
|
|
|
if (gNeckoChild) {
|
2018-09-22 23:54:11 +03:00
|
|
|
gNeckoChild->SendHTMLDNSPrefetch(nsString(hostname), isHttps,
|
2017-02-15 05:39:40 +03:00
|
|
|
aOriginAttributes, flags);
|
2015-12-24 04:00:54 +03:00
|
|
|
}
|
2010-08-21 03:24:40 +04:00
|
|
|
}
|
2010-07-26 22:49:09 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2009-01-12 18:38:32 +03:00
|
|
|
if (!(sInitialized && sDNSService && sPrefetches && sDNSListener))
|
2008-11-04 18:22:04 +03:00
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
2008-12-05 23:53:24 +03:00
|
|
|
|
2008-11-04 18:22:04 +03:00
|
|
|
nsCOMPtr<nsICancelable> tmpOutstanding;
|
2018-09-22 23:54:11 +03:00
|
|
|
nsresult rv = sDNSService->AsyncResolveNative(
|
|
|
|
NS_ConvertUTF16toUTF8(hostname), flags | nsIDNSService::RESOLVE_SPECULATE,
|
|
|
|
sDNSListener, nullptr, aOriginAttributes, getter_AddRefs(tmpOutstanding));
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch ESNI keys if needed.
|
|
|
|
if (isHttps && sEsniEnabled) {
|
|
|
|
nsAutoCString esniHost;
|
|
|
|
esniHost.Append("_esni.");
|
|
|
|
esniHost.Append(NS_ConvertUTF16toUTF8(hostname));
|
|
|
|
Unused << sDNSService->AsyncResolveByTypeNative(
|
|
|
|
esniHost, nsIDNSService::RESOLVE_TYPE_TXT,
|
|
|
|
flags | nsIDNSService::RESOLVE_SPECULATE, sDNSListener, nullptr,
|
|
|
|
aOriginAttributes, getter_AddRefs(tmpOutstanding));
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
2008-12-05 23:53:24 +03:00
|
|
|
}
|
|
|
|
|
2018-09-22 23:54:11 +03:00
|
|
|
nsresult nsHTMLDNSPrefetch::PrefetchLow(
|
2019-05-01 11:47:10 +03:00
|
|
|
const nsAString& hostname, bool isHttps,
|
2019-11-11 13:11:36 +03:00
|
|
|
const OriginAttributes& aOriginAttributes) {
|
2018-09-22 23:54:11 +03:00
|
|
|
return Prefetch(hostname, isHttps, aOriginAttributes,
|
2019-11-11 13:11:36 +03:00
|
|
|
nsIDNSService::RESOLVE_PRIORITY_LOW);
|
2008-11-04 18:22:04 +03:00
|
|
|
}
|
|
|
|
|
2018-09-22 23:54:11 +03:00
|
|
|
nsresult nsHTMLDNSPrefetch::PrefetchMedium(
|
2019-05-01 11:47:10 +03:00
|
|
|
const nsAString& hostname, bool isHttps,
|
2019-11-11 13:11:36 +03:00
|
|
|
const OriginAttributes& aOriginAttributes) {
|
2018-09-22 23:54:11 +03:00
|
|
|
return Prefetch(hostname, isHttps, aOriginAttributes,
|
2019-11-11 13:11:36 +03:00
|
|
|
nsIDNSService::RESOLVE_PRIORITY_MEDIUM);
|
2008-11-04 18:22:04 +03:00
|
|
|
}
|
|
|
|
|
2018-09-22 23:54:11 +03:00
|
|
|
nsresult nsHTMLDNSPrefetch::PrefetchHigh(
|
2019-05-01 11:47:10 +03:00
|
|
|
const nsAString& hostname, bool isHttps,
|
2019-11-11 13:11:36 +03:00
|
|
|
const OriginAttributes& aOriginAttributes) {
|
|
|
|
return Prefetch(hostname, isHttps, aOriginAttributes, 0);
|
2008-12-05 23:53:24 +03:00
|
|
|
}
|
|
|
|
|
2019-11-11 13:11:36 +03:00
|
|
|
nsresult nsHTMLDNSPrefetch::CancelPrefetch(Link* aElement, uint16_t flags,
|
2012-01-21 03:14:46 +04:00
|
|
|
nsresult aReason) {
|
|
|
|
if (!(sInitialized && sPrefetches && sDNSService && sDNSListener))
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
|
|
|
|
nsAutoString hostname;
|
2015-11-23 23:05:26 +03:00
|
|
|
aElement->GetHostname(hostname);
|
2017-02-15 05:39:40 +03:00
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
Element* element = aElement->GetElement();
|
2017-02-15 05:39:40 +03:00
|
|
|
NS_ENSURE_TRUE(element, NS_ERROR_FAILURE);
|
|
|
|
|
2018-09-22 23:54:11 +03:00
|
|
|
nsAutoString protocol;
|
|
|
|
aElement->GetProtocol(protocol);
|
|
|
|
bool isHttps = false;
|
|
|
|
if (protocol.EqualsLiteral("https:")) {
|
|
|
|
isHttps = true;
|
|
|
|
}
|
|
|
|
return CancelPrefetch(hostname, isHttps,
|
2017-02-15 05:39:40 +03:00
|
|
|
element->NodePrincipal()->OriginAttributesRef(), flags,
|
|
|
|
aReason);
|
2012-01-21 03:14:46 +04:00
|
|
|
}
|
|
|
|
|
2018-09-22 23:54:11 +03:00
|
|
|
nsresult nsHTMLDNSPrefetch::CancelPrefetch(
|
2019-05-01 11:47:10 +03:00
|
|
|
const nsAString& hostname, bool isHttps,
|
2019-11-11 13:11:36 +03:00
|
|
|
const OriginAttributes& aOriginAttributes, uint16_t flags,
|
2012-01-21 03:14:46 +04:00
|
|
|
nsresult aReason) {
|
|
|
|
// Forward this request to Necko Parent if we're a child process
|
|
|
|
if (IsNeckoChild()) {
|
|
|
|
// We need to check IsEmpty() because net_IsValidHostName()
|
|
|
|
// considers empty strings to be valid hostnames
|
|
|
|
if (!hostname.IsEmpty() &&
|
|
|
|
net_IsValidHostName(NS_ConvertUTF16toUTF8(hostname))) {
|
2015-12-24 04:00:54 +03:00
|
|
|
// during shutdown gNeckoChild might be null
|
|
|
|
if (gNeckoChild) {
|
2017-02-15 05:39:40 +03:00
|
|
|
gNeckoChild->SendCancelHTMLDNSPrefetch(
|
|
|
|
nsString(hostname), isHttps, aOriginAttributes, flags, aReason);
|
2015-12-24 04:00:54 +03:00
|
|
|
}
|
2012-01-21 03:14:46 +04:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(sInitialized && sDNSService && sPrefetches && sDNSListener))
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
|
|
|
|
// Forward cancellation to DNS service
|
2018-09-22 23:54:11 +03:00
|
|
|
nsresult rv = sDNSService->CancelAsyncResolveNative(
|
|
|
|
NS_ConvertUTF16toUTF8(hostname), flags | nsIDNSService::RESOLVE_SPECULATE,
|
|
|
|
sDNSListener, aReason, aOriginAttributes);
|
|
|
|
// Cancel fetching ESNI keys if needed.
|
|
|
|
if (sEsniEnabled && isHttps) {
|
|
|
|
nsAutoCString esniHost;
|
|
|
|
esniHost.Append("_esni.");
|
|
|
|
esniHost.Append(NS_ConvertUTF16toUTF8(hostname));
|
|
|
|
sDNSService->CancelAsyncResolveByTypeNative(
|
|
|
|
esniHost, nsIDNSService::RESOLVE_TYPE_TXT,
|
|
|
|
flags | nsIDNSService::RESOLVE_SPECULATE, sDNSListener, aReason,
|
|
|
|
aOriginAttributes);
|
|
|
|
}
|
|
|
|
return rv;
|
2012-01-21 03:14:46 +04:00
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
nsresult nsHTMLDNSPrefetch::CancelPrefetchLow(Link* aElement,
|
2012-01-21 03:14:46 +04:00
|
|
|
nsresult aReason) {
|
2019-11-11 13:11:36 +03:00
|
|
|
return CancelPrefetch(aElement, nsIDNSService::RESOLVE_PRIORITY_LOW, aReason);
|
2012-01-21 03:14:46 +04:00
|
|
|
}
|
|
|
|
|
2018-09-22 23:54:11 +03:00
|
|
|
nsresult nsHTMLDNSPrefetch::CancelPrefetchLow(
|
2019-05-01 11:47:10 +03:00
|
|
|
const nsAString& hostname, bool isHttps,
|
2019-11-11 13:11:36 +03:00
|
|
|
const OriginAttributes& aOriginAttributes, nsresult aReason) {
|
2018-09-22 23:54:11 +03:00
|
|
|
return CancelPrefetch(hostname, isHttps, aOriginAttributes,
|
2019-11-11 13:11:36 +03:00
|
|
|
nsIDNSService::RESOLVE_PRIORITY_LOW, aReason);
|
2012-01-21 03:14:46 +04:00
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
void nsHTMLDNSPrefetch::LinkDestroyed(Link* aLink) {
|
2017-04-19 13:06:36 +03:00
|
|
|
MOZ_ASSERT(aLink->IsInDNSPrefetch());
|
|
|
|
if (sPrefetches) {
|
|
|
|
// Clean up all the possible links at once.
|
|
|
|
sPrefetches->RemoveUnboundLinks();
|
|
|
|
}
|
|
|
|
}
|
2012-01-21 03:14:46 +04:00
|
|
|
|
2009-01-12 18:38:32 +03:00
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(nsHTMLDNSPrefetch::nsListener, nsIDNSListener)
|
2009-01-12 18:38:32 +03:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2019-05-01 11:47:10 +03:00
|
|
|
nsHTMLDNSPrefetch::nsListener::OnLookupComplete(nsICancelable* request,
|
|
|
|
nsIDNSRecord* rec,
|
2009-01-12 18:38:32 +03:00
|
|
|
nsresult status) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2008-12-05 23:53:24 +03:00
|
|
|
|
2018-09-22 23:54:11 +03:00
|
|
|
NS_IMETHODIMP
|
2019-05-01 11:47:10 +03:00
|
|
|
nsHTMLDNSPrefetch::nsListener::OnLookupByTypeComplete(nsICancelable* request,
|
|
|
|
nsIDNSByTypeRecord* res,
|
2018-09-22 23:54:11 +03:00
|
|
|
nsresult status) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2008-12-05 23:53:24 +03:00
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
nsHTMLDNSPrefetch::nsDeferrals::nsDeferrals()
|
2009-01-12 18:36:55 +03:00
|
|
|
: mHead(0), mTail(0), mActiveLoaderCount(0), mTimerArmed(false) {
|
2017-10-16 09:15:40 +03:00
|
|
|
mTimer = NS_NewTimer();
|
2018-11-30 13:46:48 +03:00
|
|
|
;
|
2008-11-04 18:22:04 +03:00
|
|
|
}
|
|
|
|
|
2008-12-11 22:16:00 +03:00
|
|
|
nsHTMLDNSPrefetch::nsDeferrals::~nsDeferrals() {
|
2009-01-12 18:36:55 +03:00
|
|
|
if (mTimerArmed) {
|
2011-10-17 18:59:28 +04:00
|
|
|
mTimerArmed = false;
|
2009-01-12 18:36:55 +03:00
|
|
|
mTimer->Cancel();
|
2008-12-11 22:16:00 +03:00
|
|
|
}
|
2009-01-12 18:36:55 +03:00
|
|
|
|
|
|
|
Flush();
|
2008-12-11 22:16:00 +03:00
|
|
|
}
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(nsHTMLDNSPrefetch::nsDeferrals, nsIWebProgressListener,
|
|
|
|
nsISupportsWeakReference, nsIObserver)
|
2009-01-12 18:36:55 +03:00
|
|
|
|
|
|
|
void nsHTMLDNSPrefetch::nsDeferrals::Flush() {
|
|
|
|
while (mHead != mTail) {
|
2017-04-19 13:06:36 +03:00
|
|
|
if (mEntries[mTail].mElement) {
|
|
|
|
mEntries[mTail].mElement->ClearIsInDNSPrefetch();
|
|
|
|
}
|
2012-07-30 18:20:58 +04:00
|
|
|
mEntries[mTail].mElement = nullptr;
|
2009-01-12 18:36:55 +03:00
|
|
|
mTail = (mTail + 1) & sMaxDeferredMask;
|
|
|
|
}
|
|
|
|
}
|
2008-12-05 23:53:24 +03:00
|
|
|
|
2019-11-11 13:11:36 +03:00
|
|
|
nsresult nsHTMLDNSPrefetch::nsDeferrals::Add(uint16_t flags, Link* aElement) {
|
2008-12-05 23:53:24 +03:00
|
|
|
// The FIFO has no lock, so it can only be accessed on main thread
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "nsDeferrals::Add must be on main thread");
|
|
|
|
|
2012-01-21 03:14:46 +04:00
|
|
|
aElement->OnDNSPrefetchDeferred();
|
|
|
|
|
2008-12-05 23:53:24 +03:00
|
|
|
if (((mHead + 1) & sMaxDeferredMask) == mTail)
|
|
|
|
return NS_ERROR_DNS_LOOKUP_QUEUE_FULL;
|
2017-04-19 13:06:36 +03:00
|
|
|
|
|
|
|
aElement->SetIsInDNSPrefetch();
|
2008-12-05 23:53:24 +03:00
|
|
|
mEntries[mHead].mFlags = flags;
|
2017-04-19 13:06:36 +03:00
|
|
|
mEntries[mHead].mElement = aElement;
|
2008-12-05 23:53:24 +03:00
|
|
|
mHead = (mHead + 1) & sMaxDeferredMask;
|
|
|
|
|
2009-01-12 18:36:55 +03:00
|
|
|
if (!mActiveLoaderCount && !mTimerArmed && mTimer) {
|
2011-10-17 18:59:28 +04:00
|
|
|
mTimerArmed = true;
|
2017-02-14 02:45:23 +03:00
|
|
|
mTimer->InitWithNamedFuncCallback(Tick, this, 2000, nsITimer::TYPE_ONE_SHOT,
|
|
|
|
"nsHTMLDNSPrefetch::nsDeferrals::Tick");
|
2009-01-12 18:36:55 +03:00
|
|
|
}
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2008-12-05 23:53:24 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void nsHTMLDNSPrefetch::nsDeferrals::SubmitQueue() {
|
2009-01-12 18:36:55 +03:00
|
|
|
NS_ASSERTION(NS_IsMainThread(),
|
|
|
|
"nsDeferrals::SubmitQueue must be on main thread");
|
2008-12-05 23:53:24 +03:00
|
|
|
nsCString hostName;
|
|
|
|
if (!sDNSService) return;
|
|
|
|
|
|
|
|
while (mHead != mTail) {
|
2017-04-19 13:06:36 +03:00
|
|
|
nsCOMPtr<Link> link = mEntries[mTail].mElement;
|
|
|
|
if (link) {
|
|
|
|
link->ClearIsInDNSPrefetch();
|
2012-01-21 03:14:46 +04:00
|
|
|
// Only prefetch here if request was deferred and deferral not cancelled
|
|
|
|
if (link && link->HasDeferredDNSPrefetchRequest()) {
|
2012-07-30 18:20:58 +04:00
|
|
|
nsCOMPtr<nsIURI> hrefURI(link ? link->GetURI() : nullptr);
|
2015-02-12 17:21:00 +03:00
|
|
|
bool isLocalResource = false;
|
2016-05-02 23:30:36 +03:00
|
|
|
nsresult rv = NS_OK;
|
2019-05-01 11:47:10 +03:00
|
|
|
Element* element = link->GetElement();
|
2015-02-12 17:21:00 +03:00
|
|
|
|
|
|
|
hostName.Truncate();
|
2018-09-22 23:54:11 +03:00
|
|
|
bool isHttps = false;
|
2015-02-12 17:21:00 +03:00
|
|
|
if (hrefURI) {
|
2012-01-21 03:14:46 +04:00
|
|
|
hrefURI->GetAsciiHost(hostName);
|
2015-02-12 17:21:00 +03:00
|
|
|
rv = NS_URIChainHasFlags(hrefURI,
|
|
|
|
nsIProtocolHandler::URI_IS_LOCAL_RESOURCE,
|
|
|
|
&isLocalResource);
|
2019-08-02 11:54:18 +03:00
|
|
|
isHttps = hrefURI->SchemeIs("https");
|
2015-02-12 17:21:00 +03:00
|
|
|
}
|
2012-01-21 03:14:46 +04:00
|
|
|
|
2017-02-15 05:39:40 +03:00
|
|
|
if (!hostName.IsEmpty() && NS_SUCCEEDED(rv) && !isLocalResource &&
|
|
|
|
element) {
|
2012-01-21 03:14:46 +04:00
|
|
|
if (IsNeckoChild()) {
|
2015-12-24 04:00:54 +03:00
|
|
|
// during shutdown gNeckoChild might be null
|
|
|
|
if (gNeckoChild) {
|
|
|
|
gNeckoChild->SendHTMLDNSPrefetch(
|
|
|
|
NS_ConvertUTF8toUTF16(hostName), isHttps,
|
2017-02-15 05:39:40 +03:00
|
|
|
element->NodePrincipal()->OriginAttributesRef(),
|
2015-12-24 04:00:54 +03:00
|
|
|
mEntries[mTail].mFlags);
|
|
|
|
}
|
2012-01-21 03:14:46 +04:00
|
|
|
} else {
|
|
|
|
nsCOMPtr<nsICancelable> tmpOutstanding;
|
|
|
|
|
2017-02-15 05:39:40 +03:00
|
|
|
rv = sDNSService->AsyncResolveNative(
|
|
|
|
hostName,
|
|
|
|
mEntries[mTail].mFlags | nsIDNSService::RESOLVE_SPECULATE,
|
|
|
|
sDNSListener, nullptr,
|
|
|
|
element->NodePrincipal()->OriginAttributesRef(),
|
|
|
|
getter_AddRefs(tmpOutstanding));
|
2018-09-22 23:54:11 +03:00
|
|
|
// Fetch ESNI keys if needed.
|
|
|
|
if (NS_SUCCEEDED(rv) && sEsniEnabled && isHttps) {
|
|
|
|
nsAutoCString esniHost;
|
|
|
|
esniHost.Append("_esni.");
|
|
|
|
esniHost.Append(hostName);
|
|
|
|
sDNSService->AsyncResolveByTypeNative(
|
|
|
|
esniHost, nsIDNSService::RESOLVE_TYPE_TXT,
|
|
|
|
mEntries[mTail].mFlags | nsIDNSService::RESOLVE_SPECULATE,
|
|
|
|
sDNSListener, nullptr,
|
|
|
|
element->NodePrincipal()->OriginAttributesRef(),
|
|
|
|
getter_AddRefs(tmpOutstanding));
|
|
|
|
}
|
2012-01-21 03:14:46 +04:00
|
|
|
// Tell link that deferred prefetch was requested
|
|
|
|
if (NS_SUCCEEDED(rv)) link->OnDNSPrefetchRequested();
|
|
|
|
}
|
2012-01-14 10:21:08 +04:00
|
|
|
}
|
2009-01-16 22:21:32 +03:00
|
|
|
}
|
2008-12-05 23:53:24 +03:00
|
|
|
}
|
2017-02-15 05:39:40 +03:00
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
mEntries[mTail].mElement = nullptr;
|
2008-12-05 23:53:24 +03:00
|
|
|
mTail = (mTail + 1) & sMaxDeferredMask;
|
|
|
|
}
|
2017-02-15 05:39:40 +03:00
|
|
|
|
2009-01-12 18:36:55 +03:00
|
|
|
if (mTimerArmed) {
|
2011-10-17 18:59:28 +04:00
|
|
|
mTimerArmed = false;
|
2009-01-12 18:36:55 +03:00
|
|
|
mTimer->Cancel();
|
|
|
|
}
|
2008-12-05 23:53:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void nsHTMLDNSPrefetch::nsDeferrals::Activate() {
|
2017-07-06 15:00:35 +03:00
|
|
|
// Register as an observer for the document loader
|
2018-12-17 04:27:58 +03:00
|
|
|
nsCOMPtr<nsIWebProgress> progress = components::DocLoader::Service();
|
2008-12-05 23:53:24 +03:00
|
|
|
if (progress)
|
|
|
|
progress->AddProgressListener(this, nsIWebProgress::NOTIFY_STATE_DOCUMENT);
|
2009-01-12 18:36:55 +03:00
|
|
|
|
|
|
|
// Register as an observer for xpcom shutdown events so we can drop any
|
|
|
|
// element refs
|
|
|
|
nsCOMPtr<nsIObserverService> observerService =
|
Bug 560095 - Use mozilla::services::GetObserverService(). r=biesi,dveditz,gavin,josh,jst,mrbkap,roc,sdwilsh,shaver,sicking,smontagu,surkov
2010-04-29 20:59:13 +04:00
|
|
|
mozilla::services::GetObserverService();
|
|
|
|
if (observerService)
|
2011-10-17 18:59:28 +04:00
|
|
|
observerService->AddObserver(this, "xpcom-shutdown", true);
|
2009-01-12 18:36:55 +03:00
|
|
|
}
|
|
|
|
|
2017-04-19 13:06:36 +03:00
|
|
|
void nsHTMLDNSPrefetch::nsDeferrals::RemoveUnboundLinks() {
|
|
|
|
uint16_t tail = mTail;
|
|
|
|
while (mHead != tail) {
|
|
|
|
if (mEntries[tail].mElement &&
|
|
|
|
!mEntries[tail].mElement->GetElement()->IsInComposedDoc()) {
|
|
|
|
mEntries[tail].mElement->ClearIsInDNSPrefetch();
|
|
|
|
mEntries[tail].mElement = nullptr;
|
|
|
|
}
|
|
|
|
tail = (tail + 1) & sMaxDeferredMask;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-12 18:36:55 +03:00
|
|
|
// nsITimer related method
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
void nsHTMLDNSPrefetch::nsDeferrals::Tick(nsITimer* aTimer, void* aClosure) {
|
|
|
|
nsHTMLDNSPrefetch::nsDeferrals* self =
|
|
|
|
(nsHTMLDNSPrefetch::nsDeferrals*)aClosure;
|
2009-01-12 18:36:55 +03:00
|
|
|
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "nsDeferrals::Tick must be on main thread");
|
|
|
|
NS_ASSERTION(self->mTimerArmed, "Timer is not armed");
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
self->mTimerArmed = false;
|
2009-01-12 18:36:55 +03:00
|
|
|
|
|
|
|
// If the queue is not submitted here because there are outstanding pages
|
2017-07-06 15:00:35 +03:00
|
|
|
// being loaded, there is no need to rearm the timer as the queue will be
|
|
|
|
// submtited when those loads complete.
|
|
|
|
if (!self->mActiveLoaderCount) self->SubmitQueue();
|
2008-12-05 23:53:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
//////////// nsIWebProgressListener methods
|
|
|
|
|
2017-07-06 15:00:35 +03:00
|
|
|
NS_IMETHODIMP
|
2019-05-01 11:47:10 +03:00
|
|
|
nsHTMLDNSPrefetch::nsDeferrals::OnStateChange(nsIWebProgress* aWebProgress,
|
|
|
|
nsIRequest* aRequest,
|
2017-07-06 15:00:35 +03:00
|
|
|
uint32_t progressStateFlags,
|
2008-12-05 23:53:24 +03:00
|
|
|
nsresult aStatus) {
|
|
|
|
// The FIFO has no lock, so it can only be accessed on main thread
|
|
|
|
NS_ASSERTION(NS_IsMainThread(),
|
|
|
|
"nsDeferrals::OnStateChange must be on main thread");
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2008-12-05 23:53:24 +03:00
|
|
|
if (progressStateFlags & STATE_IS_DOCUMENT) {
|
|
|
|
if (progressStateFlags & STATE_STOP) {
|
|
|
|
// Initialization may have missed a STATE_START notification, so do
|
|
|
|
// not go negative
|
|
|
|
if (mActiveLoaderCount) mActiveLoaderCount--;
|
|
|
|
|
|
|
|
if (!mActiveLoaderCount) SubmitQueue();
|
|
|
|
} else if (progressStateFlags & STATE_START)
|
|
|
|
mActiveLoaderCount++;
|
|
|
|
}
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2008-12-05 23:53:24 +03:00
|
|
|
return NS_OK;
|
2008-11-04 18:22:04 +03:00
|
|
|
}
|
|
|
|
|
2008-12-05 23:53:24 +03:00
|
|
|
NS_IMETHODIMP
|
2019-05-01 11:47:10 +03:00
|
|
|
nsHTMLDNSPrefetch::nsDeferrals::OnProgressChange(nsIWebProgress* aProgress,
|
|
|
|
nsIRequest* aRequest,
|
2017-07-06 15:00:35 +03:00
|
|
|
int32_t curSelfProgress,
|
|
|
|
int32_t maxSelfProgress,
|
|
|
|
int32_t curTotalProgress,
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t maxTotalProgress) {
|
2008-12-05 23:53:24 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2008-11-04 18:22:04 +03:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2019-05-01 11:47:10 +03:00
|
|
|
nsHTMLDNSPrefetch::nsDeferrals::OnLocationChange(nsIWebProgress* aWebProgress,
|
|
|
|
nsIRequest* aRequest,
|
|
|
|
nsIURI* location,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t aFlags) {
|
2008-12-05 23:53:24 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2017-07-06 15:00:35 +03:00
|
|
|
NS_IMETHODIMP
|
2019-05-01 11:47:10 +03:00
|
|
|
nsHTMLDNSPrefetch::nsDeferrals::OnStatusChange(nsIWebProgress* aWebProgress,
|
|
|
|
nsIRequest* aRequest,
|
2008-12-05 23:53:24 +03:00
|
|
|
nsresult aStatus,
|
2019-05-01 11:47:10 +03:00
|
|
|
const char16_t* aMessage) {
|
2008-12-05 23:53:24 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2017-07-06 15:00:35 +03:00
|
|
|
NS_IMETHODIMP
|
2019-05-01 11:47:10 +03:00
|
|
|
nsHTMLDNSPrefetch::nsDeferrals::OnSecurityChange(nsIWebProgress* aWebProgress,
|
|
|
|
nsIRequest* aRequest,
|
2019-01-07 01:45:57 +03:00
|
|
|
uint32_t aState) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHTMLDNSPrefetch::nsDeferrals::OnContentBlockingEvent(
|
2019-05-01 11:47:10 +03:00
|
|
|
nsIWebProgress* aWebProgress, nsIRequest* aRequest, uint32_t aEvent) {
|
2008-11-04 18:22:04 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2009-01-12 18:36:55 +03:00
|
|
|
|
|
|
|
//////////// nsIObserver method
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2019-05-01 11:47:10 +03:00
|
|
|
nsHTMLDNSPrefetch::nsDeferrals::Observe(nsISupports* subject, const char* topic,
|
|
|
|
const char16_t* data) {
|
2009-01-12 18:36:55 +03:00
|
|
|
if (!strcmp(topic, "xpcom-shutdown")) Flush();
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2009-01-12 18:36:55 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|