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