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