| 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_counting_helper.h" | 5 #include "components/browsing_data/content/conditional_cache_counting_helper.h" |
| 6 | 6 |
| 7 #include "base/callback.h" | 7 #include "base/callback.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 "content/public/browser/browser_thread.h" | 10 #include "content/public/browser/browser_thread.h" |
| (...skipping 18 matching lines...) Expand all Loading... |
| 29 } | 29 } |
| 30 | 30 |
| 31 ConditionalCacheCountingHelper::ConditionalCacheCountingHelper( | 31 ConditionalCacheCountingHelper::ConditionalCacheCountingHelper( |
| 32 base::Time begin_time, | 32 base::Time begin_time, |
| 33 base::Time end_time, | 33 base::Time end_time, |
| 34 net::URLRequestContextGetter* main_context_getter, | 34 net::URLRequestContextGetter* main_context_getter, |
| 35 net::URLRequestContextGetter* media_context_getter) | 35 net::URLRequestContextGetter* media_context_getter) |
| 36 : calculation_result_(0), | 36 : calculation_result_(0), |
| 37 begin_time_(begin_time), | 37 begin_time_(begin_time), |
| 38 end_time_(end_time), | 38 end_time_(end_time), |
| 39 is_cancelled_(false), |
| 39 is_finished_(false), | 40 is_finished_(false), |
| 40 main_context_getter_(main_context_getter), | 41 main_context_getter_(main_context_getter), |
| 41 media_context_getter_(media_context_getter), | 42 media_context_getter_(media_context_getter), |
| 42 next_cache_state_(CacheState::NONE), | 43 next_cache_state_(CacheState::NONE), |
| 43 cache_(nullptr), | 44 cache_(nullptr), |
| 44 iterator_(nullptr), | 45 iterator_(nullptr), |
| 46 current_entry_(nullptr), |
| 45 weak_ptr_factory_(this) { | 47 weak_ptr_factory_(this) { |
| 46 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 48 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 47 } | 49 } |
| 48 | 50 |
| 49 ConditionalCacheCountingHelper::~ConditionalCacheCountingHelper() { | 51 ConditionalCacheCountingHelper::~ConditionalCacheCountingHelper() { |
| 50 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 52 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 51 } | 53 } |
| 52 | 54 |
| 53 base::WeakPtr<ConditionalCacheCountingHelper> | 55 base::WeakPtr<ConditionalCacheCountingHelper> |
| 54 ConditionalCacheCountingHelper::CountAndDestroySelfWhenFinished( | 56 ConditionalCacheCountingHelper::CountAndDestroySelfWhenFinished( |
| 55 const CacheCountCallback& result_callback) { | 57 const CacheCountCallback& result_callback) { |
| 56 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 58 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 57 DCHECK(!result_callback.is_null()); | 59 DCHECK(!result_callback.is_null()); |
| 58 result_callback_ = result_callback; | 60 result_callback_ = result_callback; |
| 59 calculation_result_ = 0; | 61 calculation_result_ = 0; |
| 60 | 62 |
| 61 BrowserThread::PostTask( | 63 BrowserThread::PostTask( |
| 62 BrowserThread::IO, FROM_HERE, | 64 BrowserThread::IO, FROM_HERE, |
| 63 base::Bind(&ConditionalCacheCountingHelper::CountHttpCacheOnIOThread, | 65 base::Bind(&ConditionalCacheCountingHelper::CountHttpCacheOnIOThread, |
| 64 base::Unretained(this))); | 66 base::Unretained(this))); |
| 65 return weak_ptr_factory_.GetWeakPtr(); | 67 return weak_ptr_factory_.GetWeakPtr(); |
| 66 } | 68 } |
| 67 | 69 |
| 70 void ConditionalCacheCountingHelper::CancelCounting() { |
| 71 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 72 DCHECK(!is_finished_); |
| 73 is_cancelled_ = true; |
| 74 } |
| 75 |
| 68 bool ConditionalCacheCountingHelper::IsFinished() { | 76 bool ConditionalCacheCountingHelper::IsFinished() { |
| 69 return is_finished_; | 77 return is_finished_; |
| 70 } | 78 } |
| 71 | 79 |
| 72 void ConditionalCacheCountingHelper::Finished() { | 80 void ConditionalCacheCountingHelper::Finished() { |
| 73 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 81 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 74 DCHECK(!is_finished_); | 82 DCHECK(!is_finished_); |
| 75 is_finished_ = true; | 83 is_finished_ = true; |
| 76 result_callback_.Run(calculation_result_); | 84 result_callback_.Run(calculation_result_); |
| 77 base::ThreadTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE, this); | 85 base::ThreadTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE, this); |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 134 ? CacheState::CREATE_MEDIA | 142 ? CacheState::CREATE_MEDIA |
| 135 : CacheState::DONE; | 143 : CacheState::DONE; |
| 136 | 144 |
| 137 // |cache_| can be null if it cannot be initialized. | 145 // |cache_| can be null if it cannot be initialized. |
| 138 if (cache_) { | 146 if (cache_) { |
| 139 if (begin_time_.is_null() && end_time_.is_max()) { | 147 if (begin_time_.is_null() && end_time_.is_max()) { |
| 140 rv = cache_->CalculateSizeOfAllEntries( | 148 rv = cache_->CalculateSizeOfAllEntries( |
| 141 base::Bind(&ConditionalCacheCountingHelper::DoCountCache, | 149 base::Bind(&ConditionalCacheCountingHelper::DoCountCache, |
| 142 base::Unretained(this))); | 150 base::Unretained(this))); |
| 143 } else { | 151 } else { |
| 144 // TODO(dullweber): Readd code for counting with timeout. | |
| 145 // TODO(dullweber): Implement faster counting for SimpleBackendImpl. | 152 // TODO(dullweber): Implement faster counting for SimpleBackendImpl. |
| 146 rv = cache_->CalculateSizeOfAllEntries( | 153 rv = CountEntries(cache_); |
| 147 base::Bind(&ConditionalCacheCountingHelper::DoCountCache, | |
| 148 base::Unretained(this))); | |
| 149 } | 154 } |
| 150 cache_ = NULL; | 155 cache_ = NULL; |
| 151 } | 156 } |
| 152 break; | 157 break; |
| 153 } | 158 } |
| 154 case CacheState::DONE: { | 159 case CacheState::DONE: { |
| 155 cache_ = NULL; | 160 cache_ = NULL; |
| 156 next_cache_state_ = CacheState::NONE; | 161 next_cache_state_ = CacheState::NONE; |
| 157 // Notify the UI thread that we are done. | 162 // Notify the UI thread that we are done. |
| 158 BrowserThread::PostTask( | 163 BrowserThread::PostTask( |
| 159 BrowserThread::UI, FROM_HERE, | 164 BrowserThread::UI, FROM_HERE, |
| 160 base::Bind(&ConditionalCacheCountingHelper::Finished, | 165 base::Bind(&ConditionalCacheCountingHelper::Finished, |
| 161 base::Unretained(this))); | 166 base::Unretained(this))); |
| 162 return; | 167 return; |
| 163 } | 168 } |
| 164 case CacheState::NONE: { | 169 case CacheState::NONE: { |
| 165 NOTREACHED() << "bad state"; | 170 NOTREACHED() << "bad state"; |
| 166 return; | 171 return; |
| 167 } | 172 } |
| 168 } | 173 } |
| 169 } | 174 } |
| 170 } | 175 } |
| 171 | 176 |
| 177 int ConditionalCacheCountingHelper::CountEntries(disk_cache::Backend* backend) { |
| 178 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 179 iterator_ = backend->CreateIterator(); |
| 180 current_entry_ = nullptr; |
| 181 IterateOverEntries(net::OK); |
| 182 return net::ERR_IO_PENDING; |
| 183 } |
| 184 |
| 185 void ConditionalCacheCountingHelper::IterateOverEntries(int error) { |
| 186 while (error != net::ERR_IO_PENDING) { |
| 187 if (is_cancelled_) { |
| 188 if (current_entry_) |
| 189 current_entry_->Close(); |
| 190 iterator_.reset(); |
| 191 DoCountCache(net::ERR_ABORTED); |
| 192 return; |
| 193 } |
| 194 if (error == net::ERR_FAILED) { |
| 195 // The iteration finished successfully or we can no longer iterate |
| 196 // (e.g. the cache was destroyed). We cannot distinguish between the two, |
| 197 // but we know that there is nothing more that we can do, so we return to |
| 198 // the main calculation loop. |
| 199 iterator_.reset(); |
| 200 DoCountCache(net::OK); |
| 201 return; |
| 202 } |
| 203 |
| 204 if (current_entry_) { |
| 205 if (current_entry_->GetLastUsed() >= begin_time_ && |
| 206 current_entry_->GetLastUsed() < end_time_) { |
| 207 calculation_result_ += current_entry_->GetEntrySize(); |
| 208 } |
| 209 current_entry_->Close(); |
| 210 } |
| 211 |
| 212 error = iterator_->OpenNextEntry( |
| 213 ¤t_entry_, |
| 214 base::Bind(&ConditionalCacheCountingHelper::IterateOverEntries, |
| 215 base::Unretained(this))); |
| 216 } |
| 217 } |
| 218 |
| 172 } // namespace browsing_data | 219 } // namespace browsing_data |
| OLD | NEW |