| OLD | NEW |
| 1 // Copyright (c) 2006-2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2010 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 // See net/disk_cache/disk_cache.h for the public interface of the cache. | 5 // See net/disk_cache/disk_cache.h for the public interface of the cache. |
| 6 | 6 |
| 7 #ifndef NET_DISK_CACHE_BACKEND_IMPL_H_ | 7 #ifndef NET_DISK_CACHE_BACKEND_IMPL_H_ |
| 8 #define NET_DISK_CACHE_BACKEND_IMPL_H_ | 8 #define NET_DISK_CACHE_BACKEND_IMPL_H_ |
| 9 | 9 |
| 10 #include "base/file_path.h" | 10 #include "base/file_path.h" |
| 11 #include "base/hash_tables.h" | 11 #include "base/hash_tables.h" |
| 12 #include "base/timer.h" | 12 #include "base/timer.h" |
| 13 #include "net/disk_cache/block_files.h" | 13 #include "net/disk_cache/block_files.h" |
| 14 #include "net/disk_cache/disk_cache.h" | 14 #include "net/disk_cache/disk_cache.h" |
| 15 #include "net/disk_cache/eviction.h" | 15 #include "net/disk_cache/eviction.h" |
| 16 #include "net/disk_cache/in_flight_backend_io.h" |
| 16 #include "net/disk_cache/rankings.h" | 17 #include "net/disk_cache/rankings.h" |
| 17 #include "net/disk_cache/stats.h" | 18 #include "net/disk_cache/stats.h" |
| 18 #include "net/disk_cache/trace.h" | 19 #include "net/disk_cache/trace.h" |
| 19 | 20 |
| 20 namespace disk_cache { | 21 namespace disk_cache { |
| 21 | 22 |
| 22 enum BackendFlags { | 23 enum BackendFlags { |
| 23 kNone = 0, | 24 kNone = 0, |
| 24 kMask = 1, // A mask (for the index table) was specified. | 25 kMask = 1, // A mask (for the index table) was specified. |
| 25 kMaxSize = 1 << 1, // A maximum size was provided. | 26 kMaxSize = 1 << 1, // A maximum size was provided. |
| 26 kUnitTestMode = 1 << 2, // We are modifying the behavior for testing. | 27 kUnitTestMode = 1 << 2, // We are modifying the behavior for testing. |
| 27 kUpgradeMode = 1 << 3, // This is the upgrade tool (dump). | 28 kUpgradeMode = 1 << 3, // This is the upgrade tool (dump). |
| 28 kNewEviction = 1 << 4, // Use of new eviction was specified. | 29 kNewEviction = 1 << 4, // Use of new eviction was specified. |
| 29 kNoRandom = 1 << 5, // Don't add randomness to the behavior. | 30 kNoRandom = 1 << 5, // Don't add randomness to the behavior. |
| 30 kNoLoadProtection = 1 << 6 // Don't act conservatively under load. | 31 kNoLoadProtection = 1 << 6 // Don't act conservatively under load. |
| 31 }; | 32 }; |
| 32 | 33 |
| 33 // This class implements the Backend interface. An object of this | 34 // This class implements the Backend interface. An object of this |
| 34 // class handles the operations of the cache for a particular profile. | 35 // class handles the operations of the cache for a particular profile. |
| 35 class BackendImpl : public Backend { | 36 class BackendImpl : public Backend { |
| 36 friend class Eviction; | 37 friend class Eviction; |
| 37 public: | 38 public: |
| 38 BackendImpl(const FilePath& path, base::MessageLoopProxy* cache_thread) | 39 BackendImpl(const FilePath& path, base::MessageLoopProxy* cache_thread) |
| 39 : path_(path), block_files_(path), mask_(0), max_size_(0), | 40 : ALLOW_THIS_IN_INITIALIZER_LIST(background_queue_(this, cache_thread)), |
| 41 path_(path), block_files_(path), mask_(0), max_size_(0), |
| 40 cache_type_(net::DISK_CACHE), uma_report_(0), user_flags_(0), | 42 cache_type_(net::DISK_CACHE), uma_report_(0), user_flags_(0), |
| 41 init_(false), restarted_(false), unit_test_(false), read_only_(false), | 43 init_(false), restarted_(false), unit_test_(false), read_only_(false), |
| 42 new_eviction_(false), first_timer_(true), | 44 new_eviction_(false), first_timer_(true), done_(true, false), |
| 43 ALLOW_THIS_IN_INITIALIZER_LIST(factory_(this)) {} | 45 ALLOW_THIS_IN_INITIALIZER_LIST(factory_(this)) {} |
| 44 // mask can be used to limit the usable size of the hash table, for testing. | 46 // mask can be used to limit the usable size of the hash table, for testing. |
| 45 BackendImpl(const FilePath& path, uint32 mask, | 47 BackendImpl(const FilePath& path, uint32 mask, |
| 46 base::MessageLoopProxy* cache_thread) | 48 base::MessageLoopProxy* cache_thread) |
| 47 : path_(path), block_files_(path), mask_(mask), max_size_(0), | 49 : ALLOW_THIS_IN_INITIALIZER_LIST(background_queue_(this, cache_thread)), |
| 50 path_(path), block_files_(path), mask_(mask), max_size_(0), |
| 48 cache_type_(net::DISK_CACHE), uma_report_(0), user_flags_(kMask), | 51 cache_type_(net::DISK_CACHE), uma_report_(0), user_flags_(kMask), |
| 49 init_(false), restarted_(false), unit_test_(false), read_only_(false), | 52 init_(false), restarted_(false), unit_test_(false), read_only_(false), |
| 50 new_eviction_(false), first_timer_(true), | 53 new_eviction_(false), first_timer_(true), done_(true, false), |
| 51 ALLOW_THIS_IN_INITIALIZER_LIST(factory_(this)) {} | 54 ALLOW_THIS_IN_INITIALIZER_LIST(factory_(this)) {} |
| 52 ~BackendImpl(); | 55 ~BackendImpl(); |
| 53 | 56 |
| 54 // Returns a new backend with the desired flags. See the declaration of | 57 // Returns a new backend with the desired flags. See the declaration of |
| 55 // CreateCacheBackend(). | 58 // CreateCacheBackend(). |
| 56 static int CreateBackend(const FilePath& full_path, bool force, | 59 static int CreateBackend(const FilePath& full_path, bool force, |
| 57 int max_bytes, net::CacheType type, | 60 int max_bytes, net::CacheType type, |
| 58 uint32 flags, base::MessageLoopProxy* thread, | 61 uint32 flags, base::MessageLoopProxy* thread, |
| 59 Backend** backend, CompletionCallback* callback); | 62 Backend** backend, CompletionCallback* callback); |
| 60 | 63 |
| 61 // Performs general initialization for this current instance of the cache. | 64 // Performs general initialization for this current instance of the cache. |
| 62 bool Init(); | 65 bool Init(); // Deprecated. |
| 66 int Init(CompletionCallback* callback); |
| 67 int SyncInit(); |
| 68 |
| 69 // Performs final cleanup on destruction. |
| 70 void CleanupCache(); |
| 63 | 71 |
| 64 // Backend interface. | 72 // Backend interface. |
| 65 virtual int32 GetEntryCount() const; | 73 virtual int32 GetEntryCount() const; |
| 66 virtual int OpenEntry(const std::string& key, Entry** entry, | 74 virtual int OpenEntry(const std::string& key, Entry** entry, |
| 67 CompletionCallback* callback); | 75 CompletionCallback* callback); |
| 68 virtual int CreateEntry(const std::string& key, Entry** entry, | 76 virtual int CreateEntry(const std::string& key, Entry** entry, |
| 69 CompletionCallback* callback); | 77 CompletionCallback* callback); |
| 70 virtual int DoomEntry(const std::string& key, CompletionCallback* callback); | 78 virtual int DoomEntry(const std::string& key, CompletionCallback* callback); |
| 71 virtual int DoomAllEntries(CompletionCallback* callback); | 79 virtual int DoomAllEntries(CompletionCallback* callback); |
| 72 virtual int DoomEntriesBetween(const base::Time initial_time, | 80 virtual int DoomEntriesBetween(const base::Time initial_time, |
| 73 const base::Time end_time, | 81 const base::Time end_time, |
| 74 CompletionCallback* callback); | 82 CompletionCallback* callback); |
| 75 virtual int DoomEntriesSince(const base::Time initial_time, | 83 virtual int DoomEntriesSince(const base::Time initial_time, |
| 76 CompletionCallback* callback); | 84 CompletionCallback* callback); |
| 77 virtual int OpenNextEntry(void** iter, Entry** next_entry, | 85 virtual int OpenNextEntry(void** iter, Entry** next_entry, |
| 78 CompletionCallback* callback); | 86 CompletionCallback* callback); |
| 79 virtual void EndEnumeration(void** iter); | 87 virtual void EndEnumeration(void** iter); |
| 80 virtual void GetStats(StatsItems* stats); | 88 virtual void GetStats(StatsItems* stats); |
| 81 | 89 |
| 90 // Synchronous implementation of the asynchronous interface. |
| 91 int SyncOpenEntry(const std::string& key, Entry** entry); |
| 92 int SyncCreateEntry(const std::string& key, Entry** entry); |
| 93 int SyncDoomEntry(const std::string& key); |
| 94 int SyncDoomAllEntries(); |
| 95 int SyncDoomEntriesBetween(const base::Time initial_time, |
| 96 const base::Time end_time); |
| 97 int SyncDoomEntriesSince(const base::Time initial_time); |
| 98 int SyncOpenNextEntry(void** iter, Entry** next_entry); |
| 99 void SyncEndEnumeration(void* iter); |
| 100 |
| 82 // Sets the maximum size for the total amount of data stored by this instance. | 101 // Sets the maximum size for the total amount of data stored by this instance. |
| 83 bool SetMaxSize(int max_bytes); | 102 bool SetMaxSize(int max_bytes); |
| 84 | 103 |
| 85 // Sets the cache type for this backend. | 104 // Sets the cache type for this backend. |
| 86 void SetType(net::CacheType type); | 105 void SetType(net::CacheType type); |
| 87 | 106 |
| 88 // Returns the full name for an external storage file. | 107 // Returns the full name for an external storage file. |
| 89 FilePath GetFileName(Addr address) const; | 108 FilePath GetFileName(Addr address) const; |
| 90 | 109 |
| 91 // Returns the actual file used to store a given (non-external) address. | 110 // Returns the actual file used to store a given (non-external) address. |
| 92 MappedFile* File(Addr address); | 111 MappedFile* File(Addr address); |
| 93 | 112 |
| 113 InFlightBackendIO* background_queue() { |
| 114 return &background_queue_; |
| 115 } |
| 116 |
| 94 // Creates an external storage file. | 117 // Creates an external storage file. |
| 95 bool CreateExternalFile(Addr* address); | 118 bool CreateExternalFile(Addr* address); |
| 96 | 119 |
| 97 // Creates a new storage block of size block_count. | 120 // Creates a new storage block of size block_count. |
| 98 bool CreateBlock(FileType block_type, int block_count, | 121 bool CreateBlock(FileType block_type, int block_count, |
| 99 Addr* block_address); | 122 Addr* block_address); |
| 100 | 123 |
| 101 // Deletes a given storage block. deep set to true can be used to zero-fill | 124 // Deletes a given storage block. deep set to true can be used to zero-fill |
| 102 // the related storage in addition of releasing the related block. | 125 // the related storage in addition of releasing the related block. |
| 103 void DeleteBlock(Addr block_address, bool deep); | 126 void DeleteBlock(Addr block_address, bool deep); |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 186 | 209 |
| 187 // Sets the eviction algorithm to version 2. | 210 // Sets the eviction algorithm to version 2. |
| 188 void SetNewEviction(); | 211 void SetNewEviction(); |
| 189 | 212 |
| 190 // Sets an explicit set of BackendFlags. | 213 // Sets an explicit set of BackendFlags. |
| 191 void SetFlags(uint32 flags); | 214 void SetFlags(uint32 flags); |
| 192 | 215 |
| 193 // Clears the counter of references to test handling of corruptions. | 216 // Clears the counter of references to test handling of corruptions. |
| 194 void ClearRefCountForTest(); | 217 void ClearRefCountForTest(); |
| 195 | 218 |
| 219 // Sends a dummy operation through the operation queue, for unit tests. |
| 220 int FlushQueueForTest(CompletionCallback* callback); |
| 221 |
| 196 // Peforms a simple self-check, and returns the number of dirty items | 222 // Peforms a simple self-check, and returns the number of dirty items |
| 197 // or an error code (negative value). | 223 // or an error code (negative value). |
| 198 int SelfCheck(); | 224 int SelfCheck(); |
| 199 | 225 |
| 200 // Same bahavior as OpenNextEntry but walks the list from back to front. | 226 // Same bahavior as OpenNextEntry but walks the list from back to front. |
| 201 bool OpenPrevEntry(void** iter, Entry** prev_entry); | 227 int OpenPrevEntry(void** iter, Entry** prev_entry, |
| 228 CompletionCallback* callback); |
| 229 int SyncOpenPrevEntry(void** iter, Entry** prev_entry); |
| 202 | 230 |
| 203 // Old Backend interface. | 231 // Old Backend interface. |
| 204 bool OpenEntry(const std::string& key, Entry** entry); | 232 bool OpenEntry(const std::string& key, Entry** entry); |
| 205 bool CreateEntry(const std::string& key, Entry** entry); | 233 bool CreateEntry(const std::string& key, Entry** entry); |
| 206 bool DoomEntry(const std::string& key); | 234 bool DoomEntry(const std::string& key); |
| 207 bool DoomAllEntries(); | 235 bool DoomAllEntries(); |
| 208 bool DoomEntriesBetween(const base::Time initial_time, | 236 bool DoomEntriesBetween(const base::Time initial_time, |
| 209 const base::Time end_time); | 237 const base::Time end_time); |
| 210 bool DoomEntriesSince(const base::Time initial_time); | 238 bool DoomEntriesSince(const base::Time initial_time); |
| 211 bool OpenNextEntry(void** iter, Entry** next_entry); | |
| 212 | 239 |
| 213 // Open or create an entry for the given |key|. | 240 // Open or create an entry for the given |key| or |iter|. |
| 214 EntryImpl* OpenEntryImpl(const std::string& key); | 241 EntryImpl* OpenEntryImpl(const std::string& key); |
| 215 EntryImpl* CreateEntryImpl(const std::string& key); | 242 EntryImpl* CreateEntryImpl(const std::string& key); |
| 243 EntryImpl* OpenNextEntryImpl(void** iter); |
| 244 EntryImpl* OpenPrevEntryImpl(void** iter); |
| 216 | 245 |
| 217 private: | 246 private: |
| 218 typedef base::hash_map<CacheAddr, EntryImpl*> EntriesMap; | 247 typedef base::hash_map<CacheAddr, EntryImpl*> EntriesMap; |
| 219 | 248 |
| 220 // Creates a new backing file for the cache index. | 249 // Creates a new backing file for the cache index. |
| 221 bool CreateBackingStore(disk_cache::File* file); | 250 bool CreateBackingStore(disk_cache::File* file); |
| 222 bool InitBackingStore(bool* file_created); | 251 bool InitBackingStore(bool* file_created); |
| 223 void AdjustMaxCacheSize(int table_len); | 252 void AdjustMaxCacheSize(int table_len); |
| 224 | 253 |
| 225 // Deletes the cache and starts again. | 254 // Deletes the cache and starts again. |
| 226 void RestartCache(); | 255 void RestartCache(); |
| 227 void PrepareForRestart(); | 256 void PrepareForRestart(); |
| 228 | 257 |
| 229 // Creates a new entry object and checks to see if it is dirty. Returns zero | 258 // Creates a new entry object and checks to see if it is dirty. Returns zero |
| 230 // on success, or a disk_cache error on failure. | 259 // on success, or a disk_cache error on failure. |
| 231 int NewEntry(Addr address, EntryImpl** entry, bool* dirty); | 260 int NewEntry(Addr address, EntryImpl** entry, bool* dirty); |
| 232 | 261 |
| 233 // Returns a given entry from the cache. The entry to match is determined by | 262 // Returns a given entry from the cache. The entry to match is determined by |
| 234 // key and hash, and the returned entry may be the matched one or it's parent | 263 // key and hash, and the returned entry may be the matched one or it's parent |
| 235 // on the list of entries with the same hash (or bucket). | 264 // on the list of entries with the same hash (or bucket). |
| 236 EntryImpl* MatchEntry(const std::string& key, uint32 hash, bool find_parent); | 265 EntryImpl* MatchEntry(const std::string& key, uint32 hash, bool find_parent); |
| 237 | 266 |
| 238 // Opens the next or previous entry on a cache iteration. | 267 // Opens the next or previous entry on a cache iteration. |
| 239 bool OpenFollowingEntry(bool forward, void** iter, Entry** next_entry); | 268 EntryImpl* OpenFollowingEntry(bool forward, void** iter); |
| 240 | 269 |
| 241 // Opens the next or previous entry on a single list. If successfull, | 270 // Opens the next or previous entry on a single list. If successfull, |
| 242 // |from_entry| will be updated to point to the new entry, otherwise it will | 271 // |from_entry| will be updated to point to the new entry, otherwise it will |
| 243 // be set to NULL; in other words, it is used as an explicit iterator. | 272 // be set to NULL; in other words, it is used as an explicit iterator. |
| 244 bool OpenFollowingEntryFromList(bool forward, Rankings::List list, | 273 bool OpenFollowingEntryFromList(bool forward, Rankings::List list, |
| 245 CacheRankingsBlock** from_entry, | 274 CacheRankingsBlock** from_entry, |
| 246 EntryImpl** next_entry); | 275 EntryImpl** next_entry); |
| 247 | 276 |
| 248 // Returns the entry that is pointed by |next|. If we are trimming the cache, | 277 // Returns the entry that is pointed by |next|. If we are trimming the cache, |
| 249 // |to_evict| should be true so that we don't perform extra disk writes. | 278 // |to_evict| should be true so that we don't perform extra disk writes. |
| (...skipping 26 matching lines...) Expand all Loading... |
| 276 | 305 |
| 277 // Performs basic checks on the index file. Returns false on failure. | 306 // Performs basic checks on the index file. Returns false on failure. |
| 278 bool CheckIndex(); | 307 bool CheckIndex(); |
| 279 | 308 |
| 280 // Part of the selt test. Returns the number or dirty entries, or an error. | 309 // Part of the selt test. Returns the number or dirty entries, or an error. |
| 281 int CheckAllEntries(); | 310 int CheckAllEntries(); |
| 282 | 311 |
| 283 // Part of the self test. Returns false if the entry is corrupt. | 312 // Part of the self test. Returns false if the entry is corrupt. |
| 284 bool CheckEntry(EntryImpl* cache_entry); | 313 bool CheckEntry(EntryImpl* cache_entry); |
| 285 | 314 |
| 315 InFlightBackendIO background_queue_; // The controller of pending operations. |
| 286 scoped_refptr<MappedFile> index_; // The main cache index. | 316 scoped_refptr<MappedFile> index_; // The main cache index. |
| 287 FilePath path_; // Path to the folder used as backing storage. | 317 FilePath path_; // Path to the folder used as backing storage. |
| 288 Index* data_; // Pointer to the index data. | 318 Index* data_; // Pointer to the index data. |
| 289 BlockFiles block_files_; // Set of files used to store all data. | 319 BlockFiles block_files_; // Set of files used to store all data. |
| 290 Rankings rankings_; // Rankings to be able to trim the cache. | 320 Rankings rankings_; // Rankings to be able to trim the cache. |
| 291 uint32 mask_; // Binary mask to map a hash to the hash table. | 321 uint32 mask_; // Binary mask to map a hash to the hash table. |
| 292 int32 max_size_; // Maximum data size for this instance. | 322 int32 max_size_; // Maximum data size for this instance. |
| 293 Eviction eviction_; // Handler of the eviction algorithm. | 323 Eviction eviction_; // Handler of the eviction algorithm. |
| 294 EntriesMap open_entries_; // Map of open entries. | 324 EntriesMap open_entries_; // Map of open entries. |
| 295 int num_refs_; // Number of referenced cache entries. | 325 int num_refs_; // Number of referenced cache entries. |
| 296 int max_refs_; // Max number of referenced cache entries. | 326 int max_refs_; // Max number of referenced cache entries. |
| 297 int num_pending_io_; // Number of pending IO operations. | 327 int num_pending_io_; // Number of pending IO operations. |
| 298 net::CacheType cache_type_; | 328 net::CacheType cache_type_; |
| 299 int uma_report_; // Controls transmision of UMA data. | 329 int uma_report_; // Controls transmision of UMA data. |
| 300 uint32 user_flags_; // Flags set by the user. | 330 uint32 user_flags_; // Flags set by the user. |
| 301 bool init_; // controls the initialization of the system. | 331 bool init_; // controls the initialization of the system. |
| 302 bool restarted_; | 332 bool restarted_; |
| 303 bool unit_test_; | 333 bool unit_test_; |
| 304 bool read_only_; // Prevents updates of the rankings data (used by tools). | 334 bool read_only_; // Prevents updates of the rankings data (used by tools). |
| 305 bool disabled_; | 335 bool disabled_; |
| 306 bool new_eviction_; // What eviction algorithm should be used. | 336 bool new_eviction_; // What eviction algorithm should be used. |
| 307 bool first_timer_; // True if the timer has not been called. | 337 bool first_timer_; // True if the timer has not been called. |
| 308 | 338 |
| 309 Stats stats_; // Usage statistcs. | 339 Stats stats_; // Usage statistcs. |
| 310 base::RepeatingTimer<BackendImpl> timer_; // Usage timer. | 340 base::RepeatingTimer<BackendImpl> timer_; // Usage timer. |
| 341 base::WaitableEvent done_; // Signals the end of background work. |
| 311 scoped_refptr<TraceObject> trace_object_; // Inits internal tracing. | 342 scoped_refptr<TraceObject> trace_object_; // Inits internal tracing. |
| 312 ScopedRunnableMethodFactory<BackendImpl> factory_; | 343 ScopedRunnableMethodFactory<BackendImpl> factory_; |
| 313 | 344 |
| 314 DISALLOW_COPY_AND_ASSIGN(BackendImpl); | 345 DISALLOW_COPY_AND_ASSIGN(BackendImpl); |
| 315 }; | 346 }; |
| 316 | 347 |
| 317 // Returns the prefered max cache size given the available disk space. | 348 // Returns the prefered max cache size given the available disk space. |
| 318 int PreferedCacheSize(int64 available); | 349 int PreferedCacheSize(int64 available); |
| 319 | 350 |
| 320 } // namespace disk_cache | 351 } // namespace disk_cache |
| 321 | 352 |
| 322 #endif // NET_DISK_CACHE_BACKEND_IMPL_H_ | 353 #endif // NET_DISK_CACHE_BACKEND_IMPL_H_ |
| OLD | NEW |