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

Side by Side Diff: chrome/browser/browsing_data/cache_counter.cc

Issue 2556363003: Refactor cache counting into a separate helper class (Closed)
Patch Set: extract cache_test_util and fixes 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 (c) 2015 The Chromium Authors. All rights reserved. 1 // Copyright (c) 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 "chrome/browser/browsing_data/cache_counter.h" 5 #include "chrome/browser/browsing_data/cache_counter.h"
6 #include "chrome/browser/profiles/profile.h" 6 #include "chrome/browser/profiles/profile.h"
7 #include "components/browsing_data/content/storage_partition_http_cache_data_rem over.h" 7 #include "components/browsing_data/content/conditional_cache_counting_helper.h"
8 #include "components/browsing_data/core/pref_names.h" 8 #include "components/browsing_data/core/pref_names.h"
9 #include "content/public/browser/browser_thread.h"
9 #include "net/base/net_errors.h" 10 #include "net/base/net_errors.h"
10 11
12 namespace {
13 base::TimeDelta kDefaultCacheCounterTimeout = base::TimeDelta::FromSeconds(3);
14 }
15
16 CacheCounter::CacheResult::CacheResult(const CacheCounter* source,
17 int64_t cache_size,
18 bool is_upper_limit)
19 : FinishedResult(source, cache_size),
20 cache_size_(cache_size),
21 is_upper_limit_(is_upper_limit) {}
22
23 CacheCounter::CacheResult::~CacheResult() {}
24
11 CacheCounter::CacheCounter(Profile* profile) 25 CacheCounter::CacheCounter(Profile* profile)
12 : profile_(profile), 26 : profile_(profile),
13 pending_(false), 27 pending_(false),
28 timeout_(kDefaultCacheCounterTimeout),
14 weak_ptr_factory_(this) {} 29 weak_ptr_factory_(this) {}
15 30
16 CacheCounter::~CacheCounter() { 31 CacheCounter::~CacheCounter() {
17 } 32 }
18 33
19 const char* CacheCounter::GetPrefName() const { 34 const char* CacheCounter::GetPrefName() const {
20 return browsing_data::prefs::kDeleteCache; 35 return browsing_data::prefs::kDeleteCache;
21 } 36 }
22 37
23 void CacheCounter::Count() { 38 void CacheCounter::Count() {
24 // TODO(msramek): StoragePartitionHttpCacheDataRemover currently does not 39 base::WeakPtr<browsing_data::ConditionalCacheCountingHelper> counter =
25 // implement counting for subsets of cache, only for the entire cache. Thus, 40 browsing_data::ConditionalCacheCountingHelper::CreateForRange(
26 // we ignore the time period setting and always request counting for 41 content::BrowserContext::GetDefaultStoragePartition(profile_),
27 // the unbounded time interval. It is up to the UI to interpret the results 42 GetPeriodStart(), base::Time::Max())
28 // for finite time intervals as upper estimates. 43 ->CountAndDestroySelfWhenFinished(
29 browsing_data::StoragePartitionHttpCacheDataRemover::CreateForRange( 44 base::Bind(&CacheCounter::OnCacheSizeCalculated,
30 content::BrowserContext::GetDefaultStoragePartition(profile_), 45 weak_ptr_factory_.GetWeakPtr(), false));
31 base::Time(), base::Time::Max()) 46 // Counting a subset of the cache can take a long time. If it doesn't succeed
32 ->Count(base::Bind(&CacheCounter::OnCacheSizeCalculated, 47 // before |cache_counter_timeout| runs out, get the total cache size as an
33 weak_ptr_factory_.GetWeakPtr())); 48 // estimate instead. We don't need a timeout if GetPeriodStart() is null as
49 // this already retrieves the full cache size.
50 if (!GetPeriodStart().is_null()) {
51 content::BrowserThread::PostDelayedTask(
52 content::BrowserThread::UI, FROM_HERE,
53 base::Bind(&CacheCounter::FetchEstimate, weak_ptr_factory_.GetWeakPtr(),
54 counter),
55 timeout_);
56 }
34 pending_ = true; 57 pending_ = true;
35 } 58 }
36 59
37 void CacheCounter::OnCacheSizeCalculated(int64_t result_bytes) { 60 void CacheCounter::FetchEstimate(
61 base::WeakPtr<browsing_data::ConditionalCacheCountingHelper> counter) {
62 if (!counter || counter->IsFinished())
63 return;
64 counter->CancelCounting();
65 // Try again for the whole cache.
66 browsing_data::ConditionalCacheCountingHelper::CreateForRange(
67 content::BrowserContext::GetDefaultStoragePartition(profile_),
68 base::Time(), base::Time::Max())
69 ->CountAndDestroySelfWhenFinished(
70 base::Bind(&CacheCounter::OnCacheSizeCalculated,
71 weak_ptr_factory_.GetWeakPtr(), true));
72 }
73
74 void CacheCounter::SetTimeout(base::TimeDelta timeout) {
75 timeout_ = timeout;
76 }
77
78 void CacheCounter::OnCacheSizeCalculated(bool is_upper_limit,
79 int64_t result_bytes) {
80 if (result_bytes == net::ERR_ABORTED) {
81 return;
82 }
38 pending_ = false; 83 pending_ = false;
39 84
40 // A value less than 0 means a net error code. 85 // A value less than 0 means a net error code.
41 if (result_bytes < 0) 86 if (result_bytes < 0)
42 return; 87 return;
43 88 auto result =
44 ReportResult(result_bytes); 89 base::MakeUnique<CacheResult>(this, result_bytes, is_upper_limit);
90 ReportResult(std::move(result));
45 } 91 }
46 92
47 bool CacheCounter::Pending() { 93 bool CacheCounter::Pending() {
48 return pending_; 94 return pending_;
49 } 95 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698