Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 :
16 : static nsIDNSService *sDNSService = nullptr;
17 :
18 : nsresult
19 2 : nsDNSPrefetch::Initialize(nsIDNSService *aDNSService)
20 : {
21 2 : NS_IF_RELEASE(sDNSService);
22 2 : sDNSService = aDNSService;
23 2 : NS_IF_ADDREF(sDNSService);
24 2 : return NS_OK;
25 : }
26 :
27 : nsresult
28 0 : nsDNSPrefetch::Shutdown()
29 : {
30 0 : NS_IF_RELEASE(sDNSService);
31 0 : return NS_OK;
32 : }
33 :
34 4 : nsDNSPrefetch::nsDNSPrefetch(nsIURI *aURI,
35 : mozilla::OriginAttributes& aOriginAttributes,
36 : nsIDNSListener *aListener,
37 4 : bool storeTiming)
38 : : mOriginAttributes(aOriginAttributes)
39 : , mStoreTiming(storeTiming)
40 4 : , mListener(do_GetWeakReference(aListener))
41 : {
42 4 : aURI->GetAsciiHost(mHostname);
43 4 : }
44 :
45 : nsresult
46 4 : nsDNSPrefetch::Prefetch(uint16_t flags)
47 : {
48 4 : if (mHostname.IsEmpty())
49 0 : return NS_ERROR_NOT_AVAILABLE;
50 :
51 4 : if (!sDNSService)
52 0 : return NS_ERROR_NOT_AVAILABLE;
53 :
54 8 : nsCOMPtr<nsICancelable> tmpOutstanding;
55 :
56 4 : if (mStoreTiming)
57 4 : 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 8 : nsCOMPtr<nsIEventTarget> main = mozilla::GetMainThreadEventTarget();
63 12 : return sDNSService->AsyncResolveNative(mHostname,
64 4 : flags | nsIDNSService::RESOLVE_SPECULATE,
65 : this, main, mOriginAttributes,
66 8 : getter_AddRefs(tmpOutstanding));
67 : }
68 :
69 : nsresult
70 0 : nsDNSPrefetch::PrefetchLow(bool refreshDNS)
71 : {
72 0 : return Prefetch(nsIDNSService::RESOLVE_PRIORITY_LOW |
73 0 : (refreshDNS ? nsIDNSService::RESOLVE_BYPASS_CACHE : 0));
74 : }
75 :
76 : nsresult
77 0 : nsDNSPrefetch::PrefetchMedium(bool refreshDNS)
78 : {
79 0 : return Prefetch(nsIDNSService::RESOLVE_PRIORITY_MEDIUM |
80 0 : (refreshDNS ? nsIDNSService::RESOLVE_BYPASS_CACHE : 0));
81 : }
82 :
83 : nsresult
84 4 : nsDNSPrefetch::PrefetchHigh(bool refreshDNS)
85 : {
86 4 : return Prefetch(refreshDNS ?
87 4 : nsIDNSService::RESOLVE_BYPASS_CACHE : 0);
88 : }
89 :
90 :
91 12 : NS_IMPL_ISUPPORTS(nsDNSPrefetch, nsIDNSListener)
92 :
93 : NS_IMETHODIMP
94 0 : nsDNSPrefetch::OnLookupComplete(nsICancelable *request,
95 : nsIDNSRecord *rec,
96 : nsresult status)
97 : {
98 0 : MOZ_ASSERT(NS_IsMainThread(), "Expecting DNS callback on main thread.");
99 :
100 0 : if (mStoreTiming) {
101 0 : mEndTimestamp = mozilla::TimeStamp::Now();
102 : }
103 0 : nsCOMPtr<nsIDNSListener> listener = do_QueryReferent(mListener);
104 0 : if (listener) {
105 0 : listener->OnLookupComplete(request, rec, status);
106 : }
107 0 : return NS_OK;
108 : }
|