Bug 1625151 - P2: Lazy initialize nsHTMLDNSPrefetch::sDNSService r=dragana

Differential Revision: https://phabricator.services.mozilla.com/D68389

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Kershaw Chang 2020-04-13 08:31:09 +00:00
Родитель 2fc9815db5
Коммит 9bfcce4383
1 изменённых файлов: 22 добавлений и 10 удалений

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

@ -62,11 +62,6 @@ nsresult nsHTMLDNSPrefetch::Initialize() {
sEsniEnabled = Preferences::GetBool("network.security.esni.enabled", false); sEsniEnabled = Preferences::GetBool("network.security.esni.enabled", false);
NS_IF_RELEASE(sDNSService);
nsresult rv;
rv = CallGetService(kDNSServiceCID, &sDNSService);
if (NS_FAILED(rv)) return rv;
if (IsNeckoChild()) NeckoChild::InitNeckoChild(); if (IsNeckoChild()) NeckoChild::InitNeckoChild();
sInitialized = true; sInitialized = true;
@ -86,6 +81,21 @@ nsresult nsHTMLDNSPrefetch::Shutdown() {
return NS_OK; return NS_OK;
} }
static bool EnsureDNSService() {
if (sDNSService) {
return true;
}
NS_IF_RELEASE(sDNSService);
nsresult rv;
rv = CallGetService(kDNSServiceCID, &sDNSService);
if (NS_FAILED(rv)) {
return false;
}
return !!sDNSService;
}
bool nsHTMLDNSPrefetch::IsAllowed(Document* aDocument) { bool nsHTMLDNSPrefetch::IsAllowed(Document* aDocument) {
// There is no need to do prefetch on non UI scenarios such as XMLHttpRequest. // There is no need to do prefetch on non UI scenarios such as XMLHttpRequest.
return aDocument->IsDNSPrefetchAllowed() && aDocument->GetWindow(); return aDocument->IsDNSPrefetchAllowed() && aDocument->GetWindow();
@ -102,7 +112,7 @@ static uint32_t GetDNSFlagsFromLink(Link* aElement) {
} }
nsresult nsHTMLDNSPrefetch::Prefetch(Link* aElement, uint32_t flags) { nsresult nsHTMLDNSPrefetch::Prefetch(Link* aElement, uint32_t flags) {
if (!(sInitialized && sPrefetches && sDNSService && sDNSListener)) if (!(sInitialized && sPrefetches && sDNSListener) || !EnsureDNSService())
return NS_ERROR_NOT_AVAILABLE; return NS_ERROR_NOT_AVAILABLE;
flags |= GetDNSFlagsFromLink(aElement); flags |= GetDNSFlagsFromLink(aElement);
@ -138,7 +148,7 @@ nsresult nsHTMLDNSPrefetch::Prefetch(const nsAString& hostname, bool isHttps,
return NS_OK; return NS_OK;
} }
if (!(sInitialized && sDNSService && sPrefetches && sDNSListener)) if (!(sInitialized && sPrefetches && sDNSListener) || !EnsureDNSService())
return NS_ERROR_NOT_AVAILABLE; return NS_ERROR_NOT_AVAILABLE;
nsCOMPtr<nsICancelable> tmpOutstanding; nsCOMPtr<nsICancelable> tmpOutstanding;
@ -188,7 +198,7 @@ nsresult nsHTMLDNSPrefetch::PrefetchHigh(
nsresult nsHTMLDNSPrefetch::CancelPrefetch(Link* aElement, uint32_t flags, nsresult nsHTMLDNSPrefetch::CancelPrefetch(Link* aElement, uint32_t flags,
nsresult aReason) { nsresult aReason) {
if (!(sInitialized && sPrefetches && sDNSService && sDNSListener)) if (!(sInitialized && sPrefetches && sDNSListener) || !EnsureDNSService())
return NS_ERROR_NOT_AVAILABLE; return NS_ERROR_NOT_AVAILABLE;
nsAutoString hostname; nsAutoString hostname;
@ -227,7 +237,7 @@ nsresult nsHTMLDNSPrefetch::CancelPrefetch(
return NS_OK; return NS_OK;
} }
if (!(sInitialized && sDNSService && sPrefetches && sDNSListener)) if (!(sInitialized && sPrefetches && sDNSListener) || !EnsureDNSService())
return NS_ERROR_NOT_AVAILABLE; return NS_ERROR_NOT_AVAILABLE;
// Forward cancellation to DNS service // Forward cancellation to DNS service
@ -341,7 +351,9 @@ void nsHTMLDNSPrefetch::nsDeferrals::SubmitQueue() {
NS_ASSERTION(NS_IsMainThread(), NS_ASSERTION(NS_IsMainThread(),
"nsDeferrals::SubmitQueue must be on main thread"); "nsDeferrals::SubmitQueue must be on main thread");
nsCString hostName; nsCString hostName;
if (!sDNSService) return; if (!EnsureDNSService()) {
return;
}
while (mHead != mTail) { while (mHead != mTail) {
nsCOMPtr<Link> link = mEntries[mTail].mElement; nsCOMPtr<Link> link = mEntries[mTail].mElement;