1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 // See net/disk_cache/disk_cache.h for the public interface of the cache. 6 7 #ifndef NET_DISK_CACHE_MEM_BACKEND_IMPL_H__ 8 #define NET_DISK_CACHE_MEM_BACKEND_IMPL_H__ 9 10 #include "base/compiler_specific.h" 11 #include "base/containers/hash_tables.h" 12 #include "net/disk_cache/disk_cache.h" 13 #include "net/disk_cache/mem_rankings.h" 14 15 namespace net { 16 class NetLog; 17 } // namespace net 18 19 namespace disk_cache { 20 21 class MemEntryImpl; 22 23 // This class implements the Backend interface. An object of this class handles 24 // the operations of the cache without writing to disk. 25 class NET_EXPORT_PRIVATE MemBackendImpl : public Backend { 26 public: 27 explicit MemBackendImpl(net::NetLog* net_log); 28 virtual ~MemBackendImpl(); 29 30 // Returns an instance of a Backend implemented only in memory. The returned 31 // object should be deleted when not needed anymore. max_bytes is the maximum 32 // size the cache can grow to. If zero is passed in as max_bytes, the cache 33 // will determine the value to use based on the available memory. The returned 34 // pointer can be NULL if a fatal error is found. 35 static scoped_ptr<Backend> CreateBackend(int max_bytes, net::NetLog* net_log); 36 37 // Performs general initialization for this current instance of the cache. 38 bool Init(); 39 40 // Sets the maximum size for the total amount of data stored by this instance. 41 bool SetMaxSize(int max_bytes); 42 43 // Permanently deletes an entry. 44 void InternalDoomEntry(MemEntryImpl* entry); 45 46 // Updates the ranking information for an entry. 47 void UpdateRank(MemEntryImpl* node); 48 49 // A user data block is being created, extended or truncated. 50 void ModifyStorageSize(int32 old_size, int32 new_size); 51 52 // Returns the maximum size for a file to reside on the cache. 53 int MaxFileSize() const; 54 55 // Insert an MemEntryImpl into the ranking list. This method is only called 56 // from MemEntryImpl to insert child entries. The reference can be removed 57 // by calling RemoveFromRankingList(|entry|). 58 void InsertIntoRankingList(MemEntryImpl* entry); 59 60 // Remove |entry| from ranking list. This method is only called from 61 // MemEntryImpl to remove a child entry from the ranking list. 62 void RemoveFromRankingList(MemEntryImpl* entry); 63 64 // Backend interface. 65 virtual net::CacheType GetCacheType() const OVERRIDE; 66 virtual int32 GetEntryCount() const OVERRIDE; 67 virtual int OpenEntry(const std::string& key, Entry** entry, 68 const CompletionCallback& callback) OVERRIDE; 69 virtual int CreateEntry(const std::string& key, Entry** entry, 70 const CompletionCallback& callback) OVERRIDE; 71 virtual int DoomEntry(const std::string& key, 72 const CompletionCallback& callback) OVERRIDE; 73 virtual int DoomAllEntries(const CompletionCallback& callback) OVERRIDE; 74 virtual int DoomEntriesBetween(base::Time initial_time, 75 base::Time end_time, 76 const CompletionCallback& callback) OVERRIDE; 77 virtual int DoomEntriesSince(base::Time initial_time, 78 const CompletionCallback& callback) OVERRIDE; 79 virtual int OpenNextEntry(void** iter, Entry** next_entry, 80 const CompletionCallback& callback) OVERRIDE; 81 virtual void EndEnumeration(void** iter) OVERRIDE; 82 virtual void GetStats( 83 std::vector<std::pair<std::string, std::string> >* stats) OVERRIDE {} 84 virtual void OnExternalCacheHit(const std::string& key) OVERRIDE; 85 86 private: 87 typedef base::hash_map<std::string, MemEntryImpl*> EntryMap; 88 89 // Old Backend interface. 90 bool OpenEntry(const std::string& key, Entry** entry); 91 bool CreateEntry(const std::string& key, Entry** entry); 92 bool DoomEntry(const std::string& key); 93 bool DoomAllEntries(); 94 bool DoomEntriesBetween(const base::Time initial_time, 95 const base::Time end_time); 96 bool DoomEntriesSince(const base::Time initial_time); 97 bool OpenNextEntry(void** iter, Entry** next_entry); 98 99 // Deletes entries from the cache until the current size is below the limit. 100 // If empty is true, the whole cache will be trimmed, regardless of being in 101 // use. 102 void TrimCache(bool empty); 103 104 // Handles the used storage count. 105 void AddStorageSize(int32 bytes); 106 void SubstractStorageSize(int32 bytes); 107 108 EntryMap entries_; 109 MemRankings rankings_; // Rankings to be able to trim the cache. 110 int32 max_size_; // Maximum data size for this instance. 111 int32 current_size_; 112 113 net::NetLog* net_log_; 114 115 DISALLOW_COPY_AND_ASSIGN(MemBackendImpl); 116 }; 117 118 } // namespace disk_cache 119 120 #endif // NET_DISK_CACHE_MEM_BACKEND_IMPL_H__ 121