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