2015-11-20 02:31:28 +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: */
|
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/. */
|
|
|
|
|
|
|
|
#include "CacheLog.h"
|
|
|
|
#include "CacheStorageService.h"
|
|
|
|
#include "CacheFileIOManager.h"
|
|
|
|
#include "CacheObserver.h"
|
2014-02-27 03:11:40 +04:00
|
|
|
#include "CacheIndex.h"
|
2014-05-01 15:28:12 +04:00
|
|
|
#include "CacheIndexIterator.h"
|
2013-09-20 13:11:25 +04:00
|
|
|
#include "CacheStorage.h"
|
|
|
|
#include "CacheEntry.h"
|
2014-02-18 21:26:48 +04:00
|
|
|
#include "CacheFileUtils.h"
|
2013-09-20 13:11:25 +04:00
|
|
|
|
2014-05-01 15:28:12 +04:00
|
|
|
#include "nsICacheStorageVisitor.h"
|
|
|
|
#include "nsIObserverService.h"
|
2013-09-20 13:11:25 +04:00
|
|
|
#include "nsIFile.h"
|
|
|
|
#include "nsIURI.h"
|
2021-10-12 18:41:15 +03:00
|
|
|
#include "nsINetworkPredictor.h"
|
2013-09-20 13:11:25 +04:00
|
|
|
#include "nsCOMPtr.h"
|
2018-07-20 14:57:18 +03:00
|
|
|
#include "nsContentUtils.h"
|
2013-09-20 13:11:25 +04:00
|
|
|
#include "nsNetCID.h"
|
2014-05-01 15:28:12 +04:00
|
|
|
#include "nsNetUtil.h"
|
2013-09-20 13:11:25 +04:00
|
|
|
#include "nsServiceManagerUtils.h"
|
2017-10-18 22:10:22 +03:00
|
|
|
#include "nsXULAppAPI.h"
|
2020-12-08 19:56:23 +03:00
|
|
|
#include "mozilla/AtomicBitfields.h"
|
2013-09-20 13:11:25 +04:00
|
|
|
#include "mozilla/TimeStamp.h"
|
|
|
|
#include "mozilla/DebugOnly.h"
|
|
|
|
#include "mozilla/Services.h"
|
2021-07-07 19:05:06 +03:00
|
|
|
#include "mozilla/StoragePrincipalHelper.h"
|
2016-12-16 06:16:31 +03:00
|
|
|
#include "mozilla/IntegerPrintfMacros.h"
|
2021-04-30 10:20:24 +03:00
|
|
|
#include "mozilla/Telemetry.h"
|
2021-06-22 11:23:36 +03:00
|
|
|
#include "mozilla/StaticPrefs_network.h"
|
2013-09-20 13:11:25 +04:00
|
|
|
|
2021-06-04 15:35:16 +03:00
|
|
|
namespace mozilla::net {
|
2013-09-20 13:11:25 +04:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2019-03-20 16:54:57 +03:00
|
|
|
void AppendMemoryStorageTag(nsAutoCString& key) {
|
|
|
|
// Using DEL as the very last ascii-7 character we can use in the list of
|
|
|
|
// attributes
|
|
|
|
key.Append('\x7f');
|
|
|
|
key.Append(',');
|
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
|
|
|
|
|
|
|
// Not defining as static or class member of CacheStorageService since
|
|
|
|
// it would otherwise need to include CacheEntry.h and that then would
|
|
|
|
// need to be exported to make nsNetModule.cpp compilable.
|
2021-06-04 15:35:16 +03:00
|
|
|
using GlobalEntryTables = nsClassHashtable<nsCStringHashKey, CacheEntryTable>;
|
2013-09-20 13:11:25 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Keeps tables of entries. There is one entries table for each distinct load
|
|
|
|
* context type. The distinction is based on following load context info
|
2019-05-03 20:14:23 +03:00
|
|
|
* states: <isPrivate|isAnon|inIsolatedMozBrowser> which builds a mapping
|
2016-02-05 04:42:44 +03:00
|
|
|
* key.
|
2013-09-20 13:11:25 +04:00
|
|
|
*
|
|
|
|
* Thread-safe to access, protected by the service mutex.
|
|
|
|
*/
|
|
|
|
static GlobalEntryTables* sGlobalEntryTables;
|
|
|
|
|
2021-01-28 14:05:02 +03:00
|
|
|
CacheMemoryConsumer::CacheMemoryConsumer(uint32_t aFlags) {
|
|
|
|
StoreFlags(aFlags);
|
|
|
|
}
|
2013-09-20 13:11:25 +04:00
|
|
|
|
|
|
|
void CacheMemoryConsumer::DoMemoryReport(uint32_t aCurrentSize) {
|
2021-01-28 14:05:02 +03:00
|
|
|
if (!(LoadFlags() & DONT_REPORT) && CacheStorageService::Self()) {
|
2013-09-20 13:11:25 +04:00
|
|
|
CacheStorageService::Self()->OnMemoryConsumptionChange(this, aCurrentSize);
|
2014-04-06 22:45:18 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-23 12:14:19 +03:00
|
|
|
CacheStorageService::MemoryPool::MemoryPool(EType aType) : mType(aType) {}
|
2014-04-06 22:45:18 +04:00
|
|
|
|
|
|
|
CacheStorageService::MemoryPool::~MemoryPool() {
|
|
|
|
if (mMemorySize != 0) {
|
|
|
|
NS_ERROR(
|
|
|
|
"Network cache reported memory consumption is not at 0, probably "
|
|
|
|
"leaking?");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t CacheStorageService::MemoryPool::Limit() const {
|
2019-01-08 17:28:00 +03:00
|
|
|
uint32_t limit = 0;
|
|
|
|
|
2014-04-06 22:45:18 +04:00
|
|
|
switch (mType) {
|
|
|
|
case DISK:
|
2019-01-08 17:28:00 +03:00
|
|
|
limit = CacheObserver::MetadataMemoryLimit();
|
|
|
|
break;
|
2014-04-06 22:45:18 +04:00
|
|
|
case MEMORY:
|
2019-01-08 17:28:00 +03:00
|
|
|
limit = CacheObserver::MemoryCacheCapacity();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
MOZ_CRASH("Bad pool type");
|
|
|
|
}
|
|
|
|
|
|
|
|
static const uint32_t kMaxLimit = 0x3FFFFF;
|
|
|
|
if (limit > kMaxLimit) {
|
|
|
|
LOG((" a memory limit (%u) is unexpectedly high, clipping to %u", limit,
|
|
|
|
kMaxLimit));
|
|
|
|
limit = kMaxLimit;
|
2014-04-06 22:45:18 +04:00
|
|
|
}
|
|
|
|
|
2019-01-08 17:28:00 +03:00
|
|
|
return limit << 10;
|
2013-09-20 13:11:25 +04:00
|
|
|
}
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(CacheStorageService, nsICacheStorageService,
|
|
|
|
nsIMemoryReporter, nsITimerCallback, nsICacheTesting,
|
2017-07-26 21:18:20 +03:00
|
|
|
nsINamed)
|
2013-09-20 13:11:25 +04:00
|
|
|
|
|
|
|
CacheStorageService* CacheStorageService::sSelf = nullptr;
|
|
|
|
|
2021-09-23 12:14:19 +03:00
|
|
|
CacheStorageService::CacheStorageService() {
|
2013-09-20 13:11:25 +04:00
|
|
|
CacheFileIOManager::Init();
|
|
|
|
|
2017-10-18 22:10:22 +03:00
|
|
|
MOZ_ASSERT(XRE_IsParentProcess());
|
2013-09-20 13:11:25 +04:00
|
|
|
MOZ_ASSERT(!sSelf);
|
|
|
|
|
|
|
|
sSelf = this;
|
|
|
|
sGlobalEntryTables = new GlobalEntryTables();
|
2014-02-27 03:11:40 +04:00
|
|
|
|
|
|
|
RegisterStrongMemoryReporter(this);
|
2013-09-20 13:11:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
CacheStorageService::~CacheStorageService() {
|
|
|
|
LOG(("CacheStorageService::~CacheStorageService"));
|
|
|
|
sSelf = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CacheStorageService::Shutdown() {
|
2016-09-20 13:53:00 +03:00
|
|
|
mozilla::MutexAutoLock lock(mLock);
|
|
|
|
|
2013-09-20 13:11:25 +04:00
|
|
|
if (mShutdown) return;
|
|
|
|
|
|
|
|
LOG(("CacheStorageService::Shutdown - start"));
|
|
|
|
|
|
|
|
mShutdown = true;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIRunnable> event =
|
2017-06-12 22:34:10 +03:00
|
|
|
NewRunnableMethod("net::CacheStorageService::ShutdownBackground", this,
|
|
|
|
&CacheStorageService::ShutdownBackground);
|
2014-01-22 21:54:52 +04:00
|
|
|
Dispatch(event);
|
2013-09-20 13:11:25 +04:00
|
|
|
|
2016-05-18 07:39:00 +03:00
|
|
|
#ifdef NS_FREE_PERMANENT_DATA
|
2016-09-20 13:53:00 +03:00
|
|
|
sGlobalEntryTables->Clear();
|
|
|
|
delete sGlobalEntryTables;
|
2016-05-18 07:39:00 +03:00
|
|
|
#endif
|
2016-09-20 13:53:00 +03:00
|
|
|
sGlobalEntryTables = nullptr;
|
2013-09-20 13:11:25 +04:00
|
|
|
|
|
|
|
LOG(("CacheStorageService::Shutdown - done"));
|
|
|
|
}
|
|
|
|
|
|
|
|
void CacheStorageService::ShutdownBackground() {
|
2016-09-20 13:53:00 +03:00
|
|
|
LOG(("CacheStorageService::ShutdownBackground - start"));
|
|
|
|
|
2013-09-20 13:11:25 +04:00
|
|
|
MOZ_ASSERT(IsOnManagementThread());
|
|
|
|
|
2016-09-20 13:53:00 +03:00
|
|
|
{
|
|
|
|
mozilla::MutexAutoLock lock(mLock);
|
|
|
|
|
|
|
|
// Cancel purge timer to avoid leaking.
|
|
|
|
if (mPurgeTimer) {
|
|
|
|
LOG((" freeing the timer"));
|
|
|
|
mPurgeTimer->Cancel();
|
|
|
|
}
|
2014-09-03 00:33:32 +04:00
|
|
|
}
|
|
|
|
|
2016-05-18 07:39:00 +03:00
|
|
|
#ifdef NS_FREE_PERMANENT_DATA
|
2014-04-06 22:45:18 +04:00
|
|
|
Pool(false).mFrecencyArray.Clear();
|
|
|
|
Pool(false).mExpirationArray.Clear();
|
|
|
|
Pool(true).mFrecencyArray.Clear();
|
|
|
|
Pool(true).mExpirationArray.Clear();
|
2016-05-18 07:39:00 +03:00
|
|
|
#endif
|
2016-09-20 13:53:00 +03:00
|
|
|
|
|
|
|
LOG(("CacheStorageService::ShutdownBackground - done"));
|
2013-09-20 13:11:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Internal management methods
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
namespace {
|
2013-09-20 13:11:25 +04:00
|
|
|
|
2014-05-01 15:28:12 +04:00
|
|
|
// WalkCacheRunnable
|
|
|
|
// Base class for particular storage entries visiting
|
2016-04-26 03:23:21 +03:00
|
|
|
class WalkCacheRunnable : public Runnable,
|
2014-05-01 15:28:12 +04:00
|
|
|
public CacheStorageService::EntryInfoCallback {
|
|
|
|
protected:
|
2017-06-12 22:34:10 +03:00
|
|
|
WalkCacheRunnable(nsICacheStorageVisitor* aVisitor, bool aVisitEntries)
|
|
|
|
: Runnable("net::WalkCacheRunnable"),
|
|
|
|
mService(CacheStorageService::Self()),
|
2021-09-23 12:14:19 +03:00
|
|
|
mCallback(aVisitor) {
|
2014-05-17 17:38:50 +04:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2020-12-09 21:14:55 +03:00
|
|
|
StoreNotifyStorage(true);
|
|
|
|
StoreVisitEntries(aVisitEntries);
|
2014-05-17 17:38:50 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~WalkCacheRunnable() {
|
|
|
|
if (mCallback) {
|
2017-06-14 04:27:17 +03:00
|
|
|
ProxyReleaseMainThread("WalkCacheRunnable::mCallback", mCallback);
|
2014-05-17 17:38:50 +04:00
|
|
|
}
|
2014-05-01 15:28:12 +04:00
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<CacheStorageService> mService;
|
2014-05-01 15:28:12 +04:00
|
|
|
nsCOMPtr<nsICacheStorageVisitor> mCallback;
|
|
|
|
|
2021-09-23 12:14:19 +03:00
|
|
|
uint64_t mSize{0};
|
2014-05-01 15:28:12 +04:00
|
|
|
|
2020-12-08 19:56:23 +03:00
|
|
|
// clang-format off
|
|
|
|
MOZ_ATOMIC_BITFIELDS(mAtomicBitfields, 8, (
|
|
|
|
(bool, NotifyStorage, 1),
|
|
|
|
(bool, VisitEntries, 1)
|
|
|
|
))
|
|
|
|
// clang-format on
|
2016-05-12 17:19:00 +03:00
|
|
|
|
2021-09-23 12:14:19 +03:00
|
|
|
Atomic<bool> mCancel{false};
|
2014-05-01 15:28:12 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
// WalkMemoryCacheRunnable
|
|
|
|
// Responsible to visit memory storage and walk
|
|
|
|
// all entries on it asynchronously.
|
|
|
|
class WalkMemoryCacheRunnable : public WalkCacheRunnable {
|
|
|
|
public:
|
2015-07-31 08:57:50 +03:00
|
|
|
WalkMemoryCacheRunnable(nsILoadContextInfo* aLoadInfo, bool aVisitEntries,
|
2014-05-01 15:28:12 +04:00
|
|
|
nsICacheStorageVisitor* aVisitor)
|
|
|
|
: WalkCacheRunnable(aVisitor, aVisitEntries) {
|
2015-07-31 08:57:50 +03:00
|
|
|
CacheFileUtils::AppendKeyPrefix(aLoadInfo, mContextKey);
|
2013-09-20 13:11:25 +04:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
}
|
|
|
|
|
2014-05-01 15:28:12 +04:00
|
|
|
nsresult Walk() { return mService->Dispatch(this); }
|
|
|
|
|
2013-09-20 13:11:25 +04:00
|
|
|
private:
|
2016-08-08 05:18:10 +03:00
|
|
|
NS_IMETHOD Run() override {
|
2013-09-20 13:11:25 +04:00
|
|
|
if (CacheStorageService::IsOnManagementThread()) {
|
2014-05-01 15:28:12 +04:00
|
|
|
LOG(("WalkMemoryCacheRunnable::Run - collecting [this=%p]", this));
|
2013-09-20 13:11:25 +04:00
|
|
|
// First, walk, count and grab all entries from the storage
|
|
|
|
|
|
|
|
mozilla::MutexAutoLock lock(CacheStorageService::Self()->Lock());
|
|
|
|
|
|
|
|
if (!CacheStorageService::IsRunning()) return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
|
2021-03-24 20:56:49 +03:00
|
|
|
for (const auto& entries : sGlobalEntryTables->Values()) {
|
2020-07-03 14:41:53 +03:00
|
|
|
if (entries->Type() != CacheEntryTable::MEMORY_ONLY) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-03-24 20:56:49 +03:00
|
|
|
for (CacheEntry* entry : entries->Values()) {
|
2020-07-03 14:41:53 +03:00
|
|
|
MOZ_ASSERT(!entry->IsUsingDisk());
|
2015-11-20 02:31:28 +03:00
|
|
|
|
|
|
|
mSize += entry->GetMetadataMemoryConsumption();
|
|
|
|
|
|
|
|
int64_t size;
|
|
|
|
if (NS_SUCCEEDED(entry->GetDataSize(&size))) {
|
|
|
|
mSize += size;
|
|
|
|
}
|
|
|
|
mEntryArray.AppendElement(entry);
|
|
|
|
}
|
|
|
|
}
|
2013-09-20 13:11:25 +04:00
|
|
|
|
|
|
|
// Next, we dispatch to the main thread
|
2014-05-01 15:28:12 +04:00
|
|
|
} else if (NS_IsMainThread()) {
|
|
|
|
LOG(("WalkMemoryCacheRunnable::Run - notifying [this=%p]", this));
|
|
|
|
|
2020-12-09 21:14:55 +03:00
|
|
|
if (LoadNotifyStorage()) {
|
2013-09-20 13:11:25 +04:00
|
|
|
LOG((" storage"));
|
2014-05-01 15:28:12 +04:00
|
|
|
|
2019-01-08 17:28:00 +03:00
|
|
|
uint64_t capacity = CacheObserver::MemoryCacheCapacity();
|
|
|
|
capacity <<= 10; // kilobytes to bytes
|
|
|
|
|
2013-09-20 13:11:25 +04:00
|
|
|
// Second, notify overall storage info
|
2019-01-08 17:28:00 +03:00
|
|
|
mCallback->OnCacheStorageInfo(mEntryArray.Length(), mSize, capacity,
|
2014-05-01 15:28:12 +04:00
|
|
|
nullptr);
|
2020-12-09 21:14:55 +03:00
|
|
|
if (!LoadVisitEntries()) return NS_OK; // done
|
2013-09-20 13:11:25 +04:00
|
|
|
|
2020-12-09 21:14:55 +03:00
|
|
|
StoreNotifyStorage(false);
|
2014-05-01 15:28:12 +04:00
|
|
|
|
|
|
|
} else {
|
2017-07-26 23:03:57 +03:00
|
|
|
LOG((" entry [left=%zu, canceled=%d]", mEntryArray.Length(),
|
|
|
|
(bool)mCancel));
|
2014-05-01 15:28:12 +04:00
|
|
|
|
2016-05-12 17:19:00 +03:00
|
|
|
// Third, notify each entry until depleted or canceled
|
|
|
|
if (!mEntryArray.Length() || mCancel) {
|
2013-09-20 13:11:25 +04:00
|
|
|
mCallback->OnCacheEntryVisitCompleted();
|
|
|
|
return NS_OK; // done
|
|
|
|
}
|
|
|
|
|
2014-05-01 15:28:12 +04:00
|
|
|
// Grab the next entry
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<CacheEntry> entry = mEntryArray[0];
|
2013-09-20 13:11:25 +04:00
|
|
|
mEntryArray.RemoveElementAt(0);
|
|
|
|
|
2014-05-01 15:28:12 +04:00
|
|
|
// Invokes this->OnEntryInfo, that calls the callback with all
|
|
|
|
// information of the entry.
|
|
|
|
CacheStorageService::GetCacheEntryInfo(entry, this);
|
2013-09-20 13:11:25 +04:00
|
|
|
}
|
2014-05-01 15:28:12 +04:00
|
|
|
} else {
|
|
|
|
MOZ_CRASH("Bad thread");
|
2013-09-20 13:11:25 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_DispatchToMainThread(this);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-05-01 15:28:12 +04:00
|
|
|
virtual ~WalkMemoryCacheRunnable() {
|
2021-06-04 15:35:16 +03:00
|
|
|
if (mCallback) {
|
2017-06-14 04:27:17 +03:00
|
|
|
ProxyReleaseMainThread("WalkMemoryCacheRunnable::mCallback", mCallback);
|
2021-06-04 15:35:16 +03:00
|
|
|
}
|
2013-09-20 13:11:25 +04:00
|
|
|
}
|
|
|
|
|
2014-05-01 15:28:12 +04:00
|
|
|
virtual void OnEntryInfo(const nsACString& aURISpec,
|
|
|
|
const nsACString& aIdEnhance, int64_t aDataSize,
|
2022-08-04 19:20:17 +03:00
|
|
|
int64_t aAltDataSize, uint32_t aFetchCount,
|
|
|
|
uint32_t aLastModifiedTime, uint32_t aExpirationTime,
|
|
|
|
bool aPinned, nsILoadContextInfo* aInfo) override {
|
2016-05-12 17:19:00 +03:00
|
|
|
nsresult rv;
|
|
|
|
|
2014-05-01 15:28:12 +04:00
|
|
|
nsCOMPtr<nsIURI> uri;
|
2016-05-12 17:19:00 +03:00
|
|
|
rv = NS_NewURI(getter_AddRefs(uri), aURISpec);
|
|
|
|
if (NS_FAILED(rv)) {
|
2014-05-01 15:28:12 +04:00
|
|
|
return;
|
2016-05-12 17:19:00 +03:00
|
|
|
}
|
2014-05-01 15:28:12 +04:00
|
|
|
|
2022-08-04 19:20:17 +03:00
|
|
|
rv = mCallback->OnCacheEntryInfo(uri, aIdEnhance, aDataSize, aAltDataSize,
|
|
|
|
aFetchCount, aLastModifiedTime,
|
|
|
|
aExpirationTime, aPinned, aInfo);
|
2016-05-12 17:19:00 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
LOG((" callback failed, canceling the walk"));
|
|
|
|
mCancel = true;
|
|
|
|
}
|
2014-05-01 15:28:12 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2013-09-20 13:11:25 +04:00
|
|
|
nsCString mContextKey;
|
2015-10-18 08:24:48 +03:00
|
|
|
nsTArray<RefPtr<CacheEntry>> mEntryArray;
|
2014-05-01 15:28:12 +04:00
|
|
|
};
|
2014-04-30 14:39:18 +04:00
|
|
|
|
2014-05-01 15:28:12 +04:00
|
|
|
// WalkDiskCacheRunnable
|
|
|
|
// Using the cache index information to get the list of files per context.
|
|
|
|
class WalkDiskCacheRunnable : public WalkCacheRunnable {
|
|
|
|
public:
|
2018-04-13 16:01:28 +03:00
|
|
|
WalkDiskCacheRunnable(nsILoadContextInfo* aLoadInfo, bool aVisitEntries,
|
2014-05-01 15:28:12 +04:00
|
|
|
nsICacheStorageVisitor* aVisitor)
|
|
|
|
: WalkCacheRunnable(aVisitor, aVisitEntries),
|
|
|
|
mLoadInfo(aLoadInfo),
|
|
|
|
mPass(COLLECT_STATS),
|
2018-06-14 11:19:07 +03:00
|
|
|
mCount(0) {}
|
2014-04-30 14:39:18 +04:00
|
|
|
|
2014-05-01 15:28:12 +04:00
|
|
|
nsresult Walk() {
|
|
|
|
// TODO, bug 998693
|
|
|
|
// Initial index build should be forced here so that about:cache soon
|
|
|
|
// after startup gives some meaningfull results.
|
|
|
|
|
|
|
|
// Dispatch to the INDEX level in hope that very recent cache entries
|
|
|
|
// information gets to the index list before we grab the index iterator
|
|
|
|
// for the first time. This tries to avoid miss of entries that has
|
|
|
|
// been created right before the visit is required.
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<CacheIOThread> thread = CacheFileIOManager::IOThread();
|
2014-05-01 15:28:12 +04:00
|
|
|
NS_ENSURE_TRUE(thread, NS_ERROR_NOT_INITIALIZED);
|
|
|
|
|
|
|
|
return thread->Dispatch(this, CacheIOThread::INDEX);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
// Invokes OnCacheEntryInfo callback for each single found entry.
|
|
|
|
// There is one instance of this class per one entry.
|
2016-04-26 03:23:21 +03:00
|
|
|
class OnCacheEntryInfoRunnable : public Runnable {
|
2014-05-01 15:28:12 +04:00
|
|
|
public:
|
2014-08-05 17:20:24 +04:00
|
|
|
explicit OnCacheEntryInfoRunnable(WalkDiskCacheRunnable* aWalker)
|
2017-06-12 22:34:10 +03:00
|
|
|
: Runnable("net::WalkDiskCacheRunnable::OnCacheEntryInfoRunnable"),
|
2021-09-23 12:14:19 +03:00
|
|
|
mWalker(aWalker) {}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-08-08 05:18:10 +03:00
|
|
|
NS_IMETHOD Run() override {
|
2014-05-01 15:28:12 +04:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2016-05-12 17:19:00 +03:00
|
|
|
nsresult rv;
|
|
|
|
|
2014-05-01 15:28:12 +04:00
|
|
|
nsCOMPtr<nsIURI> uri;
|
2016-05-12 17:19:00 +03:00
|
|
|
rv = NS_NewURI(getter_AddRefs(uri), mURISpec);
|
|
|
|
if (NS_FAILED(rv)) {
|
2014-05-01 15:28:12 +04:00
|
|
|
return NS_OK;
|
2016-05-12 17:19:00 +03:00
|
|
|
}
|
2014-05-01 15:28:12 +04:00
|
|
|
|
2016-05-12 17:19:00 +03:00
|
|
|
rv = mWalker->mCallback->OnCacheEntryInfo(
|
2022-08-04 19:20:17 +03:00
|
|
|
uri, mIdEnhance, mDataSize, mAltDataSize, mFetchCount,
|
|
|
|
mLastModifiedTime, mExpirationTime, mPinned, mInfo);
|
2016-05-12 17:19:00 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
mWalker->mCancel = true;
|
|
|
|
}
|
|
|
|
|
2014-05-01 15:28:12 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<WalkDiskCacheRunnable> mWalker;
|
2014-05-01 15:28:12 +04:00
|
|
|
|
|
|
|
nsCString mURISpec;
|
|
|
|
nsCString mIdEnhance;
|
2021-09-23 12:14:19 +03:00
|
|
|
int64_t mDataSize{0};
|
2022-08-04 19:20:17 +03:00
|
|
|
int64_t mAltDataSize{0};
|
2022-08-04 19:20:17 +03:00
|
|
|
uint32_t mFetchCount{0};
|
2021-09-23 12:14:19 +03:00
|
|
|
uint32_t mLastModifiedTime{0};
|
|
|
|
uint32_t mExpirationTime{0};
|
|
|
|
bool mPinned{false};
|
2017-02-03 06:14:00 +03:00
|
|
|
nsCOMPtr<nsILoadContextInfo> mInfo;
|
2014-05-01 15:28:12 +04:00
|
|
|
};
|
|
|
|
|
2016-08-08 05:18:10 +03:00
|
|
|
NS_IMETHOD Run() override {
|
2014-05-01 15:28:12 +04:00
|
|
|
// The main loop
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
if (CacheStorageService::IsOnManagementThread()) {
|
|
|
|
switch (mPass) {
|
|
|
|
case COLLECT_STATS:
|
|
|
|
// Get quickly the cache stats.
|
|
|
|
uint32_t size;
|
|
|
|
rv = CacheIndex::GetCacheStats(mLoadInfo, &size, &mCount);
|
|
|
|
if (NS_FAILED(rv)) {
|
2020-12-09 21:14:55 +03:00
|
|
|
if (LoadVisitEntries()) {
|
2014-05-01 15:28:12 +04:00
|
|
|
// both onStorageInfo and onCompleted are expected
|
|
|
|
NS_DispatchToMainThread(this);
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2014-05-01 15:28:12 +04:00
|
|
|
return NS_DispatchToMainThread(this);
|
|
|
|
}
|
|
|
|
|
2017-03-01 17:23:00 +03:00
|
|
|
mSize = static_cast<uint64_t>(size) << 10;
|
2014-05-01 15:28:12 +04:00
|
|
|
|
|
|
|
// Invoke onCacheStorageInfo with valid information.
|
|
|
|
NS_DispatchToMainThread(this);
|
|
|
|
|
2020-12-09 21:14:55 +03:00
|
|
|
if (!LoadVisitEntries()) {
|
2014-05-01 15:28:12 +04:00
|
|
|
return NS_OK; // done
|
|
|
|
}
|
|
|
|
|
|
|
|
mPass = ITERATE_METADATA;
|
2019-12-20 10:16:43 +03:00
|
|
|
[[fallthrough]];
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2014-05-01 15:28:12 +04:00
|
|
|
case ITERATE_METADATA:
|
|
|
|
// Now grab the context iterator.
|
|
|
|
if (!mIter) {
|
|
|
|
rv =
|
|
|
|
CacheIndex::GetIterator(mLoadInfo, true, getter_AddRefs(mIter));
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
// Invoke onCacheEntryVisitCompleted now
|
|
|
|
return NS_DispatchToMainThread(this);
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2014-05-01 15:28:12 +04:00
|
|
|
}
|
|
|
|
|
2016-06-01 08:20:17 +03:00
|
|
|
while (!mCancel && !CacheObserver::ShuttingDown()) {
|
2014-05-01 15:28:12 +04:00
|
|
|
if (CacheIOThread::YieldAndRerun()) return NS_OK;
|
|
|
|
|
|
|
|
SHA1Sum::Hash hash;
|
|
|
|
rv = mIter->GetNextHash(&hash);
|
|
|
|
if (NS_FAILED(rv)) break; // done (or error?)
|
|
|
|
|
2016-08-09 18:23:00 +03:00
|
|
|
// This synchronously invokes OnEntryInfo on this class where we
|
2014-05-01 15:28:12 +04:00
|
|
|
// redispatch to the main thread for the consumer callback.
|
|
|
|
CacheFileIOManager::GetEntryInfo(&hash, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Invoke onCacheEntryVisitCompleted on the main thread
|
|
|
|
NS_DispatchToMainThread(this);
|
|
|
|
}
|
|
|
|
} else if (NS_IsMainThread()) {
|
2020-12-09 21:14:55 +03:00
|
|
|
if (LoadNotifyStorage()) {
|
2014-05-01 15:28:12 +04:00
|
|
|
nsCOMPtr<nsIFile> dir;
|
|
|
|
CacheFileIOManager::GetCacheDirectory(getter_AddRefs(dir));
|
2019-01-08 17:28:00 +03:00
|
|
|
uint64_t capacity = CacheObserver::DiskCacheCapacity();
|
|
|
|
capacity <<= 10; // kilobytes to bytes
|
|
|
|
mCallback->OnCacheStorageInfo(mCount, mSize, capacity, dir);
|
2020-12-09 21:14:55 +03:00
|
|
|
StoreNotifyStorage(false);
|
2014-05-01 15:28:12 +04:00
|
|
|
} else {
|
|
|
|
mCallback->OnCacheEntryVisitCompleted();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
MOZ_CRASH("Bad thread");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void OnEntryInfo(const nsACString& aURISpec,
|
|
|
|
const nsACString& aIdEnhance, int64_t aDataSize,
|
2022-08-04 19:20:17 +03:00
|
|
|
int64_t aAltDataSize, uint32_t aFetchCount,
|
|
|
|
uint32_t aLastModifiedTime, uint32_t aExpirationTime,
|
|
|
|
bool aPinned, nsILoadContextInfo* aInfo) override {
|
2014-05-01 15:28:12 +04:00
|
|
|
// Called directly from CacheFileIOManager::GetEntryInfo.
|
|
|
|
|
|
|
|
// Invoke onCacheEntryInfo on the main thread for this entry.
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<OnCacheEntryInfoRunnable> info = new OnCacheEntryInfoRunnable(this);
|
2014-05-01 15:28:12 +04:00
|
|
|
info->mURISpec = aURISpec;
|
|
|
|
info->mIdEnhance = aIdEnhance;
|
|
|
|
info->mDataSize = aDataSize;
|
2022-08-04 19:20:17 +03:00
|
|
|
info->mAltDataSize = aAltDataSize;
|
2014-05-01 15:28:12 +04:00
|
|
|
info->mFetchCount = aFetchCount;
|
|
|
|
info->mLastModifiedTime = aLastModifiedTime;
|
|
|
|
info->mExpirationTime = aExpirationTime;
|
2015-10-30 10:12:00 +03:00
|
|
|
info->mPinned = aPinned;
|
2017-02-03 06:14:00 +03:00
|
|
|
info->mInfo = aInfo;
|
2014-05-01 15:28:12 +04:00
|
|
|
|
|
|
|
NS_DispatchToMainThread(info);
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsILoadContextInfo> mLoadInfo;
|
2014-05-01 15:28:12 +04:00
|
|
|
enum {
|
|
|
|
// First, we collect stats for the load context.
|
|
|
|
COLLECT_STATS,
|
|
|
|
|
|
|
|
// Second, if demanded, we iterate over the entries gethered
|
|
|
|
// from the iterator and call CacheFileIOManager::GetEntryInfo
|
|
|
|
// for each found entry.
|
|
|
|
ITERATE_METADATA,
|
|
|
|
} mPass;
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<CacheIndexIterator> mIter;
|
2014-05-01 15:28:12 +04:00
|
|
|
uint32_t mCount;
|
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
|
|
|
|
|
|
|
void CacheStorageService::DropPrivateBrowsingEntries() {
|
|
|
|
mozilla::MutexAutoLock lock(mLock);
|
|
|
|
|
|
|
|
if (mShutdown) return;
|
|
|
|
|
|
|
|
nsTArray<nsCString> keys;
|
2021-03-24 20:51:12 +03:00
|
|
|
for (const nsACString& key : sGlobalEntryTables->Keys()) {
|
2015-11-20 03:44:24 +03:00
|
|
|
nsCOMPtr<nsILoadContextInfo> info = CacheFileUtils::ParseKey(key);
|
|
|
|
if (info && info->IsPrivate()) {
|
|
|
|
keys.AppendElement(key);
|
|
|
|
}
|
|
|
|
}
|
2013-09-20 13:11:25 +04:00
|
|
|
|
2015-11-20 03:44:24 +03:00
|
|
|
for (uint32_t i = 0; i < keys.Length(); ++i) {
|
2015-10-22 13:11:00 +03:00
|
|
|
DoomStorageEntries(keys[i], nullptr, true, false, nullptr);
|
2015-11-20 03:44:24 +03:00
|
|
|
}
|
2013-09-20 13:11:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Helper methods
|
|
|
|
|
2014-01-22 21:54:52 +04:00
|
|
|
// static
|
|
|
|
bool CacheStorageService::IsOnManagementThread() {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<CacheStorageService> service = Self();
|
2014-01-22 21:54:52 +04:00
|
|
|
if (!service) return false;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIEventTarget> target = service->Thread();
|
|
|
|
if (!target) return false;
|
|
|
|
|
|
|
|
bool currentThread;
|
|
|
|
nsresult rv = target->IsOnCurrentThread(¤tThread);
|
|
|
|
return NS_SUCCEEDED(rv) && currentThread;
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<nsIEventTarget> CacheStorageService::Thread() const {
|
|
|
|
return CacheFileIOManager::IOTarget();
|
|
|
|
}
|
|
|
|
|
2013-09-20 13:11:25 +04:00
|
|
|
nsresult CacheStorageService::Dispatch(nsIRunnable* aEvent) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<CacheIOThread> cacheIOThread = CacheFileIOManager::IOThread();
|
2013-09-20 13:11:25 +04:00
|
|
|
if (!cacheIOThread) return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
|
2014-01-22 21:54:52 +04:00
|
|
|
return cacheIOThread->Dispatch(aEvent, CacheIOThread::MANAGEMENT);
|
2013-09-20 13:11:25 +04:00
|
|
|
}
|
|
|
|
|
2019-12-02 22:47:05 +03:00
|
|
|
namespace CacheStorageEvictHelper {
|
|
|
|
|
|
|
|
nsresult ClearStorage(bool const aPrivate, bool const aAnonymous,
|
|
|
|
OriginAttributes& aOa) {
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
aOa.SyncAttributesWithPrivateBrowsing(aPrivate);
|
|
|
|
RefPtr<LoadContextInfo> info = GetLoadContextInfo(aAnonymous, aOa);
|
|
|
|
|
|
|
|
nsCOMPtr<nsICacheStorage> storage;
|
|
|
|
RefPtr<CacheStorageService> service = CacheStorageService::Self();
|
|
|
|
NS_ENSURE_TRUE(service, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
// Clear disk storage
|
2021-04-30 10:20:24 +03:00
|
|
|
rv = service->DiskCacheStorage(info, getter_AddRefs(storage));
|
2019-12-02 22:47:05 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
rv = storage->AsyncEvictStorage(nullptr);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// Clear memory storage
|
|
|
|
rv = service->MemoryCacheStorage(info, getter_AddRefs(storage));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
rv = storage->AsyncEvictStorage(nullptr);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult Run(OriginAttributes& aOa) {
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
// Clear all [private X anonymous] combinations
|
|
|
|
rv = ClearStorage(false, false, aOa);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
rv = ClearStorage(false, true, aOa);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
rv = ClearStorage(true, false, aOa);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
rv = ClearStorage(true, true, aOa);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace CacheStorageEvictHelper
|
|
|
|
|
2013-09-20 13:11:25 +04:00
|
|
|
// nsICacheStorageService
|
|
|
|
|
|
|
|
NS_IMETHODIMP CacheStorageService::MemoryCacheStorage(
|
|
|
|
nsILoadContextInfo* aLoadContextInfo, nsICacheStorage** _retval) {
|
|
|
|
NS_ENSURE_ARG(_retval);
|
|
|
|
|
2017-08-24 11:25:38 +03:00
|
|
|
nsCOMPtr<nsICacheStorage> storage =
|
2021-04-30 10:20:24 +03:00
|
|
|
new CacheStorage(aLoadContextInfo, false, false, false);
|
2013-09-20 13:11:25 +04:00
|
|
|
storage.forget(_retval);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP CacheStorageService::DiskCacheStorage(
|
2021-04-30 10:20:24 +03:00
|
|
|
nsILoadContextInfo* aLoadContextInfo, nsICacheStorage** _retval) {
|
2013-09-20 13:11:25 +04:00
|
|
|
NS_ENSURE_ARG(_retval);
|
|
|
|
|
|
|
|
// TODO save some heap granularity - cache commonly used storages.
|
|
|
|
|
2014-02-04 04:53:00 +04:00
|
|
|
// When disk cache is disabled, still provide a storage, but just keep stuff
|
|
|
|
// in memory.
|
|
|
|
bool useDisk = CacheObserver::UseDiskCache();
|
|
|
|
|
2021-04-30 10:20:24 +03:00
|
|
|
nsCOMPtr<nsICacheStorage> storage = new CacheStorage(
|
|
|
|
aLoadContextInfo, useDisk, false /* size limit */, false /* don't pin */);
|
2013-09-20 13:11:25 +04:00
|
|
|
storage.forget(_retval);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-10-22 13:11:00 +03:00
|
|
|
NS_IMETHODIMP CacheStorageService::PinningCacheStorage(
|
|
|
|
nsILoadContextInfo* aLoadContextInfo, nsICacheStorage** _retval) {
|
|
|
|
NS_ENSURE_ARG(aLoadContextInfo);
|
|
|
|
NS_ENSURE_ARG(_retval);
|
|
|
|
|
|
|
|
// When disk cache is disabled don't pretend we cache.
|
|
|
|
if (!CacheObserver::UseDiskCache()) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
2021-04-30 10:20:24 +03:00
|
|
|
nsCOMPtr<nsICacheStorage> storage =
|
|
|
|
new CacheStorage(aLoadContextInfo, true /* use disk */,
|
|
|
|
true /* ignore size checks */, true /* pin */);
|
2015-10-22 13:11:00 +03:00
|
|
|
storage.forget(_retval);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-09-20 13:11:25 +04:00
|
|
|
NS_IMETHODIMP CacheStorageService::Clear() {
|
|
|
|
nsresult rv;
|
|
|
|
|
2017-08-24 11:25:38 +03:00
|
|
|
// Tell the index to block notification to AsyncGetDiskConsumption.
|
|
|
|
// Will be allowed again from CacheFileContextEvictor::EvictEntries()
|
|
|
|
// when all the context have been removed from disk.
|
|
|
|
CacheIndex::OnAsyncEviction(true);
|
2016-02-19 13:56:00 +03:00
|
|
|
|
2017-08-24 11:25:38 +03:00
|
|
|
mozilla::MutexAutoLock lock(mLock);
|
2013-09-20 13:11:25 +04:00
|
|
|
|
2017-08-24 11:25:38 +03:00
|
|
|
{
|
|
|
|
mozilla::MutexAutoLock forcedValidEntriesLock(mForcedValidEntriesLock);
|
|
|
|
mForcedValidEntries.Clear();
|
|
|
|
}
|
2013-09-20 13:11:25 +04:00
|
|
|
|
2017-08-24 11:25:38 +03:00
|
|
|
NS_ENSURE_TRUE(!mShutdown, NS_ERROR_NOT_INITIALIZED);
|
2013-09-20 13:11:25 +04:00
|
|
|
|
2021-03-24 20:51:12 +03:00
|
|
|
const auto keys = ToTArray<nsTArray<nsCString>>(sGlobalEntryTables->Keys());
|
|
|
|
for (const auto& key : keys) {
|
|
|
|
DoomStorageEntries(key, nullptr, true, false, nullptr);
|
2013-09-20 13:11:25 +04:00
|
|
|
}
|
|
|
|
|
2017-08-24 11:25:38 +03:00
|
|
|
// Passing null as a load info means to evict all contexts.
|
|
|
|
// EvictByContext() respects the entry pinning. EvictAll() does not.
|
2020-09-23 18:17:15 +03:00
|
|
|
rv = CacheFileIOManager::EvictByContext(nullptr, false, u""_ns);
|
2018-07-20 14:57:18 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP CacheStorageService::ClearOrigin(nsIPrincipal* aPrincipal) {
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
if (NS_WARN_IF(!aPrincipal)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoString origin;
|
|
|
|
rv = nsContentUtils::GetUTFOrigin(aPrincipal, origin);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = ClearOriginInternal(origin, aPrincipal->OriginAttributesRef(), true);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = ClearOriginInternal(origin, aPrincipal->OriginAttributesRef(), false);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2019-12-02 22:47:05 +03:00
|
|
|
NS_IMETHODIMP CacheStorageService::ClearOriginAttributes(
|
|
|
|
const nsAString& aOriginAttributes) {
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
if (NS_WARN_IF(aOriginAttributes.IsEmpty())) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
OriginAttributes oa;
|
|
|
|
if (!oa.Init(aOriginAttributes)) {
|
|
|
|
NS_ERROR("Could not parse the argument for OriginAttributes");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = CacheStorageEvictHelper::Run(oa);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2018-07-20 14:57:18 +03:00
|
|
|
static bool RemoveExactEntry(CacheEntryTable* aEntries, nsACString const& aKey,
|
|
|
|
CacheEntry* aEntry, bool aOverwrite) {
|
|
|
|
RefPtr<CacheEntry> existingEntry;
|
|
|
|
if (!aEntries->Get(aKey, getter_AddRefs(existingEntry))) {
|
|
|
|
LOG(("RemoveExactEntry [entry=%p already gone]", aEntry));
|
|
|
|
return false; // Already removed...
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!aOverwrite && existingEntry != aEntry) {
|
|
|
|
LOG(("RemoveExactEntry [entry=%p already replaced]", aEntry));
|
|
|
|
return false; // Already replaced...
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG(("RemoveExactEntry [entry=%p removed]", aEntry));
|
|
|
|
aEntries->Remove(aKey);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-06-24 12:13:30 +03:00
|
|
|
NS_IMETHODIMP CacheStorageService::ClearBaseDomain(
|
|
|
|
const nsAString& aBaseDomain) {
|
|
|
|
if (sGlobalEntryTables) {
|
|
|
|
mozilla::MutexAutoLock lock(mLock);
|
|
|
|
|
|
|
|
if (mShutdown) return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
|
|
|
|
nsCString cBaseDomain = NS_ConvertUTF16toUTF8(aBaseDomain);
|
|
|
|
|
|
|
|
nsTArray<nsCString> keys;
|
|
|
|
for (const auto& globalEntry : *sGlobalEntryTables) {
|
|
|
|
// Match by partitionKey base domain. This should cover most cache entries
|
|
|
|
// because we statically partition the cache. Most first party cache
|
|
|
|
// entries will also have a partitionKey set where the partitionKey base
|
|
|
|
// domain will match the entry URI base domain.
|
|
|
|
const nsACString& key = globalEntry.GetKey();
|
|
|
|
nsCOMPtr<nsILoadContextInfo> info =
|
|
|
|
CacheFileUtils::ParseKey(globalEntry.GetKey());
|
|
|
|
|
2021-07-07 19:05:06 +03:00
|
|
|
if (info &&
|
|
|
|
StoragePrincipalHelper::PartitionKeyHasBaseDomain(
|
|
|
|
info->OriginAttributesPtr()->mPartitionKey, aBaseDomain)) {
|
|
|
|
keys.AppendElement(key);
|
|
|
|
continue;
|
2021-06-24 12:13:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// If we didn't get a partitionKey match, try to match by entry URI. This
|
|
|
|
// requires us to iterate over all entries.
|
|
|
|
CacheEntryTable* table = globalEntry.GetWeak();
|
|
|
|
MOZ_ASSERT(table);
|
|
|
|
|
|
|
|
nsTArray<RefPtr<CacheEntry>> entriesToDelete;
|
|
|
|
|
|
|
|
for (CacheEntry* entry : table->Values()) {
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
nsresult rv = NS_NewURI(getter_AddRefs(uri), entry->GetURI());
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoCString host;
|
|
|
|
rv = uri->GetHost(host);
|
|
|
|
// Some entries may not have valid hosts. We can skip them.
|
|
|
|
if (NS_FAILED(rv) || host.IsEmpty()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool hasRootDomain = false;
|
2022-08-31 14:19:14 +03:00
|
|
|
rv = HasRootDomain(host, cBaseDomain, &hasRootDomain);
|
2021-06-24 12:13:30 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (hasRootDomain) {
|
|
|
|
entriesToDelete.AppendElement(entry);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clear individual matched entries.
|
|
|
|
for (RefPtr<CacheEntry>& entry : entriesToDelete) {
|
|
|
|
nsAutoCString entryKey;
|
|
|
|
nsresult rv = entry->HashingKey(entryKey);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_ERROR("aEntry->HashingKey() failed?");
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
RemoveExactEntry(table, entryKey, entry, false /* don't overwrite */);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clear matched keys.
|
|
|
|
for (uint32_t i = 0; i < keys.Length(); ++i) {
|
|
|
|
DoomStorageEntries(keys[i], nullptr, true, false, nullptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return CacheFileIOManager::EvictByContext(nullptr, false /* pinned */, u""_ns,
|
|
|
|
aBaseDomain);
|
|
|
|
}
|
|
|
|
|
2018-07-20 14:57:18 +03:00
|
|
|
nsresult CacheStorageService::ClearOriginInternal(
|
|
|
|
const nsAString& aOrigin, const OriginAttributes& aOriginAttributes,
|
|
|
|
bool aAnonymous) {
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
RefPtr<LoadContextInfo> info =
|
|
|
|
GetLoadContextInfo(aAnonymous, aOriginAttributes);
|
|
|
|
if (NS_WARN_IF(!info)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
mozilla::MutexAutoLock lock(mLock);
|
|
|
|
|
|
|
|
if (sGlobalEntryTables) {
|
2021-03-17 18:49:46 +03:00
|
|
|
for (const auto& globalEntry : *sGlobalEntryTables) {
|
2018-07-20 14:57:18 +03:00
|
|
|
bool matches = false;
|
2021-03-17 18:49:46 +03:00
|
|
|
rv = CacheFileUtils::KeyMatchesLoadContextInfo(globalEntry.GetKey(), info,
|
|
|
|
&matches);
|
2018-07-20 14:57:18 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
if (!matches) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-03-17 18:49:46 +03:00
|
|
|
CacheEntryTable* table = globalEntry.GetWeak();
|
2018-07-20 14:57:18 +03:00
|
|
|
MOZ_ASSERT(table);
|
|
|
|
|
|
|
|
nsTArray<RefPtr<CacheEntry>> entriesToDelete;
|
|
|
|
|
2021-03-24 20:56:49 +03:00
|
|
|
for (CacheEntry* entry : table->Values()) {
|
2018-07-20 14:57:18 +03:00
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
rv = NS_NewURI(getter_AddRefs(uri), entry->GetURI());
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsAutoString origin;
|
|
|
|
rv = nsContentUtils::GetUTFOrigin(uri, origin);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (origin != aOrigin) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
entriesToDelete.AppendElement(entry);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (RefPtr<CacheEntry>& entry : entriesToDelete) {
|
|
|
|
nsAutoCString entryKey;
|
|
|
|
rv = entry->HashingKey(entryKey);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_ERROR("aEntry->HashingKey() failed?");
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
RemoveExactEntry(table, entryKey, entry, false /* don't overwrite */);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = CacheFileIOManager::EvictByContext(info, false /* pinned */, aOrigin);
|
2017-08-24 11:25:38 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2013-09-20 13:11:25 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP CacheStorageService::PurgeFromMemory(uint32_t aWhat) {
|
|
|
|
uint32_t what;
|
|
|
|
|
|
|
|
switch (aWhat) {
|
|
|
|
case PURGE_DISK_DATA_ONLY:
|
|
|
|
what = CacheEntry::PURGE_DATA_ONLY_DISK_BACKED;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PURGE_DISK_ALL:
|
|
|
|
what = CacheEntry::PURGE_WHOLE_ONLY_DISK_BACKED;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PURGE_EVERYTHING:
|
|
|
|
what = CacheEntry::PURGE_WHOLE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIRunnable> event = new PurgeFromMemoryRunnable(this, what);
|
|
|
|
|
|
|
|
return Dispatch(event);
|
|
|
|
}
|
|
|
|
|
2015-10-22 13:11:00 +03:00
|
|
|
NS_IMETHODIMP CacheStorageService::PurgeFromMemoryRunnable::Run() {
|
|
|
|
if (NS_IsMainThread()) {
|
|
|
|
nsCOMPtr<nsIObserverService> observerService =
|
|
|
|
mozilla::services::GetObserverService();
|
|
|
|
if (observerService) {
|
|
|
|
observerService->NotifyObservers(
|
|
|
|
nullptr, "cacheservice:purge-memory-pools", nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mService) {
|
|
|
|
// TODO not all flags apply to both pools
|
|
|
|
mService->Pool(true).PurgeAll(mWhat);
|
|
|
|
mService->Pool(false).PurgeAll(mWhat);
|
|
|
|
mService = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_DispatchToMainThread(this);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-04-06 22:44:40 +04:00
|
|
|
NS_IMETHODIMP CacheStorageService::AsyncGetDiskConsumption(
|
|
|
|
nsICacheStorageConsumptionObserver* aObserver) {
|
|
|
|
NS_ENSURE_ARG(aObserver);
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
|
2017-08-24 11:25:38 +03:00
|
|
|
rv = CacheIndex::AsyncGetDiskConsumption(aObserver);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2014-04-06 22:44:40 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-09-20 13:11:25 +04:00
|
|
|
NS_IMETHODIMP CacheStorageService::GetIoTarget(nsIEventTarget** aEventTarget) {
|
|
|
|
NS_ENSURE_ARG(aEventTarget);
|
|
|
|
|
2017-08-24 11:25:38 +03:00
|
|
|
nsCOMPtr<nsIEventTarget> ioTarget = CacheFileIOManager::IOTarget();
|
|
|
|
ioTarget.forget(aEventTarget);
|
2013-09-20 13:11:25 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2017-02-03 06:15:00 +03:00
|
|
|
NS_IMETHODIMP CacheStorageService::AsyncVisitAllStorages(
|
|
|
|
nsICacheStorageVisitor* aVisitor, bool aVisitEntries) {
|
|
|
|
LOG(("CacheStorageService::AsyncVisitAllStorages [cb=%p]", aVisitor));
|
|
|
|
NS_ENSURE_FALSE(mShutdown, NS_ERROR_NOT_INITIALIZED);
|
|
|
|
|
|
|
|
// Walking the disk cache also walks the memory cache.
|
|
|
|
RefPtr<WalkDiskCacheRunnable> event =
|
|
|
|
new WalkDiskCacheRunnable(nullptr, aVisitEntries, aVisitor);
|
|
|
|
return event->Walk();
|
|
|
|
}
|
|
|
|
|
2013-09-20 13:11:25 +04:00
|
|
|
// Methods used by CacheEntry for management of in-memory structures.
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
namespace {
|
2013-09-20 13:11:25 +04:00
|
|
|
|
|
|
|
class FrecencyComparator {
|
|
|
|
public:
|
|
|
|
bool Equals(CacheEntry* a, CacheEntry* b) const {
|
|
|
|
return a->GetFrecency() == b->GetFrecency();
|
|
|
|
}
|
|
|
|
bool LessThan(CacheEntry* a, CacheEntry* b) const {
|
2020-06-19 08:43:42 +03:00
|
|
|
// We deliberately want to keep the '0' frecency entries at the tail of the
|
|
|
|
// aray, because these are new entries and would just slow down purging of
|
|
|
|
// the pools based on frecency.
|
|
|
|
if (a->GetFrecency() == 0.0 && b->GetFrecency() > 0.0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (a->GetFrecency() > 0.0 && b->GetFrecency() == 0.0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-09-20 13:11:25 +04:00
|
|
|
return a->GetFrecency() < b->GetFrecency();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class ExpirationComparator {
|
|
|
|
public:
|
|
|
|
bool Equals(CacheEntry* a, CacheEntry* b) const {
|
|
|
|
return a->GetExpirationTime() == b->GetExpirationTime();
|
|
|
|
}
|
|
|
|
bool LessThan(CacheEntry* a, CacheEntry* b) const {
|
|
|
|
return a->GetExpirationTime() < b->GetExpirationTime();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace
|
2013-09-20 13:11:25 +04:00
|
|
|
|
|
|
|
void CacheStorageService::RegisterEntry(CacheEntry* aEntry) {
|
|
|
|
MOZ_ASSERT(IsOnManagementThread());
|
|
|
|
|
|
|
|
if (mShutdown || !aEntry->CanRegister()) return;
|
|
|
|
|
2014-07-08 18:48:00 +04:00
|
|
|
TelemetryRecordEntryCreation(aEntry);
|
|
|
|
|
2013-09-20 13:11:25 +04:00
|
|
|
LOG(("CacheStorageService::RegisterEntry [entry=%p]", aEntry));
|
|
|
|
|
2014-04-06 22:45:18 +04:00
|
|
|
MemoryPool& pool = Pool(aEntry->IsUsingDisk());
|
2015-08-18 01:18:59 +03:00
|
|
|
pool.mFrecencyArray.AppendElement(aEntry);
|
|
|
|
pool.mExpirationArray.AppendElement(aEntry);
|
2013-09-20 13:11:25 +04:00
|
|
|
|
|
|
|
aEntry->SetRegistered(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CacheStorageService::UnregisterEntry(CacheEntry* aEntry) {
|
|
|
|
MOZ_ASSERT(IsOnManagementThread());
|
|
|
|
|
|
|
|
if (!aEntry->IsRegistered()) return;
|
|
|
|
|
2014-07-08 18:48:00 +04:00
|
|
|
TelemetryRecordEntryRemoval(aEntry);
|
|
|
|
|
2013-09-20 13:11:25 +04:00
|
|
|
LOG(("CacheStorageService::UnregisterEntry [entry=%p]", aEntry));
|
|
|
|
|
2014-04-06 22:45:18 +04:00
|
|
|
MemoryPool& pool = Pool(aEntry->IsUsingDisk());
|
|
|
|
mozilla::DebugOnly<bool> removedFrecency =
|
|
|
|
pool.mFrecencyArray.RemoveElement(aEntry);
|
|
|
|
mozilla::DebugOnly<bool> removedExpiration =
|
|
|
|
pool.mExpirationArray.RemoveElement(aEntry);
|
2013-09-20 13:11:25 +04:00
|
|
|
|
|
|
|
MOZ_ASSERT(mShutdown || (removedFrecency && removedExpiration));
|
|
|
|
|
|
|
|
// Note: aEntry->CanRegister() since now returns false
|
|
|
|
aEntry->SetRegistered(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool AddExactEntry(CacheEntryTable* aEntries, nsACString const& aKey,
|
|
|
|
CacheEntry* aEntry, bool aOverwrite) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<CacheEntry> existingEntry;
|
2013-09-20 13:11:25 +04:00
|
|
|
if (!aOverwrite && aEntries->Get(aKey, getter_AddRefs(existingEntry))) {
|
|
|
|
bool equals = existingEntry == aEntry;
|
|
|
|
LOG(("AddExactEntry [entry=%p equals=%d]", aEntry, equals));
|
|
|
|
return equals; // Already there...
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG(("AddExactEntry [entry=%p put]", aEntry));
|
2021-02-26 12:11:46 +03:00
|
|
|
aEntries->InsertOrUpdate(aKey, RefPtr{aEntry});
|
2013-09-20 13:11:25 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-01-07 00:24:41 +04:00
|
|
|
bool CacheStorageService::RemoveEntry(CacheEntry* aEntry,
|
|
|
|
bool aOnlyUnreferenced) {
|
2013-09-20 13:11:25 +04:00
|
|
|
LOG(("CacheStorageService::RemoveEntry [entry=%p]", aEntry));
|
|
|
|
|
|
|
|
nsAutoCString entryKey;
|
|
|
|
nsresult rv = aEntry->HashingKey(entryKey);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_ERROR("aEntry->HashingKey() failed?");
|
2014-01-07 00:24:41 +04:00
|
|
|
return false;
|
2013-09-20 13:11:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
mozilla::MutexAutoLock lock(mLock);
|
|
|
|
|
|
|
|
if (mShutdown) {
|
|
|
|
LOG((" after shutdown"));
|
2014-01-07 00:24:41 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-07-21 22:34:17 +04:00
|
|
|
if (aOnlyUnreferenced) {
|
|
|
|
if (aEntry->IsReferenced()) {
|
|
|
|
LOG((" still referenced, not removing"));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-02-19 13:56:00 +03:00
|
|
|
if (!aEntry->IsUsingDisk() &&
|
|
|
|
IsForcedValidEntry(aEntry->GetStorageID(), entryKey)) {
|
2014-07-21 22:34:17 +04:00
|
|
|
LOG((" forced valid, not removing"));
|
|
|
|
return false;
|
|
|
|
}
|
2013-09-20 13:11:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
CacheEntryTable* entries;
|
2021-06-04 15:35:16 +03:00
|
|
|
if (sGlobalEntryTables->Get(aEntry->GetStorageID(), &entries)) {
|
2013-09-20 13:11:25 +04:00
|
|
|
RemoveExactEntry(entries, entryKey, aEntry, false /* don't overwrite */);
|
2021-06-04 15:35:16 +03:00
|
|
|
}
|
2013-09-20 13:11:25 +04:00
|
|
|
|
|
|
|
nsAutoCString memoryStorageID(aEntry->GetStorageID());
|
2019-03-20 16:54:57 +03:00
|
|
|
AppendMemoryStorageTag(memoryStorageID);
|
2013-09-20 13:11:25 +04:00
|
|
|
|
2021-06-04 15:35:16 +03:00
|
|
|
if (sGlobalEntryTables->Get(memoryStorageID, &entries)) {
|
2013-09-20 13:11:25 +04:00
|
|
|
RemoveExactEntry(entries, entryKey, aEntry, false /* don't overwrite */);
|
2021-06-04 15:35:16 +03:00
|
|
|
}
|
2014-01-07 00:24:41 +04:00
|
|
|
|
|
|
|
return true;
|
2013-09-20 13:11:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void CacheStorageService::RecordMemoryOnlyEntry(CacheEntry* aEntry,
|
|
|
|
bool aOnlyInMemory,
|
|
|
|
bool aOverwrite) {
|
|
|
|
LOG(
|
|
|
|
("CacheStorageService::RecordMemoryOnlyEntry [entry=%p, memory=%d, "
|
|
|
|
"overwrite=%d]",
|
|
|
|
aEntry, aOnlyInMemory, aOverwrite));
|
|
|
|
// This method is responsible to put this entry to a special record hashtable
|
|
|
|
// that contains only entries that are stored in memory.
|
|
|
|
// Keep in mind that every entry, regardless of whether is in-memory-only or
|
|
|
|
// not is always recorded in the storage master hash table, the one identified
|
|
|
|
// by CacheEntry.StorageID().
|
|
|
|
|
|
|
|
mLock.AssertCurrentThreadOwns();
|
|
|
|
|
|
|
|
if (mShutdown) {
|
|
|
|
LOG((" after shutdown"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
nsAutoCString entryKey;
|
|
|
|
rv = aEntry->HashingKey(entryKey);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_ERROR("aEntry->HashingKey() failed?");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
CacheEntryTable* entries = nullptr;
|
|
|
|
nsAutoCString memoryStorageID(aEntry->GetStorageID());
|
2019-03-20 16:54:57 +03:00
|
|
|
AppendMemoryStorageTag(memoryStorageID);
|
2013-09-20 13:11:25 +04:00
|
|
|
|
|
|
|
if (!sGlobalEntryTables->Get(memoryStorageID, &entries)) {
|
|
|
|
if (!aOnlyInMemory) {
|
|
|
|
LOG((" not recorded as memory only"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-02-26 12:11:46 +03:00
|
|
|
entries = sGlobalEntryTables
|
|
|
|
->InsertOrUpdate(
|
|
|
|
memoryStorageID,
|
|
|
|
MakeUnique<CacheEntryTable>(CacheEntryTable::MEMORY_ONLY))
|
|
|
|
.get();
|
2013-09-20 13:11:25 +04:00
|
|
|
LOG((" new memory-only storage table for %s", memoryStorageID.get()));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aOnlyInMemory) {
|
|
|
|
AddExactEntry(entries, entryKey, aEntry, aOverwrite);
|
|
|
|
} else {
|
|
|
|
RemoveExactEntry(entries, entryKey, aEntry, aOverwrite);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-21 22:34:17 +04:00
|
|
|
// Checks if a cache entry is forced valid (will be loaded directly from cache
|
|
|
|
// without further validation) - see nsICacheEntry.idl for further details
|
2016-02-19 13:56:00 +03:00
|
|
|
bool CacheStorageService::IsForcedValidEntry(nsACString const& aContextKey,
|
|
|
|
nsACString const& aEntryKey) {
|
|
|
|
return IsForcedValidEntry(aContextKey + aEntryKey);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CacheStorageService::IsForcedValidEntry(
|
|
|
|
nsACString const& aContextEntryKey) {
|
2014-09-10 16:43:50 +04:00
|
|
|
mozilla::MutexAutoLock lock(mForcedValidEntriesLock);
|
|
|
|
|
2021-10-12 18:41:15 +03:00
|
|
|
ForcedValidData data;
|
2014-07-21 22:34:17 +04:00
|
|
|
|
2021-10-12 18:41:15 +03:00
|
|
|
if (!mForcedValidEntries.Get(aContextEntryKey, &data)) {
|
2014-07-21 22:34:17 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-10-12 18:41:15 +03:00
|
|
|
if (data.validUntil.IsNull()) {
|
|
|
|
MOZ_ASSERT_UNREACHABLE("the timeStamp should never be null");
|
2014-07-21 22:34:17 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Entry timeout not reached yet
|
2021-10-12 18:41:15 +03:00
|
|
|
if (TimeStamp::NowLoRes() <= data.validUntil) {
|
2014-07-21 22:34:17 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Entry timeout has been reached
|
2016-02-19 13:56:00 +03:00
|
|
|
mForcedValidEntries.Remove(aContextEntryKey);
|
2021-10-12 18:41:15 +03:00
|
|
|
|
|
|
|
if (!data.viewed) {
|
|
|
|
Telemetry::AccumulateCategorical(
|
|
|
|
Telemetry::LABELS_PREDICTOR_PREFETCH_USE_STATUS::WaitedTooLong);
|
|
|
|
}
|
2014-07-21 22:34:17 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-10-12 18:41:15 +03:00
|
|
|
void CacheStorageService::MarkForcedValidEntryUse(nsACString const& aContextKey,
|
|
|
|
nsACString const& aEntryKey) {
|
|
|
|
mozilla::MutexAutoLock lock(mForcedValidEntriesLock);
|
|
|
|
|
|
|
|
ForcedValidData data;
|
|
|
|
|
|
|
|
if (!mForcedValidEntries.Get(aContextKey + aEntryKey, &data)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
data.viewed = true;
|
|
|
|
mForcedValidEntries.InsertOrUpdate(aContextKey + aEntryKey, data);
|
|
|
|
}
|
|
|
|
|
2014-07-21 22:34:17 +04:00
|
|
|
// Allows a cache entry to be loaded directly from cache without further
|
|
|
|
// validation - see nsICacheEntry.idl for further details
|
2016-02-19 13:56:00 +03:00
|
|
|
void CacheStorageService::ForceEntryValidFor(nsACString const& aContextKey,
|
|
|
|
nsACString const& aEntryKey,
|
2014-07-21 22:34:17 +04:00
|
|
|
uint32_t aSecondsToTheFuture) {
|
2014-09-10 16:43:50 +04:00
|
|
|
mozilla::MutexAutoLock lock(mForcedValidEntriesLock);
|
2014-07-21 22:34:17 +04:00
|
|
|
|
2014-07-15 20:23:09 +04:00
|
|
|
TimeStamp now = TimeStamp::NowLoRes();
|
|
|
|
ForcedValidEntriesPrune(now);
|
|
|
|
|
2021-10-12 18:41:15 +03:00
|
|
|
ForcedValidData data;
|
|
|
|
data.validUntil = now + TimeDuration::FromSeconds(aSecondsToTheFuture);
|
|
|
|
data.viewed = false;
|
2014-07-21 22:34:17 +04:00
|
|
|
|
2021-10-12 18:41:15 +03:00
|
|
|
mForcedValidEntries.InsertOrUpdate(aContextKey + aEntryKey, data);
|
2016-02-19 13:56:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void CacheStorageService::RemoveEntryForceValid(nsACString const& aContextKey,
|
|
|
|
nsACString const& aEntryKey) {
|
|
|
|
mozilla::MutexAutoLock lock(mForcedValidEntriesLock);
|
|
|
|
|
2016-04-15 05:26:58 +03:00
|
|
|
LOG(("CacheStorageService::RemoveEntryForceValid context='%s' entryKey=%s",
|
|
|
|
aContextKey.BeginReading(), aEntryKey.BeginReading()));
|
2021-10-12 18:41:15 +03:00
|
|
|
ForcedValidData data;
|
|
|
|
bool ok = mForcedValidEntries.Get(aContextKey + aEntryKey, &data);
|
|
|
|
if (ok && !data.viewed) {
|
|
|
|
Telemetry::AccumulateCategorical(
|
|
|
|
Telemetry::LABELS_PREDICTOR_PREFETCH_USE_STATUS::WaitedTooLong);
|
|
|
|
}
|
2016-02-19 13:56:00 +03:00
|
|
|
mForcedValidEntries.Remove(aContextKey + aEntryKey);
|
2014-07-21 22:34:17 +04:00
|
|
|
}
|
|
|
|
|
2014-07-15 20:23:09 +04:00
|
|
|
// Cleans out the old entries in mForcedValidEntries
|
|
|
|
void CacheStorageService::ForcedValidEntriesPrune(TimeStamp& now) {
|
|
|
|
static TimeDuration const oneMinute = TimeDuration::FromSeconds(60);
|
|
|
|
static TimeStamp dontPruneUntil = now + oneMinute;
|
|
|
|
if (now < dontPruneUntil) return;
|
|
|
|
|
2015-12-04 00:59:58 +03:00
|
|
|
for (auto iter = mForcedValidEntries.Iter(); !iter.Done(); iter.Next()) {
|
2021-10-12 18:41:15 +03:00
|
|
|
if (iter.Data().validUntil < now) {
|
|
|
|
if (!iter.Data().viewed) {
|
|
|
|
Telemetry::AccumulateCategorical(
|
|
|
|
Telemetry::LABELS_PREDICTOR_PREFETCH_USE_STATUS::WaitedTooLong);
|
|
|
|
}
|
2015-12-04 00:59:58 +03:00
|
|
|
iter.Remove();
|
|
|
|
}
|
|
|
|
}
|
2014-07-15 20:23:09 +04:00
|
|
|
dontPruneUntil = now + oneMinute;
|
|
|
|
}
|
|
|
|
|
2013-09-20 13:11:25 +04:00
|
|
|
void CacheStorageService::OnMemoryConsumptionChange(
|
|
|
|
CacheMemoryConsumer* aConsumer, uint32_t aCurrentMemoryConsumption) {
|
|
|
|
LOG(("CacheStorageService::OnMemoryConsumptionChange [consumer=%p, size=%u]",
|
|
|
|
aConsumer, aCurrentMemoryConsumption));
|
|
|
|
|
2021-01-28 14:05:02 +03:00
|
|
|
uint32_t savedMemorySize = aConsumer->LoadReportedMemoryConsumption();
|
2013-09-20 13:11:25 +04:00
|
|
|
if (savedMemorySize == aCurrentMemoryConsumption) return;
|
|
|
|
|
|
|
|
// Exchange saved size with current one.
|
2021-01-28 14:05:02 +03:00
|
|
|
aConsumer->StoreReportedMemoryConsumption(aCurrentMemoryConsumption);
|
2013-09-20 13:11:25 +04:00
|
|
|
|
2021-01-28 14:05:02 +03:00
|
|
|
bool usingDisk = !(aConsumer->LoadFlags() & CacheMemoryConsumer::MEMORY_ONLY);
|
2014-04-06 22:45:18 +04:00
|
|
|
bool overLimit = Pool(usingDisk).OnMemoryConsumptionChange(
|
|
|
|
savedMemorySize, aCurrentMemoryConsumption);
|
2013-09-20 13:11:25 +04:00
|
|
|
|
2014-04-06 22:45:18 +04:00
|
|
|
if (!overLimit) return;
|
2013-09-20 13:11:25 +04:00
|
|
|
|
2020-02-17 21:44:21 +03:00
|
|
|
// It's likely the timer has already been set when we get here,
|
|
|
|
// check outside the lock to save resources.
|
|
|
|
#ifdef MOZ_TSAN
|
|
|
|
if (mPurgeTimerActive) {
|
|
|
|
#else
|
|
|
|
if (mPurgeTimer) {
|
|
|
|
#endif
|
|
|
|
return;
|
|
|
|
}
|
2013-09-20 13:11:25 +04:00
|
|
|
|
2014-04-06 22:45:18 +04:00
|
|
|
// We don't know if this is called under the service lock or not,
|
|
|
|
// hence rather dispatch.
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsIEventTarget> cacheIOTarget = Thread();
|
2014-04-06 22:45:18 +04:00
|
|
|
if (!cacheIOTarget) return;
|
2013-09-20 13:11:25 +04:00
|
|
|
|
2014-04-06 22:45:18 +04:00
|
|
|
// Dispatch as a priority task, we want to set the purge timer
|
|
|
|
// ASAP to prevent vain redispatch of this event.
|
|
|
|
nsCOMPtr<nsIRunnable> event = NewRunnableMethod(
|
2017-06-12 22:34:10 +03:00
|
|
|
"net::CacheStorageService::SchedulePurgeOverMemoryLimit", this,
|
|
|
|
&CacheStorageService::SchedulePurgeOverMemoryLimit);
|
2014-04-06 22:45:18 +04:00
|
|
|
cacheIOTarget->Dispatch(event, nsIEventTarget::DISPATCH_NORMAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CacheStorageService::MemoryPool::OnMemoryConsumptionChange(
|
|
|
|
uint32_t aSavedMemorySize, uint32_t aCurrentMemoryConsumption) {
|
|
|
|
mMemorySize -= aSavedMemorySize;
|
|
|
|
mMemorySize += aCurrentMemoryConsumption;
|
|
|
|
|
|
|
|
LOG((" mMemorySize=%u (+%u,-%u)", uint32_t(mMemorySize),
|
|
|
|
aCurrentMemoryConsumption, aSavedMemorySize));
|
|
|
|
|
|
|
|
// Bypass purging when memory has not grew up significantly
|
|
|
|
if (aCurrentMemoryConsumption <= aSavedMemorySize) return false;
|
|
|
|
|
|
|
|
return mMemorySize > Limit();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CacheStorageService::SchedulePurgeOverMemoryLimit() {
|
2016-09-20 13:53:00 +03:00
|
|
|
LOG(("CacheStorageService::SchedulePurgeOverMemoryLimit"));
|
|
|
|
|
2014-04-06 22:45:18 +04:00
|
|
|
mozilla::MutexAutoLock lock(mLock);
|
|
|
|
|
2016-09-20 13:53:00 +03:00
|
|
|
if (mShutdown) {
|
|
|
|
LOG((" past shutdown"));
|
2013-09-20 13:11:25 +04:00
|
|
|
return;
|
2016-09-20 13:53:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mPurgeTimer) {
|
|
|
|
LOG((" timer already up"));
|
|
|
|
return;
|
|
|
|
}
|
2013-09-20 13:11:25 +04:00
|
|
|
|
2017-10-16 09:12:02 +03:00
|
|
|
mPurgeTimer = NS_NewTimer();
|
2016-09-20 13:53:00 +03:00
|
|
|
if (mPurgeTimer) {
|
2020-02-17 21:44:21 +03:00
|
|
|
#ifdef MOZ_TSAN
|
|
|
|
mPurgeTimerActive = true;
|
|
|
|
#endif
|
2016-09-20 13:53:00 +03:00
|
|
|
nsresult rv;
|
|
|
|
rv = mPurgeTimer->InitWithCallback(this, 1000, nsITimer::TYPE_ONE_SHOT);
|
2016-12-16 06:16:31 +03:00
|
|
|
LOG((" timer init rv=0x%08" PRIx32, static_cast<uint32_t>(rv)));
|
2016-09-20 13:53:00 +03:00
|
|
|
}
|
2014-04-06 22:45:18 +04:00
|
|
|
}
|
2013-09-20 13:11:25 +04:00
|
|
|
|
2014-04-06 22:45:18 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
CacheStorageService::Notify(nsITimer* aTimer) {
|
2016-09-20 13:53:00 +03:00
|
|
|
LOG(("CacheStorageService::Notify"));
|
|
|
|
|
|
|
|
mozilla::MutexAutoLock lock(mLock);
|
|
|
|
|
2014-04-06 22:45:18 +04:00
|
|
|
if (aTimer == mPurgeTimer) {
|
2020-02-17 21:44:21 +03:00
|
|
|
#ifdef MOZ_TSAN
|
|
|
|
mPurgeTimerActive = false;
|
|
|
|
#endif
|
2014-04-06 22:45:18 +04:00
|
|
|
mPurgeTimer = nullptr;
|
2013-09-20 13:11:25 +04:00
|
|
|
|
2014-04-06 22:45:18 +04:00
|
|
|
nsCOMPtr<nsIRunnable> event =
|
2017-06-12 22:34:10 +03:00
|
|
|
NewRunnableMethod("net::CacheStorageService::PurgeOverMemoryLimit",
|
|
|
|
this, &CacheStorageService::PurgeOverMemoryLimit);
|
2014-04-06 22:45:18 +04:00
|
|
|
Dispatch(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
2013-09-20 13:11:25 +04:00
|
|
|
}
|
|
|
|
|
2017-07-26 21:18:20 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
CacheStorageService::GetName(nsACString& aName) {
|
|
|
|
aName.AssignLiteral("CacheStorageService");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-09-20 13:11:25 +04:00
|
|
|
void CacheStorageService::PurgeOverMemoryLimit() {
|
|
|
|
MOZ_ASSERT(IsOnManagementThread());
|
|
|
|
|
|
|
|
LOG(("CacheStorageService::PurgeOverMemoryLimit"));
|
|
|
|
|
2016-02-15 13:45:00 +03:00
|
|
|
static TimeDuration const kFourSeconds = TimeDuration::FromSeconds(4);
|
|
|
|
TimeStamp now = TimeStamp::NowLoRes();
|
|
|
|
|
|
|
|
if (!mLastPurgeTime.IsNull() && now - mLastPurgeTime < kFourSeconds) {
|
|
|
|
LOG((" bypassed, too soon"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mLastPurgeTime = now;
|
|
|
|
|
2014-04-06 22:45:18 +04:00
|
|
|
Pool(true).PurgeOverMemoryLimit();
|
|
|
|
Pool(false).PurgeOverMemoryLimit();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CacheStorageService::MemoryPool::PurgeOverMemoryLimit() {
|
2013-09-20 13:11:25 +04:00
|
|
|
TimeStamp start(TimeStamp::Now());
|
|
|
|
|
2014-04-06 22:45:18 +04:00
|
|
|
uint32_t const memoryLimit = Limit();
|
2013-09-20 13:11:25 +04:00
|
|
|
if (mMemorySize > memoryLimit) {
|
|
|
|
LOG((" memory data consumption over the limit, abandon expired entries"));
|
|
|
|
PurgeExpired();
|
|
|
|
}
|
|
|
|
|
2014-04-06 22:45:18 +04:00
|
|
|
// No longer makes sense since:
|
|
|
|
// Memory entries are never purged partially, only as a whole when the memory
|
|
|
|
// cache limit is overreached.
|
|
|
|
// Disk entries throw the data away ASAP so that only metadata are kept.
|
|
|
|
// TODO when this concept of two separate pools is found working, the code
|
|
|
|
// should clean up.
|
|
|
|
#if 0
|
2013-09-20 13:11:25 +04:00
|
|
|
if (mMemorySize > memoryLimit) {
|
|
|
|
LOG((" memory data consumption over the limit, abandon disk backed data"));
|
2020-06-19 08:43:42 +03:00
|
|
|
PurgeByFrecency(CacheEntry::PURGE_DATA_ONLY_DISK_BACKED);
|
2013-09-20 13:11:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mMemorySize > memoryLimit) {
|
|
|
|
LOG((" metadata consumtion over the limit, abandon disk backed entries"));
|
2020-06-19 08:43:42 +03:00
|
|
|
PurgeByFrecency(CacheEntry::PURGE_WHOLE_ONLY_DISK_BACKED);
|
2013-09-20 13:11:25 +04:00
|
|
|
}
|
2014-04-06 22:45:18 +04:00
|
|
|
#endif
|
2013-09-20 13:11:25 +04:00
|
|
|
|
|
|
|
if (mMemorySize > memoryLimit) {
|
|
|
|
LOG((" memory data consumption over the limit, abandon any entry"));
|
2020-06-19 08:43:42 +03:00
|
|
|
PurgeByFrecency(CacheEntry::PURGE_WHOLE);
|
2013-09-20 13:11:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
LOG((" purging took %1.2fms", (TimeStamp::Now() - start).ToMilliseconds()));
|
|
|
|
}
|
|
|
|
|
2014-04-06 22:45:18 +04:00
|
|
|
void CacheStorageService::MemoryPool::PurgeExpired() {
|
2013-09-20 13:11:25 +04:00
|
|
|
MOZ_ASSERT(IsOnManagementThread());
|
|
|
|
|
|
|
|
mExpirationArray.Sort(ExpirationComparator());
|
|
|
|
uint32_t now = NowInSeconds();
|
|
|
|
|
2014-04-06 22:45:18 +04:00
|
|
|
uint32_t const memoryLimit = Limit();
|
2013-09-20 13:11:25 +04:00
|
|
|
|
|
|
|
for (uint32_t i = 0;
|
|
|
|
mMemorySize > memoryLimit && i < mExpirationArray.Length();) {
|
2014-02-27 03:11:42 +04:00
|
|
|
if (CacheIOThread::YieldAndRerun()) return;
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<CacheEntry> entry = mExpirationArray[i];
|
2013-09-20 13:11:25 +04:00
|
|
|
|
|
|
|
uint32_t expirationTime = entry->GetExpirationTime();
|
2015-02-12 22:00:00 +03:00
|
|
|
if (expirationTime > 0 && expirationTime <= now &&
|
|
|
|
entry->Purge(CacheEntry::PURGE_WHOLE)) {
|
|
|
|
LOG((" purged expired, entry=%p, exptime=%u (now=%u)", entry.get(),
|
2013-09-20 13:11:25 +04:00
|
|
|
entry->GetExpirationTime(), now));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// not purged, move to the next one
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-19 08:43:42 +03:00
|
|
|
void CacheStorageService::MemoryPool::PurgeByFrecency(uint32_t aWhat) {
|
2013-09-20 13:11:25 +04:00
|
|
|
MOZ_ASSERT(IsOnManagementThread());
|
|
|
|
|
2020-06-19 08:43:42 +03:00
|
|
|
// Pretend the limit is 10% lower so that we get rid of more entries at one
|
|
|
|
// shot and save the sorting below.
|
|
|
|
uint32_t const memoryLimit = Limit() * 0.9;
|
2013-09-20 13:11:25 +04:00
|
|
|
|
2020-06-19 08:43:42 +03:00
|
|
|
// Let's do our best and try to shorten the array to at least this size so
|
|
|
|
// that it doesn't overgrow. We will ignore higher priority events and keep
|
|
|
|
// looping to try to purge while the array is larget than this size.
|
|
|
|
static size_t const kFrecencyArrayLengthLimit = 2000;
|
|
|
|
|
|
|
|
LOG(("MemoryPool::PurgeByFrecency, len=%zu", mFrecencyArray.Length()));
|
|
|
|
|
|
|
|
mFrecencyArray.Sort(FrecencyComparator());
|
2013-09-20 13:11:25 +04:00
|
|
|
|
|
|
|
for (uint32_t i = 0;
|
|
|
|
mMemorySize > memoryLimit && i < mFrecencyArray.Length();) {
|
2020-06-19 08:43:42 +03:00
|
|
|
if (mFrecencyArray.Length() <= kFrecencyArrayLengthLimit &&
|
|
|
|
CacheIOThread::YieldAndRerun()) {
|
|
|
|
LOG(("MemoryPool::PurgeByFrecency interrupted"));
|
|
|
|
return;
|
|
|
|
}
|
2014-02-27 03:11:42 +04:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<CacheEntry> entry = mFrecencyArray[i];
|
2013-09-20 13:11:25 +04:00
|
|
|
if (entry->Purge(aWhat)) {
|
|
|
|
LOG((" abandoned (%d), entry=%p, frecency=%1.10f", aWhat, entry.get(),
|
|
|
|
entry->GetFrecency()));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// not purged, move to the next one
|
|
|
|
++i;
|
|
|
|
}
|
2020-06-19 08:43:42 +03:00
|
|
|
|
|
|
|
LOG(("MemoryPool::PurgeByFrecency done"));
|
2013-09-20 13:11:25 +04:00
|
|
|
}
|
|
|
|
|
2014-04-06 22:45:18 +04:00
|
|
|
void CacheStorageService::MemoryPool::PurgeAll(uint32_t aWhat) {
|
|
|
|
LOG(("CacheStorageService::MemoryPool::PurgeAll aWhat=%d", aWhat));
|
2013-09-20 13:11:25 +04:00
|
|
|
MOZ_ASSERT(IsOnManagementThread());
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < mFrecencyArray.Length();) {
|
2014-02-27 03:11:42 +04:00
|
|
|
if (CacheIOThread::YieldAndRerun()) return;
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<CacheEntry> entry = mFrecencyArray[i];
|
2013-09-20 13:11:25 +04:00
|
|
|
|
|
|
|
if (entry->Purge(aWhat)) {
|
|
|
|
LOG((" abandoned entry=%p", entry.get()));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// not purged, move to the next one
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Methods exposed to and used by CacheStorage.
|
|
|
|
|
|
|
|
nsresult CacheStorageService::AddStorageEntry(CacheStorage const* aStorage,
|
2016-05-12 13:38:00 +03:00
|
|
|
const nsACString& aURI,
|
2013-09-20 13:11:25 +04:00
|
|
|
const nsACString& aIdExtension,
|
2021-06-22 11:23:36 +03:00
|
|
|
uint32_t aFlags,
|
2014-01-07 00:24:41 +04:00
|
|
|
CacheEntryHandle** aResult) {
|
2013-09-20 13:11:25 +04:00
|
|
|
NS_ENSURE_FALSE(mShutdown, NS_ERROR_NOT_INITIALIZED);
|
|
|
|
|
|
|
|
NS_ENSURE_ARG(aStorage);
|
|
|
|
|
|
|
|
nsAutoCString contextKey;
|
2015-07-31 08:57:50 +03:00
|
|
|
CacheFileUtils::AppendKeyPrefix(aStorage->LoadInfo(), contextKey);
|
2013-09-20 13:11:25 +04:00
|
|
|
|
|
|
|
return AddStorageEntry(contextKey, aURI, aIdExtension,
|
2015-10-22 13:11:00 +03:00
|
|
|
aStorage->WriteToDisk(), aStorage->SkipSizeCheck(),
|
2021-06-22 11:23:36 +03:00
|
|
|
aStorage->Pinning(), aFlags, aResult);
|
2013-09-20 13:11:25 +04:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2017-06-20 12:19:52 +03:00
|
|
|
nsresult CacheStorageService::AddStorageEntry(
|
|
|
|
const nsACString& aContextKey, const nsACString& aURI,
|
2013-09-20 13:11:25 +04:00
|
|
|
const nsACString& aIdExtension, bool aWriteToDisk, bool aSkipSizeCheck,
|
2021-06-22 11:23:36 +03:00
|
|
|
bool aPin, uint32_t aFlags, CacheEntryHandle** aResult) {
|
2013-09-20 13:11:25 +04:00
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
nsAutoCString entryKey;
|
2020-09-23 18:17:15 +03:00
|
|
|
rv = CacheEntry::HashingKey(""_ns, aIdExtension, aURI, entryKey);
|
2013-09-20 13:11:25 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
LOG(("CacheStorageService::AddStorageEntry [entryKey=%s, contextKey=%s]",
|
|
|
|
entryKey.get(), aContextKey.BeginReading()));
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<CacheEntry> entry;
|
|
|
|
RefPtr<CacheEntryHandle> handle;
|
2013-09-20 13:11:25 +04:00
|
|
|
|
|
|
|
{
|
|
|
|
mozilla::MutexAutoLock lock(mLock);
|
|
|
|
|
|
|
|
NS_ENSURE_FALSE(mShutdown, NS_ERROR_NOT_INITIALIZED);
|
|
|
|
|
|
|
|
// Ensure storage table
|
2021-02-15 18:12:17 +03:00
|
|
|
CacheEntryTable* const entries =
|
|
|
|
sGlobalEntryTables
|
2021-02-26 12:11:45 +03:00
|
|
|
->LookupOrInsertWith(
|
2021-02-15 18:12:17 +03:00
|
|
|
aContextKey,
|
|
|
|
[&aContextKey] {
|
|
|
|
LOG((" new storage entries table for context '%s'",
|
|
|
|
aContextKey.BeginReading()));
|
|
|
|
return MakeUnique<CacheEntryTable>(
|
|
|
|
CacheEntryTable::ALL_ENTRIES);
|
|
|
|
})
|
|
|
|
.get();
|
2013-09-20 13:11:25 +04:00
|
|
|
|
|
|
|
bool entryExists = entries->Get(entryKey, getter_AddRefs(entry));
|
2021-09-28 10:08:28 +03:00
|
|
|
if (!entryExists && (aFlags & nsICacheStorage::OPEN_READONLY) &&
|
|
|
|
(aFlags & nsICacheStorage::OPEN_SECRETLY) &&
|
2021-06-22 11:23:36 +03:00
|
|
|
StaticPrefs::network_cache_bug1708673()) {
|
|
|
|
return NS_ERROR_CACHE_KEY_NOT_FOUND;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool replace = aFlags & nsICacheStorage::OPEN_TRUNCATE;
|
2013-09-20 13:11:25 +04:00
|
|
|
|
2021-06-22 11:23:36 +03:00
|
|
|
if (entryExists && !replace) {
|
2015-10-22 13:11:00 +03:00
|
|
|
// check whether we want to turn this entry to a memory-only.
|
|
|
|
if (MOZ_UNLIKELY(!aWriteToDisk) && MOZ_LIKELY(entry->IsUsingDisk())) {
|
2016-02-19 13:56:00 +03:00
|
|
|
LOG((" entry is persistent but we want mem-only, replacing it"));
|
2021-06-22 11:23:36 +03:00
|
|
|
replace = true;
|
2014-06-25 20:42:50 +04:00
|
|
|
}
|
2014-02-18 21:26:48 +04:00
|
|
|
}
|
|
|
|
|
2013-09-20 13:11:25 +04:00
|
|
|
// If truncate is demanded, delete and doom the current entry
|
2021-06-22 11:23:36 +03:00
|
|
|
if (entryExists && replace) {
|
2013-09-20 13:11:25 +04:00
|
|
|
entries->Remove(entryKey);
|
|
|
|
|
|
|
|
LOG((" dooming entry %p for %s because of OPEN_TRUNCATE", entry.get(),
|
|
|
|
entryKey.get()));
|
|
|
|
// On purpose called under the lock to prevent races of doom and open on
|
2014-04-10 14:47:20 +04:00
|
|
|
// I/O thread No need to remove from both memory-only and all-entries
|
|
|
|
// tables. The new entry will overwrite the shadow entry in its ctor.
|
2013-09-20 13:11:25 +04:00
|
|
|
entry->DoomAlreadyRemoved();
|
|
|
|
|
|
|
|
entry = nullptr;
|
|
|
|
entryExists = false;
|
2016-02-19 13:56:00 +03:00
|
|
|
|
|
|
|
// Would only lead to deleting force-valid timestamp again. We don't need
|
|
|
|
// the replace information anymore after this point anyway.
|
2021-06-22 11:23:36 +03:00
|
|
|
replace = false;
|
2013-09-20 13:11:25 +04:00
|
|
|
}
|
|
|
|
|
2016-02-19 13:56:00 +03:00
|
|
|
// Ensure entry for the particular URL
|
|
|
|
if (!entryExists) {
|
|
|
|
// When replacing with a new entry, always remove the current force-valid
|
|
|
|
// timestamp, this is the only place to do it.
|
2021-06-22 11:23:36 +03:00
|
|
|
if (replace) {
|
2016-02-19 13:56:00 +03:00
|
|
|
RemoveEntryForceValid(aContextKey, entryKey);
|
|
|
|
}
|
|
|
|
|
2013-09-20 13:11:25 +04:00
|
|
|
// Entry is not in the hashtable or has just been truncated...
|
2015-10-22 13:11:00 +03:00
|
|
|
entry = new CacheEntry(aContextKey, aURI, aIdExtension, aWriteToDisk,
|
|
|
|
aSkipSizeCheck, aPin);
|
2021-02-26 12:11:46 +03:00
|
|
|
entries->InsertOrUpdate(entryKey, RefPtr{entry});
|
2013-09-20 13:11:25 +04:00
|
|
|
LOG((" new entry %p for %s", entry.get(), entryKey.get()));
|
|
|
|
}
|
2014-01-07 00:24:41 +04:00
|
|
|
|
|
|
|
if (entry) {
|
|
|
|
// Here, if this entry was not for a long time referenced by any consumer,
|
2014-04-06 22:45:18 +04:00
|
|
|
// gets again first 'handles count' reference.
|
2014-01-07 00:24:41 +04:00
|
|
|
handle = entry->NewHandle();
|
|
|
|
}
|
2013-09-20 13:11:25 +04:00
|
|
|
}
|
|
|
|
|
2014-01-07 00:24:41 +04:00
|
|
|
handle.forget(aResult);
|
2013-09-20 13:11:25 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-06-24 20:08:35 +04:00
|
|
|
nsresult CacheStorageService::CheckStorageEntry(CacheStorage const* aStorage,
|
2016-05-12 13:38:00 +03:00
|
|
|
const nsACString& aURI,
|
2014-06-24 20:08:35 +04:00
|
|
|
const nsACString& aIdExtension,
|
|
|
|
bool* aResult) {
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
nsAutoCString contextKey;
|
2015-07-31 08:57:50 +03:00
|
|
|
CacheFileUtils::AppendKeyPrefix(aStorage->LoadInfo(), contextKey);
|
2014-06-24 20:08:35 +04:00
|
|
|
|
|
|
|
if (!aStorage->WriteToDisk()) {
|
2019-03-20 16:54:57 +03:00
|
|
|
AppendMemoryStorageTag(contextKey);
|
2014-06-24 20:08:35 +04:00
|
|
|
}
|
|
|
|
|
2016-05-12 13:38:00 +03:00
|
|
|
LOG(("CacheStorageService::CheckStorageEntry [uri=%s, eid=%s, contextKey=%s]",
|
|
|
|
aURI.BeginReading(), aIdExtension.BeginReading(), contextKey.get()));
|
2014-06-24 20:08:35 +04:00
|
|
|
|
|
|
|
{
|
|
|
|
mozilla::MutexAutoLock lock(mLock);
|
|
|
|
|
|
|
|
NS_ENSURE_FALSE(mShutdown, NS_ERROR_NOT_INITIALIZED);
|
|
|
|
|
|
|
|
nsAutoCString entryKey;
|
2020-09-23 18:17:15 +03:00
|
|
|
rv = CacheEntry::HashingKey(""_ns, aIdExtension, aURI, entryKey);
|
2014-06-24 20:08:35 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
CacheEntryTable* entries;
|
|
|
|
if ((*aResult = sGlobalEntryTables->Get(contextKey, &entries)) &&
|
|
|
|
entries->GetWeak(entryKey, aResult)) {
|
|
|
|
LOG((" found in hash tables"));
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!aStorage->WriteToDisk()) {
|
|
|
|
// Memory entry, nothing more to do.
|
|
|
|
LOG((" not found in hash tables"));
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Disk entry, not found in the hashtable, check the index.
|
|
|
|
nsAutoCString fileKey;
|
|
|
|
rv = CacheEntry::HashingKey(contextKey, aIdExtension, aURI, fileKey);
|
|
|
|
|
|
|
|
CacheIndex::EntryStatus status;
|
|
|
|
rv = CacheIndex::HasEntry(fileKey, &status);
|
|
|
|
if (NS_FAILED(rv) || status == CacheIndex::DO_NOT_KNOW) {
|
2016-12-16 06:16:31 +03:00
|
|
|
LOG((" index doesn't know, rv=0x%08" PRIx32, static_cast<uint32_t>(rv)));
|
2014-06-24 20:08:35 +04:00
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
*aResult = status == CacheIndex::EXISTS;
|
|
|
|
LOG((" %sfound in index", *aResult ? "" : "not "));
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2017-04-10 14:22:23 +03:00
|
|
|
nsresult CacheStorageService::GetCacheIndexEntryAttrs(
|
|
|
|
CacheStorage const* aStorage, const nsACString& aURI,
|
|
|
|
const nsACString& aIdExtension, bool* aHasAltData, uint32_t* aFileSizeKb) {
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
nsAutoCString contextKey;
|
|
|
|
CacheFileUtils::AppendKeyPrefix(aStorage->LoadInfo(), contextKey);
|
|
|
|
|
|
|
|
LOG(
|
|
|
|
("CacheStorageService::GetCacheIndexEntryAttrs [uri=%s, eid=%s, "
|
|
|
|
"contextKey=%s]",
|
|
|
|
aURI.BeginReading(), aIdExtension.BeginReading(), contextKey.get()));
|
|
|
|
|
|
|
|
nsAutoCString fileKey;
|
|
|
|
rv = CacheEntry::HashingKey(contextKey, aIdExtension, aURI, fileKey);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
*aHasAltData = false;
|
|
|
|
*aFileSizeKb = 0;
|
|
|
|
auto closure = [&aHasAltData, &aFileSizeKb](const CacheIndexEntry* entry) {
|
|
|
|
*aHasAltData = entry->GetHasAltData();
|
|
|
|
*aFileSizeKb = entry->GetFileSize();
|
|
|
|
};
|
|
|
|
|
|
|
|
CacheIndex::EntryStatus status;
|
|
|
|
rv = CacheIndex::HasEntry(fileKey, &status, closure);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (status != CacheIndex::EXISTS) {
|
|
|
|
return NS_ERROR_CACHE_KEY_NOT_FOUND;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
namespace {
|
2013-09-20 13:11:25 +04:00
|
|
|
|
|
|
|
class CacheEntryDoomByKeyCallback : public CacheFileIOListener,
|
2014-08-18 17:36:27 +04:00
|
|
|
public nsIRunnable {
|
2013-09-20 13:11:25 +04:00
|
|
|
public:
|
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
2014-08-18 17:36:27 +04:00
|
|
|
NS_DECL_NSIRUNNABLE
|
2013-09-20 13:11:25 +04:00
|
|
|
|
2014-08-05 17:20:24 +04:00
|
|
|
explicit CacheEntryDoomByKeyCallback(nsICacheEntryDoomCallback* aCallback)
|
2018-06-14 11:19:07 +03:00
|
|
|
: mCallback(aCallback), mResult(NS_ERROR_NOT_INITIALIZED) {}
|
2013-09-20 13:11:25 +04:00
|
|
|
|
|
|
|
private:
|
2014-06-24 20:36:44 +04:00
|
|
|
virtual ~CacheEntryDoomByKeyCallback();
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
NS_IMETHOD OnFileOpened(CacheFileHandle* aHandle, nsresult aResult) override {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
NS_IMETHOD OnDataWritten(CacheFileHandle* aHandle, const char* aBuf,
|
|
|
|
nsresult aResult) override {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
NS_IMETHOD OnDataRead(CacheFileHandle* aHandle, char* aBuf,
|
|
|
|
nsresult aResult) override {
|
|
|
|
return NS_OK;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2015-03-21 19:28:04 +03:00
|
|
|
NS_IMETHOD OnFileDoomed(CacheFileHandle* aHandle, nsresult aResult) override;
|
|
|
|
NS_IMETHOD OnEOFSet(CacheFileHandle* aHandle, nsresult aResult) override {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
NS_IMETHOD OnFileRenamed(CacheFileHandle* aHandle,
|
|
|
|
nsresult aResult) override {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2013-09-20 13:11:25 +04:00
|
|
|
|
|
|
|
nsCOMPtr<nsICacheEntryDoomCallback> mCallback;
|
2014-08-18 17:36:27 +04:00
|
|
|
nsresult mResult;
|
2013-09-20 13:11:25 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
CacheEntryDoomByKeyCallback::~CacheEntryDoomByKeyCallback() {
|
2021-06-04 15:35:16 +03:00
|
|
|
if (mCallback) {
|
2017-06-14 04:27:17 +03:00
|
|
|
ProxyReleaseMainThread("CacheEntryDoomByKeyCallback::mCallback", mCallback);
|
2021-06-04 15:35:16 +03:00
|
|
|
}
|
2013-09-20 13:11:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP CacheEntryDoomByKeyCallback::OnFileDoomed(
|
|
|
|
CacheFileHandle* aHandle, nsresult aResult) {
|
|
|
|
if (!mCallback) return NS_OK;
|
|
|
|
|
2014-08-18 17:36:27 +04:00
|
|
|
mResult = aResult;
|
|
|
|
if (NS_IsMainThread()) {
|
|
|
|
Run();
|
|
|
|
} else {
|
|
|
|
NS_DispatchToMainThread(this);
|
|
|
|
}
|
|
|
|
|
2013-09-20 13:11:25 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-08-18 17:36:27 +04:00
|
|
|
NS_IMETHODIMP CacheEntryDoomByKeyCallback::Run() {
|
|
|
|
mCallback->OnCacheEntryDoomed(mResult);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS(CacheEntryDoomByKeyCallback, CacheFileIOListener,
|
|
|
|
nsIRunnable);
|
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
|
|
|
|
|
|
|
nsresult CacheStorageService::DoomStorageEntry(
|
|
|
|
CacheStorage const* aStorage, const nsACString& aURI,
|
|
|
|
const nsACString& aIdExtension, nsICacheEntryDoomCallback* aCallback) {
|
|
|
|
LOG(("CacheStorageService::DoomStorageEntry"));
|
|
|
|
|
|
|
|
NS_ENSURE_ARG(aStorage);
|
|
|
|
|
|
|
|
nsAutoCString contextKey;
|
2015-07-31 08:57:50 +03:00
|
|
|
CacheFileUtils::AppendKeyPrefix(aStorage->LoadInfo(), contextKey);
|
2013-09-20 13:11:25 +04:00
|
|
|
|
|
|
|
nsAutoCString entryKey;
|
2020-09-23 18:17:15 +03:00
|
|
|
nsresult rv = CacheEntry::HashingKey(""_ns, aIdExtension, aURI, entryKey);
|
2013-09-20 13:11:25 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<CacheEntry> entry;
|
2013-09-20 13:11:25 +04:00
|
|
|
{
|
|
|
|
mozilla::MutexAutoLock lock(mLock);
|
|
|
|
|
|
|
|
NS_ENSURE_FALSE(mShutdown, NS_ERROR_NOT_INITIALIZED);
|
|
|
|
|
|
|
|
CacheEntryTable* entries;
|
|
|
|
if (sGlobalEntryTables->Get(contextKey, &entries)) {
|
|
|
|
if (entries->Get(entryKey, getter_AddRefs(entry))) {
|
2014-06-25 20:42:50 +04:00
|
|
|
if (aStorage->WriteToDisk() || !entry->IsUsingDisk()) {
|
2013-09-20 13:11:25 +04:00
|
|
|
// When evicting from disk storage, purge
|
|
|
|
// When evicting from memory storage and the entry is memory-only,
|
|
|
|
// purge
|
|
|
|
LOG(
|
|
|
|
(" purging entry %p for %s [storage use disk=%d, entry use "
|
|
|
|
"disk=%d]",
|
2014-06-25 20:42:50 +04:00
|
|
|
entry.get(), entryKey.get(), aStorage->WriteToDisk(),
|
|
|
|
entry->IsUsingDisk()));
|
2013-09-20 13:11:25 +04:00
|
|
|
entries->Remove(entryKey);
|
|
|
|
} else {
|
|
|
|
// Otherwise, leave it
|
|
|
|
LOG(
|
|
|
|
(" leaving entry %p for %s [storage use disk=%d, entry use "
|
|
|
|
"disk=%d]",
|
2014-06-25 20:42:50 +04:00
|
|
|
entry.get(), entryKey.get(), aStorage->WriteToDisk(),
|
|
|
|
entry->IsUsingDisk()));
|
2013-09-20 13:11:25 +04:00
|
|
|
entry = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-02-19 13:56:00 +03:00
|
|
|
|
|
|
|
if (!entry) {
|
|
|
|
RemoveEntryForceValid(contextKey, entryKey);
|
|
|
|
}
|
2013-09-20 13:11:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (entry) {
|
|
|
|
LOG((" dooming entry %p for %s", entry.get(), entryKey.get()));
|
|
|
|
return entry->AsyncDoom(aCallback);
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG((" no entry loaded for %s", entryKey.get()));
|
|
|
|
|
|
|
|
if (aStorage->WriteToDisk()) {
|
|
|
|
nsAutoCString contextKey;
|
2015-07-31 08:57:50 +03:00
|
|
|
CacheFileUtils::AppendKeyPrefix(aStorage->LoadInfo(), contextKey);
|
2013-09-20 13:11:25 +04:00
|
|
|
|
|
|
|
rv = CacheEntry::HashingKey(contextKey, aIdExtension, aURI, entryKey);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
LOG((" dooming file only for %s", entryKey.get()));
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<CacheEntryDoomByKeyCallback> callback(
|
2013-09-20 13:11:25 +04:00
|
|
|
new CacheEntryDoomByKeyCallback(aCallback));
|
|
|
|
rv = CacheFileIOManager::DoomFileByKey(entryKey, callback);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-04-26 03:23:21 +03:00
|
|
|
class Callback : public Runnable {
|
2014-08-18 17:36:27 +04:00
|
|
|
public:
|
2017-06-12 22:34:10 +03:00
|
|
|
explicit Callback(nsICacheEntryDoomCallback* aCallback)
|
|
|
|
: mozilla::Runnable("Callback"), mCallback(aCallback) {}
|
2016-08-08 05:18:10 +03:00
|
|
|
NS_IMETHOD Run() override {
|
2014-08-18 17:36:27 +04:00
|
|
|
mCallback->OnCacheEntryDoomed(NS_ERROR_NOT_AVAILABLE);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
nsCOMPtr<nsICacheEntryDoomCallback> mCallback;
|
|
|
|
};
|
|
|
|
|
|
|
|
if (aCallback) {
|
2016-04-26 03:23:21 +03:00
|
|
|
RefPtr<Runnable> callback = new Callback(aCallback);
|
2014-08-18 17:36:27 +04:00
|
|
|
return NS_DispatchToMainThread(callback);
|
|
|
|
}
|
2013-09-20 13:11:25 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult CacheStorageService::DoomStorageEntries(
|
|
|
|
CacheStorage const* aStorage, nsICacheEntryDoomCallback* aCallback) {
|
|
|
|
LOG(("CacheStorageService::DoomStorageEntries"));
|
|
|
|
|
|
|
|
NS_ENSURE_FALSE(mShutdown, NS_ERROR_NOT_INITIALIZED);
|
|
|
|
NS_ENSURE_ARG(aStorage);
|
|
|
|
|
|
|
|
nsAutoCString contextKey;
|
2015-07-31 08:57:50 +03:00
|
|
|
CacheFileUtils::AppendKeyPrefix(aStorage->LoadInfo(), contextKey);
|
2013-09-20 13:11:25 +04:00
|
|
|
|
|
|
|
mozilla::MutexAutoLock lock(mLock);
|
|
|
|
|
2014-04-10 14:47:20 +04:00
|
|
|
return DoomStorageEntries(contextKey, aStorage->LoadInfo(),
|
2015-10-22 13:11:00 +03:00
|
|
|
aStorage->WriteToDisk(), aStorage->Pinning(),
|
|
|
|
aCallback);
|
2013-09-20 13:11:25 +04:00
|
|
|
}
|
|
|
|
|
2017-06-20 12:19:52 +03:00
|
|
|
nsresult CacheStorageService::DoomStorageEntries(
|
2014-04-10 14:47:20 +04:00
|
|
|
const nsACString& aContextKey, nsILoadContextInfo* aContext,
|
2013-09-20 13:11:25 +04:00
|
|
|
bool aDiskStorage, bool aPinned, nsICacheEntryDoomCallback* aCallback) {
|
2015-10-22 13:11:00 +03:00
|
|
|
LOG(("CacheStorageService::DoomStorageEntries [context=%s]",
|
|
|
|
aContextKey.BeginReading()));
|
|
|
|
|
2013-09-20 13:11:25 +04:00
|
|
|
mLock.AssertCurrentThreadOwns();
|
|
|
|
|
|
|
|
NS_ENSURE_TRUE(!mShutdown, NS_ERROR_NOT_INITIALIZED);
|
|
|
|
|
|
|
|
nsAutoCString memoryStorageID(aContextKey);
|
2019-03-20 16:54:57 +03:00
|
|
|
AppendMemoryStorageTag(memoryStorageID);
|
2013-09-20 13:11:25 +04:00
|
|
|
|
|
|
|
if (aDiskStorage) {
|
|
|
|
LOG((" dooming disk+memory storage of %s", aContextKey.BeginReading()));
|
2014-04-10 14:47:20 +04:00
|
|
|
|
2015-10-22 13:11:00 +03:00
|
|
|
// Walk one by one and remove entries according their pin status
|
|
|
|
CacheEntryTable *diskEntries, *memoryEntries;
|
|
|
|
if (sGlobalEntryTables->Get(aContextKey, &diskEntries)) {
|
|
|
|
sGlobalEntryTables->Get(memoryStorageID, &memoryEntries);
|
|
|
|
|
|
|
|
for (auto iter = diskEntries->Iter(); !iter.Done(); iter.Next()) {
|
|
|
|
auto entry = iter.Data();
|
|
|
|
if (entry->DeferOrBypassRemovalOnPinStatus(aPinned)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (memoryEntries) {
|
|
|
|
RemoveExactEntry(memoryEntries, iter.Key(), entry, false);
|
|
|
|
}
|
|
|
|
iter.Remove();
|
|
|
|
}
|
|
|
|
}
|
2014-04-10 14:47:20 +04:00
|
|
|
|
|
|
|
if (aContext && !aContext->IsPrivate()) {
|
|
|
|
LOG((" dooming disk entries"));
|
2020-09-23 18:17:15 +03:00
|
|
|
CacheFileIOManager::EvictByContext(aContext, aPinned, u""_ns);
|
2014-04-10 14:47:20 +04:00
|
|
|
}
|
|
|
|
} else {
|
2013-09-20 13:11:25 +04:00
|
|
|
LOG((" dooming memory-only storage of %s", aContextKey.BeginReading()));
|
2014-04-10 14:47:20 +04:00
|
|
|
|
|
|
|
// Remove the memory entries table from the global tables.
|
|
|
|
// Since we store memory entries also in the disk entries table
|
|
|
|
// we need to remove the memory entries from the disk table one
|
|
|
|
// by one manually.
|
2020-01-13 22:18:56 +03:00
|
|
|
mozilla::UniquePtr<CacheEntryTable> memoryEntries;
|
2017-07-05 03:01:45 +03:00
|
|
|
sGlobalEntryTables->Remove(memoryStorageID, &memoryEntries);
|
2014-04-10 14:47:20 +04:00
|
|
|
|
2015-10-22 13:11:00 +03:00
|
|
|
CacheEntryTable* diskEntries;
|
2015-12-07 08:38:00 +03:00
|
|
|
if (memoryEntries && sGlobalEntryTables->Get(aContextKey, &diskEntries)) {
|
2021-03-17 18:49:46 +03:00
|
|
|
for (const auto& memoryEntry : *memoryEntries) {
|
2021-06-04 15:35:16 +03:00
|
|
|
const auto& entry = memoryEntry.GetData();
|
2021-03-17 18:49:46 +03:00
|
|
|
RemoveExactEntry(diskEntries, memoryEntry.GetKey(), entry, false);
|
2015-10-22 13:11:00 +03:00
|
|
|
}
|
|
|
|
}
|
2013-09-20 13:11:25 +04:00
|
|
|
}
|
|
|
|
|
2016-02-19 13:56:00 +03:00
|
|
|
{
|
|
|
|
mozilla::MutexAutoLock lock(mForcedValidEntriesLock);
|
|
|
|
|
2016-02-25 21:41:52 +03:00
|
|
|
if (aContext) {
|
|
|
|
for (auto iter = mForcedValidEntries.Iter(); !iter.Done(); iter.Next()) {
|
|
|
|
bool matches;
|
|
|
|
DebugOnly<nsresult> rv = CacheFileUtils::KeyMatchesLoadContextInfo(
|
|
|
|
iter.Key(), aContext, &matches);
|
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(rv));
|
|
|
|
|
|
|
|
if (matches) {
|
|
|
|
iter.Remove();
|
|
|
|
}
|
2016-02-19 13:56:00 +03:00
|
|
|
}
|
2016-02-25 21:41:52 +03:00
|
|
|
} else {
|
|
|
|
mForcedValidEntries.Clear();
|
2016-02-19 13:56:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-10 14:47:20 +04:00
|
|
|
// An artificial callback. This is a candidate for removal tho. In the new
|
|
|
|
// cache any 'doom' or 'evict' function ensures that the entry or entries
|
|
|
|
// being doomed is/are not accessible after the function returns. So there is
|
|
|
|
// probably no need for a callback - has no meaning. But for compatibility
|
|
|
|
// with the old cache that is still in the tree we keep the API similar to be
|
|
|
|
// able to make tests as well as other consumers work for now.
|
2016-04-26 03:23:21 +03:00
|
|
|
class Callback : public Runnable {
|
2014-04-10 14:47:20 +04:00
|
|
|
public:
|
2017-06-12 22:34:10 +03:00
|
|
|
explicit Callback(nsICacheEntryDoomCallback* aCallback)
|
|
|
|
: mozilla::Runnable("Callback"), mCallback(aCallback) {}
|
2016-08-08 05:18:10 +03:00
|
|
|
NS_IMETHOD Run() override {
|
2014-04-10 14:47:20 +04:00
|
|
|
mCallback->OnCacheEntryDoomed(NS_OK);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
nsCOMPtr<nsICacheEntryDoomCallback> mCallback;
|
|
|
|
};
|
2013-09-20 13:11:25 +04:00
|
|
|
|
2014-04-10 14:47:20 +04:00
|
|
|
if (aCallback) {
|
2016-04-26 03:23:21 +03:00
|
|
|
RefPtr<Runnable> callback = new Callback(aCallback);
|
2014-08-18 17:36:27 +04:00
|
|
|
return NS_DispatchToMainThread(callback);
|
2014-04-10 14:47:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
2013-09-20 13:11:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult CacheStorageService::WalkStorageEntries(
|
|
|
|
CacheStorage const* aStorage, bool aVisitEntries,
|
|
|
|
nsICacheStorageVisitor* aVisitor) {
|
|
|
|
LOG(("CacheStorageService::WalkStorageEntries [cb=%p, visitentries=%d]",
|
|
|
|
aVisitor, aVisitEntries));
|
|
|
|
NS_ENSURE_FALSE(mShutdown, NS_ERROR_NOT_INITIALIZED);
|
|
|
|
|
|
|
|
NS_ENSURE_ARG(aStorage);
|
|
|
|
|
2014-05-01 15:28:12 +04:00
|
|
|
if (aStorage->WriteToDisk()) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<WalkDiskCacheRunnable> event = new WalkDiskCacheRunnable(
|
2014-05-01 15:28:12 +04:00
|
|
|
aStorage->LoadInfo(), aVisitEntries, aVisitor);
|
|
|
|
return event->Walk();
|
|
|
|
}
|
2013-09-20 13:11:25 +04:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<WalkMemoryCacheRunnable> event = new WalkMemoryCacheRunnable(
|
2015-07-31 08:57:50 +03:00
|
|
|
aStorage->LoadInfo(), aVisitEntries, aVisitor);
|
2014-05-01 15:28:12 +04:00
|
|
|
return event->Walk();
|
2013-09-20 13:11:25 +04:00
|
|
|
}
|
|
|
|
|
2014-02-18 21:26:48 +04:00
|
|
|
void CacheStorageService::CacheFileDoomed(nsILoadContextInfo* aLoadContextInfo,
|
2015-07-31 08:57:50 +03:00
|
|
|
const nsACString& aIdExtension,
|
|
|
|
const nsACString& aURISpec) {
|
2014-02-18 21:26:48 +04:00
|
|
|
nsAutoCString contextKey;
|
2015-07-31 08:57:50 +03:00
|
|
|
CacheFileUtils::AppendKeyPrefix(aLoadContextInfo, contextKey);
|
2014-02-18 21:26:48 +04:00
|
|
|
|
2014-04-06 22:44:40 +04:00
|
|
|
nsAutoCString entryKey;
|
2020-09-23 18:17:15 +03:00
|
|
|
CacheEntry::HashingKey(""_ns, aIdExtension, aURISpec, entryKey);
|
2014-02-18 21:26:48 +04:00
|
|
|
|
2014-04-06 22:44:40 +04:00
|
|
|
mozilla::MutexAutoLock lock(mLock);
|
2014-02-18 21:26:48 +04:00
|
|
|
|
2016-02-19 13:56:00 +03:00
|
|
|
if (mShutdown) {
|
2014-04-06 22:44:40 +04:00
|
|
|
return;
|
2016-02-19 13:56:00 +03:00
|
|
|
}
|
2014-02-18 21:26:48 +04:00
|
|
|
|
2014-04-06 22:44:40 +04:00
|
|
|
CacheEntryTable* entries;
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<CacheEntry> entry;
|
2014-04-06 22:44:40 +04:00
|
|
|
|
2016-02-19 13:56:00 +03:00
|
|
|
if (sGlobalEntryTables->Get(contextKey, &entries) &&
|
|
|
|
entries->Get(entryKey, getter_AddRefs(entry))) {
|
|
|
|
if (entry->IsFileDoomed()) {
|
|
|
|
// Need to remove under the lock to avoid possible race leading
|
|
|
|
// to duplication of the entry per its key.
|
|
|
|
RemoveExactEntry(entries, entryKey, entry, false);
|
|
|
|
entry->DoomAlreadyRemoved();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Entry found, but it's not the entry that has been found doomed
|
|
|
|
// by the lower eviction layer. Just leave everything unchanged.
|
2014-04-06 22:44:40 +04:00
|
|
|
return;
|
2016-02-19 13:56:00 +03:00
|
|
|
}
|
2014-04-06 22:44:40 +04:00
|
|
|
|
2016-02-19 13:56:00 +03:00
|
|
|
RemoveEntryForceValid(contextKey, entryKey);
|
2014-02-18 21:26:48 +04:00
|
|
|
}
|
|
|
|
|
2014-05-01 15:28:12 +04:00
|
|
|
bool CacheStorageService::GetCacheEntryInfo(
|
|
|
|
nsILoadContextInfo* aLoadContextInfo, const nsACString& aIdExtension,
|
|
|
|
const nsACString& aURISpec, EntryInfoCallback* aCallback) {
|
|
|
|
nsAutoCString contextKey;
|
2015-07-31 08:57:50 +03:00
|
|
|
CacheFileUtils::AppendKeyPrefix(aLoadContextInfo, contextKey);
|
2014-05-01 15:28:12 +04:00
|
|
|
|
|
|
|
nsAutoCString entryKey;
|
2020-09-23 18:17:15 +03:00
|
|
|
CacheEntry::HashingKey(""_ns, aIdExtension, aURISpec, entryKey);
|
2014-05-01 15:28:12 +04:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<CacheEntry> entry;
|
2014-05-01 15:28:12 +04:00
|
|
|
{
|
|
|
|
mozilla::MutexAutoLock lock(mLock);
|
|
|
|
|
|
|
|
if (mShutdown) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
CacheEntryTable* entries;
|
|
|
|
if (!sGlobalEntryTables->Get(contextKey, &entries)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!entries->Get(entryKey, getter_AddRefs(entry))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
GetCacheEntryInfo(entry, aCallback);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
void CacheStorageService::GetCacheEntryInfo(CacheEntry* aEntry,
|
|
|
|
EntryInfoCallback* aCallback) {
|
2016-05-12 13:38:00 +03:00
|
|
|
nsCString const uriSpec = aEntry->GetURI();
|
2014-05-01 15:28:12 +04:00
|
|
|
nsCString const enhanceId = aEntry->GetEnhanceID();
|
2016-05-12 13:38:00 +03:00
|
|
|
|
2017-02-03 06:14:00 +03:00
|
|
|
nsAutoCString entryKey;
|
|
|
|
aEntry->HashingKeyWithStorage(entryKey);
|
|
|
|
|
|
|
|
nsCOMPtr<nsILoadContextInfo> info = CacheFileUtils::ParseKey(entryKey);
|
|
|
|
|
2014-05-01 15:28:12 +04:00
|
|
|
uint32_t dataSize;
|
|
|
|
if (NS_FAILED(aEntry->GetStorageDataSize(&dataSize))) {
|
|
|
|
dataSize = 0;
|
|
|
|
}
|
2022-08-04 19:20:17 +03:00
|
|
|
int64_t altDataSize;
|
|
|
|
if (NS_FAILED(aEntry->GetAltDataSize(&altDataSize))) {
|
|
|
|
altDataSize = 0;
|
|
|
|
}
|
2022-08-04 19:20:17 +03:00
|
|
|
uint32_t fetchCount;
|
2014-05-01 15:28:12 +04:00
|
|
|
if (NS_FAILED(aEntry->GetFetchCount(&fetchCount))) {
|
|
|
|
fetchCount = 0;
|
|
|
|
}
|
|
|
|
uint32_t lastModified;
|
|
|
|
if (NS_FAILED(aEntry->GetLastModified(&lastModified))) {
|
|
|
|
lastModified = 0;
|
|
|
|
}
|
|
|
|
uint32_t expirationTime;
|
|
|
|
if (NS_FAILED(aEntry->GetExpirationTime(&expirationTime))) {
|
|
|
|
expirationTime = 0;
|
|
|
|
}
|
|
|
|
|
2022-08-04 19:20:17 +03:00
|
|
|
aCallback->OnEntryInfo(uriSpec, enhanceId, dataSize, altDataSize, fetchCount,
|
|
|
|
lastModified, expirationTime, aEntry->IsPinned(),
|
|
|
|
info);
|
2014-05-01 15:28:12 +04:00
|
|
|
}
|
|
|
|
|
2016-11-10 18:14:34 +03:00
|
|
|
// static
|
|
|
|
uint32_t CacheStorageService::CacheQueueSize(bool highPriority) {
|
|
|
|
RefPtr<CacheIOThread> thread = CacheFileIOManager::IOThread();
|
2017-09-07 16:15:25 +03:00
|
|
|
// The thread will be null at shutdown.
|
|
|
|
if (!thread) {
|
|
|
|
return 0;
|
|
|
|
}
|
2016-11-10 18:14:34 +03:00
|
|
|
return thread->QueueSize(highPriority);
|
|
|
|
}
|
|
|
|
|
2017-06-02 21:49:22 +03:00
|
|
|
// Telemetry collection
|
2014-07-08 18:48:00 +04:00
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
namespace {
|
2014-07-08 18:48:00 +04:00
|
|
|
|
|
|
|
bool TelemetryEntryKey(CacheEntry const* entry, nsAutoCString& key) {
|
|
|
|
nsAutoCString entryKey;
|
|
|
|
nsresult rv = entry->HashingKey(entryKey);
|
|
|
|
if (NS_FAILED(rv)) return false;
|
|
|
|
|
|
|
|
if (entry->GetStorageID().IsEmpty()) {
|
|
|
|
// Hopefully this will be const-copied, saves some memory
|
|
|
|
key = entryKey;
|
|
|
|
} else {
|
|
|
|
key.Assign(entry->GetStorageID());
|
|
|
|
key.Append(':');
|
|
|
|
key.Append(entryKey);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace
|
2014-07-08 18:48:00 +04:00
|
|
|
|
|
|
|
void CacheStorageService::TelemetryPrune(TimeStamp& now) {
|
|
|
|
static TimeDuration const oneMinute = TimeDuration::FromSeconds(60);
|
|
|
|
static TimeStamp dontPruneUntil = now + oneMinute;
|
|
|
|
if (now < dontPruneUntil) return;
|
|
|
|
|
2015-12-04 00:59:58 +03:00
|
|
|
static TimeDuration const fifteenMinutes = TimeDuration::FromSeconds(900);
|
|
|
|
for (auto iter = mPurgeTimeStamps.Iter(); !iter.Done(); iter.Next()) {
|
|
|
|
if (now - iter.Data() > fifteenMinutes) {
|
|
|
|
// We are not interested in resurrection of entries after 15 minutes
|
|
|
|
// of time. This is also the limit for the telemetry.
|
|
|
|
iter.Remove();
|
|
|
|
}
|
|
|
|
}
|
2014-07-08 18:48:00 +04:00
|
|
|
dontPruneUntil = now + oneMinute;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CacheStorageService::TelemetryRecordEntryCreation(
|
|
|
|
CacheEntry const* entry) {
|
|
|
|
MOZ_ASSERT(CacheStorageService::IsOnManagementThread());
|
|
|
|
|
|
|
|
nsAutoCString key;
|
|
|
|
if (!TelemetryEntryKey(entry, key)) return;
|
|
|
|
|
|
|
|
TimeStamp now = TimeStamp::NowLoRes();
|
|
|
|
TelemetryPrune(now);
|
|
|
|
|
|
|
|
// When an entry is craeted (registered actually) we check if there is
|
|
|
|
// a timestamp marked when this very same cache entry has been removed
|
|
|
|
// (deregistered) because of over-memory-limit purging. If there is such
|
|
|
|
// a timestamp found accumulate telemetry on how long the entry was away.
|
|
|
|
TimeStamp timeStamp;
|
|
|
|
if (!mPurgeTimeStamps.Get(key, &timeStamp)) return;
|
|
|
|
|
|
|
|
mPurgeTimeStamps.Remove(key);
|
|
|
|
|
|
|
|
Telemetry::AccumulateTimeDelta(Telemetry::HTTP_CACHE_ENTRY_RELOAD_TIME,
|
|
|
|
timeStamp, TimeStamp::NowLoRes());
|
|
|
|
}
|
|
|
|
|
2022-01-12 23:38:37 +03:00
|
|
|
void CacheStorageService::TelemetryRecordEntryRemoval(CacheEntry* entry) {
|
2014-07-08 18:48:00 +04:00
|
|
|
MOZ_ASSERT(CacheStorageService::IsOnManagementThread());
|
|
|
|
|
|
|
|
// Doomed entries must not be considered, we are only interested in purged
|
|
|
|
// entries. Note that the mIsDoomed flag is always set before deregistration
|
|
|
|
// happens.
|
|
|
|
if (entry->IsDoomed()) return;
|
|
|
|
|
|
|
|
nsAutoCString key;
|
|
|
|
if (!TelemetryEntryKey(entry, key)) return;
|
|
|
|
|
|
|
|
// When an entry is removed (deregistered actually) we put a timestamp for
|
|
|
|
// this entry to the hashtable so that when the entry is created (registered)
|
|
|
|
// again we know how long it was away. Also accumulate number of AsyncOpen
|
|
|
|
// calls on the entry, this tells us how efficiently the pool actually works.
|
|
|
|
|
|
|
|
TimeStamp now = TimeStamp::NowLoRes();
|
|
|
|
TelemetryPrune(now);
|
2021-02-26 12:11:46 +03:00
|
|
|
mPurgeTimeStamps.InsertOrUpdate(key, now);
|
2014-07-08 18:48:00 +04:00
|
|
|
|
|
|
|
Telemetry::Accumulate(Telemetry::HTTP_CACHE_ENTRY_REUSE_COUNT,
|
|
|
|
entry->UseCount());
|
|
|
|
Telemetry::AccumulateTimeDelta(Telemetry::HTTP_CACHE_ENTRY_ALIVE_TIME,
|
|
|
|
entry->LoadStart(), TimeStamp::NowLoRes());
|
|
|
|
}
|
|
|
|
|
2014-02-27 03:11:40 +04:00
|
|
|
// nsIMemoryReporter
|
|
|
|
|
|
|
|
size_t CacheStorageService::SizeOfExcludingThis(
|
|
|
|
mozilla::MallocSizeOf mallocSizeOf) const {
|
|
|
|
CacheStorageService::Self()->Lock().AssertCurrentThreadOwns();
|
|
|
|
|
|
|
|
size_t n = 0;
|
|
|
|
// The elemets are referenced by sGlobalEntryTables and are reported from
|
|
|
|
// there
|
2015-07-29 09:24:24 +03:00
|
|
|
n += Pool(true).mFrecencyArray.ShallowSizeOfExcludingThis(mallocSizeOf);
|
|
|
|
n += Pool(true).mExpirationArray.ShallowSizeOfExcludingThis(mallocSizeOf);
|
|
|
|
n += Pool(false).mFrecencyArray.ShallowSizeOfExcludingThis(mallocSizeOf);
|
|
|
|
n += Pool(false).mExpirationArray.ShallowSizeOfExcludingThis(mallocSizeOf);
|
2014-02-27 03:11:40 +04:00
|
|
|
// Entries reported manually in CacheStorageService::CollectReports callback
|
|
|
|
if (sGlobalEntryTables) {
|
2015-07-31 07:19:57 +03:00
|
|
|
n += sGlobalEntryTables->ShallowSizeOfIncludingThis(mallocSizeOf);
|
2014-02-27 03:11:40 +04:00
|
|
|
}
|
2018-03-27 10:21:00 +03:00
|
|
|
n += mPurgeTimeStamps.SizeOfExcludingThis(mallocSizeOf);
|
2014-02-27 03:11:40 +04:00
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t CacheStorageService::SizeOfIncludingThis(
|
|
|
|
mozilla::MallocSizeOf mallocSizeOf) const {
|
|
|
|
return mallocSizeOf(this) + SizeOfExcludingThis(mallocSizeOf);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2016-08-24 08:23:45 +03:00
|
|
|
CacheStorageService::CollectReports(nsIHandleReportCallback* aHandleReport,
|
2014-05-21 10:06:54 +04:00
|
|
|
nsISupports* aData, bool aAnonymize) {
|
2021-12-22 01:52:17 +03:00
|
|
|
MutexAutoLock lock(mLock);
|
2014-02-27 03:11:40 +04:00
|
|
|
MOZ_COLLECT_REPORT("explicit/network/cache2/io", KIND_HEAP, UNITS_BYTES,
|
|
|
|
CacheFileIOManager::SizeOfIncludingThis(MallocSizeOf),
|
|
|
|
"Memory used by the cache IO manager.");
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2014-02-27 03:11:40 +04:00
|
|
|
MOZ_COLLECT_REPORT("explicit/network/cache2/index", KIND_HEAP, UNITS_BYTES,
|
|
|
|
CacheIndex::SizeOfIncludingThis(MallocSizeOf),
|
|
|
|
"Memory used by the cache index.");
|
|
|
|
|
|
|
|
// Report the service instance, this doesn't report entries, done lower
|
|
|
|
MOZ_COLLECT_REPORT("explicit/network/cache2/service", KIND_HEAP, UNITS_BYTES,
|
|
|
|
SizeOfIncludingThis(MallocSizeOf),
|
|
|
|
"Memory used by the cache storage service.");
|
|
|
|
|
|
|
|
// Report all entries, each storage separately (by the context key)
|
|
|
|
//
|
|
|
|
// References are:
|
|
|
|
// sGlobalEntryTables to N CacheEntryTable
|
|
|
|
// CacheEntryTable to N CacheEntry
|
|
|
|
// CacheEntry to 1 CacheFile
|
|
|
|
// CacheFile to
|
|
|
|
// N CacheFileChunk (keeping the actual data)
|
|
|
|
// 1 CacheFileMetadata (keeping http headers etc.)
|
|
|
|
// 1 CacheFileOutputStream
|
|
|
|
// N CacheFileInputStream
|
2015-06-17 18:23:43 +03:00
|
|
|
if (sGlobalEntryTables) {
|
2021-03-17 18:49:46 +03:00
|
|
|
for (const auto& globalEntry : *sGlobalEntryTables) {
|
2015-11-20 03:46:10 +03:00
|
|
|
CacheStorageService::Self()->Lock().AssertCurrentThreadOwns();
|
|
|
|
|
2021-03-17 18:49:46 +03:00
|
|
|
CacheEntryTable* table = globalEntry.GetWeak();
|
2015-11-20 03:46:10 +03:00
|
|
|
|
|
|
|
size_t size = 0;
|
|
|
|
mozilla::MallocSizeOf mallocSizeOf = CacheStorageService::MallocSizeOf;
|
|
|
|
|
|
|
|
size += table->ShallowSizeOfIncludingThis(mallocSizeOf);
|
2021-03-17 18:49:46 +03:00
|
|
|
for (const auto& tableEntry : *table) {
|
|
|
|
size += tableEntry.GetKey().SizeOfExcludingThisIfUnshared(mallocSizeOf);
|
2015-11-20 03:46:10 +03:00
|
|
|
|
|
|
|
// Bypass memory-only entries, those will be reported when iterating the
|
|
|
|
// memory only table. Memory-only entries are stored in both ALL_ENTRIES
|
|
|
|
// and MEMORY_ONLY hashtables.
|
2021-03-17 18:49:46 +03:00
|
|
|
RefPtr<mozilla::net::CacheEntry> const& entry = tableEntry.GetData();
|
2015-11-20 03:46:10 +03:00
|
|
|
if (table->Type() == CacheEntryTable::MEMORY_ONLY ||
|
|
|
|
entry->IsUsingDisk()) {
|
|
|
|
size += entry->SizeOfIncludingThis(mallocSizeOf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
aHandleReport->Callback(
|
2020-09-23 18:17:15 +03:00
|
|
|
""_ns,
|
2015-11-20 03:46:10 +03:00
|
|
|
nsPrintfCString(
|
|
|
|
"explicit/network/cache2/%s-storage(%s)",
|
|
|
|
table->Type() == CacheEntryTable::MEMORY_ONLY ? "memory" : "disk",
|
2021-03-17 18:49:46 +03:00
|
|
|
aAnonymize ? "<anonymized>"
|
|
|
|
: globalEntry.GetKey().BeginReading()),
|
2016-08-24 08:23:45 +03:00
|
|
|
nsIMemoryReporter::KIND_HEAP, nsIMemoryReporter::UNITS_BYTES, size,
|
2015-11-20 03:46:10 +03:00
|
|
|
"Memory used by the cache storage."_ns, aData);
|
|
|
|
}
|
2015-06-17 18:23:43 +03:00
|
|
|
}
|
2014-02-27 03:11:40 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2014-02-18 21:26:48 +04:00
|
|
|
|
2015-10-22 13:11:00 +03:00
|
|
|
// nsICacheTesting
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
CacheStorageService::IOThreadSuspender::Run() {
|
|
|
|
MonitorAutoLock mon(mMon);
|
2016-01-14 13:24:00 +03:00
|
|
|
while (!mSignaled) {
|
|
|
|
mon.Wait();
|
|
|
|
}
|
2015-10-22 13:11:00 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CacheStorageService::IOThreadSuspender::Notify() {
|
|
|
|
MonitorAutoLock mon(mMon);
|
2016-01-14 13:24:00 +03:00
|
|
|
mSignaled = true;
|
2015-10-22 13:11:00 +03:00
|
|
|
mon.Notify();
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
CacheStorageService::SuspendCacheIOThread(uint32_t aLevel) {
|
|
|
|
RefPtr<CacheIOThread> thread = CacheFileIOManager::IOThread();
|
|
|
|
if (!thread) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(!mActiveIOSuspender);
|
|
|
|
mActiveIOSuspender = new IOThreadSuspender();
|
|
|
|
return thread->Dispatch(mActiveIOSuspender, aLevel);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
CacheStorageService::ResumeCacheIOThread() {
|
|
|
|
MOZ_ASSERT(mActiveIOSuspender);
|
|
|
|
|
|
|
|
RefPtr<IOThreadSuspender> suspender;
|
|
|
|
suspender.swap(mActiveIOSuspender);
|
|
|
|
suspender->Notify();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-03-22 13:42:00 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
CacheStorageService::Flush(nsIObserver* aObserver) {
|
|
|
|
RefPtr<CacheIOThread> thread = CacheFileIOManager::IOThread();
|
|
|
|
if (!thread) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIObserverService> observerService =
|
|
|
|
mozilla::services::GetObserverService();
|
|
|
|
if (!observerService) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Adding as weak, the consumer is responsible to keep the reference
|
|
|
|
// until notified.
|
|
|
|
observerService->AddObserver(aObserver, "cacheservice:purge-memory-pools",
|
|
|
|
false);
|
|
|
|
|
|
|
|
// This runnable will do the purging and when done, notifies the above
|
|
|
|
// observer. We dispatch it to the CLOSE level, so all data writes scheduled
|
|
|
|
// up to this time will be done before this purging happens.
|
|
|
|
RefPtr<CacheStorageService::PurgeFromMemoryRunnable> r =
|
|
|
|
new CacheStorageService::PurgeFromMemoryRunnable(this,
|
|
|
|
CacheEntry::PURGE_WHOLE);
|
|
|
|
|
2016-10-12 11:32:00 +03:00
|
|
|
return thread->Dispatch(r, CacheIOThread::WRITE);
|
2016-03-22 13:42:00 +03:00
|
|
|
}
|
|
|
|
|
2021-06-04 15:35:16 +03:00
|
|
|
} // namespace mozilla::net
|