#ifndef NET_DISK_CACHE_SIMPLE_SIMPLE_BACKEND_IMPL_H_
#define NET_DISK_CACHE_SIMPLE_SIMPLE_BACKEND_IMPL_H_
#include <stdint.h>
#include <memory>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
#include "base/compiler_specific.h"
#include "base/files/file_path.h"
#include "base/functional/callback_forward.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/scoped_refptr.h"
#include "base/memory/weak_ptr.h"
#include "base/strings/string_split.h"
#include "base/task/sequenced_task_runner.h"
#include "base/task/task_traits.h"
#include "base/time/time.h"
#include "build/build_config.h"
#include "net/base/cache_type.h"
#include "net/base/net_export.h"
#include "net/disk_cache/disk_cache.h"
#include "net/disk_cache/simple/post_operation_waiter.h"
#include "net/disk_cache/simple/simple_entry_impl.h"
#include "net/disk_cache/simple/simple_index_delegate.h"
namespace net {
class PrioritizedTaskRunner;
}
namespace disk_cache {
class BackendCleanupTracker;
class BackendFileOperationsFactory;
class SimpleEntryImpl;
class SimpleFileTracker;
class SimpleIndex;
class NET_EXPORT_PRIVATE SimpleBackendImpl final : public Backend,
public SimpleIndexDelegate {
public:
SimpleBackendImpl(
scoped_refptr<BackendFileOperationsFactory> file_operations_factory,
const base::FilePath& path,
scoped_refptr<BackendCleanupTracker> cleanup_tracker,
SimpleFileTracker* file_tracker,
int64_t max_bytes,
net::CacheType cache_type,
net::NetLog* net_log);
~SimpleBackendImpl() override;
SimpleIndex* index() { return index_.get(); }
void SetTaskRunnerForTesting(
scoped_refptr<base::SequencedTaskRunner> task_runner);
void Init(CompletionOnceCallback completion_callback);
int64_t MaxFileSize() const override;
scoped_refptr<SimplePostOperationWaiterTable> OnDoomStart(
uint64_t entry_hash);
void DoomEntries(std::vector<uint64_t>* entry_hashes,
CompletionOnceCallback callback) override;
int32_t GetEntryCount(
net::Int32CompletionOnceCallback callback) const override;
EntryResult OpenEntry(const std::string& key,
net::RequestPriority request_priority,
EntryResultCallback callback) override;
EntryResult CreateEntry(const std::string& key,
net::RequestPriority request_priority,
EntryResultCallback callback) override;
EntryResult OpenOrCreateEntry(const std::string& key,
net::RequestPriority priority,
EntryResultCallback callback) override;
net::Error DoomEntry(const std::string& key,
net::RequestPriority priority,
CompletionOnceCallback callback) override;
net::Error DoomAllEntries(CompletionOnceCallback callback) override;
net::Error DoomEntriesBetween(base::Time initial_time,
base::Time end_time,
CompletionOnceCallback callback) override;
net::Error DoomEntriesSince(base::Time initial_time,
CompletionOnceCallback callback) override;
int64_t CalculateSizeOfAllEntries(
Int64CompletionOnceCallback callback) override;
int64_t CalculateSizeOfEntriesBetween(
base::Time initial_time,
base::Time end_time,
Int64CompletionOnceCallback callback) override;
std::unique_ptr<Iterator> CreateIterator() override;
void GetStats(base::StringPairs* stats) override;
void OnExternalCacheHit(const std::string& key) override;
uint8_t GetEntryInMemoryData(const std::string& key) override;
net::PrioritizedTaskRunner* prioritized_task_runner() const {
return prioritized_task_runner_.get();
}
static constexpr base::TaskTraits kWorkerPoolTaskTraits = {
base::MayBlock(), base::WithBaseSyncPrimitives(),
base::TaskPriority::USER_BLOCKING,
base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN};
#if BUILDFLAG(IS_ANDROID)
void set_app_status_listener_getter(
ApplicationStatusListenerGetter app_status_listener_getter) {
app_status_listener_getter_ = std::move(app_status_listener_getter);
}
#endif
base::WeakPtr<SimpleBackendImpl> AsWeakPtr() {
return weak_ptr_factory_.GetWeakPtr();
}
private:
class SimpleIterator;
friend class SimpleIterator;
using EntryMap =
std::unordered_map<uint64_t, raw_ptr<SimpleEntryImpl, CtnExperimental>>;
class ActiveEntryProxy;
friend class ActiveEntryProxy;
struct DiskStatResult {
base::Time cache_dir_mtime;
uint64_t max_size;
bool detected_magic_number_mismatch;
int net_error;
};
enum class PostOperationQueue { kNone, kPostDoom, kPostOpenByHash };
void InitializeIndex(CompletionOnceCallback callback,
const DiskStatResult& result);
void IndexReadyForDoom(base::Time initial_time,
base::Time end_time,
CompletionOnceCallback callback,
int result);
void IndexReadyForSizeCalculation(Int64CompletionOnceCallback callback,
int result);
void IndexReadyForSizeBetweenCalculation(base::Time initial_time,
base::Time end_time,
Int64CompletionOnceCallback callback,
int result);
static DiskStatResult InitCacheStructureOnDisk(
std::unique_ptr<BackendFileOperations> file_operations,
const base::FilePath& path,
uint64_t suggested_max_size,
net::CacheType cache_type);
scoped_refptr<SimpleEntryImpl> CreateOrFindActiveOrDoomedEntry(
uint64_t entry_hash,
const std::string& key,
net::RequestPriority request_priority,
std::vector<base::OnceClosure>*& post_operation,
PostOperationQueue& post_operation_queue);
scoped_refptr<SimpleEntryImpl> MaybeOptimisticCreateForPostDoom(
uint64_t entry_hash,
const std::string& key,
net::RequestPriority request_priority,
std::vector<base::OnceClosure>* post_doom);
EntryResult OpenEntryFromHash(uint64_t entry_hash,
EntryResultCallback callback);
net::Error DoomEntryFromHash(uint64_t entry_hash,
CompletionOnceCallback callback);
void OnEntryOpenedFromHash(uint64_t hash,
EntryResultCallback callback,
EntryResult result);
void DoomEntriesComplete(std::unique_ptr<std::vector<uint64_t>> entry_hashes,
CompletionOnceCallback callback,
int result);
uint32_t GetNewEntryPriority(net::RequestPriority request_priority);
scoped_refptr<BackendFileOperationsFactory> file_operations_factory_;
scoped_refptr<BackendCleanupTracker> cleanup_tracker_;
const raw_ptr<SimpleFileTracker> file_tracker_;
const base::FilePath path_;
std::unique_ptr<SimpleIndex> index_;
scoped_refptr<net::PrioritizedTaskRunner> prioritized_task_runner_;
int64_t orig_max_size_;
const SimpleEntryImpl::OperationsMode entry_operations_mode_;
EntryMap active_entries_;
scoped_refptr<SimplePostOperationWaiterTable> post_doom_waiting_;
scoped_refptr<SimplePostOperationWaiterTable> post_open_by_hash_waiting_;
const raw_ptr<net::NetLog> net_log_;
uint32_t entry_count_ = 0;
#if BUILDFLAG(IS_ANDROID)
ApplicationStatusListenerGetter app_status_listener_getter_;
#endif
base::WeakPtrFactory<SimpleBackendImpl> weak_ptr_factory_{this};
};
}
#endif