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

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: Remove code depending on GetEntrySize() Created 3 years, 11 months 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
« no previous file with comments | « components/browsing_data/content/storage_partition_http_cache_data_remover.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 16 matching lines...) Expand all
27 base::Callback<bool(const GURL&)> url_predicate, 27 base::Callback<bool(const GURL&)> url_predicate,
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_(CacheState::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_ = CacheState::NONE;
101 DCHECK_EQ(STATE_NONE, next_cache_state_); 85 DCHECK_EQ(CacheState::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_ = CacheState::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() { 99 // The expected state sequence is CacheState::NONE --> CacheState::CREATE_MAIN
127 DCHECK_CURRENTLY_ON(BrowserThread::UI); 100 // --> CacheState::PROCESS_MAIN --> CacheState::CREATE_MEDIA -->
128 result_callback_.Run(calculation_result_); 101 // CacheState::PROCESS_MEDIA --> CacheState::DONE, and any errors are ignored.
129 base::ThreadTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE, this); 102 void StoragePartitionHttpCacheDataRemover::DoClearCache(int rv) {
130 } 103 DCHECK_NE(CacheState::NONE, next_cache_state_);
131 104
132 // The expected state sequence is STATE_NONE --> STATE_CREATE_MAIN --> 105 while (rv != net::ERR_IO_PENDING && next_cache_state_ != CacheState::NONE) {
133 // STATE_PROCESS_MAIN --> STATE_CREATE_MEDIA --> STATE_PROCESS_MEDIA -->
134 // STATE_DONE, and any errors are ignored.
135 void StoragePartitionHttpCacheDataRemover::DoClearCache(int rv) {
136 DCHECK_NE(STATE_NONE, next_cache_state_);
137
138 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 CacheState::CREATE_MAIN:
141 case STATE_CREATE_MEDIA: { 108 case CacheState::CREATE_MEDIA: {
142 // Get a pointer to the cache. 109 // Get a pointer to the cache.
143 net::URLRequestContextGetter* getter = 110 net::URLRequestContextGetter* getter =
144 (next_cache_state_ == STATE_CREATE_MAIN) 111 (next_cache_state_ == CacheState::CREATE_MAIN)
145 ? main_context_getter_.get() 112 ? main_context_getter_.get()
146 : media_context_getter_.get(); 113 : media_context_getter_.get();
147 net::HttpCache* http_cache = getter->GetURLRequestContext() 114 net::HttpCache* http_cache = getter->GetURLRequestContext()
148 ->http_transaction_factory() 115 ->http_transaction_factory()
149 ->GetCache(); 116 ->GetCache();
150 117
151 next_cache_state_ = (next_cache_state_ == STATE_CREATE_MAIN) 118 next_cache_state_ = (next_cache_state_ == CacheState::CREATE_MAIN)
152 ? STATE_PROCESS_MAIN 119 ? CacheState::DELETE_MAIN
153 : STATE_PROCESS_MEDIA; 120 : CacheState::DELETE_MEDIA;
154 121
155 // Clear QUIC server information from memory and the disk cache. 122 // Clear QUIC server information from memory and the disk cache.
156 http_cache->GetSession() 123 http_cache->GetSession()
157 ->quic_stream_factory() 124 ->quic_stream_factory()
158 ->ClearCachedStatesInCryptoConfig(url_predicate_); 125 ->ClearCachedStatesInCryptoConfig(url_predicate_);
159 126
160 // Clear SDCH dictionary state. 127 // Clear SDCH dictionary state.
161 net::SdchManager* sdch_manager = 128 net::SdchManager* sdch_manager =
162 getter->GetURLRequestContext()->sdch_manager(); 129 getter->GetURLRequestContext()->sdch_manager();
163 // The test is probably overkill, since chrome should always have an 130 // The test is probably overkill, since chrome should always have an
164 // SdchManager. But in general the URLRequestContext is *not* 131 // SdchManager. But in general the URLRequestContext is *not*
165 // guaranteed to have an SdchManager, so checking is wise. 132 // guaranteed to have an SdchManager, so checking is wise.
166 if (sdch_manager) 133 if (sdch_manager)
167 sdch_manager->ClearData(); 134 sdch_manager->ClearData();
168 135
169 rv = http_cache->GetBackend( 136 rv = http_cache->GetBackend(
170 &cache_, 137 &cache_,
171 base::Bind(&StoragePartitionHttpCacheDataRemover::DoClearCache, 138 base::Bind(&StoragePartitionHttpCacheDataRemover::DoClearCache,
172 base::Unretained(this))); 139 base::Unretained(this)));
173 break; 140 break;
174 } 141 }
175 case STATE_PROCESS_MAIN: 142 case CacheState::DELETE_MAIN:
176 case STATE_PROCESS_MEDIA: { 143 case CacheState::DELETE_MEDIA: {
177 next_cache_state_ = (next_cache_state_ == STATE_PROCESS_MAIN) 144 next_cache_state_ = (next_cache_state_ == CacheState::DELETE_MAIN)
178 ? STATE_CREATE_MEDIA 145 ? CacheState::CREATE_MEDIA
179 : STATE_DONE; 146 : CacheState::DONE;
180 147
181 // |cache_| can be null if it cannot be initialized. 148 // |cache_| can be null if it cannot be initialized.
182 if (cache_) { 149 if (cache_) {
183 if (!url_predicate_.is_null()) { 150 if (!url_predicate_.is_null()) {
184 rv = (new ConditionalCacheDeletionHelper( 151 rv = (new ConditionalCacheDeletionHelper(
185 cache_, 152 cache_,
186 ConditionalCacheDeletionHelper::CreateURLAndTimeCondition( 153 ConditionalCacheDeletionHelper::CreateURLAndTimeCondition(
187 url_predicate_, 154 url_predicate_,
188 delete_begin_, 155 delete_begin_,
189 delete_end_)))->DeleteAndDestroySelfWhenFinished( 156 delete_end_)))->DeleteAndDestroySelfWhenFinished(
190 base::Bind( 157 base::Bind(
191 &StoragePartitionHttpCacheDataRemover::DoClearCache, 158 &StoragePartitionHttpCacheDataRemover::DoClearCache,
192 base::Unretained(this))); 159 base::Unretained(this)));
193 } else if (delete_begin_.is_null() && delete_end_.is_max()) { 160 } else if (delete_begin_.is_null() && delete_end_.is_max()) {
194 rv = cache_->DoomAllEntries(base::Bind( 161 rv = cache_->DoomAllEntries(base::Bind(
195 &StoragePartitionHttpCacheDataRemover::DoClearCache, 162 &StoragePartitionHttpCacheDataRemover::DoClearCache,
196 base::Unretained(this))); 163 base::Unretained(this)));
197 } else { 164 } else {
198 rv = cache_->DoomEntriesBetween( 165 rv = cache_->DoomEntriesBetween(
199 delete_begin_, delete_end_, 166 delete_begin_, delete_end_,
200 base::Bind( 167 base::Bind(
201 &StoragePartitionHttpCacheDataRemover::DoClearCache, 168 &StoragePartitionHttpCacheDataRemover::DoClearCache,
202 base::Unretained(this))); 169 base::Unretained(this)));
203 } 170 }
204 cache_ = NULL; 171 cache_ = NULL;
205 } 172 }
206 break; 173 break;
207 } 174 }
208 case STATE_DONE: { 175 case CacheState::DONE: {
209 cache_ = NULL; 176 cache_ = NULL;
210 next_cache_state_ = STATE_NONE; 177 next_cache_state_ = CacheState::NONE;
211 178
212 // Notify the UI thread that we are done. 179 // Notify the UI thread that we are done.
213 BrowserThread::PostTask( 180 BrowserThread::PostTask(
214 BrowserThread::UI, FROM_HERE, 181 BrowserThread::UI, FROM_HERE,
215 base::Bind(&StoragePartitionHttpCacheDataRemover::ClearedHttpCache, 182 base::Bind(&StoragePartitionHttpCacheDataRemover::ClearedHttpCache,
216 base::Unretained(this))); 183 base::Unretained(this)));
217 return; 184 return;
218 } 185 }
219 default: { 186 case CacheState::NONE: {
220 NOTREACHED() << "bad state"; 187 NOTREACHED() << "bad state";
221 next_cache_state_ = STATE_NONE; // Stop looping.
222 return; 188 return;
223 } 189 }
224 } 190 }
225 }
226 }
227
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 } 191 }
308 } 192 }
309 193
310 } // namespace browsing_data 194 } // namespace browsing_data
OLDNEW
« no previous file with comments | « components/browsing_data/content/storage_partition_http_cache_data_remover.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698