conditional_cache_deletion_helper.h 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // Copyright 2018 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 SERVICES_NETWORK_CONDITIONAL_CACHE_DELETION_HELPER_H_
  5. #define SERVICES_NETWORK_CONDITIONAL_CACHE_DELETION_HELPER_H_
  6. #include <memory>
  7. #include "base/callback_forward.h"
  8. #include "base/memory/raw_ptr.h"
  9. #include "base/memory/weak_ptr.h"
  10. #include "base/task/sequenced_task_runner_helpers.h"
  11. #include "net/base/net_errors.h"
  12. #include "net/disk_cache/disk_cache.h"
  13. #include "url/gurl.h"
  14. namespace disk_cache {
  15. class Entry;
  16. }
  17. namespace network {
  18. // Helper to remove HTTP cache data.
  19. class ConditionalCacheDeletionHelper {
  20. public:
  21. // Creates a helper to delete |cache| entries whose last modified time is
  22. // between |begin_time| (inclusively), |end_time| (exclusively) and whose URL
  23. // is matched by the |url_matcher|. Note that |begin_time| and |end_time| can
  24. // be null to indicate unbounded time interval in their respective direction.
  25. // Starts the deletion and calls |completion_callback| when done.
  26. static std::unique_ptr<ConditionalCacheDeletionHelper> CreateAndStart(
  27. disk_cache::Backend* cache,
  28. const base::RepeatingCallback<bool(const GURL&)>& url_matcher,
  29. const base::Time& begin_time,
  30. const base::Time& end_time,
  31. base::OnceClosure completion_callback);
  32. ConditionalCacheDeletionHelper(const ConditionalCacheDeletionHelper&) =
  33. delete;
  34. ConditionalCacheDeletionHelper& operator=(
  35. const ConditionalCacheDeletionHelper&) = delete;
  36. ~ConditionalCacheDeletionHelper();
  37. private:
  38. ConditionalCacheDeletionHelper(
  39. const base::RepeatingCallback<bool(const disk_cache::Entry*)>& condition,
  40. base::OnceClosure completion_callback,
  41. std::unique_ptr<disk_cache::Backend::Iterator> iterator);
  42. void IterateOverEntries(disk_cache::EntryResult result);
  43. void NotifyCompletion();
  44. const base::RepeatingCallback<bool(const disk_cache::Entry*)> condition_;
  45. base::OnceClosure completion_callback_;
  46. std::unique_ptr<disk_cache::Backend::Iterator> iterator_;
  47. raw_ptr<disk_cache::Entry> previous_entry_ = nullptr;
  48. base::WeakPtrFactory<ConditionalCacheDeletionHelper> weak_factory_{this};
  49. };
  50. } // namespace network
  51. #endif // SERVICES_NETWORK_CONDITIONAL_CACHE_DELETION_HELPER_H_