Index: components/browsing_data/content/conditional_cache_counting_helper.cc |
diff --git a/components/browsing_data/content/conditional_cache_counting_helper.cc b/components/browsing_data/content/conditional_cache_counting_helper.cc |
index 94b772f14c77872889bde3042fc4ed67035ae367..8cf044e381a276383119e40e8b4d37a2466108f2 100644 |
--- a/components/browsing_data/content/conditional_cache_counting_helper.cc |
+++ b/components/browsing_data/content/conditional_cache_counting_helper.cc |
@@ -36,12 +36,14 @@ ConditionalCacheCountingHelper::ConditionalCacheCountingHelper( |
: calculation_result_(0), |
begin_time_(begin_time), |
end_time_(end_time), |
+ is_cancelled_(false), |
is_finished_(false), |
main_context_getter_(main_context_getter), |
media_context_getter_(media_context_getter), |
next_cache_state_(CacheState::NONE), |
cache_(nullptr), |
iterator_(nullptr), |
+ current_entry_(nullptr), |
weak_ptr_factory_(this) { |
DCHECK_CURRENTLY_ON(BrowserThread::UI); |
} |
@@ -65,6 +67,12 @@ ConditionalCacheCountingHelper::CountAndDestroySelfWhenFinished( |
return weak_ptr_factory_.GetWeakPtr(); |
} |
+void ConditionalCacheCountingHelper::CancelCounting() { |
+ DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
+ DCHECK(!is_finished_); |
+ is_cancelled_ = true; |
+} |
+ |
bool ConditionalCacheCountingHelper::IsFinished() { |
return is_finished_; |
} |
@@ -141,11 +149,8 @@ void ConditionalCacheCountingHelper::DoCountCache(int rv) { |
base::Bind(&ConditionalCacheCountingHelper::DoCountCache, |
base::Unretained(this))); |
} else { |
- // TODO(dullweber): Readd code for counting with timeout. |
// TODO(dullweber): Implement faster counting for SimpleBackendImpl. |
- rv = cache_->CalculateSizeOfAllEntries( |
- base::Bind(&ConditionalCacheCountingHelper::DoCountCache, |
- base::Unretained(this))); |
+ rv = CountEntries(cache_); |
} |
cache_ = NULL; |
} |
@@ -169,4 +174,46 @@ void ConditionalCacheCountingHelper::DoCountCache(int rv) { |
} |
} |
+int ConditionalCacheCountingHelper::CountEntries(disk_cache::Backend* backend) { |
+ DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
+ iterator_ = backend->CreateIterator(); |
+ current_entry_ = nullptr; |
+ IterateOverEntries(net::OK); |
+ return net::ERR_IO_PENDING; |
+} |
+ |
+void ConditionalCacheCountingHelper::IterateOverEntries(int error) { |
+ while (error != net::ERR_IO_PENDING) { |
+ if (is_cancelled_) { |
+ if (current_entry_) |
+ current_entry_->Close(); |
+ iterator_.reset(); |
+ DoCountCache(net::ERR_ABORTED); |
+ return; |
+ } |
+ if (error == net::ERR_FAILED) { |
+ // The iteration finished successfully or we can no longer iterate |
+ // (e.g. the cache was destroyed). We cannot distinguish between the two, |
+ // but we know that there is nothing more that we can do, so we return to |
+ // the main calculation loop. |
+ iterator_.reset(); |
+ DoCountCache(net::OK); |
+ return; |
+ } |
+ |
+ if (current_entry_) { |
+ if (current_entry_->GetLastUsed() >= begin_time_ && |
+ current_entry_->GetLastUsed() < end_time_) { |
+ calculation_result_ += current_entry_->GetEntrySize(); |
+ } |
+ current_entry_->Close(); |
+ } |
+ |
+ error = iterator_->OpenNextEntry( |
+ ¤t_entry_, |
+ base::Bind(&ConditionalCacheCountingHelper::IterateOverEntries, |
+ base::Unretained(this))); |
+ } |
+} |
+ |
} // namespace browsing_data |