Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / net / disk_cache / memory / mem_backend_impl.h
blob6be8e946797326ed0e3ed1e4ba20791382eefccf
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.
5 // See net/disk_cache/disk_cache.h for the public interface of the cache.
7 #ifndef NET_DISK_CACHE_MEMORY_MEM_BACKEND_IMPL_H_
8 #define NET_DISK_CACHE_MEMORY_MEM_BACKEND_IMPL_H_
10 #include "base/compiler_specific.h"
11 #include "base/containers/hash_tables.h"
12 #include "base/memory/weak_ptr.h"
13 #include "base/strings/string_split.h"
14 #include "net/disk_cache/disk_cache.h"
15 #include "net/disk_cache/memory/mem_rankings.h"
17 namespace net {
18 class NetLog;
19 } // namespace net
21 namespace disk_cache {
23 class MemEntryImpl;
25 // This class implements the Backend interface. An object of this class handles
26 // the operations of the cache without writing to disk.
27 class NET_EXPORT_PRIVATE MemBackendImpl : public Backend {
28 public:
29 explicit MemBackendImpl(net::NetLog* net_log);
30 ~MemBackendImpl() override;
32 // Returns an instance of a Backend implemented only in memory. The returned
33 // object should be deleted when not needed anymore. max_bytes is the maximum
34 // size the cache can grow to. If zero is passed in as max_bytes, the cache
35 // will determine the value to use based on the available memory. The returned
36 // pointer can be NULL if a fatal error is found.
37 static scoped_ptr<Backend> CreateBackend(int max_bytes, net::NetLog* net_log);
39 // Performs general initialization for this current instance of the cache.
40 bool Init();
42 // Sets the maximum size for the total amount of data stored by this instance.
43 bool SetMaxSize(int max_bytes);
45 // Permanently deletes an entry.
46 void InternalDoomEntry(MemEntryImpl* entry);
48 // Updates the ranking information for an entry.
49 void UpdateRank(MemEntryImpl* node);
51 // A user data block is being created, extended or truncated.
52 void ModifyStorageSize(int32 old_size, int32 new_size);
54 // Returns the maximum size for a file to reside on the cache.
55 int MaxFileSize() const;
57 // Insert an MemEntryImpl into the ranking list. This method is only called
58 // from MemEntryImpl to insert child entries. The reference can be removed
59 // by calling RemoveFromRankingList(|entry|).
60 void InsertIntoRankingList(MemEntryImpl* entry);
62 // Remove |entry| from ranking list. This method is only called from
63 // MemEntryImpl to remove a child entry from the ranking list.
64 void RemoveFromRankingList(MemEntryImpl* entry);
66 // Backend interface.
67 net::CacheType GetCacheType() const override;
68 int32 GetEntryCount() const override;
69 int OpenEntry(const std::string& key,
70 Entry** entry,
71 const CompletionCallback& callback) override;
72 int CreateEntry(const std::string& key,
73 Entry** entry,
74 const CompletionCallback& callback) override;
75 int DoomEntry(const std::string& key,
76 const CompletionCallback& callback) override;
77 int DoomAllEntries(const CompletionCallback& callback) override;
78 int DoomEntriesBetween(base::Time initial_time,
79 base::Time end_time,
80 const CompletionCallback& callback) override;
81 int DoomEntriesSince(base::Time initial_time,
82 const CompletionCallback& callback) override;
83 scoped_ptr<Iterator> CreateIterator() override;
84 void GetStats(base::StringPairs* stats) override {}
85 void OnExternalCacheHit(const std::string& key) override;
87 private:
88 class MemIterator;
89 friend class MemIterator;
91 typedef base::hash_map<std::string, MemEntryImpl*> EntryMap;
93 // Old Backend interface.
94 bool OpenEntry(const std::string& key, Entry** entry);
95 bool CreateEntry(const std::string& key, Entry** entry);
96 bool DoomEntry(const std::string& key);
97 bool DoomAllEntries();
98 bool DoomEntriesBetween(const base::Time initial_time,
99 const base::Time end_time);
100 bool DoomEntriesSince(const base::Time initial_time);
102 // Deletes entries from the cache until the current size is below the limit.
103 // If empty is true, the whole cache will be trimmed, regardless of being in
104 // use.
105 void TrimCache(bool empty);
107 // Handles the used storage count.
108 void AddStorageSize(int32 bytes);
109 void SubstractStorageSize(int32 bytes);
111 EntryMap entries_;
112 MemRankings rankings_; // Rankings to be able to trim the cache.
113 int32 max_size_; // Maximum data size for this instance.
114 int32 current_size_;
116 net::NetLog* net_log_;
118 base::WeakPtrFactory<MemBackendImpl> weak_factory_;
120 DISALLOW_COPY_AND_ASSIGN(MemBackendImpl);
123 } // namespace disk_cache
125 #endif // NET_DISK_CACHE_MEMORY_MEM_BACKEND_IMPL_H_