OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "content/browser/cache_storage/cache_storage_index.h" |
| 6 |
| 7 #include <utility> |
| 8 |
| 9 namespace content { |
| 10 |
| 11 CacheStorageIndex::CacheStorageIndex() |
| 12 : doomed_cache_metadata_("", CacheStorage::kSizeUnknown) { |
| 13 ClearDoomedCache(); |
| 14 } |
| 15 |
| 16 CacheStorageIndex::~CacheStorageIndex() = default; |
| 17 |
| 18 CacheStorageIndex& CacheStorageIndex::operator=(CacheStorageIndex&& rhs) { |
| 19 DCHECK(!has_doomed_cache_); |
| 20 ordered_cache_metadata_ = std::move(rhs.ordered_cache_metadata_); |
| 21 cache_metadata_map_ = std::move(rhs.cache_metadata_map_); |
| 22 storage_size_ = rhs.storage_size_; |
| 23 rhs.storage_size_ = CacheStorage::kSizeUnknown; |
| 24 return *this; |
| 25 } |
| 26 |
| 27 void CacheStorageIndex::Insert(const CacheMetadata& cache_metadata) { |
| 28 DCHECK(!has_doomed_cache_); |
| 29 DCHECK(cache_metadata_map_.find(cache_metadata.name) == |
| 30 cache_metadata_map_.end()); |
| 31 ordered_cache_metadata_.push_back(cache_metadata); |
| 32 cache_metadata_map_[cache_metadata.name] = --ordered_cache_metadata_.end(); |
| 33 storage_size_ = CacheStorage::kSizeUnknown; |
| 34 } |
| 35 |
| 36 void CacheStorageIndex::Delete(const std::string& cache_name) { |
| 37 DCHECK(!has_doomed_cache_); |
| 38 auto it = cache_metadata_map_.find(cache_name); |
| 39 DCHECK(it != cache_metadata_map_.end()); |
| 40 ordered_cache_metadata_.erase(it->second); |
| 41 cache_metadata_map_.erase(it); |
| 42 storage_size_ = CacheStorage::kSizeUnknown; |
| 43 } |
| 44 |
| 45 bool CacheStorageIndex::SetCacheSize(const std::string& cache_name, |
| 46 int64_t size) { |
| 47 if (has_doomed_cache_) |
| 48 DCHECK_NE(cache_name, doomed_cache_metadata_.name); |
| 49 auto it = cache_metadata_map_.find(cache_name); |
| 50 DCHECK(it != cache_metadata_map_.end()); |
| 51 if (it->second->size == size) |
| 52 return false; |
| 53 it->second->size = size; |
| 54 storage_size_ = CacheStorage::kSizeUnknown; |
| 55 return true; |
| 56 } |
| 57 |
| 58 int64_t CacheStorageIndex::GetCacheSize(const std::string& cache_name) const { |
| 59 const auto& it = cache_metadata_map_.find(cache_name); |
| 60 if (it == cache_metadata_map_.end()) |
| 61 return CacheStorage::kSizeUnknown; |
| 62 return it->second->size; |
| 63 } |
| 64 |
| 65 int64_t CacheStorageIndex::GetStorageSize() { |
| 66 if (storage_size_ == CacheStorage::kSizeUnknown) |
| 67 UpdateStorageSize(); |
| 68 return storage_size_; |
| 69 } |
| 70 |
| 71 void CacheStorageIndex::UpdateStorageSize() { |
| 72 int64_t storage_size = 0; |
| 73 storage_size_ = CacheStorage::kSizeUnknown; |
| 74 for (const CacheMetadata& info : ordered_cache_metadata_) { |
| 75 if (info.size == CacheStorage::kSizeUnknown) |
| 76 return; |
| 77 storage_size += info.size; |
| 78 } |
| 79 storage_size_ = storage_size; |
| 80 } |
| 81 |
| 82 void CacheStorageIndex::DoomCache(const std::string& cache_name) { |
| 83 DCHECK(!has_doomed_cache_); |
| 84 auto map_it = cache_metadata_map_.find(cache_name); |
| 85 DCHECK(map_it != cache_metadata_map_.end()); |
| 86 doomed_cache_metadata_ = std::move(*(map_it->second)); |
| 87 after_doomed_cache_metadata_ = ordered_cache_metadata_.erase(map_it->second); |
| 88 cache_metadata_map_.erase(map_it); |
| 89 storage_size_ = CacheStorage::kSizeUnknown; |
| 90 has_doomed_cache_ = true; |
| 91 } |
| 92 |
| 93 void CacheStorageIndex::FinalizeDoomedCache() { |
| 94 DCHECK(has_doomed_cache_); |
| 95 ClearDoomedCache(); |
| 96 } |
| 97 |
| 98 void CacheStorageIndex::ResetDoomedCache() { |
| 99 DCHECK(has_doomed_cache_); |
| 100 const auto cache_name = doomed_cache_metadata_.name; |
| 101 cache_metadata_map_[cache_name] = ordered_cache_metadata_.insert( |
| 102 after_doomed_cache_metadata_, std::move(doomed_cache_metadata_)); |
| 103 after_doomed_cache_metadata_ = ordered_cache_metadata_.end(); |
| 104 storage_size_ = CacheStorage::kSizeUnknown; |
| 105 ClearDoomedCache(); |
| 106 } |
| 107 |
| 108 void CacheStorageIndex::ClearDoomedCache() { |
| 109 doomed_cache_metadata_.name.clear(); |
| 110 after_doomed_cache_metadata_ = ordered_cache_metadata_.end(); |
| 111 has_doomed_cache_ = false; |
| 112 } |
| 113 |
| 114 } // namespace content |
OLD | NEW |