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 
5 #ifndef CONTENT_BROWSER_BROWSING_DATA_STORAGE_PARTITION_CODE_CACHE_DATA_REMOVER_H_
6 #define CONTENT_BROWSER_BROWSING_DATA_STORAGE_PARTITION_CODE_CACHE_DATA_REMOVER_H_
7 
8 #include <stdint.h>
9 
10 #include "base/callback.h"
11 #include "base/macros.h"
12 #include "base/sequenced_task_runner_helpers.h"
13 #include "net/base/completion_once_callback.h"
14 #include "url/gurl.h"
15 
16 namespace disk_cache {
17 class Backend;
18 }
19 
20 namespace content {
21 
22 class StoragePartition;
23 class GeneratedCodeCacheContext;
24 
25 // Helper to remove code cache data from a StoragePartition. This class is
26 // created on and acts on the UI thread.
27 class StoragePartitionCodeCacheDataRemover {
28  public:
29   // Creates a StoragePartitionCodeCacheDataRemover that deletes all cache
30   // entries.
31   static StoragePartitionCodeCacheDataRemover* Create(
32       content::StoragePartition* storage_partition,
33       base::RepeatingCallback<bool(const GURL&)> url_predicate,
34       base::Time begin_time,
35       base::Time end_time);
36 
37   // Calls |done_callback| on UI thread upon completion and also destroys
38   // itself on UI thread.
39   // This expects that either storage_partition with which this object was
40   // created is live till the end of operation or GeneratedCodeCacheContext
41   // is destroyed when the storage_partition is destroyed. This ensures the
42   // code cache and hence the backend is destroyed. If this is not guaranteed
43   // there could be a callback accessing the destroyed objects.
44   void Remove(base::OnceClosure done_callback);
45 
46  private:
47   // StoragePartitionCodeCacheDataRemover deletes itself (using DeleteHelper)
48   // and is not supposed to be deleted by other objects so make destructor
49   // private and DeleteHelper a friend.
50   friend class base::DeleteHelper<StoragePartitionCodeCacheDataRemover>;
51 
52   explicit StoragePartitionCodeCacheDataRemover(
53       GeneratedCodeCacheContext* generated_code_cache_context,
54       base::RepeatingCallback<bool(const GURL&)> url_predicate,
55       base::Time begin_time,
56       base::Time end_time);
57 
58   ~StoragePartitionCodeCacheDataRemover();
59 
60   void ClearJSCodeCache();
61   void ClearWASMCodeCache(int rv);
62   void ClearCache(net::CompletionOnceCallback callback,
63                   disk_cache::Backend* backend);
64   void DoneClearCodeCache(int rv);
65 
66   const scoped_refptr<GeneratedCodeCacheContext> generated_code_cache_context_;
67 
68   base::OnceClosure done_callback_;
69   base::Time begin_time_;
70   base::Time end_time_;
71   base::RepeatingCallback<bool(const GURL&)> url_predicate_;
72 
73   DISALLOW_COPY_AND_ASSIGN(StoragePartitionCodeCacheDataRemover);
74 };
75 
76 }  // namespace content
77 
78 #endif  // CONTENT_BROWSER_BROWSING_DATA_STORAGE_PARTITION_CODE_CACHE_DATA_REMOVER_H_
79