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