2014-04-10 14:47:20 +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 "CacheFileContextEvictor.h"
|
|
|
|
#include "CacheFileIOManager.h"
|
2021-01-25 15:23:13 +03:00
|
|
|
#include "CacheFileMetadata.h"
|
2014-04-10 14:47:20 +04:00
|
|
|
#include "CacheIndex.h"
|
|
|
|
#include "CacheIndexIterator.h"
|
|
|
|
#include "CacheFileUtils.h"
|
2021-01-25 15:23:13 +03:00
|
|
|
#include "CacheObserver.h"
|
2014-04-10 14:47:20 +04:00
|
|
|
#include "nsIFile.h"
|
|
|
|
#include "LoadContextInfo.h"
|
|
|
|
#include "nsThreadUtils.h"
|
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsIDirectoryEnumerator.h"
|
|
|
|
#include "mozilla/Base64.h"
|
2016-12-16 06:16:31 +03:00
|
|
|
#include "mozilla/IntegerPrintfMacros.h"
|
2020-12-01 11:12:16 +03:00
|
|
|
#include "nsContentUtils.h"
|
|
|
|
#include "nsNetUtil.h"
|
2014-04-10 14:47:20 +04:00
|
|
|
|
2021-06-04 15:35:16 +03:00
|
|
|
namespace mozilla::net {
|
2014-04-10 14:47:20 +04:00
|
|
|
|
2015-05-16 11:07:10 +03:00
|
|
|
#define CONTEXT_EVICTION_PREFIX "ce_"
|
2014-04-10 14:47:20 +04:00
|
|
|
const uint32_t kContextEvictionPrefixLength =
|
2015-05-16 11:07:10 +03:00
|
|
|
sizeof(CONTEXT_EVICTION_PREFIX) - 1;
|
2014-04-10 14:47:20 +04:00
|
|
|
|
|
|
|
bool CacheFileContextEvictor::sDiskAlreadySearched = false;
|
|
|
|
|
2021-06-11 10:10:41 +03:00
|
|
|
CacheFileContextEvictor::CacheFileContextEvictor() {
|
2014-04-10 14:47:20 +04:00
|
|
|
LOG(("CacheFileContextEvictor::CacheFileContextEvictor() [this=%p]", this));
|
|
|
|
}
|
|
|
|
|
|
|
|
CacheFileContextEvictor::~CacheFileContextEvictor() {
|
|
|
|
LOG(("CacheFileContextEvictor::~CacheFileContextEvictor() [this=%p]", this));
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult CacheFileContextEvictor::Init(nsIFile* aCacheDirectory) {
|
|
|
|
LOG(("CacheFileContextEvictor::Init()"));
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
MOZ_ASSERT(CacheFileIOManager::IsOnIOThread());
|
|
|
|
|
|
|
|
CacheIndex::IsUpToDate(&mIndexIsUpToDate);
|
|
|
|
|
|
|
|
mCacheDirectory = aCacheDirectory;
|
|
|
|
|
|
|
|
rv = aCacheDirectory->Clone(getter_AddRefs(mEntriesDir));
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2015-05-16 11:07:10 +03:00
|
|
|
rv = mEntriesDir->AppendNative(nsLiteralCString(ENTRIES_DIR));
|
2014-04-10 14:47:20 +04:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!sDiskAlreadySearched) {
|
|
|
|
LoadEvictInfoFromDisk();
|
|
|
|
if ((mEntries.Length() != 0) && mIndexIsUpToDate) {
|
|
|
|
CreateIterators();
|
|
|
|
StartEvicting();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2018-01-18 15:14:00 +03:00
|
|
|
void CacheFileContextEvictor::Shutdown() {
|
|
|
|
LOG(("CacheFileContextEvictor::Shutdown()"));
|
|
|
|
|
|
|
|
MOZ_ASSERT(CacheFileIOManager::IsOnIOThread());
|
|
|
|
|
|
|
|
CloseIterators();
|
|
|
|
}
|
|
|
|
|
2014-04-10 14:47:20 +04:00
|
|
|
uint32_t CacheFileContextEvictor::ContextsCount() {
|
|
|
|
MOZ_ASSERT(CacheFileIOManager::IsOnIOThread());
|
|
|
|
|
|
|
|
return mEntries.Length();
|
|
|
|
}
|
|
|
|
|
2015-10-22 13:11:00 +03:00
|
|
|
nsresult CacheFileContextEvictor::AddContext(
|
|
|
|
nsILoadContextInfo* aLoadContextInfo, bool aPinned,
|
2018-07-20 14:57:18 +03:00
|
|
|
const nsAString& aOrigin) {
|
2015-10-22 13:11:00 +03:00
|
|
|
LOG(
|
|
|
|
("CacheFileContextEvictor::AddContext() [this=%p, loadContextInfo=%p, "
|
|
|
|
"pinned=%d]",
|
|
|
|
this, aLoadContextInfo, aPinned));
|
2014-04-10 14:47:20 +04:00
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
MOZ_ASSERT(CacheFileIOManager::IsOnIOThread());
|
|
|
|
|
|
|
|
CacheFileContextEvictorEntry* entry = nullptr;
|
2015-10-22 13:11:00 +03:00
|
|
|
if (aLoadContextInfo) {
|
|
|
|
for (uint32_t i = 0; i < mEntries.Length(); ++i) {
|
|
|
|
if (mEntries[i]->mInfo && mEntries[i]->mInfo->Equals(aLoadContextInfo) &&
|
2018-07-20 14:57:18 +03:00
|
|
|
mEntries[i]->mPinned == aPinned &&
|
|
|
|
mEntries[i]->mOrigin.Equals(aOrigin)) {
|
2020-02-11 19:20:08 +03:00
|
|
|
entry = mEntries[i].get();
|
2015-10-22 13:11:00 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Not providing load context info means we want to delete everything,
|
|
|
|
// so let's not bother with any currently running context cleanups
|
|
|
|
// for the same pinning state.
|
|
|
|
for (uint32_t i = mEntries.Length(); i > 0;) {
|
|
|
|
--i;
|
|
|
|
if (mEntries[i]->mInfo && mEntries[i]->mPinned == aPinned) {
|
2018-07-20 14:57:18 +03:00
|
|
|
RemoveEvictInfoFromDisk(mEntries[i]->mInfo, mEntries[i]->mPinned,
|
|
|
|
mEntries[i]->mOrigin);
|
2015-10-22 13:11:00 +03:00
|
|
|
mEntries.RemoveElementAt(i);
|
|
|
|
}
|
2014-04-10 14:47:20 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!entry) {
|
|
|
|
entry = new CacheFileContextEvictorEntry();
|
|
|
|
entry->mInfo = aLoadContextInfo;
|
2015-10-22 13:11:00 +03:00
|
|
|
entry->mPinned = aPinned;
|
2018-07-20 14:57:18 +03:00
|
|
|
entry->mOrigin = aOrigin;
|
2020-02-11 19:20:08 +03:00
|
|
|
mEntries.AppendElement(WrapUnique(entry));
|
2014-04-10 14:47:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
entry->mTimeStamp = PR_Now() / PR_USEC_PER_MSEC;
|
|
|
|
|
2018-07-20 14:57:18 +03:00
|
|
|
PersistEvictionInfoToDisk(aLoadContextInfo, aPinned, aOrigin);
|
2014-04-10 14:47:20 +04:00
|
|
|
|
|
|
|
if (mIndexIsUpToDate) {
|
|
|
|
// Already existing context could be added again, in this case the iterator
|
|
|
|
// would be recreated. Close the old iterator explicitely.
|
|
|
|
if (entry->mIterator) {
|
|
|
|
entry->mIterator->Close();
|
|
|
|
entry->mIterator = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = CacheIndex::GetIterator(aLoadContextInfo, false,
|
|
|
|
getter_AddRefs(entry->mIterator));
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
// This could probably happen during shutdown. Remove the entry from
|
|
|
|
// the array, but leave the info on the disk. No entry can be opened
|
|
|
|
// during shutdown and we'll load the eviction info on next start.
|
|
|
|
LOG(
|
|
|
|
("CacheFileContextEvictor::AddContext() - Cannot get an iterator. "
|
2016-12-16 06:16:31 +03:00
|
|
|
"[rv=0x%08" PRIx32 "]",
|
|
|
|
static_cast<uint32_t>(rv)));
|
2014-04-10 14:47:20 +04:00
|
|
|
mEntries.RemoveElement(entry);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
StartEvicting();
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2019-12-02 16:21:06 +03:00
|
|
|
void CacheFileContextEvictor::CacheIndexStateChanged() {
|
2014-04-10 14:47:20 +04:00
|
|
|
LOG(("CacheFileContextEvictor::CacheIndexStateChanged() [this=%p]", this));
|
|
|
|
|
|
|
|
MOZ_ASSERT(CacheFileIOManager::IsOnIOThread());
|
|
|
|
|
|
|
|
bool isUpToDate = false;
|
|
|
|
CacheIndex::IsUpToDate(&isUpToDate);
|
|
|
|
if (mEntries.Length() == 0) {
|
|
|
|
// Just save the state and exit, since there is nothing to do
|
|
|
|
mIndexIsUpToDate = isUpToDate;
|
2019-12-02 16:21:06 +03:00
|
|
|
return;
|
2014-04-10 14:47:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!isUpToDate && !mIndexIsUpToDate) {
|
|
|
|
// Index is outdated and status has not changed, nothing to do.
|
2019-12-02 16:21:06 +03:00
|
|
|
return;
|
2014-04-10 14:47:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isUpToDate && mIndexIsUpToDate) {
|
|
|
|
// Status has not changed, but make sure the eviction is running.
|
|
|
|
if (mEvicting) {
|
2019-12-02 16:21:06 +03:00
|
|
|
return;
|
2014-04-10 14:47:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// We're not evicting, but we should be evicting?!
|
|
|
|
LOG(
|
|
|
|
("CacheFileContextEvictor::CacheIndexStateChanged() - Index is up to "
|
|
|
|
"date, we have some context to evict but eviction is not running! "
|
|
|
|
"Starting now."));
|
|
|
|
}
|
|
|
|
|
|
|
|
mIndexIsUpToDate = isUpToDate;
|
|
|
|
|
|
|
|
if (mIndexIsUpToDate) {
|
|
|
|
CreateIterators();
|
|
|
|
StartEvicting();
|
|
|
|
} else {
|
|
|
|
CloseIterators();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-02 16:21:06 +03:00
|
|
|
void CacheFileContextEvictor::WasEvicted(const nsACString& aKey, nsIFile* aFile,
|
|
|
|
bool* aEvictedAsPinned,
|
|
|
|
bool* aEvictedAsNonPinned) {
|
2014-04-10 14:47:20 +04:00
|
|
|
LOG(("CacheFileContextEvictor::WasEvicted() [key=%s]",
|
|
|
|
PromiseFlatCString(aKey).get()));
|
|
|
|
|
2015-10-22 13:11:00 +03:00
|
|
|
*aEvictedAsPinned = false;
|
|
|
|
*aEvictedAsNonPinned = false;
|
|
|
|
|
2014-04-10 14:47:20 +04:00
|
|
|
MOZ_ASSERT(CacheFileIOManager::IsOnIOThread());
|
|
|
|
|
|
|
|
nsCOMPtr<nsILoadContextInfo> info = CacheFileUtils::ParseKey(aKey);
|
|
|
|
MOZ_ASSERT(info);
|
|
|
|
if (!info) {
|
|
|
|
LOG(("CacheFileContextEvictor::WasEvicted() - Cannot parse key!"));
|
2019-12-02 16:21:06 +03:00
|
|
|
return;
|
2014-04-10 14:47:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < mEntries.Length(); ++i) {
|
2020-02-11 19:20:08 +03:00
|
|
|
const auto& entry = mEntries[i];
|
2015-10-22 13:11:00 +03:00
|
|
|
|
|
|
|
if (entry->mInfo && !info->Equals(entry->mInfo)) {
|
|
|
|
continue;
|
2014-04-10 14:47:20 +04:00
|
|
|
}
|
|
|
|
|
2015-10-22 13:11:00 +03:00
|
|
|
PRTime lastModifiedTime;
|
2019-12-02 16:21:06 +03:00
|
|
|
if (NS_FAILED(aFile->GetLastModifiedTime(&lastModifiedTime))) {
|
2015-10-22 13:11:00 +03:00
|
|
|
LOG(
|
|
|
|
("CacheFileContextEvictor::WasEvicted() - Cannot get last modified "
|
2019-12-02 16:21:06 +03:00
|
|
|
"time, returning."));
|
|
|
|
return;
|
2015-10-22 13:11:00 +03:00
|
|
|
}
|
2014-04-10 14:47:20 +04:00
|
|
|
|
2015-10-22 13:11:00 +03:00
|
|
|
if (lastModifiedTime > entry->mTimeStamp) {
|
|
|
|
// File has been modified since context eviction.
|
|
|
|
continue;
|
|
|
|
}
|
2014-04-10 14:47:20 +04:00
|
|
|
|
2015-10-22 13:11:00 +03:00
|
|
|
LOG(
|
|
|
|
("CacheFileContextEvictor::WasEvicted() - evicted [pinning=%d, "
|
2016-12-16 06:16:31 +03:00
|
|
|
"mTimeStamp=%" PRId64 ", lastModifiedTime=%" PRId64 "]",
|
2015-10-22 13:11:00 +03:00
|
|
|
entry->mPinned, entry->mTimeStamp, lastModifiedTime));
|
|
|
|
|
|
|
|
if (entry->mPinned) {
|
|
|
|
*aEvictedAsPinned = true;
|
|
|
|
} else {
|
|
|
|
*aEvictedAsNonPinned = true;
|
|
|
|
}
|
|
|
|
}
|
2014-04-10 14:47:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult CacheFileContextEvictor::PersistEvictionInfoToDisk(
|
2018-07-20 14:57:18 +03:00
|
|
|
nsILoadContextInfo* aLoadContextInfo, bool aPinned,
|
|
|
|
const nsAString& aOrigin) {
|
2014-04-10 14:47:20 +04:00
|
|
|
LOG(
|
|
|
|
("CacheFileContextEvictor::PersistEvictionInfoToDisk() [this=%p, "
|
|
|
|
"loadContextInfo=%p]",
|
|
|
|
this, aLoadContextInfo));
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
MOZ_ASSERT(CacheFileIOManager::IsOnIOThread());
|
|
|
|
|
|
|
|
nsCOMPtr<nsIFile> file;
|
2018-07-20 14:57:18 +03:00
|
|
|
rv = GetContextFile(aLoadContextInfo, aPinned, aOrigin, getter_AddRefs(file));
|
2014-04-10 14:47:20 +04:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2017-12-15 14:21:19 +03:00
|
|
|
nsCString path = file->HumanReadablePath();
|
2014-04-10 14:47:20 +04:00
|
|
|
|
|
|
|
PRFileDesc* fd;
|
|
|
|
rv =
|
|
|
|
file->OpenNSPRFileDesc(PR_RDWR | PR_CREATE_FILE | PR_TRUNCATE, 0600, &fd);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
LOG(
|
|
|
|
("CacheFileContextEvictor::PersistEvictionInfoToDisk() - Creating file "
|
2016-12-16 06:16:31 +03:00
|
|
|
"failed! [path=%s, rv=0x%08" PRIx32 "]",
|
|
|
|
path.get(), static_cast<uint32_t>(rv)));
|
2014-04-10 14:47:20 +04:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
PR_Close(fd);
|
|
|
|
|
|
|
|
LOG(
|
|
|
|
("CacheFileContextEvictor::PersistEvictionInfoToDisk() - Successfully "
|
|
|
|
"created file. [path=%s]",
|
|
|
|
path.get()));
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult CacheFileContextEvictor::RemoveEvictInfoFromDisk(
|
2018-07-20 14:57:18 +03:00
|
|
|
nsILoadContextInfo* aLoadContextInfo, bool aPinned,
|
|
|
|
const nsAString& aOrigin) {
|
2014-04-10 14:47:20 +04:00
|
|
|
LOG(
|
|
|
|
("CacheFileContextEvictor::RemoveEvictInfoFromDisk() [this=%p, "
|
|
|
|
"loadContextInfo=%p]",
|
|
|
|
this, aLoadContextInfo));
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
MOZ_ASSERT(CacheFileIOManager::IsOnIOThread());
|
|
|
|
|
|
|
|
nsCOMPtr<nsIFile> file;
|
2018-07-20 14:57:18 +03:00
|
|
|
rv = GetContextFile(aLoadContextInfo, aPinned, aOrigin, getter_AddRefs(file));
|
2014-04-10 14:47:20 +04:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2017-12-15 14:21:19 +03:00
|
|
|
nsCString path = file->HumanReadablePath();
|
2014-04-10 14:47:20 +04:00
|
|
|
|
|
|
|
rv = file->Remove(false);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
LOG(
|
|
|
|
("CacheFileContextEvictor::RemoveEvictionInfoFromDisk() - Removing file"
|
2016-12-16 06:16:31 +03:00
|
|
|
" failed! [path=%s, rv=0x%08" PRIx32 "]",
|
|
|
|
path.get(), static_cast<uint32_t>(rv)));
|
2014-04-10 14:47:20 +04:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG(
|
|
|
|
("CacheFileContextEvictor::RemoveEvictionInfoFromDisk() - Successfully "
|
|
|
|
"removed file. [path=%s]",
|
|
|
|
path.get()));
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult CacheFileContextEvictor::LoadEvictInfoFromDisk() {
|
|
|
|
LOG(("CacheFileContextEvictor::LoadEvictInfoFromDisk() [this=%p]", this));
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
MOZ_ASSERT(CacheFileIOManager::IsOnIOThread());
|
|
|
|
|
|
|
|
sDiskAlreadySearched = true;
|
|
|
|
|
2018-05-20 06:17:45 +03:00
|
|
|
nsCOMPtr<nsIDirectoryEnumerator> dirEnum;
|
|
|
|
rv = mCacheDirectory->GetDirectoryEntries(getter_AddRefs(dirEnum));
|
2014-04-10 14:47:20 +04:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
nsCOMPtr<nsIFile> file;
|
|
|
|
rv = dirEnum->GetNextFile(getter_AddRefs(file));
|
|
|
|
if (!file) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isDir = false;
|
|
|
|
file->IsDirectory(&isDir);
|
|
|
|
if (isDir) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoCString leaf;
|
|
|
|
rv = file->GetNativeLeafName(leaf);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
LOG(
|
|
|
|
("CacheFileContextEvictor::LoadEvictInfoFromDisk() - "
|
|
|
|
"GetNativeLeafName() failed! Skipping file."));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (leaf.Length() < kContextEvictionPrefixLength) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-05-16 11:07:10 +03:00
|
|
|
if (!StringBeginsWith(leaf, nsLiteralCString(CONTEXT_EVICTION_PREFIX))) {
|
2014-04-10 14:47:20 +04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoCString encoded;
|
|
|
|
encoded = Substring(leaf, kContextEvictionPrefixLength);
|
|
|
|
encoded.ReplaceChar('-', '/');
|
|
|
|
|
|
|
|
nsAutoCString decoded;
|
|
|
|
rv = Base64Decode(encoded, decoded);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
LOG(
|
|
|
|
("CacheFileContextEvictor::LoadEvictInfoFromDisk() - Base64 decoding "
|
|
|
|
"failed. Removing the file. [file=%s]",
|
|
|
|
leaf.get()));
|
|
|
|
file->Remove(false);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-10-22 13:11:00 +03:00
|
|
|
bool pinned = decoded[0] == '\t';
|
|
|
|
if (pinned) {
|
|
|
|
decoded = Substring(decoded, 1);
|
|
|
|
}
|
2014-04-10 14:47:20 +04:00
|
|
|
|
2018-07-20 14:57:18 +03:00
|
|
|
// Let's see if we have an origin.
|
|
|
|
nsAutoCString origin;
|
|
|
|
if (decoded.Contains('\t')) {
|
|
|
|
auto split = decoded.Split('\t');
|
2019-09-02 20:26:08 +03:00
|
|
|
MOZ_ASSERT(decoded.CountChar('\t') == 1);
|
2018-07-20 14:57:18 +03:00
|
|
|
|
2020-12-17 15:36:47 +03:00
|
|
|
auto splitIt = split.begin();
|
|
|
|
origin = *splitIt;
|
|
|
|
++splitIt;
|
|
|
|
decoded = *splitIt;
|
2018-07-20 14:57:18 +03:00
|
|
|
}
|
|
|
|
|
2015-10-22 13:11:00 +03:00
|
|
|
nsCOMPtr<nsILoadContextInfo> info;
|
|
|
|
if (!"*"_ns.Equals(decoded)) {
|
|
|
|
// "*" is indication of 'delete all', info left null will pass
|
|
|
|
// to CacheFileContextEvictor::AddContext and clear all the cache data.
|
|
|
|
info = CacheFileUtils::ParseKey(decoded);
|
|
|
|
if (!info) {
|
|
|
|
LOG(
|
|
|
|
("CacheFileContextEvictor::LoadEvictInfoFromDisk() - Cannot parse "
|
|
|
|
"context key, removing file. [contextKey=%s, file=%s]",
|
|
|
|
decoded.get(), leaf.get()));
|
|
|
|
file->Remove(false);
|
|
|
|
continue;
|
|
|
|
}
|
2014-04-10 14:47:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
PRTime lastModifiedTime;
|
|
|
|
rv = file->GetLastModifiedTime(&lastModifiedTime);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
CacheFileContextEvictorEntry* entry = new CacheFileContextEvictorEntry();
|
|
|
|
entry->mInfo = info;
|
2015-10-22 13:11:00 +03:00
|
|
|
entry->mPinned = pinned;
|
2018-07-20 14:57:18 +03:00
|
|
|
CopyUTF8toUTF16(origin, entry->mOrigin);
|
2014-04-10 14:47:20 +04:00
|
|
|
entry->mTimeStamp = lastModifiedTime;
|
|
|
|
mEntries.AppendElement(entry);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult CacheFileContextEvictor::GetContextFile(
|
|
|
|
nsILoadContextInfo* aLoadContextInfo, bool aPinned,
|
2018-07-20 14:57:18 +03:00
|
|
|
const nsAString& aOrigin, nsIFile** _retval) {
|
2014-04-10 14:47:20 +04:00
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
nsAutoCString keyPrefix;
|
2015-10-22 13:11:00 +03:00
|
|
|
if (aPinned) {
|
|
|
|
// Mark pinned context files with a tab char at the start.
|
|
|
|
// Tab is chosen because it can never be used as a context key tag.
|
|
|
|
keyPrefix.Append('\t');
|
|
|
|
}
|
|
|
|
if (aLoadContextInfo) {
|
|
|
|
CacheFileUtils::AppendKeyPrefix(aLoadContextInfo, keyPrefix);
|
|
|
|
} else {
|
|
|
|
keyPrefix.Append('*');
|
|
|
|
}
|
2018-07-20 14:57:18 +03:00
|
|
|
if (!aOrigin.IsEmpty()) {
|
|
|
|
keyPrefix.Append('\t');
|
|
|
|
keyPrefix.Append(NS_ConvertUTF16toUTF8(aOrigin));
|
|
|
|
}
|
2014-04-10 14:47:20 +04:00
|
|
|
|
2020-08-19 20:45:16 +03:00
|
|
|
nsAutoCString leafName;
|
|
|
|
leafName.AssignLiteral(CONTEXT_EVICTION_PREFIX);
|
|
|
|
|
|
|
|
rv = Base64EncodeAppend(keyPrefix, leafName);
|
2014-04-10 14:47:20 +04:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Replace '/' with '-' since '/' cannot be part of the filename.
|
2020-08-19 20:45:16 +03:00
|
|
|
leafName.ReplaceChar('/', '-');
|
2014-04-10 14:47:20 +04:00
|
|
|
|
|
|
|
nsCOMPtr<nsIFile> file;
|
|
|
|
rv = mCacheDirectory->Clone(getter_AddRefs(file));
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = file->AppendNative(leafName);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
file.swap(*_retval);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CacheFileContextEvictor::CreateIterators() {
|
|
|
|
LOG(("CacheFileContextEvictor::CreateIterators() [this=%p]", this));
|
|
|
|
|
|
|
|
CloseIterators();
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < mEntries.Length();) {
|
|
|
|
rv = CacheIndex::GetIterator(mEntries[i]->mInfo, false,
|
|
|
|
getter_AddRefs(mEntries[i]->mIterator));
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
LOG(
|
|
|
|
("CacheFileContextEvictor::CreateIterators() - Cannot get an iterator"
|
2016-12-16 06:16:31 +03:00
|
|
|
". [rv=0x%08" PRIx32 "]",
|
|
|
|
static_cast<uint32_t>(rv)));
|
2014-04-10 14:47:20 +04:00
|
|
|
mEntries.RemoveElementAt(i);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CacheFileContextEvictor::CloseIterators() {
|
|
|
|
LOG(("CacheFileContextEvictor::CloseIterators() [this=%p]", this));
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < mEntries.Length(); ++i) {
|
|
|
|
if (mEntries[i]->mIterator) {
|
|
|
|
mEntries[i]->mIterator->Close();
|
|
|
|
mEntries[i]->mIterator = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CacheFileContextEvictor::StartEvicting() {
|
|
|
|
LOG(("CacheFileContextEvictor::StartEvicting() [this=%p]", this));
|
|
|
|
|
|
|
|
MOZ_ASSERT(CacheFileIOManager::IsOnIOThread());
|
|
|
|
|
|
|
|
if (mEvicting) {
|
2018-07-20 14:57:18 +03:00
|
|
|
LOG(("CacheFileContextEvictor::StartEvicting() - already evicting."));
|
2014-04-10 14:47:20 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mEntries.Length() == 0) {
|
|
|
|
LOG(("CacheFileContextEvictor::StartEvicting() - no context to evict."));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-12-02 16:21:06 +03:00
|
|
|
nsCOMPtr<nsIRunnable> ev =
|
|
|
|
NewRunnableMethod("net::CacheFileContextEvictor::EvictEntries", this,
|
|
|
|
&CacheFileContextEvictor::EvictEntries);
|
2014-04-10 14:47:20 +04:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<CacheIOThread> ioThread = CacheFileIOManager::IOThread();
|
2014-04-10 14:47:20 +04:00
|
|
|
|
|
|
|
nsresult rv = ioThread->Dispatch(ev, CacheIOThread::EVICT);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
LOG(
|
|
|
|
("CacheFileContextEvictor::StartEvicting() - Cannot dispatch event to "
|
2016-12-16 06:16:31 +03:00
|
|
|
"IO thread. [rv=0x%08" PRIx32 "]",
|
|
|
|
static_cast<uint32_t>(rv)));
|
2014-04-10 14:47:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
mEvicting = true;
|
|
|
|
}
|
|
|
|
|
2019-12-02 16:21:06 +03:00
|
|
|
void CacheFileContextEvictor::EvictEntries() {
|
2014-04-10 14:47:20 +04:00
|
|
|
LOG(("CacheFileContextEvictor::EvictEntries()"));
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
MOZ_ASSERT(CacheFileIOManager::IsOnIOThread());
|
|
|
|
|
|
|
|
mEvicting = false;
|
|
|
|
|
|
|
|
if (!mIndexIsUpToDate) {
|
|
|
|
LOG(
|
|
|
|
("CacheFileContextEvictor::EvictEntries() - Stopping evicting due to "
|
|
|
|
"outdated index."));
|
2019-12-02 16:21:06 +03:00
|
|
|
return;
|
2014-04-10 14:47:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
while (true) {
|
2016-08-09 21:29:40 +03:00
|
|
|
if (CacheObserver::ShuttingDown()) {
|
|
|
|
LOG(
|
|
|
|
("CacheFileContextEvictor::EvictEntries() - Stopping evicting due to "
|
|
|
|
"shutdown."));
|
|
|
|
mEvicting =
|
|
|
|
true; // We don't want to start eviction again during shutdown
|
|
|
|
// process. Setting this flag to true ensures it.
|
2019-12-02 16:21:06 +03:00
|
|
|
return;
|
2016-08-09 21:29:40 +03:00
|
|
|
}
|
|
|
|
|
2014-04-10 14:47:20 +04:00
|
|
|
if (CacheIOThread::YieldAndRerun()) {
|
|
|
|
LOG(
|
|
|
|
("CacheFileContextEvictor::EvictEntries() - Breaking loop for higher "
|
|
|
|
"level events."));
|
|
|
|
mEvicting = true;
|
2019-12-02 16:21:06 +03:00
|
|
|
return;
|
2014-04-10 14:47:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mEntries.Length() == 0) {
|
|
|
|
LOG(
|
|
|
|
("CacheFileContextEvictor::EvictEntries() - Stopping evicting, there "
|
|
|
|
"is no context to evict."));
|
2016-04-05 06:59:00 +03:00
|
|
|
|
|
|
|
// Allow index to notify AsyncGetDiskConsumption callbacks. The size is
|
|
|
|
// actual again.
|
|
|
|
CacheIndex::OnAsyncEviction(false);
|
2019-12-02 16:21:06 +03:00
|
|
|
return;
|
2014-04-10 14:47:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
SHA1Sum::Hash hash;
|
|
|
|
rv = mEntries[0]->mIterator->GetNextHash(&hash);
|
|
|
|
if (rv == NS_ERROR_NOT_AVAILABLE) {
|
|
|
|
LOG(
|
|
|
|
("CacheFileContextEvictor::EvictEntries() - No more entries left in "
|
|
|
|
"iterator. [iterator=%p, info=%p]",
|
|
|
|
mEntries[0]->mIterator.get(), mEntries[0]->mInfo.get()));
|
2018-07-20 14:57:18 +03:00
|
|
|
RemoveEvictInfoFromDisk(mEntries[0]->mInfo, mEntries[0]->mPinned,
|
|
|
|
mEntries[0]->mOrigin);
|
2014-04-10 14:47:20 +04:00
|
|
|
mEntries.RemoveElementAt(0);
|
|
|
|
continue;
|
2021-06-04 15:35:16 +03:00
|
|
|
}
|
|
|
|
if (NS_FAILED(rv)) {
|
2014-04-10 14:47:20 +04:00
|
|
|
LOG(
|
|
|
|
("CacheFileContextEvictor::EvictEntries() - Iterator failed to "
|
|
|
|
"provide next hash (shutdown?), keeping eviction info on disk."
|
|
|
|
" [iterator=%p, info=%p]",
|
|
|
|
mEntries[0]->mIterator.get(), mEntries[0]->mInfo.get()));
|
|
|
|
mEntries.RemoveElementAt(0);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG(
|
|
|
|
("CacheFileContextEvictor::EvictEntries() - Processing hash. "
|
|
|
|
"[hash=%08x%08x%08x%08x%08x, iterator=%p, info=%p]",
|
|
|
|
LOGSHA1(&hash), mEntries[0]->mIterator.get(),
|
|
|
|
mEntries[0]->mInfo.get()));
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<CacheFileHandle> handle;
|
2015-02-24 19:49:46 +03:00
|
|
|
CacheFileIOManager::gInstance->mHandles.GetHandle(&hash,
|
2014-04-10 14:47:20 +04:00
|
|
|
getter_AddRefs(handle));
|
|
|
|
if (handle) {
|
|
|
|
// We doom any active handle in CacheFileIOManager::EvictByContext(), so
|
|
|
|
// this must be a new one. Skip it.
|
|
|
|
LOG(
|
|
|
|
("CacheFileContextEvictor::EvictEntries() - Skipping entry since we "
|
|
|
|
"found an active handle. [handle=%p]",
|
|
|
|
handle.get()));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-10-22 13:11:00 +03:00
|
|
|
CacheIndex::EntryStatus status;
|
2017-04-10 14:22:23 +03:00
|
|
|
bool pinned = false;
|
|
|
|
auto callback = [&pinned](const CacheIndexEntry* aEntry) {
|
|
|
|
pinned = aEntry->IsPinned();
|
|
|
|
};
|
|
|
|
rv = CacheIndex::HasEntry(hash, &status, callback);
|
2015-10-22 13:11:00 +03:00
|
|
|
// This must never fail, since eviction (this code) happens only when the
|
|
|
|
// index is up-to-date and thus the informatin is known.
|
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(rv));
|
|
|
|
|
|
|
|
if (pinned != mEntries[0]->mPinned) {
|
|
|
|
LOG(
|
|
|
|
("CacheFileContextEvictor::EvictEntries() - Skipping entry since "
|
|
|
|
"pinning "
|
|
|
|
"doesn't match [evicting pinned=%d, entry pinned=%d]",
|
|
|
|
mEntries[0]->mPinned, pinned));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-07-20 14:57:18 +03:00
|
|
|
if (!mEntries[0]->mOrigin.IsEmpty()) {
|
|
|
|
nsCOMPtr<nsIFile> file;
|
|
|
|
CacheFileIOManager::gInstance->GetFile(&hash, getter_AddRefs(file));
|
|
|
|
|
|
|
|
// Read metadata from the file synchronously
|
|
|
|
RefPtr<CacheFileMetadata> metadata = new CacheFileMetadata();
|
|
|
|
rv = metadata->SyncReadMetadata(file);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now get the context + enhance id + URL from the key.
|
|
|
|
nsAutoCString uriSpec;
|
|
|
|
RefPtr<nsILoadContextInfo> info =
|
2019-12-02 16:21:06 +03:00
|
|
|
CacheFileUtils::ParseKey(metadata->GetKey(), nullptr, &uriSpec);
|
2018-07-20 14:57:18 +03:00
|
|
|
MOZ_ASSERT(info);
|
|
|
|
if (!info) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-12-01 11:12:16 +03:00
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
rv = NS_NewURI(getter_AddRefs(uri), uriSpec);
|
2018-07-20 14:57:18 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
LOG(
|
|
|
|
("CacheFileContextEvictor::EvictEntries() - Skipping entry since "
|
2020-12-01 11:12:16 +03:00
|
|
|
"NS_NewURI failed to parse the uriSpec"));
|
2018-07-20 14:57:18 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-12-01 11:12:16 +03:00
|
|
|
nsAutoString urlOrigin;
|
|
|
|
rv = nsContentUtils::GetUTFOrigin(uri, urlOrigin);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
LOG(
|
|
|
|
("CacheFileContextEvictor::EvictEntries() - Skipping entry since "
|
|
|
|
"We failed to extract an origin"));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!urlOrigin.Equals(mEntries[0]->mOrigin)) {
|
2018-07-20 14:57:18 +03:00
|
|
|
LOG(
|
|
|
|
("CacheFileContextEvictor::EvictEntries() - Skipping entry since "
|
|
|
|
"origin "
|
|
|
|
"doesn't match"));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-10 14:47:20 +04:00
|
|
|
nsAutoCString leafName;
|
|
|
|
CacheFileIOManager::HashToStr(&hash, leafName);
|
|
|
|
|
|
|
|
PRTime lastModifiedTime;
|
|
|
|
nsCOMPtr<nsIFile> file;
|
|
|
|
rv = mEntriesDir->Clone(getter_AddRefs(file));
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
rv = file->AppendNative(leafName);
|
|
|
|
}
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
rv = file->GetLastModifiedTime(&lastModifiedTime);
|
|
|
|
}
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
LOG(
|
|
|
|
("CacheFileContextEvictor::EvictEntries() - Cannot get last modified "
|
|
|
|
"time, skipping entry."));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lastModifiedTime > mEntries[0]->mTimeStamp) {
|
|
|
|
LOG(
|
|
|
|
("CacheFileContextEvictor::EvictEntries() - Skipping newer entry. "
|
2016-12-16 06:16:31 +03:00
|
|
|
"[mTimeStamp=%" PRId64 ", lastModifiedTime=%" PRId64 "]",
|
|
|
|
mEntries[0]->mTimeStamp, lastModifiedTime));
|
2014-04-10 14:47:20 +04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG(("CacheFileContextEvictor::EvictEntries - Removing entry."));
|
|
|
|
file->Remove(false);
|
|
|
|
CacheIndex::RemoveEntry(&hash);
|
|
|
|
}
|
|
|
|
|
2018-06-18 08:43:11 +03:00
|
|
|
MOZ_ASSERT_UNREACHABLE("We should never get here");
|
2014-04-10 14:47:20 +04:00
|
|
|
}
|
|
|
|
|
2021-06-04 15:35:16 +03:00
|
|
|
} // namespace mozilla::net
|