2007-03-22 10:30:00 -07:00
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is Mozilla.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Netscape Communications Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2002
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Darin Fisher <darin@netscape.com> (original author)
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
|
|
|
#include "nsPrefetchService.h"
|
2007-05-14 13:09:20 -07:00
|
|
|
#include "nsICacheSession.h"
|
|
|
|
#include "nsICacheService.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsIServiceManager.h"
|
|
|
|
#include "nsICategoryManager.h"
|
|
|
|
#include "nsIObserverService.h"
|
|
|
|
#include "nsIPrefService.h"
|
|
|
|
#include "nsIPrefBranch2.h"
|
|
|
|
#include "nsIDocCharset.h"
|
|
|
|
#include "nsIWebProgress.h"
|
|
|
|
#include "nsCURILoader.h"
|
|
|
|
#include "nsICachingChannel.h"
|
|
|
|
#include "nsICacheVisitor.h"
|
|
|
|
#include "nsIHttpChannel.h"
|
|
|
|
#include "nsIURL.h"
|
2007-07-08 23:19:27 -07:00
|
|
|
#include "nsISimpleEnumerator.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsNetUtil.h"
|
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsXPIDLString.h"
|
|
|
|
#include "nsReadableUtils.h"
|
2007-07-08 23:19:27 -07:00
|
|
|
#include "nsStreamUtils.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsAutoPtr.h"
|
|
|
|
#include "prtime.h"
|
|
|
|
#include "prlog.h"
|
|
|
|
#include "plstr.h"
|
2010-08-04 19:15:55 -07:00
|
|
|
#include "nsIAsyncVerifyRedirectCallback.h"
|
2007-03-22 10:30:00 -07: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)
|
|
|
|
|
|
|
|
#define PREFETCH_PREF "network.prefetch-next"
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// helpers
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
static inline PRUint32
|
|
|
|
PRTimeToSeconds(PRTime t_usec)
|
|
|
|
{
|
|
|
|
PRTime usec_per_sec;
|
|
|
|
PRUint32 t_sec;
|
|
|
|
LL_I2L(usec_per_sec, PR_USEC_PER_SEC);
|
|
|
|
LL_DIV(t_usec, t_usec, usec_per_sec);
|
|
|
|
LL_L2I(t_sec, t_usec);
|
|
|
|
return t_sec;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define NowInSeconds() PRTimeToSeconds(PR_Now())
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2007-07-08 23:19:27 -07:00
|
|
|
// nsPrefetchQueueEnumerator
|
2007-03-22 10:30:00 -07:00
|
|
|
//-----------------------------------------------------------------------------
|
2007-07-08 23:19:27 -07:00
|
|
|
class nsPrefetchQueueEnumerator : public nsISimpleEnumerator
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSISIMPLEENUMERATOR
|
2008-08-24 20:37:10 -07:00
|
|
|
nsPrefetchQueueEnumerator(nsPrefetchService *aService);
|
2007-07-08 23:19:27 -07:00
|
|
|
~nsPrefetchQueueEnumerator();
|
|
|
|
|
|
|
|
private:
|
|
|
|
void Increment();
|
|
|
|
|
|
|
|
nsRefPtr<nsPrefetchService> mService;
|
|
|
|
nsRefPtr<nsPrefetchNode> mCurrent;
|
|
|
|
PRBool mStarted;
|
|
|
|
};
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsPrefetchQueueEnumerator <public>
|
|
|
|
//-----------------------------------------------------------------------------
|
2008-08-24 20:37:10 -07:00
|
|
|
nsPrefetchQueueEnumerator::nsPrefetchQueueEnumerator(nsPrefetchService *aService)
|
2007-07-08 23:19:27 -07:00
|
|
|
: mService(aService)
|
|
|
|
, mStarted(PR_FALSE)
|
|
|
|
{
|
|
|
|
Increment();
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-07-08 23:19:27 -07:00
|
|
|
nsPrefetchQueueEnumerator::~nsPrefetchQueueEnumerator()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2007-07-08 23:19:27 -07:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsPrefetchQueueEnumerator::nsISimpleEnumerator
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPrefetchQueueEnumerator::HasMoreElements(PRBool *aHasMore)
|
|
|
|
{
|
|
|
|
*aHasMore = (mCurrent != nsnull);
|
|
|
|
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()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-02-24 23:12:12 -08: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.
|
|
|
|
mStarted = PR_TRUE;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2007-07-08 23:19:27 -07:00
|
|
|
// nsPrefetchQueueEnumerator::nsISupports
|
2007-03-22 10:30:00 -07:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2007-07-08 23:19:27 -07:00
|
|
|
NS_IMPL_ISUPPORTS1(nsPrefetchQueueEnumerator, nsISimpleEnumerator)
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsPrefetchNode <public>
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
nsPrefetchNode::nsPrefetchNode(nsPrefetchService *aService,
|
|
|
|
nsIURI *aURI,
|
|
|
|
nsIURI *aReferrerURI,
|
2008-08-24 20:37:10 -07:00
|
|
|
nsIDOMNode *aSource)
|
2007-07-08 23:19:27 -07:00
|
|
|
: mNext(nsnull)
|
|
|
|
, mURI(aURI)
|
|
|
|
, mReferrerURI(aReferrerURI)
|
|
|
|
, mService(aService)
|
|
|
|
, mChannel(nsnull)
|
|
|
|
, mState(nsIDOMLoadStatus::UNINITIALIZED)
|
|
|
|
, mBytesRead(0)
|
|
|
|
{
|
|
|
|
mSource = do_GetWeakReference(aSource);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsPrefetchNode::OpenChannel()
|
|
|
|
{
|
|
|
|
nsresult rv = NS_NewChannel(getter_AddRefs(mChannel),
|
|
|
|
mURI,
|
|
|
|
nsnull, nsnull, this,
|
|
|
|
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"),
|
|
|
|
PR_FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = mChannel->AsyncOpen(this, nsnull);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
mState = nsIDOMLoadStatus::REQUESTED;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsPrefetchNode::CancelChannel(nsresult error)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2007-07-08 23:19:27 -07:00
|
|
|
mChannel->Cancel(error);
|
|
|
|
mChannel = nsnull;
|
|
|
|
|
|
|
|
mState = nsIDOMLoadStatus::UNINITIALIZED;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2007-07-08 23:19:27 -07:00
|
|
|
// nsPrefetchNode::nsISupports
|
2007-03-22 10:30:00 -07:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2007-07-08 23:19:27 -07:00
|
|
|
NS_IMPL_ISUPPORTS5(nsPrefetchNode,
|
|
|
|
nsIDOMLoadStatus,
|
2007-03-22 10:30:00 -07:00
|
|
|
nsIRequestObserver,
|
|
|
|
nsIStreamListener,
|
|
|
|
nsIInterfaceRequestor,
|
|
|
|
nsIChannelEventSink)
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2007-07-08 23:19:27 -07:00
|
|
|
// nsPrefetchNode::nsIStreamListener
|
2007-03-22 10:30:00 -07:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2007-07-08 23:19:27 -07:00
|
|
|
nsPrefetchNode::OnStartRequest(nsIRequest *aRequest,
|
|
|
|
nsISupports *aContext)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
nsCOMPtr<nsICachingChannel> cachingChannel =
|
|
|
|
do_QueryInterface(aRequest, &rv);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2008-08-24 20:37:10 -07:00
|
|
|
// no need to prefetch a document that is already in the cache
|
|
|
|
PRBool fromCache;
|
|
|
|
if (NS_SUCCEEDED(cachingChannel->IsFromCache(&fromCache)) &&
|
|
|
|
fromCache) {
|
|
|
|
LOG(("document is already in the cache; canceling prefetch\n"));
|
|
|
|
return NS_BINDING_ABORTED;
|
|
|
|
}
|
2008-08-19 22:54:37 -07:00
|
|
|
|
2008-08-24 20:37:10 -07: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-19 22:54:37 -07:00
|
|
|
|
2008-08-24 20:37:10 -07:00
|
|
|
nsCOMPtr<nsICacheEntryInfo> entryInfo =
|
|
|
|
do_QueryInterface(cacheToken, &rv);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
2008-08-19 22:54:37 -07:00
|
|
|
|
2008-08-24 20:37:10 -07:00
|
|
|
PRUint32 expTime;
|
|
|
|
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-19 22:54:37 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2007-07-08 23:19:27 -07:00
|
|
|
|
|
|
|
mState = nsIDOMLoadStatus::RECEIVING;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2007-07-08 23:19:27 -07:00
|
|
|
nsPrefetchNode::OnDataAvailable(nsIRequest *aRequest,
|
|
|
|
nsISupports *aContext,
|
|
|
|
nsIInputStream *aStream,
|
|
|
|
PRUint32 aOffset,
|
|
|
|
PRUint32 aCount)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
PRUint32 bytesRead = 0;
|
2007-07-08 23:19:27 -07:00
|
|
|
aStream->ReadSegments(NS_DiscardSegment, nsnull, aCount, &bytesRead);
|
|
|
|
mBytesRead += bytesRead;
|
2007-03-22 10:30:00 -07:00
|
|
|
LOG(("prefetched %u bytes [offset=%u]\n", bytesRead, aOffset));
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2007-07-08 23:19:27 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_IMETHODIMP
|
2007-07-08 23:19:27 -07:00
|
|
|
nsPrefetchNode::OnStopRequest(nsIRequest *aRequest,
|
|
|
|
nsISupports *aContext,
|
|
|
|
nsresult aStatus)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
LOG(("done prefetching [status=%x]\n", aStatus));
|
|
|
|
|
2007-07-08 23:19:27 -07: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);
|
2007-03-22 10:30:00 -07:00
|
|
|
mService->ProcessNextURI();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2007-07-08 23:19:27 -07:00
|
|
|
// nsPrefetchNode::nsIInterfaceRequestor
|
2007-03-22 10:30:00 -07:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2007-07-08 23:19:27 -07:00
|
|
|
nsPrefetchNode::GetInterface(const nsIID &aIID, void **aResult)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
if (aIID.Equals(NS_GET_IID(nsIChannelEventSink))) {
|
|
|
|
NS_ADDREF_THIS();
|
2007-07-08 00:08:04 -07:00
|
|
|
*aResult = static_cast<nsIChannelEventSink *>(this);
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_ERROR_NO_INTERFACE;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2007-07-08 23:19:27 -07:00
|
|
|
// nsPrefetchNode::nsIChannelEventSink
|
2007-03-22 10:30:00 -07:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2010-08-04 19:15:55 -07:00
|
|
|
nsPrefetchNode::AsyncOnChannelRedirect(nsIChannel *aOldChannel,
|
|
|
|
nsIChannel *aNewChannel,
|
|
|
|
PRUint32 aFlags,
|
|
|
|
nsIAsyncVerifyRedirectCallback *callback)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIURI> newURI;
|
|
|
|
nsresult rv = aNewChannel->GetURI(getter_AddRefs(newURI));
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
nsCOMPtr<nsICachingChannel> oldCachingChannel =
|
|
|
|
do_QueryInterface(aOldChannel);
|
|
|
|
|
|
|
|
PRBool match;
|
|
|
|
rv = newURI->SchemeIs("http", &match);
|
|
|
|
if (NS_FAILED(rv) || !match) {
|
2008-09-30 01:53:48 -07: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;
|
|
|
|
}
|
2007-03-22 10:30:00 -07: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-24 20:37:10 -07:00
|
|
|
NS_LITERAL_CSTRING("prefetch"),
|
2007-03-22 10:30:00 -07:00
|
|
|
PR_FALSE);
|
|
|
|
|
2007-07-08 23:19:27 -07:00
|
|
|
mChannel = aNewChannel;
|
|
|
|
|
2010-08-04 19:15:55 -07:00
|
|
|
callback->OnRedirectVerifyCallback(NS_OK);
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2007-07-08 23:19:27 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsPrefetchService <public>
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
nsPrefetchService::nsPrefetchService()
|
|
|
|
: mQueueHead(nsnull)
|
|
|
|
, mQueueTail(nsnull)
|
|
|
|
, mStopCount(0)
|
2007-05-29 02:45:30 -07:00
|
|
|
, mHaveProcessed(PR_FALSE)
|
2007-03-22 10:30:00 -07:00
|
|
|
, mDisabled(PR_TRUE)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsPrefetchService::~nsPrefetchService()
|
|
|
|
{
|
|
|
|
// cannot reach destructor if prefetch in progress (listener owns reference
|
|
|
|
// to this service)
|
2008-08-24 20:37:10 -07:00
|
|
|
EmptyQueue();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsPrefetchService::Init()
|
|
|
|
{
|
|
|
|
#if defined(PR_LOGGING)
|
|
|
|
if (!gPrefetchLog)
|
|
|
|
gPrefetchLog = PR_NewLogModule("nsPrefetch");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
// read prefs and hook up pref observer
|
2007-05-14 13:09:20 -07:00
|
|
|
nsCOMPtr<nsIPrefBranch2> prefs(do_GetService(NS_PREFSERVICE_CONTRACTID, &rv));
|
2007-03-22 10:30:00 -07:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
PRBool enabled;
|
|
|
|
rv = prefs->GetBoolPref(PREFETCH_PREF, &enabled);
|
|
|
|
if (NS_SUCCEEDED(rv) && enabled)
|
|
|
|
mDisabled = PR_FALSE;
|
|
|
|
|
|
|
|
prefs->AddObserver(PREFETCH_PREF, this, PR_TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 09:59:13 -07:00
|
|
|
nsCOMPtr<nsIObserverService> observerService =
|
|
|
|
mozilla::services::GetObserverService();
|
|
|
|
if (!observerService)
|
|
|
|
return NS_ERROR_FAILURE;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
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
|
|
|
rv = observerService->AddObserver(this, NS_XPCOM_SHUTDOWN_OBSERVER_ID, PR_TRUE);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (!mDisabled)
|
|
|
|
AddProgressListener();
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsPrefetchService::ProcessNextURI()
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIURI> uri, referrer;
|
|
|
|
|
2007-07-08 23:19:27 -07:00
|
|
|
mCurrentNode = nsnull;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
do {
|
2007-07-08 23:19:27 -07:00
|
|
|
rv = DequeueNode(getter_AddRefs(mCurrentNode));
|
2007-05-14 13:09:20 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
if (NS_FAILED(rv)) break;
|
|
|
|
|
|
|
|
#if defined(PR_LOGGING)
|
|
|
|
if (LOG_ENABLED()) {
|
|
|
|
nsCAutoString spec;
|
2007-07-08 23:19:27 -07:00
|
|
|
mCurrentNode->mURI->GetSpec(spec);
|
2007-03-22 10:30:00 -07:00
|
|
|
LOG(("ProcessNextURI [%s]\n", spec.get()));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
//
|
|
|
|
// if opening the channel fails, then just skip to the next uri
|
|
|
|
//
|
2007-07-08 23:19:27 -07:00
|
|
|
rv = mCurrentNode->OpenChannel();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
while (NS_FAILED(rv));
|
|
|
|
}
|
|
|
|
|
2007-07-08 23:19:27 -07: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 09:59:13 -07:00
|
|
|
mozilla::services::GetObserverService();
|
|
|
|
if (!observerService)
|
|
|
|
return;
|
2007-07-08 23:19:27 -07:00
|
|
|
|
|
|
|
observerService->NotifyObservers(static_cast<nsIDOMLoadStatus*>(node),
|
2008-08-24 20:37:10 -07:00
|
|
|
"prefetch-load-requested", nsnull);
|
2007-07-08 23:19:27 -07: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 09:59:13 -07:00
|
|
|
mozilla::services::GetObserverService();
|
|
|
|
if (!observerService)
|
|
|
|
return;
|
2007-07-08 23:19:27 -07:00
|
|
|
|
|
|
|
observerService->NotifyObservers(static_cast<nsIDOMLoadStatus*>(node),
|
2008-08-24 20:37:10 -07:00
|
|
|
"prefetch-load-completed", nsnull);
|
2007-07-08 23:19:27 -07:00
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsPrefetchService <private>
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void
|
|
|
|
nsPrefetchService::AddProgressListener()
|
|
|
|
{
|
|
|
|
// Register as an observer for the document loader
|
2007-05-14 13:09:20 -07:00
|
|
|
nsCOMPtr<nsIWebProgress> progress =
|
|
|
|
do_GetService(NS_DOCUMENTLOADER_SERVICE_CONTRACTID);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (progress)
|
|
|
|
progress->AddProgressListener(this, nsIWebProgress::NOTIFY_STATE_DOCUMENT);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsPrefetchService::RemoveProgressListener()
|
|
|
|
{
|
|
|
|
// Register as an observer for the document loader
|
2007-05-14 13:09:20 -07:00
|
|
|
nsCOMPtr<nsIWebProgress> progress =
|
|
|
|
do_GetService(NS_DOCUMENTLOADER_SERVICE_CONTRACTID);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (progress)
|
|
|
|
progress->RemoveProgressListener(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2007-07-08 23:19:27 -07:00
|
|
|
nsPrefetchService::EnqueueNode(nsPrefetchNode *aNode)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2007-07-08 23:19:27 -07:00
|
|
|
NS_ADDREF(aNode);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (!mQueueTail) {
|
2007-07-08 23:19:27 -07:00
|
|
|
mQueueHead = aNode;
|
|
|
|
mQueueTail = aNode;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
else {
|
2007-07-08 23:19:27 -07:00
|
|
|
mQueueTail->mNext = aNode;
|
|
|
|
mQueueTail = aNode;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2007-07-08 23:19:27 -07:00
|
|
|
nsPrefetchService::EnqueueURI(nsIURI *aURI,
|
|
|
|
nsIURI *aReferrerURI,
|
|
|
|
nsIDOMNode *aSource,
|
|
|
|
nsPrefetchNode **aNode)
|
|
|
|
{
|
|
|
|
nsPrefetchNode *node = new nsPrefetchNode(this, aURI, aReferrerURI,
|
2008-08-24 20:37:10 -07:00
|
|
|
aSource);
|
2007-07-08 23:19:27 -07:00
|
|
|
if (!node)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
NS_ADDREF(*aNode = node);
|
|
|
|
|
|
|
|
return EnqueueNode(node);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsPrefetchService::DequeueNode(nsPrefetchNode **node)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
if (!mQueueHead)
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
|
2007-07-08 23:19:27 -07:00
|
|
|
// give the ref to the caller
|
|
|
|
*node = mQueueHead;
|
2007-03-22 10:30:00 -07:00
|
|
|
mQueueHead = mQueueHead->mNext;
|
2007-07-08 23:19:27 -07:00
|
|
|
(*node)->mNext = nsnull;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (!mQueueHead)
|
|
|
|
mQueueTail = nsnull;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2008-08-24 20:37:10 -07:00
|
|
|
nsPrefetchService::EmptyQueue()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-02-24 23:12:12 -08:00
|
|
|
do {
|
|
|
|
nsRefPtr<nsPrefetchNode> node;
|
|
|
|
DequeueNode(getter_AddRefs(node));
|
|
|
|
} while (mQueueHead);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsPrefetchService::StartPrefetching()
|
|
|
|
{
|
|
|
|
//
|
|
|
|
// 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--;
|
|
|
|
|
|
|
|
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-08 23:19:27 -07:00
|
|
|
if (mStopCount == 0 && !mCurrentNode) {
|
2007-05-29 02:45:30 -07:00
|
|
|
mHaveProcessed = PR_TRUE;
|
2007-03-22 10:30:00 -07:00
|
|
|
ProcessNextURI();
|
2007-05-29 02:45:30 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsPrefetchService::StopPrefetching()
|
|
|
|
{
|
|
|
|
mStopCount++;
|
|
|
|
|
|
|
|
LOG(("StopPrefetching [stopcount=%d]\n", mStopCount));
|
|
|
|
|
|
|
|
// only kill the prefetch queue if we've actually started prefetching.
|
2007-07-08 23:19:27 -07:00
|
|
|
if (!mCurrentNode)
|
2007-03-22 10:30:00 -07:00
|
|
|
return;
|
|
|
|
|
2007-07-08 23:19:27 -07:00
|
|
|
mCurrentNode->CancelChannel(NS_BINDING_ABORTED);
|
|
|
|
mCurrentNode = nsnull;
|
2008-08-24 20:37:10 -07:00
|
|
|
EmptyQueue();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsPrefetchService::nsISupports
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS4(nsPrefetchService,
|
|
|
|
nsIPrefetchService,
|
|
|
|
nsIWebProgressListener,
|
|
|
|
nsIObserver,
|
|
|
|
nsISupportsWeakReference)
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsPrefetchService::nsIPrefetchService
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsPrefetchService::Prefetch(nsIURI *aURI,
|
|
|
|
nsIURI *aReferrerURI,
|
2007-07-08 23:19:27 -07:00
|
|
|
nsIDOMNode *aSource,
|
2008-08-24 20:37:10 -07:00
|
|
|
PRBool aExplicit)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
NS_ENSURE_ARG_POINTER(aURI);
|
|
|
|
NS_ENSURE_ARG_POINTER(aReferrerURI);
|
|
|
|
|
|
|
|
#if defined(PR_LOGGING)
|
|
|
|
if (LOG_ENABLED()) {
|
|
|
|
nsCAutoString spec;
|
|
|
|
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.
|
|
|
|
//
|
|
|
|
PRBool match;
|
|
|
|
rv = aURI->SchemeIs("http", &match);
|
|
|
|
if (NS_FAILED(rv) || !match) {
|
2008-09-30 01:53:48 -07:00
|
|
|
rv = aURI->SchemeIs("https", &match);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (NS_FAILED(rv) || !match) {
|
2008-09-30 01:53:48 -07:00
|
|
|
LOG(("rejected: URL is not of type http/https\n"));
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_ERROR_ABORT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// the referrer URI must be http:
|
|
|
|
//
|
|
|
|
rv = aReferrerURI->SchemeIs("http", &match);
|
|
|
|
if (NS_FAILED(rv) || !match) {
|
2008-09-30 01:53:48 -07:00
|
|
|
rv = aReferrerURI->SchemeIs("https", &match);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (NS_FAILED(rv) || !match) {
|
2008-09-30 01:53:48 -07:00
|
|
|
LOG(("rejected: referrer URL is neither http nor https\n"));
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_ERROR_ABORT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
nsCAutoString query;
|
|
|
|
rv = url->GetQuery(query);
|
|
|
|
if (NS_FAILED(rv) || !query.IsEmpty()) {
|
|
|
|
LOG(("rejected: URL has a query string\n"));
|
|
|
|
return NS_ERROR_ABORT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-08 23:19:27 -07:00
|
|
|
//
|
2007-03-22 10:30:00 -07:00
|
|
|
// cancel if being prefetched
|
|
|
|
//
|
2007-07-08 23:19:27 -07:00
|
|
|
if (mCurrentNode) {
|
|
|
|
PRBool equals;
|
|
|
|
if (NS_SUCCEEDED(mCurrentNode->mURI->Equals(aURI, &equals)) && equals) {
|
2008-08-24 20:37:10 -07:00
|
|
|
LOG(("rejected: URL is already being prefetched\n"));
|
|
|
|
return NS_ERROR_ABORT;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// cancel if already on the prefetch queue
|
|
|
|
//
|
|
|
|
nsPrefetchNode *node = mQueueHead;
|
|
|
|
for (; node; node = node->mNext) {
|
|
|
|
PRBool equals;
|
|
|
|
if (NS_SUCCEEDED(node->mURI->Equals(aURI, &equals)) && equals) {
|
|
|
|
LOG(("rejected: URL is already on prefetch queue\n"));
|
|
|
|
return NS_ERROR_ABORT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-08 23:19:27 -07:00
|
|
|
nsRefPtr<nsPrefetchNode> enqueuedNode;
|
2008-08-24 20:37:10 -07:00
|
|
|
rv = EnqueueURI(aURI, aReferrerURI, aSource,
|
2007-07-08 23:19:27 -07:00
|
|
|
getter_AddRefs(enqueuedNode));
|
2007-05-29 02:45:30 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2007-07-08 23:19:27 -07:00
|
|
|
NotifyLoadRequested(enqueuedNode);
|
|
|
|
|
2007-05-29 02:45:30 -07:00
|
|
|
// if there are no pages loading, kick off the request immediately
|
|
|
|
if (mStopCount == 0 && mHaveProcessed)
|
|
|
|
ProcessNextURI();
|
|
|
|
|
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPrefetchService::PrefetchURI(nsIURI *aURI,
|
|
|
|
nsIURI *aReferrerURI,
|
2007-07-08 23:19:27 -07:00
|
|
|
nsIDOMNode *aSource,
|
2007-03-22 10:30:00 -07:00
|
|
|
PRBool aExplicit)
|
|
|
|
{
|
2008-08-24 20:37:10 -07:00
|
|
|
return Prefetch(aURI, aReferrerURI, aSource, aExplicit);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPrefetchService::PrefetchURIForOfflineUse(nsIURI *aURI,
|
|
|
|
nsIURI *aReferrerURI,
|
2007-07-08 23:19:27 -07:00
|
|
|
nsIDOMNode *aSource,
|
2007-03-22 10:30:00 -07:00
|
|
|
PRBool aExplicit)
|
|
|
|
{
|
2008-08-24 20:37:10 -07:00
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
2007-07-08 23:19:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPrefetchService::EnumerateQueue(PRBool aIncludeNormalItems,
|
|
|
|
PRBool aIncludeOfflineItems,
|
|
|
|
nsISimpleEnumerator **aEnumerator)
|
|
|
|
{
|
2008-08-24 20:37:10 -07:00
|
|
|
NS_ENSURE_TRUE(aIncludeNormalItems && !aIncludeOfflineItems,
|
|
|
|
NS_ERROR_NOT_IMPLEMENTED);
|
|
|
|
|
|
|
|
*aEnumerator = new nsPrefetchQueueEnumerator(this);
|
2007-07-08 23:19:27 -07:00
|
|
|
if (!*aEnumerator) return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
NS_ADDREF(*aEnumerator);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsPrefetchNode::nsIDOMLoadStatus
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPrefetchNode::GetSource(nsIDOMNode **aSource)
|
|
|
|
{
|
|
|
|
*aSource = nsnull;
|
|
|
|
nsCOMPtr<nsIDOMNode> source = do_QueryReferent(mSource);
|
|
|
|
if (source)
|
|
|
|
source.swap(*aSource);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPrefetchNode::GetUri(nsAString &aURI)
|
|
|
|
{
|
|
|
|
nsCAutoString spec;
|
|
|
|
nsresult rv = mURI->GetSpec(spec);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
CopyUTF8toUTF16(spec, aURI);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPrefetchNode::GetTotalSize(PRInt32 *aTotalSize)
|
|
|
|
{
|
|
|
|
if (mChannel) {
|
2010-08-30 13:20:39 -07:00
|
|
|
return mChannel->GetContentLength(aTotalSize);
|
2007-07-08 23:19:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
*aTotalSize = -1;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPrefetchNode::GetLoadedSize(PRInt32 *aLoadedSize)
|
|
|
|
{
|
2010-08-30 13:20:39 -07:00
|
|
|
*aLoadedSize = mBytesRead;
|
2007-07-08 23:19:27 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPrefetchNode::GetReadyState(PRUint16 *aReadyState)
|
|
|
|
{
|
|
|
|
*aReadyState = mState;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPrefetchNode::GetStatus(PRUint16 *aStatus)
|
|
|
|
{
|
|
|
|
if (!mChannel) {
|
|
|
|
*aStatus = 0;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIHttpChannel> httpChannel = do_QueryInterface(mChannel, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
PRUint32 httpStatus;
|
|
|
|
rv = httpChannel->GetResponseStatus(&httpStatus);
|
|
|
|
if (rv == NS_ERROR_NOT_AVAILABLE) {
|
|
|
|
// Someone's calling this before we got a response... Check our
|
|
|
|
// ReadyState. If we're at RECEIVING or LOADED, then this means the
|
|
|
|
// connection errored before we got any data; return a somewhat
|
|
|
|
// sensible error code in that case.
|
|
|
|
if (mState >= nsIDOMLoadStatus::RECEIVING) {
|
|
|
|
*aStatus = NS_ERROR_NOT_AVAILABLE;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
*aStatus = 0;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
*aStatus = PRUint16(httpStatus);
|
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsPrefetchService::nsIWebProgressListener
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPrefetchService::OnProgressChange(nsIWebProgress *aProgress,
|
|
|
|
nsIRequest *aRequest,
|
|
|
|
PRInt32 curSelfProgress,
|
|
|
|
PRInt32 maxSelfProgress,
|
|
|
|
PRInt32 curTotalProgress,
|
|
|
|
PRInt32 maxTotalProgress)
|
|
|
|
{
|
|
|
|
NS_NOTREACHED("notification excluded in AddProgressListener(...)");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPrefetchService::OnStateChange(nsIWebProgress* aWebProgress,
|
|
|
|
nsIRequest *aRequest,
|
|
|
|
PRUint32 progressStateFlags,
|
|
|
|
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,
|
|
|
|
nsIURI *location)
|
|
|
|
{
|
|
|
|
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,
|
|
|
|
PRUint32 state)
|
|
|
|
{
|
|
|
|
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-24 23:12:12 -08:00
|
|
|
EmptyQueue();
|
2007-03-22 10:30:00 -07:00
|
|
|
mDisabled = PR_TRUE;
|
|
|
|
}
|
|
|
|
else if (!strcmp(aTopic, NS_PREFBRANCH_PREFCHANGE_TOPIC_ID)) {
|
|
|
|
nsCOMPtr<nsIPrefBranch> prefs(do_QueryInterface(aSubject));
|
|
|
|
PRBool enabled;
|
|
|
|
nsresult rv = prefs->GetBoolPref(PREFETCH_PREF, &enabled);
|
|
|
|
if (NS_SUCCEEDED(rv) && enabled) {
|
|
|
|
if (mDisabled) {
|
|
|
|
LOG(("enabling prefetching\n"));
|
|
|
|
mDisabled = PR_FALSE;
|
|
|
|
AddProgressListener();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (!mDisabled) {
|
|
|
|
LOG(("disabling prefetching\n"));
|
|
|
|
StopPrefetching();
|
2009-02-24 23:12:12 -08:00
|
|
|
EmptyQueue();
|
2007-03-22 10:30:00 -07:00
|
|
|
mDisabled = PR_TRUE;
|
|
|
|
RemoveProgressListener();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// vim: ts=4 sw=4 expandtab
|