| 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 #ifndef CONTENT_BROWSER_CACHE_STORAGE_CACHE_STORAGE_INDEX_H_ | 5 #ifndef CONTENT_BROWSER_CACHE_STORAGE_CACHE_STORAGE_INDEX_H_ |
| 6 #define CONTENT_BROWSER_CACHE_STORAGE_CACHE_STORAGE_INDEX_H_ | 6 #define CONTENT_BROWSER_CACHE_STORAGE_CACHE_STORAGE_INDEX_H_ |
| 7 | 7 |
| 8 #include <list> | 8 #include <list> |
| 9 #include <string> | 9 #include <string> |
| 10 #include <unordered_map> | 10 #include <unordered_map> |
| 11 | 11 |
| 12 #include "base/gtest_prod_util.h" | |
| 13 #include "base/macros.h" | 12 #include "base/macros.h" |
| 14 #include "content/browser/cache_storage/cache_storage.h" | 13 #include "content/browser/cache_storage/cache_storage.h" |
| 15 | 14 |
| 16 namespace content { | 15 namespace content { |
| 17 | 16 |
| 18 // CacheStorageIndex maintains an ordered list of metadata (CacheMetadata) | 17 // CacheStorageIndex maintains an ordered list of metadata (CacheMetadata) |
| 19 // for each cache owned by a CacheStorage object. This class is not thread safe, | 18 // for each cache owned by a CacheStorage object. This class is not thread safe, |
| 20 // and is owned by the CacheStorage. | 19 // and is owned by the CacheStorage. |
| 21 class CONTENT_EXPORT CacheStorageIndex { | 20 class CONTENT_EXPORT CacheStorageIndex { |
| 22 public: | 21 public: |
| 23 struct CacheMetadata { | 22 struct CacheMetadata { |
| 24 CacheMetadata(const std::string& name, | 23 CacheMetadata(const std::string& name, int64_t size) |
| 25 int64_t size, | 24 : name(name), size(size) {} |
| 26 int64_t padding, | |
| 27 const std::string& padding_key) | |
| 28 : name(name), size(size), padding(padding), padding_key(padding_key) {} | |
| 29 std::string name; | 25 std::string name; |
| 30 // The size (in bytes) of the cache. Set to CacheStorage::kSizeUnknown if | 26 // The size (in bytes) of the cache. Set to CacheStorage::kSizeUnknown if |
| 31 // size not known. | 27 // size not known. |
| 32 int64_t size; | 28 int64_t size; |
| 33 | |
| 34 // The padding (in bytes) of the cache. Set to CacheStorage::kSizeUnknown | |
| 35 // if padding not known. | |
| 36 int64_t padding; | |
| 37 | |
| 38 // The raw key used to calculate padding for some cache entries. | |
| 39 std::string padding_key; | |
| 40 | |
| 41 // The algorithm version used to calculate this padding. | |
| 42 int32_t padding_version; | |
| 43 }; | 29 }; |
| 44 | 30 |
| 45 CacheStorageIndex(); | 31 CacheStorageIndex(); |
| 46 ~CacheStorageIndex(); | 32 ~CacheStorageIndex(); |
| 47 | 33 |
| 48 CacheStorageIndex& operator=(CacheStorageIndex&& rhs); | 34 CacheStorageIndex& operator=(CacheStorageIndex&& rhs); |
| 49 | 35 |
| 50 void Insert(const CacheMetadata& cache_metadata); | 36 void Insert(const CacheMetadata& cache_metadata); |
| 51 void Delete(const std::string& cache_name); | 37 void Delete(const std::string& cache_name); |
| 52 | 38 |
| 53 // Sets the actual (unpadded) cache size. Returns true if the new size is | 39 // Sets the cache size. Returns true if the new size is different than the |
| 54 // different than the current size else false. | 40 // current size else false. |
| 55 bool SetCacheSize(const std::string& cache_name, int64_t size); | 41 bool SetCacheSize(const std::string& cache_name, int64_t size); |
| 56 | 42 |
| 57 // Get the cache metadata for a given cache name. If not found nullptr is | 43 // Return the size (in bytes) of the specified cache. Will return |
| 58 // returned. | 44 // CacheStorage::kSizeUnknown if the specified cache does not exist. |
| 59 const CacheMetadata* GetMetadata(const std::string& cache_name) const; | 45 int64_t GetCacheSize(const std::string& cache_name) const; |
| 60 | |
| 61 // Sets the cache padding. Returns true if the new padding is different than | |
| 62 // the current padding else false. | |
| 63 bool SetCachePadding(const std::string& cache_name, int64_t padding); | |
| 64 | 46 |
| 65 const std::list<CacheMetadata>& ordered_cache_metadata() const { | 47 const std::list<CacheMetadata>& ordered_cache_metadata() const { |
| 66 return ordered_cache_metadata_; | 48 return ordered_cache_metadata_; |
| 67 } | 49 } |
| 68 | 50 |
| 69 size_t num_entries() const { return ordered_cache_metadata_.size(); } | 51 size_t num_entries() const { return ordered_cache_metadata_.size(); } |
| 70 | 52 |
| 71 // Will calculate (if necessary), and return the total sum of all cache sizes. | 53 // Will calculate (if necessary), and return the total sum of all cache sizes. |
| 72 int64_t GetPaddedStorageSize(); | 54 int64_t GetStorageSize(); |
| 73 | 55 |
| 74 // Mark the cache as doomed. This removes the cache metadata from the index. | 56 // Mark the cache as doomed. This removes the cache metadata from the index. |
| 75 // All const methods (eg: num_entries) will behave as if the doomed cache is | 57 // All const methods (eg: num_entries) will behave as if the doomed cache is |
| 76 // not present in the index. Prior to calling any non-const method the doomed | 58 // not present in the index. Prior to calling any non-const method the doomed |
| 77 // cache must either be finalized (by calling FinalizeDoomedCache) or restored | 59 // cache must either be finalized (by calling FinalizeDoomedCache) or restored |
| 78 // (by calling RestoreDoomedCache). | 60 // (by calling RestoreDoomedCache). |
| 79 // | 61 // |
| 80 // RestoreDoomedCache restores the metadata to the index at the original | 62 // RestoreDoomedCache restores the metadata to the index at the original |
| 81 // position prior to calling DoomCache. | 63 // position prior to calling DoomCache. |
| 82 void DoomCache(const std::string& cache_name); | 64 void DoomCache(const std::string& cache_name); |
| 83 void FinalizeDoomedCache(); | 65 void FinalizeDoomedCache(); |
| 84 void RestoreDoomedCache(); | 66 void RestoreDoomedCache(); |
| 85 | 67 |
| 86 private: | 68 private: |
| 87 FRIEND_TEST_ALL_PREFIXES(CacheStorageIndexTest, TestSetCacheSize); | |
| 88 FRIEND_TEST_ALL_PREFIXES(CacheStorageIndexTest, TestSetCachePadding); | |
| 89 | |
| 90 void UpdateStorageSize(); | 69 void UpdateStorageSize(); |
| 91 void CalculateStoragePadding(); | |
| 92 void ClearDoomedCache(); | 70 void ClearDoomedCache(); |
| 93 | 71 |
| 94 // Return the size (in bytes) of the specified cache. Will return | |
| 95 // CacheStorage::kSizeUnknown if the specified cache does not exist. | |
| 96 int64_t GetCacheSizeForTesting(const std::string& cache_name) const; | |
| 97 | |
| 98 // Return the padding (in bytes) of the specified cache. Will return | |
| 99 // CacheStorage::kSizeUnknown if the specified cache does not exist. | |
| 100 int64_t GetCachePaddingForTesting(const std::string& cache_name) const; | |
| 101 | |
| 102 // Use a list to keep saved iterators valid during insert/erase. | 72 // Use a list to keep saved iterators valid during insert/erase. |
| 103 // Note: ordered by cache creation. | 73 // Note: ordered by cache creation. |
| 104 std::list<CacheMetadata> ordered_cache_metadata_; | 74 std::list<CacheMetadata> ordered_cache_metadata_; |
| 105 std::unordered_map<std::string, std::list<CacheMetadata>::iterator> | 75 std::unordered_map<std::string, std::list<CacheMetadata>::iterator> |
| 106 cache_metadata_map_; | 76 cache_metadata_map_; |
| 107 | 77 |
| 108 // The total unpadded size of all caches in this store. | 78 // The total size of all caches in this store. |
| 109 int64_t storage_size_ = CacheStorage::kSizeUnknown; | 79 int64_t storage_size_ = CacheStorage::kSizeUnknown; |
| 110 | 80 |
| 111 // The total padding of all caches in this store. | |
| 112 int64_t storage_padding_ = CacheStorage::kSizeUnknown; | |
| 113 | |
| 114 // The doomed cache metadata saved when calling DoomCache. | 81 // The doomed cache metadata saved when calling DoomCache. |
| 115 CacheMetadata doomed_cache_metadata_; | 82 CacheMetadata doomed_cache_metadata_; |
| 116 std::list<CacheMetadata>::iterator after_doomed_cache_metadata_; | 83 std::list<CacheMetadata>::iterator after_doomed_cache_metadata_; |
| 117 bool has_doomed_cache_ = false; | 84 bool has_doomed_cache_ = false; |
| 118 | 85 |
| 119 DISALLOW_COPY_AND_ASSIGN(CacheStorageIndex); | 86 DISALLOW_COPY_AND_ASSIGN(CacheStorageIndex); |
| 120 }; | 87 }; |
| 121 | 88 |
| 122 } // namespace content | 89 } // namespace content |
| 123 | 90 |
| 124 #endif // CONTENT_BROWSER_CACHE_STORAGE_CACHE_STORAGE_INDEX_H_ | 91 #endif // CONTENT_BROWSER_CACHE_STORAGE_CACHE_STORAGE_INDEX_H_ |
| OLD | NEW |