Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(376)

Side by Side Diff: content/browser/cache_storage/cache_storage_index.h

Issue 2901083002: [CacheStorage] Pad and bin opaque resource sizes. (Closed)
Patch Set: Storing padding key in cache. Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 HMAC key used to calculate padding for some cache entries.
39 std::string padding_key;
29 }; 40 };
30 41
31 CacheStorageIndex(); 42 CacheStorageIndex();
32 ~CacheStorageIndex(); 43 ~CacheStorageIndex();
33 44
34 CacheStorageIndex& operator=(CacheStorageIndex&& rhs); 45 CacheStorageIndex& operator=(CacheStorageIndex&& rhs);
35 46
36 void Insert(const CacheMetadata& cache_metadata); 47 void Insert(const CacheMetadata& cache_metadata);
37 void Delete(const std::string& cache_name); 48 void Delete(const std::string& cache_name);
38 49
39 // Sets the cache size. Returns true if the new size is different than the 50 // Sets the cache size. Returns true if the new size is different than the
40 // current size else false. 51 // current size else false.
41 bool SetCacheSize(const std::string& cache_name, int64_t size); 52 bool SetCacheSize(const std::string& cache_name, int64_t size);
42 53
43 // Return the size (in bytes) of the specified cache. Will return 54 // Find the cache metadata for a given cache name. If not found nullptr is
44 // CacheStorage::kSizeUnknown if the specified cache does not exist. 55 // returned.
45 int64_t GetCacheSize(const std::string& cache_name) const; 56 const CacheMetadata* FindMetadata(const std::string& cache_name) const;
57
58 // Sets the cache padding. Returns true if the new padding is different than
59 // the current padding else false.
60 bool SetCachePadding(const std::string& cache_name, int64_t padding);
46 61
47 const std::list<CacheMetadata>& ordered_cache_metadata() const { 62 const std::list<CacheMetadata>& ordered_cache_metadata() const {
48 return ordered_cache_metadata_; 63 return ordered_cache_metadata_;
49 } 64 }
50 65
51 size_t num_entries() const { return ordered_cache_metadata_.size(); } 66 size_t num_entries() const { return ordered_cache_metadata_.size(); }
52 67
53 // Will calculate (if necessary), and return the total sum of all cache sizes. 68 // Will calculate (if necessary), and return the total sum of all cache sizes.
54 int64_t GetStorageSize(); 69 int64_t GetPaddedStorageSize();
55 70
56 // Mark the cache as doomed. This removes the cache metadata from the index. 71 // 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 72 // 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 73 // 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 74 // cache must either be finalized (by calling FinalizeDoomedCache) or restored
60 // (by calling RestoreDoomedCache). 75 // (by calling RestoreDoomedCache).
61 // 76 //
62 // RestoreDoomedCache restores the metadata to the index at the original 77 // RestoreDoomedCache restores the metadata to the index at the original
63 // position prior to calling DoomCache. 78 // position prior to calling DoomCache.
64 void DoomCache(const std::string& cache_name); 79 void DoomCache(const std::string& cache_name);
65 void FinalizeDoomedCache(); 80 void FinalizeDoomedCache();
66 void RestoreDoomedCache(); 81 void RestoreDoomedCache();
67 82
68 private: 83 private:
84 FRIEND_TEST_ALL_PREFIXES(CacheStorageIndexTest, TestSetCacheSize);
85 FRIEND_TEST_ALL_PREFIXES(CacheStorageIndexTest, TestSetCachePadding);
86
69 void UpdateStorageSize(); 87 void UpdateStorageSize();
88 void UpdateStoragePadding();
70 void ClearDoomedCache(); 89 void ClearDoomedCache();
71 90
91 // Return the size (in bytes) of the specified cache. Will return
92 // CacheStorage::kSizeUnknown if the specified cache does not exist.
93 int64_t GetCacheSize(const std::string& cache_name) const;
94
95 // Return the padding (in bytes) of the specified cache. Will return
96 // CacheStorage::kSizeUnknown if the specified cache does not exist.
97 int64_t GetCachePadding(const std::string& cache_name) const;
98
72 // Use a list to keep saved iterators valid during insert/erase. 99 // Use a list to keep saved iterators valid during insert/erase.
73 // Note: ordered by cache creation. 100 // Note: ordered by cache creation.
74 std::list<CacheMetadata> ordered_cache_metadata_; 101 std::list<CacheMetadata> ordered_cache_metadata_;
75 std::unordered_map<std::string, std::list<CacheMetadata>::iterator> 102 std::unordered_map<std::string, std::list<CacheMetadata>::iterator>
76 cache_metadata_map_; 103 cache_metadata_map_;
77 104
78 // The total size of all caches in this store. 105 // The total unpadded size of all caches in this store.
79 int64_t storage_size_ = CacheStorage::kSizeUnknown; 106 int64_t storage_size_ = CacheStorage::kSizeUnknown;
80 107
108 // The total padding of all caches in this store.
109 int64_t storage_padding_ = CacheStorage::kSizeUnknown;
110
81 // The doomed cache metadata saved when calling DoomCache. 111 // The doomed cache metadata saved when calling DoomCache.
82 CacheMetadata doomed_cache_metadata_; 112 CacheMetadata doomed_cache_metadata_;
83 std::list<CacheMetadata>::iterator after_doomed_cache_metadata_; 113 std::list<CacheMetadata>::iterator after_doomed_cache_metadata_;
84 bool has_doomed_cache_ = false; 114 bool has_doomed_cache_ = false;
85 115
86 DISALLOW_COPY_AND_ASSIGN(CacheStorageIndex); 116 DISALLOW_COPY_AND_ASSIGN(CacheStorageIndex);
87 }; 117 };
88 118
89 } // namespace content 119 } // namespace content
90 120
91 #endif // CONTENT_BROWSER_CACHE_STORAGE_CACHE_STORAGE_INDEX_H_ 121 #endif // CONTENT_BROWSER_CACHE_STORAGE_CACHE_STORAGE_INDEX_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698