Source code

Revision control

Other Tools

1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* This Source Code Form is subject to the terms of the Mozilla Public
3
* License, v. 2.0. If a copy of the MPL was not distributed with this
4
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5
6
#include "nsDNSPrefetch.h"
7
#include "nsCOMPtr.h"
8
#include "nsString.h"
9
#include "nsThreadUtils.h"
10
11
#include "nsIDNSListener.h"
12
#include "nsIDNSService.h"
13
#include "nsICancelable.h"
14
#include "nsIURI.h"
15
#include "mozilla/Preferences.h"
16
17
static nsIDNSService* sDNSService = nullptr;
18
static bool sESNIEnabled = false;
19
20
nsresult nsDNSPrefetch::Initialize(nsIDNSService* aDNSService) {
21
NS_IF_RELEASE(sDNSService);
22
sDNSService = aDNSService;
23
NS_IF_ADDREF(sDNSService);
24
mozilla::Preferences::AddBoolVarCache(&sESNIEnabled,
25
"network.security.esni.enabled");
26
return NS_OK;
27
}
28
29
nsresult nsDNSPrefetch::Shutdown() {
30
NS_IF_RELEASE(sDNSService);
31
return NS_OK;
32
}
33
34
nsDNSPrefetch::nsDNSPrefetch(nsIURI* aURI,
35
mozilla::OriginAttributes& aOriginAttributes,
36
nsIDNSListener* aListener, bool storeTiming)
37
: mOriginAttributes(aOriginAttributes),
38
mStoreTiming(storeTiming),
39
mListener(do_GetWeakReference(aListener)) {
40
aURI->GetAsciiHost(mHostname);
41
mIsHttps = aURI->SchemeIs("https");
42
}
43
44
nsresult nsDNSPrefetch::Prefetch(uint16_t flags) {
45
// This can work properly only if this call is on the main thread.
46
// Curenlty we use nsDNSPrefetch only in nsHttpChannel which will call
47
// PrefetchHigh() from the main thread. Let's add assertion to catch
48
// if things change.
49
MOZ_ASSERT(NS_IsMainThread(), "Expecting DNS callback on main thread.");
50
51
if (mHostname.IsEmpty()) return NS_ERROR_NOT_AVAILABLE;
52
53
if (!sDNSService) return NS_ERROR_NOT_AVAILABLE;
54
55
nsCOMPtr<nsICancelable> tmpOutstanding;
56
57
if (mStoreTiming) mStartTimestamp = mozilla::TimeStamp::Now();
58
// If AsyncResolve fails, for example because prefetching is disabled,
59
// then our timing will be useless. However, in such a case,
60
// mEndTimestamp will be a null timestamp and callers should check
61
// TimingsValid() before using the timing.
62
nsCOMPtr<nsIEventTarget> main = mozilla::GetMainThreadEventTarget();
63
64
nsresult rv = sDNSService->AsyncResolveNative(
65
mHostname, flags | nsIDNSService::RESOLVE_SPECULATE, this, main,
66
mOriginAttributes, getter_AddRefs(tmpOutstanding));
67
if (NS_FAILED(rv)) {
68
return rv;
69
}
70
71
// Fetch esni keys if needed.
72
if (sESNIEnabled && mIsHttps) {
73
nsAutoCString esniHost;
74
esniHost.Append("_esni.");
75
esniHost.Append(mHostname);
76
sDNSService->AsyncResolveByTypeNative(
77
esniHost, nsIDNSService::RESOLVE_TYPE_TXT,
78
flags | nsIDNSService::RESOLVE_SPECULATE, this, main, mOriginAttributes,
79
getter_AddRefs(tmpOutstanding));
80
}
81
return NS_OK;
82
}
83
84
nsresult nsDNSPrefetch::PrefetchLow(bool refreshDNS) {
85
return Prefetch(nsIDNSService::RESOLVE_PRIORITY_LOW |
86
(refreshDNS ? nsIDNSService::RESOLVE_BYPASS_CACHE : 0));
87
}
88
89
nsresult nsDNSPrefetch::PrefetchMedium(bool refreshDNS) {
90
return Prefetch(nsIDNSService::RESOLVE_PRIORITY_MEDIUM |
91
(refreshDNS ? nsIDNSService::RESOLVE_BYPASS_CACHE : 0));
92
}
93
94
nsresult nsDNSPrefetch::PrefetchHigh(bool refreshDNS) {
95
return Prefetch(refreshDNS ? nsIDNSService::RESOLVE_BYPASS_CACHE : 0);
96
}
97
98
NS_IMPL_ISUPPORTS(nsDNSPrefetch, nsIDNSListener)
99
100
NS_IMETHODIMP
101
nsDNSPrefetch::OnLookupComplete(nsICancelable* request, nsIDNSRecord* rec,
102
nsresult status) {
103
MOZ_ASSERT(NS_IsMainThread(), "Expecting DNS callback on main thread.");
104
105
if (mStoreTiming) {
106
mEndTimestamp = mozilla::TimeStamp::Now();
107
}
108
nsCOMPtr<nsIDNSListener> listener = do_QueryReferent(mListener);
109
if (listener) {
110
listener->OnLookupComplete(request, rec, status);
111
}
112
return NS_OK;
113
}
114
115
NS_IMETHODIMP
116
nsDNSPrefetch::OnLookupByTypeComplete(nsICancelable* request,
117
nsIDNSByTypeRecord* res,
118
nsresult status) {
119
MOZ_ASSERT(NS_IsMainThread(), "Expecting DNS callback on main thread.");
120
121
if (mStoreTiming) {
122
mEndTimestamp = mozilla::TimeStamp::Now();
123
}
124
nsCOMPtr<nsIDNSListener> listener = do_QueryReferent(mListener);
125
if (listener) {
126
listener->OnLookupByTypeComplete(request, res, status);
127
}
128
return NS_OK;
129
}