blob: 9b920ac59494ef70b03e8ff0a8dbb9fc23fdfef4 [file] [log] [blame]
[email protected]73a797fb2010-06-07 02:10:181// Copyright (c) 2010 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit586acc5fe2008-07-26 22:42:524
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]32b76ef2010-07-26 23:08:249#pragma once
initial.commit586acc5fe2008-07-26 22:42:5210
[email protected]408d35f52008-08-13 18:30:2211#include "base/hash_tables.h"
initial.commit586acc5fe2008-07-26 22:42:5212
13#include "net/disk_cache/disk_cache.h"
14#include "net/disk_cache/mem_rankings.h"
15
16namespace disk_cache {
17
18class MemEntryImpl;
19
20// This class implements the Backend interface. An object of this class handles
[email protected]658c1732009-06-15 20:41:3021// the operations of the cache without writing to disk.
initial.commit586acc5fe2008-07-26 22:42:5222class MemBackendImpl : public Backend {
23 public:
[email protected]b104b502010-10-18 20:21:3124 MemBackendImpl();
initial.commit586acc5fe2008-07-26 22:42:5225 ~MemBackendImpl();
26
[email protected]ec44a9a2010-06-15 19:31:5127 // 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.commit586acc5fe2008-07-26 22:42:5234 // Performs general initialization for this current instance of the cache.
35 bool Init();
36
initial.commit586acc5fe2008-07-26 22:42:5237 // 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]658c1732009-06-15 20:41:3052 // 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]4b3c95dd2011-01-07 23:02:1161 // 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.commit586acc5fe2008-07-26 22:42:5280 private:
[email protected]4b3c95dd2011-01-07 23:02:1181 typedef base::hash_map<std::string, MemEntryImpl*> EntryMap;
82
[email protected]3f73cd312010-06-17 21:59:3983 // 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.commit586acc5fe2008-07-26 22:42:5293 // 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.commit586acc5fe2008-07-26 22:42:52102 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]73a797fb2010-06-07 02:10:18107 DISALLOW_COPY_AND_ASSIGN(MemBackendImpl);
initial.commit586acc5fe2008-07-26 22:42:52108};
109
110} // namespace disk_cache
111
112#endif // NET_DISK_CACHE_MEM_BACKEND_IMPL_H__