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

Unified Diff: chrome/browser/browsing_data/cache_counter.cc

Issue 2612033002: Count exact cache size for time ranges (Closed)
Patch Set: rebase 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/browsing_data/cache_counter.cc
diff --git a/chrome/browser/browsing_data/cache_counter.cc b/chrome/browser/browsing_data/cache_counter.cc
index 384193e86449091ccb09b6af4663432a1bb241f9..0743db3d246a3bbf839120f7cd52de200a7384cb 100644
--- a/chrome/browser/browsing_data/cache_counter.cc
+++ b/chrome/browser/browsing_data/cache_counter.cc
@@ -9,6 +9,10 @@
#include "content/public/browser/browser_thread.h"
#include "net/base/net_errors.h"
+namespace {
+base::TimeDelta kDefaultCacheCounterTimeout = base::TimeDelta::FromSeconds(3);
+}
+
CacheCounter::CacheResult::CacheResult(const CacheCounter* source,
int64_t cache_size,
bool is_upper_limit)
@@ -21,6 +25,7 @@ CacheCounter::CacheResult::~CacheResult() {}
CacheCounter::CacheCounter(Profile* profile)
: profile_(profile),
pending_(false),
+ timeout_(kDefaultCacheCounterTimeout),
weak_ptr_factory_(this) {}
CacheCounter::~CacheCounter() {
@@ -31,19 +36,50 @@ const char* CacheCounter::GetPrefName() const {
}
void CacheCounter::Count() {
- bool is_upper_limit = !GetPeriodStart().is_null();
base::WeakPtr<browsing_data::ConditionalCacheCountingHelper> counter =
browsing_data::ConditionalCacheCountingHelper::CreateForRange(
content::BrowserContext::GetDefaultStoragePartition(profile_),
- base::Time(), base::Time::Max())
+ GetPeriodStart(), base::Time::Max())
->CountAndDestroySelfWhenFinished(
base::Bind(&CacheCounter::OnCacheSizeCalculated,
- weak_ptr_factory_.GetWeakPtr(), is_upper_limit));
+ weak_ptr_factory_.GetWeakPtr(), false));
+ // Counting a subset of the cache can take a long time. If it doesn't succeed
+ // before |cache_counter_timeout| runs out, get the total cache size as an
+ // estimate instead. We don't need a timeout if GetPeriodStart() is null as
+ // this already retrieves the full cache size.
+ if (!GetPeriodStart().is_null()) {
+ content::BrowserThread::PostDelayedTask(
+ content::BrowserThread::UI, FROM_HERE,
+ base::Bind(&CacheCounter::FetchEstimate, weak_ptr_factory_.GetWeakPtr(),
+ counter),
+ timeout_);
+ }
pending_ = true;
}
+void CacheCounter::FetchEstimate(
+ base::WeakPtr<browsing_data::ConditionalCacheCountingHelper> counter) {
+ if (!counter || counter->IsFinished())
+ return;
+ counter->CancelCounting();
+ // Try again for the whole cache.
+ browsing_data::ConditionalCacheCountingHelper::CreateForRange(
+ content::BrowserContext::GetDefaultStoragePartition(profile_),
+ base::Time(), base::Time::Max())
+ ->CountAndDestroySelfWhenFinished(
+ base::Bind(&CacheCounter::OnCacheSizeCalculated,
+ weak_ptr_factory_.GetWeakPtr(), true));
+}
+
+void CacheCounter::SetTimeout(base::TimeDelta timeout) {
+ timeout_ = timeout;
+}
+
void CacheCounter::OnCacheSizeCalculated(bool is_upper_limit,
int64_t result_bytes) {
+ if (result_bytes == net::ERR_ABORTED) {
+ return;
+ }
pending_ = false;
// A value less than 0 means a net error code.
« no previous file with comments | « chrome/browser/browsing_data/cache_counter.h ('k') | chrome/browser/browsing_data/cache_counter_browsertest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698