2019-07-05 20:05:57 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* 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-09-20 13:11:25 +04:00
|
|
|
// Stuff to link the old imp to the new api - will go away!
|
|
|
|
|
2013-10-15 22:08:26 +04:00
|
|
|
#include "CacheLog.h"
|
2013-09-20 13:11:25 +04:00
|
|
|
#include "OldWrappers.h"
|
|
|
|
#include "CacheStorage.h"
|
|
|
|
#include "CacheStorageService.h"
|
|
|
|
#include "LoadContextInfo.h"
|
2014-05-01 15:28:12 +04:00
|
|
|
#include "nsCacheService.h"
|
2013-09-20 13:11:25 +04:00
|
|
|
|
|
|
|
#include "nsIURI.h"
|
|
|
|
#include "nsICacheSession.h"
|
|
|
|
#include "nsIApplicationCache.h"
|
|
|
|
#include "nsIApplicationCacheService.h"
|
|
|
|
#include "nsIStreamTransportService.h"
|
|
|
|
#include "nsIFile.h"
|
|
|
|
#include "nsICacheEntryDoomCallback.h"
|
|
|
|
#include "nsICacheListener.h"
|
|
|
|
#include "nsICacheStorageVisitor.h"
|
|
|
|
|
|
|
|
#include "nsServiceManagerUtils.h"
|
|
|
|
#include "nsNetCID.h"
|
2014-05-01 15:28:12 +04:00
|
|
|
#include "nsNetUtil.h"
|
2013-09-20 13:11:25 +04:00
|
|
|
#include "nsProxyRelease.h"
|
2016-12-16 06:16:31 +03:00
|
|
|
#include "mozilla/IntegerPrintfMacros.h"
|
2013-09-26 23:37:03 +04:00
|
|
|
#include "mozilla/Telemetry.h"
|
2013-09-20 13:11:25 +04:00
|
|
|
|
|
|
|
static NS_DEFINE_CID(kStreamTransportServiceCID, NS_STREAMTRANSPORTSERVICE_CID);
|
|
|
|
|
2013-11-21 02:20:17 +04:00
|
|
|
static uint32_t const CHECK_MULTITHREADED =
|
|
|
|
nsICacheStorage::CHECK_MULTITHREADED;
|
|
|
|
|
2013-09-20 13:11:25 +04:00
|
|
|
namespace mozilla {
|
|
|
|
namespace net {
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
namespace {
|
2013-09-20 13:11:25 +04:00
|
|
|
|
|
|
|
// Fires the doom callback back on the main thread
|
|
|
|
// after the cache I/O thread is looped.
|
|
|
|
|
2016-04-26 03:23:21 +03:00
|
|
|
class DoomCallbackSynchronizer : public Runnable {
|
2013-09-20 13:11:25 +04:00
|
|
|
public:
|
2019-05-01 11:47:10 +03:00
|
|
|
explicit DoomCallbackSynchronizer(nsICacheEntryDoomCallback* cb)
|
2017-06-12 22:34:10 +03:00
|
|
|
: Runnable("net::DoomCallbackSynchronizer"), mCB(cb) {}
|
2013-09-20 13:11:25 +04:00
|
|
|
nsresult Dispatch();
|
|
|
|
|
|
|
|
private:
|
2018-04-30 19:46:04 +03:00
|
|
|
virtual ~DoomCallbackSynchronizer() = default;
|
2013-09-20 13:11:25 +04:00
|
|
|
|
|
|
|
NS_DECL_NSIRUNNABLE
|
|
|
|
nsCOMPtr<nsICacheEntryDoomCallback> mCB;
|
|
|
|
};
|
|
|
|
|
|
|
|
nsresult DoomCallbackSynchronizer::Dispatch() {
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
nsCOMPtr<nsICacheService> serv =
|
|
|
|
do_GetService(NS_CACHESERVICE_CONTRACTID, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIEventTarget> eventTarget;
|
|
|
|
rv = serv->GetCacheIOTarget(getter_AddRefs(eventTarget));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = eventTarget->Dispatch(this, NS_DISPATCH_NORMAL);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP DoomCallbackSynchronizer::Run() {
|
|
|
|
if (!NS_IsMainThread()) {
|
|
|
|
NS_DispatchToMainThread(this);
|
|
|
|
} else {
|
|
|
|
if (mCB) mCB->OnCacheEntryDoomed(NS_OK);
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Receives doom callback from the old API and forwards to the new API
|
|
|
|
|
|
|
|
class DoomCallbackWrapper : public nsICacheListener {
|
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
|
|
|
NS_DECL_NSICACHELISTENER
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
explicit DoomCallbackWrapper(nsICacheEntryDoomCallback* cb) : mCB(cb) {}
|
2013-09-20 13:11:25 +04:00
|
|
|
|
|
|
|
private:
|
2018-04-30 19:46:04 +03:00
|
|
|
virtual ~DoomCallbackWrapper() = default;
|
2013-09-20 13:11:25 +04:00
|
|
|
|
|
|
|
nsCOMPtr<nsICacheEntryDoomCallback> mCB;
|
|
|
|
};
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(DoomCallbackWrapper, nsICacheListener);
|
2013-09-20 13:11:25 +04:00
|
|
|
|
|
|
|
NS_IMETHODIMP DoomCallbackWrapper::OnCacheEntryAvailable(
|
2019-05-01 11:47:10 +03:00
|
|
|
nsICacheEntryDescriptor* descriptor, nsCacheAccessMode accessGranted,
|
2013-09-20 13:11:25 +04:00
|
|
|
nsresult status) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP DoomCallbackWrapper::OnCacheEntryDoomed(nsresult status) {
|
|
|
|
if (!mCB) return NS_ERROR_NULL_POINTER;
|
|
|
|
|
|
|
|
mCB->OnCacheEntryDoomed(status);
|
|
|
|
mCB = nullptr;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace
|
2014-05-01 15:28:12 +04:00
|
|
|
|
|
|
|
// _OldVisitCallbackWrapper
|
2013-09-20 13:11:25 +04:00
|
|
|
// Receives visit callbacks from the old API and forwards it to the new API
|
|
|
|
|
2014-05-01 15:28:12 +04:00
|
|
|
NS_IMPL_ISUPPORTS(_OldVisitCallbackWrapper, nsICacheVisitor)
|
|
|
|
|
|
|
|
_OldVisitCallbackWrapper::~_OldVisitCallbackWrapper() {
|
|
|
|
if (!mHit) {
|
|
|
|
// The device has not been found, to not break the chain, simulate
|
|
|
|
// storage info callback.
|
|
|
|
mCB->OnCacheStorageInfo(0, 0, 0, nullptr);
|
2013-09-20 13:11:25 +04:00
|
|
|
}
|
|
|
|
|
2014-05-01 15:28:12 +04:00
|
|
|
if (mVisitEntries) {
|
2013-09-20 13:11:25 +04:00
|
|
|
mCB->OnCacheEntryVisitCompleted();
|
2014-05-01 15:28:12 +04:00
|
|
|
}
|
2013-09-20 13:11:25 +04:00
|
|
|
}
|
|
|
|
|
2014-05-01 15:28:12 +04:00
|
|
|
NS_IMETHODIMP _OldVisitCallbackWrapper::VisitDevice(
|
2019-05-01 11:47:10 +03:00
|
|
|
const char* deviceID, nsICacheDeviceInfo* deviceInfo, bool* _retval) {
|
2013-09-20 13:11:25 +04:00
|
|
|
if (!mCB) return NS_ERROR_NULL_POINTER;
|
|
|
|
|
|
|
|
*_retval = false;
|
|
|
|
if (strcmp(deviceID, mDeviceID)) {
|
|
|
|
// Not the device we want to visit
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-05-01 15:28:12 +04:00
|
|
|
mHit = true;
|
|
|
|
|
2013-09-20 13:11:25 +04:00
|
|
|
nsresult rv;
|
|
|
|
|
2014-05-01 15:28:12 +04:00
|
|
|
uint32_t capacity;
|
|
|
|
rv = deviceInfo->GetMaximumSize(&capacity);
|
2013-09-20 13:11:25 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2014-05-01 15:28:12 +04:00
|
|
|
nsCOMPtr<nsIFile> dir;
|
|
|
|
if (!strcmp(mDeviceID, "disk")) {
|
|
|
|
nsCacheService::GetDiskCacheDirectory(getter_AddRefs(dir));
|
|
|
|
} else if (!strcmp(mDeviceID, "offline")) {
|
|
|
|
nsCacheService::GetAppCacheDirectory(getter_AddRefs(dir));
|
|
|
|
}
|
2014-04-30 14:39:18 +04:00
|
|
|
|
2014-05-01 15:28:12 +04:00
|
|
|
if (mLoadInfo->IsAnonymous()) {
|
|
|
|
// Anonymous visiting reports 0, 0 since we cannot count that
|
|
|
|
// early the number of anon entries.
|
|
|
|
mCB->OnCacheStorageInfo(0, 0, capacity, dir);
|
|
|
|
} else {
|
|
|
|
// Non-anon visitor counts all non-anon + ALL ANON entries,
|
|
|
|
// there is no way to determine the number of entries when
|
|
|
|
// using the old cache APIs - there is no concept of anonymous
|
|
|
|
// storage.
|
|
|
|
uint32_t entryCount;
|
|
|
|
rv = deviceInfo->GetEntryCount(&entryCount);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2014-04-30 15:05:43 +04:00
|
|
|
|
2014-05-01 15:28:12 +04:00
|
|
|
uint32_t totalSize;
|
|
|
|
rv = deviceInfo->GetTotalSize(&totalSize);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
mCB->OnCacheStorageInfo(entryCount, totalSize, capacity, dir);
|
|
|
|
}
|
|
|
|
|
|
|
|
*_retval = mVisitEntries;
|
2013-09-20 13:11:25 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
NS_IMETHODIMP _OldVisitCallbackWrapper::VisitEntry(const char* deviceID,
|
|
|
|
nsICacheEntryInfo* entryInfo,
|
|
|
|
bool* _retval) {
|
2013-09-20 13:11:25 +04:00
|
|
|
MOZ_ASSERT(!strcmp(deviceID, mDeviceID));
|
|
|
|
|
2014-05-01 15:28:12 +04:00
|
|
|
nsresult rv;
|
2014-04-30 15:05:43 +04:00
|
|
|
|
2014-05-01 15:28:12 +04:00
|
|
|
*_retval = true;
|
2013-09-20 13:11:25 +04:00
|
|
|
|
2014-05-01 15:28:12 +04:00
|
|
|
// Read all informative properties from the entry.
|
2017-10-29 14:19:48 +03:00
|
|
|
nsAutoCString clientId;
|
|
|
|
rv = entryInfo->GetClientID(clientId);
|
2014-05-01 15:28:12 +04:00
|
|
|
if (NS_FAILED(rv)) return NS_OK;
|
|
|
|
|
|
|
|
if (mLoadInfo->IsPrivate() !=
|
|
|
|
StringBeginsWith(clientId, NS_LITERAL_CSTRING("HTTP-memory-only-PB"))) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoCString cacheKey, enhanceId;
|
|
|
|
rv = entryInfo->GetKey(cacheKey);
|
|
|
|
if (NS_FAILED(rv)) return NS_OK;
|
|
|
|
|
|
|
|
if (StringBeginsWith(cacheKey, NS_LITERAL_CSTRING("anon&"))) {
|
|
|
|
if (!mLoadInfo->IsAnonymous()) return NS_OK;
|
|
|
|
|
|
|
|
cacheKey = Substring(cacheKey, 5, cacheKey.Length());
|
|
|
|
} else if (mLoadInfo->IsAnonymous()) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (StringBeginsWith(cacheKey, NS_LITERAL_CSTRING("id="))) {
|
|
|
|
int32_t uriSpecEnd = cacheKey.Find("&uri=");
|
|
|
|
if (uriSpecEnd == kNotFound) // Corrupted, ignore
|
|
|
|
return NS_OK;
|
2014-04-30 15:05:43 +04:00
|
|
|
|
2014-05-01 15:28:12 +04:00
|
|
|
enhanceId = Substring(cacheKey, 3, uriSpecEnd - 3);
|
|
|
|
cacheKey = Substring(cacheKey, uriSpecEnd + 1, cacheKey.Length());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (StringBeginsWith(cacheKey, NS_LITERAL_CSTRING("uri="))) {
|
|
|
|
cacheKey = Substring(cacheKey, 4, cacheKey.Length());
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
// cacheKey is strip of any prefixes
|
|
|
|
rv = NS_NewURI(getter_AddRefs(uri), cacheKey);
|
|
|
|
if (NS_FAILED(rv)) return NS_OK;
|
|
|
|
|
|
|
|
uint32_t dataSize;
|
|
|
|
if (NS_FAILED(entryInfo->GetDataSize(&dataSize))) dataSize = 0;
|
|
|
|
int32_t fetchCount;
|
|
|
|
if (NS_FAILED(entryInfo->GetFetchCount(&fetchCount))) fetchCount = 0;
|
|
|
|
uint32_t expirationTime;
|
|
|
|
if (NS_FAILED(entryInfo->GetExpirationTime(&expirationTime)))
|
|
|
|
expirationTime = 0;
|
|
|
|
uint32_t lastModified;
|
|
|
|
if (NS_FAILED(entryInfo->GetLastModified(&lastModified))) lastModified = 0;
|
|
|
|
|
|
|
|
// Send them to the consumer.
|
2017-02-03 06:14:00 +03:00
|
|
|
rv = mCB->OnCacheEntryInfo(uri, enhanceId, (int64_t)dataSize, fetchCount,
|
|
|
|
lastModified, expirationTime, false, mLoadInfo);
|
2014-05-01 15:28:12 +04:00
|
|
|
|
|
|
|
*_retval = NS_SUCCEEDED(rv);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2014-04-30 15:05:43 +04:00
|
|
|
|
2014-04-24 20:50:46 +04:00
|
|
|
// _OldGetDiskConsumption
|
|
|
|
|
|
|
|
// static
|
|
|
|
nsresult _OldGetDiskConsumption::Get(
|
2019-05-01 11:47:10 +03:00
|
|
|
nsICacheStorageConsumptionObserver* aCallback) {
|
2014-04-24 20:50:46 +04:00
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
nsCOMPtr<nsICacheService> serv =
|
|
|
|
do_GetService(NS_CACHESERVICE_CONTRACTID, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<_OldGetDiskConsumption> cb = new _OldGetDiskConsumption(aCallback);
|
2014-04-24 20:50:46 +04:00
|
|
|
|
|
|
|
// _OldGetDiskConsumption stores the found size value, but until dispatched
|
|
|
|
// to the main thread it doesn't call on the consupmtion observer. See bellow.
|
|
|
|
rv = serv->VisitEntries(cb);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// We are called from CacheStorageService::AsyncGetDiskConsumption whose IDL
|
|
|
|
// documentation claims the callback is always delievered asynchronously
|
|
|
|
// back to the main thread. Despite we know the result synchronosusly when
|
|
|
|
// querying the old cache, we need to stand the word and dispatch the result
|
|
|
|
// to the main thread asynchronously. Hence the dispatch here.
|
|
|
|
return NS_DispatchToMainThread(cb);
|
|
|
|
}
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS_INHERITED(_OldGetDiskConsumption, Runnable, nsICacheVisitor)
|
2014-04-24 20:50:46 +04:00
|
|
|
|
|
|
|
_OldGetDiskConsumption::_OldGetDiskConsumption(
|
2019-05-01 11:47:10 +03:00
|
|
|
nsICacheStorageConsumptionObserver* aCallback)
|
2017-06-12 22:34:10 +03:00
|
|
|
: Runnable("net::_OldGetDiskConsumption"), mCallback(aCallback), mSize(0) {}
|
2014-04-24 20:50:46 +04:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
_OldGetDiskConsumption::Run() {
|
|
|
|
mCallback->OnNetworkCacheDiskConsumption(mSize);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2019-05-01 11:47:10 +03:00
|
|
|
_OldGetDiskConsumption::VisitDevice(const char* deviceID,
|
|
|
|
nsICacheDeviceInfo* deviceInfo,
|
|
|
|
bool* _retval) {
|
2014-04-24 20:50:46 +04:00
|
|
|
if (!strcmp(deviceID, "disk")) {
|
|
|
|
uint32_t size;
|
|
|
|
nsresult rv = deviceInfo->GetTotalSize(&size);
|
|
|
|
if (NS_SUCCEEDED(rv)) mSize = (int64_t)size;
|
|
|
|
}
|
|
|
|
|
|
|
|
*_retval = false;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2019-05-01 11:47:10 +03:00
|
|
|
_OldGetDiskConsumption::VisitEntry(const char* deviceID,
|
|
|
|
nsICacheEntryInfo* entryInfo,
|
|
|
|
bool* _retval) {
|
2014-04-24 20:50:46 +04:00
|
|
|
MOZ_CRASH("Unexpected");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-09-20 13:11:25 +04:00
|
|
|
// _OldCacheEntryWrapper
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
_OldCacheEntryWrapper::_OldCacheEntryWrapper(nsICacheEntryDescriptor* desc)
|
2017-10-10 11:09:00 +03:00
|
|
|
: mOldDesc(desc), mOldInfo(desc), mCacheEntryId(CacheEntry::GetNextId()) {
|
2013-09-20 13:11:25 +04:00
|
|
|
LOG(("Creating _OldCacheEntryWrapper %p for descriptor %p", this, desc));
|
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
_OldCacheEntryWrapper::_OldCacheEntryWrapper(nsICacheEntryInfo* info)
|
2017-10-10 11:09:00 +03:00
|
|
|
: mOldDesc(nullptr),
|
|
|
|
mOldInfo(info),
|
|
|
|
mCacheEntryId(CacheEntry::GetNextId()) {
|
2013-09-20 13:11:25 +04:00
|
|
|
LOG(("Creating _OldCacheEntryWrapper %p for info %p", this, info));
|
|
|
|
}
|
|
|
|
|
|
|
|
_OldCacheEntryWrapper::~_OldCacheEntryWrapper() {
|
|
|
|
LOG(("Destroying _OldCacheEntryWrapper %p for descriptor %p", this,
|
|
|
|
mOldInfo.get()));
|
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
NS_IMETHODIMP _OldCacheEntryWrapper::GetIsForcedValid(bool* aIsForcedValid) {
|
2014-07-21 22:34:17 +04:00
|
|
|
// Unused stub
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP _OldCacheEntryWrapper::ForceValidFor(
|
|
|
|
uint32_t aSecondsToTheFuture) {
|
|
|
|
// Unused stub
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(_OldCacheEntryWrapper, nsICacheEntry)
|
2013-09-20 13:11:25 +04:00
|
|
|
|
|
|
|
NS_IMETHODIMP _OldCacheEntryWrapper::AsyncDoom(
|
2019-05-01 11:47:10 +03:00
|
|
|
nsICacheEntryDoomCallback* listener) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<DoomCallbackWrapper> cb =
|
2013-09-20 13:11:25 +04:00
|
|
|
listener ? new DoomCallbackWrapper(listener) : nullptr;
|
|
|
|
return AsyncDoom(cb);
|
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
NS_IMETHODIMP _OldCacheEntryWrapper::GetDataSize(int64_t* aSize) {
|
2013-09-20 13:11:25 +04:00
|
|
|
uint32_t size;
|
|
|
|
nsresult rv = GetDataSize(&size);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
*aSize = size;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
NS_IMETHODIMP _OldCacheEntryWrapper::GetAltDataSize(int64_t* aSize) {
|
2016-09-27 11:26:00 +03:00
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
NS_IMETHODIMP _OldCacheEntryWrapper::GetAltDataType(nsACString& aType) {
|
Bug 1487100 - Allow calling nsICacheInfoChannel.preferAlternativeDataType(altDataType, contentType) multiple times r=michal,luke
This patch changes the way we set and handle the preferred alternate data type.
It is no longer just one choice, but a set of preferences, each conditional
on the contentType of the resource.
For example:
var cc = chan.QueryInterface(Ci.nsICacheInfoChannel);
cc.preferAlternativeDataType("js-bytecode", "text/javascript");
cc.preferAlternativeDataType("ammended-text", "text/plain");
cc.preferAlternativeDataType("something-else", "");
When loaded from the cache, the available alt-data type will be checked against
"js-bytecode" if the contentType is "text/javascript", "ammended-text" if the contentType is "text/plain" or "something-else" for all contentTypes.
Note that the alt-data type could be "something-else" even if the contentType is "text/javascript".
The preferences are saved as an nsTArray<mozilla::Tuple<nsCString, nsCString>>.
Differential Revision: https://phabricator.services.mozilla.com/D8071
--HG--
extra : rebase_source : eb4961f05a52e557e7d2d986d59e0a2cf18a3447
extra : source : dd1c31ea78c2b15d14750d137037a54d50719997
2018-10-17 16:58:30 +03:00
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
NS_IMETHODIMP _OldCacheEntryWrapper::GetPersistent(bool* aPersistToDisk) {
|
2013-09-20 13:11:25 +04:00
|
|
|
if (!mOldDesc) {
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
nsCacheStoragePolicy policy;
|
|
|
|
rv = mOldDesc->GetStoragePolicy(&policy);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
*aPersistToDisk = policy != nsICache::STORE_IN_MEMORY;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-02-04 04:52:59 +04:00
|
|
|
NS_IMETHODIMP _OldCacheEntryWrapper::Recreate(bool aMemoryOnly,
|
2019-05-01 11:47:10 +03:00
|
|
|
nsICacheEntry** aResult) {
|
2013-09-20 13:11:25 +04:00
|
|
|
NS_ENSURE_TRUE(mOldDesc, NS_ERROR_NOT_AVAILABLE);
|
|
|
|
|
|
|
|
nsCacheAccessMode mode;
|
|
|
|
nsresult rv = mOldDesc->GetAccessGranted(&mode);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (!(mode & nsICache::ACCESS_WRITE)) return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
|
|
|
|
LOG(("_OldCacheEntryWrapper::Recreate [this=%p]", this));
|
|
|
|
|
2014-02-04 04:52:59 +04:00
|
|
|
if (aMemoryOnly) mOldDesc->SetStoragePolicy(nsICache::STORE_IN_MEMORY);
|
|
|
|
|
2013-09-20 13:11:25 +04:00
|
|
|
nsCOMPtr<nsICacheEntry> self(this);
|
|
|
|
self.forget(aResult);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP _OldCacheEntryWrapper::OpenInputStream(int64_t offset,
|
2019-05-01 11:47:10 +03:00
|
|
|
nsIInputStream** _retval) {
|
2013-09-20 13:11:25 +04:00
|
|
|
if (offset > PR_UINT32_MAX) return NS_ERROR_INVALID_ARG;
|
|
|
|
|
|
|
|
return OpenInputStream(uint32_t(offset), _retval);
|
|
|
|
}
|
|
|
|
NS_IMETHODIMP _OldCacheEntryWrapper::OpenOutputStream(
|
2019-05-01 11:47:10 +03:00
|
|
|
int64_t offset, int64_t predictedSize, nsIOutputStream** _retval) {
|
2013-09-20 13:11:25 +04:00
|
|
|
if (offset > PR_UINT32_MAX) return NS_ERROR_INVALID_ARG;
|
|
|
|
|
|
|
|
return OpenOutputStream(uint32_t(offset), _retval);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP _OldCacheEntryWrapper::MaybeMarkValid() {
|
|
|
|
LOG(("_OldCacheEntryWrapper::MaybeMarkValid [this=%p]", this));
|
|
|
|
|
|
|
|
NS_ENSURE_TRUE(mOldDesc, NS_ERROR_NULL_POINTER);
|
|
|
|
|
|
|
|
nsCacheAccessMode mode;
|
|
|
|
nsresult rv = mOldDesc->GetAccessGranted(&mode);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (mode & nsICache::ACCESS_WRITE) {
|
|
|
|
LOG(("Marking cache entry valid [entry=%p, descr=%p]", this, mOldDesc));
|
|
|
|
return mOldDesc->MarkValid();
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG(("Not marking read-only cache entry valid [entry=%p, descr=%p]", this,
|
|
|
|
mOldDesc));
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP _OldCacheEntryWrapper::HasWriteAccess(bool aWriteAllowed_unused,
|
2019-05-01 11:47:10 +03:00
|
|
|
bool* aWriteAccess) {
|
2013-09-20 13:11:25 +04:00
|
|
|
NS_ENSURE_TRUE(mOldDesc, NS_ERROR_NULL_POINTER);
|
|
|
|
NS_ENSURE_ARG(aWriteAccess);
|
|
|
|
|
|
|
|
nsCacheAccessMode mode;
|
|
|
|
nsresult rv = mOldDesc->GetAccessGranted(&mode);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
*aWriteAccess = !!(mode & nsICache::ACCESS_WRITE);
|
|
|
|
|
|
|
|
LOG(("_OldCacheEntryWrapper::HasWriteAccess [this=%p, write-access=%d]", this,
|
|
|
|
*aWriteAccess));
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
namespace {
|
2014-05-01 15:28:12 +04:00
|
|
|
|
|
|
|
class MetaDataVisitorWrapper : public nsICacheMetaDataVisitor {
|
2018-04-30 19:46:04 +03:00
|
|
|
virtual ~MetaDataVisitorWrapper() = default;
|
2014-06-24 20:36:44 +04:00
|
|
|
|
2014-05-01 15:28:12 +04:00
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSICACHEMETADATAVISITOR
|
2019-05-01 11:47:10 +03:00
|
|
|
explicit MetaDataVisitorWrapper(nsICacheEntryMetaDataVisitor* cb) : mCB(cb) {}
|
2014-05-01 15:28:12 +04:00
|
|
|
nsCOMPtr<nsICacheEntryMetaDataVisitor> mCB;
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS(MetaDataVisitorWrapper, nsICacheMetaDataVisitor)
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2019-05-01 11:47:10 +03:00
|
|
|
MetaDataVisitorWrapper::VisitMetaDataElement(char const* key, char const* value,
|
|
|
|
bool* goon) {
|
2014-05-01 15:28:12 +04:00
|
|
|
*goon = true;
|
|
|
|
return mCB->OnMetaDataElement(key, value);
|
|
|
|
}
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace
|
2014-05-01 15:28:12 +04:00
|
|
|
|
|
|
|
NS_IMETHODIMP _OldCacheEntryWrapper::VisitMetaData(
|
2019-05-01 11:47:10 +03:00
|
|
|
nsICacheEntryMetaDataVisitor* cb) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<MetaDataVisitorWrapper> w = new MetaDataVisitorWrapper(cb);
|
2014-05-01 15:28:12 +04:00
|
|
|
return mOldDesc->VisitMetaData(w);
|
|
|
|
}
|
2013-09-20 13:11:25 +04:00
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
namespace {
|
2013-09-20 13:11:25 +04:00
|
|
|
|
2019-12-02 16:21:06 +03:00
|
|
|
void GetCacheSessionNameForStoragePolicy(const nsACString& scheme,
|
|
|
|
nsCacheStoragePolicy storagePolicy,
|
|
|
|
bool isPrivate,
|
|
|
|
OriginAttributes const* originAttribs,
|
|
|
|
nsACString& sessionName) {
|
2013-09-20 13:11:25 +04:00
|
|
|
MOZ_ASSERT(!isPrivate || storagePolicy == nsICache::STORE_IN_MEMORY);
|
|
|
|
|
2013-11-21 02:20:16 +04:00
|
|
|
// HTTP
|
2014-05-22 07:48:51 +04:00
|
|
|
if (scheme.EqualsLiteral("http") || scheme.EqualsLiteral("https")) {
|
2013-11-21 02:20:16 +04:00
|
|
|
switch (storagePolicy) {
|
2013-09-20 13:11:25 +04:00
|
|
|
case nsICache::STORE_IN_MEMORY:
|
2013-11-21 02:20:16 +04:00
|
|
|
if (isPrivate)
|
2014-05-22 07:48:52 +04:00
|
|
|
sessionName.AssignLiteral("HTTP-memory-only-PB");
|
2013-11-21 02:20:16 +04:00
|
|
|
else
|
2014-05-22 07:48:52 +04:00
|
|
|
sessionName.AssignLiteral("HTTP-memory-only");
|
2013-09-20 13:11:25 +04:00
|
|
|
break;
|
|
|
|
case nsICache::STORE_OFFLINE:
|
2013-11-21 02:20:16 +04:00
|
|
|
// XXX This is actually never used, only added to prevent
|
|
|
|
// any compatibility damage.
|
2014-05-22 07:48:52 +04:00
|
|
|
sessionName.AssignLiteral("HTTP-offline");
|
2013-09-20 13:11:25 +04:00
|
|
|
break;
|
|
|
|
default:
|
2014-05-22 07:48:52 +04:00
|
|
|
sessionName.AssignLiteral("HTTP");
|
2013-09-20 13:11:25 +04:00
|
|
|
break;
|
2013-11-21 02:20:16 +04:00
|
|
|
}
|
2013-09-20 13:11:25 +04:00
|
|
|
}
|
2013-11-21 02:20:16 +04:00
|
|
|
// FTP
|
2014-05-22 07:48:51 +04:00
|
|
|
else if (scheme.EqualsLiteral("ftp")) {
|
2013-11-21 02:20:16 +04:00
|
|
|
if (isPrivate)
|
2014-05-22 07:48:52 +04:00
|
|
|
sessionName.AssignLiteral("FTP-private");
|
2013-11-21 02:20:16 +04:00
|
|
|
else
|
2014-05-22 07:48:52 +04:00
|
|
|
sessionName.AssignLiteral("FTP");
|
2013-11-21 02:20:16 +04:00
|
|
|
}
|
|
|
|
// all remaining URL scheme
|
|
|
|
else {
|
|
|
|
// Since with the new API a consumer cannot specify its own session name
|
|
|
|
// and partitioning of the cache is handled stricly only by the cache
|
|
|
|
// back-end internally, we will use a separate session name to pretend
|
|
|
|
// functionality of the new API wrapping the Darin's cache for all other
|
|
|
|
// URL schemes.
|
|
|
|
// Deliberately omitting |anonymous| since other session types don't
|
|
|
|
// recognize it too.
|
2014-05-22 07:48:52 +04:00
|
|
|
sessionName.AssignLiteral("other");
|
2014-05-22 07:48:50 +04:00
|
|
|
if (isPrivate) sessionName.AppendLiteral("-private");
|
2013-11-21 02:20:16 +04:00
|
|
|
}
|
|
|
|
|
2015-10-11 19:13:09 +03:00
|
|
|
nsAutoCString suffix;
|
|
|
|
originAttribs->CreateSuffix(suffix);
|
|
|
|
sessionName.Append(suffix);
|
2013-09-20 13:11:25 +04:00
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
nsresult GetCacheSession(const nsACString& aScheme, bool aWriteToDisk,
|
|
|
|
nsILoadContextInfo* aLoadInfo,
|
|
|
|
nsIApplicationCache* aAppCache,
|
|
|
|
nsICacheSession** _result) {
|
2013-09-20 13:11:25 +04:00
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
nsCacheStoragePolicy storagePolicy;
|
|
|
|
if (aAppCache)
|
|
|
|
storagePolicy = nsICache::STORE_OFFLINE;
|
|
|
|
else if (!aWriteToDisk || aLoadInfo->IsPrivate())
|
|
|
|
storagePolicy = nsICache::STORE_IN_MEMORY;
|
|
|
|
else
|
|
|
|
storagePolicy = nsICache::STORE_ANYWHERE;
|
|
|
|
|
|
|
|
nsAutoCString clientId;
|
|
|
|
if (aAppCache) {
|
|
|
|
aAppCache->GetClientID(clientId);
|
|
|
|
} else {
|
2019-12-02 16:21:06 +03:00
|
|
|
GetCacheSessionNameForStoragePolicy(
|
2013-09-20 13:11:25 +04:00
|
|
|
aScheme, storagePolicy, aLoadInfo->IsPrivate(),
|
2015-10-11 19:13:09 +03:00
|
|
|
aLoadInfo->OriginAttributesPtr(), clientId);
|
2013-09-20 13:11:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
LOG((" GetCacheSession for client=%s, policy=%d", clientId.get(),
|
|
|
|
storagePolicy));
|
|
|
|
|
|
|
|
nsCOMPtr<nsICacheService> serv =
|
|
|
|
do_GetService(NS_CACHESERVICE_CONTRACTID, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsCOMPtr<nsICacheSession> session;
|
2014-06-05 22:14:07 +04:00
|
|
|
rv = nsCacheService::GlobalInstance()->CreateSessionInternal(
|
|
|
|
clientId.get(), storagePolicy, nsICache::STREAM_BASED,
|
|
|
|
getter_AddRefs(session));
|
2013-09-20 13:11:25 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = session->SetIsPrivate(aLoadInfo->IsPrivate());
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = session->SetDoomEntriesIfExpired(false);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (aAppCache) {
|
|
|
|
nsCOMPtr<nsIFile> profileDirectory;
|
|
|
|
aAppCache->GetProfileDirectory(getter_AddRefs(profileDirectory));
|
|
|
|
if (profileDirectory) rv = session->SetProfileDirectory(profileDirectory);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
session.forget(_result);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace
|
2013-09-20 13:11:25 +04:00
|
|
|
|
2016-04-26 03:23:21 +03:00
|
|
|
NS_IMPL_ISUPPORTS_INHERITED(_OldCacheLoad, Runnable, nsICacheListener)
|
2013-09-20 13:11:25 +04:00
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
_OldCacheLoad::_OldCacheLoad(const nsACString& aScheme,
|
|
|
|
const nsACString& aCacheKey,
|
|
|
|
nsICacheEntryOpenCallback* aCallback,
|
|
|
|
nsIApplicationCache* aAppCache,
|
|
|
|
nsILoadContextInfo* aLoadInfo, bool aWriteToDisk,
|
2013-09-20 13:11:25 +04:00
|
|
|
uint32_t aFlags)
|
2017-06-12 22:34:10 +03:00
|
|
|
: Runnable("net::_OldCacheLoad"),
|
|
|
|
mScheme(aScheme),
|
2013-11-21 02:20:16 +04:00
|
|
|
mCacheKey(aCacheKey),
|
2013-09-20 13:11:25 +04:00
|
|
|
mCallback(aCallback),
|
|
|
|
mLoadInfo(GetLoadContextInfo(aLoadInfo)),
|
|
|
|
mFlags(aFlags),
|
|
|
|
mWriteToDisk(aWriteToDisk),
|
|
|
|
mNew(true),
|
2013-11-21 02:20:17 +04:00
|
|
|
mOpening(true),
|
|
|
|
mSync(false),
|
2013-09-20 13:11:25 +04:00
|
|
|
mStatus(NS_ERROR_UNEXPECTED),
|
|
|
|
mRunCount(0),
|
|
|
|
mAppCache(aAppCache) {}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2013-09-20 13:11:25 +04:00
|
|
|
_OldCacheLoad::~_OldCacheLoad() {
|
2017-06-14 04:27:17 +03:00
|
|
|
ProxyReleaseMainThread("_OldCacheLoad::mAppCache", mAppCache);
|
2013-09-20 13:11:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult _OldCacheLoad::Start() {
|
|
|
|
LOG(("_OldCacheLoad::Start [this=%p, key=%s]", this, mCacheKey.get()));
|
|
|
|
|
2013-09-26 23:37:03 +04:00
|
|
|
mLoadStart = mozilla::TimeStamp::Now();
|
|
|
|
|
2013-09-20 13:11:25 +04:00
|
|
|
nsresult rv;
|
|
|
|
|
2013-11-21 02:20:17 +04:00
|
|
|
// Consumers that can invoke this code as first and off the main thread
|
|
|
|
// are responsible for initiating these two services on the main thread.
|
2019-02-28 04:09:48 +03:00
|
|
|
// Currently no one does that.
|
2013-11-21 02:20:17 +04:00
|
|
|
|
2013-09-20 13:11:25 +04:00
|
|
|
// XXX: Start the cache service; otherwise DispatchToCacheIOThread will
|
|
|
|
// fail.
|
|
|
|
nsCOMPtr<nsICacheService> service =
|
|
|
|
do_GetService(NS_CACHESERVICE_CONTRACTID, &rv);
|
|
|
|
|
|
|
|
// Ensure the stream transport service gets initialized on the main thread
|
2013-11-21 02:20:17 +04:00
|
|
|
if (NS_SUCCEEDED(rv) && NS_IsMainThread()) {
|
2013-09-20 13:11:25 +04:00
|
|
|
nsCOMPtr<nsIStreamTransportService> sts =
|
|
|
|
do_GetService(kStreamTransportServiceCID, &rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
rv = service->GetCacheIOTarget(getter_AddRefs(mCacheThread));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2013-11-21 02:20:17 +04:00
|
|
|
bool onCacheTarget;
|
|
|
|
rv = mCacheThread->IsOnCurrentThread(&onCacheTarget);
|
|
|
|
if (NS_SUCCEEDED(rv) && onCacheTarget) {
|
|
|
|
mSync = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
if (mSync) {
|
|
|
|
rv = Run();
|
|
|
|
} else {
|
|
|
|
rv = mCacheThread->Dispatch(this, NS_DISPATCH_NORMAL);
|
|
|
|
}
|
2013-09-20 13:11:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
_OldCacheLoad::Run() {
|
|
|
|
LOG(("_OldCacheLoad::Run [this=%p, key=%s, cb=%p]", this, mCacheKey.get(),
|
|
|
|
mCallback.get()));
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
|
2013-11-21 02:20:17 +04:00
|
|
|
if (mOpening) {
|
|
|
|
mOpening = false;
|
2013-09-20 13:11:25 +04:00
|
|
|
nsCOMPtr<nsICacheSession> session;
|
2013-11-21 02:20:16 +04:00
|
|
|
rv = GetCacheSession(mScheme, mWriteToDisk, mLoadInfo, mAppCache,
|
|
|
|
getter_AddRefs(session));
|
2013-09-20 13:11:25 +04:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
// AsyncOpenCacheEntry isn't really async when its called on the
|
|
|
|
// cache service thread.
|
|
|
|
|
|
|
|
nsCacheAccessMode cacheAccess;
|
|
|
|
if (mFlags & nsICacheStorage::OPEN_TRUNCATE)
|
|
|
|
cacheAccess = nsICache::ACCESS_WRITE;
|
|
|
|
else if ((mFlags & nsICacheStorage::OPEN_READONLY) || mAppCache)
|
|
|
|
cacheAccess = nsICache::ACCESS_READ;
|
|
|
|
else
|
|
|
|
cacheAccess = nsICache::ACCESS_READ_WRITE;
|
|
|
|
|
|
|
|
LOG((" session->AsyncOpenCacheEntry with access=%d", cacheAccess));
|
|
|
|
|
|
|
|
bool bypassBusy = mFlags & nsICacheStorage::OPEN_BYPASS_IF_BUSY;
|
|
|
|
|
2013-11-21 02:20:17 +04:00
|
|
|
if (mSync && cacheAccess == nsICache::ACCESS_WRITE) {
|
|
|
|
nsCOMPtr<nsICacheEntryDescriptor> entry;
|
|
|
|
rv = session->OpenCacheEntry(mCacheKey, cacheAccess, bypassBusy,
|
|
|
|
getter_AddRefs(entry));
|
|
|
|
|
|
|
|
nsCacheAccessMode grantedAccess = 0;
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
entry->GetAccessGranted(&grantedAccess);
|
|
|
|
}
|
|
|
|
|
|
|
|
return OnCacheEntryAvailable(entry, grantedAccess, rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = session->AsyncOpenCacheEntry(mCacheKey, cacheAccess, this,
|
|
|
|
bypassBusy);
|
2013-09-20 13:11:25 +04:00
|
|
|
if (NS_SUCCEEDED(rv)) return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Opening failed, propagate the error to the consumer
|
2016-12-16 06:16:31 +03:00
|
|
|
LOG((" Opening cache entry failed with rv=0x%08" PRIx32,
|
|
|
|
static_cast<uint32_t>(rv)));
|
2013-09-20 13:11:25 +04:00
|
|
|
mStatus = rv;
|
|
|
|
mNew = false;
|
|
|
|
NS_DispatchToMainThread(this);
|
|
|
|
} else {
|
|
|
|
if (!mCallback) {
|
|
|
|
LOG((" duplicate call, bypassed"));
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-09-26 23:37:03 +04:00
|
|
|
if (NS_SUCCEEDED(mStatus)) {
|
|
|
|
if (mFlags & nsICacheStorage::OPEN_TRUNCATE) {
|
|
|
|
mozilla::Telemetry::AccumulateTimeDelta(
|
|
|
|
mozilla::Telemetry::NETWORK_CACHE_V1_TRUNCATE_TIME_MS, mLoadStart);
|
|
|
|
} else if (mNew) {
|
|
|
|
mozilla::Telemetry::AccumulateTimeDelta(
|
|
|
|
mozilla::Telemetry::NETWORK_CACHE_V1_MISS_TIME_MS, mLoadStart);
|
|
|
|
} else {
|
|
|
|
mozilla::Telemetry::AccumulateTimeDelta(
|
|
|
|
mozilla::Telemetry::NETWORK_CACHE_V1_HIT_TIME_MS, mLoadStart);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-21 02:20:17 +04:00
|
|
|
if (!(mFlags & CHECK_MULTITHREADED)) Check();
|
2013-09-20 13:11:25 +04:00
|
|
|
|
|
|
|
// break cycles
|
2020-02-13 17:38:48 +03:00
|
|
|
nsCOMPtr<nsICacheEntryOpenCallback> cb = std::move(mCallback);
|
2013-09-20 13:11:25 +04:00
|
|
|
mCacheThread = nullptr;
|
2020-02-13 17:38:48 +03:00
|
|
|
nsCOMPtr<nsICacheEntry> entry = std::move(mCacheEntry);
|
2013-09-20 13:11:25 +04:00
|
|
|
|
|
|
|
rv = cb->OnCacheEntryAvailable(entry, mNew, mAppCache, mStatus);
|
|
|
|
|
|
|
|
if (NS_FAILED(rv) && entry) {
|
2016-12-16 06:16:31 +03:00
|
|
|
LOG((" cb->OnCacheEntryAvailable failed with rv=0x%08" PRIx32,
|
|
|
|
static_cast<uint32_t>(rv)));
|
2013-09-20 13:11:25 +04:00
|
|
|
if (mNew)
|
|
|
|
entry->AsyncDoom(nullptr);
|
|
|
|
else
|
|
|
|
entry->Close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2019-05-01 11:47:10 +03:00
|
|
|
_OldCacheLoad::OnCacheEntryAvailable(nsICacheEntryDescriptor* entry,
|
2013-09-20 13:11:25 +04:00
|
|
|
nsCacheAccessMode access,
|
|
|
|
nsresult status) {
|
|
|
|
LOG(
|
|
|
|
("_OldCacheLoad::OnCacheEntryAvailable [this=%p, ent=%p, cb=%p, "
|
|
|
|
"appcache=%p, access=%x]",
|
|
|
|
this, entry, mCallback.get(), mAppCache.get(), access));
|
|
|
|
|
|
|
|
// XXX Bug 759805: Sometimes we will call this method directly from
|
|
|
|
// HttpCacheQuery::Run when AsyncOpenCacheEntry fails, but
|
|
|
|
// AsyncOpenCacheEntry will also call this method. As a workaround, we just
|
|
|
|
// ensure we only execute this code once.
|
|
|
|
NS_ENSURE_TRUE(mRunCount == 0, NS_ERROR_UNEXPECTED);
|
|
|
|
++mRunCount;
|
|
|
|
|
|
|
|
mCacheEntry = entry ? new _OldCacheEntryWrapper(entry) : nullptr;
|
|
|
|
mStatus = status;
|
|
|
|
mNew = access == nsICache::ACCESS_WRITE;
|
|
|
|
|
2013-11-21 02:20:17 +04:00
|
|
|
if (mFlags & CHECK_MULTITHREADED) Check();
|
2013-09-20 13:11:25 +04:00
|
|
|
|
2013-11-21 02:20:17 +04:00
|
|
|
if (mSync) return Run();
|
|
|
|
|
2013-09-20 13:11:25 +04:00
|
|
|
return NS_DispatchToMainThread(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void _OldCacheLoad::Check() {
|
|
|
|
if (!mCacheEntry) return;
|
|
|
|
|
|
|
|
if (mNew) return;
|
|
|
|
|
|
|
|
uint32_t result;
|
|
|
|
nsresult rv = mCallback->OnCacheEntryCheck(mCacheEntry, mAppCache, &result);
|
2016-12-16 06:16:31 +03:00
|
|
|
LOG((" OnCacheEntryCheck result ent=%p, cb=%p, appcache=%p, rv=0x%08" PRIx32
|
|
|
|
", result=%d",
|
|
|
|
mCacheEntry.get(), mCallback.get(), mAppCache.get(),
|
|
|
|
static_cast<uint32_t>(rv), result));
|
2013-09-20 13:11:25 +04:00
|
|
|
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("cache check failed");
|
|
|
|
}
|
|
|
|
|
2014-03-17 21:21:39 +04:00
|
|
|
if (NS_FAILED(rv) || result == nsICacheEntryOpenCallback::ENTRY_NOT_WANTED) {
|
2013-09-20 13:11:25 +04:00
|
|
|
mCacheEntry->Close();
|
|
|
|
mCacheEntry = nullptr;
|
|
|
|
mStatus = NS_ERROR_CACHE_KEY_NOT_FOUND;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
_OldCacheLoad::OnCacheEntryDoomed(nsresult) { return NS_ERROR_NOT_IMPLEMENTED; }
|
|
|
|
|
|
|
|
// nsICacheStorage old cache wrapper
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(_OldStorage, nsICacheStorage)
|
2013-09-20 13:11:25 +04:00
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
_OldStorage::_OldStorage(nsILoadContextInfo* aInfo, bool aAllowDisk,
|
2013-09-20 13:11:25 +04:00
|
|
|
bool aLookupAppCache, bool aOfflineStorage,
|
2019-05-01 11:47:10 +03:00
|
|
|
nsIApplicationCache* aAppCache)
|
2013-09-20 13:11:25 +04:00
|
|
|
: mLoadInfo(GetLoadContextInfo(aInfo)),
|
|
|
|
mAppCache(aAppCache),
|
|
|
|
mWriteToDisk(aAllowDisk),
|
|
|
|
mLookupAppCache(aLookupAppCache),
|
|
|
|
mOfflineStorage(aOfflineStorage) {}
|
|
|
|
|
2018-04-30 19:46:04 +03:00
|
|
|
_OldStorage::~_OldStorage() = default;
|
2013-09-20 13:11:25 +04:00
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
NS_IMETHODIMP _OldStorage::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
|
|
|
NS_ENSURE_ARG(aURI);
|
|
|
|
NS_ENSURE_ARG(aCallback);
|
|
|
|
|
|
|
|
#ifdef MOZ_LOGGING
|
|
|
|
nsAutoCString uriSpec;
|
|
|
|
aURI->GetAsciiSpec(uriSpec);
|
|
|
|
LOG(("_OldStorage::AsyncOpenURI [this=%p, uri=%s, ide=%s, flags=%x]", this,
|
|
|
|
uriSpec.get(), aIdExtension.BeginReading(), aFlags));
|
|
|
|
#endif
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
|
2013-11-21 02:20:16 +04:00
|
|
|
nsAutoCString cacheKey, scheme;
|
|
|
|
rv = AssembleCacheKey(aURI, aIdExtension, cacheKey, scheme);
|
2013-09-20 13:11:25 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (!mAppCache && (mLookupAppCache || mOfflineStorage)) {
|
|
|
|
rv = ChooseApplicationCache(cacheKey, getter_AddRefs(mAppCache));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2013-10-09 21:34:44 +04:00
|
|
|
|
|
|
|
if (mAppCache) {
|
|
|
|
// From a chosen appcache open only as readonly
|
|
|
|
aFlags &= ~nsICacheStorage::OPEN_TRUNCATE;
|
|
|
|
}
|
2013-09-20 13:11:25 +04:00
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<_OldCacheLoad> cacheLoad = new _OldCacheLoad(
|
2013-11-21 02:20:16 +04:00
|
|
|
scheme, cacheKey, aCallback, mAppCache, mLoadInfo, mWriteToDisk, aFlags);
|
2013-09-20 13:11:25 +04:00
|
|
|
|
|
|
|
rv = cacheLoad->Start();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
NS_IMETHODIMP _OldStorage::OpenTruncate(nsIURI* aURI,
|
|
|
|
const nsACString& aIdExtension,
|
|
|
|
nsICacheEntry** aCacheEntry) {
|
2014-07-16 18:16:30 +04:00
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
NS_IMETHODIMP _OldStorage::Exists(nsIURI* aURI, const nsACString& aIdExtension,
|
|
|
|
bool* aResult) {
|
2014-06-24 20:08:35 +04:00
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
NS_IMETHODIMP _OldStorage::AsyncDoomURI(nsIURI* aURI,
|
|
|
|
const nsACString& aIdExtension,
|
|
|
|
nsICacheEntryDoomCallback* aCallback) {
|
2013-09-20 13:11:25 +04:00
|
|
|
LOG(("_OldStorage::AsyncDoomURI"));
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
|
2013-11-21 02:20:16 +04:00
|
|
|
nsAutoCString cacheKey, scheme;
|
|
|
|
rv = AssembleCacheKey(aURI, aIdExtension, cacheKey, scheme);
|
2013-09-20 13:11:25 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsCOMPtr<nsICacheSession> session;
|
2013-11-21 02:20:16 +04:00
|
|
|
rv = GetCacheSession(scheme, mWriteToDisk, mLoadInfo, mAppCache,
|
|
|
|
getter_AddRefs(session));
|
2013-09-20 13:11:25 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<DoomCallbackWrapper> cb =
|
2013-09-20 13:11:25 +04:00
|
|
|
aCallback ? new DoomCallbackWrapper(aCallback) : nullptr;
|
|
|
|
rv = session->DoomEntry(cacheKey, cb);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP _OldStorage::AsyncEvictStorage(
|
2019-05-01 11:47:10 +03:00
|
|
|
nsICacheEntryDoomCallback* aCallback) {
|
2013-09-20 13:11:25 +04:00
|
|
|
LOG(("_OldStorage::AsyncEvictStorage"));
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
if (!mAppCache && mOfflineStorage) {
|
2016-01-18 22:20:08 +03:00
|
|
|
nsCOMPtr<nsIApplicationCacheService> appCacheService =
|
|
|
|
do_GetService(NS_APPLICATIONCACHESERVICE_CONTRACTID, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2015-10-11 19:13:09 +03:00
|
|
|
|
2016-01-18 22:20:08 +03:00
|
|
|
rv = appCacheService->Evict(mLoadInfo);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
} else if (mAppCache) {
|
|
|
|
nsCOMPtr<nsICacheSession> session;
|
|
|
|
rv = GetCacheSession(EmptyCString(), mWriteToDisk, mLoadInfo, mAppCache,
|
|
|
|
getter_AddRefs(session));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2013-09-20 13:11:25 +04:00
|
|
|
|
2016-01-18 22:20:08 +03:00
|
|
|
rv = session->EvictEntries();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
} else {
|
|
|
|
// Oh, I'll be so happy when session names are gone...
|
|
|
|
nsCOMPtr<nsICacheSession> session;
|
|
|
|
rv = GetCacheSession(NS_LITERAL_CSTRING("http"), mWriteToDisk, mLoadInfo,
|
|
|
|
mAppCache, getter_AddRefs(session));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2013-09-20 13:11:25 +04:00
|
|
|
|
2016-01-18 22:20:08 +03:00
|
|
|
rv = session->EvictEntries();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2019-02-28 04:09:48 +03:00
|
|
|
// This clears any data from schemes other than http or ftp.
|
2016-01-18 22:20:08 +03:00
|
|
|
rv = GetCacheSession(EmptyCString(), mWriteToDisk, mLoadInfo, mAppCache,
|
|
|
|
getter_AddRefs(session));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = session->EvictEntries();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2013-09-20 13:11:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (aCallback) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<DoomCallbackSynchronizer> sync =
|
2013-09-20 13:11:25 +04:00
|
|
|
new DoomCallbackSynchronizer(aCallback);
|
|
|
|
rv = sync->Dispatch();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
NS_IMETHODIMP _OldStorage::AsyncVisitStorage(nsICacheStorageVisitor* aVisitor,
|
2013-09-20 13:11:25 +04:00
|
|
|
bool aVisitEntries) {
|
|
|
|
LOG(("_OldStorage::AsyncVisitStorage"));
|
|
|
|
|
|
|
|
NS_ENSURE_ARG(aVisitor);
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
nsCOMPtr<nsICacheService> serv =
|
2014-05-01 15:28:12 +04:00
|
|
|
do_GetService(NS_CACHESERVICE_CONTRACTID, &rv);
|
2013-09-20 13:11:25 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
char* deviceID;
|
2013-09-20 13:11:25 +04:00
|
|
|
if (mAppCache || mOfflineStorage) {
|
2019-05-01 11:47:10 +03:00
|
|
|
deviceID = const_cast<char*>("offline");
|
2013-09-20 13:11:25 +04:00
|
|
|
} else if (!mWriteToDisk || mLoadInfo->IsPrivate()) {
|
2019-05-01 11:47:10 +03:00
|
|
|
deviceID = const_cast<char*>("memory");
|
2013-09-20 13:11:25 +04:00
|
|
|
} else {
|
2019-05-01 11:47:10 +03:00
|
|
|
deviceID = const_cast<char*>("disk");
|
2013-09-20 13:11:25 +04:00
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<_OldVisitCallbackWrapper> cb = new _OldVisitCallbackWrapper(
|
2014-05-01 15:28:12 +04:00
|
|
|
deviceID, aVisitor, aVisitEntries, mLoadInfo);
|
2014-06-05 22:14:07 +04:00
|
|
|
rv = nsCacheService::GlobalInstance()->VisitEntriesInternal(cb);
|
2013-09-20 13:11:25 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2017-04-28 12:14:21 +03:00
|
|
|
NS_IMETHODIMP _OldStorage::GetCacheIndexEntryAttrs(
|
2019-05-01 11:47:10 +03:00
|
|
|
nsIURI* aURI, const nsACString& aIdExtension, bool* aHasAltData,
|
|
|
|
uint32_t* aSizeInKB) {
|
2017-04-28 12:14:21 +03:00
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
2013-09-20 13:11:25 +04:00
|
|
|
// Internal
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
nsresult _OldStorage::AssembleCacheKey(nsIURI* aURI,
|
|
|
|
nsACString const& aIdExtension,
|
|
|
|
nsACString& aCacheKey,
|
|
|
|
nsACString& aScheme) {
|
2013-09-20 13:11:25 +04:00
|
|
|
// Copied from nsHttpChannel::AssembleCacheKey
|
|
|
|
|
|
|
|
aCacheKey.Truncate();
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
|
2013-11-21 02:20:16 +04:00
|
|
|
rv = aURI->GetScheme(aScheme);
|
2013-09-20 13:11:25 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsAutoCString uriSpec;
|
2014-05-22 07:48:51 +04:00
|
|
|
if (aScheme.EqualsLiteral("http") || aScheme.EqualsLiteral("https")) {
|
2013-11-21 02:20:16 +04:00
|
|
|
if (mLoadInfo->IsAnonymous()) {
|
|
|
|
aCacheKey.AssignLiteral("anon&");
|
|
|
|
}
|
2013-09-20 13:11:25 +04:00
|
|
|
|
2013-11-21 02:20:16 +04:00
|
|
|
if (!aIdExtension.IsEmpty()) {
|
|
|
|
aCacheKey.AppendPrintf("id=%s&", aIdExtension.BeginReading());
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> noRefURI;
|
2018-07-23 14:28:47 +03:00
|
|
|
rv = NS_GetURIWithoutRef(aURI, getter_AddRefs(noRefURI));
|
2013-11-21 02:20:16 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = noRefURI->GetAsciiSpec(uriSpec);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (!aCacheKey.IsEmpty()) {
|
|
|
|
aCacheKey.AppendLiteral("uri=");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
rv = aURI->GetAsciiSpec(uriSpec);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
2013-09-20 13:11:25 +04:00
|
|
|
aCacheKey.Append(uriSpec);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
nsresult _OldStorage::ChooseApplicationCache(const nsACString& cacheKey,
|
|
|
|
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);
|
|
|
|
|
|
|
|
rv = appCacheService->ChooseApplicationCache(cacheKey, mLoadInfo, aCache);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace net
|
|
|
|
} // namespace mozilla
|