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 #include "content/browser/browsing_data/conditional_cache_deletion_helper.h"
6
7 #include "base/bind.h"
8 #include "base/callback.h"
9 #include "base/location.h"
10 #include "base/sequence_checker.h"
11 #include "base/single_thread_task_runner.h"
12 #include "base/threading/thread_task_runner_handle.h"
13 #include "content/public/browser/browser_thread.h"
14
15 namespace {
16
EntryPredicateFromURLsAndTime(const base::RepeatingCallback<bool (const GURL &)> & url_predicate,const base::RepeatingCallback<std::string (const std::string &)> & get_url_from_key,base::Time begin_time,base::Time end_time,const disk_cache::Entry * entry)17 bool EntryPredicateFromURLsAndTime(
18 const base::RepeatingCallback<bool(const GURL&)>& url_predicate,
19 const base::RepeatingCallback<std::string(const std::string&)>&
20 get_url_from_key,
21 base::Time begin_time,
22 base::Time end_time,
23 const disk_cache::Entry* entry) {
24 std::string url = entry->GetKey();
25 if (!get_url_from_key.is_null())
26 url = get_url_from_key.Run(url);
27 return (entry->GetLastUsed() >= begin_time &&
28 entry->GetLastUsed() < end_time && !url.empty() &&
29 url_predicate.Run(GURL(url)));
30 }
31
32 } // namespace
33
34 namespace content {
35
ConditionalCacheDeletionHelper(disk_cache::Backend * cache,base::RepeatingCallback<bool (const disk_cache::Entry *)> condition)36 ConditionalCacheDeletionHelper::ConditionalCacheDeletionHelper(
37 disk_cache::Backend* cache,
38 base::RepeatingCallback<bool(const disk_cache::Entry*)> condition)
39 : cache_(cache),
40 condition_(std::move(condition)),
41 previous_entry_(nullptr) {
42 }
43
44 // static
45 base::RepeatingCallback<bool(const disk_cache::Entry*)>
CreateURLAndTimeCondition(base::RepeatingCallback<bool (const GURL &)> url_predicate,base::RepeatingCallback<std::string (const std::string &)> get_url_from_key,base::Time begin_time,base::Time end_time)46 ConditionalCacheDeletionHelper::CreateURLAndTimeCondition(
47 base::RepeatingCallback<bool(const GURL&)> url_predicate,
48 base::RepeatingCallback<std::string(const std::string&)> get_url_from_key,
49 base::Time begin_time,
50 base::Time end_time) {
51 return base::BindRepeating(&EntryPredicateFromURLsAndTime,
52 std::move(url_predicate),
53 std::move(get_url_from_key),
54 begin_time.is_null() ? base::Time() : begin_time,
55 end_time.is_null() ? base::Time::Max() : end_time);
56 }
57
DeleteAndDestroySelfWhenFinished(net::CompletionOnceCallback completion_callback)58 int ConditionalCacheDeletionHelper::DeleteAndDestroySelfWhenFinished(
59 net::CompletionOnceCallback completion_callback) {
60 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
61
62 completion_callback_ = std::move(completion_callback);
63 iterator_ = cache_->CreateIterator();
64
65 // Any status other than OK (since no entry), IO_PENDING, or FAILED would
66 // work here.
67 IterateOverEntries(
68 disk_cache::EntryResult::MakeError(net::ERR_CACHE_OPEN_FAILURE));
69
70 // DeleteAndDestroySelfWhenFinished() itself is always async since
71 // |completion_callback| is always posted and never run directly.
72 return net::ERR_IO_PENDING;
73 }
74
~ConditionalCacheDeletionHelper()75 ConditionalCacheDeletionHelper::~ConditionalCacheDeletionHelper() {}
76
IterateOverEntries(disk_cache::EntryResult result)77 void ConditionalCacheDeletionHelper::IterateOverEntries(
78 disk_cache::EntryResult result) {
79 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
80 while (result.net_error() != net::ERR_IO_PENDING) {
81 // If the entry obtained in the previous iteration matches the condition,
82 // mark it for deletion. The iterator is already one step forward, so it
83 // won't be invalidated. Always close the previous entry so it does not
84 // leak.
85 if (previous_entry_) {
86 if (condition_.Run(previous_entry_))
87 previous_entry_->Doom();
88 previous_entry_->Close();
89 }
90
91 if (result.net_error() == net::ERR_FAILED) {
92 // The iteration finished successfully or we can no longer iterate
93 // (e.g. the cache was destroyed). We cannot distinguish between the two,
94 // but we know that there is nothing more that we can do, so we return OK.
95 DCHECK(completion_callback_);
96 base::ThreadTaskRunnerHandle::Get()->PostTask(
97 FROM_HERE, base::BindOnce(std::move(completion_callback_), net::OK));
98 base::ThreadTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE, this);
99 return;
100 }
101
102 previous_entry_ = result.ReleaseEntry();
103 result = iterator_->OpenNextEntry(
104 base::BindOnce(&ConditionalCacheDeletionHelper::IterateOverEntries,
105 base::Unretained(this)));
106 }
107 }
108
109 } // namespace content
110