2013-09-20 13:11:25 +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/. */
|
|
|
|
|
2013-10-15 22:08:26 +04:00
|
|
|
#include "CacheLog.h"
|
2013-09-20 13:11:25 +04:00
|
|
|
#include "CacheStorage.h"
|
|
|
|
#include "CacheStorageService.h"
|
|
|
|
#include "CacheEntry.h"
|
2014-02-04 04:53:00 +04:00
|
|
|
#include "CacheObserver.h"
|
2013-09-20 13:11:25 +04: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 {
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(CacheStorage, nsICacheStorage)
|
2013-09-20 13:11:25 +04:00
|
|
|
|
|
|
|
CacheStorage::CacheStorage(nsILoadContextInfo* aInfo,
|
|
|
|
bool aAllowDisk,
|
2015-09-04 02:05:42 +03:00
|
|
|
bool aLookupAppCache,
|
2015-10-22 13:11:00 +03:00
|
|
|
bool aSkipSizeCheck,
|
|
|
|
bool aPinning)
|
2013-09-20 13:11:25 +04:00
|
|
|
: mLoadContextInfo(GetLoadContextInfo(aInfo))
|
|
|
|
, mWriteToDisk(aAllowDisk)
|
|
|
|
, mLookupAppCache(aLookupAppCache)
|
2015-09-04 02:05:42 +03:00
|
|
|
, mSkipSizeCheck(aSkipSizeCheck)
|
2015-10-22 13:11:00 +03:00
|
|
|
, mPinning(aPinning)
|
2013-09-20 13:11:25 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
CacheStorage::~CacheStorage()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP CacheStorage::AsyncOpenURI(nsIURI *aURI,
|
|
|
|
const nsACString & aIdExtension,
|
|
|
|
uint32_t aFlags,
|
|
|
|
nsICacheEntryOpenCallback *aCallback)
|
|
|
|
{
|
|
|
|
if (!CacheStorageService::Self())
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
|
2016-02-02 21:03:20 +03:00
|
|
|
if (MOZ_UNLIKELY(!CacheObserver::UseDiskCache()) && mWriteToDisk &&
|
|
|
|
!(aFlags & OPEN_INTERCEPTED)) {
|
2014-02-04 04:53:00 +04:00
|
|
|
aCallback->OnCacheEntryAvailable(nullptr, false, nullptr, NS_ERROR_NOT_AVAILABLE);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-02-02 21:03:20 +03:00
|
|
|
if (MOZ_UNLIKELY(!CacheObserver::UseMemoryCache()) && !mWriteToDisk &&
|
|
|
|
!(aFlags & OPEN_INTERCEPTED)) {
|
2014-02-04 04:53:00 +04:00
|
|
|
aCallback->OnCacheEntryAvailable(nullptr, false, nullptr, NS_ERROR_NOT_AVAILABLE);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-09-20 13:11:25 +04: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);
|
|
|
|
|
2016-05-12 13:38:00 +03:00
|
|
|
nsAutoCString asciiSpec;
|
|
|
|
rv = noRefURI->GetAsciiSpec(asciiSpec);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2013-09-20 13:11:25 +04:00
|
|
|
nsCOMPtr<nsIApplicationCache> appCache;
|
|
|
|
if (LookupAppCache()) {
|
|
|
|
rv = ChooseApplicationCache(noRefURI, getter_AddRefs(appCache));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2014-11-19 20:46:10 +03:00
|
|
|
|
|
|
|
if (appCache) {
|
|
|
|
// From a chosen appcache open only as readonly
|
|
|
|
aFlags &= ~nsICacheStorage::OPEN_TRUNCATE;
|
|
|
|
}
|
2013-09-20 13:11:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (appCache) {
|
2013-11-21 02:20:16 +04:00
|
|
|
nsAutoCString scheme;
|
|
|
|
rv = noRefURI->GetScheme(scheme);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<_OldCacheLoad> appCacheLoad =
|
2016-05-12 13:38:00 +03:00
|
|
|
new _OldCacheLoad(scheme, asciiSpec, aCallback, appCache,
|
2013-09-20 13:11:25 +04:00
|
|
|
LoadInfo(), WriteToDisk(), aFlags);
|
|
|
|
rv = appCacheLoad->Start();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
LOG(("CacheStorage::AsyncOpenURI loading from appcache"));
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<CacheEntryHandle> entry;
|
2013-09-20 13:11:25 +04:00
|
|
|
rv = CacheStorageService::Self()->AddStorageEntry(
|
2016-05-12 13:38:00 +03:00
|
|
|
this, asciiSpec, aIdExtension,
|
2013-09-20 13:11:25 +04:00
|
|
|
truncate, // replace any existing one?
|
|
|
|
getter_AddRefs(entry));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// May invoke the callback synchronously
|
2014-01-07 00:24:41 +04:00
|
|
|
entry->Entry()->AsyncOpen(aCallback, aFlags);
|
2013-09-20 13:11:25 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-06-24 20:08:35 +04:00
|
|
|
|
2014-07-16 18:16:30 +04:00
|
|
|
NS_IMETHODIMP CacheStorage::OpenTruncate(nsIURI *aURI, const nsACString & aIdExtension,
|
|
|
|
nsICacheEntry **aCacheEntry)
|
|
|
|
{
|
|
|
|
if (!CacheStorageService::Self())
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> noRefURI;
|
|
|
|
rv = aURI->CloneIgnoringRef(getter_AddRefs(noRefURI));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2016-05-12 13:38:00 +03:00
|
|
|
nsAutoCString asciiSpec;
|
|
|
|
rv = noRefURI->GetAsciiSpec(asciiSpec);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2016-01-12 09:31:00 +03:00
|
|
|
RefPtr<CacheEntryHandle> handle;
|
2014-07-16 18:16:30 +04:00
|
|
|
rv = CacheStorageService::Self()->AddStorageEntry(
|
2016-05-12 13:38:00 +03:00
|
|
|
this, asciiSpec, aIdExtension,
|
2014-07-16 18:16:30 +04:00
|
|
|
true, // replace any existing one
|
2016-01-12 09:31:00 +03:00
|
|
|
getter_AddRefs(handle));
|
2014-07-16 18:16:30 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// Just open w/o callback, similar to nsICacheEntry.recreate().
|
2016-01-12 09:31:00 +03:00
|
|
|
handle->Entry()->AsyncOpen(nullptr, OPEN_TRUNCATE);
|
|
|
|
|
|
|
|
// Return a write handler, consumer is supposed to fill in the entry.
|
|
|
|
RefPtr<CacheEntryHandle> writeHandle = handle->Entry()->NewWriteHandle();
|
|
|
|
writeHandle.forget(aCacheEntry);
|
2014-07-16 18:16:30 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-06-24 20:08:35 +04:00
|
|
|
NS_IMETHODIMP CacheStorage::Exists(nsIURI *aURI, const nsACString & aIdExtension,
|
|
|
|
bool *aResult)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG(aURI);
|
|
|
|
NS_ENSURE_ARG(aResult);
|
|
|
|
|
|
|
|
if (!CacheStorageService::Self())
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> noRefURI;
|
|
|
|
rv = aURI->CloneIgnoringRef(getter_AddRefs(noRefURI));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2016-05-12 13:38:00 +03:00
|
|
|
nsAutoCString asciiSpec;
|
|
|
|
rv = noRefURI->GetAsciiSpec(asciiSpec);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2014-06-24 20:08:35 +04:00
|
|
|
return CacheStorageService::Self()->CheckStorageEntry(
|
2016-05-12 13:38:00 +03:00
|
|
|
this, asciiSpec, aIdExtension, aResult);
|
2014-06-24 20:08:35 +04:00
|
|
|
}
|
|
|
|
|
2013-09-20 13:11:25 +04:00
|
|
|
NS_IMETHODIMP CacheStorage::AsyncDoomURI(nsIURI *aURI, const nsACString & aIdExtension,
|
|
|
|
nsICacheEntryDoomCallback* aCallback)
|
|
|
|
{
|
|
|
|
if (!CacheStorageService::Self())
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
|
2016-05-12 13:38:00 +03:00
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> noRefURI;
|
|
|
|
rv = aURI->CloneIgnoringRef(getter_AddRefs(noRefURI));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsAutoCString asciiSpec;
|
|
|
|
rv = noRefURI->GetAsciiSpec(asciiSpec);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = CacheStorageService::Self()->DoomStorageEntry(
|
|
|
|
this, asciiSpec, aIdExtension, aCallback);
|
2013-09-20 13:11:25 +04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace net
|
|
|
|
} // namespace mozilla
|