OLD | NEW |
| (Empty) |
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 "components/browsing_data/conditional_cache_deletion_helper.h" | |
6 | |
7 #include "base/callback.h" | |
8 #include "base/location.h" | |
9 #include "base/single_thread_task_runner.h" | |
10 #include "base/threading/thread_task_runner_handle.h" | |
11 #include "content/public/browser/browser_thread.h" | |
12 | |
13 namespace { | |
14 | |
15 bool EntryPredicateFromURLsAndTime( | |
16 const base::Callback<bool(const GURL&)>& url_predicate, | |
17 const base::Time& begin_time, | |
18 const base::Time& end_time, | |
19 const disk_cache::Entry* entry) { | |
20 return (entry->GetLastModified() >= begin_time && | |
21 entry->GetLastModified() < end_time && | |
22 url_predicate.Run(GURL(entry->GetKey()))); | |
23 } | |
24 | |
25 } // namespace | |
26 | |
27 namespace browsing_data { | |
28 | |
29 ConditionalCacheDeletionHelper::ConditionalCacheDeletionHelper( | |
30 disk_cache::Backend* cache, | |
31 const base::Callback<bool(const disk_cache::Entry*)>& condition) | |
32 : cache_(cache), | |
33 condition_(condition), | |
34 current_entry_(nullptr), | |
35 previous_entry_(nullptr) { | |
36 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | |
37 } | |
38 | |
39 // static | |
40 base::Callback<bool(const disk_cache::Entry*)> | |
41 ConditionalCacheDeletionHelper::CreateURLAndTimeCondition( | |
42 const base::Callback<bool(const GURL&)>& url_predicate, | |
43 const base::Time& begin_time, | |
44 const base::Time& end_time) { | |
45 return base::Bind( | |
46 &EntryPredicateFromURLsAndTime, | |
47 url_predicate, | |
48 begin_time.is_null() ? base::Time() : begin_time, | |
49 end_time.is_null() ? base::Time::Max() : end_time); | |
50 } | |
51 | |
52 int ConditionalCacheDeletionHelper::DeleteAndDestroySelfWhenFinished( | |
53 const net::CompletionCallback& completion_callback) { | |
54 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | |
55 | |
56 completion_callback_ = completion_callback; | |
57 iterator_ = cache_->CreateIterator(); | |
58 | |
59 IterateOverEntries(net::OK); | |
60 return net::ERR_IO_PENDING; | |
61 } | |
62 | |
63 ConditionalCacheDeletionHelper::~ConditionalCacheDeletionHelper() { | |
64 } | |
65 | |
66 void ConditionalCacheDeletionHelper::IterateOverEntries(int error) { | |
67 while (error != net::ERR_IO_PENDING) { | |
68 // If the entry obtained in the previous iteration matches the condition, | |
69 // mark it for deletion. The iterator is already one step forward, so it | |
70 // won't be invalidated. Always close the previous entry so it does not | |
71 // leak. | |
72 if (previous_entry_) { | |
73 if (condition_.Run(previous_entry_)) | |
74 previous_entry_->Doom(); | |
75 previous_entry_->Close(); | |
76 } | |
77 | |
78 if (error == net::ERR_FAILED) { | |
79 // The iteration finished successfuly or we can no longer iterate | |
80 // (e.g. the cache was destroyed). We cannot distinguish between the two, | |
81 // but we know that there is nothing more that we can do, so we return OK. | |
82 base::MessageLoop::current()->task_runner()->PostTask( | |
83 FROM_HERE, base::Bind(completion_callback_, net::OK)); | |
84 base::ThreadTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE, this); | |
85 return; | |
86 } | |
87 | |
88 previous_entry_ = current_entry_; | |
89 error = iterator_->OpenNextEntry( | |
90 ¤t_entry_, | |
91 base::Bind(&ConditionalCacheDeletionHelper::IterateOverEntries, | |
92 base::Unretained(this))); | |
93 } | |
94 } | |
95 | |
96 } // namespace browsing_data | |
OLD | NEW |