OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/browsing_data/content/conditional_cache_deletion_helper.h" | 5 #include "content/browser/browsing_data/conditional_cache_deletion_helper.h" |
6 | 6 |
7 #include "base/callback.h" | 7 #include "base/callback.h" |
8 #include "base/location.h" | 8 #include "base/location.h" |
9 #include "base/single_thread_task_runner.h" | 9 #include "base/single_thread_task_runner.h" |
10 #include "base/threading/thread_task_runner_handle.h" | 10 #include "base/threading/thread_task_runner_handle.h" |
11 #include "content/public/browser/browser_thread.h" | 11 #include "content/public/browser/browser_thread.h" |
12 | 12 |
13 namespace { | 13 namespace { |
14 | 14 |
15 bool EntryPredicateFromURLsAndTime( | 15 bool EntryPredicateFromURLsAndTime( |
16 const base::Callback<bool(const GURL&)>& url_predicate, | 16 const base::Callback<bool(const GURL&)>& url_predicate, |
17 const base::Time& begin_time, | 17 const base::Time& begin_time, |
18 const base::Time& end_time, | 18 const base::Time& end_time, |
19 const disk_cache::Entry* entry) { | 19 const disk_cache::Entry* entry) { |
20 return (entry->GetLastUsed() >= begin_time && | 20 return (entry->GetLastUsed() >= begin_time && |
21 entry->GetLastUsed() < end_time && | 21 entry->GetLastUsed() < end_time && |
22 url_predicate.Run(GURL(entry->GetKey()))); | 22 url_predicate.Run(GURL(entry->GetKey()))); |
23 } | 23 } |
24 | 24 |
25 } // namespace | 25 } // namespace |
26 | 26 |
27 namespace browsing_data { | 27 namespace content { |
28 | 28 |
29 ConditionalCacheDeletionHelper::ConditionalCacheDeletionHelper( | 29 ConditionalCacheDeletionHelper::ConditionalCacheDeletionHelper( |
30 disk_cache::Backend* cache, | 30 disk_cache::Backend* cache, |
31 const base::Callback<bool(const disk_cache::Entry*)>& condition) | 31 const base::Callback<bool(const disk_cache::Entry*)>& condition) |
32 : cache_(cache), | 32 : cache_(cache), |
33 condition_(condition), | 33 condition_(condition), |
34 current_entry_(nullptr), | 34 current_entry_(nullptr), |
35 previous_entry_(nullptr) { | 35 previous_entry_(nullptr) { |
36 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 36 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
37 } | 37 } |
38 | 38 |
39 // static | 39 // static |
40 base::Callback<bool(const disk_cache::Entry*)> | 40 base::Callback<bool(const disk_cache::Entry*)> |
41 ConditionalCacheDeletionHelper::CreateURLAndTimeCondition( | 41 ConditionalCacheDeletionHelper::CreateURLAndTimeCondition( |
42 const base::Callback<bool(const GURL&)>& url_predicate, | 42 const base::Callback<bool(const GURL&)>& url_predicate, |
43 const base::Time& begin_time, | 43 const base::Time& begin_time, |
44 const base::Time& end_time) { | 44 const base::Time& end_time) { |
45 return base::Bind( | 45 return base::Bind(&EntryPredicateFromURLsAndTime, url_predicate, |
46 &EntryPredicateFromURLsAndTime, | 46 begin_time.is_null() ? base::Time() : begin_time, |
47 url_predicate, | 47 end_time.is_null() ? base::Time::Max() : end_time); |
48 begin_time.is_null() ? base::Time() : begin_time, | |
49 end_time.is_null() ? base::Time::Max() : end_time); | |
50 } | 48 } |
51 | 49 |
52 int ConditionalCacheDeletionHelper::DeleteAndDestroySelfWhenFinished( | 50 int ConditionalCacheDeletionHelper::DeleteAndDestroySelfWhenFinished( |
53 const net::CompletionCallback& completion_callback) { | 51 const net::CompletionCallback& completion_callback) { |
54 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 52 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
55 | 53 |
56 completion_callback_ = completion_callback; | 54 completion_callback_ = completion_callback; |
57 iterator_ = cache_->CreateIterator(); | 55 iterator_ = cache_->CreateIterator(); |
58 | 56 |
59 IterateOverEntries(net::OK); | 57 IterateOverEntries(net::OK); |
60 return net::ERR_IO_PENDING; | 58 return net::ERR_IO_PENDING; |
61 } | 59 } |
62 | 60 |
63 ConditionalCacheDeletionHelper::~ConditionalCacheDeletionHelper() { | 61 ConditionalCacheDeletionHelper::~ConditionalCacheDeletionHelper() {} |
64 } | |
65 | 62 |
66 void ConditionalCacheDeletionHelper::IterateOverEntries(int error) { | 63 void ConditionalCacheDeletionHelper::IterateOverEntries(int error) { |
67 while (error != net::ERR_IO_PENDING) { | 64 while (error != net::ERR_IO_PENDING) { |
68 // If the entry obtained in the previous iteration matches the condition, | 65 // 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 | 66 // 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 | 67 // won't be invalidated. Always close the previous entry so it does not |
71 // leak. | 68 // leak. |
72 if (previous_entry_) { | 69 if (previous_entry_) { |
73 if (condition_.Run(previous_entry_)) | 70 if (condition_.Run(previous_entry_)) |
74 previous_entry_->Doom(); | 71 previous_entry_->Doom(); |
(...skipping 11 matching lines...) Expand all Loading... |
86 } | 83 } |
87 | 84 |
88 previous_entry_ = current_entry_; | 85 previous_entry_ = current_entry_; |
89 error = iterator_->OpenNextEntry( | 86 error = iterator_->OpenNextEntry( |
90 ¤t_entry_, | 87 ¤t_entry_, |
91 base::Bind(&ConditionalCacheDeletionHelper::IterateOverEntries, | 88 base::Bind(&ConditionalCacheDeletionHelper::IterateOverEntries, |
92 base::Unretained(this))); | 89 base::Unretained(this))); |
93 } | 90 } |
94 } | 91 } |
95 | 92 |
96 } // namespace browsing_data | 93 } // namespace content |
OLD | NEW |