• 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_doom_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 OnDoomComplete.
96   scoped_refptr<SimplePostDoomWaiterTable> OnDoomStart(uint64_t entry_hash);
97 
98   // SimpleIndexDelegate:
99   void DoomEntries(std::vector<uint64_t>* entry_hashes,
100                    CompletionOnceCallback callback) override;
101 
102   // Backend:
103   int32_t GetEntryCount() const override;
104   EntryResult OpenEntry(const std::string& key,
105                         net::RequestPriority request_priority,
106                         EntryResultCallback callback) override;
107   EntryResult CreateEntry(const std::string& key,
108                           net::RequestPriority request_priority,
109                           EntryResultCallback callback) override;
110   EntryResult OpenOrCreateEntry(const std::string& key,
111                                 net::RequestPriority priority,
112                                 EntryResultCallback callback) override;
113   net::Error DoomEntry(const std::string& key,
114                        net::RequestPriority priority,
115                        CompletionOnceCallback callback) override;
116   net::Error DoomAllEntries(CompletionOnceCallback callback) override;
117   net::Error DoomEntriesBetween(base::Time initial_time,
118                                 base::Time end_time,
119                                 CompletionOnceCallback callback) override;
120   net::Error DoomEntriesSince(base::Time initial_time,
121                               CompletionOnceCallback callback) override;
122   int64_t CalculateSizeOfAllEntries(
123       Int64CompletionOnceCallback callback) override;
124   int64_t CalculateSizeOfEntriesBetween(
125       base::Time initial_time,
126       base::Time end_time,
127       Int64CompletionOnceCallback callback) override;
128   std::unique_ptr<Iterator> CreateIterator() override;
129   void GetStats(base::StringPairs* stats) override;
130   void OnExternalCacheHit(const std::string& key) override;
131   uint8_t GetEntryInMemoryData(const std::string& key) override;
132   void SetEntryInMemoryData(const std::string& key, uint8_t data) override;
133 
prioritized_task_runner()134   net::PrioritizedTaskRunner* prioritized_task_runner() const {
135     return prioritized_task_runner_.get();
136   }
137 
138   static constexpr base::TaskTraits kWorkerPoolTaskTraits = {
139       base::MayBlock(), base::WithBaseSyncPrimitives(),
140       base::TaskPriority::USER_BLOCKING,
141       base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN};
142 
143 #if BUILDFLAG(IS_ANDROID)
set_app_status_listener(base::android::ApplicationStatusListener * app_status_listener)144   void set_app_status_listener(
145       base::android::ApplicationStatusListener* app_status_listener) {
146     app_status_listener_ = app_status_listener;
147   }
148 #endif
149 
150  private:
151   class SimpleIterator;
152   friend class SimpleIterator;
153 
154   using EntryMap = std::unordered_map<uint64_t, SimpleEntryImpl*>;
155 
156   class ActiveEntryProxy;
157   friend class ActiveEntryProxy;
158 
159   // Return value of InitCacheStructureOnDisk().
160   struct DiskStatResult {
161     base::Time cache_dir_mtime;
162     uint64_t max_size;
163     bool detected_magic_number_mismatch;
164     int net_error;
165   };
166 
167   void InitializeIndex(CompletionOnceCallback callback,
168                        const DiskStatResult& result);
169 
170   // Dooms all entries previously accessed between |initial_time| and
171   // |end_time|. Invoked when the index is ready.
172   void IndexReadyForDoom(base::Time initial_time,
173                          base::Time end_time,
174                          CompletionOnceCallback callback,
175                          int result);
176 
177   // Calculates the size of the entire cache. Invoked when the index is ready.
178   void IndexReadyForSizeCalculation(Int64CompletionOnceCallback callback,
179                                     int result);
180 
181   // Calculates the size all cache entries between |initial_time| and
182   // |end_time|. Invoked when the index is ready.
183   void IndexReadyForSizeBetweenCalculation(base::Time initial_time,
184                                            base::Time end_time,
185                                            Int64CompletionOnceCallback callback,
186                                            int result);
187 
188   // Try to create the directory if it doesn't exist. This must run on the
189   // sequence on which SimpleIndexFile is running disk I/O.
190   static DiskStatResult InitCacheStructureOnDisk(
191       std::unique_ptr<BackendFileOperations> file_operations,
192       const base::FilePath& path,
193       uint64_t suggested_max_size,
194       net::CacheType cache_type);
195 
196   // Looks at current state of |entries_pending_doom_| and |active_entries_|
197   // relevant to |entry_hash|, and, as appropriate, either returns a valid entry
198   // matching |entry_hash| and |key|, or returns nullptr and sets |*post_doom|
199   // to point to a vector of closures which will be invoked when it's an
200   // appropriate time to try again.  The caller is expected to append its retry
201   // closure to that vector.
202   scoped_refptr<SimpleEntryImpl> CreateOrFindActiveOrDoomedEntry(
203       uint64_t entry_hash,
204       const std::string& key,
205       net::RequestPriority request_priority,
206       std::vector<SimplePostDoomWaiter>** post_doom);
207 
208   // If post-doom and settings indicates that optimistically succeeding a create
209   // due to being immediately after a doom is possible, sets up an entry for
210   // that, and returns a non-null pointer. (CreateEntry still needs to be called
211   // to actually do the creation operation). Otherwise returns nullptr.
212   //
213   // Pre-condition: |post_doom| is non-null.
214   scoped_refptr<SimpleEntryImpl> MaybeOptimisticCreateForPostDoom(
215       uint64_t entry_hash,
216       const std::string& key,
217       net::RequestPriority request_priority,
218       std::vector<SimplePostDoomWaiter>* post_doom);
219 
220   // Given a hash, will try to open the corresponding Entry. If we have an Entry
221   // corresponding to |hash| in the map of active entries, opens it. Otherwise,
222   // a new empty Entry will be created, opened and filled with information from
223   // the disk.
224   EntryResult OpenEntryFromHash(uint64_t entry_hash,
225                                 EntryResultCallback callback);
226 
227   // Doom the entry corresponding to |entry_hash|, if it's active or currently
228   // pending doom. This function does not block if there is an active entry,
229   // which is very important to prevent races in DoomEntries() above.
230   net::Error DoomEntryFromHash(uint64_t entry_hash,
231                                CompletionOnceCallback callback);
232 
233   // Called when we tried to open an entry with hash alone. When a blank entry
234   // has been created and filled in with information from the disk - based on a
235   // hash alone - this checks that a duplicate active entry was not created
236   // using a key in the meantime.
237   void OnEntryOpenedFromHash(uint64_t hash,
238                              const scoped_refptr<SimpleEntryImpl>& simple_entry,
239                              EntryResultCallback callback,
240                              EntryResult result);
241 
242   // Called when we tried to open an entry from key. When the entry has been
243   // opened, a check for key mismatch is performed.
244   void OnEntryOpenedFromKey(const std::string key,
245                             Entry** entry,
246                             const scoped_refptr<SimpleEntryImpl>& simple_entry,
247                             CompletionOnceCallback callback,
248                             int error_code);
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. The base::OnceClosure |SimplePostDoomWaiter::run_post_doom|
280   // field is used to store deferred operations to be run at the completion of
281   // the Doom.
282   scoped_refptr<SimplePostDoomWaiterTable> post_doom_waiting_;
283 
284   const raw_ptr<net::NetLog> net_log_;
285 
286   uint32_t entry_count_ = 0;
287 
288 #if BUILDFLAG(IS_ANDROID)
289   raw_ptr<base::android::ApplicationStatusListener> app_status_listener_ =
290       nullptr;
291 #endif
292 };
293 
294 }  // namespace disk_cache
295 
296 #endif  // NET_DISK_CACHE_SIMPLE_SIMPLE_BACKEND_IMPL_H_
297