xref: /aosp_15_r20/external/cronet/net/disk_cache/simple/simple_backend_impl.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2013 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 #ifndef NET_DISK_CACHE_SIMPLE_SIMPLE_BACKEND_IMPL_H_
6 #define NET_DISK_CACHE_SIMPLE_SIMPLE_BACKEND_IMPL_H_
7 
8 #include <stdint.h>
9 
10 #include <memory>
11 #include <string>
12 #include <unordered_map>
13 #include <utility>
14 #include <vector>
15 
16 #include "base/compiler_specific.h"
17 #include "base/files/file_path.h"
18 #include "base/functional/callback_forward.h"
19 #include "base/memory/raw_ptr.h"
20 #include "base/memory/scoped_refptr.h"
21 #include "base/memory/weak_ptr.h"
22 #include "base/strings/string_split.h"
23 #include "base/task/sequenced_task_runner.h"
24 #include "base/task/task_traits.h"
25 #include "base/time/time.h"
26 #include "build/build_config.h"
27 #include "net/base/cache_type.h"
28 #include "net/base/net_export.h"
29 #include "net/disk_cache/disk_cache.h"
30 #include "net/disk_cache/simple/post_operation_waiter.h"
31 #include "net/disk_cache/simple/simple_entry_impl.h"
32 #include "net/disk_cache/simple/simple_index_delegate.h"
33 
34 namespace net {
35 class PrioritizedTaskRunner;
36 }  // namespace net
37 
38 namespace disk_cache {
39 
40 // SimpleBackendImpl is a new cache backend that stores entries in individual
41 // files.
42 // See
43 // http://www.chromium.org/developers/design-documents/network-stack/disk-cache/very-simple-backend
44 //
45 // The SimpleBackendImpl provides safe iteration; mutating entries during
46 // iteration cannot cause a crash. It is undefined whether entries created or
47 // destroyed during the iteration will be included in any pre-existing
48 // iterations.
49 //
50 // The non-static functions below must be called on the sequence on which the
51 // SimpleBackendImpl instance is created.
52 
53 class BackendCleanupTracker;
54 class BackendFileOperationsFactory;
55 class SimpleEntryImpl;
56 class SimpleFileTracker;
57 class SimpleIndex;
58 
59 class NET_EXPORT_PRIVATE SimpleBackendImpl : public Backend,
60     public SimpleIndexDelegate,
61     public base::SupportsWeakPtr<SimpleBackendImpl> {
62  public:
63   // Note: only pass non-nullptr for |file_tracker| if you don't want the global
64   // one (which things other than tests would want). |file_tracker| must outlive
65   // the backend and all the entries, including their asynchronous close.
66   // |Init()| must be called to finish the initialization process.
67   SimpleBackendImpl(
68       scoped_refptr<BackendFileOperationsFactory> file_operations_factory,
69       const base::FilePath& path,
70       scoped_refptr<BackendCleanupTracker> cleanup_tracker,
71       SimpleFileTracker* file_tracker,
72       int64_t max_bytes,
73       net::CacheType cache_type,
74       net::NetLog* net_log);
75 
76   ~SimpleBackendImpl() override;
77 
index()78   SimpleIndex* index() { return index_.get(); }
79 
80   void SetTaskRunnerForTesting(
81       scoped_refptr<base::SequencedTaskRunner> task_runner);
82 
83   // Finishes initialization. Always asynchronous.
84   void Init(CompletionOnceCallback completion_callback);
85 
86   // Sets the maximum size for the total amount of data stored by this instance.
87   bool SetMaxSize(int64_t max_bytes);
88 
89   // Returns the maximum file size permitted in this backend.
90   int64_t MaxFileSize() const override;
91 
92   // The entry for |entry_hash| is being doomed; the backend will not attempt
93   // run new operations for this |entry_hash| until the Doom is completed.
94   //
95   // The return value should be used to call OnOperationComplete.
96   scoped_refptr<SimplePostOperationWaiterTable> OnDoomStart(
97       uint64_t entry_hash);
98 
99   // SimpleIndexDelegate:
100   void DoomEntries(std::vector<uint64_t>* entry_hashes,
101                    CompletionOnceCallback callback) override;
102 
103   // Backend:
104   int32_t GetEntryCount() const override;
105   EntryResult OpenEntry(const std::string& key,
106                         net::RequestPriority request_priority,
107                         EntryResultCallback callback) override;
108   EntryResult CreateEntry(const std::string& key,
109                           net::RequestPriority request_priority,
110                           EntryResultCallback callback) override;
111   EntryResult OpenOrCreateEntry(const std::string& key,
112                                 net::RequestPriority priority,
113                                 EntryResultCallback callback) override;
114   net::Error DoomEntry(const std::string& key,
115                        net::RequestPriority priority,
116                        CompletionOnceCallback callback) override;
117   net::Error DoomAllEntries(CompletionOnceCallback callback) override;
118   net::Error DoomEntriesBetween(base::Time initial_time,
119                                 base::Time end_time,
120                                 CompletionOnceCallback callback) override;
121   net::Error DoomEntriesSince(base::Time initial_time,
122                               CompletionOnceCallback callback) override;
123   int64_t CalculateSizeOfAllEntries(
124       Int64CompletionOnceCallback callback) override;
125   int64_t CalculateSizeOfEntriesBetween(
126       base::Time initial_time,
127       base::Time end_time,
128       Int64CompletionOnceCallback callback) override;
129   std::unique_ptr<Iterator> CreateIterator() override;
130   void GetStats(base::StringPairs* stats) override;
131   void OnExternalCacheHit(const std::string& key) override;
132   uint8_t GetEntryInMemoryData(const std::string& key) override;
133   void SetEntryInMemoryData(const std::string& key, uint8_t data) override;
134 
prioritized_task_runner()135   net::PrioritizedTaskRunner* prioritized_task_runner() const {
136     return prioritized_task_runner_.get();
137   }
138 
139   static constexpr base::TaskTraits kWorkerPoolTaskTraits = {
140       base::MayBlock(), base::WithBaseSyncPrimitives(),
141       base::TaskPriority::USER_BLOCKING,
142       base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN};
143 
144 #if BUILDFLAG(IS_ANDROID)
145   // Note: null callback is OK, and will make the cache use a
146   // base::android::ApplicationStatusListener. Callback returning nullptr
147   // means to not use an app status listener at all.
set_app_status_listener_getter(ApplicationStatusListenerGetter app_status_listener_getter)148   void set_app_status_listener_getter(
149       ApplicationStatusListenerGetter app_status_listener_getter) {
150     app_status_listener_getter_ = std::move(app_status_listener_getter);
151   }
152 #endif
153 
154  private:
155   class SimpleIterator;
156   friend class SimpleIterator;
157 
158   using EntryMap = std::unordered_map<uint64_t, SimpleEntryImpl*>;
159 
160   class ActiveEntryProxy;
161   friend class ActiveEntryProxy;
162 
163   // Return value of InitCacheStructureOnDisk().
164   struct DiskStatResult {
165     base::Time cache_dir_mtime;
166     uint64_t max_size;
167     bool detected_magic_number_mismatch;
168     int net_error;
169   };
170 
171   enum class PostOperationQueue { kNone, kPostDoom, kPostOpenByHash };
172 
173   void InitializeIndex(CompletionOnceCallback callback,
174                        const DiskStatResult& result);
175 
176   // Dooms all entries previously accessed between |initial_time| and
177   // |end_time|. Invoked when the index is ready.
178   void IndexReadyForDoom(base::Time initial_time,
179                          base::Time end_time,
180                          CompletionOnceCallback callback,
181                          int result);
182 
183   // Calculates the size of the entire cache. Invoked when the index is ready.
184   void IndexReadyForSizeCalculation(Int64CompletionOnceCallback callback,
185                                     int result);
186 
187   // Calculates the size all cache entries between |initial_time| and
188   // |end_time|. Invoked when the index is ready.
189   void IndexReadyForSizeBetweenCalculation(base::Time initial_time,
190                                            base::Time end_time,
191                                            Int64CompletionOnceCallback callback,
192                                            int result);
193 
194   // Try to create the directory if it doesn't exist. This must run on the
195   // sequence on which SimpleIndexFile is running disk I/O.
196   static DiskStatResult InitCacheStructureOnDisk(
197       std::unique_ptr<BackendFileOperations> file_operations,
198       const base::FilePath& path,
199       uint64_t suggested_max_size,
200       net::CacheType cache_type);
201 
202   // Looks at current state of `post_doom_waiting_`,
203   // `post_open_by_hash_waiting_` and `active_entries_` relevant to
204   // `entry_hash`, and, as appropriate, either returns a valid entry matching
205   // `entry_hash` and `key`, or returns nullptr and sets `post_operation` to
206   // point to a vector of closures which will be invoked when it's an
207   // appropriate time to try again. The caller is expected to append its retry
208   // closure to that vector. `post_operation_queue` will be set exactly when
209   // `post_operation` is.
210   scoped_refptr<SimpleEntryImpl> CreateOrFindActiveOrDoomedEntry(
211       uint64_t entry_hash,
212       const std::string& key,
213       net::RequestPriority request_priority,
214       std::vector<base::OnceClosure>*& post_operation,
215       PostOperationQueue& post_operation_queue);
216 
217   // If post-doom and settings indicates that optimistically succeeding a create
218   // due to being immediately after a doom is possible, sets up an entry for
219   // that, and returns a non-null pointer. (CreateEntry still needs to be called
220   // to actually do the creation operation). Otherwise returns nullptr.
221   //
222   // Pre-condition: |post_doom| is non-null.
223   scoped_refptr<SimpleEntryImpl> MaybeOptimisticCreateForPostDoom(
224       uint64_t entry_hash,
225       const std::string& key,
226       net::RequestPriority request_priority,
227       std::vector<base::OnceClosure>* post_doom);
228 
229   // Given a hash, will try to open the corresponding Entry. If we have an Entry
230   // corresponding to |hash| in the map of active entries, opens it. Otherwise,
231   // a new empty Entry will be created, opened and filled with information from
232   // the disk.
233   EntryResult OpenEntryFromHash(uint64_t entry_hash,
234                                 EntryResultCallback callback);
235 
236   // Doom the entry corresponding to |entry_hash|, if it's active or currently
237   // pending doom. This function does not block if there is an active entry,
238   // which is very important to prevent races in DoomEntries() above.
239   net::Error DoomEntryFromHash(uint64_t entry_hash,
240                                CompletionOnceCallback callback);
241 
242   // Called when we tried to open an entry with hash alone. When a blank entry
243   // has been created and filled in with information from the disk - based on a
244   // hash alone - this resumes operations that were waiting on the key
245   // information to have been loaded.
246   void OnEntryOpenedFromHash(uint64_t hash,
247                              EntryResultCallback callback,
248                              EntryResult result);
249 
250   // A callback thunk used by DoomEntries to clear the |entries_pending_doom_|
251   // after a mass doom.
252   void DoomEntriesComplete(std::unique_ptr<std::vector<uint64_t>> entry_hashes,
253                            CompletionOnceCallback callback,
254                            int result);
255 
256   // Calculates and returns a new entry's worker pool priority.
257   uint32_t GetNewEntryPriority(net::RequestPriority request_priority);
258 
259   scoped_refptr<BackendFileOperationsFactory> file_operations_factory_;
260 
261   // We want this destroyed after every other field.
262   scoped_refptr<BackendCleanupTracker> cleanup_tracker_;
263 
264   const raw_ptr<SimpleFileTracker> file_tracker_;
265 
266   const base::FilePath path_;
267   std::unique_ptr<SimpleIndex> index_;
268 
269   // This is used for all the entry I/O.
270   scoped_refptr<net::PrioritizedTaskRunner> prioritized_task_runner_;
271 
272   int64_t orig_max_size_;
273   const SimpleEntryImpl::OperationsMode entry_operations_mode_;
274 
275   EntryMap active_entries_;
276 
277   // The set of all entries which are currently being doomed. To avoid races,
278   // these entries cannot have Doom/Create/Open operations run until the doom
279   // is complete. They store a vector of base::OnceClosure's of deferred
280   // operations to be run at the completion of the Doom.
281   scoped_refptr<SimplePostOperationWaiterTable> post_doom_waiting_;
282 
283   // Set of entries which are being opened by hash. We don't have their key,
284   // so can't check for collisions on Doom/Open/Create ops until that is
285   // complete.  This stores a vector of base::OnceClosure's of deferred
286   // operations to be run at the completion of the Doom.
287   scoped_refptr<SimplePostOperationWaiterTable> post_open_by_hash_waiting_;
288 
289   const raw_ptr<net::NetLog> net_log_;
290 
291   uint32_t entry_count_ = 0;
292 
293 #if BUILDFLAG(IS_ANDROID)
294   ApplicationStatusListenerGetter app_status_listener_getter_;
295 #endif
296 };
297 
298 }  // namespace disk_cache
299 
300 #endif  // NET_DISK_CACHE_SIMPLE_SIMPLE_BACKEND_IMPL_H_
301