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(const CacheStorageIndex& other) { |
| 12 for (const auto& cache_metadata : other.ordered_cache_metadata_) |
| 13 Insert(cache_metadata); |
| 14 } |
| 15 |
| 16 CacheStorageIndex::CacheStorageIndex() = default; |
| 17 |
| 18 CacheStorageIndex::~CacheStorageIndex() = default; |
| 19 |
| 20 CacheStorageIndex& CacheStorageIndex::operator=(CacheStorageIndex&& rhs) { |
| 21 ordered_cache_metadata_ = std::move(rhs.ordered_cache_metadata_); |
| 22 cache_metadata_map_ = std::move(rhs.cache_metadata_map_); |
| 23 storage_size_ = rhs.storage_size_; |
| 24 rhs.storage_size_ = CacheStorage::kSizeUnknown; |
| 25 return *this; |
| 26 } |
| 27 |
| 28 CacheStorageIndex& CacheStorageIndex::operator=(const CacheStorageIndex& rhs) { |
| 29 ordered_cache_metadata_.clear(); |
| 30 cache_metadata_map_.clear(); |
| 31 for (const auto& metadata : rhs.ordered_cache_metadata_) |
| 32 Insert(metadata); |
| 33 storage_size_ = rhs.storage_size_; |
| 34 return *this; |
| 35 } |
| 36 |
| 37 void CacheStorageIndex::Insert(const CacheMetadata& cache_metadata) { |
| 38 DCHECK(cache_metadata_map_.find(cache_metadata.name) == |
| 39 cache_metadata_map_.end()); |
| 40 ordered_cache_metadata_.push_back(cache_metadata); |
| 41 cache_metadata_map_[cache_metadata.name] = --ordered_cache_metadata_.end(); |
| 42 storage_size_ = CacheStorage::kSizeUnknown; |
| 43 } |
| 44 |
| 45 void CacheStorageIndex::Delete(const std::string& cache_name) { |
| 46 auto it = cache_metadata_map_.find(cache_name); |
| 47 DCHECK(it != cache_metadata_map_.end()); |
| 48 ordered_cache_metadata_.erase(it->second); |
| 49 cache_metadata_map_.erase(it); |
| 50 storage_size_ = CacheStorage::kSizeUnknown; |
| 51 } |
| 52 |
| 53 // Sets the cache size. Returns true if the new size is different than the |
| 54 // current size else false. |
| 55 bool CacheStorageIndex::SetCacheSize(const std::string& cache_name, |
| 56 int64_t size) { |
| 57 auto it = cache_metadata_map_.find(cache_name); |
| 58 if (it == cache_metadata_map_.end()) { |
| 59 // Deleted (but still referenced) caches can still run. |
| 60 return false; |
| 61 } |
| 62 if (it->second->size == size) |
| 63 return false; |
| 64 it->second->size = size; |
| 65 storage_size_ = CacheStorage::kSizeUnknown; |
| 66 return true; |
| 67 } |
| 68 |
| 69 // The cache was modified, increasing/decreasing cache size by |size_delta|. |
| 70 void CacheStorageIndex::SetCacheSizeModified(const std::string& cache_name, |
| 71 int64_t size_delta) { |
| 72 DCHECK_NE(size_delta, 0); |
| 73 |
| 74 auto it = cache_metadata_map_.find(cache_name); |
| 75 if (it == cache_metadata_map_.end()) { |
| 76 // Deleted (but still referenced) caches can still run. |
| 77 return; |
| 78 } |
| 79 DCHECK_NE(it->second->size, CacheStorage::kSizeUnknown); |
| 80 it->second->size += size_delta; |
| 81 DCHECK_GE(it->second->size, 0U); |
| 82 if (storage_size_ == CacheStorage::kSizeUnknown) |
| 83 return; |
| 84 DCHECK_NE(storage_size_, CacheStorage::kSizeUnknown); |
| 85 storage_size_ += size_delta; |
| 86 DCHECK_GE(storage_size_, 0U); |
| 87 } |
| 88 |
| 89 int64_t CacheStorageIndex::GetCacheSize(const std::string& cache_name) const { |
| 90 const auto& it = cache_metadata_map_.find(cache_name); |
| 91 if (it == cache_metadata_map_.end()) |
| 92 return CacheStorage::kSizeUnknown; |
| 93 return it->second->size; |
| 94 } |
| 95 |
| 96 int64_t CacheStorageIndex::GetStorageSize() { |
| 97 if (storage_size_ == CacheStorage::kSizeUnknown) |
| 98 UpdateStorageSize(); |
| 99 return storage_size_; |
| 100 } |
| 101 |
| 102 void CacheStorageIndex::UpdateStorageSize() { |
| 103 int64_t storage_size = 0; |
| 104 storage_size_ = CacheStorage::kSizeUnknown; |
| 105 for (const CacheMetadata& info : ordered_cache_metadata_) { |
| 106 if (info.size == CacheStorage::kSizeUnknown) |
| 107 return; |
| 108 storage_size += info.size; |
| 109 } |
| 110 storage_size_ = storage_size; |
| 111 } |
| 112 |
| 113 } // namespace content |
OLD | NEW |