Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #include "nsDNSPrefetch.h" |
michael@0 | 7 | #include "nsCOMPtr.h" |
michael@0 | 8 | #include "nsString.h" |
michael@0 | 9 | #include "nsThreadUtils.h" |
michael@0 | 10 | |
michael@0 | 11 | #include "nsIDNSListener.h" |
michael@0 | 12 | #include "nsIDNSService.h" |
michael@0 | 13 | #include "nsICancelable.h" |
michael@0 | 14 | #include "nsIURI.h" |
michael@0 | 15 | |
michael@0 | 16 | static nsIDNSService *sDNSService = nullptr; |
michael@0 | 17 | |
michael@0 | 18 | nsresult |
michael@0 | 19 | nsDNSPrefetch::Initialize(nsIDNSService *aDNSService) |
michael@0 | 20 | { |
michael@0 | 21 | NS_IF_RELEASE(sDNSService); |
michael@0 | 22 | sDNSService = aDNSService; |
michael@0 | 23 | NS_IF_ADDREF(sDNSService); |
michael@0 | 24 | return NS_OK; |
michael@0 | 25 | } |
michael@0 | 26 | |
michael@0 | 27 | nsresult |
michael@0 | 28 | nsDNSPrefetch::Shutdown() |
michael@0 | 29 | { |
michael@0 | 30 | NS_IF_RELEASE(sDNSService); |
michael@0 | 31 | return NS_OK; |
michael@0 | 32 | } |
michael@0 | 33 | |
michael@0 | 34 | nsDNSPrefetch::nsDNSPrefetch(nsIURI *aURI, |
michael@0 | 35 | nsIDNSListener *aListener, |
michael@0 | 36 | bool storeTiming) |
michael@0 | 37 | : mStoreTiming(storeTiming) |
michael@0 | 38 | , mListener(do_GetWeakReference(aListener)) |
michael@0 | 39 | { |
michael@0 | 40 | aURI->GetAsciiHost(mHostname); |
michael@0 | 41 | } |
michael@0 | 42 | |
michael@0 | 43 | nsresult |
michael@0 | 44 | nsDNSPrefetch::Prefetch(uint16_t flags) |
michael@0 | 45 | { |
michael@0 | 46 | if (mHostname.IsEmpty()) |
michael@0 | 47 | return NS_ERROR_NOT_AVAILABLE; |
michael@0 | 48 | |
michael@0 | 49 | if (!sDNSService) |
michael@0 | 50 | return NS_ERROR_NOT_AVAILABLE; |
michael@0 | 51 | |
michael@0 | 52 | nsCOMPtr<nsICancelable> tmpOutstanding; |
michael@0 | 53 | |
michael@0 | 54 | if (mStoreTiming) |
michael@0 | 55 | mStartTimestamp = mozilla::TimeStamp::Now(); |
michael@0 | 56 | // If AsyncResolve fails, for example because prefetching is disabled, |
michael@0 | 57 | // then our timing will be useless. However, in such a case, |
michael@0 | 58 | // mEndTimestamp will be a null timestamp and callers should check |
michael@0 | 59 | // TimingsValid() before using the timing. |
michael@0 | 60 | nsCOMPtr<nsIThread> mainThread = do_GetMainThread(); |
michael@0 | 61 | return sDNSService->AsyncResolve(mHostname, |
michael@0 | 62 | flags | nsIDNSService::RESOLVE_SPECULATE, |
michael@0 | 63 | this, mainThread, |
michael@0 | 64 | getter_AddRefs(tmpOutstanding)); |
michael@0 | 65 | } |
michael@0 | 66 | |
michael@0 | 67 | nsresult |
michael@0 | 68 | nsDNSPrefetch::PrefetchLow(bool refreshDNS) |
michael@0 | 69 | { |
michael@0 | 70 | return Prefetch(nsIDNSService::RESOLVE_PRIORITY_LOW | |
michael@0 | 71 | (refreshDNS ? nsIDNSService::RESOLVE_BYPASS_CACHE : 0)); |
michael@0 | 72 | } |
michael@0 | 73 | |
michael@0 | 74 | nsresult |
michael@0 | 75 | nsDNSPrefetch::PrefetchMedium(bool refreshDNS) |
michael@0 | 76 | { |
michael@0 | 77 | return Prefetch(nsIDNSService::RESOLVE_PRIORITY_MEDIUM | |
michael@0 | 78 | (refreshDNS ? nsIDNSService::RESOLVE_BYPASS_CACHE : 0)); |
michael@0 | 79 | } |
michael@0 | 80 | |
michael@0 | 81 | nsresult |
michael@0 | 82 | nsDNSPrefetch::PrefetchHigh(bool refreshDNS) |
michael@0 | 83 | { |
michael@0 | 84 | return Prefetch(refreshDNS ? |
michael@0 | 85 | nsIDNSService::RESOLVE_BYPASS_CACHE : 0); |
michael@0 | 86 | } |
michael@0 | 87 | |
michael@0 | 88 | |
michael@0 | 89 | NS_IMPL_ISUPPORTS(nsDNSPrefetch, nsIDNSListener) |
michael@0 | 90 | |
michael@0 | 91 | NS_IMETHODIMP |
michael@0 | 92 | nsDNSPrefetch::OnLookupComplete(nsICancelable *request, |
michael@0 | 93 | nsIDNSRecord *rec, |
michael@0 | 94 | nsresult status) |
michael@0 | 95 | { |
michael@0 | 96 | MOZ_ASSERT(NS_IsMainThread(), "Expecting DNS callback on main thread."); |
michael@0 | 97 | |
michael@0 | 98 | if (mStoreTiming) { |
michael@0 | 99 | mEndTimestamp = mozilla::TimeStamp::Now(); |
michael@0 | 100 | } |
michael@0 | 101 | nsCOMPtr<nsIDNSListener> listener = do_QueryReferent(mListener); |
michael@0 | 102 | if (listener) { |
michael@0 | 103 | listener->OnLookupComplete(request, rec, status); |
michael@0 | 104 | } |
michael@0 | 105 | return NS_OK; |
michael@0 | 106 | } |