simple_backend_impl.h 12 KB

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