Backed out changeset bbaf0d5fef61 (bug 442803)

This commit is contained in:
Dave Camp 2008-08-19 22:54:37 -07:00
parent c57900b94b
commit 01ca11fc8d
3 changed files with 193 additions and 62 deletions

View File

@ -59,8 +59,14 @@ interface nsIPrefetchService : nsISupports
in boolean aExplicit);
/**
* @status DEPRECATED This method is no longer used, and will throw
* NS_ERROR_NOT_IMPLEMENTED.
* Enqueue a request to prefetch the specified URI, making sure it's in the
* offline cache.
*
* @param aURI the URI of the document to prefetch
* @param aReferrerURI the URI of the referring page
* @param aSource the DOM node (such as a <link> tag) that requested this
* fetch, or null if the prefetch was not requested by a DOM node.
* @param aExplicit the link element has an explicit offline link type
*/
void prefetchURIForOfflineUse(in nsIURI aURI,
in nsIURI aReferrerURI,
@ -71,12 +77,9 @@ interface nsIPrefetchService : nsISupports
* Enumerate the items in the prefetch queue. Each element in the
* enumeration is an nsIDOMLoadStatus.
*
* @param aIncludeNormalItems include normal prefetch items in the
* list. This parameter is deprecated and must be TRUE,
* or NS_ERROR_INT_IMPLEMENTED will be thrown.
* @param aIncludeNormalItems include normal prefetch items in the list.
* @param aIncludeOfflineItems include items being fetched for offline
* use. This parameter is deprecated and must be FALSE,
* or NS_ERROR_NOT_IMPLEMENTED will be thrown.
* use.
*/
nsISimpleEnumerator enumerateQueue(in boolean aIncludeNormalItems,
in boolean aIncludeOfflineItems);

View File

@ -37,6 +37,7 @@
#include "nsPrefetchService.h"
#include "nsICacheSession.h"
#include "nsIOfflineCacheSession.h"
#include "nsICacheService.h"
#include "nsIServiceManager.h"
#include "nsICategoryManager.h"
@ -103,7 +104,9 @@ class nsPrefetchQueueEnumerator : public nsISimpleEnumerator
public:
NS_DECL_ISUPPORTS
NS_DECL_NSISIMPLEENUMERATOR
nsPrefetchQueueEnumerator(nsPrefetchService *aService);
nsPrefetchQueueEnumerator(nsPrefetchService *aService,
PRBool aIncludeNormal,
PRBool aIncludeOffline);
~nsPrefetchQueueEnumerator();
private:
@ -111,14 +114,20 @@ private:
nsRefPtr<nsPrefetchService> mService;
nsRefPtr<nsPrefetchNode> mCurrent;
PRBool mIncludeNormal;
PRBool mIncludeOffline;
PRBool mStarted;
};
//-----------------------------------------------------------------------------
// nsPrefetchQueueEnumerator <public>
//-----------------------------------------------------------------------------
nsPrefetchQueueEnumerator::nsPrefetchQueueEnumerator(nsPrefetchService *aService)
nsPrefetchQueueEnumerator::nsPrefetchQueueEnumerator(nsPrefetchService *aService,
PRBool aIncludeNormal,
PRBool aIncludeOffline)
: mService(aService)
, mIncludeNormal(aIncludeNormal)
, mIncludeOffline(aIncludeOffline)
, mStarted(PR_FALSE)
{
Increment();
@ -178,7 +187,8 @@ nsPrefetchQueueEnumerator::Increment()
mCurrent = mCurrent->mNext;
}
}
} while (mCurrent);
} while (mCurrent && mIncludeOffline != mCurrent->mOffline &&
mIncludeNormal == mCurrent->mOffline);
}
//-----------------------------------------------------------------------------
@ -194,10 +204,12 @@ NS_IMPL_ISUPPORTS1(nsPrefetchQueueEnumerator, nsISimpleEnumerator)
nsPrefetchNode::nsPrefetchNode(nsPrefetchService *aService,
nsIURI *aURI,
nsIURI *aReferrerURI,
nsIDOMNode *aSource)
nsIDOMNode *aSource,
PRBool aOffline)
: mNext(nsnull)
, mURI(aURI)
, mReferrerURI(aReferrerURI)
, mOffline(aOffline)
, mService(aService)
, mChannel(nsnull)
, mState(nsIDOMLoadStatus::UNINITIALIZED)
@ -223,10 +235,21 @@ nsPrefetchNode::OpenChannel()
httpChannel->SetReferrer(mReferrerURI);
httpChannel->SetRequestHeader(
NS_LITERAL_CSTRING("X-Moz"),
mOffline ?
NS_LITERAL_CSTRING("offline-resource") :
NS_LITERAL_CSTRING("prefetch"),
PR_FALSE);
}
if (mOffline) {
nsCOMPtr<nsICachingChannel> cachingChannel =
do_QueryInterface(mChannel);
if (cachingChannel) {
rv = cachingChannel->SetCacheForOfflineUse(PR_TRUE);
NS_ENSURE_SUCCESS(rv, rv);
}
}
rv = mChannel->AsyncOpen(this, nsnull);
NS_ENSURE_SUCCESS(rv, rv);
@ -271,34 +294,40 @@ nsPrefetchNode::OnStartRequest(nsIRequest *aRequest,
do_QueryInterface(aRequest, &rv);
if (NS_FAILED(rv)) return rv;
// 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;
}
//
// 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
nsCOMPtr<nsICacheEntryInfo> entryInfo =
do_QueryInterface(cacheToken, &rv);
PRBool cacheForOfflineUse;
rv = cachingChannel->GetCacheForOfflineUse(&cacheForOfflineUse);
if (NS_FAILED(rv)) return rv;
PRUint32 expTime;
if (NS_SUCCEEDED(entryInfo->GetExpirationTime(&expTime))) {
if (NowInSeconds() >= expTime) {
LOG(("document cannot be reused from cache; "
"canceling prefetch\n"));
if (!cacheForOfflineUse) {
// 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;
}
//
// 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
nsCOMPtr<nsICacheEntryInfo> entryInfo =
do_QueryInterface(cacheToken, &rv);
if (NS_FAILED(rv)) return rv;
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;
}
}
}
mState = nsIDOMLoadStatus::RECEIVING;
@ -372,14 +401,26 @@ nsPrefetchNode::OnChannelRedirect(nsIChannel *aOldChannel,
if (NS_FAILED(rv))
return rv;
PRBool offline;
nsCOMPtr<nsICachingChannel> oldCachingChannel =
do_QueryInterface(aOldChannel);
if (NS_SUCCEEDED(oldCachingChannel->GetCacheForOfflineUse(&offline)) &&
offline) {
nsCOMPtr<nsICachingChannel> newCachingChannel =
do_QueryInterface(aOldChannel);
if (newCachingChannel)
newCachingChannel->SetCacheForOfflineUse(PR_TRUE);
}
PRBool match;
rv = newURI->SchemeIs("http", &match);
if (NS_FAILED(rv) || !match) {
LOG(("rejected: URL is not of type http\n"));
return NS_ERROR_ABORT;
if (!offline ||
NS_FAILED(newURI->SchemeIs("https", &match)) ||
!match) {
LOG(("rejected: URL is not of type http\n"));
return NS_ERROR_ABORT;
}
}
// HTTP request headers are not automatically forwarded to the new channel.
@ -387,7 +428,9 @@ nsPrefetchNode::OnChannelRedirect(nsIChannel *aOldChannel,
NS_ENSURE_STATE(httpChannel);
httpChannel->SetRequestHeader(NS_LITERAL_CSTRING("X-Moz"),
NS_LITERAL_CSTRING("prefetch"),
offline ?
NS_LITERAL_CSTRING("offline-resource") :
NS_LITERAL_CSTRING("prefetch"),
PR_FALSE);
mChannel = aNewChannel;
@ -406,6 +449,7 @@ nsPrefetchService::nsPrefetchService()
, mStopCount(0)
, mHaveProcessed(PR_FALSE)
, mDisabled(PR_TRUE)
, mFetchedOffline(PR_FALSE)
{
}
@ -413,7 +457,7 @@ nsPrefetchService::~nsPrefetchService()
{
// cannot reach destructor if prefetch in progress (listener owns reference
// to this service)
EmptyQueue();
EmptyQueue(PR_TRUE);
}
nsresult
@ -461,6 +505,18 @@ nsPrefetchService::ProcessNextURI()
do {
rv = DequeueNode(getter_AddRefs(mCurrentNode));
if (rv == NS_ERROR_NOT_AVAILABLE && mFetchedOffline) {
// done loading stuff, go ahead and evict unowned entries from
// the offline cache
mFetchedOffline = PR_FALSE;
nsCOMPtr<nsIOfflineCacheSession> session;
rv = GetOfflineCacheSession(getter_AddRefs(session));
if (NS_FAILED(rv)) break;
session->EvictUnownedEntries();
break;
}
if (NS_FAILED(rv)) break;
@ -476,6 +532,8 @@ nsPrefetchService::ProcessNextURI()
// if opening the channel fails, then just skip to the next uri
//
rv = mCurrentNode->OpenChannel();
if (NS_SUCCEEDED(rv) && mCurrentNode->mOffline)
mFetchedOffline = PR_TRUE;
}
while (NS_FAILED(rv));
}
@ -489,8 +547,11 @@ nsPrefetchService::NotifyLoadRequested(nsPrefetchNode *node)
do_GetService("@mozilla.org/observer-service;1", &rv);
if (NS_FAILED(rv)) return;
const char *topic = node->mOffline ? "offline-load-requested" :
"prefetch-load-requested";
observerService->NotifyObservers(static_cast<nsIDOMLoadStatus*>(node),
"prefetch-load-requested", nsnull);
topic, nsnull);
}
void
@ -502,8 +563,11 @@ nsPrefetchService::NotifyLoadCompleted(nsPrefetchNode *node)
do_GetService("@mozilla.org/observer-service;1", &rv);
if (NS_FAILED(rv)) return;
const char *topic = node->mOffline ? "offline-load-completed" :
"prefetch-load-completed";
observerService->NotifyObservers(static_cast<nsIDOMLoadStatus*>(node),
"prefetch-load-completed", nsnull);
topic, nsnull);
}
//-----------------------------------------------------------------------------
@ -551,10 +615,11 @@ nsresult
nsPrefetchService::EnqueueURI(nsIURI *aURI,
nsIURI *aReferrerURI,
nsIDOMNode *aSource,
PRBool aOffline,
nsPrefetchNode **aNode)
{
nsPrefetchNode *node = new nsPrefetchNode(this, aURI, aReferrerURI,
aSource);
aSource, aOffline);
if (!node)
return NS_ERROR_OUT_OF_MEMORY;
@ -581,23 +646,52 @@ nsPrefetchService::DequeueNode(nsPrefetchNode **node)
}
void
nsPrefetchService::EmptyQueue()
nsPrefetchService::EmptyQueue(PRBool includeOffline)
{
nsPrefetchNode *prev = 0;
nsPrefetchNode *node = mQueueHead;
while (node) {
nsPrefetchNode *next = node->mNext;
if (prev)
prev->mNext = next;
if (includeOffline || !node->mOffline) {
if (prev)
prev->mNext = next;
else
mQueueHead = next;
NS_RELEASE(node);
}
else
mQueueHead = next;
NS_RELEASE(node);
prev = node;
node = next;
}
}
nsresult
nsPrefetchService::GetOfflineCacheSession(nsIOfflineCacheSession **aSession)
{
if (!mOfflineCacheSession) {
nsresult rv;
nsCOMPtr<nsICacheService> serv =
do_GetService(NS_CACHESERVICE_CONTRACTID,
&rv);
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsICacheSession> session;
rv = serv->CreateSession("HTTP-offline",
nsICache::STORE_OFFLINE,
nsICache::STREAM_BASED,
getter_AddRefs(session));
NS_ENSURE_SUCCESS(rv, rv);
mOfflineCacheSession = do_QueryInterface(session, &rv);
NS_ENSURE_SUCCESS(rv, rv);
}
NS_ADDREF(*aSession = mOfflineCacheSession);
return NS_OK;
}
void
nsPrefetchService::StartPrefetching()
{
@ -631,9 +725,14 @@ nsPrefetchService::StopPrefetching()
if (!mCurrentNode)
return;
// if it's an offline prefetch, requeue it for when prefetching starts
// again
if (mCurrentNode->mOffline)
EnqueueNode(mCurrentNode);
mCurrentNode->CancelChannel(NS_BINDING_ABORTED);
mCurrentNode = nsnull;
EmptyQueue();
EmptyQueue(PR_FALSE);
}
//-----------------------------------------------------------------------------
@ -654,7 +753,8 @@ nsresult
nsPrefetchService::Prefetch(nsIURI *aURI,
nsIURI *aReferrerURI,
nsIDOMNode *aSource,
PRBool aExplicit)
PRBool aExplicit,
PRBool aOffline)
{
nsresult rv;
@ -688,6 +788,11 @@ nsPrefetchService::Prefetch(nsIURI *aURI,
PRBool match;
rv = aURI->SchemeIs("http", &match);
if (NS_FAILED(rv) || !match) {
if (aOffline) {
// Offline https urls can be prefetched
rv = aURI->SchemeIs("https", &match);
}
if (NS_FAILED(rv) || !match) {
LOG(("rejected: URL is not of type http\n"));
return NS_ERROR_ABORT;
@ -699,6 +804,11 @@ nsPrefetchService::Prefetch(nsIURI *aURI,
//
rv = aReferrerURI->SchemeIs("http", &match);
if (NS_FAILED(rv) || !match) {
if (aOffline) {
// Offline https urls can be prefetched
rv = aURI->SchemeIs("https", &match);
}
if (NS_FAILED(rv) || !match) {
LOG(("rejected: referrer URL is not of type http\n"));
return NS_ERROR_ABORT;
@ -724,8 +834,12 @@ nsPrefetchService::Prefetch(nsIURI *aURI,
if (mCurrentNode) {
PRBool equals;
if (NS_SUCCEEDED(mCurrentNode->mURI->Equals(aURI, &equals)) && equals) {
LOG(("rejected: URL is already being prefetched\n"));
return NS_ERROR_ABORT;
// We may still need to put it on the queue if the channel
// isn't fetching to the offline cache
if (!aOffline || mCurrentNode->mOffline) {
LOG(("rejected: URL is already being prefetched\n"));
return NS_ERROR_ABORT;
}
}
}
@ -736,13 +850,17 @@ nsPrefetchService::Prefetch(nsIURI *aURI,
for (; node; node = node->mNext) {
PRBool equals;
if (NS_SUCCEEDED(node->mURI->Equals(aURI, &equals)) && equals) {
if (aOffline) {
// make sure the node is placed in the offline cache
node->mOffline = PR_TRUE;
}
LOG(("rejected: URL is already on prefetch queue\n"));
return NS_ERROR_ABORT;
}
}
nsRefPtr<nsPrefetchNode> enqueuedNode;
rv = EnqueueURI(aURI, aReferrerURI, aSource,
rv = EnqueueURI(aURI, aReferrerURI, aSource, aOffline,
getter_AddRefs(enqueuedNode));
NS_ENSURE_SUCCESS(rv, rv);
@ -761,7 +879,7 @@ nsPrefetchService::PrefetchURI(nsIURI *aURI,
nsIDOMNode *aSource,
PRBool aExplicit)
{
return Prefetch(aURI, aReferrerURI, aSource, aExplicit);
return Prefetch(aURI, aReferrerURI, aSource, aExplicit, PR_FALSE);
}
NS_IMETHODIMP
@ -770,7 +888,7 @@ nsPrefetchService::PrefetchURIForOfflineUse(nsIURI *aURI,
nsIDOMNode *aSource,
PRBool aExplicit)
{
return NS_ERROR_NOT_IMPLEMENTED;
return Prefetch(aURI, aReferrerURI, aSource, aExplicit, PR_TRUE);
}
NS_IMETHODIMP
@ -778,10 +896,9 @@ nsPrefetchService::EnumerateQueue(PRBool aIncludeNormalItems,
PRBool aIncludeOfflineItems,
nsISimpleEnumerator **aEnumerator)
{
NS_ENSURE_TRUE(aIncludeNormalItems && !aIncludeOfflineItems,
NS_ERROR_NOT_IMPLEMENTED);
*aEnumerator = new nsPrefetchQueueEnumerator(this);
*aEnumerator = new nsPrefetchQueueEnumerator(this,
aIncludeNormalItems,
aIncludeOfflineItems);
if (!*aEnumerator) return NS_ERROR_OUT_OF_MEMORY;
NS_ADDREF(*aEnumerator);

View File

@ -56,6 +56,7 @@
class nsPrefetchService;
class nsPrefetchListener;
class nsPrefetchNode;
class nsIOfflineCacheSession;
//-----------------------------------------------------------------------------
// nsPrefetchService
@ -89,19 +90,25 @@ private:
nsresult Prefetch(nsIURI *aURI,
nsIURI *aReferrerURI,
nsIDOMNode *aSource,
PRBool aExplicit);
PRBool aExplicit,
PRBool aOffline);
void AddProgressListener();
void RemoveProgressListener();
nsresult EnqueueURI(nsIURI *aURI, nsIURI *aReferrerURI,
nsIDOMNode *aSource, nsPrefetchNode **node);
nsIDOMNode *aSource, PRBool aOffline,
nsPrefetchNode **node);
nsresult EnqueueNode(nsPrefetchNode *node);
nsresult DequeueNode(nsPrefetchNode **node);
void EmptyQueue();
void EmptyQueue(PRBool includeOffline);
nsresult SaveOfflineList(nsIURI *aDocumentUri,
nsIDOMDocument *aDoc);
nsresult GetOfflineCacheSession(nsIOfflineCacheSession **aSession);
void StartPrefetching();
void StopPrefetching();
nsCOMPtr<nsIOfflineCacheSession> mOfflineCacheSession;
nsPrefetchNode *mQueueHead;
nsPrefetchNode *mQueueTail;
nsRefPtr<nsPrefetchNode> mCurrentNode;
@ -109,6 +116,8 @@ private:
// true if pending document loads have ever reached zero.
PRInt32 mHaveProcessed;
PRBool mDisabled;
PRBool mFetchedOffline;
};
//-----------------------------------------------------------------------------
@ -131,7 +140,8 @@ public:
nsPrefetchNode(nsPrefetchService *aPrefetchService,
nsIURI *aURI,
nsIURI *aReferrerURI,
nsIDOMNode *aSource);
nsIDOMNode *aSource,
PRBool aOffline);
~nsPrefetchNode() {}
@ -142,6 +152,7 @@ public:
nsCOMPtr<nsIURI> mURI;
nsCOMPtr<nsIURI> mReferrerURI;
nsCOMPtr<nsIWeakReference> mSource;
PRBool mOffline;
private:
nsRefPtr<nsPrefetchService> mService;