#include "net/disk_cache/simple/simple_backend_impl.h"
#include <algorithm>
#include <cstdlib>
#include <functional>
#include <limits>
#include "base/functional/callback_helpers.h"
#include "base/task/sequenced_task_runner.h"
#include "base/task/thread_pool.h"
#include "build/build_config.h"
#if BUILDFLAG(IS_POSIX)
#include <sys/resource.h>
#endif
#include "base/files/file_util.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/lazy_instance.h"
#include "base/location.h"
#include "base/memory/ptr_util.h"
#include "base/metrics/field_trial.h"
#include "base/metrics/field_trial_params.h"
#include "base/metrics/histogram_functions.h"
#include "base/metrics/histogram_macros.h"
#include "base/system/sys_info.h"
#include "base/task/thread_pool/thread_pool_instance.h"
#include "base/time/time.h"
#include "build/build_config.h"
#include "net/base/net_errors.h"
#include "net/base/prioritized_task_runner.h"
#include "net/disk_cache/backend_cleanup_tracker.h"
#include "net/disk_cache/cache_util.h"
#include "net/disk_cache/simple/simple_entry_format.h"
#include "net/disk_cache/simple/simple_entry_impl.h"
#include "net/disk_cache/simple/simple_file_tracker.h"
#include "net/disk_cache/simple/simple_histogram_macros.h"
#include "net/disk_cache/simple/simple_index.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"
#include "net/disk_cache/simple/simple_version_upgrade.h"
FilePath;
Time;
namespace disk_cache {
namespace {
const int kMaxFileRatio = …;
const int kMaxNativeCodeFileRatio = …;
const int64_t kMinFileSizeLimit = …;
base::LazyInstance<SimpleFileTracker>::Leaky g_simple_file_tracker = …;
SimpleCacheConsistencyResult FileStructureConsistent(
BackendFileOperations* file_operations,
const base::FilePath& path) { … }
struct BarrierContext { … };
void BarrierCompletionCallbackImpl(
BarrierContext* context,
int result) { … }
base::RepeatingCallback<void(int)> MakeBarrierCompletionCallback(
int count,
net::CompletionOnceCallback final_callback) { … }
void RunOperationAndCallback(
base::WeakPtr<SimpleBackendImpl> backend,
base::OnceCallback<net::Error(net::CompletionOnceCallback)> operation,
net::CompletionOnceCallback operation_callback) { … }
void RunEntryResultOperationAndCallback(
base::WeakPtr<SimpleBackendImpl> backend,
base::OnceCallback<EntryResult(EntryResultCallback)> operation,
EntryResultCallback operation_callback) { … }
void RecordIndexLoad(net::CacheType cache_type,
base::TimeTicks constructed_since,
int result) { … }
SimpleEntryImpl::OperationsMode CacheTypeToOperationsMode(net::CacheType type) { … }
}
class SimpleBackendImpl::ActiveEntryProxy
: public SimpleEntryImpl::ActiveEntryProxy { … };
SimpleBackendImpl::SimpleBackendImpl(
scoped_refptr<BackendFileOperationsFactory> file_operations_factory,
const FilePath& path,
scoped_refptr<BackendCleanupTracker> cleanup_tracker,
SimpleFileTracker* file_tracker,
int64_t max_bytes,
net::CacheType cache_type,
net::NetLog* net_log)
: … { … }
SimpleBackendImpl::~SimpleBackendImpl() { … }
void SimpleBackendImpl::SetTaskRunnerForTesting(
scoped_refptr<base::SequencedTaskRunner> task_runner) { … }
void SimpleBackendImpl::Init(CompletionOnceCallback completion_callback) { … }
bool SimpleBackendImpl::SetMaxSize(int64_t max_bytes) { … }
int64_t SimpleBackendImpl::MaxFileSize() const { … }
scoped_refptr<SimplePostOperationWaiterTable> SimpleBackendImpl::OnDoomStart(
uint64_t entry_hash) { … }
void SimpleBackendImpl::DoomEntries(std::vector<uint64_t>* entry_hashes,
net::CompletionOnceCallback callback) { … }
int32_t SimpleBackendImpl::GetEntryCount() const { … }
EntryResult SimpleBackendImpl::OpenEntry(const std::string& key,
net::RequestPriority request_priority,
EntryResultCallback callback) { … }
EntryResult SimpleBackendImpl::CreateEntry(
const std::string& key,
net::RequestPriority request_priority,
EntryResultCallback callback) { … }
EntryResult SimpleBackendImpl::OpenOrCreateEntry(
const std::string& key,
net::RequestPriority request_priority,
EntryResultCallback callback) { … }
scoped_refptr<SimpleEntryImpl>
SimpleBackendImpl::MaybeOptimisticCreateForPostDoom(
uint64_t entry_hash,
const std::string& key,
net::RequestPriority request_priority,
std::vector<base::OnceClosure>* post_doom) { … }
net::Error SimpleBackendImpl::DoomEntry(const std::string& key,
net::RequestPriority priority,
CompletionOnceCallback callback) { … }
net::Error SimpleBackendImpl::DoomAllEntries(CompletionOnceCallback callback) { … }
net::Error SimpleBackendImpl::DoomEntriesBetween(
const Time initial_time,
const Time end_time,
CompletionOnceCallback callback) { … }
net::Error SimpleBackendImpl::DoomEntriesSince(
const Time initial_time,
CompletionOnceCallback callback) { … }
int64_t SimpleBackendImpl::CalculateSizeOfAllEntries(
Int64CompletionOnceCallback callback) { … }
int64_t SimpleBackendImpl::CalculateSizeOfEntriesBetween(
base::Time initial_time,
base::Time end_time,
Int64CompletionOnceCallback callback) { … }
class SimpleBackendImpl::SimpleIterator final : public Iterator { … };
std::unique_ptr<Backend::Iterator> SimpleBackendImpl::CreateIterator() { … }
void SimpleBackendImpl::GetStats(base::StringPairs* stats) { … }
void SimpleBackendImpl::OnExternalCacheHit(const std::string& key) { … }
uint8_t SimpleBackendImpl::GetEntryInMemoryData(const std::string& key) { … }
void SimpleBackendImpl::SetEntryInMemoryData(const std::string& key,
uint8_t data) { … }
void SimpleBackendImpl::InitializeIndex(CompletionOnceCallback callback,
const DiskStatResult& result) { … }
void SimpleBackendImpl::IndexReadyForDoom(Time initial_time,
Time end_time,
CompletionOnceCallback callback,
int result) { … }
void SimpleBackendImpl::IndexReadyForSizeCalculation(
Int64CompletionOnceCallback callback,
int result) { … }
void SimpleBackendImpl::IndexReadyForSizeBetweenCalculation(
base::Time initial_time,
base::Time end_time,
Int64CompletionOnceCallback callback,
int result) { … }
SimpleBackendImpl::DiskStatResult SimpleBackendImpl::InitCacheStructureOnDisk(
std::unique_ptr<BackendFileOperations> file_operations,
const base::FilePath& path,
uint64_t suggested_max_size,
net::CacheType cache_type) { … }
scoped_refptr<SimpleEntryImpl>
SimpleBackendImpl::CreateOrFindActiveOrDoomedEntry(
const uint64_t entry_hash,
const std::string& key,
net::RequestPriority request_priority,
std::vector<base::OnceClosure>*& post_operation,
PostOperationQueue& post_operation_queue) { … }
EntryResult SimpleBackendImpl::OpenEntryFromHash(uint64_t entry_hash,
EntryResultCallback callback) { … }
net::Error SimpleBackendImpl::DoomEntryFromHash(
uint64_t entry_hash,
CompletionOnceCallback callback) { … }
void SimpleBackendImpl::OnEntryOpenedFromHash(
uint64_t hash,
EntryResultCallback callback,
EntryResult result) { … }
void SimpleBackendImpl::DoomEntriesComplete(
std::unique_ptr<std::vector<uint64_t>> entry_hashes,
CompletionOnceCallback callback,
int result) { … }
uint32_t SimpleBackendImpl::GetNewEntryPriority(
net::RequestPriority request_priority) { … }
}