Bug 938803 - Cancel DNS refresh requested in nsHttpChannel::mCaps r=mcmanus

This commit is contained in:
Steve Workman 2013-11-26 16:09:49 -08:00
parent e7311e3096
commit 68d26f138f
4 changed files with 62 additions and 6 deletions

View File

@ -6,6 +6,7 @@
#include "nsDNSPrefetch.h"
#include "nsCOMPtr.h"
#include "nsString.h"
#include "nsThreadUtils.h"
#include "nsIDNSListener.h"
#include "nsIDNSService.h"
@ -30,8 +31,11 @@ nsDNSPrefetch::Shutdown()
return NS_OK;
}
nsDNSPrefetch::nsDNSPrefetch(nsIURI *aURI, bool storeTiming)
nsDNSPrefetch::nsDNSPrefetch(nsIURI *aURI,
nsIDNSListener *aListener,
bool storeTiming)
: mStoreTiming(storeTiming)
, mListener(do_GetWeakReference(aListener))
{
aURI->GetAsciiHost(mHostname);
}
@ -53,8 +57,11 @@ nsDNSPrefetch::Prefetch(uint16_t flags)
// 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.
return sDNSService->AsyncResolve(mHostname, flags | nsIDNSService::RESOLVE_SPECULATE,
this, nullptr, getter_AddRefs(tmpOutstanding));
nsCOMPtr<nsIThread> mainThread = do_GetMainThread();
return sDNSService->AsyncResolve(mHostname,
flags | nsIDNSService::RESOLVE_SPECULATE,
this, mainThread,
getter_AddRefs(tmpOutstanding));
}
nsresult
@ -86,7 +93,14 @@ nsDNSPrefetch::OnLookupComplete(nsICancelable *request,
nsIDNSRecord *rec,
nsresult status)
{
if (mStoreTiming)
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->OnLookupComplete(request, rec, status);
}
return NS_OK;
}

View File

@ -6,6 +6,7 @@
#ifndef nsDNSPrefetch_h___
#define nsDNSPrefetch_h___
#include "nsWeakReference.h"
#include "nsString.h"
#include "mozilla/TimeStamp.h"
#include "mozilla/Attributes.h"
@ -21,7 +22,7 @@ public:
NS_DECL_THREADSAFE_ISUPPORTS
NS_DECL_NSIDNSLISTENER
nsDNSPrefetch(nsIURI *aURI, bool storeTiming);
nsDNSPrefetch(nsIURI *aURI, nsIDNSListener *aListener, bool storeTiming);
bool TimingsValid() const {
return !mStartTimestamp.IsNull() && !mEndTimestamp.IsNull();
}
@ -42,6 +43,7 @@ private:
bool mStoreTiming;
mozilla::TimeStamp mStartTimestamp;
mozilla::TimeStamp mEndTimestamp;
nsWeakPtr mListener;
nsresult Prefetch(uint16_t flags);
};

View File

@ -4302,6 +4302,8 @@ NS_INTERFACE_MAP_BEGIN(nsHttpChannel)
NS_INTERFACE_MAP_ENTRY(nsITimedChannel)
NS_INTERFACE_MAP_ENTRY(nsIThreadRetargetableRequest)
NS_INTERFACE_MAP_ENTRY(nsIThreadRetargetableStreamListener)
NS_INTERFACE_MAP_ENTRY(nsIDNSListener)
NS_INTERFACE_MAP_ENTRY(nsISupportsWeakReference)
NS_INTERFACE_MAP_END_INHERITING(HttpBaseChannel)
//-----------------------------------------------------------------------------
@ -4534,7 +4536,7 @@ nsHttpChannel::BeginConnect()
// timing we used.
LOG(("nsHttpChannel::BeginConnect [this=%p] prefetching%s\n",
this, mCaps & NS_HTTP_REFRESH_DNS ? ", refresh requested" : ""));
mDNSPrefetch = new nsDNSPrefetch(mURI, mTimingEnabled);
mDNSPrefetch = new nsDNSPrefetch(mURI, this, mTimingEnabled);
mDNSPrefetch->PrefetchHigh(mCaps & NS_HTTP_REFRESH_DNS);
}
@ -5919,6 +5921,39 @@ nsHttpChannel::PopRedirectAsyncFunc(nsContinueRedirectionFunc func)
mRedirectFuncStack.TruncateLength(mRedirectFuncStack.Length() - 1);
}
//-----------------------------------------------------------------------------
// nsIDNSListener functions
//-----------------------------------------------------------------------------
NS_IMETHODIMP
nsHttpChannel::OnLookupComplete(nsICancelable *request,
nsIDNSRecord *rec,
nsresult status)
{
MOZ_ASSERT(NS_IsMainThread(), "Expecting DNS callback on main thread.");
MOZ_ASSERT(mDNSPrefetch, "Expecting an nsDNSPrefetch object.");
LOG(("nsHttpChannel::OnLookupComplete [this=%p] prefetch complete%s: "
"%s status[0x%x]\n",
this, mCaps & NS_HTTP_REFRESH_DNS ? ", refresh requested" : "",
NS_SUCCEEDED(status) ? "success" : "failure", status));
// We no longer need the dns prefetch object
if (mDNSPrefetch && mDNSPrefetch->TimingsValid()) {
mTransactionTimings.domainLookupStart =
mDNSPrefetch->StartTimestamp();
mTransactionTimings.domainLookupEnd =
mDNSPrefetch->EndTimestamp();
}
mDNSPrefetch = nullptr;
// Unset DNS cache refresh if it was requested,
if (mCaps & NS_HTTP_REFRESH_DNS) {
mCaps &= ~NS_HTTP_REFRESH_DNS;
}
return NS_OK;
}
//-----------------------------------------------------------------------------
// nsHttpChannel internal functions

View File

@ -12,6 +12,7 @@
#include "nsICachingChannel.h"
#include "nsICacheEntry.h"
#include "nsICacheEntryOpenCallback.h"
#include "nsIDNSListener.h"
#include "nsIApplicationCacheChannel.h"
#include "nsIProtocolProxyCallback.h"
#include "nsIHttpAuthenticableChannel.h"
@ -19,6 +20,7 @@
#include "nsITimedChannel.h"
#include "nsIThreadRetargetableRequest.h"
#include "nsIThreadRetargetableStreamListener.h"
#include "nsWeakReference.h"
#include "TimingStruct.h"
#include "AutoClose.h"
#include "mozilla/Telemetry.h"
@ -51,6 +53,8 @@ class nsHttpChannel : public HttpBaseChannel
, public nsITimedChannel
, public nsIThreadRetargetableRequest
, public nsIThreadRetargetableStreamListener
, public nsIDNSListener
, public nsSupportsWeakReference
{
public:
NS_DECL_ISUPPORTS_INHERITED
@ -68,6 +72,7 @@ public:
NS_DECL_NSIASYNCVERIFYREDIRECTCALLBACK
NS_DECL_NSITIMEDCHANNEL
NS_DECL_NSITHREADRETARGETABLEREQUEST
NS_DECL_NSIDNSLISTENER
// nsIHttpAuthenticableChannel. We can't use
// NS_DECL_NSIHTTPAUTHENTICABLECHANNEL because it duplicates cancel() and