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_MEMORY_MEM_BACKEND_IMPL_H_
8#define NET_DISK_CACHE_MEMORY_MEM_BACKEND_IMPL_H_
9
10#include "base/compiler_specific.h"
11#include "base/containers/hash_tables.h"
12#include "base/memory/weak_ptr.h"
13#include "net/disk_cache/disk_cache.h"
14#include "net/disk_cache/memory/mem_rankings.h"
15
16namespace net {
17class NetLog;
18}  // namespace net
19
20namespace disk_cache {
21
22class 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.
26class NET_EXPORT_PRIVATE MemBackendImpl : public Backend {
27 public:
28  explicit MemBackendImpl(net::NetLog* net_log);
29  virtual ~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 scoped_ptr<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 net::CacheType GetCacheType() const OVERRIDE;
67  virtual int32 GetEntryCount() const OVERRIDE;
68  virtual int OpenEntry(const std::string& key, Entry** entry,
69                        const CompletionCallback& callback) OVERRIDE;
70  virtual int CreateEntry(const std::string& key, Entry** entry,
71                          const CompletionCallback& callback) OVERRIDE;
72  virtual int DoomEntry(const std::string& key,
73                        const CompletionCallback& callback) OVERRIDE;
74  virtual int DoomAllEntries(const CompletionCallback& callback) OVERRIDE;
75  virtual int DoomEntriesBetween(base::Time initial_time,
76                                 base::Time end_time,
77                                 const CompletionCallback& callback) OVERRIDE;
78  virtual int DoomEntriesSince(base::Time initial_time,
79                               const CompletionCallback& callback) OVERRIDE;
80  virtual scoped_ptr<Iterator> CreateIterator() OVERRIDE;
81  virtual void GetStats(
82      std::vector<std::pair<std::string, std::string> >* stats) OVERRIDE {}
83  virtual void OnExternalCacheHit(const std::string& key) OVERRIDE;
84
85 private:
86  class MemIterator;
87  friend class MemIterator;
88
89  typedef base::hash_map<std::string, MemEntryImpl*> EntryMap;
90
91  // Old Backend interface.
92  bool OpenEntry(const std::string& key, Entry** entry);
93  bool CreateEntry(const std::string& key, Entry** entry);
94  bool DoomEntry(const std::string& key);
95  bool DoomAllEntries();
96  bool DoomEntriesBetween(const base::Time initial_time,
97                          const base::Time end_time);
98  bool DoomEntriesSince(const base::Time initial_time);
99
100  // Deletes entries from the cache until the current size is below the limit.
101  // If empty is true, the whole cache will be trimmed, regardless of being in
102  // use.
103  void TrimCache(bool empty);
104
105  // Handles the used storage count.
106  void AddStorageSize(int32 bytes);
107  void SubstractStorageSize(int32 bytes);
108
109  EntryMap entries_;
110  MemRankings rankings_;  // Rankings to be able to trim the cache.
111  int32 max_size_;        // Maximum data size for this instance.
112  int32 current_size_;
113
114  net::NetLog* net_log_;
115
116  base::WeakPtrFactory<MemBackendImpl> weak_factory_;
117
118  DISALLOW_COPY_AND_ASSIGN(MemBackendImpl);
119};
120
121}  // namespace disk_cache
122
123#endif  // NET_DISK_CACHE_MEMORY_MEM_BACKEND_IMPL_H_
124