1 // Copyright 2016 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 
5 #ifndef CONTENT_BROWSER_BROWSING_DATA_CONDITIONAL_CACHE_DELETION_HELPER_H_
6 #define CONTENT_BROWSER_BROWSING_DATA_CONDITIONAL_CACHE_DELETION_HELPER_H_
7 
8 #include <memory>
9 
10 #include "base/callback_forward.h"
11 #include "base/sequenced_task_runner_helpers.h"
12 #include "content/common/content_export.h"
13 #include "net/base/completion_once_callback.h"
14 #include "net/base/net_errors.h"
15 #include "net/disk_cache/disk_cache.h"
16 #include "url/gurl.h"
17 
18 namespace disk_cache {
19 class Entry;
20 }
21 
22 namespace content {
23 
24 // Helper to remove http/code cache data from a StoragePartition.
25 class CONTENT_EXPORT ConditionalCacheDeletionHelper {
26  public:
27   // Creates a helper to delete |cache| entries that match the |condition|.
28   ConditionalCacheDeletionHelper(
29       disk_cache::Backend* cache,
30       base::RepeatingCallback<bool(const disk_cache::Entry*)> condition);
31 
32   // A convenience method to create a condition matching cache entries whose
33   // last modified time is between |begin_time| (inclusively), |end_time|
34   // (exclusively) and whose URL obtained by passing the key to the
35   // |get_url_from_key| is matched by the |url_predicate|. The
36   // |get_url_from_key| method is useful when the entries are not keyed by the
37   // resource url alone. For ex: using two keys for site isolation.
38   static base::RepeatingCallback<bool(const disk_cache::Entry*)>
39   CreateURLAndTimeCondition(
40       base::RepeatingCallback<bool(const GURL&)> url_predicate,
41       base::RepeatingCallback<std::string(const std::string&)> get_url_from_key,
42       base::Time begin_time,
43       base::Time end_time);
44 
45   // Deletes the cache entries according to the specified condition. Destroys
46   // this instance of ConditionalCacheDeletionHelper when finished.
47   //
48   // The return value is a net error code. If this method returns
49   // ERR_IO_PENDING, the |completion_callback| will be invoked when the
50   // operation completes.
51   int DeleteAndDestroySelfWhenFinished(
52       net::CompletionOnceCallback completion_callback);
53 
54  private:
55   friend class base::DeleteHelper<ConditionalCacheDeletionHelper>;
56   ~ConditionalCacheDeletionHelper();
57 
58   void IterateOverEntries(disk_cache::EntryResult result);
59 
60   disk_cache::Backend* cache_;
61   const base::RepeatingCallback<bool(const disk_cache::Entry*)> condition_;
62 
63   net::CompletionOnceCallback completion_callback_;
64 
65   SEQUENCE_CHECKER(sequence_checker_);
66 
67   std::unique_ptr<disk_cache::Backend::Iterator> iterator_;
68   disk_cache::Entry* previous_entry_;
69 
70   DISALLOW_COPY_AND_ASSIGN(ConditionalCacheDeletionHelper);
71 };
72 
73 }  // namespace content
74 
75 #endif  // CONTENT_BROWSER_BROWSING_DATA_CONDITIONAL_CACHE_DELETION_HELPER_H_
76