7b18ca06138c7f2f9b2a256473ca43cbfc5d64dd
[platform/framework/web/crosswalk.git] / src / net / disk_cache / memory / mem_backend_impl.h
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
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 NET_EXPORT_PRIVATE MemBackendImpl : public Backend {
27  public:
28   explicit MemBackendImpl(net::NetLog* net_log);
29   ~MemBackendImpl() override;
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   net::CacheType GetCacheType() const override;
67   int32 GetEntryCount() const override;
68   int OpenEntry(const std::string& key,
69                 Entry** entry,
70                 const CompletionCallback& callback) override;
71   int CreateEntry(const std::string& key,
72                   Entry** entry,
73                   const CompletionCallback& callback) override;
74   int DoomEntry(const std::string& key,
75                 const CompletionCallback& callback) override;
76   int DoomAllEntries(const CompletionCallback& callback) override;
77   int DoomEntriesBetween(base::Time initial_time,
78                          base::Time end_time,
79                          const CompletionCallback& callback) override;
80   int DoomEntriesSince(base::Time initial_time,
81                        const CompletionCallback& callback) override;
82   scoped_ptr<Iterator> CreateIterator() override;
83   void GetStats(
84       std::vector<std::pair<std::string, std::string>>* stats) override {}
85   void OnExternalCacheHit(const std::string& key) override;
86
87  private:
88   class MemIterator;
89   friend class MemIterator;
90
91   typedef base::hash_map<std::string, MemEntryImpl*> EntryMap;
92
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);
101
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);
106
107   // Handles the used storage count.
108   void AddStorageSize(int32 bytes);
109   void SubstractStorageSize(int32 bytes);
110
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_;
115
116   net::NetLog* net_log_;
117
118   base::WeakPtrFactory<MemBackendImpl> weak_factory_;
119
120   DISALLOW_COPY_AND_ASSIGN(MemBackendImpl);
121 };
122
123 }  // namespace disk_cache
124
125 #endif  // NET_DISK_CACHE_MEMORY_MEM_BACKEND_IMPL_H_