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