OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 NET_DISK_CACHE_SIMPLE_SIMPLE_BACKEND_IMPL_H_ | 5 #ifndef NET_DISK_CACHE_SIMPLE_SIMPLE_BACKEND_IMPL_H_ |
6 #define NET_DISK_CACHE_SIMPLE_SIMPLE_BACKEND_IMPL_H_ | 6 #define NET_DISK_CACHE_SIMPLE_SIMPLE_BACKEND_IMPL_H_ |
7 | 7 |
| 8 #include <stdint.h> |
| 9 |
8 #include <string> | 10 #include <string> |
9 #include <utility> | 11 #include <utility> |
10 #include <vector> | 12 #include <vector> |
11 | 13 |
12 #include "base/callback_forward.h" | 14 #include "base/callback_forward.h" |
13 #include "base/compiler_specific.h" | 15 #include "base/compiler_specific.h" |
14 #include "base/containers/hash_tables.h" | 16 #include "base/containers/hash_tables.h" |
15 #include "base/files/file_path.h" | 17 #include "base/files/file_path.h" |
16 #include "base/memory/ref_counted.h" | 18 #include "base/memory/ref_counted.h" |
17 #include "base/memory/scoped_ptr.h" | 19 #include "base/memory/scoped_ptr.h" |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
70 bool SetMaxSize(int max_bytes); | 72 bool SetMaxSize(int max_bytes); |
71 | 73 |
72 // Returns the maximum file size permitted in this backend. | 74 // Returns the maximum file size permitted in this backend. |
73 int GetMaxFileSize() const; | 75 int GetMaxFileSize() const; |
74 | 76 |
75 // Flush our SequencedWorkerPool. | 77 // Flush our SequencedWorkerPool. |
76 static void FlushWorkerPoolForTesting(); | 78 static void FlushWorkerPoolForTesting(); |
77 | 79 |
78 // The entry for |entry_hash| is being doomed; the backend will not attempt | 80 // The entry for |entry_hash| is being doomed; the backend will not attempt |
79 // run new operations for this |entry_hash| until the Doom is completed. | 81 // run new operations for this |entry_hash| until the Doom is completed. |
80 void OnDoomStart(uint64 entry_hash); | 82 void OnDoomStart(uint64_t entry_hash); |
81 | 83 |
82 // The entry for |entry_hash| has been successfully doomed, we can now allow | 84 // The entry for |entry_hash| has been successfully doomed, we can now allow |
83 // operations on this entry, and we can run any operations enqueued while the | 85 // operations on this entry, and we can run any operations enqueued while the |
84 // doom completed. | 86 // doom completed. |
85 void OnDoomComplete(uint64 entry_hash); | 87 void OnDoomComplete(uint64_t entry_hash); |
86 | 88 |
87 // SimpleIndexDelegate: | 89 // SimpleIndexDelegate: |
88 void DoomEntries(std::vector<uint64>* entry_hashes, | 90 void DoomEntries(std::vector<uint64_t>* entry_hashes, |
89 const CompletionCallback& callback) override; | 91 const CompletionCallback& callback) override; |
90 | 92 |
91 // Backend: | 93 // Backend: |
92 net::CacheType GetCacheType() const override; | 94 net::CacheType GetCacheType() const override; |
93 int32 GetEntryCount() const override; | 95 int32_t GetEntryCount() const override; |
94 int OpenEntry(const std::string& key, | 96 int OpenEntry(const std::string& key, |
95 Entry** entry, | 97 Entry** entry, |
96 const CompletionCallback& callback) override; | 98 const CompletionCallback& callback) override; |
97 int CreateEntry(const std::string& key, | 99 int CreateEntry(const std::string& key, |
98 Entry** entry, | 100 Entry** entry, |
99 const CompletionCallback& callback) override; | 101 const CompletionCallback& callback) override; |
100 int DoomEntry(const std::string& key, | 102 int DoomEntry(const std::string& key, |
101 const CompletionCallback& callback) override; | 103 const CompletionCallback& callback) override; |
102 int DoomAllEntries(const CompletionCallback& callback) override; | 104 int DoomAllEntries(const CompletionCallback& callback) override; |
103 int DoomEntriesBetween(base::Time initial_time, | 105 int DoomEntriesBetween(base::Time initial_time, |
104 base::Time end_time, | 106 base::Time end_time, |
105 const CompletionCallback& callback) override; | 107 const CompletionCallback& callback) override; |
106 int DoomEntriesSince(base::Time initial_time, | 108 int DoomEntriesSince(base::Time initial_time, |
107 const CompletionCallback& callback) override; | 109 const CompletionCallback& callback) override; |
108 int CalculateSizeOfAllEntries(const CompletionCallback& callback) override; | 110 int CalculateSizeOfAllEntries(const CompletionCallback& callback) override; |
109 scoped_ptr<Iterator> CreateIterator() override; | 111 scoped_ptr<Iterator> CreateIterator() override; |
110 void GetStats(base::StringPairs* stats) override; | 112 void GetStats(base::StringPairs* stats) override; |
111 void OnExternalCacheHit(const std::string& key) override; | 113 void OnExternalCacheHit(const std::string& key) override; |
112 | 114 |
113 private: | 115 private: |
114 class SimpleIterator; | 116 class SimpleIterator; |
115 friend class SimpleIterator; | 117 friend class SimpleIterator; |
116 | 118 |
117 typedef base::hash_map<uint64, SimpleEntryImpl*> EntryMap; | 119 typedef base::hash_map<uint64_t, SimpleEntryImpl*> EntryMap; |
118 | 120 |
119 typedef base::Callback<void(base::Time mtime, uint64 max_size, int result)> | 121 typedef base::Callback<void(base::Time mtime, uint64_t max_size, int result)> |
120 InitializeIndexCallback; | 122 InitializeIndexCallback; |
121 | 123 |
122 class ActiveEntryProxy; | 124 class ActiveEntryProxy; |
123 friend class ActiveEntryProxy; | 125 friend class ActiveEntryProxy; |
124 | 126 |
125 // Return value of InitCacheStructureOnDisk(). | 127 // Return value of InitCacheStructureOnDisk(). |
126 struct DiskStatResult { | 128 struct DiskStatResult { |
127 base::Time cache_dir_mtime; | 129 base::Time cache_dir_mtime; |
128 uint64 max_size; | 130 uint64_t max_size; |
129 bool detected_magic_number_mismatch; | 131 bool detected_magic_number_mismatch; |
130 int net_error; | 132 int net_error; |
131 }; | 133 }; |
132 | 134 |
133 void InitializeIndex(const CompletionCallback& callback, | 135 void InitializeIndex(const CompletionCallback& callback, |
134 const DiskStatResult& result); | 136 const DiskStatResult& result); |
135 | 137 |
136 // Dooms all entries previously accessed between |initial_time| and | 138 // Dooms all entries previously accessed between |initial_time| and |
137 // |end_time|. Invoked when the index is ready. | 139 // |end_time|. Invoked when the index is ready. |
138 void IndexReadyForDoom(base::Time initial_time, | 140 void IndexReadyForDoom(base::Time initial_time, |
139 base::Time end_time, | 141 base::Time end_time, |
140 const CompletionCallback& callback, | 142 const CompletionCallback& callback, |
141 int result); | 143 int result); |
142 | 144 |
143 // Calculates the size of the entire cache. Invoked when the index is ready. | 145 // Calculates the size of the entire cache. Invoked when the index is ready. |
144 void IndexReadyForSizeCalculation(const CompletionCallback& callback, | 146 void IndexReadyForSizeCalculation(const CompletionCallback& callback, |
145 int result); | 147 int result); |
146 | 148 |
147 // Try to create the directory if it doesn't exist. This must run on the IO | 149 // Try to create the directory if it doesn't exist. This must run on the IO |
148 // thread. | 150 // thread. |
149 static DiskStatResult InitCacheStructureOnDisk(const base::FilePath& path, | 151 static DiskStatResult InitCacheStructureOnDisk(const base::FilePath& path, |
150 uint64 suggested_max_size); | 152 uint64_t suggested_max_size); |
151 | 153 |
152 // Searches |active_entries_| for the entry corresponding to |key|. If found, | 154 // Searches |active_entries_| for the entry corresponding to |key|. If found, |
153 // returns the found entry. Otherwise, creates a new entry and returns that. | 155 // returns the found entry. Otherwise, creates a new entry and returns that. |
154 scoped_refptr<SimpleEntryImpl> CreateOrFindActiveEntry( | 156 scoped_refptr<SimpleEntryImpl> CreateOrFindActiveEntry( |
155 uint64 entry_hash, | 157 uint64_t entry_hash, |
156 const std::string& key); | 158 const std::string& key); |
157 | 159 |
158 // Given a hash, will try to open the corresponding Entry. If we have an Entry | 160 // Given a hash, will try to open the corresponding Entry. If we have an Entry |
159 // corresponding to |hash| in the map of active entries, opens it. Otherwise, | 161 // corresponding to |hash| in the map of active entries, opens it. Otherwise, |
160 // a new empty Entry will be created, opened and filled with information from | 162 // a new empty Entry will be created, opened and filled with information from |
161 // the disk. | 163 // the disk. |
162 int OpenEntryFromHash(uint64 entry_hash, | 164 int OpenEntryFromHash(uint64_t entry_hash, |
163 Entry** entry, | 165 Entry** entry, |
164 const CompletionCallback& callback); | 166 const CompletionCallback& callback); |
165 | 167 |
166 // Doom the entry corresponding to |entry_hash|, if it's active or currently | 168 // Doom the entry corresponding to |entry_hash|, if it's active or currently |
167 // pending doom. This function does not block if there is an active entry, | 169 // pending doom. This function does not block if there is an active entry, |
168 // which is very important to prevent races in DoomEntries() above. | 170 // which is very important to prevent races in DoomEntries() above. |
169 int DoomEntryFromHash(uint64 entry_hash, const CompletionCallback & callback); | 171 int DoomEntryFromHash(uint64_t entry_hash, |
| 172 const CompletionCallback& callback); |
170 | 173 |
171 // Called when we tried to open an entry with hash alone. When a blank entry | 174 // Called when we tried to open an entry with hash alone. When a blank entry |
172 // has been created and filled in with information from the disk - based on a | 175 // has been created and filled in with information from the disk - based on a |
173 // hash alone - this checks that a duplicate active entry was not created | 176 // hash alone - this checks that a duplicate active entry was not created |
174 // using a key in the meantime. | 177 // using a key in the meantime. |
175 void OnEntryOpenedFromHash(uint64 hash, | 178 void OnEntryOpenedFromHash(uint64_t hash, |
176 Entry** entry, | 179 Entry** entry, |
177 const scoped_refptr<SimpleEntryImpl>& simple_entry, | 180 const scoped_refptr<SimpleEntryImpl>& simple_entry, |
178 const CompletionCallback& callback, | 181 const CompletionCallback& callback, |
179 int error_code); | 182 int error_code); |
180 | 183 |
181 // Called when we tried to open an entry from key. When the entry has been | 184 // Called when we tried to open an entry from key. When the entry has been |
182 // opened, a check for key mismatch is performed. | 185 // opened, a check for key mismatch is performed. |
183 void OnEntryOpenedFromKey(const std::string key, | 186 void OnEntryOpenedFromKey(const std::string key, |
184 Entry** entry, | 187 Entry** entry, |
185 const scoped_refptr<SimpleEntryImpl>& simple_entry, | 188 const scoped_refptr<SimpleEntryImpl>& simple_entry, |
186 const CompletionCallback& callback, | 189 const CompletionCallback& callback, |
187 int error_code); | 190 int error_code); |
188 | 191 |
189 // A callback thunk used by DoomEntries to clear the |entries_pending_doom_| | 192 // A callback thunk used by DoomEntries to clear the |entries_pending_doom_| |
190 // after a mass doom. | 193 // after a mass doom. |
191 void DoomEntriesComplete(scoped_ptr<std::vector<uint64> > entry_hashes, | 194 void DoomEntriesComplete(scoped_ptr<std::vector<uint64_t>> entry_hashes, |
192 const CompletionCallback& callback, | 195 const CompletionCallback& callback, |
193 int result); | 196 int result); |
194 | 197 |
195 const base::FilePath path_; | 198 const base::FilePath path_; |
196 const net::CacheType cache_type_; | 199 const net::CacheType cache_type_; |
197 scoped_ptr<SimpleIndex> index_; | 200 scoped_ptr<SimpleIndex> index_; |
198 const scoped_refptr<base::SingleThreadTaskRunner> cache_thread_; | 201 const scoped_refptr<base::SingleThreadTaskRunner> cache_thread_; |
199 scoped_refptr<base::TaskRunner> worker_pool_; | 202 scoped_refptr<base::TaskRunner> worker_pool_; |
200 | 203 |
201 int orig_max_size_; | 204 int orig_max_size_; |
202 const SimpleEntryImpl::OperationsMode entry_operations_mode_; | 205 const SimpleEntryImpl::OperationsMode entry_operations_mode_; |
203 | 206 |
204 EntryMap active_entries_; | 207 EntryMap active_entries_; |
205 | 208 |
206 // The set of all entries which are currently being doomed. To avoid races, | 209 // The set of all entries which are currently being doomed. To avoid races, |
207 // these entries cannot have Doom/Create/Open operations run until the doom | 210 // these entries cannot have Doom/Create/Open operations run until the doom |
208 // is complete. The base::Closure map target is used to store deferred | 211 // is complete. The base::Closure map target is used to store deferred |
209 // operations to be run at the completion of the Doom. | 212 // operations to be run at the completion of the Doom. |
210 base::hash_map<uint64, std::vector<base::Closure> > entries_pending_doom_; | 213 base::hash_map<uint64_t, std::vector<base::Closure>> entries_pending_doom_; |
211 | 214 |
212 net::NetLog* const net_log_; | 215 net::NetLog* const net_log_; |
213 }; | 216 }; |
214 | 217 |
215 } // namespace disk_cache | 218 } // namespace disk_cache |
216 | 219 |
217 #endif // NET_DISK_CACHE_SIMPLE_SIMPLE_BACKEND_IMPL_H_ | 220 #endif // NET_DISK_CACHE_SIMPLE_SIMPLE_BACKEND_IMPL_H_ |
OLD | NEW |