backend_cleanup_tracker.h 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // Copyright (c) 2017 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_BACKEND_CLEANUP_TRACKER_H_
  5. #define NET_DISK_CACHE_BACKEND_CLEANUP_TRACKER_H_
  6. #include <utility>
  7. #include <vector>
  8. #include "base/callback.h"
  9. #include "base/files/file_path.h"
  10. #include "base/memory/ref_counted.h"
  11. #include "base/sequence_checker.h"
  12. #include "net/base/net_export.h"
  13. namespace base {
  14. class SequencedTaskRunner;
  15. } // namespace base
  16. namespace disk_cache {
  17. // Internal helper used to sequence cleanup and reuse of cache directories.
  18. // One of these is created before each backend, and is kept alive till both
  19. // the backend is destroyed and all of its work is done by its refcount,
  20. // which keeps track of outstanding work. That refcount is expected to only be
  21. // updated from the I/O thread or its equivalent.
  22. class NET_EXPORT_PRIVATE BackendCleanupTracker
  23. : public base::RefCounted<BackendCleanupTracker> {
  24. public:
  25. // Either returns a fresh cleanup tracker for |path| if none exists, or
  26. // will eventually post |retry_closure| to the calling thread,
  27. // and return null.
  28. static scoped_refptr<BackendCleanupTracker> TryCreate(
  29. const base::FilePath& path,
  30. base::OnceClosure retry_closure);
  31. BackendCleanupTracker(const BackendCleanupTracker&) = delete;
  32. BackendCleanupTracker& operator=(const BackendCleanupTracker&) = delete;
  33. // Register a callback to be posted after all the work of associated
  34. // context is complete (which will result in destruction of this context).
  35. // Should only be called by owner, on its I/O-thread-like execution context,
  36. // and will in turn eventually post |cb| there.
  37. void AddPostCleanupCallback(base::OnceClosure cb);
  38. private:
  39. friend class base::RefCounted<BackendCleanupTracker>;
  40. explicit BackendCleanupTracker(const base::FilePath& path);
  41. ~BackendCleanupTracker();
  42. void AddPostCleanupCallbackImpl(base::OnceClosure cb);
  43. base::FilePath path_;
  44. // Since it's possible that a different thread may want to create a
  45. // cache for a reused path, we keep track of runners contexts of
  46. // post-cleanup callbacks.
  47. std::vector<
  48. std::pair<scoped_refptr<base::SequencedTaskRunner>, base::OnceClosure>>
  49. post_cleanup_cbs_;
  50. // We expect only TryMakeContext to be multithreaded, everything
  51. // else should be sequenced.
  52. SEQUENCE_CHECKER(seq_checker_);
  53. };
  54. } // namespace disk_cache
  55. #endif // NET_DISK_CACHE_BACKEND_CLEANUP_TRACKER_H_