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"
|
2018-07-23 14:28:47 +03:00
|
|
|
#include "nsNetUtil.h"
|
2013-09-20 13:11:25 +04:00
|
|
|
#include "nsServiceManagerUtils.h"
|
|
|
|
|
2020-01-18 16:48:34 +03:00
|
|
|
namespace mozilla::net {
|
2013-09-20 13:11:25 +04:00
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(CacheStorage, nsICacheStorage)
|
2013-09-20 13:11:25 +04:00
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
CacheStorage::CacheStorage(nsILoadContextInfo* aInfo, bool aAllowDisk,
|
2015-09-04 02:05:42 +03:00
|
|
|
bool aLookupAppCache, bool aSkipSizeCheck,
|
2015-10-22 13:11:00 +03:00
|
|
|
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
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
NS_IMETHODIMP CacheStorage::AsyncOpenURI(nsIURI* aURI,
|
|
|
|
const nsACString& aIdExtension,
|
2013-09-20 13:11:25 +04:00
|
|
|
uint32_t aFlags,
|
2019-05-01 11:47:10 +03:00
|
|
|
nsICacheEntryOpenCallback* aCallback) {
|
2013-09-20 13:11:25 +04:00
|
|
|
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;
|
2018-07-23 14:28:47 +03:00
|
|
|
rv = NS_GetURIWithoutRef(aURI, getter_AddRefs(noRefURI));
|
2013-09-20 13:11:25 +04:00
|
|
|
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, LoadInfo(),
|
2013-09-20 13:11:25 +04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
NS_IMETHODIMP CacheStorage::OpenTruncate(nsIURI* aURI,
|
|
|
|
const nsACString& aIdExtension,
|
|
|
|
nsICacheEntry** aCacheEntry) {
|
2014-07-16 18:16:30 +04:00
|
|
|
if (!CacheStorageService::Self()) return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> noRefURI;
|
2018-07-23 14:28:47 +03:00
|
|
|
rv = NS_GetURIWithoutRef(aURI, getter_AddRefs(noRefURI));
|
2014-07-16 18:16:30 +04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
NS_IMETHODIMP CacheStorage::Exists(nsIURI* aURI, const nsACString& aIdExtension,
|
|
|
|
bool* aResult) {
|
2014-06-24 20:08:35 +04:00
|
|
|
NS_ENSURE_ARG(aURI);
|
|
|
|
NS_ENSURE_ARG(aResult);
|
|
|
|
|
|
|
|
if (!CacheStorageService::Self()) return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> noRefURI;
|
2018-07-23 14:28:47 +03:00
|
|
|
rv = NS_GetURIWithoutRef(aURI, getter_AddRefs(noRefURI));
|
2014-06-24 20:08:35 +04:00
|
|
|
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(this, asciiSpec,
|
2016-05-12 13:38:00 +03:00
|
|
|
aIdExtension, aResult);
|
2014-06-24 20:08:35 +04:00
|
|
|
}
|
|
|
|
|
2017-04-28 12:14:21 +03:00
|
|
|
NS_IMETHODIMP
|
2019-05-01 11:47:10 +03:00
|
|
|
CacheStorage::GetCacheIndexEntryAttrs(nsIURI* aURI,
|
|
|
|
const nsACString& aIdExtension,
|
|
|
|
bool* aHasAltData, uint32_t* aSizeInKB) {
|
2017-04-10 14:22:23 +03:00
|
|
|
NS_ENSURE_ARG(aURI);
|
|
|
|
NS_ENSURE_ARG(aHasAltData);
|
|
|
|
NS_ENSURE_ARG(aSizeInKB);
|
|
|
|
if (!CacheStorageService::Self()) {
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> noRefURI;
|
2018-07-23 14:28:47 +03:00
|
|
|
rv = NS_GetURIWithoutRef(aURI, getter_AddRefs(noRefURI));
|
2017-04-10 14:22:23 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsAutoCString asciiSpec;
|
|
|
|
rv = noRefURI->GetAsciiSpec(asciiSpec);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return CacheStorageService::Self()->GetCacheIndexEntryAttrs(
|
|
|
|
this, asciiSpec, aIdExtension, aHasAltData, aSizeInKB);
|
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
NS_IMETHODIMP CacheStorage::AsyncDoomURI(nsIURI* aURI,
|
|
|
|
const nsACString& aIdExtension,
|
|
|
|
nsICacheEntryDoomCallback* aCallback) {
|
2013-09-20 13:11:25 +04:00
|
|
|
if (!CacheStorageService::Self()) return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
|
2016-05-12 13:38:00 +03:00
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> noRefURI;
|
2018-07-23 14:28:47 +03:00
|
|
|
rv = NS_GetURIWithoutRef(aURI, getter_AddRefs(noRefURI));
|
2016-05-12 13:38:00 +03:00
|
|
|
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(
|
2019-05-01 11:47:10 +03:00
|
|
|
nsICacheEntryDoomCallback* aCallback) {
|
2013-09-20 13:11:25 +04:00
|
|
|
if (!CacheStorageService::Self()) return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
|
|
|
|
nsresult rv =
|
|
|
|
CacheStorageService::Self()->DoomStorageEntries(this, aCallback);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
NS_IMETHODIMP CacheStorage::AsyncVisitStorage(nsICacheStorageVisitor* aVisitor,
|
2013-09-20 13:11:25 +04:00
|
|
|
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
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
nsresult CacheStorage::ChooseApplicationCache(nsIURI* aURI,
|
|
|
|
nsIApplicationCache** aCache) {
|
2013-09-20 13:11:25 +04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-01-18 16:48:34 +03:00
|
|
|
} // namespace mozilla::net
|