2013-09-20 02:11:25 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
2013-10-15 11:08:26 -07:00
|
|
|
#include "CacheLog.h"
|
2013-09-20 02:11:25 -07:00
|
|
|
#include "CacheStorage.h"
|
|
|
|
#include "CacheStorageService.h"
|
|
|
|
#include "CacheEntry.h"
|
2014-02-03 16:53:00 -08:00
|
|
|
#include "CacheObserver.h"
|
2013-09-20 02:11:25 -07:00
|
|
|
|
|
|
|
#include "OldWrappers.h"
|
|
|
|
|
|
|
|
#include "nsICacheEntryDoomCallback.h"
|
|
|
|
|
|
|
|
#include "nsIApplicationCache.h"
|
|
|
|
#include "nsIApplicationCacheService.h"
|
|
|
|
#include "nsIURI.h"
|
|
|
|
#include "nsNetCID.h"
|
|
|
|
#include "nsServiceManagerUtils.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace net {
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS1(CacheStorage, nsICacheStorage)
|
|
|
|
|
|
|
|
CacheStorage::CacheStorage(nsILoadContextInfo* aInfo,
|
|
|
|
bool aAllowDisk,
|
|
|
|
bool aLookupAppCache)
|
|
|
|
: mLoadContextInfo(GetLoadContextInfo(aInfo))
|
|
|
|
, mWriteToDisk(aAllowDisk)
|
|
|
|
, mLookupAppCache(aLookupAppCache)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
CacheStorage::~CacheStorage()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP CacheStorage::AsyncOpenURI(nsIURI *aURI,
|
|
|
|
const nsACString & aIdExtension,
|
|
|
|
uint32_t aFlags,
|
|
|
|
nsICacheEntryOpenCallback *aCallback)
|
|
|
|
{
|
|
|
|
if (!CacheStorageService::Self())
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
|
2014-02-03 16:53:00 -08:00
|
|
|
if (MOZ_UNLIKELY(!CacheObserver::UseDiskCache()) && mWriteToDisk) {
|
|
|
|
aCallback->OnCacheEntryAvailable(nullptr, false, nullptr, NS_ERROR_NOT_AVAILABLE);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (MOZ_UNLIKELY(!CacheObserver::UseMemoryCache()) && !mWriteToDisk) {
|
|
|
|
aCallback->OnCacheEntryAvailable(nullptr, false, nullptr, NS_ERROR_NOT_AVAILABLE);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-09-20 02:11:25 -07:00
|
|
|
NS_ENSURE_ARG(aURI);
|
|
|
|
NS_ENSURE_ARG(aCallback);
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
bool truncate = aFlags & nsICacheStorage::OPEN_TRUNCATE;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> noRefURI;
|
|
|
|
rv = aURI->CloneIgnoringRef(getter_AddRefs(noRefURI));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIApplicationCache> appCache;
|
|
|
|
if (LookupAppCache()) {
|
|
|
|
rv = ChooseApplicationCache(noRefURI, getter_AddRefs(appCache));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (appCache) {
|
|
|
|
nsAutoCString cacheKey;
|
|
|
|
rv = noRefURI->GetAsciiSpec(cacheKey);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2013-11-20 14:20:16 -08:00
|
|
|
nsAutoCString scheme;
|
|
|
|
rv = noRefURI->GetScheme(scheme);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2013-09-20 02:11:25 -07:00
|
|
|
nsRefPtr<_OldCacheLoad> appCacheLoad =
|
2013-11-20 14:20:16 -08:00
|
|
|
new _OldCacheLoad(scheme, cacheKey, aCallback, appCache,
|
2013-09-20 02:11:25 -07:00
|
|
|
LoadInfo(), WriteToDisk(), aFlags);
|
|
|
|
rv = appCacheLoad->Start();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
LOG(("CacheStorage::AsyncOpenURI loading from appcache"));
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-01-06 12:24:41 -08:00
|
|
|
nsRefPtr<CacheEntryHandle> entry;
|
2013-09-20 02:11:25 -07:00
|
|
|
rv = CacheStorageService::Self()->AddStorageEntry(
|
|
|
|
this, noRefURI, aIdExtension,
|
|
|
|
true, // create always
|
|
|
|
truncate, // replace any existing one?
|
|
|
|
getter_AddRefs(entry));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// May invoke the callback synchronously
|
2014-01-06 12:24:41 -08:00
|
|
|
entry->Entry()->AsyncOpen(aCallback, aFlags);
|
2013-09-20 02:11:25 -07:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP CacheStorage::AsyncDoomURI(nsIURI *aURI, const nsACString & aIdExtension,
|
|
|
|
nsICacheEntryDoomCallback* aCallback)
|
|
|
|
{
|
|
|
|
if (!CacheStorageService::Self())
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
|
|
|
|
nsresult rv = CacheStorageService::Self()->DoomStorageEntry(
|
|
|
|
this, aURI, aIdExtension, aCallback);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP CacheStorage::AsyncEvictStorage(nsICacheEntryDoomCallback* aCallback)
|
|
|
|
{
|
|
|
|
if (!CacheStorageService::Self())
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
|
|
|
|
nsresult rv = CacheStorageService::Self()->DoomStorageEntries(
|
|
|
|
this, aCallback);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP CacheStorage::AsyncVisitStorage(nsICacheStorageVisitor* aVisitor,
|
|
|
|
bool aVisitEntries)
|
|
|
|
{
|
|
|
|
LOG(("CacheStorage::AsyncVisitStorage [this=%p, cb=%p, disk=%d]", this, aVisitor, (bool)mWriteToDisk));
|
|
|
|
if (!CacheStorageService::Self())
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
|
|
|
|
nsresult rv = CacheStorageService::Self()->WalkStorageEntries(
|
|
|
|
this, aVisitEntries, aVisitor);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Internal
|
|
|
|
|
|
|
|
nsresult CacheStorage::ChooseApplicationCache(nsIURI* aURI,
|
|
|
|
nsIApplicationCache** aCache)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIApplicationCacheService> appCacheService =
|
|
|
|
do_GetService(NS_APPLICATIONCACHESERVICE_CONTRACTID, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsAutoCString cacheKey;
|
|
|
|
rv = aURI->GetAsciiSpec(cacheKey);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = appCacheService->ChooseApplicationCache(cacheKey, LoadInfo(), aCache);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // net
|
|
|
|
} // mozilla
|