2008-11-04 07:22:04 -08:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 04:12:37 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2008-11-04 07:22:04 -08:00
|
|
|
|
2010-07-26 11:49:09 -07:00
|
|
|
#include "base/basictypes.h"
|
|
|
|
#include "mozilla/net/NeckoCommon.h"
|
|
|
|
#include "mozilla/net/NeckoChild.h"
|
2010-08-20 20:06:53 -07:00
|
|
|
#include "nsURLHelper.h"
|
2010-07-26 11:49:09 -07:00
|
|
|
|
2008-11-04 07:22:04 -08:00
|
|
|
#include "nsHTMLDNSPrefetch.h"
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsString.h"
|
|
|
|
|
|
|
|
#include "nsNetUtil.h"
|
|
|
|
|
|
|
|
#include "nsIDNSListener.h"
|
2008-12-05 12:53:24 -08:00
|
|
|
#include "nsIWebProgressListener.h"
|
|
|
|
#include "nsIWebProgress.h"
|
|
|
|
#include "nsCURILoader.h"
|
2008-11-04 07:22:04 -08:00
|
|
|
#include "nsIDNSRecord.h"
|
|
|
|
#include "nsIDNSService.h"
|
|
|
|
#include "nsICancelable.h"
|
|
|
|
#include "nsGkAtoms.h"
|
|
|
|
#include "nsIDocument.h"
|
2008-12-05 12:53:24 -08:00
|
|
|
#include "nsThreadUtils.h"
|
2009-01-12 07:36:55 -08:00
|
|
|
#include "nsITimer.h"
|
|
|
|
#include "nsIObserverService.h"
|
2010-02-24 08:37:03 -08:00
|
|
|
#include "mozilla/dom/Link.h"
|
|
|
|
|
2011-05-24 23:31:59 -07:00
|
|
|
#include "mozilla/Preferences.h"
|
|
|
|
|
|
|
|
using namespace mozilla;
|
2010-02-24 08:37:03 -08:00
|
|
|
using namespace mozilla::dom;
|
2010-08-20 16:24:40 -07:00
|
|
|
using namespace mozilla::net;
|
2008-11-04 07:22:04 -08:00
|
|
|
|
|
|
|
static NS_DEFINE_CID(kDNSServiceCID, NS_DNSSERVICE_CID);
|
2011-09-28 23:19:26 -07:00
|
|
|
bool sDisablePrefetchHTTPSPref;
|
|
|
|
static bool sInitialized = false;
|
2012-07-30 07:20:58 -07:00
|
|
|
static nsIDNSService *sDNSService = nullptr;
|
|
|
|
static nsHTMLDNSPrefetch::nsDeferrals *sPrefetches = nullptr;
|
|
|
|
static nsHTMLDNSPrefetch::nsListener *sDNSListener = nullptr;
|
2008-11-04 07:22:04 -08:00
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsHTMLDNSPrefetch::Initialize()
|
|
|
|
{
|
|
|
|
if (sInitialized) {
|
|
|
|
NS_WARNING("Initialize() called twice");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2008-12-05 12:53:24 -08:00
|
|
|
sPrefetches = new nsHTMLDNSPrefetch::nsDeferrals();
|
|
|
|
NS_ADDREF(sPrefetches);
|
2009-01-12 07:38:32 -08:00
|
|
|
|
|
|
|
sDNSListener = new nsHTMLDNSPrefetch::nsListener();
|
|
|
|
NS_ADDREF(sDNSListener);
|
|
|
|
|
2008-12-05 12:53:24 -08:00
|
|
|
sPrefetches->Activate();
|
|
|
|
|
2011-05-28 16:42:57 -07:00
|
|
|
Preferences::AddBoolVarCache(&sDisablePrefetchHTTPSPref,
|
|
|
|
"network.dns.disablePrefetchFromHTTPS");
|
2008-11-04 07:22:04 -08:00
|
|
|
|
|
|
|
// Default is false, so we need an explicit call to prime the cache.
|
|
|
|
sDisablePrefetchHTTPSPref =
|
2011-09-28 23:19:26 -07:00
|
|
|
Preferences::GetBool("network.dns.disablePrefetchFromHTTPS", true);
|
2008-11-04 07:22:04 -08:00
|
|
|
|
|
|
|
NS_IF_RELEASE(sDNSService);
|
|
|
|
nsresult rv;
|
|
|
|
rv = CallGetService(kDNSServiceCID, &sDNSService);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2010-08-20 16:24:40 -07:00
|
|
|
if (IsNeckoChild())
|
|
|
|
NeckoChild::InitNeckoChild();
|
2010-08-10 17:20:17 -07:00
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
sInitialized = true;
|
2008-11-04 07:22:04 -08:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsHTMLDNSPrefetch::Shutdown()
|
|
|
|
{
|
|
|
|
if (!sInitialized) {
|
|
|
|
NS_WARNING("Not Initialized");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2011-10-17 07:59:28 -07:00
|
|
|
sInitialized = false;
|
2008-11-04 07:22:04 -08:00
|
|
|
NS_IF_RELEASE(sDNSService);
|
2008-12-05 12:53:24 -08:00
|
|
|
NS_IF_RELEASE(sPrefetches);
|
2009-01-12 07:38:32 -08:00
|
|
|
NS_IF_RELEASE(sDNSListener);
|
2008-11-04 07:22:04 -08:00
|
|
|
|
2008-12-05 12:53:24 -08:00
|
|
|
return NS_OK;
|
2008-11-04 07:22:04 -08:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2008-11-04 07:22:04 -08:00
|
|
|
nsHTMLDNSPrefetch::IsAllowed (nsIDocument *aDocument)
|
|
|
|
{
|
2014-08-22 16:17:18 -07:00
|
|
|
if (NS_IsAppOffline(aDocument->NodePrincipal())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-11-04 07:22:04 -08:00
|
|
|
// There is no need to do prefetch on non UI scenarios such as XMLHttpRequest.
|
2009-05-17 07:22:54 -07:00
|
|
|
return aDocument->IsDNSPrefetchAllowed() && aDocument->GetWindow();
|
2008-11-04 07:22:04 -08:00
|
|
|
}
|
|
|
|
|
2008-12-05 12:53:24 -08:00
|
|
|
nsresult
|
2012-08-22 08:56:38 -07:00
|
|
|
nsHTMLDNSPrefetch::Prefetch(Link *aElement, uint16_t flags)
|
2008-11-04 07:22:04 -08:00
|
|
|
{
|
2009-01-12 07:38:32 -08:00
|
|
|
if (!(sInitialized && sPrefetches && sDNSService && sDNSListener))
|
2008-11-04 07:22:04 -08:00
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
|
2009-01-12 07:36:55 -08:00
|
|
|
return sPrefetches->Add(flags, aElement);
|
2008-12-05 12:53:24 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2010-02-24 08:37:03 -08:00
|
|
|
nsHTMLDNSPrefetch::PrefetchLow(Link *aElement)
|
2008-12-05 12:53:24 -08:00
|
|
|
{
|
2009-01-12 07:36:55 -08:00
|
|
|
return Prefetch(aElement, nsIDNSService::RESOLVE_PRIORITY_LOW);
|
2008-12-05 12:53:24 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2010-02-24 08:37:03 -08:00
|
|
|
nsHTMLDNSPrefetch::PrefetchMedium(Link *aElement)
|
2008-12-05 12:53:24 -08:00
|
|
|
{
|
2009-01-12 07:36:55 -08:00
|
|
|
return Prefetch(aElement, nsIDNSService::RESOLVE_PRIORITY_MEDIUM);
|
2008-12-05 12:53:24 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2010-02-24 08:37:03 -08:00
|
|
|
nsHTMLDNSPrefetch::PrefetchHigh(Link *aElement)
|
2008-12-05 12:53:24 -08:00
|
|
|
{
|
2009-01-12 07:36:55 -08:00
|
|
|
return Prefetch(aElement, 0);
|
2008-12-05 12:53:24 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-08-22 08:56:38 -07:00
|
|
|
nsHTMLDNSPrefetch::Prefetch(const nsAString &hostname, uint16_t flags)
|
2008-12-05 12:53:24 -08:00
|
|
|
{
|
2010-08-20 16:24:40 -07:00
|
|
|
if (IsNeckoChild()) {
|
|
|
|
// We need to check IsEmpty() because net_IsValidHostName()
|
|
|
|
// considers empty strings to be valid hostnames
|
|
|
|
if (!hostname.IsEmpty() &&
|
|
|
|
net_IsValidHostName(NS_ConvertUTF16toUTF8(hostname))) {
|
|
|
|
gNeckoChild->SendHTMLDNSPrefetch(nsAutoString(hostname), flags);
|
|
|
|
}
|
2010-07-26 11:49:09 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2009-01-12 07:38:32 -08:00
|
|
|
if (!(sInitialized && sDNSService && sPrefetches && sDNSListener))
|
2008-11-04 07:22:04 -08:00
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
2008-12-05 12:53:24 -08:00
|
|
|
|
2008-11-04 07:22:04 -08:00
|
|
|
nsCOMPtr<nsICancelable> tmpOutstanding;
|
2012-01-20 15:14:46 -08:00
|
|
|
return sDNSService->AsyncResolve(NS_ConvertUTF16toUTF8(hostname),
|
|
|
|
flags | nsIDNSService::RESOLVE_SPECULATE,
|
2012-07-30 07:20:58 -07:00
|
|
|
sDNSListener, nullptr,
|
2012-01-20 15:14:46 -08:00
|
|
|
getter_AddRefs(tmpOutstanding));
|
2008-12-05 12:53:24 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-01-20 15:14:46 -08:00
|
|
|
nsHTMLDNSPrefetch::PrefetchLow(const nsAString &hostname)
|
2008-12-05 12:53:24 -08:00
|
|
|
{
|
|
|
|
return Prefetch(hostname, nsIDNSService::RESOLVE_PRIORITY_LOW);
|
2008-11-04 07:22:04 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-01-20 15:14:46 -08:00
|
|
|
nsHTMLDNSPrefetch::PrefetchMedium(const nsAString &hostname)
|
2008-11-04 07:22:04 -08:00
|
|
|
{
|
2008-12-05 12:53:24 -08:00
|
|
|
return Prefetch(hostname, nsIDNSService::RESOLVE_PRIORITY_MEDIUM);
|
2008-11-04 07:22:04 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-01-20 15:14:46 -08:00
|
|
|
nsHTMLDNSPrefetch::PrefetchHigh(const nsAString &hostname)
|
2008-12-05 12:53:24 -08:00
|
|
|
{
|
|
|
|
return Prefetch(hostname, 0);
|
|
|
|
}
|
|
|
|
|
2012-01-20 15:14:46 -08:00
|
|
|
nsresult
|
|
|
|
nsHTMLDNSPrefetch::CancelPrefetch(Link *aElement,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint16_t flags,
|
2012-01-20 15:14:46 -08:00
|
|
|
nsresult aReason)
|
|
|
|
{
|
|
|
|
if (!(sInitialized && sPrefetches && sDNSService && sDNSListener))
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
|
|
|
|
nsAutoString hostname;
|
2014-07-11 16:30:27 -07:00
|
|
|
ErrorResult rv;
|
|
|
|
aElement->GetHostname(hostname, rv);
|
2012-01-20 15:14:46 -08:00
|
|
|
return CancelPrefetch(hostname, flags, aReason);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsHTMLDNSPrefetch::CancelPrefetch(const nsAString &hostname,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint16_t flags,
|
2012-01-20 15:14:46 -08:00
|
|
|
nsresult aReason)
|
|
|
|
{
|
|
|
|
// Forward this request to Necko Parent if we're a child process
|
|
|
|
if (IsNeckoChild()) {
|
|
|
|
// We need to check IsEmpty() because net_IsValidHostName()
|
|
|
|
// considers empty strings to be valid hostnames
|
|
|
|
if (!hostname.IsEmpty() &&
|
|
|
|
net_IsValidHostName(NS_ConvertUTF16toUTF8(hostname))) {
|
|
|
|
gNeckoChild->SendCancelHTMLDNSPrefetch(nsString(hostname), flags,
|
|
|
|
aReason);
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(sInitialized && sDNSService && sPrefetches && sDNSListener))
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
|
|
|
|
// Forward cancellation to DNS service
|
|
|
|
return sDNSService->CancelAsyncResolve(NS_ConvertUTF16toUTF8(hostname),
|
|
|
|
flags
|
|
|
|
| nsIDNSService::RESOLVE_SPECULATE,
|
|
|
|
sDNSListener, aReason);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsHTMLDNSPrefetch::CancelPrefetchLow(Link *aElement, nsresult aReason)
|
|
|
|
{
|
|
|
|
return CancelPrefetch(aElement, nsIDNSService::RESOLVE_PRIORITY_LOW,
|
|
|
|
aReason);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsHTMLDNSPrefetch::CancelPrefetchLow(const nsAString &hostname, nsresult aReason)
|
|
|
|
{
|
|
|
|
return CancelPrefetch(hostname, nsIDNSService::RESOLVE_PRIORITY_LOW,
|
|
|
|
aReason);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-01-12 07:38:32 -08:00
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2014-04-27 00:06:00 -07:00
|
|
|
NS_IMPL_ISUPPORTS(nsHTMLDNSPrefetch::nsListener,
|
|
|
|
nsIDNSListener)
|
2009-01-12 07:38:32 -08:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHTMLDNSPrefetch::nsListener::OnLookupComplete(nsICancelable *request,
|
|
|
|
nsIDNSRecord *rec,
|
|
|
|
nsresult status)
|
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2008-12-05 12:53:24 -08:00
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
nsHTMLDNSPrefetch::nsDeferrals::nsDeferrals()
|
|
|
|
: mHead(0),
|
|
|
|
mTail(0),
|
2009-01-12 07:36:55 -08:00
|
|
|
mActiveLoaderCount(0),
|
2011-10-17 07:59:28 -07:00
|
|
|
mTimerArmed(false)
|
2008-11-04 07:22:04 -08:00
|
|
|
{
|
2009-01-12 07:36:55 -08:00
|
|
|
mTimer = do_CreateInstance("@mozilla.org/timer;1");
|
2008-11-04 07:22:04 -08:00
|
|
|
}
|
|
|
|
|
2008-12-11 11:16:00 -08:00
|
|
|
nsHTMLDNSPrefetch::nsDeferrals::~nsDeferrals()
|
|
|
|
{
|
2009-01-12 07:36:55 -08:00
|
|
|
if (mTimerArmed) {
|
2011-10-17 07:59:28 -07:00
|
|
|
mTimerArmed = false;
|
2009-01-12 07:36:55 -08:00
|
|
|
mTimer->Cancel();
|
2008-12-11 11:16:00 -08:00
|
|
|
}
|
2009-01-12 07:36:55 -08:00
|
|
|
|
|
|
|
Flush();
|
2008-12-11 11:16:00 -08:00
|
|
|
}
|
|
|
|
|
2014-04-27 00:06:00 -07:00
|
|
|
NS_IMPL_ISUPPORTS(nsHTMLDNSPrefetch::nsDeferrals,
|
|
|
|
nsIWebProgressListener,
|
|
|
|
nsISupportsWeakReference,
|
|
|
|
nsIObserver)
|
2009-01-12 07:36:55 -08:00
|
|
|
|
|
|
|
void
|
|
|
|
nsHTMLDNSPrefetch::nsDeferrals::Flush()
|
|
|
|
{
|
|
|
|
while (mHead != mTail) {
|
2012-07-30 07:20:58 -07:00
|
|
|
mEntries[mTail].mElement = nullptr;
|
2009-01-12 07:36:55 -08:00
|
|
|
mTail = (mTail + 1) & sMaxDeferredMask;
|
|
|
|
}
|
|
|
|
}
|
2008-12-05 12:53:24 -08:00
|
|
|
|
2008-11-04 07:22:04 -08:00
|
|
|
nsresult
|
2012-08-22 08:56:38 -07:00
|
|
|
nsHTMLDNSPrefetch::nsDeferrals::Add(uint16_t flags, Link *aElement)
|
2008-12-05 12:53:24 -08:00
|
|
|
{
|
|
|
|
// 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");
|
|
|
|
|
2012-01-20 15:14:46 -08:00
|
|
|
aElement->OnDNSPrefetchDeferred();
|
|
|
|
|
2008-12-05 12:53:24 -08:00
|
|
|
if (((mHead + 1) & sMaxDeferredMask) == mTail)
|
|
|
|
return NS_ERROR_DNS_LOOKUP_QUEUE_FULL;
|
|
|
|
|
|
|
|
mEntries[mHead].mFlags = flags;
|
2009-12-21 09:43:05 -08:00
|
|
|
mEntries[mHead].mElement = do_GetWeakReference(aElement);
|
2008-12-05 12:53:24 -08:00
|
|
|
mHead = (mHead + 1) & sMaxDeferredMask;
|
|
|
|
|
2009-01-12 07:36:55 -08:00
|
|
|
if (!mActiveLoaderCount && !mTimerArmed && mTimer) {
|
2011-10-17 07:59:28 -07:00
|
|
|
mTimerArmed = true;
|
2009-01-12 07:36:55 -08:00
|
|
|
mTimer->InitWithFuncCallback(Tick, this, 2000, nsITimer::TYPE_ONE_SHOT);
|
|
|
|
}
|
|
|
|
|
2008-12-05 12:53:24 -08:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsHTMLDNSPrefetch::nsDeferrals::SubmitQueue()
|
|
|
|
{
|
2009-01-12 07:36:55 -08:00
|
|
|
NS_ASSERTION(NS_IsMainThread(), "nsDeferrals::SubmitQueue must be on main thread");
|
2008-12-05 12:53:24 -08:00
|
|
|
nsCString hostName;
|
|
|
|
if (!sDNSService) return;
|
|
|
|
|
|
|
|
while (mHead != mTail) {
|
2009-12-21 09:43:05 -08:00
|
|
|
nsCOMPtr<nsIContent> content = do_QueryReferent(mEntries[mTail].mElement);
|
2011-10-18 04:19:44 -07:00
|
|
|
if (content) {
|
2010-02-24 08:37:03 -08:00
|
|
|
nsCOMPtr<Link> link = do_QueryInterface(content);
|
2012-01-20 15:14:46 -08:00
|
|
|
// Only prefetch here if request was deferred and deferral not cancelled
|
|
|
|
if (link && link->HasDeferredDNSPrefetchRequest()) {
|
2012-07-30 07:20:58 -07:00
|
|
|
nsCOMPtr<nsIURI> hrefURI(link ? link->GetURI() : nullptr);
|
2012-01-20 15:14:46 -08:00
|
|
|
if (hrefURI)
|
|
|
|
hrefURI->GetAsciiHost(hostName);
|
|
|
|
|
|
|
|
if (!hostName.IsEmpty()) {
|
|
|
|
if (IsNeckoChild()) {
|
|
|
|
gNeckoChild->SendHTMLDNSPrefetch(NS_ConvertUTF8toUTF16(hostName),
|
2012-01-13 22:21:08 -08:00
|
|
|
mEntries[mTail].mFlags);
|
2012-01-20 15:14:46 -08:00
|
|
|
} else {
|
|
|
|
nsCOMPtr<nsICancelable> tmpOutstanding;
|
|
|
|
|
|
|
|
nsresult rv = sDNSService->AsyncResolve(hostName,
|
|
|
|
mEntries[mTail].mFlags
|
|
|
|
| nsIDNSService::RESOLVE_SPECULATE,
|
2012-07-30 07:20:58 -07:00
|
|
|
sDNSListener, nullptr,
|
2012-01-20 15:14:46 -08:00
|
|
|
getter_AddRefs(tmpOutstanding));
|
|
|
|
// Tell link that deferred prefetch was requested
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
link->OnDNSPrefetchRequested();
|
|
|
|
}
|
2012-01-13 22:21:08 -08:00
|
|
|
}
|
2009-01-16 11:21:32 -08:00
|
|
|
}
|
2008-12-05 12:53:24 -08:00
|
|
|
}
|
2009-01-16 11:21:32 -08:00
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
mEntries[mTail].mElement = nullptr;
|
2008-12-05 12:53:24 -08:00
|
|
|
mTail = (mTail + 1) & sMaxDeferredMask;
|
|
|
|
}
|
2009-01-12 07:36:55 -08:00
|
|
|
|
|
|
|
if (mTimerArmed) {
|
2011-10-17 07:59:28 -07:00
|
|
|
mTimerArmed = false;
|
2009-01-12 07:36:55 -08:00
|
|
|
mTimer->Cancel();
|
|
|
|
}
|
2008-12-05 12:53:24 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsHTMLDNSPrefetch::nsDeferrals::Activate()
|
|
|
|
{
|
|
|
|
// Register as an observer for the document loader
|
|
|
|
nsCOMPtr<nsIWebProgress> progress =
|
|
|
|
do_GetService(NS_DOCUMENTLOADER_SERVICE_CONTRACTID);
|
|
|
|
if (progress)
|
|
|
|
progress->AddProgressListener(this, nsIWebProgress::NOTIFY_STATE_DOCUMENT);
|
2009-01-12 07:36:55 -08:00
|
|
|
|
|
|
|
// Register as an observer for xpcom shutdown events so we can drop any element refs
|
|
|
|
nsCOMPtr<nsIObserverService> observerService =
|
Bug 560095 - Use mozilla::services::GetObserverService(). r=biesi,dveditz,gavin,josh,jst,mrbkap,roc,sdwilsh,shaver,sicking,smontagu,surkov
2010-04-29 09:59:13 -07:00
|
|
|
mozilla::services::GetObserverService();
|
|
|
|
if (observerService)
|
2011-10-17 07:59:28 -07:00
|
|
|
observerService->AddObserver(this, "xpcom-shutdown", true);
|
2009-01-12 07:36:55 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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");
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
self->mTimerArmed = false;
|
2009-01-12 07:36:55 -08:00
|
|
|
|
|
|
|
// 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();
|
2008-12-05 12:53:24 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
//////////// nsIWebProgressListener methods
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHTMLDNSPrefetch::nsDeferrals::OnStateChange(nsIWebProgress* aWebProgress,
|
|
|
|
nsIRequest *aRequest,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t progressStateFlags,
|
2008-12-05 12:53:24 -08:00
|
|
|
nsresult aStatus)
|
2008-11-04 07:22:04 -08:00
|
|
|
{
|
2008-12-05 12:53:24 -08:00
|
|
|
// The FIFO has no lock, so it can only be accessed on main thread
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "nsDeferrals::OnStateChange must be on main thread");
|
|
|
|
|
|
|
|
if (progressStateFlags & STATE_IS_DOCUMENT) {
|
|
|
|
if (progressStateFlags & STATE_STOP) {
|
|
|
|
|
|
|
|
// Initialization may have missed a STATE_START notification, so do
|
|
|
|
// not go negative
|
|
|
|
if (mActiveLoaderCount)
|
|
|
|
mActiveLoaderCount--;
|
|
|
|
|
|
|
|
if (!mActiveLoaderCount)
|
|
|
|
SubmitQueue();
|
|
|
|
}
|
|
|
|
else if (progressStateFlags & STATE_START)
|
|
|
|
mActiveLoaderCount++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
2008-11-04 07:22:04 -08:00
|
|
|
}
|
|
|
|
|
2008-12-05 12:53:24 -08:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHTMLDNSPrefetch::nsDeferrals::OnProgressChange(nsIWebProgress *aProgress,
|
|
|
|
nsIRequest *aRequest,
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t curSelfProgress,
|
|
|
|
int32_t maxSelfProgress,
|
|
|
|
int32_t curTotalProgress,
|
|
|
|
int32_t maxTotalProgress)
|
2008-12-05 12:53:24 -08:00
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2008-11-04 07:22:04 -08:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2008-12-05 12:53:24 -08:00
|
|
|
nsHTMLDNSPrefetch::nsDeferrals::OnLocationChange(nsIWebProgress* aWebProgress,
|
|
|
|
nsIRequest* aRequest,
|
2011-11-10 06:01:11 -08:00
|
|
|
nsIURI *location,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t aFlags)
|
2008-12-05 12:53:24 -08:00
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHTMLDNSPrefetch::nsDeferrals::OnStatusChange(nsIWebProgress* aWebProgress,
|
|
|
|
nsIRequest* aRequest,
|
|
|
|
nsresult aStatus,
|
2014-01-04 07:02:17 -08:00
|
|
|
const char16_t* aMessage)
|
2008-12-05 12:53:24 -08:00
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHTMLDNSPrefetch::nsDeferrals::OnSecurityChange(nsIWebProgress *aWebProgress,
|
|
|
|
nsIRequest *aRequest,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t state)
|
2008-11-04 07:22:04 -08:00
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2009-01-12 07:36:55 -08:00
|
|
|
|
|
|
|
//////////// nsIObserver method
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHTMLDNSPrefetch::nsDeferrals::Observe(nsISupports *subject,
|
|
|
|
const char *topic,
|
2014-01-04 07:02:17 -08:00
|
|
|
const char16_t *data)
|
2009-01-12 07:36:55 -08:00
|
|
|
{
|
|
|
|
if (!strcmp(topic, "xpcom-shutdown"))
|
|
|
|
Flush();
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|