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