2012-05-21 15:12:37 +04: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/. */
|
2002-09-02 04:14:20 +04:00
|
|
|
|
|
|
|
#include "nsPrefetchService.h"
|
2007-05-15 00:09:20 +04:00
|
|
|
#include "nsICacheSession.h"
|
|
|
|
#include "nsICacheService.h"
|
2002-09-02 04:14:20 +04:00
|
|
|
#include "nsIServiceManager.h"
|
|
|
|
#include "nsICategoryManager.h"
|
|
|
|
#include "nsIObserverService.h"
|
|
|
|
#include "nsIWebProgress.h"
|
|
|
|
#include "nsCURILoader.h"
|
|
|
|
#include "nsICachingChannel.h"
|
|
|
|
#include "nsICacheVisitor.h"
|
|
|
|
#include "nsIHttpChannel.h"
|
|
|
|
#include "nsIURL.h"
|
2007-07-09 10:19:27 +04:00
|
|
|
#include "nsISimpleEnumerator.h"
|
2002-09-02 04:14:20 +04:00
|
|
|
#include "nsNetUtil.h"
|
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsXPIDLString.h"
|
|
|
|
#include "nsReadableUtils.h"
|
2007-07-09 10:19:27 +04:00
|
|
|
#include "nsStreamUtils.h"
|
2005-06-30 00:57:34 +04:00
|
|
|
#include "nsAutoPtr.h"
|
2002-09-02 04:14:20 +04:00
|
|
|
#include "prtime.h"
|
|
|
|
#include "prlog.h"
|
|
|
|
#include "plstr.h"
|
2010-08-05 06:15:55 +04:00
|
|
|
#include "nsIAsyncVerifyRedirectCallback.h"
|
2011-07-01 12:35:28 +04:00
|
|
|
#include "mozilla/Preferences.h"
|
2012-06-13 07:53:09 +04:00
|
|
|
#include "mozilla/Attributes.h"
|
2012-07-02 03:45:59 +04:00
|
|
|
#include "nsIDOMNode.h"
|
2012-09-07 22:06:44 +04:00
|
|
|
#include "nsINode.h"
|
|
|
|
#include "nsIDocument.h"
|
2011-07-01 12:35:28 +04:00
|
|
|
|
|
|
|
using namespace mozilla;
|
2002-09-02 04:14:20 +04:00
|
|
|
|
|
|
|
#if defined(PR_LOGGING)
|
|
|
|
//
|
|
|
|
// To enable logging (see prlog.h for full details):
|
|
|
|
//
|
|
|
|
// set NSPR_LOG_MODULES=nsPrefetch:5
|
|
|
|
// set NSPR_LOG_FILE=prefetch.log
|
|
|
|
//
|
|
|
|
// this enables PR_LOG_ALWAYS level information and places all output in
|
|
|
|
// the file http.log
|
|
|
|
//
|
|
|
|
static PRLogModuleInfo *gPrefetchLog;
|
|
|
|
#endif
|
|
|
|
#define LOG(args) PR_LOG(gPrefetchLog, 4, args)
|
|
|
|
#define LOG_ENABLED() PR_LOG_TEST(gPrefetchLog, 4)
|
|
|
|
|
2003-01-28 01:23:28 +03:00
|
|
|
#define PREFETCH_PREF "network.prefetch-next"
|
|
|
|
|
2002-09-02 04:14:20 +04:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// helpers
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
static inline uint32_t
|
2002-09-02 04:14:20 +04:00
|
|
|
PRTimeToSeconds(PRTime t_usec)
|
|
|
|
{
|
2012-10-12 21:29:11 +04:00
|
|
|
PRTime usec_per_sec = PR_USEC_PER_SEC;
|
|
|
|
return uint32_t(t_usec /= usec_per_sec);
|
2002-09-02 04:14:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
#define NowInSeconds() PRTimeToSeconds(PR_Now())
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2007-07-09 10:19:27 +04:00
|
|
|
// nsPrefetchQueueEnumerator
|
2002-09-02 04:14:20 +04:00
|
|
|
//-----------------------------------------------------------------------------
|
2012-06-13 07:53:09 +04:00
|
|
|
class nsPrefetchQueueEnumerator MOZ_FINAL : public nsISimpleEnumerator
|
2007-07-09 10:19:27 +04:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSISIMPLEENUMERATOR
|
2008-08-25 07:37:10 +04:00
|
|
|
nsPrefetchQueueEnumerator(nsPrefetchService *aService);
|
2007-07-09 10:19:27 +04:00
|
|
|
~nsPrefetchQueueEnumerator();
|
|
|
|
|
|
|
|
private:
|
|
|
|
void Increment();
|
|
|
|
|
|
|
|
nsRefPtr<nsPrefetchService> mService;
|
|
|
|
nsRefPtr<nsPrefetchNode> mCurrent;
|
2011-09-29 10:19:26 +04:00
|
|
|
bool mStarted;
|
2007-07-09 10:19:27 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsPrefetchQueueEnumerator <public>
|
|
|
|
//-----------------------------------------------------------------------------
|
2008-08-25 07:37:10 +04:00
|
|
|
nsPrefetchQueueEnumerator::nsPrefetchQueueEnumerator(nsPrefetchService *aService)
|
2007-07-09 10:19:27 +04:00
|
|
|
: mService(aService)
|
2011-10-17 18:59:28 +04:00
|
|
|
, mStarted(false)
|
2007-07-09 10:19:27 +04:00
|
|
|
{
|
|
|
|
Increment();
|
|
|
|
}
|
2002-09-02 04:14:20 +04:00
|
|
|
|
2007-07-09 10:19:27 +04:00
|
|
|
nsPrefetchQueueEnumerator::~nsPrefetchQueueEnumerator()
|
2002-09-02 04:14:20 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2007-07-09 10:19:27 +04:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsPrefetchQueueEnumerator::nsISimpleEnumerator
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
NS_IMETHODIMP
|
2011-09-29 10:19:26 +04:00
|
|
|
nsPrefetchQueueEnumerator::HasMoreElements(bool *aHasMore)
|
2007-07-09 10:19:27 +04:00
|
|
|
{
|
2012-07-30 18:20:58 +04:00
|
|
|
*aHasMore = (mCurrent != nullptr);
|
2007-07-09 10:19:27 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPrefetchQueueEnumerator::GetNext(nsISupports **aItem)
|
|
|
|
{
|
|
|
|
if (!mCurrent) return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
NS_ADDREF(*aItem = static_cast<nsIDOMLoadStatus*>(mCurrent.get()));
|
|
|
|
|
|
|
|
Increment();
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsPrefetchQueueEnumerator <private>
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void
|
|
|
|
nsPrefetchQueueEnumerator::Increment()
|
2002-09-02 04:14:20 +04:00
|
|
|
{
|
2009-02-25 10:12:12 +03:00
|
|
|
if (!mStarted) {
|
|
|
|
// If the service is currently serving a request, it won't be in
|
|
|
|
// the pending queue, so we return it first. If it isn't, we'll
|
|
|
|
// just start with the pending queue.
|
2011-10-17 18:59:28 +04:00
|
|
|
mStarted = true;
|
2009-02-25 10:12:12 +03:00
|
|
|
mCurrent = mService->GetCurrentNode();
|
|
|
|
if (!mCurrent)
|
|
|
|
mCurrent = mService->GetQueueHead();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mCurrent) {
|
|
|
|
if (mCurrent == mService->GetCurrentNode()) {
|
|
|
|
// If we just returned the node being processed by the service,
|
|
|
|
// start with the pending queue
|
|
|
|
mCurrent = mService->GetQueueHead();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Otherwise just advance to the next item in the queue
|
|
|
|
mCurrent = mCurrent->mNext;
|
|
|
|
}
|
|
|
|
}
|
2002-09-02 04:14:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2007-07-09 10:19:27 +04:00
|
|
|
// nsPrefetchQueueEnumerator::nsISupports
|
2002-09-02 04:14:20 +04:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2007-07-09 10:19:27 +04:00
|
|
|
NS_IMPL_ISUPPORTS1(nsPrefetchQueueEnumerator, nsISimpleEnumerator)
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsPrefetchNode <public>
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
nsPrefetchNode::nsPrefetchNode(nsPrefetchService *aService,
|
|
|
|
nsIURI *aURI,
|
|
|
|
nsIURI *aReferrerURI,
|
2008-08-25 07:37:10 +04:00
|
|
|
nsIDOMNode *aSource)
|
2012-07-30 18:20:58 +04:00
|
|
|
: mNext(nullptr)
|
2007-07-09 10:19:27 +04:00
|
|
|
, mURI(aURI)
|
|
|
|
, mReferrerURI(aReferrerURI)
|
|
|
|
, mService(aService)
|
2012-07-30 18:20:58 +04:00
|
|
|
, mChannel(nullptr)
|
2007-07-09 10:19:27 +04:00
|
|
|
, mState(nsIDOMLoadStatus::UNINITIALIZED)
|
|
|
|
, mBytesRead(0)
|
|
|
|
{
|
|
|
|
mSource = do_GetWeakReference(aSource);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsPrefetchNode::OpenChannel()
|
|
|
|
{
|
2012-09-07 22:06:44 +04:00
|
|
|
nsCOMPtr<nsINode> source = do_QueryReferent(mSource);
|
|
|
|
if (!source) {
|
|
|
|
// Don't attempt to prefetch if we don't have a source node
|
|
|
|
// (which should never happen).
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
nsCOMPtr<nsILoadGroup> loadGroup = source->OwnerDoc()->GetDocumentLoadGroup();
|
2007-07-09 10:19:27 +04:00
|
|
|
nsresult rv = NS_NewChannel(getter_AddRefs(mChannel),
|
|
|
|
mURI,
|
2012-09-07 22:06:44 +04:00
|
|
|
nullptr, loadGroup, this,
|
2007-07-09 10:19:27 +04:00
|
|
|
nsIRequest::LOAD_BACKGROUND |
|
|
|
|
nsICachingChannel::LOAD_ONLY_IF_MODIFIED);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// configure HTTP specific stuff
|
|
|
|
nsCOMPtr<nsIHttpChannel> httpChannel =
|
|
|
|
do_QueryInterface(mChannel);
|
|
|
|
if (httpChannel) {
|
|
|
|
httpChannel->SetReferrer(mReferrerURI);
|
|
|
|
httpChannel->SetRequestHeader(
|
|
|
|
NS_LITERAL_CSTRING("X-Moz"),
|
|
|
|
NS_LITERAL_CSTRING("prefetch"),
|
2011-10-17 18:59:28 +04:00
|
|
|
false);
|
2007-07-09 10:19:27 +04:00
|
|
|
}
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
rv = mChannel->AsyncOpen(this, nullptr);
|
2007-07-09 10:19:27 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
mState = nsIDOMLoadStatus::REQUESTED;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsPrefetchNode::CancelChannel(nsresult error)
|
2002-09-02 04:14:20 +04:00
|
|
|
{
|
2007-07-09 10:19:27 +04:00
|
|
|
mChannel->Cancel(error);
|
2012-07-30 18:20:58 +04:00
|
|
|
mChannel = nullptr;
|
2007-07-09 10:19:27 +04:00
|
|
|
|
|
|
|
mState = nsIDOMLoadStatus::UNINITIALIZED;
|
|
|
|
|
2002-09-02 04:14:20 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2007-07-09 10:19:27 +04:00
|
|
|
// nsPrefetchNode::nsISupports
|
2002-09-02 04:14:20 +04:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2012-07-31 22:59:17 +04:00
|
|
|
NS_IMPL_ISUPPORTS6(nsPrefetchNode,
|
2007-07-09 10:19:27 +04:00
|
|
|
nsIDOMLoadStatus,
|
2002-09-02 04:14:20 +04:00
|
|
|
nsIRequestObserver,
|
2005-06-30 00:57:34 +04:00
|
|
|
nsIStreamListener,
|
|
|
|
nsIInterfaceRequestor,
|
2012-07-31 22:59:17 +04:00
|
|
|
nsIChannelEventSink,
|
|
|
|
nsIRedirectResultListener)
|
2002-09-02 04:14:20 +04:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2007-07-09 10:19:27 +04:00
|
|
|
// nsPrefetchNode::nsIStreamListener
|
2002-09-02 04:14:20 +04:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2007-07-09 10:19:27 +04:00
|
|
|
nsPrefetchNode::OnStartRequest(nsIRequest *aRequest,
|
|
|
|
nsISupports *aContext)
|
2002-09-02 04:14:20 +04:00
|
|
|
{
|
2002-10-24 07:17:44 +04:00
|
|
|
nsresult rv;
|
|
|
|
|
2007-03-14 04:52:07 +03:00
|
|
|
nsCOMPtr<nsICachingChannel> cachingChannel =
|
|
|
|
do_QueryInterface(aRequest, &rv);
|
2002-10-24 07:17:44 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2008-08-25 07:37:10 +04:00
|
|
|
// no need to prefetch a document that is already in the cache
|
2011-09-29 10:19:26 +04:00
|
|
|
bool fromCache;
|
2008-08-25 07:37:10 +04:00
|
|
|
if (NS_SUCCEEDED(cachingChannel->IsFromCache(&fromCache)) &&
|
|
|
|
fromCache) {
|
|
|
|
LOG(("document is already in the cache; canceling prefetch\n"));
|
|
|
|
return NS_BINDING_ABORTED;
|
|
|
|
}
|
2008-08-20 09:54:37 +04:00
|
|
|
|
2008-08-25 07:37:10 +04:00
|
|
|
//
|
|
|
|
// no need to prefetch a document that must be requested fresh each
|
|
|
|
// and every time.
|
|
|
|
//
|
|
|
|
nsCOMPtr<nsISupports> cacheToken;
|
|
|
|
cachingChannel->GetCacheToken(getter_AddRefs(cacheToken));
|
|
|
|
if (!cacheToken)
|
|
|
|
return NS_ERROR_ABORT; // bail, no cache entry
|
2008-08-20 09:54:37 +04:00
|
|
|
|
2008-08-25 07:37:10 +04:00
|
|
|
nsCOMPtr<nsICacheEntryInfo> entryInfo =
|
|
|
|
do_QueryInterface(cacheToken, &rv);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
2008-08-20 09:54:37 +04:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t expTime;
|
2008-08-25 07:37:10 +04:00
|
|
|
if (NS_SUCCEEDED(entryInfo->GetExpirationTime(&expTime))) {
|
|
|
|
if (NowInSeconds() >= expTime) {
|
|
|
|
LOG(("document cannot be reused from cache; "
|
|
|
|
"canceling prefetch\n"));
|
|
|
|
return NS_BINDING_ABORTED;
|
2008-08-20 09:54:37 +04:00
|
|
|
}
|
2002-09-02 04:14:20 +04:00
|
|
|
}
|
2007-07-09 10:19:27 +04:00
|
|
|
|
|
|
|
mState = nsIDOMLoadStatus::RECEIVING;
|
|
|
|
|
2002-09-02 04:14:20 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2007-07-09 10:19:27 +04:00
|
|
|
nsPrefetchNode::OnDataAvailable(nsIRequest *aRequest,
|
|
|
|
nsISupports *aContext,
|
|
|
|
nsIInputStream *aStream,
|
2012-09-06 06:41:02 +04:00
|
|
|
uint64_t aOffset,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t aCount)
|
2002-09-02 04:14:20 +04:00
|
|
|
{
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t bytesRead = 0;
|
2012-07-30 18:20:58 +04:00
|
|
|
aStream->ReadSegments(NS_DiscardSegment, nullptr, aCount, &bytesRead);
|
2007-07-09 10:19:27 +04:00
|
|
|
mBytesRead += bytesRead;
|
2012-09-06 06:41:02 +04:00
|
|
|
LOG(("prefetched %u bytes [offset=%llu]\n", bytesRead, aOffset));
|
2002-09-02 04:14:20 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2007-07-09 10:19:27 +04:00
|
|
|
|
2002-09-02 04:14:20 +04:00
|
|
|
NS_IMETHODIMP
|
2007-07-09 10:19:27 +04:00
|
|
|
nsPrefetchNode::OnStopRequest(nsIRequest *aRequest,
|
|
|
|
nsISupports *aContext,
|
|
|
|
nsresult aStatus)
|
2002-09-02 04:14:20 +04:00
|
|
|
{
|
|
|
|
LOG(("done prefetching [status=%x]\n", aStatus));
|
|
|
|
|
2007-07-09 10:19:27 +04:00
|
|
|
mState = nsIDOMLoadStatus::LOADED;
|
|
|
|
|
|
|
|
if (mBytesRead == 0 && aStatus == NS_OK) {
|
|
|
|
// we didn't need to read (because LOAD_ONLY_IF_MODIFIED was
|
|
|
|
// specified), but the object should report loadedSize as if it
|
|
|
|
// did.
|
|
|
|
mChannel->GetContentLength(&mBytesRead);
|
|
|
|
}
|
|
|
|
|
|
|
|
mService->NotifyLoadCompleted(this);
|
2002-09-02 04:14:20 +04:00
|
|
|
mService->ProcessNextURI();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2005-06-30 00:57:34 +04:00
|
|
|
//-----------------------------------------------------------------------------
|
2007-07-09 10:19:27 +04:00
|
|
|
// nsPrefetchNode::nsIInterfaceRequestor
|
2005-06-30 00:57:34 +04:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2007-07-09 10:19:27 +04:00
|
|
|
nsPrefetchNode::GetInterface(const nsIID &aIID, void **aResult)
|
2005-06-30 00:57:34 +04:00
|
|
|
{
|
|
|
|
if (aIID.Equals(NS_GET_IID(nsIChannelEventSink))) {
|
|
|
|
NS_ADDREF_THIS();
|
2007-07-08 11:08:04 +04:00
|
|
|
*aResult = static_cast<nsIChannelEventSink *>(this);
|
2005-06-30 00:57:34 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-07-31 22:59:17 +04:00
|
|
|
if (aIID.Equals(NS_GET_IID(nsIRedirectResultListener))) {
|
|
|
|
NS_ADDREF_THIS();
|
|
|
|
*aResult = static_cast<nsIRedirectResultListener *>(this);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2005-06-30 00:57:34 +04:00
|
|
|
return NS_ERROR_NO_INTERFACE;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2007-07-09 10:19:27 +04:00
|
|
|
// nsPrefetchNode::nsIChannelEventSink
|
2005-06-30 00:57:34 +04:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2010-08-05 06:15:55 +04:00
|
|
|
nsPrefetchNode::AsyncOnChannelRedirect(nsIChannel *aOldChannel,
|
|
|
|
nsIChannel *aNewChannel,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t aFlags,
|
2010-08-05 06:15:55 +04:00
|
|
|
nsIAsyncVerifyRedirectCallback *callback)
|
2005-06-30 00:57:34 +04:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIURI> newURI;
|
|
|
|
nsresult rv = aNewChannel->GetURI(getter_AddRefs(newURI));
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool match;
|
2005-06-30 00:57:34 +04:00
|
|
|
rv = newURI->SchemeIs("http", &match);
|
|
|
|
if (NS_FAILED(rv) || !match) {
|
2008-09-30 12:53:48 +04:00
|
|
|
rv = newURI->SchemeIs("https", &match);
|
|
|
|
if (NS_FAILED(rv) || !match) {
|
|
|
|
LOG(("rejected: URL is not of type http/https\n"));
|
|
|
|
return NS_ERROR_ABORT;
|
|
|
|
}
|
2005-06-30 00:57:34 +04:00
|
|
|
}
|
|
|
|
|
2006-01-25 03:48:40 +03:00
|
|
|
// HTTP request headers are not automatically forwarded to the new channel.
|
|
|
|
nsCOMPtr<nsIHttpChannel> httpChannel = do_QueryInterface(aNewChannel);
|
|
|
|
NS_ENSURE_STATE(httpChannel);
|
|
|
|
|
|
|
|
httpChannel->SetRequestHeader(NS_LITERAL_CSTRING("X-Moz"),
|
2008-08-25 07:37:10 +04:00
|
|
|
NS_LITERAL_CSTRING("prefetch"),
|
2011-10-17 18:59:28 +04:00
|
|
|
false);
|
2006-01-25 03:48:40 +03:00
|
|
|
|
2012-07-31 22:59:17 +04:00
|
|
|
// Assign to mChannel after we get notification about success of the
|
|
|
|
// redirect in OnRedirectResult.
|
|
|
|
mRedirectChannel = aNewChannel;
|
2007-07-09 10:19:27 +04:00
|
|
|
|
2010-08-05 06:15:55 +04:00
|
|
|
callback->OnRedirectVerifyCallback(NS_OK);
|
2005-06-30 00:57:34 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-07-31 22:59:17 +04:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsPrefetchNode::nsIRedirectResultListener
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPrefetchNode::OnRedirectResult(bool proceeding)
|
|
|
|
{
|
|
|
|
if (proceeding && mRedirectChannel)
|
|
|
|
mChannel = mRedirectChannel;
|
|
|
|
|
2012-08-12 14:43:47 +04:00
|
|
|
mRedirectChannel = nullptr;
|
2012-07-31 22:59:17 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2007-07-09 10:19:27 +04:00
|
|
|
|
2002-09-02 04:14:20 +04:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsPrefetchService <public>
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
nsPrefetchService::nsPrefetchService()
|
2012-07-30 18:20:58 +04:00
|
|
|
: mQueueHead(nullptr)
|
|
|
|
, mQueueTail(nullptr)
|
2002-09-11 07:12:13 +04:00
|
|
|
, mStopCount(0)
|
2011-10-17 18:59:28 +04:00
|
|
|
, mHaveProcessed(false)
|
|
|
|
, mDisabled(true)
|
2002-09-02 04:14:20 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsPrefetchService::~nsPrefetchService()
|
|
|
|
{
|
2011-07-01 12:35:28 +04:00
|
|
|
Preferences::RemoveObserver(this, PREFETCH_PREF);
|
2002-09-02 04:14:20 +04:00
|
|
|
// cannot reach destructor if prefetch in progress (listener owns reference
|
|
|
|
// to this service)
|
2008-08-25 07:37:10 +04:00
|
|
|
EmptyQueue();
|
2002-09-02 04:14:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsPrefetchService::Init()
|
|
|
|
{
|
|
|
|
#if defined(PR_LOGGING)
|
|
|
|
if (!gPrefetchLog)
|
|
|
|
gPrefetchLog = PR_NewLogModule("nsPrefetch");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
|
2003-01-28 01:23:28 +03:00
|
|
|
// read prefs and hook up pref observer
|
2011-07-01 12:35:28 +04:00
|
|
|
mDisabled = !Preferences::GetBool(PREFETCH_PREF, !mDisabled);
|
|
|
|
Preferences::AddWeakObserver(this, PREFETCH_PREF);
|
2002-09-02 04:14:20 +04:00
|
|
|
|
|
|
|
// Observe xpcom-shutdown event
|
Bug 560095 - Use mozilla::services::GetObserverService(). r=biesi,dveditz,gavin,josh,jst,mrbkap,roc,sdwilsh,shaver,sicking,smontagu,surkov
2010-04-29 20:59:13 +04:00
|
|
|
nsCOMPtr<nsIObserverService> observerService =
|
|
|
|
mozilla::services::GetObserverService();
|
|
|
|
if (!observerService)
|
|
|
|
return NS_ERROR_FAILURE;
|
2002-09-02 04:14:20 +04:00
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
rv = observerService->AddObserver(this, NS_XPCOM_SHUTDOWN_OBSERVER_ID, true);
|
Bug 560095 - Use mozilla::services::GetObserverService(). r=biesi,dveditz,gavin,josh,jst,mrbkap,roc,sdwilsh,shaver,sicking,smontagu,surkov
2010-04-29 20:59:13 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2002-09-02 04:14:20 +04:00
|
|
|
|
2003-01-28 01:23:28 +03:00
|
|
|
if (!mDisabled)
|
|
|
|
AddProgressListener();
|
|
|
|
|
|
|
|
return NS_OK;
|
2002-09-02 04:14:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsPrefetchService::ProcessNextURI()
|
|
|
|
{
|
|
|
|
nsresult rv;
|
2002-10-24 07:17:44 +04:00
|
|
|
nsCOMPtr<nsIURI> uri, referrer;
|
2002-09-02 04:14:20 +04:00
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
mCurrentNode = nullptr;
|
2002-09-02 04:14:20 +04:00
|
|
|
|
|
|
|
do {
|
2007-07-09 10:19:27 +04:00
|
|
|
rv = DequeueNode(getter_AddRefs(mCurrentNode));
|
2007-05-15 00:09:20 +04:00
|
|
|
|
2002-09-02 04:14:20 +04:00
|
|
|
if (NS_FAILED(rv)) break;
|
|
|
|
|
|
|
|
#if defined(PR_LOGGING)
|
|
|
|
if (LOG_ENABLED()) {
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString spec;
|
2007-07-09 10:19:27 +04:00
|
|
|
mCurrentNode->mURI->GetSpec(spec);
|
2002-09-02 04:14:20 +04:00
|
|
|
LOG(("ProcessNextURI [%s]\n", spec.get()));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
//
|
|
|
|
// if opening the channel fails, then just skip to the next uri
|
|
|
|
//
|
2007-07-09 10:19:27 +04:00
|
|
|
rv = mCurrentNode->OpenChannel();
|
2002-09-02 04:14:20 +04:00
|
|
|
}
|
|
|
|
while (NS_FAILED(rv));
|
|
|
|
}
|
|
|
|
|
2007-07-09 10:19:27 +04:00
|
|
|
void
|
|
|
|
nsPrefetchService::NotifyLoadRequested(nsPrefetchNode *node)
|
|
|
|
{
|
|
|
|
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 20:59:13 +04:00
|
|
|
mozilla::services::GetObserverService();
|
|
|
|
if (!observerService)
|
|
|
|
return;
|
2007-07-09 10:19:27 +04:00
|
|
|
|
|
|
|
observerService->NotifyObservers(static_cast<nsIDOMLoadStatus*>(node),
|
2012-07-30 18:20:58 +04:00
|
|
|
"prefetch-load-requested", nullptr);
|
2007-07-09 10:19:27 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsPrefetchService::NotifyLoadCompleted(nsPrefetchNode *node)
|
|
|
|
{
|
|
|
|
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 20:59:13 +04:00
|
|
|
mozilla::services::GetObserverService();
|
|
|
|
if (!observerService)
|
|
|
|
return;
|
2007-07-09 10:19:27 +04:00
|
|
|
|
|
|
|
observerService->NotifyObservers(static_cast<nsIDOMLoadStatus*>(node),
|
2012-07-30 18:20:58 +04:00
|
|
|
"prefetch-load-completed", nullptr);
|
2007-07-09 10:19:27 +04:00
|
|
|
}
|
|
|
|
|
2002-09-02 04:14:20 +04:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsPrefetchService <private>
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2003-01-28 01:23:28 +03:00
|
|
|
void
|
|
|
|
nsPrefetchService::AddProgressListener()
|
|
|
|
{
|
|
|
|
// Register as an observer for the document loader
|
2007-05-15 00:09:20 +04:00
|
|
|
nsCOMPtr<nsIWebProgress> progress =
|
|
|
|
do_GetService(NS_DOCUMENTLOADER_SERVICE_CONTRACTID);
|
2003-01-28 01:23:28 +03:00
|
|
|
if (progress)
|
|
|
|
progress->AddProgressListener(this, nsIWebProgress::NOTIFY_STATE_DOCUMENT);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsPrefetchService::RemoveProgressListener()
|
|
|
|
{
|
|
|
|
// Register as an observer for the document loader
|
2007-05-15 00:09:20 +04:00
|
|
|
nsCOMPtr<nsIWebProgress> progress =
|
|
|
|
do_GetService(NS_DOCUMENTLOADER_SERVICE_CONTRACTID);
|
2003-01-28 01:23:28 +03:00
|
|
|
if (progress)
|
|
|
|
progress->RemoveProgressListener(this);
|
|
|
|
}
|
|
|
|
|
2002-09-02 04:14:20 +04:00
|
|
|
nsresult
|
2007-07-09 10:19:27 +04:00
|
|
|
nsPrefetchService::EnqueueNode(nsPrefetchNode *aNode)
|
2002-09-02 04:14:20 +04:00
|
|
|
{
|
2007-07-09 10:19:27 +04:00
|
|
|
NS_ADDREF(aNode);
|
2002-09-02 04:14:20 +04:00
|
|
|
|
|
|
|
if (!mQueueTail) {
|
2007-07-09 10:19:27 +04:00
|
|
|
mQueueHead = aNode;
|
|
|
|
mQueueTail = aNode;
|
2002-09-02 04:14:20 +04:00
|
|
|
}
|
|
|
|
else {
|
2007-07-09 10:19:27 +04:00
|
|
|
mQueueTail->mNext = aNode;
|
|
|
|
mQueueTail = aNode;
|
2002-09-02 04:14:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2007-07-09 10:19:27 +04:00
|
|
|
nsPrefetchService::EnqueueURI(nsIURI *aURI,
|
|
|
|
nsIURI *aReferrerURI,
|
|
|
|
nsIDOMNode *aSource,
|
|
|
|
nsPrefetchNode **aNode)
|
|
|
|
{
|
|
|
|
nsPrefetchNode *node = new nsPrefetchNode(this, aURI, aReferrerURI,
|
2008-08-25 07:37:10 +04:00
|
|
|
aSource);
|
2007-07-09 10:19:27 +04:00
|
|
|
if (!node)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
NS_ADDREF(*aNode = node);
|
|
|
|
|
|
|
|
return EnqueueNode(node);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsPrefetchService::DequeueNode(nsPrefetchNode **node)
|
2002-09-02 04:14:20 +04:00
|
|
|
{
|
|
|
|
if (!mQueueHead)
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
|
2007-07-09 10:19:27 +04:00
|
|
|
// give the ref to the caller
|
|
|
|
*node = mQueueHead;
|
2002-09-02 04:14:20 +04:00
|
|
|
mQueueHead = mQueueHead->mNext;
|
2012-07-30 18:20:58 +04:00
|
|
|
(*node)->mNext = nullptr;
|
2002-09-02 04:14:20 +04:00
|
|
|
|
|
|
|
if (!mQueueHead)
|
2012-07-30 18:20:58 +04:00
|
|
|
mQueueTail = nullptr;
|
2002-09-02 04:14:20 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2008-08-25 07:37:10 +04:00
|
|
|
nsPrefetchService::EmptyQueue()
|
2002-09-02 04:14:20 +04:00
|
|
|
{
|
2009-02-25 10:12:12 +03:00
|
|
|
do {
|
|
|
|
nsRefPtr<nsPrefetchNode> node;
|
|
|
|
DequeueNode(getter_AddRefs(node));
|
|
|
|
} while (mQueueHead);
|
2002-09-02 04:14:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsPrefetchService::StartPrefetching()
|
|
|
|
{
|
2002-09-11 07:12:13 +04:00
|
|
|
//
|
|
|
|
// at initialization time we might miss the first DOCUMENT START
|
|
|
|
// notification, so we have to be careful to avoid letting our
|
|
|
|
// stop count go negative.
|
|
|
|
//
|
|
|
|
if (mStopCount > 0)
|
|
|
|
mStopCount--;
|
2002-09-02 04:14:20 +04:00
|
|
|
|
2002-09-11 07:12:13 +04:00
|
|
|
LOG(("StartPrefetching [stopcount=%d]\n", mStopCount));
|
|
|
|
|
|
|
|
// only start prefetching after we've received enough DOCUMENT
|
|
|
|
// STOP notifications. we do this inorder to defer prefetching
|
|
|
|
// until after all sub-frames have finished loading.
|
2007-07-09 10:19:27 +04:00
|
|
|
if (mStopCount == 0 && !mCurrentNode) {
|
2011-10-17 18:59:28 +04:00
|
|
|
mHaveProcessed = true;
|
2002-09-02 04:14:20 +04:00
|
|
|
ProcessNextURI();
|
2007-05-29 13:45:30 +04:00
|
|
|
}
|
2002-09-02 04:14:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsPrefetchService::StopPrefetching()
|
|
|
|
{
|
2002-09-11 07:12:13 +04:00
|
|
|
mStopCount++;
|
2002-09-02 04:14:20 +04:00
|
|
|
|
2002-09-11 07:12:13 +04:00
|
|
|
LOG(("StopPrefetching [stopcount=%d]\n", mStopCount));
|
2002-09-02 04:14:20 +04:00
|
|
|
|
2002-09-11 07:12:13 +04:00
|
|
|
// only kill the prefetch queue if we've actually started prefetching.
|
2007-07-09 10:19:27 +04:00
|
|
|
if (!mCurrentNode)
|
2002-09-11 07:12:13 +04:00
|
|
|
return;
|
2002-09-02 04:14:20 +04:00
|
|
|
|
2007-07-09 10:19:27 +04:00
|
|
|
mCurrentNode->CancelChannel(NS_BINDING_ABORTED);
|
2012-07-30 18:20:58 +04:00
|
|
|
mCurrentNode = nullptr;
|
2008-08-25 07:37:10 +04:00
|
|
|
EmptyQueue();
|
2002-09-02 04:14:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsPrefetchService::nsISupports
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2002-09-11 07:12:13 +04:00
|
|
|
NS_IMPL_ISUPPORTS4(nsPrefetchService,
|
2002-09-02 04:14:20 +04:00
|
|
|
nsIPrefetchService,
|
|
|
|
nsIWebProgressListener,
|
|
|
|
nsIObserver,
|
|
|
|
nsISupportsWeakReference)
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2007-03-14 04:52:07 +03:00
|
|
|
// nsPrefetchService::nsIPrefetchService
|
2002-09-02 04:14:20 +04:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2007-03-14 04:52:07 +03:00
|
|
|
nsresult
|
|
|
|
nsPrefetchService::Prefetch(nsIURI *aURI,
|
|
|
|
nsIURI *aReferrerURI,
|
2007-07-09 10:19:27 +04:00
|
|
|
nsIDOMNode *aSource,
|
2011-09-29 10:19:26 +04:00
|
|
|
bool aExplicit)
|
2002-09-02 04:14:20 +04:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
2002-10-24 07:17:44 +04:00
|
|
|
NS_ENSURE_ARG_POINTER(aURI);
|
|
|
|
NS_ENSURE_ARG_POINTER(aReferrerURI);
|
|
|
|
|
2002-09-02 04:14:20 +04:00
|
|
|
#if defined(PR_LOGGING)
|
|
|
|
if (LOG_ENABLED()) {
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString spec;
|
2002-09-02 04:14:20 +04:00
|
|
|
aURI->GetSpec(spec);
|
|
|
|
LOG(("PrefetchURI [%s]\n", spec.get()));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (mDisabled) {
|
|
|
|
LOG(("rejected: prefetch service is disabled\n"));
|
|
|
|
return NS_ERROR_ABORT;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// XXX we should really be asking the protocol handler if it supports
|
|
|
|
// caching, so we can determine if there is any value to prefetching.
|
|
|
|
// for now, we'll only prefetch http links since we know that's the
|
|
|
|
// most common case. ignore https links since https content only goes
|
|
|
|
// into the memory cache.
|
|
|
|
//
|
|
|
|
// XXX we might want to either leverage nsIProtocolHandler::protocolFlags
|
|
|
|
// or possibly nsIRequest::loadFlags to determine if this URI should be
|
|
|
|
// prefetched.
|
|
|
|
//
|
2011-09-29 10:19:26 +04:00
|
|
|
bool match;
|
2002-10-24 07:17:44 +04:00
|
|
|
rv = aURI->SchemeIs("http", &match);
|
|
|
|
if (NS_FAILED(rv) || !match) {
|
2008-09-30 12:53:48 +04:00
|
|
|
rv = aURI->SchemeIs("https", &match);
|
2007-03-14 04:52:07 +03:00
|
|
|
if (NS_FAILED(rv) || !match) {
|
2008-09-30 12:53:48 +04:00
|
|
|
LOG(("rejected: URL is not of type http/https\n"));
|
2007-03-14 04:52:07 +03:00
|
|
|
return NS_ERROR_ABORT;
|
|
|
|
}
|
2002-09-02 04:14:20 +04:00
|
|
|
}
|
|
|
|
|
2002-10-24 07:17:44 +04:00
|
|
|
//
|
|
|
|
// the referrer URI must be http:
|
|
|
|
//
|
|
|
|
rv = aReferrerURI->SchemeIs("http", &match);
|
|
|
|
if (NS_FAILED(rv) || !match) {
|
2008-09-30 12:53:48 +04:00
|
|
|
rv = aReferrerURI->SchemeIs("https", &match);
|
2007-03-14 04:52:07 +03:00
|
|
|
if (NS_FAILED(rv) || !match) {
|
2008-09-30 12:53:48 +04:00
|
|
|
LOG(("rejected: referrer URL is neither http nor https\n"));
|
2007-03-14 04:52:07 +03:00
|
|
|
return NS_ERROR_ABORT;
|
|
|
|
}
|
2002-10-24 07:17:44 +04:00
|
|
|
}
|
|
|
|
|
2003-03-07 00:37:09 +03:00
|
|
|
// skip URLs that contain query strings, except URLs for which prefetching
|
|
|
|
// has been explicitly requested.
|
|
|
|
if (!aExplicit) {
|
|
|
|
nsCOMPtr<nsIURL> url(do_QueryInterface(aURI, &rv));
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString query;
|
2003-03-07 00:37:09 +03:00
|
|
|
rv = url->GetQuery(query);
|
|
|
|
if (NS_FAILED(rv) || !query.IsEmpty()) {
|
|
|
|
LOG(("rejected: URL has a query string\n"));
|
|
|
|
return NS_ERROR_ABORT;
|
|
|
|
}
|
2002-09-02 04:14:20 +04:00
|
|
|
}
|
|
|
|
|
2007-07-09 10:19:27 +04:00
|
|
|
//
|
2002-09-02 04:14:20 +04:00
|
|
|
// cancel if being prefetched
|
|
|
|
//
|
2007-07-09 10:19:27 +04:00
|
|
|
if (mCurrentNode) {
|
2011-09-29 10:19:26 +04:00
|
|
|
bool equals;
|
2007-07-09 10:19:27 +04:00
|
|
|
if (NS_SUCCEEDED(mCurrentNode->mURI->Equals(aURI, &equals)) && equals) {
|
2008-08-25 07:37:10 +04:00
|
|
|
LOG(("rejected: URL is already being prefetched\n"));
|
|
|
|
return NS_ERROR_ABORT;
|
2002-09-02 04:14:20 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// cancel if already on the prefetch queue
|
|
|
|
//
|
|
|
|
nsPrefetchNode *node = mQueueHead;
|
|
|
|
for (; node; node = node->mNext) {
|
2011-09-29 10:19:26 +04:00
|
|
|
bool equals;
|
2002-09-02 04:14:20 +04:00
|
|
|
if (NS_SUCCEEDED(node->mURI->Equals(aURI, &equals)) && equals) {
|
|
|
|
LOG(("rejected: URL is already on prefetch queue\n"));
|
|
|
|
return NS_ERROR_ABORT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-09 10:19:27 +04:00
|
|
|
nsRefPtr<nsPrefetchNode> enqueuedNode;
|
2008-08-25 07:37:10 +04:00
|
|
|
rv = EnqueueURI(aURI, aReferrerURI, aSource,
|
2007-07-09 10:19:27 +04:00
|
|
|
getter_AddRefs(enqueuedNode));
|
2007-05-29 13:45:30 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2007-07-09 10:19:27 +04:00
|
|
|
NotifyLoadRequested(enqueuedNode);
|
|
|
|
|
2007-05-29 13:45:30 +04:00
|
|
|
// if there are no pages loading, kick off the request immediately
|
|
|
|
if (mStopCount == 0 && mHaveProcessed)
|
|
|
|
ProcessNextURI();
|
|
|
|
|
|
|
|
return NS_OK;
|
2007-03-14 04:52:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPrefetchService::PrefetchURI(nsIURI *aURI,
|
|
|
|
nsIURI *aReferrerURI,
|
2007-07-09 10:19:27 +04:00
|
|
|
nsIDOMNode *aSource,
|
2011-09-29 10:19:26 +04:00
|
|
|
bool aExplicit)
|
2007-03-14 04:52:07 +03:00
|
|
|
{
|
2008-08-25 07:37:10 +04:00
|
|
|
return Prefetch(aURI, aReferrerURI, aSource, aExplicit);
|
2007-03-14 04:52:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPrefetchService::PrefetchURIForOfflineUse(nsIURI *aURI,
|
|
|
|
nsIURI *aReferrerURI,
|
2007-07-09 10:19:27 +04:00
|
|
|
nsIDOMNode *aSource,
|
2011-09-29 10:19:26 +04:00
|
|
|
bool aExplicit)
|
2007-03-14 04:52:07 +03:00
|
|
|
{
|
2008-08-25 07:37:10 +04:00
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
2007-07-09 10:19:27 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-29 10:19:26 +04:00
|
|
|
nsPrefetchService::EnumerateQueue(bool aIncludeNormalItems,
|
|
|
|
bool aIncludeOfflineItems,
|
2007-07-09 10:19:27 +04:00
|
|
|
nsISimpleEnumerator **aEnumerator)
|
|
|
|
{
|
2008-08-25 07:37:10 +04:00
|
|
|
NS_ENSURE_TRUE(aIncludeNormalItems && !aIncludeOfflineItems,
|
|
|
|
NS_ERROR_NOT_IMPLEMENTED);
|
|
|
|
|
|
|
|
*aEnumerator = new nsPrefetchQueueEnumerator(this);
|
2007-07-09 10:19:27 +04:00
|
|
|
if (!*aEnumerator) return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
NS_ADDREF(*aEnumerator);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsPrefetchNode::nsIDOMLoadStatus
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPrefetchNode::GetSource(nsIDOMNode **aSource)
|
|
|
|
{
|
2012-07-30 18:20:58 +04:00
|
|
|
*aSource = nullptr;
|
2007-07-09 10:19:27 +04:00
|
|
|
nsCOMPtr<nsIDOMNode> source = do_QueryReferent(mSource);
|
|
|
|
if (source)
|
|
|
|
source.swap(*aSource);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPrefetchNode::GetUri(nsAString &aURI)
|
|
|
|
{
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString spec;
|
2007-07-09 10:19:27 +04:00
|
|
|
nsresult rv = mURI->GetSpec(spec);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
CopyUTF8toUTF16(spec, aURI);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsPrefetchNode::GetTotalSize(int32_t *aTotalSize)
|
2007-07-09 10:19:27 +04:00
|
|
|
{
|
|
|
|
if (mChannel) {
|
2012-10-22 21:51:07 +04:00
|
|
|
int64_t size64;
|
|
|
|
nsresult rv = mChannel->GetContentLength(&size64);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
*aTotalSize = int32_t(size64); // XXX - loses precision
|
|
|
|
return NS_OK;
|
2007-07-09 10:19:27 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
*aTotalSize = -1;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsPrefetchNode::GetLoadedSize(int32_t *aLoadedSize)
|
2007-07-09 10:19:27 +04:00
|
|
|
{
|
2012-10-22 21:51:07 +04:00
|
|
|
*aLoadedSize = int32_t(mBytesRead); // XXX - loses precision
|
2007-07-09 10:19:27 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsPrefetchNode::GetReadyState(uint16_t *aReadyState)
|
2007-07-09 10:19:27 +04:00
|
|
|
{
|
|
|
|
*aReadyState = mState;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsPrefetchNode::GetStatus(uint16_t *aStatus)
|
2007-07-09 10:19:27 +04:00
|
|
|
{
|
|
|
|
if (!mChannel) {
|
|
|
|
*aStatus = 0;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIHttpChannel> httpChannel = do_QueryInterface(mChannel, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t httpStatus;
|
2007-07-09 10:19:27 +04:00
|
|
|
rv = httpChannel->GetResponseStatus(&httpStatus);
|
|
|
|
if (rv == NS_ERROR_NOT_AVAILABLE) {
|
|
|
|
*aStatus = 0;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2012-08-22 19:56:38 +04:00
|
|
|
*aStatus = uint16_t(httpStatus);
|
2007-07-09 10:19:27 +04:00
|
|
|
return NS_OK;
|
2002-09-02 04:14:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsPrefetchService::nsIWebProgressListener
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPrefetchService::OnProgressChange(nsIWebProgress *aProgress,
|
|
|
|
nsIRequest *aRequest,
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t curSelfProgress,
|
|
|
|
int32_t maxSelfProgress,
|
|
|
|
int32_t curTotalProgress,
|
|
|
|
int32_t maxTotalProgress)
|
2002-09-02 04:14:20 +04:00
|
|
|
{
|
|
|
|
NS_NOTREACHED("notification excluded in AddProgressListener(...)");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPrefetchService::OnStateChange(nsIWebProgress* aWebProgress,
|
|
|
|
nsIRequest *aRequest,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t progressStateFlags,
|
2002-09-02 04:14:20 +04:00
|
|
|
nsresult aStatus)
|
|
|
|
{
|
|
|
|
if (progressStateFlags & STATE_IS_DOCUMENT) {
|
|
|
|
if (progressStateFlags & STATE_STOP)
|
|
|
|
StartPrefetching();
|
|
|
|
else if (progressStateFlags & STATE_START)
|
|
|
|
StopPrefetching();
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPrefetchService::OnLocationChange(nsIWebProgress* aWebProgress,
|
|
|
|
nsIRequest* aRequest,
|
2011-11-10 18:01:11 +04:00
|
|
|
nsIURI *location,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t aFlags)
|
2002-09-02 04:14:20 +04:00
|
|
|
{
|
|
|
|
NS_NOTREACHED("notification excluded in AddProgressListener(...)");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPrefetchService::OnStatusChange(nsIWebProgress* aWebProgress,
|
|
|
|
nsIRequest* aRequest,
|
|
|
|
nsresult aStatus,
|
|
|
|
const PRUnichar* aMessage)
|
|
|
|
{
|
|
|
|
NS_NOTREACHED("notification excluded in AddProgressListener(...)");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPrefetchService::OnSecurityChange(nsIWebProgress *aWebProgress,
|
|
|
|
nsIRequest *aRequest,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t state)
|
2002-09-02 04:14:20 +04:00
|
|
|
{
|
|
|
|
NS_NOTREACHED("notification excluded in AddProgressListener(...)");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsPrefetchService::nsIObserver
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPrefetchService::Observe(nsISupports *aSubject,
|
|
|
|
const char *aTopic,
|
|
|
|
const PRUnichar *aData)
|
|
|
|
{
|
|
|
|
LOG(("nsPrefetchService::Observe [topic=%s]\n", aTopic));
|
|
|
|
|
|
|
|
if (!strcmp(aTopic, NS_XPCOM_SHUTDOWN_OBSERVER_ID)) {
|
|
|
|
StopPrefetching();
|
2009-02-25 10:12:12 +03:00
|
|
|
EmptyQueue();
|
2011-10-17 18:59:28 +04:00
|
|
|
mDisabled = true;
|
2002-09-02 04:14:20 +04:00
|
|
|
}
|
2003-01-28 01:23:28 +03:00
|
|
|
else if (!strcmp(aTopic, NS_PREFBRANCH_PREFCHANGE_TOPIC_ID)) {
|
2011-09-29 10:19:26 +04:00
|
|
|
if (Preferences::GetBool(PREFETCH_PREF, false)) {
|
2003-01-28 01:23:28 +03:00
|
|
|
if (mDisabled) {
|
|
|
|
LOG(("enabling prefetching\n"));
|
2011-10-17 18:59:28 +04:00
|
|
|
mDisabled = false;
|
2003-01-28 01:23:28 +03:00
|
|
|
AddProgressListener();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (!mDisabled) {
|
|
|
|
LOG(("disabling prefetching\n"));
|
|
|
|
StopPrefetching();
|
2009-02-25 10:12:12 +03:00
|
|
|
EmptyQueue();
|
2011-10-17 18:59:28 +04:00
|
|
|
mDisabled = true;
|
2003-01-28 01:23:28 +03:00
|
|
|
RemoveProgressListener();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2002-09-02 04:14:20 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// vim: ts=4 sw=4 expandtab
|