#include "net/disk_cache/simple/simple_index.h"
#include <algorithm>
#include <limits>
#include <string>
#include <utility>
#include "base/check_op.h"
#include "base/files/file_util.h"
#include "base/functional/bind.h"
#include "base/not_fatal_until.h"
#include "base/numerics/safe_conversions.h"
#include "base/pickle.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_tokenizer.h"
#include "base/task/sequenced_task_runner.h"
#include "base/task/task_runner.h"
#include "base/time/time.h"
#include "base/trace_event/memory_usage_estimator.h"
#include "build/build_config.h"
#include "net/base/net_errors.h"
#include "net/disk_cache/backend_cleanup_tracker.h"
#include "net/disk_cache/simple/simple_entry_format.h"
#include "net/disk_cache/simple/simple_histogram_macros.h"
#include "net/disk_cache/simple/simple_index_delegate.h"
#include "net/disk_cache/simple/simple_index_file.h"
#include "net/disk_cache/simple/simple_synchronous_entry.h"
#include "net/disk_cache/simple/simple_util.h"
#if BUILDFLAG(IS_POSIX)
#include <sys/stat.h>
#include <sys/time.h>
#endif
namespace {
const int kWriteToDiskDelayMSecs = …;
const int kWriteToDiskOnBackgroundDelayMSecs = …;
const uint32_t kEvictionMarginDivisor = …;
const uint32_t kBytesInKb = …;
static const int kEstimatedEntryOverhead = …;
}
namespace disk_cache {
EntryMetadata::EntryMetadata()
: … { … }
EntryMetadata::EntryMetadata(base::Time last_used_time,
base::StrictNumeric<uint32_t> entry_size)
: … { … }
EntryMetadata::EntryMetadata(int32_t trailer_prefetch_size,
base::StrictNumeric<uint32_t> entry_size)
: … { … }
base::Time EntryMetadata::GetLastUsedTime() const { … }
void EntryMetadata::SetLastUsedTime(const base::Time& last_used_time) { … }
int32_t EntryMetadata::GetTrailerPrefetchSize() const { … }
void EntryMetadata::SetTrailerPrefetchSize(int32_t size) { … }
uint32_t EntryMetadata::GetEntrySize() const { … }
void EntryMetadata::SetEntrySize(base::StrictNumeric<uint32_t> entry_size) { … }
void EntryMetadata::Serialize(net::CacheType cache_type,
base::Pickle* pickle) const { … }
bool EntryMetadata::Deserialize(net::CacheType cache_type,
base::PickleIterator* it,
bool has_entry_in_memory_data,
bool app_cache_has_trailer_prefetch_size) { … }
SimpleIndex::SimpleIndex(
const scoped_refptr<base::SequencedTaskRunner>& task_runner,
scoped_refptr<BackendCleanupTracker> cleanup_tracker,
SimpleIndexDelegate* delegate,
net::CacheType cache_type,
std::unique_ptr<SimpleIndexFile> index_file)
: … { … }
SimpleIndex::~SimpleIndex() { … }
void SimpleIndex::Initialize(base::Time cache_mtime) { … }
void SimpleIndex::SetMaxSize(uint64_t max_bytes) { … }
void SimpleIndex::ExecuteWhenReady(net::CompletionOnceCallback task) { … }
std::unique_ptr<SimpleIndex::HashList> SimpleIndex::GetEntriesBetween(
base::Time initial_time,
base::Time end_time) { … }
std::unique_ptr<SimpleIndex::HashList> SimpleIndex::GetAllHashes() { … }
int32_t SimpleIndex::GetEntryCount() const { … }
uint64_t SimpleIndex::GetCacheSize() const { … }
uint64_t SimpleIndex::GetCacheSizeBetween(base::Time initial_time,
base::Time end_time) const { … }
base::Time SimpleIndex::GetLastUsedTime(uint64_t entry_hash) { … }
void SimpleIndex::SetLastUsedTimeForTest(uint64_t entry_hash,
const base::Time last_used) { … }
bool SimpleIndex::HasPendingWrite() const { … }
void SimpleIndex::Insert(uint64_t entry_hash) { … }
void SimpleIndex::Remove(uint64_t entry_hash) { … }
bool SimpleIndex::Has(uint64_t hash) const { … }
uint8_t SimpleIndex::GetEntryInMemoryData(uint64_t entry_hash) const { … }
void SimpleIndex::SetEntryInMemoryData(uint64_t entry_hash, uint8_t value) { … }
bool SimpleIndex::UseIfExists(uint64_t entry_hash) { … }
void SimpleIndex::StartEvictionIfNeeded() { … }
int32_t SimpleIndex::GetTrailerPrefetchSize(uint64_t entry_hash) const { … }
void SimpleIndex::SetTrailerPrefetchSize(uint64_t entry_hash, int32_t size) { … }
bool SimpleIndex::UpdateEntrySize(uint64_t entry_hash,
base::StrictNumeric<uint32_t> entry_size) { … }
void SimpleIndex::EvictionDone(int result) { … }
bool SimpleIndex::InsertInEntrySet(
uint64_t entry_hash,
const disk_cache::EntryMetadata& entry_metadata,
EntrySet* entry_set) { … }
void SimpleIndex::InsertEntryForTesting(uint64_t entry_hash,
const EntryMetadata& entry_metadata) { … }
void SimpleIndex::PostponeWritingToDisk() { … }
bool SimpleIndex::UpdateEntryIteratorSize(
EntrySet::iterator* it,
base::StrictNumeric<uint32_t> entry_size) { … }
void SimpleIndex::MergeInitializingSet(
std::unique_ptr<SimpleIndexLoadResult> load_result) { … }
#if BUILDFLAG(IS_ANDROID)
void SimpleIndex::OnApplicationStateChange(
base::android::ApplicationState state) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (state == base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES) {
app_on_background_ = false;
} else if (state ==
base::android::APPLICATION_STATE_HAS_STOPPED_ACTIVITIES) {
app_on_background_ = true;
WriteToDisk(INDEX_WRITE_REASON_ANDROID_STOPPED);
}
}
#endif
void SimpleIndex::WriteToDisk(IndexWriteToDiskReason reason) { … }
}