This commit is contained in:
Boris Zbarsky 2009-01-08 14:58:53 -05:00
Родитель badc071b5c 557ba078cc
Коммит 817d0447b3
3 изменённых файлов: 28 добавлений и 72 удалений

Просмотреть файл

@ -215,7 +215,11 @@ nsHTMLAnchorElement::BindToTree(nsIDocument* aDocument, nsIContent* aParent,
// Prefetch links
if (aDocument && nsHTMLDNSPrefetch::IsAllowed(GetOwnerDoc())) {
nsHTMLDNSPrefetch::PrefetchLow(this);
nsCOMPtr<nsIURI> hrefURI;
GetHrefURI(getter_AddRefs(hrefURI));
if (hrefURI)
nsHTMLDNSPrefetch::PrefetchLow(hrefURI);
}
return rv;
}

Просмотреть файл

@ -53,8 +53,6 @@
#include "nsGkAtoms.h"
#include "nsIDocument.h"
#include "nsThreadUtils.h"
#include "nsGenericHTMLElement.h"
#include "nsITimer.h"
static NS_DEFINE_CID(kDNSServiceCID, NS_DNSSERVICE_CID);
static PRBool sDisablePrefetchHTTPSPref;
@ -140,30 +138,30 @@ nsHTMLDNSPrefetch::IsAllowed (nsIDocument *aDocument)
}
nsresult
nsHTMLDNSPrefetch::Prefetch(nsGenericHTMLElement *aElement, PRUint16 flags)
nsHTMLDNSPrefetch::Prefetch(nsIURI *aURI, PRUint16 flags)
{
if (!(sInitialized && sPrefetches && sDNSService))
return NS_ERROR_NOT_AVAILABLE;
return sPrefetches->Add(flags, aElement);
return sPrefetches->Add(flags, aURI);
}
nsresult
nsHTMLDNSPrefetch::PrefetchLow(nsGenericHTMLElement *aElement)
nsHTMLDNSPrefetch::PrefetchLow(nsIURI *aURI)
{
return Prefetch(aElement, nsIDNSService::RESOLVE_PRIORITY_LOW);
return Prefetch(aURI, nsIDNSService::RESOLVE_PRIORITY_LOW);
}
nsresult
nsHTMLDNSPrefetch::PrefetchMedium(nsGenericHTMLElement *aElement)
nsHTMLDNSPrefetch::PrefetchMedium(nsIURI *aURI)
{
return Prefetch(aElement, nsIDNSService::RESOLVE_PRIORITY_MEDIUM);
return Prefetch(aURI, nsIDNSService::RESOLVE_PRIORITY_MEDIUM);
}
nsresult
nsHTMLDNSPrefetch::PrefetchHigh(nsGenericHTMLElement *aElement)
nsHTMLDNSPrefetch::PrefetchHigh(nsIURI *aURI)
{
return Prefetch(aElement, 0);
return Prefetch(aURI, 0);
}
nsresult
@ -201,21 +199,14 @@ nsHTMLDNSPrefetch::PrefetchHigh(nsAString &hostname)
nsHTMLDNSPrefetch::nsDeferrals::nsDeferrals()
: mHead(0),
mTail(0),
mActiveLoaderCount(0),
mTimerArmed(PR_FALSE)
mActiveLoaderCount(0)
{
mTimer = do_CreateInstance("@mozilla.org/timer;1");
}
nsHTMLDNSPrefetch::nsDeferrals::~nsDeferrals()
{
if (mTimerArmed) {
mTimerArmed = PR_FALSE;
mTimer->Cancel();
}
while (mHead != mTail) {
mEntries[mTail].mElement = nsnull;
mEntries[mTail].mURI = nsnull;
mTail = (mTail + 1) & sMaxDeferredMask;
}
}
@ -226,7 +217,7 @@ NS_IMPL_THREADSAFE_ISUPPORTS3(nsHTMLDNSPrefetch::nsDeferrals,
nsISupportsWeakReference)
nsresult
nsHTMLDNSPrefetch::nsDeferrals::Add(PRUint16 flags, nsGenericHTMLElement *aElement)
nsHTMLDNSPrefetch::nsDeferrals::Add(PRUint16 flags, nsIURI *aURI)
{
// The FIFO has no lock, so it can only be accessed on main thread
NS_ASSERTION(NS_IsMainThread(), "nsDeferrals::Add must be on main thread");
@ -235,45 +226,31 @@ nsHTMLDNSPrefetch::nsDeferrals::Add(PRUint16 flags, nsGenericHTMLElement *aEleme
return NS_ERROR_DNS_LOOKUP_QUEUE_FULL;
mEntries[mHead].mFlags = flags;
mEntries[mHead].mElement = aElement;
mEntries[mHead].mURI = aURI;
mHead = (mHead + 1) & sMaxDeferredMask;
if (!mActiveLoaderCount && !mTimerArmed && mTimer) {
mTimerArmed = PR_TRUE;
mTimer->InitWithFuncCallback(Tick, this, 2000, nsITimer::TYPE_ONE_SHOT);
}
return NS_OK;
}
void
nsHTMLDNSPrefetch::nsDeferrals::SubmitQueue()
{
NS_ASSERTION(NS_IsMainThread(), "nsDeferrals::SubmitQueue must be on main thread");
nsCString hostName;
if (!sDNSService) return;
while (mHead != mTail) {
nsCOMPtr<nsIURI> hrefURI;
mEntries[mTail].mElement->GetHrefURIForAnchors(getter_AddRefs(hrefURI));
if (hrefURI)
hrefURI->GetAsciiHost(hostName);
mEntries[mTail].mURI->GetAsciiHost(hostName);
if (!hostName.IsEmpty()) {
nsCOMPtr<nsICancelable> tmpOutstanding;
sDNSService->AsyncResolve(hostName,
mEntries[mTail].mFlags,
this, nsnull, getter_AddRefs(tmpOutstanding));
}
mEntries[mTail].mElement = nsnull;
mEntries[mTail].mURI = nsnull;
mTail = (mTail + 1) & sMaxDeferredMask;
}
if (mTimerArmed) {
mTimerArmed = PR_FALSE;
mTimer->Cancel();
}
}
void
@ -286,25 +263,6 @@ nsHTMLDNSPrefetch::nsDeferrals::Activate()
progress->AddProgressListener(this, nsIWebProgress::NOTIFY_STATE_DOCUMENT);
}
// nsITimer related method
void
nsHTMLDNSPrefetch::nsDeferrals::Tick(nsITimer *aTimer, void *aClosure)
{
nsHTMLDNSPrefetch::nsDeferrals *self = (nsHTMLDNSPrefetch::nsDeferrals *) aClosure;
NS_ASSERTION(NS_IsMainThread(), "nsDeferrals::Tick must be on main thread");
NS_ASSERTION(self->mTimerArmed, "Timer is not armed");
self->mTimerArmed = PR_FALSE;
// If the queue is not submitted here because there are outstanding pages being loaded,
// there is no need to rearm the timer as the queue will be submtited when those
// loads complete.
if (!self->mActiveLoaderCount)
self->SubmitQueue();
}
//////////// nsIDNSListener method
NS_IMETHODIMP

Просмотреть файл

@ -40,16 +40,14 @@
#define nsHTMLDNSPrefetch_h___
#include "nsCOMPtr.h"
#include "nsAutoPtr.h"
#include "nsString.h"
#include "nsIDNSListener.h"
#include "nsIWebProgressListener.h"
#include "nsWeakReference.h"
class nsIURI;
class nsIDocument;
class nsGenericHTMLElement;
class nsITimer;
class nsHTMLDNSPrefetch
{
@ -70,16 +68,16 @@ public:
// weight, but its request is also more likely to be dropped due to a
// full queue and it may only be used from the main thread.
static nsresult PrefetchHigh(nsGenericHTMLElement *aElement);
static nsresult PrefetchMedium(nsGenericHTMLElement *aElement);
static nsresult PrefetchLow(nsGenericHTMLElement *aElement);
static nsresult PrefetchHigh(nsIURI *aURI);
static nsresult PrefetchMedium(nsIURI *aURI);
static nsresult PrefetchLow(nsIURI *aURI);
static nsresult PrefetchHigh(nsAString &host);
static nsresult PrefetchMedium(nsAString &host);
static nsresult PrefetchLow(nsAString &host);
private:
static nsresult Prefetch(nsAString &host, PRUint16 flags);
static nsresult Prefetch(nsGenericHTMLElement *aElement, PRUint16 flags);
static nsresult Prefetch(nsIURI *aURI, PRUint16 flags);
static PRBool IsSecureBaseContext(nsIDocument *aDocument);
public:
@ -95,7 +93,7 @@ public:
nsDeferrals();
void Activate();
nsresult Add(PRUint16 flags, nsGenericHTMLElement *aElement);
nsresult Add(PRUint16 flags, nsIURI *aURI);
private:
~nsDeferrals();
@ -105,18 +103,14 @@ public:
PRUint16 mHead;
PRUint16 mTail;
PRUint32 mActiveLoaderCount;
nsCOMPtr<nsITimer> mTimer;
PRBool mTimerArmed;
static void Tick(nsITimer *aTimer, void *aClosure);
static const int sMaxDeferred = 512; // keep power of 2 for masking
static const int sMaxDeferredMask = (sMaxDeferred - 1);
struct deferred_entry
{
PRUint16 mFlags;
nsRefPtr<nsGenericHTMLElement> mElement;
PRUint16 mFlags;
nsCOMPtr<nsIURI> mURI;
} mEntries[sMaxDeferred];
};
};