Bug 1613873 - P3: Make nsDNSPrefetch support off main thread r=dragana

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

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Kershaw Chang 2020-03-03 09:34:07 +00:00
parent 9cddfd9589
commit 69329b4fc3
2 changed files with 33 additions and 17 deletions

View File

@ -12,25 +12,41 @@
#include "nsIDNSService.h"
#include "nsICancelable.h"
#include "nsIURI.h"
#include "mozilla/Atomics.h"
#include "mozilla/Preferences.h"
static nsIDNSService* sDNSService = nullptr;
static bool sESNIEnabled = false;
static mozilla::Atomic<bool, mozilla::Relaxed> sESNIEnabled(false);
const char kESNIPref[] = "network.security.esni.enabled";
nsresult nsDNSPrefetch::Initialize(nsIDNSService* aDNSService) {
MOZ_ASSERT(NS_IsMainThread());
NS_IF_RELEASE(sDNSService);
sDNSService = aDNSService;
NS_IF_ADDREF(sDNSService);
mozilla::Preferences::AddBoolVarCache(&sESNIEnabled,
"network.security.esni.enabled");
mozilla::Preferences::RegisterCallback(nsDNSPrefetch::PrefChanged, kESNIPref);
PrefChanged(nullptr, nullptr);
return NS_OK;
}
nsresult nsDNSPrefetch::Shutdown() {
NS_IF_RELEASE(sDNSService);
mozilla::Preferences::UnregisterCallback(nsDNSPrefetch::PrefChanged,
kESNIPref);
return NS_OK;
}
// static
void nsDNSPrefetch::PrefChanged(const char* aPref, void* aClosure) {
if (!aPref || strcmp(aPref, kESNIPref) == 0) {
bool enabled = false;
if (NS_SUCCEEDED(mozilla::Preferences::GetBool(kESNIPref, &enabled))) {
sESNIEnabled = enabled;
}
}
}
nsDNSPrefetch::nsDNSPrefetch(nsIURI* aURI,
mozilla::OriginAttributes& aOriginAttributes,
nsIRequest::TRRMode aTRRMode,
@ -44,12 +60,6 @@ nsDNSPrefetch::nsDNSPrefetch(nsIURI* aURI,
}
nsresult nsDNSPrefetch::Prefetch(uint32_t flags) {
// This can work properly only if this call is on the main thread.
// Curenlty we use nsDNSPrefetch only in nsHttpChannel which will call
// PrefetchHigh() from the main thread. Let's add assertion to catch
// if things change.
MOZ_ASSERT(NS_IsMainThread(), "Expecting DNS callback on main thread.");
if (mHostname.IsEmpty()) return NS_ERROR_NOT_AVAILABLE;
if (!sDNSService) return NS_ERROR_NOT_AVAILABLE;
@ -61,12 +71,12 @@ nsresult nsDNSPrefetch::Prefetch(uint32_t flags) {
// then our timing will be useless. However, in such a case,
// mEndTimestamp will be a null timestamp and callers should check
// TimingsValid() before using the timing.
nsCOMPtr<nsIEventTarget> main = mozilla::GetMainThreadEventTarget();
nsCOMPtr<nsIEventTarget> target = mozilla::GetCurrentThreadEventTarget();
flags |= nsIDNSService::GetFlagsFromTRRMode(mTRRMode);
nsresult rv = sDNSService->AsyncResolveNative(
mHostname, flags | nsIDNSService::RESOLVE_SPECULATE, this, main,
mHostname, flags | nsIDNSService::RESOLVE_SPECULATE, this, target,
mOriginAttributes, getter_AddRefs(tmpOutstanding));
if (NS_FAILED(rv)) {
return rv;
@ -79,8 +89,8 @@ nsresult nsDNSPrefetch::Prefetch(uint32_t flags) {
esniHost.Append(mHostname);
sDNSService->AsyncResolveByTypeNative(
esniHost, nsIDNSService::RESOLVE_TYPE_TXT,
flags | nsIDNSService::RESOLVE_SPECULATE, this, main, mOriginAttributes,
getter_AddRefs(tmpOutstanding));
flags | nsIDNSService::RESOLVE_SPECULATE, this, target,
mOriginAttributes, getter_AddRefs(tmpOutstanding));
}
return NS_OK;
}
@ -104,8 +114,6 @@ NS_IMPL_ISUPPORTS(nsDNSPrefetch, nsIDNSListener)
NS_IMETHODIMP
nsDNSPrefetch::OnLookupComplete(nsICancelable* request, nsIDNSRecord* rec,
nsresult status) {
MOZ_ASSERT(NS_IsMainThread(), "Expecting DNS callback on main thread.");
if (mStoreTiming) {
mEndTimestamp = mozilla::TimeStamp::Now();
}
@ -113,6 +121,10 @@ nsDNSPrefetch::OnLookupComplete(nsICancelable* request, nsIDNSRecord* rec,
if (listener) {
listener->OnLookupComplete(request, rec, status);
}
// OnLookupComplete should be called on the target thread, so we release
// mListener here to make sure mListener is also released on the target
// thread.
mListener = nullptr;
return NS_OK;
}
@ -120,8 +132,6 @@ NS_IMETHODIMP
nsDNSPrefetch::OnLookupByTypeComplete(nsICancelable* request,
nsIDNSByTypeRecord* res,
nsresult status) {
MOZ_ASSERT(NS_IsMainThread(), "Expecting DNS callback on main thread.");
if (mStoreTiming) {
mEndTimestamp = mozilla::TimeStamp::Now();
}
@ -129,5 +139,9 @@ nsDNSPrefetch::OnLookupByTypeComplete(nsICancelable* request,
if (listener) {
listener->OnLookupByTypeComplete(request, res, status);
}
// OnLookupByTypeComplete should be called on the target thread, so we release
// mListener here to make sure mListener is also released on the target
// thread.
mListener = nullptr;
return NS_OK;
}

View File

@ -43,6 +43,8 @@ class nsDNSPrefetch final : public nsIDNSListener {
nsresult PrefetchMedium(bool refreshDNS = false);
nsresult PrefetchLow(bool refreshDNS = false);
static void PrefChanged(const char* aPref, void* aClosure);
private:
nsCString mHostname;
bool mIsHttps;