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