diff --git a/dom/html/nsHTMLDNSPrefetch.cpp b/dom/html/nsHTMLDNSPrefetch.cpp index 7b0d2d40f688..916b9da92745 100644 --- a/dom/html/nsHTMLDNSPrefetch.cpp +++ b/dom/html/nsHTMLDNSPrefetch.cpp @@ -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 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 = mEntries[mTail].mElement;