1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
|
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/browser/cache_storage/cache_storage_index.h"
#include <utility>
#include "base/containers/contains.h"
namespace content {
CacheStorageIndex::CacheStorageIndex()
: doomed_cache_metadata_(std::u16string(),
CacheStorage::kSizeUnknown,
CacheStorage::kSizeUnknown) {
ClearDoomedCache();
}
CacheStorageIndex::~CacheStorageIndex() = default;
CacheStorageIndex& CacheStorageIndex::operator=(CacheStorageIndex&& rhs) {
DCHECK(!has_doomed_cache_);
ordered_cache_metadata_ = std::move(rhs.ordered_cache_metadata_);
cache_metadata_map_ = std::move(rhs.cache_metadata_map_);
storage_size_ = rhs.storage_size_;
storage_padding_ = rhs.storage_padding_;
rhs.storage_size_ = CacheStorage::kSizeUnknown;
rhs.storage_padding_ = CacheStorage::kSizeUnknown;
return *this;
}
void CacheStorageIndex::Insert(const CacheMetadata& cache_metadata) {
DCHECK(!has_doomed_cache_);
DCHECK(!base::Contains(cache_metadata_map_, cache_metadata.name));
ordered_cache_metadata_.push_back(cache_metadata);
cache_metadata_map_[cache_metadata.name] = --ordered_cache_metadata_.end();
storage_size_ = CacheStorage::kSizeUnknown;
storage_padding_ = CacheStorage::kSizeUnknown;
}
void CacheStorageIndex::Delete(const std::u16string& cache_name) {
DCHECK(!has_doomed_cache_);
auto it = cache_metadata_map_.find(cache_name);
CHECK(it != cache_metadata_map_.end());
ordered_cache_metadata_.erase(it->second);
cache_metadata_map_.erase(it);
storage_size_ = CacheStorage::kSizeUnknown;
storage_padding_ = CacheStorage::kSizeUnknown;
}
bool CacheStorageIndex::SetCacheSize(const std::u16string& cache_name,
int64_t size) {
if (has_doomed_cache_)
DCHECK_NE(cache_name, doomed_cache_metadata_.name);
auto it = cache_metadata_map_.find(cache_name);
if (it == cache_metadata_map_.end()) {
// This can happen during initialization. The cache should be added to the
// map soon and the size will be set correctly at that point.
return false;
}
if (it->second->size == size) {
return false;
}
it->second->size = size;
storage_size_ = CacheStorage::kSizeUnknown;
return true;
}
const CacheStorageIndex::CacheMetadata* CacheStorageIndex::GetMetadata(
const std::u16string& cache_name) const {
const auto& it = cache_metadata_map_.find(cache_name);
if (it == cache_metadata_map_.end())
return nullptr;
return &*it->second;
}
int64_t CacheStorageIndex::GetCacheSizeForTesting(
const std::u16string& cache_name) const {
const auto& it = cache_metadata_map_.find(cache_name);
if (it == cache_metadata_map_.end())
return CacheStorage::kSizeUnknown;
return it->second->size;
}
bool CacheStorageIndex::SetCachePadding(const std::u16string& cache_name,
int64_t padding) {
DCHECK(!has_doomed_cache_ || cache_name != doomed_cache_metadata_.name)
<< "Setting padding of doomed cache: \"" << cache_name << '"';
auto it = cache_metadata_map_.find(cache_name);
if (it == cache_metadata_map_.end()) {
// This can happen during initialization. The cache should be added to the
// map soon and the padding will be set correctly at that point.
return false;
}
if (it->second->padding == padding) {
return false;
}
it->second->padding = padding;
storage_padding_ = CacheStorage::kSizeUnknown;
return true;
}
int64_t CacheStorageIndex::GetCachePaddingForTesting(
const std::u16string& cache_name) const {
const auto& it = cache_metadata_map_.find(cache_name);
if (it == cache_metadata_map_.end())
return CacheStorage::kSizeUnknown;
return it->second->padding;
}
int64_t CacheStorageIndex::GetPaddedStorageSize() {
if (storage_size_ == CacheStorage::kSizeUnknown)
UpdateStorageSize();
if (storage_padding_ == CacheStorage::kSizeUnknown)
CalculateStoragePadding();
if (storage_size_ == CacheStorage::kSizeUnknown ||
storage_padding_ == CacheStorage::kSizeUnknown) {
return CacheStorage::kSizeUnknown;
}
return storage_size_ + storage_padding_;
}
void CacheStorageIndex::UpdateStorageSize() {
int64_t storage_size = 0;
storage_size_ = CacheStorage::kSizeUnknown;
for (const CacheMetadata& info : ordered_cache_metadata_) {
if (info.size == CacheStorage::kSizeUnknown)
return;
storage_size += info.size;
}
storage_size_ = storage_size;
}
void CacheStorageIndex::CalculateStoragePadding() {
int64_t storage_padding = 0;
storage_padding_ = CacheStorage::kSizeUnknown;
for (const CacheMetadata& info : ordered_cache_metadata_) {
if (info.padding == CacheStorage::kSizeUnknown)
return;
storage_padding += info.padding;
}
storage_padding_ = storage_padding;
}
void CacheStorageIndex::DoomCache(const std::u16string& cache_name) {
DCHECK(!has_doomed_cache_);
auto map_it = cache_metadata_map_.find(cache_name);
CHECK(map_it != cache_metadata_map_.end());
doomed_cache_metadata_ = std::move(*(map_it->second));
after_doomed_cache_metadata_ = ordered_cache_metadata_.erase(map_it->second);
cache_metadata_map_.erase(map_it);
storage_size_ = CacheStorage::kSizeUnknown;
storage_padding_ = CacheStorage::kSizeUnknown;
has_doomed_cache_ = true;
}
void CacheStorageIndex::FinalizeDoomedCache() {
DCHECK(has_doomed_cache_);
ClearDoomedCache();
}
void CacheStorageIndex::RestoreDoomedCache() {
DCHECK(has_doomed_cache_);
const auto cache_name = doomed_cache_metadata_.name;
cache_metadata_map_[cache_name] = ordered_cache_metadata_.insert(
after_doomed_cache_metadata_, std::move(doomed_cache_metadata_));
after_doomed_cache_metadata_ = ordered_cache_metadata_.end();
storage_size_ = CacheStorage::kSizeUnknown;
storage_padding_ = CacheStorage::kSizeUnknown;
ClearDoomedCache();
}
void CacheStorageIndex::ClearDoomedCache() {
doomed_cache_metadata_.name.clear();
after_doomed_cache_metadata_ = ordered_cache_metadata_.end();
has_doomed_cache_ = false;
}
} // namespace content
|