• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2012 The Chromium Authors
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 <stdint.h>
11 
12 #include <string>
13 #include <unordered_map>
14 
15 #include "base/compiler_specific.h"
16 #include "base/containers/linked_list.h"
17 #include "base/functional/callback_forward.h"
18 #include "base/memory/memory_pressure_listener.h"
19 #include "base/memory/raw_ptr.h"
20 #include "base/memory/weak_ptr.h"
21 #include "base/strings/string_split.h"
22 #include "base/time/time.h"
23 #include "net/base/net_export.h"
24 #include "net/disk_cache/disk_cache.h"
25 #include "net/disk_cache/memory/mem_entry_impl.h"
26 
27 namespace base {
28 class Clock;
29 }
30 
31 namespace net {
32 class NetLog;
33 }  // namespace net
34 
35 namespace disk_cache {
36 
37 // This class implements the Backend interface. An object of this class handles
38 // the operations of the cache without writing to disk.
39 class NET_EXPORT_PRIVATE MemBackendImpl final : public Backend {
40  public:
41   explicit MemBackendImpl(net::NetLog* net_log);
42 
43   MemBackendImpl(const MemBackendImpl&) = delete;
44   MemBackendImpl& operator=(const MemBackendImpl&) = delete;
45 
46   ~MemBackendImpl() override;
47 
48   // Returns an instance of a Backend implemented only in memory. The returned
49   // object should be deleted when not needed anymore. max_bytes is the maximum
50   // size the cache can grow to. If zero is passed in as max_bytes, the cache
51   // will determine the value to use based on the available memory. The returned
52   // pointer can be NULL if a fatal error is found.
53   static std::unique_ptr<MemBackendImpl> CreateBackend(int64_t max_bytes,
54                                                        net::NetLog* net_log);
55 
56   // Performs general initialization for this current instance of the cache.
57   bool Init();
58 
59   // Returns the maximum size for a file to reside on the cache.
60   int64_t MaxFileSize() const override;
61 
62   // These next methods (before the implementation of the Backend interface) are
63   // called by MemEntryImpl to update the state of the backend during the entry
64   // lifecycle.
65 
66   // Signals that new entry has been created, and should be placed in
67   // |lru_list_| so that it is eligable for eviction.
68   void OnEntryInserted(MemEntryImpl* entry);
69 
70   // Signals that an entry has been updated, and thus should be moved to the end
71   // of |lru_list_|.
72   void OnEntryUpdated(MemEntryImpl* entry);
73 
74   // Signals that an entry has been doomed, and so it should be removed from the
75   // list of active entries as appropriate, as well as removed from the
76   // |lru_list_|.
77   void OnEntryDoomed(MemEntryImpl* entry);
78 
79   // Adjust the current size of this backend by |delta|. This is used to
80   // determine if eviction is necessary and when eviction is finished.
81   void ModifyStorageSize(int32_t delta);
82 
83   // Returns true if the cache's size is greater than the maximum allowed
84   // size.
85   bool HasExceededStorageSize() const;
86 
87   // Sets a callback to be posted after we are destroyed. Should be called at
88   // most once.
89   void SetPostCleanupCallback(base::OnceClosure cb);
90 
91   static base::Time Now(const base::WeakPtr<MemBackendImpl>& self);
92   void SetClockForTesting(base::Clock* clock);  // doesn't take ownership.
93 
94   // Backend interface.
95   int32_t GetEntryCount() const override;
96   EntryResult OpenOrCreateEntry(const std::string& key,
97                                 net::RequestPriority request_priority,
98                                 EntryResultCallback callback) override;
99   EntryResult OpenEntry(const std::string& key,
100                         net::RequestPriority request_priority,
101                         EntryResultCallback callback) override;
102   EntryResult CreateEntry(const std::string& key,
103                           net::RequestPriority request_priority,
104                           EntryResultCallback callback) override;
105   net::Error DoomEntry(const std::string& key,
106                        net::RequestPriority priority,
107                        CompletionOnceCallback callback) override;
108   net::Error DoomAllEntries(CompletionOnceCallback callback) override;
109   net::Error DoomEntriesBetween(base::Time initial_time,
110                                 base::Time end_time,
111                                 CompletionOnceCallback callback) override;
112   net::Error DoomEntriesSince(base::Time initial_time,
113                               CompletionOnceCallback callback) override;
114   int64_t CalculateSizeOfAllEntries(
115       Int64CompletionOnceCallback callback) override;
116   int64_t CalculateSizeOfEntriesBetween(
117       base::Time initial_time,
118       base::Time end_time,
119       Int64CompletionOnceCallback callback) override;
120   std::unique_ptr<Iterator> CreateIterator() override;
GetStats(base::StringPairs * stats)121   void GetStats(base::StringPairs* stats) override {}
122   void OnExternalCacheHit(const std::string& key) override;
123 
124  private:
125   class MemIterator;
126   friend class MemIterator;
127 
128   using EntryMap =
129       std::unordered_map<std::string, raw_ptr<MemEntryImpl, CtnExperimental>>;
130 
131   // Sets the maximum size for the total amount of data stored by this instance.
132   bool SetMaxSize(int64_t max_bytes);
133 
134   // Deletes entries from the cache until the current size is below the limit.
135   void EvictIfNeeded();
136 
137   // Deletes entries until the current size is below |goal|.
138   void EvictTill(int target_size);
139 
140   // Called when we get low on memory.
141   void OnMemoryPressure(
142       base::MemoryPressureListener::MemoryPressureLevel memory_pressure_level);
143 
144   raw_ptr<base::Clock> custom_clock_for_testing_ = nullptr;  // usually nullptr.
145 
146   EntryMap entries_;
147 
148   // Stored in increasing order of last use time, from least recently used to
149   // most recently used.
150   base::LinkedList<MemEntryImpl> lru_list_;
151 
152   int32_t max_size_ = 0;  // Maximum data size for this instance.
153   int32_t current_size_ = 0;
154 
155   raw_ptr<net::NetLog> net_log_;
156   base::OnceClosure post_cleanup_callback_;
157 
158   base::MemoryPressureListener memory_pressure_listener_;
159 
160   base::WeakPtrFactory<MemBackendImpl> weak_factory_{this};
161 };
162 
163 }  // namespace disk_cache
164 
165 #endif  // NET_DISK_CACHE_MEMORY_MEM_BACKEND_IMPL_H_
166