Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(201)

Side by Side Diff: components/browsing_data/content/storage_partition_http_cache_data_remover.cc

Issue 2556363003: Refactor cache counting into a separate helper class (Closed)
Patch Set: cleanup Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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/storage_partition_http_cache_data_rem over.h" 5 #include "components/browsing_data/content/storage_partition_http_cache_data_rem over.h"
6 6
7 #include "base/location.h" 7 #include "base/location.h"
8 #include "base/single_thread_task_runner.h" 8 #include "base/single_thread_task_runner.h"
9 #include "base/threading/thread_task_runner_handle.h" 9 #include "base/threading/thread_task_runner_handle.h"
10 #include "components/browsing_data/content/conditional_cache_deletion_helper.h" 10 #include "components/browsing_data/content/conditional_cache_deletion_helper.h"
(...skipping 17 matching lines...) Expand all
28 base::Time delete_begin, 28 base::Time delete_begin,
29 base::Time delete_end, 29 base::Time delete_end,
30 net::URLRequestContextGetter* main_context_getter, 30 net::URLRequestContextGetter* main_context_getter,
31 net::URLRequestContextGetter* media_context_getter) 31 net::URLRequestContextGetter* media_context_getter)
32 : url_predicate_(url_predicate), 32 : url_predicate_(url_predicate),
33 delete_begin_(delete_begin), 33 delete_begin_(delete_begin),
34 delete_end_(delete_end), 34 delete_end_(delete_end),
35 main_context_getter_(main_context_getter), 35 main_context_getter_(main_context_getter),
36 media_context_getter_(media_context_getter), 36 media_context_getter_(media_context_getter),
37 next_cache_state_(STATE_NONE), 37 next_cache_state_(STATE_NONE),
38 cache_(nullptr), 38 cache_(nullptr) {}
39 calculation_result_(0) {
40 }
41 39
42 StoragePartitionHttpCacheDataRemover::~StoragePartitionHttpCacheDataRemover() { 40 StoragePartitionHttpCacheDataRemover::~StoragePartitionHttpCacheDataRemover() {
43 } 41 }
44 42
45 // static. 43 // static.
46 StoragePartitionHttpCacheDataRemover* 44 StoragePartitionHttpCacheDataRemover*
47 StoragePartitionHttpCacheDataRemover::CreateForRange( 45 StoragePartitionHttpCacheDataRemover::CreateForRange(
48 content::StoragePartition* storage_partition, 46 content::StoragePartition* storage_partition,
49 base::Time delete_begin, 47 base::Time delete_begin,
50 base::Time delete_end) { 48 base::Time delete_end) {
(...skipping 23 matching lines...) Expand all
74 DCHECK(!done_callback.is_null()); 72 DCHECK(!done_callback.is_null());
75 done_callback_ = done_callback; 73 done_callback_ = done_callback;
76 74
77 BrowserThread::PostTask( 75 BrowserThread::PostTask(
78 BrowserThread::IO, FROM_HERE, 76 BrowserThread::IO, FROM_HERE,
79 base::Bind( 77 base::Bind(
80 &StoragePartitionHttpCacheDataRemover::ClearHttpCacheOnIOThread, 78 &StoragePartitionHttpCacheDataRemover::ClearHttpCacheOnIOThread,
81 base::Unretained(this))); 79 base::Unretained(this)));
82 } 80 }
83 81
84 void StoragePartitionHttpCacheDataRemover::Count(
85 const net::Int64CompletionCallback& result_callback) {
86 DCHECK_CURRENTLY_ON(BrowserThread::UI);
87 DCHECK(!result_callback.is_null());
88 result_callback_ = result_callback;
89 calculation_result_ = 0;
90
91 BrowserThread::PostTask(
92 BrowserThread::IO, FROM_HERE,
93 base::Bind(
94 &StoragePartitionHttpCacheDataRemover::CountHttpCacheOnIOThread,
95 base::Unretained(this)));
96 }
97
98 void StoragePartitionHttpCacheDataRemover::ClearHttpCacheOnIOThread() { 82 void StoragePartitionHttpCacheDataRemover::ClearHttpCacheOnIOThread() {
99 DCHECK_CURRENTLY_ON(BrowserThread::IO); 83 DCHECK_CURRENTLY_ON(BrowserThread::IO);
100 next_cache_state_ = STATE_NONE; 84 next_cache_state_ = STATE_NONE;
101 DCHECK_EQ(STATE_NONE, next_cache_state_); 85 DCHECK_EQ(STATE_NONE, next_cache_state_);
102 DCHECK(main_context_getter_.get()); 86 DCHECK(main_context_getter_.get());
103 DCHECK(media_context_getter_.get()); 87 DCHECK(media_context_getter_.get());
104 88
105 next_cache_state_ = STATE_CREATE_MAIN; 89 next_cache_state_ = STATE_CREATE_MAIN;
106 DoClearCache(net::OK); 90 DoClearCache(net::OK);
107 } 91 }
108 92
109 void StoragePartitionHttpCacheDataRemover::CountHttpCacheOnIOThread() {
110 DCHECK_CURRENTLY_ON(BrowserThread::IO);
111 next_cache_state_ = STATE_NONE;
112 DCHECK_EQ(STATE_NONE, next_cache_state_);
113 DCHECK(main_context_getter_.get());
114 DCHECK(media_context_getter_.get());
115
116 next_cache_state_ = STATE_CREATE_MAIN;
117 DoCountCache(net::OK);
118 }
119
120 void StoragePartitionHttpCacheDataRemover::ClearedHttpCache() { 93 void StoragePartitionHttpCacheDataRemover::ClearedHttpCache() {
121 DCHECK_CURRENTLY_ON(BrowserThread::UI); 94 DCHECK_CURRENTLY_ON(BrowserThread::UI);
122 done_callback_.Run(); 95 done_callback_.Run();
123 base::ThreadTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE, this); 96 base::ThreadTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE, this);
124 } 97 }
125 98
126 void StoragePartitionHttpCacheDataRemover::CountedHttpCache() {
127 DCHECK_CURRENTLY_ON(BrowserThread::UI);
128 result_callback_.Run(calculation_result_);
129 base::ThreadTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE, this);
130 }
131
132 // The expected state sequence is STATE_NONE --> STATE_CREATE_MAIN --> 99 // The expected state sequence is STATE_NONE --> STATE_CREATE_MAIN -->
133 // STATE_PROCESS_MAIN --> STATE_CREATE_MEDIA --> STATE_PROCESS_MEDIA --> 100 // STATE_PROCESS_MAIN --> STATE_CREATE_MEDIA --> STATE_PROCESS_MEDIA -->
134 // STATE_DONE, and any errors are ignored. 101 // STATE_DONE, and any errors are ignored.
135 void StoragePartitionHttpCacheDataRemover::DoClearCache(int rv) { 102 void StoragePartitionHttpCacheDataRemover::DoClearCache(int rv) {
136 DCHECK_NE(STATE_NONE, next_cache_state_); 103 DCHECK_NE(STATE_NONE, next_cache_state_);
137 104
138 while (rv != net::ERR_IO_PENDING && next_cache_state_ != STATE_NONE) { 105 while (rv != net::ERR_IO_PENDING && next_cache_state_ != STATE_NONE) {
139 switch (next_cache_state_) { 106 switch (next_cache_state_) {
140 case STATE_CREATE_MAIN: 107 case STATE_CREATE_MAIN:
141 case STATE_CREATE_MEDIA: { 108 case STATE_CREATE_MEDIA: {
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
218 } 185 }
219 default: { 186 default: {
220 NOTREACHED() << "bad state"; 187 NOTREACHED() << "bad state";
221 next_cache_state_ = STATE_NONE; // Stop looping. 188 next_cache_state_ = STATE_NONE; // Stop looping.
222 return; 189 return;
223 } 190 }
224 } 191 }
225 } 192 }
226 } 193 }
227 194
228 // The expected state sequence is STATE_NONE --> STATE_CREATE_MAIN -->
229 // STATE_PROCESS_MAIN --> STATE_CREATE_MEDIA --> STATE_PROCESS_MEDIA -->
230 // STATE_DONE. On error, we jump directly to STATE_DONE.
231 void StoragePartitionHttpCacheDataRemover::DoCountCache(int rv) {
232 DCHECK_NE(STATE_NONE, next_cache_state_);
233
234 while (rv != net::ERR_IO_PENDING && next_cache_state_ != STATE_NONE) {
235 // On error, finish and return the error code. A valid result value might
236 // be of two types - either net::OK from the CREATE states, or the result
237 // of calculation from the PROCESS states. Since net::OK == 0, it is valid
238 // to simply add the value to the final calculation result.
239 if (rv < 0) {
240 calculation_result_ = rv;
241 next_cache_state_ = STATE_DONE;
242 } else {
243 DCHECK_EQ(0, net::OK);
244 calculation_result_ += rv;
245 }
246
247 switch (next_cache_state_) {
248 case STATE_CREATE_MAIN:
249 case STATE_CREATE_MEDIA: {
250 // Get a pointer to the cache.
251 net::URLRequestContextGetter* getter =
252 (next_cache_state_ == STATE_CREATE_MAIN)
253 ? main_context_getter_.get()
254 : media_context_getter_.get();
255 net::HttpCache* http_cache = getter->GetURLRequestContext()
256 ->http_transaction_factory()
257 ->GetCache();
258
259 next_cache_state_ = (next_cache_state_ == STATE_CREATE_MAIN)
260 ? STATE_PROCESS_MAIN
261 : STATE_PROCESS_MEDIA;
262
263 rv = http_cache->GetBackend(
264 &cache_,
265 base::Bind(&StoragePartitionHttpCacheDataRemover::DoCountCache,
266 base::Unretained(this)));
267 break;
268 }
269 case STATE_PROCESS_MAIN:
270 case STATE_PROCESS_MEDIA: {
271 next_cache_state_ = (next_cache_state_ == STATE_PROCESS_MAIN)
272 ? STATE_CREATE_MEDIA
273 : STATE_DONE;
274
275 // |cache_| can be null if it cannot be initialized.
276 if (cache_) {
277 if (delete_begin_.is_null() && delete_end_.is_max()) {
278 rv = cache_->CalculateSizeOfAllEntries(
279 base::Bind(
280 &StoragePartitionHttpCacheDataRemover::DoCountCache,
281 base::Unretained(this)));
282 } else {
283 // TODO(msramek): Implement this when we need it.
284 DoCountCache(net::ERR_NOT_IMPLEMENTED);
285 }
286 cache_ = NULL;
287 }
288 break;
289 }
290 case STATE_DONE: {
291 cache_ = NULL;
292 next_cache_state_ = STATE_NONE;
293
294 // Notify the UI thread that we are done.
295 BrowserThread::PostTask(
296 BrowserThread::UI, FROM_HERE,
297 base::Bind(&StoragePartitionHttpCacheDataRemover::CountedHttpCache,
298 base::Unretained(this)));
299 return;
300 }
301 default: {
302 NOTREACHED() << "bad state";
303 next_cache_state_ = STATE_NONE; // Stop looping.
304 return;
305 }
306 }
307 }
308 }
309
310 } // namespace browsing_data 195 } // namespace browsing_data
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698