netwerk/base/src/nsDNSPrefetch.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/netwerk/base/src/nsDNSPrefetch.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,106 @@
     1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#include "nsDNSPrefetch.h"
    1.10 +#include "nsCOMPtr.h"
    1.11 +#include "nsString.h"
    1.12 +#include "nsThreadUtils.h"
    1.13 +
    1.14 +#include "nsIDNSListener.h"
    1.15 +#include "nsIDNSService.h"
    1.16 +#include "nsICancelable.h"
    1.17 +#include "nsIURI.h"
    1.18 +
    1.19 +static nsIDNSService *sDNSService = nullptr;
    1.20 +
    1.21 +nsresult
    1.22 +nsDNSPrefetch::Initialize(nsIDNSService *aDNSService)
    1.23 +{
    1.24 +    NS_IF_RELEASE(sDNSService);
    1.25 +    sDNSService =  aDNSService;
    1.26 +    NS_IF_ADDREF(sDNSService);
    1.27 +    return NS_OK;
    1.28 +}
    1.29 +
    1.30 +nsresult
    1.31 +nsDNSPrefetch::Shutdown()
    1.32 +{
    1.33 +    NS_IF_RELEASE(sDNSService);
    1.34 +    return NS_OK;
    1.35 +}
    1.36 +
    1.37 +nsDNSPrefetch::nsDNSPrefetch(nsIURI *aURI,
    1.38 +                             nsIDNSListener *aListener,
    1.39 +                             bool storeTiming)
    1.40 +    : mStoreTiming(storeTiming)
    1.41 +    , mListener(do_GetWeakReference(aListener))
    1.42 +{
    1.43 +    aURI->GetAsciiHost(mHostname);
    1.44 +}
    1.45 +
    1.46 +nsresult 
    1.47 +nsDNSPrefetch::Prefetch(uint16_t flags)
    1.48 +{
    1.49 +    if (mHostname.IsEmpty())
    1.50 +        return NS_ERROR_NOT_AVAILABLE;
    1.51 +  
    1.52 +    if (!sDNSService)
    1.53 +        return NS_ERROR_NOT_AVAILABLE;
    1.54 +    
    1.55 +    nsCOMPtr<nsICancelable> tmpOutstanding;  
    1.56 +
    1.57 +    if (mStoreTiming)
    1.58 +        mStartTimestamp = mozilla::TimeStamp::Now();
    1.59 +    // If AsyncResolve fails, for example because prefetching is disabled,
    1.60 +    // then our timing will be useless. However, in such a case,
    1.61 +    // mEndTimestamp will be a null timestamp and callers should check
    1.62 +    // TimingsValid() before using the timing.
    1.63 +    nsCOMPtr<nsIThread> mainThread = do_GetMainThread();
    1.64 +    return sDNSService->AsyncResolve(mHostname,
    1.65 +                                     flags | nsIDNSService::RESOLVE_SPECULATE,
    1.66 +                                     this, mainThread,
    1.67 +                                     getter_AddRefs(tmpOutstanding));
    1.68 +}
    1.69 +
    1.70 +nsresult
    1.71 +nsDNSPrefetch::PrefetchLow(bool refreshDNS)
    1.72 +{
    1.73 +    return Prefetch(nsIDNSService::RESOLVE_PRIORITY_LOW |
    1.74 +      (refreshDNS ? nsIDNSService::RESOLVE_BYPASS_CACHE : 0));
    1.75 +}
    1.76 +
    1.77 +nsresult
    1.78 +nsDNSPrefetch::PrefetchMedium(bool refreshDNS)
    1.79 +{
    1.80 +    return Prefetch(nsIDNSService::RESOLVE_PRIORITY_MEDIUM |
    1.81 +      (refreshDNS ? nsIDNSService::RESOLVE_BYPASS_CACHE : 0));
    1.82 +}
    1.83 +
    1.84 +nsresult
    1.85 +nsDNSPrefetch::PrefetchHigh(bool refreshDNS)
    1.86 +{
    1.87 +    return Prefetch(refreshDNS ?
    1.88 +                    nsIDNSService::RESOLVE_BYPASS_CACHE : 0);
    1.89 +}
    1.90 +
    1.91 +
    1.92 +NS_IMPL_ISUPPORTS(nsDNSPrefetch, nsIDNSListener)
    1.93 +
    1.94 +NS_IMETHODIMP
    1.95 +nsDNSPrefetch::OnLookupComplete(nsICancelable *request,
    1.96 +                                nsIDNSRecord  *rec,
    1.97 +                                nsresult       status)
    1.98 +{
    1.99 +    MOZ_ASSERT(NS_IsMainThread(), "Expecting DNS callback on main thread.");
   1.100 +
   1.101 +    if (mStoreTiming) {
   1.102 +        mEndTimestamp = mozilla::TimeStamp::Now();
   1.103 +    }
   1.104 +    nsCOMPtr<nsIDNSListener> listener = do_QueryReferent(mListener);
   1.105 +    if (listener) {
   1.106 +      listener->OnLookupComplete(request, rec, status);
   1.107 +    }
   1.108 +    return NS_OK;
   1.109 +}

mercurial