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 <string> | 8 #include <string> |
9 #include <utility> | 9 #include <utility> |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "base/callback_forward.h" | 12 #include "base/callback_forward.h" |
13 #include "base/compiler_specific.h" | 13 #include "base/compiler_specific.h" |
14 #include "base/containers/hash_tables.h" | 14 #include "base/containers/hash_tables.h" |
15 #include "base/files/file_path.h" | 15 #include "base/files/file_path.h" |
16 #include "base/id_map.h" | |
17 #include "base/memory/ref_counted.h" | 16 #include "base/memory/ref_counted.h" |
18 #include "base/memory/scoped_ptr.h" | 17 #include "base/memory/scoped_ptr.h" |
19 #include "base/memory/weak_ptr.h" | 18 #include "base/memory/weak_ptr.h" |
20 #include "base/task_runner.h" | 19 #include "base/task_runner.h" |
21 #include "base/time/time.h" | 20 #include "base/time/time.h" |
22 #include "net/base/cache_type.h" | 21 #include "net/base/cache_type.h" |
23 #include "net/disk_cache/disk_cache.h" | 22 #include "net/disk_cache/disk_cache.h" |
24 #include "net/disk_cache/simple/simple_entry_impl.h" | 23 #include "net/disk_cache/simple/simple_entry_impl.h" |
25 #include "net/disk_cache/simple/simple_index_delegate.h" | 24 #include "net/disk_cache/simple/simple_index_delegate.h" |
26 | 25 |
27 namespace base { | 26 namespace base { |
28 class SingleThreadTaskRunner; | 27 class SingleThreadTaskRunner; |
29 class TaskRunner; | 28 class TaskRunner; |
30 } | 29 } |
31 | 30 |
32 namespace disk_cache { | 31 namespace disk_cache { |
33 | 32 |
34 // SimpleBackendImpl is a new cache backend that stores entries in individual | 33 // SimpleBackendImpl is a new cache backend that stores entries in individual |
35 // files. | 34 // files. |
36 // See http://www.chromium.org/developers/design-documents/network-stack/disk-ca
che/very-simple-backend | 35 // See http://www.chromium.org/developers/design-documents/network-stack/disk-ca
che/very-simple-backend |
37 // | 36 // |
| 37 // The SimpleBackendImpl provides safe iteration; mutating entries during |
| 38 // iteration cannot cause a crash. It is undefined whether entries created or |
| 39 // destroyed during the iteration will be included in any pre-existing |
| 40 // iterations. |
| 41 // |
38 // The non-static functions below must be called on the IO thread unless | 42 // The non-static functions below must be called on the IO thread unless |
39 // otherwise stated. | 43 // otherwise stated. |
40 | 44 |
41 class SimpleEntryImpl; | 45 class SimpleEntryImpl; |
42 class SimpleIndex; | 46 class SimpleIndex; |
43 | 47 |
44 class NET_EXPORT_PRIVATE SimpleBackendImpl : public Backend, | 48 class NET_EXPORT_PRIVATE SimpleBackendImpl : public Backend, |
45 public SimpleIndexDelegate, | 49 public SimpleIndexDelegate, |
46 public base::SupportsWeakPtr<SimpleBackendImpl> { | 50 public base::SupportsWeakPtr<SimpleBackendImpl> { |
47 public: | 51 public: |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
91 virtual int CreateEntry(const std::string& key, Entry** entry, | 95 virtual int CreateEntry(const std::string& key, Entry** entry, |
92 const CompletionCallback& callback) OVERRIDE; | 96 const CompletionCallback& callback) OVERRIDE; |
93 virtual int DoomEntry(const std::string& key, | 97 virtual int DoomEntry(const std::string& key, |
94 const CompletionCallback& callback) OVERRIDE; | 98 const CompletionCallback& callback) OVERRIDE; |
95 virtual int DoomAllEntries(const CompletionCallback& callback) OVERRIDE; | 99 virtual int DoomAllEntries(const CompletionCallback& callback) OVERRIDE; |
96 virtual int DoomEntriesBetween(base::Time initial_time, | 100 virtual int DoomEntriesBetween(base::Time initial_time, |
97 base::Time end_time, | 101 base::Time end_time, |
98 const CompletionCallback& callback) OVERRIDE; | 102 const CompletionCallback& callback) OVERRIDE; |
99 virtual int DoomEntriesSince(base::Time initial_time, | 103 virtual int DoomEntriesSince(base::Time initial_time, |
100 const CompletionCallback& callback) OVERRIDE; | 104 const CompletionCallback& callback) OVERRIDE; |
101 virtual int OpenNextEntry(void** iter, Entry** next_entry, | 105 virtual scoped_ptr<Iterator> CreateIterator() OVERRIDE; |
102 const CompletionCallback& callback) OVERRIDE; | |
103 virtual void EndEnumeration(void** iter) OVERRIDE; | |
104 virtual void GetStats( | 106 virtual void GetStats( |
105 std::vector<std::pair<std::string, std::string> >* stats) OVERRIDE; | 107 std::vector<std::pair<std::string, std::string> >* stats) OVERRIDE; |
106 virtual void OnExternalCacheHit(const std::string& key) OVERRIDE; | 108 virtual void OnExternalCacheHit(const std::string& key) OVERRIDE; |
107 | 109 |
108 private: | 110 private: |
| 111 class SimpleIterator; |
| 112 friend class SimpleIterator; |
| 113 |
109 typedef base::hash_map<uint64, SimpleEntryImpl*> EntryMap; | 114 typedef base::hash_map<uint64, SimpleEntryImpl*> EntryMap; |
110 | 115 |
111 typedef IDMap<std::vector<uint64>, IDMapOwnPointer> ActiveEnumerationMap; | |
112 | |
113 typedef base::Callback<void(base::Time mtime, uint64 max_size, int result)> | 116 typedef base::Callback<void(base::Time mtime, uint64 max_size, int result)> |
114 InitializeIndexCallback; | 117 InitializeIndexCallback; |
115 | 118 |
116 class ActiveEntryProxy; | 119 class ActiveEntryProxy; |
117 friend class ActiveEntryProxy; | 120 friend class ActiveEntryProxy; |
118 | 121 |
119 // Return value of InitCacheStructureOnDisk(). | 122 // Return value of InitCacheStructureOnDisk(). |
120 struct DiskStatResult { | 123 struct DiskStatResult { |
121 base::Time cache_dir_mtime; | 124 base::Time cache_dir_mtime; |
122 uint64 max_size; | 125 uint64 max_size; |
123 bool detected_magic_number_mismatch; | 126 bool detected_magic_number_mismatch; |
124 int net_error; | 127 int net_error; |
125 }; | 128 }; |
126 | 129 |
127 // Convert an iterator from OpenNextEntry() to the key type for | |
128 // ActiveEnumerationMap. Note it takes a void** argument; this is for safety; | |
129 // if it took a void*, that would be type compatible with a void** permitting | |
130 // easy calls missing the dereference. | |
131 static ActiveEnumerationMap::KeyType IteratorToEnumerationId(void** iter); | |
132 | |
133 // Convert a key from ActiveEnumerationMap back to a void*, suitable for | |
134 // storing in the iterator argument to OpenNextEntry(). | |
135 static void* EnumerationIdToIterator( | |
136 ActiveEnumerationMap::KeyType enumeration_id); | |
137 | |
138 void InitializeIndex(const CompletionCallback& callback, | 130 void InitializeIndex(const CompletionCallback& callback, |
139 const DiskStatResult& result); | 131 const DiskStatResult& result); |
140 | 132 |
141 // Dooms all entries previously accessed between |initial_time| and | 133 // Dooms all entries previously accessed between |initial_time| and |
142 // |end_time|. Invoked when the index is ready. | 134 // |end_time|. Invoked when the index is ready. |
143 void IndexReadyForDoom(base::Time initial_time, | 135 void IndexReadyForDoom(base::Time initial_time, |
144 base::Time end_time, | 136 base::Time end_time, |
145 const CompletionCallback& callback, | 137 const CompletionCallback& callback, |
146 int result); | 138 int result); |
147 | 139 |
(...skipping 14 matching lines...) Expand all Loading... |
162 // the disk. | 154 // the disk. |
163 int OpenEntryFromHash(uint64 entry_hash, | 155 int OpenEntryFromHash(uint64 entry_hash, |
164 Entry** entry, | 156 Entry** entry, |
165 const CompletionCallback& callback); | 157 const CompletionCallback& callback); |
166 | 158 |
167 // Doom the entry corresponding to |entry_hash|, if it's active or currently | 159 // Doom the entry corresponding to |entry_hash|, if it's active or currently |
168 // pending doom. This function does not block if there is an active entry, | 160 // pending doom. This function does not block if there is an active entry, |
169 // which is very important to prevent races in DoomEntries() above. | 161 // which is very important to prevent races in DoomEntries() above. |
170 int DoomEntryFromHash(uint64 entry_hash, const CompletionCallback & callback); | 162 int DoomEntryFromHash(uint64 entry_hash, const CompletionCallback & callback); |
171 | 163 |
172 // Called when the index is initilized to find the next entry in the iterator | |
173 // |iter|. If there are no more hashes in the iterator list, net::ERR_FAILED | |
174 // is returned. Otherwise, calls OpenEntryFromHash. | |
175 void GetNextEntryInIterator(void** iter, | |
176 Entry** next_entry, | |
177 const CompletionCallback& callback, | |
178 int error_code); | |
179 | |
180 // Called when we tried to open an entry with hash alone. When a blank entry | 164 // Called when we tried to open an entry with hash alone. When a blank entry |
181 // has been created and filled in with information from the disk - based on a | 165 // has been created and filled in with information from the disk - based on a |
182 // hash alone - this checks that a duplicate active entry was not created | 166 // hash alone - this checks that a duplicate active entry was not created |
183 // using a key in the meantime. | 167 // using a key in the meantime. |
184 void OnEntryOpenedFromHash(uint64 hash, | 168 void OnEntryOpenedFromHash(uint64 hash, |
185 Entry** entry, | 169 Entry** entry, |
186 const scoped_refptr<SimpleEntryImpl>& simple_entry, | 170 const scoped_refptr<SimpleEntryImpl>& simple_entry, |
187 const CompletionCallback& callback, | 171 const CompletionCallback& callback, |
188 int error_code); | 172 int error_code); |
189 | 173 |
190 // Called when we tried to open an entry from key. When the entry has been | 174 // Called when we tried to open an entry from key. When the entry has been |
191 // opened, a check for key mismatch is performed. | 175 // opened, a check for key mismatch is performed. |
192 void OnEntryOpenedFromKey(const std::string key, | 176 void OnEntryOpenedFromKey(const std::string key, |
193 Entry** entry, | 177 Entry** entry, |
194 const scoped_refptr<SimpleEntryImpl>& simple_entry, | 178 const scoped_refptr<SimpleEntryImpl>& simple_entry, |
195 const CompletionCallback& callback, | 179 const CompletionCallback& callback, |
196 int error_code); | 180 int error_code); |
197 | 181 |
198 // Called at the end of the asynchronous operation triggered by | |
199 // OpenEntryFromHash. Makes sure to continue iterating if the open entry was | |
200 // not a success. | |
201 void CheckIterationReturnValue(void** iter, | |
202 Entry** entry, | |
203 const CompletionCallback& callback, | |
204 int error_code); | |
205 | |
206 // A callback thunk used by DoomEntries to clear the |entries_pending_doom_| | 182 // A callback thunk used by DoomEntries to clear the |entries_pending_doom_| |
207 // after a mass doom. | 183 // after a mass doom. |
208 void DoomEntriesComplete(scoped_ptr<std::vector<uint64> > entry_hashes, | 184 void DoomEntriesComplete(scoped_ptr<std::vector<uint64> > entry_hashes, |
209 const CompletionCallback& callback, | 185 const CompletionCallback& callback, |
210 int result); | 186 int result); |
211 | 187 |
212 const base::FilePath path_; | 188 const base::FilePath path_; |
213 const net::CacheType cache_type_; | 189 const net::CacheType cache_type_; |
214 scoped_ptr<SimpleIndex> index_; | 190 scoped_ptr<SimpleIndex> index_; |
215 const scoped_refptr<base::SingleThreadTaskRunner> cache_thread_; | 191 const scoped_refptr<base::SingleThreadTaskRunner> cache_thread_; |
216 scoped_refptr<base::TaskRunner> worker_pool_; | 192 scoped_refptr<base::TaskRunner> worker_pool_; |
217 | 193 |
218 int orig_max_size_; | 194 int orig_max_size_; |
219 const SimpleEntryImpl::OperationsMode entry_operations_mode_; | 195 const SimpleEntryImpl::OperationsMode entry_operations_mode_; |
220 | 196 |
221 EntryMap active_entries_; | 197 EntryMap active_entries_; |
222 | 198 |
223 // One entry for every enumeration in progress. | |
224 ActiveEnumerationMap active_enumerations_; | |
225 | |
226 // The set of all entries which are currently being doomed. To avoid races, | 199 // The set of all entries which are currently being doomed. To avoid races, |
227 // these entries cannot have Doom/Create/Open operations run until the doom | 200 // these entries cannot have Doom/Create/Open operations run until the doom |
228 // is complete. The base::Closure map target is used to store deferred | 201 // is complete. The base::Closure map target is used to store deferred |
229 // operations to be run at the completion of the Doom. | 202 // operations to be run at the completion of the Doom. |
230 base::hash_map<uint64, std::vector<base::Closure> > entries_pending_doom_; | 203 base::hash_map<uint64, std::vector<base::Closure> > entries_pending_doom_; |
231 | 204 |
232 net::NetLog* const net_log_; | 205 net::NetLog* const net_log_; |
233 }; | 206 }; |
234 | 207 |
235 } // namespace disk_cache | 208 } // namespace disk_cache |
236 | 209 |
237 #endif // NET_DISK_CACHE_SIMPLE_SIMPLE_BACKEND_IMPL_H_ | 210 #endif // NET_DISK_CACHE_SIMPLE_SIMPLE_BACKEND_IMPL_H_ |
OLD | NEW |