#ifndef CONTENT_BROWSER_CACHE_STORAGE_CACHE_STORAGE_INDEX_H_
#define CONTENT_BROWSER_CACHE_STORAGE_CACHE_STORAGE_INDEX_H_
#include <list>
#include <string>
#include <unordered_map>
#include "base/gtest_prod_util.h"
#include "content/browser/cache_storage/cache_storage.h"
#include "content/common/content_export.h"
namespace content {
class CONTENT_EXPORT CacheStorageIndex {
public:
struct CacheMetadata {
CacheMetadata(const std::u16string& name, int64_t size, int64_t padding)
: name(name), size(size), padding(padding) {}
std::u16string name;
int64_t size;
int64_t padding;
int32_t padding_version;
};
CacheStorageIndex();
CacheStorageIndex(const CacheStorageIndex&) = delete;
CacheStorageIndex& operator=(const CacheStorageIndex&) = delete;
~CacheStorageIndex();
CacheStorageIndex& operator=(CacheStorageIndex&& rhs);
void Insert(const CacheMetadata& cache_metadata);
void Delete(const std::u16string& cache_name);
bool SetCacheSize(const std::u16string& cache_name, int64_t size);
const CacheMetadata* GetMetadata(const std::u16string& cache_name) const;
bool SetCachePadding(const std::u16string& cache_name, int64_t padding);
const std::list<CacheMetadata>& ordered_cache_metadata() const {
return ordered_cache_metadata_;
}
size_t num_entries() const { return ordered_cache_metadata_.size(); }
int64_t GetPaddedStorageSize();
void DoomCache(const std::u16string& cache_name);
void FinalizeDoomedCache();
void RestoreDoomedCache();
private:
FRIEND_TEST_ALL_PREFIXES(CacheStorageIndexTest, TestSetCacheSize);
FRIEND_TEST_ALL_PREFIXES(CacheStorageIndexTest, TestSetCachePadding);
FRIEND_TEST_ALL_PREFIXES(CacheStorageIndexTest, TestInvalidCacheName);
void UpdateStorageSize();
void CalculateStoragePadding();
void ClearDoomedCache();
int64_t GetCacheSizeForTesting(const std::u16string& cache_name) const;
int64_t GetCachePaddingForTesting(const std::u16string& cache_name) const;
std::list<CacheMetadata> ordered_cache_metadata_;
std::unordered_map<std::u16string, std::list<CacheMetadata>::iterator>
cache_metadata_map_;
int64_t storage_size_ = CacheStorage::kSizeUnknown;
int64_t storage_padding_ = CacheStorage::kSizeUnknown;
CacheMetadata doomed_cache_metadata_;
std::list<CacheMetadata>::iterator after_doomed_cache_metadata_;
bool has_doomed_cache_ = false;
};
}
#endif