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/macros.h" | 12 #include "base/macros.h" |
13 #include "content/browser/cache_storage/cache_storage.h" | 13 #include "content/browser/cache_storage/cache_storage.h" |
14 | 14 |
15 namespace content { | 15 namespace content { |
16 | 16 |
17 class CacheMetadata; | 17 class CacheMetadata; |
18 | 18 |
19 // CacheStorageIndex maintains an ordered list of metadata (CacheMetadata) | 19 // CacheStorageIndex maintains an ordered list of metadata (CacheMetadata) |
20 // for each cache owned by a CacheStorage object. This class is not thread safe, | 20 // for each cache owned by a CacheStorage object. This class is not thread safe, |
21 // and is owned by the CacheStorage. | 21 // and is owned by the CacheStorage. |
22 class CONTENT_EXPORT CacheStorageIndex { | 22 class CONTENT_EXPORT CacheStorageIndex { |
23 public: | 23 public: |
24 struct CacheMetadata { | 24 struct CacheMetadata { |
25 CacheMetadata(const std::string& name, int64_t size) | 25 CacheMetadata(const std::string& name, int64_t size) |
26 : name(name), size(size) {} | 26 : name(name), size(size) {} |
27 std::string name; | 27 std::string name; |
28 // The size (in bytes) of the cache. Set to CacheStorage::kSizeUnknown if | 28 // The size (in bytes) of the cache. Set to kCacheStorageSizeUnknown if |
29 // size not known. | 29 // size not known. |
30 int64_t size; | 30 int64_t size; |
31 }; | 31 }; |
32 | 32 |
33 CacheStorageIndex(); | 33 CacheStorageIndex(); |
34 ~CacheStorageIndex(); | 34 ~CacheStorageIndex(); |
35 | 35 |
36 CacheStorageIndex& operator=(CacheStorageIndex&& rhs); | 36 CacheStorageIndex& operator=(CacheStorageIndex&& rhs); |
37 | 37 |
38 void Insert(const CacheMetadata& cache_metadata); | 38 void Insert(const CacheMetadata& cache_metadata); |
39 void Delete(const std::string& cache_name); | 39 void Delete(const std::string& cache_name); |
40 | 40 |
41 // Sets the cache size. Returns true if the new size is different than the | 41 // Sets the cache size. Returns true if the new size is different than the |
42 // current size else false. | 42 // current size else false. |
43 bool SetCacheSize(const std::string& cache_name, int64_t size); | 43 bool SetCacheSize(const std::string& cache_name, int64_t size); |
44 | 44 |
45 // Return the size (in bytes) of the specified cache. Will return | 45 // Return the size (in bytes) of the specified cache. Will return |
46 // CacheStorage::kSizeUnknown if the specified cache does not exist. | 46 // kCacheStorageSizeUnknown if the specified cache does not exist. |
47 int64_t GetCacheSize(const std::string& cache_name) const; | 47 int64_t GetCacheSize(const std::string& cache_name) const; |
48 | 48 |
49 const std::list<CacheMetadata>& ordered_cache_metadata() const { | 49 const std::list<CacheMetadata>& ordered_cache_metadata() const { |
50 return ordered_cache_metadata_; | 50 return ordered_cache_metadata_; |
51 } | 51 } |
52 | 52 |
53 size_t num_entries() const { return ordered_cache_metadata_.size(); } | 53 size_t num_entries() const { return ordered_cache_metadata_.size(); } |
54 | 54 |
55 // Will calculate (if necessary), and return the total sum of all cache sizes. | 55 // Will calculate (if necessary), and return the total sum of all cache sizes. |
56 int64_t GetStorageSize(); | 56 int64_t GetStorageSize(); |
(...skipping 14 matching lines...) Expand all Loading... |
71 void UpdateStorageSize(); | 71 void UpdateStorageSize(); |
72 void ClearDoomedCache(); | 72 void ClearDoomedCache(); |
73 | 73 |
74 // Use a list to keep saved iterators valid during insert/erase. | 74 // Use a list to keep saved iterators valid during insert/erase. |
75 // Note: ordered by cache creation. | 75 // Note: ordered by cache creation. |
76 std::list<CacheMetadata> ordered_cache_metadata_; | 76 std::list<CacheMetadata> ordered_cache_metadata_; |
77 std::unordered_map<std::string, std::list<CacheMetadata>::iterator> | 77 std::unordered_map<std::string, std::list<CacheMetadata>::iterator> |
78 cache_metadata_map_; | 78 cache_metadata_map_; |
79 | 79 |
80 // The total size of all caches in this store. | 80 // The total size of all caches in this store. |
81 int64_t storage_size_ = CacheStorage::kSizeUnknown; | 81 int64_t storage_size_ = kCacheStorageSizeUnknown; |
82 | 82 |
83 // The doomed cache metadata saved when calling DoomCache. | 83 // The doomed cache metadata saved when calling DoomCache. |
84 CacheMetadata doomed_cache_metadata_; | 84 CacheMetadata doomed_cache_metadata_; |
85 std::list<CacheMetadata>::iterator after_doomed_cache_metadata_; | 85 std::list<CacheMetadata>::iterator after_doomed_cache_metadata_; |
86 bool has_doomed_cache_ = false; | 86 bool has_doomed_cache_ = false; |
87 | 87 |
88 DISALLOW_COPY_AND_ASSIGN(CacheStorageIndex); | 88 DISALLOW_COPY_AND_ASSIGN(CacheStorageIndex); |
89 }; | 89 }; |
90 | 90 |
91 } // namespace content | 91 } // namespace content |
92 | 92 |
93 #endif // CONTENT_BROWSER_CACHE_STORAGE_CACHE_STORAGE_INDEX_H_ | 93 #endif // CONTENT_BROWSER_CACHE_STORAGE_CACHE_STORAGE_INDEX_H_ |
OLD | NEW |