2008-11-04 18:22:04 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
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
|
|
|
|
|
|
|
#include "nsDNSPrefetch.h"
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsString.h"
|
2013-11-27 22:19:14 +04:00
|
|
|
#include "nsThreadUtils.h"
|
2008-11-04 18:22:04 +03:00
|
|
|
|
|
|
|
#include "nsIDNSListener.h"
|
|
|
|
#include "nsIDNSService.h"
|
|
|
|
#include "nsICancelable.h"
|
2013-09-22 07:04:57 +04:00
|
|
|
#include "nsIURI.h"
|
2018-09-22 23:54:11 +03:00
|
|
|
#include "mozilla/Preferences.h"
|
2008-11-04 18:22:04 +03:00
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
static nsIDNSService *sDNSService = nullptr;
|
2018-09-22 23:54:11 +03:00
|
|
|
static bool sESNIEnabled = false;
|
2008-11-04 18:22:04 +03:00
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsDNSPrefetch::Initialize(nsIDNSService *aDNSService)
|
|
|
|
{
|
|
|
|
NS_IF_RELEASE(sDNSService);
|
|
|
|
sDNSService = aDNSService;
|
|
|
|
NS_IF_ADDREF(sDNSService);
|
2018-09-22 23:54:11 +03:00
|
|
|
mozilla::Preferences::AddBoolVarCache(&sESNIEnabled, "network.security.esni.enabled");
|
2008-11-04 18:22:04 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsDNSPrefetch::Shutdown()
|
|
|
|
{
|
|
|
|
NS_IF_RELEASE(sDNSService);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-11-27 22:19:14 +04:00
|
|
|
nsDNSPrefetch::nsDNSPrefetch(nsIURI *aURI,
|
2017-02-15 12:48:15 +03:00
|
|
|
mozilla::OriginAttributes& aOriginAttributes,
|
2013-11-27 22:19:14 +04:00
|
|
|
nsIDNSListener *aListener,
|
|
|
|
bool storeTiming)
|
2017-02-15 12:48:15 +03:00
|
|
|
: mOriginAttributes(aOriginAttributes)
|
|
|
|
, mStoreTiming(storeTiming)
|
2013-11-27 22:19:14 +04:00
|
|
|
, mListener(do_GetWeakReference(aListener))
|
2008-11-04 18:22:04 +03:00
|
|
|
{
|
|
|
|
aURI->GetAsciiHost(mHostname);
|
2018-09-22 23:54:11 +03:00
|
|
|
mIsHttps = false;
|
|
|
|
aURI->SchemeIs("https", &mIsHttps);
|
2008-11-04 18:22:04 +03:00
|
|
|
}
|
|
|
|
|
2017-07-06 15:00:35 +03:00
|
|
|
nsresult
|
2012-08-22 19:56:38 +04:00
|
|
|
nsDNSPrefetch::Prefetch(uint16_t flags)
|
2008-11-04 18:22:04 +03:00
|
|
|
{
|
2018-09-22 23:54:11 +03:00
|
|
|
// 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.");
|
|
|
|
|
2008-11-04 18:22:04 +03:00
|
|
|
if (mHostname.IsEmpty())
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2008-11-04 18:22:04 +03:00
|
|
|
if (!sDNSService)
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2018-09-21 04:30:10 +03:00
|
|
|
nsCOMPtr<nsICancelable> tmpOutstanding;
|
|
|
|
|
2011-05-23 08:29:42 +04:00
|
|
|
if (mStoreTiming)
|
|
|
|
mStartTimestamp = mozilla::TimeStamp::Now();
|
|
|
|
// If AsyncResolve fails, for example because prefetching is disabled,
|
|
|
|
// 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.
|
2017-06-01 23:43:44 +03:00
|
|
|
nsCOMPtr<nsIEventTarget> main = mozilla::GetMainThreadEventTarget();
|
2018-09-22 23:54:11 +03:00
|
|
|
|
|
|
|
nsresult rv = sDNSService->AsyncResolveNative(mHostname,
|
|
|
|
flags | nsIDNSService::RESOLVE_SPECULATE,
|
|
|
|
this, main, mOriginAttributes,
|
|
|
|
getter_AddRefs(tmpOutstanding));
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch esni keys if needed.
|
|
|
|
if (sESNIEnabled && mIsHttps) {
|
|
|
|
nsAutoCString esniHost;
|
|
|
|
esniHost.Append("_esni.");
|
|
|
|
esniHost.Append(mHostname);
|
|
|
|
sDNSService->AsyncResolveByTypeNative(esniHost, nsIDNSService::RESOLVE_TYPE_TXT,
|
|
|
|
flags | nsIDNSService::RESOLVE_SPECULATE,
|
|
|
|
this, main, mOriginAttributes,
|
|
|
|
getter_AddRefs(tmpOutstanding));
|
|
|
|
}
|
|
|
|
return NS_OK;
|
2008-11-04 18:22:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2013-11-22 01:35:45 +04:00
|
|
|
nsDNSPrefetch::PrefetchLow(bool refreshDNS)
|
2008-11-04 18:22:04 +03:00
|
|
|
{
|
2013-11-22 01:35:45 +04:00
|
|
|
return Prefetch(nsIDNSService::RESOLVE_PRIORITY_LOW |
|
|
|
|
(refreshDNS ? nsIDNSService::RESOLVE_BYPASS_CACHE : 0));
|
2008-11-04 18:22:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2013-11-22 01:35:45 +04:00
|
|
|
nsDNSPrefetch::PrefetchMedium(bool refreshDNS)
|
2008-11-04 18:22:04 +03:00
|
|
|
{
|
2013-11-22 01:35:45 +04:00
|
|
|
return Prefetch(nsIDNSService::RESOLVE_PRIORITY_MEDIUM |
|
|
|
|
(refreshDNS ? nsIDNSService::RESOLVE_BYPASS_CACHE : 0));
|
2008-11-04 18:22:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2013-11-22 01:35:45 +04:00
|
|
|
nsDNSPrefetch::PrefetchHigh(bool refreshDNS)
|
2008-11-04 18:22:04 +03:00
|
|
|
{
|
2013-11-22 01:35:45 +04:00
|
|
|
return Prefetch(refreshDNS ?
|
|
|
|
nsIDNSService::RESOLVE_BYPASS_CACHE : 0);
|
2008-11-04 18:22:04 +03:00
|
|
|
}
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(nsDNSPrefetch, nsIDNSListener)
|
2008-11-04 18:22:04 +03:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDNSPrefetch::OnLookupComplete(nsICancelable *request,
|
|
|
|
nsIDNSRecord *rec,
|
|
|
|
nsresult status)
|
|
|
|
{
|
2013-11-27 22:19:14 +04:00
|
|
|
MOZ_ASSERT(NS_IsMainThread(), "Expecting DNS callback on main thread.");
|
|
|
|
|
2018-09-21 04:30:10 +03:00
|
|
|
if (mStoreTiming) {
|
|
|
|
mEndTimestamp = mozilla::TimeStamp::Now();
|
|
|
|
}
|
|
|
|
nsCOMPtr<nsIDNSListener> listener = do_QueryReferent(mListener);
|
|
|
|
if (listener) {
|
2018-09-22 23:54:11 +03:00
|
|
|
listener->OnLookupComplete(request, rec, status);
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
nsCOMPtr<nsIDNSListener> listener = do_QueryReferent(mListener);
|
|
|
|
if (listener) {
|
|
|
|
listener->OnLookupByTypeComplete(request, res, status);
|
2018-09-21 04:30:10 +03:00
|
|
|
}
|
2008-11-04 18:22:04 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|