Bug 767096 - Optimize nsICachingChannel interface for offline cache writing, r=sinker

This commit is contained in:
Honza Bambas 2012-07-10 23:49:18 +02:00
Родитель 4c04a1f793
Коммит 3c33725683
9 изменённых файлов: 93 добавлений и 158 удалений

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

@ -79,7 +79,7 @@ interface nsIApplicationCacheNamespace : nsISupports
* loads. Inactive caches will be removed from the cache when they are
* no longer referenced.
*/
[scriptable, uuid(231e1e53-05c1-41b6-b9de-dbbcce9385c9)]
[scriptable, uuid(C3A17414-763B-4235-8BB7-B48324F95DF8)]
interface nsIApplicationCache : nsISupports
{
/**
@ -195,5 +195,5 @@ interface nsIApplicationCache : nsISupports
* If set, this offline cache is placed in a different directory
* than the current application profile.
*/
readonly attribute nsIFile cacheDirectory;
readonly attribute nsIFile profileDirectory;
};

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

@ -10,7 +10,7 @@
/**
* Interface implemented by channels that support application caches.
*/
[scriptable, uuid(8d9024e6-ab01-442d-8119-2f55d55d91b0)]
[scriptable, uuid(6FA816B1-6D5F-4380-9704-054D0908CFA3)]
interface nsIApplicationCacheChannel : nsIApplicationCacheContainer
{
/**
@ -49,4 +49,10 @@ interface nsIApplicationCacheChannel : nsIApplicationCacheContainer
* action handling in nsContentSink.
*/
void markOfflineCacheEntryAsForeign();
/**
* Set offline application cache object to instruct the channel
* to cache for offline use using this application cache.
*/
attribute nsIApplicationCache applicationCacheForWrite;
};

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

@ -17,7 +17,7 @@ interface nsIFile;
* 3) Support for uniquely identifying cached data in cases when the URL
* is insufficient (e.g., HTTP form submission).
*/
[scriptable, uuid(E2143B61-62FE-4da5-BE2E-E31981095889)]
[scriptable, uuid(8456EBEE-7127-485b-9518-C2E328C09F5D)]
interface nsICachingChannel : nsICacheInfoChannel
{
/**
@ -73,26 +73,6 @@ interface nsICachingChannel : nsICacheInfoChannel
*/
attribute boolean cacheAsFile;
/**
* Specifies whether or not the data should be placed in the offline cache,
* in addition to normal memory/disk caching. This may fail if the offline
* cache is not present. The value of this attribute should be set before
* opening the channel.
*/
attribute boolean cacheForOfflineUse;
/**
* The session into which to cache offline data. If not specified,
* data will be placed in "HTTP-offline"
*/
attribute ACString offlineCacheClientID;
/**
* Override base (profile) directory to work with when accessing the cache.
* When not specified, the current process' profile directory will be used.
*/
attribute nsIFile profileDirectory;
/**
* Get the "file" where the cached data can be found. This is valid for
* as long as a reference to the cache token is held. This may return

2
netwerk/cache/nsDiskCacheDeviceSQL.cpp поставляемый
Просмотреть файл

@ -631,7 +631,7 @@ nsApplicationCache::GetClientID(nsACString &out)
}
NS_IMETHODIMP
nsApplicationCache::GetCacheDirectory(nsIFile **out)
nsApplicationCache::GetProfileDirectory(nsIFile **out)
{
if (mDevice->BaseDirectory())
NS_ADDREF(*out = mDevice->BaseDirectory());

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

@ -1221,6 +1221,21 @@ HttpChannelChild::SetApplicationCache(nsIApplicationCache *aApplicationCache)
// HttpChannelChild::nsIApplicationCacheChannel
//-----------------------------------------------------------------------------
NS_IMETHODIMP
HttpChannelChild::GetApplicationCacheForWrite(nsIApplicationCache **aApplicationCache)
{
*aApplicationCache = nsnull;
return NS_OK;
}
NS_IMETHODIMP
HttpChannelChild::SetApplicationCacheForWrite(nsIApplicationCache *aApplicationCache)
{
NS_ENSURE_TRUE(!mWasOpened, NS_ERROR_ALREADY_OPENED);
// Child channels are not intended to be used for cache writes
return NS_ERROR_NOT_IMPLEMENTED;
}
NS_IMETHODIMP
HttpChannelChild::GetLoadedFromApplicationCache(bool *aLoadedFromApplicationCache)
{

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

@ -201,7 +201,7 @@ public:
, mHasQueryString(HasQueryString(channel->mRequestHead.Method(),
channel->mURI))
, mLoadFlags(channel->mLoadFlags)
, mCacheForOfflineUse(channel->mCacheForOfflineUse)
, mCacheForOfflineUse(!!channel->mApplicationCacheForWrite)
, mFallbackChannel(channel->mFallbackChannel)
, mClientID(clientID)
, mStoragePolicy(storagePolicy)
@ -310,7 +310,6 @@ nsHttpChannel::nsHttpChannel()
, mAuthRetryPending(false)
, mResuming(false)
, mInitedCacheEntry(false)
, mCacheForOfflineUse(false)
, mFallbackChannel(false)
, mCustomConditionalRequest(false)
, mFallingBack(false)
@ -450,7 +449,7 @@ nsHttpChannel::Connect()
// if cacheForOfflineUse has been set, open up an offline cache
// entry to update
if (mCacheForOfflineUse) {
if (mApplicationCacheForWrite) {
rv = OpenOfflineCacheEntryForWriting();
if (NS_FAILED(rv)) return rv;
@ -996,7 +995,7 @@ nsHttpChannel::CallOnStartRequest()
rv = InstallOfflineCacheListener();
if (NS_FAILED(rv)) return rv;
}
} else if (mCacheForOfflineUse) {
} else if (mApplicationCacheForWrite) {
LOG(("offline cache is up to date, not updating"));
CloseOfflineCacheEntry();
}
@ -1368,7 +1367,7 @@ nsHttpChannel::ContinueProcessResponse(nsresult rv)
InitCacheEntry();
CloseCacheEntry(false);
if (mCacheForOfflineUse) {
if (mApplicationCacheForWrite) {
// Store response in the offline cache
InitOfflineCacheEntry();
CloseOfflineCacheEntry();
@ -2269,8 +2268,7 @@ nsHttpChannel::ProcessFallback(bool *waitingForRedirectCallback)
mOfflineCacheAccess = 0;
}
mCacheForOfflineUse = false;
mOfflineCacheClientID.Truncate();
mApplicationCacheForWrite = nsnull;
mOfflineCacheEntry = 0;
mOfflineCacheAccess = 0;
@ -2504,7 +2502,7 @@ nsHttpChannel::OnOfflineCacheEntryAvailable(nsICacheEntryDescriptor *aEntry,
if (NS_SUCCEEDED(aEntryStatus))
return NS_OK;
if (!mCacheForOfflineUse && !mFallbackChannel) {
if (!mApplicationCacheForWrite && !mFallbackChannel) {
nsCAutoString cacheKey;
GenerateCacheKey(mPostID, cacheKey);
@ -2641,7 +2639,14 @@ nsHttpChannel::OpenOfflineCacheEntryForWriting()
nsCAutoString cacheKey;
GenerateCacheKey(mPostID, cacheKey);
NS_ENSURE_TRUE(!mOfflineCacheClientID.IsEmpty(),
NS_ENSURE_TRUE(mApplicationCacheForWrite,
NS_ERROR_NOT_AVAILABLE);
nsCAutoString offlineCacheClientID;
rv = mApplicationCacheForWrite->GetClientID(offlineCacheClientID);
NS_ENSURE_SUCCESS(rv, rv);
NS_ENSURE_TRUE(!offlineCacheClientID.IsEmpty(),
NS_ERROR_NOT_AVAILABLE);
nsCOMPtr<nsICacheSession> session;
@ -2649,14 +2654,19 @@ nsHttpChannel::OpenOfflineCacheEntryForWriting()
do_GetService(NS_CACHESERVICE_CONTRACTID, &rv);
if (NS_FAILED(rv)) return rv;
rv = serv->CreateSession(mOfflineCacheClientID.get(),
rv = serv->CreateSession(offlineCacheClientID.get(),
nsICache::STORE_OFFLINE,
nsICache::STREAM_BASED,
getter_AddRefs(session));
if (NS_FAILED(rv)) return rv;
if (mProfileDirectory) {
rv = session->SetProfileDirectory(mProfileDirectory);
nsCOMPtr<nsIFile> profileDirectory;
rv = mApplicationCacheForWrite->GetProfileDirectory(
getter_AddRefs(profileDirectory));
NS_ENSURE_SUCCESS(rv, rv);
if (profileDirectory) {
rv = session->SetProfileDirectory(profileDirectory);
if (NS_FAILED(rv)) return rv;
}
@ -3296,7 +3306,7 @@ HttpCacheQuery::MustValidateBasedOnQueryUrl() const
bool
nsHttpChannel::ShouldUpdateOfflineCacheEntry()
{
if (!mCacheForOfflineUse || !mOfflineCacheEntry) {
if (!mApplicationCacheForWrite || !mOfflineCacheEntry) {
return false;
}
@ -3487,7 +3497,7 @@ nsHttpChannel::ReadFromCache(bool alreadyMarkedValid)
}
if ((mLoadFlags & LOAD_ONLY_IF_MODIFIED) && !mCachedContentIsPartial) {
if (!mCacheForOfflineUse) {
if (!mApplicationCacheForWrite) {
LOG(("Skipping read from cache based on LOAD_ONLY_IF_MODIFIED "
"load flag\n"));
MOZ_ASSERT(!mCacheInputStream);
@ -3498,7 +3508,7 @@ nsHttpChannel::ReadFromCache(bool alreadyMarkedValid)
if (!ShouldUpdateOfflineCacheEntry()) {
LOG(("Skipping read from cache based on LOAD_ONLY_IF_MODIFIED "
"load flag (mCacheForOfflineUse case)\n"));
"load flag (mApplicationCacheForWrite not null case)\n"));
mCacheInputStream.CloseAndRelease();
// TODO: Bug 759040 - We should call HandleAsyncNotModified directly
// here, to avoid event dispatching latency.
@ -3989,11 +3999,12 @@ nsHttpChannel::SetupReplacementChannel(nsIURI *newURI,
cachingChannel->SetCacheKey(cacheKey);
}
}
}
// cacheClientID, cacheForOfflineUse
cachingChannel->SetOfflineCacheClientID(mOfflineCacheClientID);
cachingChannel->SetCacheForOfflineUse(mCacheForOfflineUse);
cachingChannel->SetProfileDirectory(mProfileDirectory);
nsCOMPtr<nsIApplicationCacheChannel> appCacheChannel = do_QueryInterface(newChannel);
if (appCacheChannel) {
// app cache for write
appCacheChannel->SetApplicationCacheForWrite(mApplicationCacheForWrite);
}
}
@ -5379,62 +5390,6 @@ nsHttpChannel::SetCacheAsFile(bool value)
return mCacheEntry->SetStoragePolicy(policy);
}
NS_IMETHODIMP
nsHttpChannel::GetCacheForOfflineUse(bool *value)
{
*value = mCacheForOfflineUse;
return NS_OK;
}
NS_IMETHODIMP
nsHttpChannel::SetCacheForOfflineUse(bool value)
{
ENSURE_CALLED_BEFORE_ASYNC_OPEN();
mCacheForOfflineUse = value;
return NS_OK;
}
NS_IMETHODIMP
nsHttpChannel::GetOfflineCacheClientID(nsACString &value)
{
value = mOfflineCacheClientID;
return NS_OK;
}
NS_IMETHODIMP
nsHttpChannel::SetOfflineCacheClientID(const nsACString &value)
{
ENSURE_CALLED_BEFORE_ASYNC_OPEN();
mOfflineCacheClientID = value;
return NS_OK;
}
NS_IMETHODIMP
nsHttpChannel::GetProfileDirectory(nsIFile **_result)
{
NS_ENSURE_ARG(_result);
if (!mProfileDirectory)
return NS_ERROR_NOT_AVAILABLE;
NS_ADDREF(*_result = mProfileDirectory);
return NS_OK;
}
NS_IMETHODIMP
nsHttpChannel::SetProfileDirectory(nsIFile *value)
{
mProfileDirectory = value;
return NS_OK;
}
NS_IMETHODIMP
nsHttpChannel::GetCacheFile(nsIFile **cacheFile)
{
@ -5545,9 +5500,9 @@ nsHttpChannel::OnCacheEntryAvailableInternal(nsICacheEntryDescriptor *entry,
// proceed without using the cache
}
// if cacheForOfflineUse has been set, open up an offline cache entry
// if app cache for write has been set, open up an offline cache entry
// to update
if (mCacheForOfflineUse) {
if (mApplicationCacheForWrite) {
rv = OpenOfflineCacheEntryForWriting();
if (mOnCacheEntryAvailableCallback) {
NS_ASSERTION(NS_SUCCEEDED(rv), "Unexpected state");
@ -5633,6 +5588,7 @@ nsHttpChannel::DoAuthRetry(nsAHttpConnection *conn)
//-----------------------------------------------------------------------------
// nsHttpChannel::nsIApplicationCacheChannel
//-----------------------------------------------------------------------------
NS_IMETHODIMP
nsHttpChannel::GetApplicationCache(nsIApplicationCache **out)
{
@ -5649,6 +5605,22 @@ nsHttpChannel::SetApplicationCache(nsIApplicationCache *appCache)
return NS_OK;
}
NS_IMETHODIMP
nsHttpChannel::GetApplicationCacheForWrite(nsIApplicationCache **out)
{
NS_IF_ADDREF(*out = mApplicationCacheForWrite);
return NS_OK;
}
NS_IMETHODIMP
nsHttpChannel::SetApplicationCacheForWrite(nsIApplicationCache *appCache)
{
ENSURE_CALLED_BEFORE_ASYNC_OPEN();
mApplicationCacheForWrite = appCache;
return NS_OK;
}
NS_IMETHODIMP
nsHttpChannel::GetLoadedFromApplicationCache(bool *aLoadedFromApplicationCache)
{

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

@ -310,9 +310,7 @@ private:
nsCOMPtr<nsICacheEntryDescriptor> mOfflineCacheEntry;
nsCacheAccessMode mOfflineCacheAccess;
PRUint32 mOfflineCacheLastModifiedTime;
nsCString mOfflineCacheClientID;
nsCOMPtr<nsIFile> mProfileDirectory;
nsCOMPtr<nsIApplicationCache> mApplicationCacheForWrite;
// auth specific data
nsCOMPtr<nsIHttpChannelAuthProvider> mAuthProvider;
@ -340,7 +338,6 @@ private:
PRUint32 mAuthRetryPending : 1;
PRUint32 mResuming : 1;
PRUint32 mInitedCacheEntry : 1;
PRUint32 mCacheForOfflineUse : 1;
// True if we are loading a fallback cache entry from the
// application cache.
PRUint32 mFallbackChannel : 1;

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

@ -290,13 +290,11 @@ nsOfflineCacheUpdateItem::nsOfflineCacheUpdateItem(nsIURI *aURI,
nsIURI *aReferrerURI,
nsIApplicationCache *aApplicationCache,
nsIApplicationCache *aPreviousApplicationCache,
const nsACString &aClientID,
PRUint32 type)
: mURI(aURI)
, mReferrerURI(aReferrerURI)
, mApplicationCache(aApplicationCache)
, mPreviousApplicationCache(aPreviousApplicationCache)
, mClientID(aClientID)
, mItemType(type)
, mChannel(nsnull)
, mState(nsIDOMLoadStatus::UNINITIALIZED)
@ -340,6 +338,10 @@ nsOfflineCacheUpdateItem::OpenChannel(nsOfflineCacheUpdate *aUpdate)
rv = appCacheChannel->SetApplicationCache(mPreviousApplicationCache);
NS_ENSURE_SUCCESS(rv, rv);
// Set the new application cache as the targer for write.
rv = appCacheChannel->SetApplicationCacheForWrite(mApplicationCache);
NS_ENSURE_SUCCESS(rv, rv);
// configure HTTP specific stuff
nsCOMPtr<nsIHttpChannel> httpChannel =
do_QueryInterface(mChannel);
@ -350,25 +352,6 @@ nsOfflineCacheUpdateItem::OpenChannel(nsOfflineCacheUpdate *aUpdate)
false);
}
nsCOMPtr<nsICachingChannel> cachingChannel =
do_QueryInterface(mChannel);
if (cachingChannel) {
rv = cachingChannel->SetCacheForOfflineUse(true);
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIFile> cacheDirectory;
rv = mApplicationCache->GetCacheDirectory(getter_AddRefs(cacheDirectory));
NS_ENSURE_SUCCESS(rv, rv);
rv = cachingChannel->SetProfileDirectory(cacheDirectory);
NS_ENSURE_SUCCESS(rv, rv);
if (!mClientID.IsEmpty()) {
rv = cachingChannel->SetOfflineCacheClientID(mClientID);
NS_ENSURE_SUCCESS(rv, rv);
}
}
rv = mChannel->AsyncOpen(this, nsnull);
NS_ENSURE_SUCCESS(rv, rv);
@ -505,22 +488,10 @@ nsOfflineCacheUpdateItem::AsyncOnChannelRedirect(nsIChannel *aOldChannel,
if (NS_FAILED(rv))
return rv;
nsCOMPtr<nsICachingChannel> newCachingChannel =
nsCOMPtr<nsIApplicationCacheChannel> appCacheChannel =
do_QueryInterface(aNewChannel);
if (newCachingChannel) {
rv = newCachingChannel->SetCacheForOfflineUse(true);
NS_ENSURE_SUCCESS(rv, rv);
if (!mClientID.IsEmpty()) {
rv = newCachingChannel->SetOfflineCacheClientID(mClientID);
NS_ENSURE_SUCCESS(rv, rv);
}
nsCOMPtr<nsIFile> cacheDirectory;
rv = mApplicationCache->GetCacheDirectory(getter_AddRefs(cacheDirectory));
NS_ENSURE_SUCCESS(rv, rv);
rv = newCachingChannel->SetProfileDirectory(cacheDirectory);
if (appCacheChannel) {
rv = appCacheChannel->SetApplicationCacheForWrite(mApplicationCache);
NS_ENSURE_SUCCESS(rv, rv);
}
@ -684,10 +655,9 @@ nsOfflineCacheUpdateItem::GetStatus(PRUint16 *aStatus)
nsOfflineManifestItem::nsOfflineManifestItem(nsIURI *aURI,
nsIURI *aReferrerURI,
nsIApplicationCache *aApplicationCache,
nsIApplicationCache *aPreviousApplicationCache,
const nsACString &aClientID)
nsIApplicationCache *aPreviousApplicationCache)
: nsOfflineCacheUpdateItem(aURI, aReferrerURI,
aApplicationCache, aPreviousApplicationCache, aClientID,
aApplicationCache, aPreviousApplicationCache,
nsIApplicationCache::ITEM_MANIFEST)
, mParserState(PARSE_INIT)
, mNeedsUpdate(true)
@ -1264,9 +1234,6 @@ nsOfflineCacheUpdate::Init(nsIURI *aManifestURI,
NS_ENSURE_SUCCESS(rv, rv);
}
rv = mApplicationCache->GetClientID(mClientID);
NS_ENSURE_SUCCESS(rv, rv);
rv = nsOfflineCacheUpdateService::OfflineAppPinnedForURI(aDocumentURI,
NULL,
&mPinned);
@ -1292,7 +1259,6 @@ nsOfflineCacheUpdate::InitPartial(nsIURI *aManifestURI,
LOG(("nsOfflineCacheUpdate::InitPartial [%p]", this));
mPartialUpdate = true;
mClientID = clientID;
mDocumentURI = aDocumentURI;
mManifestURI = aManifestURI;
@ -1303,7 +1269,7 @@ nsOfflineCacheUpdate::InitPartial(nsIURI *aManifestURI,
do_GetService(NS_APPLICATIONCACHESERVICE_CONTRACTID, &rv);
NS_ENSURE_SUCCESS(rv, rv);
rv = cacheService->GetApplicationCache(mClientID,
rv = cacheService->GetApplicationCache(clientID,
getter_AddRefs(mApplicationCache));
NS_ENSURE_SUCCESS(rv, rv);
@ -1630,8 +1596,7 @@ nsOfflineCacheUpdate::Begin()
mManifestItem = new nsOfflineManifestItem(mManifestURI,
mDocumentURI,
mApplicationCache,
mPreviousApplicationCache,
mClientID);
mPreviousApplicationCache);
if (!mManifestItem) {
return NS_ERROR_OUT_OF_MEMORY;
}
@ -1899,8 +1864,12 @@ nsOfflineCacheUpdate::ScheduleImplicit()
rv = mPreviousApplicationCache->GetClientID(clientID);
NS_ENSURE_SUCCESS(rv, rv);
}
else if (mApplicationCache) {
rv = mApplicationCache->GetClientID(clientID);
NS_ENSURE_SUCCESS(rv, rv);
}
else {
clientID = mClientID;
NS_ERROR("Offline cache update not having set mApplicationCache?");
}
rv = update->InitPartial(mManifestURI, clientID, mDocumentURI);
@ -2138,7 +2107,6 @@ nsOfflineCacheUpdate::AddURI(nsIURI *aURI, PRUint32 aType)
mDocumentURI,
mApplicationCache,
mPreviousApplicationCache,
mClientID,
aType);
if (!item) return NS_ERROR_OUT_OF_MEMORY;

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

@ -57,7 +57,6 @@ public:
nsIURI *aReferrerURI,
nsIApplicationCache *aApplicationCache,
nsIApplicationCache *aPreviousApplicationCache,
const nsACString &aClientID,
PRUint32 aType);
virtual ~nsOfflineCacheUpdateItem();
@ -65,7 +64,6 @@ public:
nsCOMPtr<nsIURI> mReferrerURI;
nsCOMPtr<nsIApplicationCache> mApplicationCache;
nsCOMPtr<nsIApplicationCache> mPreviousApplicationCache;
nsCString mClientID;
nsCString mCacheKey;
PRUint32 mItemType;
@ -96,8 +94,7 @@ public:
nsOfflineManifestItem(nsIURI *aURI,
nsIURI *aReferrerURI,
nsIApplicationCache *aApplicationCache,
nsIApplicationCache *aPreviousApplicationCache,
const nsACString &aClientID);
nsIApplicationCache *aPreviousApplicationCache);
virtual ~nsOfflineManifestItem();
nsCOMArray<nsIURI> &GetExplicitURIs() { return mExplicitURIs; }