| 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_("", | 12 : doomed_cache_metadata_("", CacheStorage::kSizeUnknown) { |
| 13 CacheStorage::kSizeUnknown, | |
| 14 CacheStorage::kSizeUnknown, | |
| 15 "") { | |
| 16 ClearDoomedCache(); | 13 ClearDoomedCache(); |
| 17 } | 14 } |
| 18 | 15 |
| 19 CacheStorageIndex::~CacheStorageIndex() = default; | 16 CacheStorageIndex::~CacheStorageIndex() = default; |
| 20 | 17 |
| 21 CacheStorageIndex& CacheStorageIndex::operator=(CacheStorageIndex&& rhs) { | 18 CacheStorageIndex& CacheStorageIndex::operator=(CacheStorageIndex&& rhs) { |
| 22 DCHECK(!has_doomed_cache_); | 19 DCHECK(!has_doomed_cache_); |
| 23 ordered_cache_metadata_ = std::move(rhs.ordered_cache_metadata_); | 20 ordered_cache_metadata_ = std::move(rhs.ordered_cache_metadata_); |
| 24 cache_metadata_map_ = std::move(rhs.cache_metadata_map_); | 21 cache_metadata_map_ = std::move(rhs.cache_metadata_map_); |
| 25 storage_size_ = rhs.storage_size_; | 22 storage_size_ = rhs.storage_size_; |
| 26 storage_padding_ = rhs.storage_padding_; | |
| 27 rhs.storage_size_ = CacheStorage::kSizeUnknown; | 23 rhs.storage_size_ = CacheStorage::kSizeUnknown; |
| 28 rhs.storage_padding_ = CacheStorage::kSizeUnknown; | |
| 29 return *this; | 24 return *this; |
| 30 } | 25 } |
| 31 | 26 |
| 32 void CacheStorageIndex::Insert(const CacheMetadata& cache_metadata) { | 27 void CacheStorageIndex::Insert(const CacheMetadata& cache_metadata) { |
| 33 DCHECK(!has_doomed_cache_); | 28 DCHECK(!has_doomed_cache_); |
| 34 DCHECK(cache_metadata_map_.find(cache_metadata.name) == | 29 DCHECK(cache_metadata_map_.find(cache_metadata.name) == |
| 35 cache_metadata_map_.end()); | 30 cache_metadata_map_.end()); |
| 36 ordered_cache_metadata_.push_back(cache_metadata); | 31 ordered_cache_metadata_.push_back(cache_metadata); |
| 37 cache_metadata_map_[cache_metadata.name] = --ordered_cache_metadata_.end(); | 32 cache_metadata_map_[cache_metadata.name] = --ordered_cache_metadata_.end(); |
| 38 storage_size_ = CacheStorage::kSizeUnknown; | 33 storage_size_ = CacheStorage::kSizeUnknown; |
| 39 storage_padding_ = CacheStorage::kSizeUnknown; | |
| 40 } | 34 } |
| 41 | 35 |
| 42 void CacheStorageIndex::Delete(const std::string& cache_name) { | 36 void CacheStorageIndex::Delete(const std::string& cache_name) { |
| 43 DCHECK(!has_doomed_cache_); | 37 DCHECK(!has_doomed_cache_); |
| 44 auto it = cache_metadata_map_.find(cache_name); | 38 auto it = cache_metadata_map_.find(cache_name); |
| 45 DCHECK(it != cache_metadata_map_.end()); | 39 DCHECK(it != cache_metadata_map_.end()); |
| 46 ordered_cache_metadata_.erase(it->second); | 40 ordered_cache_metadata_.erase(it->second); |
| 47 cache_metadata_map_.erase(it); | 41 cache_metadata_map_.erase(it); |
| 48 storage_size_ = CacheStorage::kSizeUnknown; | 42 storage_size_ = CacheStorage::kSizeUnknown; |
| 49 storage_padding_ = CacheStorage::kSizeUnknown; | |
| 50 } | 43 } |
| 51 | 44 |
| 52 bool CacheStorageIndex::SetCacheSize(const std::string& cache_name, | 45 bool CacheStorageIndex::SetCacheSize(const std::string& cache_name, |
| 53 int64_t size) { | 46 int64_t size) { |
| 54 if (has_doomed_cache_) | 47 if (has_doomed_cache_) |
| 55 DCHECK_NE(cache_name, doomed_cache_metadata_.name); | 48 DCHECK_NE(cache_name, doomed_cache_metadata_.name); |
| 56 auto it = cache_metadata_map_.find(cache_name); | 49 auto it = cache_metadata_map_.find(cache_name); |
| 57 DCHECK(it != cache_metadata_map_.end()); | 50 DCHECK(it != cache_metadata_map_.end()); |
| 58 if (it->second->size == size) | 51 if (it->second->size == size) |
| 59 return false; | 52 return false; |
| 60 it->second->size = size; | 53 it->second->size = size; |
| 61 storage_size_ = CacheStorage::kSizeUnknown; | 54 storage_size_ = CacheStorage::kSizeUnknown; |
| 62 return true; | 55 return true; |
| 63 } | 56 } |
| 64 | 57 |
| 65 const CacheStorageIndex::CacheMetadata* CacheStorageIndex::GetMetadata( | 58 int64_t CacheStorageIndex::GetCacheSize(const std::string& cache_name) const { |
| 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 { | |
| 75 const auto& it = cache_metadata_map_.find(cache_name); | 59 const auto& it = cache_metadata_map_.find(cache_name); |
| 76 if (it == cache_metadata_map_.end()) | 60 if (it == cache_metadata_map_.end()) |
| 77 return CacheStorage::kSizeUnknown; | 61 return CacheStorage::kSizeUnknown; |
| 78 return it->second->size; | 62 return it->second->size; |
| 79 } | 63 } |
| 80 | 64 |
| 81 bool CacheStorageIndex::SetCachePadding(const std::string& cache_name, | 65 int64_t CacheStorageIndex::GetStorageSize() { |
| 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() { | |
| 103 if (storage_size_ == CacheStorage::kSizeUnknown) | 66 if (storage_size_ == CacheStorage::kSizeUnknown) |
| 104 UpdateStorageSize(); | 67 UpdateStorageSize(); |
| 105 if (storage_padding_ == CacheStorage::kSizeUnknown) | 68 return storage_size_; |
| 106 CalculateStoragePadding(); | |
| 107 if (storage_size_ == CacheStorage::kSizeUnknown || | |
| 108 storage_padding_ == CacheStorage::kSizeUnknown) { | |
| 109 return CacheStorage::kSizeUnknown; | |
| 110 } | |
| 111 return storage_size_ + storage_padding_; | |
| 112 } | 69 } |
| 113 | 70 |
| 114 void CacheStorageIndex::UpdateStorageSize() { | 71 void CacheStorageIndex::UpdateStorageSize() { |
| 115 int64_t storage_size = 0; | 72 int64_t storage_size = 0; |
| 116 storage_size_ = CacheStorage::kSizeUnknown; | 73 storage_size_ = CacheStorage::kSizeUnknown; |
| 117 for (const CacheMetadata& info : ordered_cache_metadata_) { | 74 for (const CacheMetadata& info : ordered_cache_metadata_) { |
| 118 if (info.size == CacheStorage::kSizeUnknown) | 75 if (info.size == CacheStorage::kSizeUnknown) |
| 119 return; | 76 return; |
| 120 storage_size += info.size; | 77 storage_size += info.size; |
| 121 } | 78 } |
| 122 storage_size_ = storage_size; | 79 storage_size_ = storage_size; |
| 123 } | 80 } |
| 124 | 81 |
| 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 | |
| 136 void CacheStorageIndex::DoomCache(const std::string& cache_name) { | 82 void CacheStorageIndex::DoomCache(const std::string& cache_name) { |
| 137 DCHECK(!has_doomed_cache_); | 83 DCHECK(!has_doomed_cache_); |
| 138 auto map_it = cache_metadata_map_.find(cache_name); | 84 auto map_it = cache_metadata_map_.find(cache_name); |
| 139 DCHECK(map_it != cache_metadata_map_.end()); | 85 DCHECK(map_it != cache_metadata_map_.end()); |
| 140 doomed_cache_metadata_ = std::move(*(map_it->second)); | 86 doomed_cache_metadata_ = std::move(*(map_it->second)); |
| 141 after_doomed_cache_metadata_ = ordered_cache_metadata_.erase(map_it->second); | 87 after_doomed_cache_metadata_ = ordered_cache_metadata_.erase(map_it->second); |
| 142 cache_metadata_map_.erase(map_it); | 88 cache_metadata_map_.erase(map_it); |
| 143 storage_size_ = CacheStorage::kSizeUnknown; | 89 storage_size_ = CacheStorage::kSizeUnknown; |
| 144 storage_padding_ = CacheStorage::kSizeUnknown; | |
| 145 has_doomed_cache_ = true; | 90 has_doomed_cache_ = true; |
| 146 } | 91 } |
| 147 | 92 |
| 148 void CacheStorageIndex::FinalizeDoomedCache() { | 93 void CacheStorageIndex::FinalizeDoomedCache() { |
| 149 DCHECK(has_doomed_cache_); | 94 DCHECK(has_doomed_cache_); |
| 150 ClearDoomedCache(); | 95 ClearDoomedCache(); |
| 151 } | 96 } |
| 152 | 97 |
| 153 void CacheStorageIndex::RestoreDoomedCache() { | 98 void CacheStorageIndex::RestoreDoomedCache() { |
| 154 DCHECK(has_doomed_cache_); | 99 DCHECK(has_doomed_cache_); |
| 155 const auto cache_name = doomed_cache_metadata_.name; | 100 const auto cache_name = doomed_cache_metadata_.name; |
| 156 cache_metadata_map_[cache_name] = ordered_cache_metadata_.insert( | 101 cache_metadata_map_[cache_name] = ordered_cache_metadata_.insert( |
| 157 after_doomed_cache_metadata_, std::move(doomed_cache_metadata_)); | 102 after_doomed_cache_metadata_, std::move(doomed_cache_metadata_)); |
| 158 after_doomed_cache_metadata_ = ordered_cache_metadata_.end(); | 103 after_doomed_cache_metadata_ = ordered_cache_metadata_.end(); |
| 159 storage_size_ = CacheStorage::kSizeUnknown; | 104 storage_size_ = CacheStorage::kSizeUnknown; |
| 160 storage_padding_ = CacheStorage::kSizeUnknown; | |
| 161 ClearDoomedCache(); | 105 ClearDoomedCache(); |
| 162 } | 106 } |
| 163 | 107 |
| 164 void CacheStorageIndex::ClearDoomedCache() { | 108 void CacheStorageIndex::ClearDoomedCache() { |
| 165 doomed_cache_metadata_.name.clear(); | 109 doomed_cache_metadata_.name.clear(); |
| 166 after_doomed_cache_metadata_ = ordered_cache_metadata_.end(); | 110 after_doomed_cache_metadata_ = ordered_cache_metadata_.end(); |
| 167 has_doomed_cache_ = false; | 111 has_doomed_cache_ = false; |
| 168 } | 112 } |
| 169 | 113 |
| 170 } // namespace content | 114 } // namespace content |
| OLD | NEW |