Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 using content::BrowserThread; | |
|
msramek
2016/12/15 15:58:50
style nit: Please use namespaces explicitly in non
dullweber
2016/12/16 16:41:10
Done.
| |
| 13 using browsing_data::ConditionalCacheCountingHelper; | |
| 14 | |
| 15 CacheCounter::CacheResult::CacheResult(const CacheCounter* source, | |
|
msramek
2016/12/15 15:58:50
style nit: please keep the ":" from initializers u
dullweber
2016/12/16 16:41:10
clangformat doesn't let me do this.
| |
| 16 int64_t cache_size, | |
| 17 bool is_upper_limit) | |
| 18 : FinishedResult(source, cache_size), | |
| 19 cache_size_(cache_size), | |
| 20 is_upper_limit_(is_upper_limit) {} | |
| 21 | |
| 22 CacheCounter::CacheResult::~CacheResult() {} | |
| 23 | |
| 11 CacheCounter::CacheCounter(Profile* profile) | 24 CacheCounter::CacheCounter(Profile* profile) |
| 12 : profile_(profile), | 25 : profile_(profile), |
| 13 pending_(false), | 26 pending_(false), |
| 14 weak_ptr_factory_(this) {} | 27 weak_ptr_factory_(this) {} |
| 15 | 28 |
| 16 CacheCounter::~CacheCounter() { | 29 CacheCounter::~CacheCounter() { |
| 17 } | 30 } |
| 18 | 31 |
| 19 const char* CacheCounter::GetPrefName() const { | 32 const char* CacheCounter::GetPrefName() const { |
| 20 return browsing_data::prefs::kDeleteCache; | 33 return browsing_data::prefs::kDeleteCache; |
| 21 } | 34 } |
| 22 | 35 |
| 23 void CacheCounter::Count() { | 36 void CacheCounter::Count() { |
| 24 // TODO(msramek): StoragePartitionHttpCacheDataRemover currently does not | 37 base::WeakPtr<ConditionalCacheCountingHelper> counter = |
| 25 // implement counting for subsets of cache, only for the entire cache. Thus, | 38 ConditionalCacheCountingHelper::CreateForRange( |
| 26 // we ignore the time period setting and always request counting for | 39 content::BrowserContext::GetDefaultStoragePartition(profile_), |
| 27 // the unbounded time interval. It is up to the UI to interpret the results | 40 GetPeriodStart(), base::Time::Max()) |
| 28 // for finite time intervals as upper estimates. | 41 ->CountAndDestroySelfWhenFinished( |
| 29 browsing_data::StoragePartitionHttpCacheDataRemover::CreateForRange( | 42 base::Bind(&CacheCounter::OnCacheSizeCalculated, |
| 30 content::BrowserContext::GetDefaultStoragePartition(profile_), | 43 weak_ptr_factory_.GetWeakPtr(), false)); |
| 31 base::Time(), base::Time::Max()) | 44 // Counting a subset of the cache can take a long time. If it doesn't succeed |
|
msramek
2016/12/15 15:58:50
nit: The following block is wrapped in GetPeriodSt
dullweber
2016/12/16 16:41:10
Done.
| |
| 32 ->Count(base::Bind(&CacheCounter::OnCacheSizeCalculated, | 45 // before |cache_counter_timeout| runs out, get the total cache size as an |
| 33 weak_ptr_factory_.GetWeakPtr())); | 46 // estimate instead. |
| 47 if (!GetPeriodStart().is_null()) { | |
| 48 base::TimeDelta cache_counter_timeout = base::TimeDelta::FromSeconds(3); | |
|
msramek
2016/12/15 15:58:50
nit: Please pull this up as kCacheCounterTimeout c
dullweber
2016/12/16 16:41:10
Done.
| |
| 49 BrowserThread::PostDelayedTask( | |
| 50 BrowserThread::UI, FROM_HERE, | |
| 51 base::Bind(&CacheCounter::FetchEstimate, weak_ptr_factory_.GetWeakPtr(), | |
| 52 counter), | |
| 53 cache_counter_timeout); | |
| 54 } | |
| 34 pending_ = true; | 55 pending_ = true; |
| 35 } | 56 } |
| 36 | 57 |
| 37 void CacheCounter::OnCacheSizeCalculated(int64_t result_bytes) { | 58 void CacheCounter::FetchEstimate( |
| 59 base::WeakPtr<ConditionalCacheCountingHelper> counter) { | |
| 60 if (!counter || counter->IsFinished()) | |
| 61 return; | |
| 62 counter->CancelCounting(); | |
| 63 // Try again for the whole cache. | |
| 64 ConditionalCacheCountingHelper::CreateForRange( | |
| 65 content::BrowserContext::GetDefaultStoragePartition(profile_), | |
| 66 base::Time(), base::Time::Max()) | |
| 67 ->CountAndDestroySelfWhenFinished( | |
| 68 base::Bind(&CacheCounter::OnCacheSizeCalculated, | |
| 69 weak_ptr_factory_.GetWeakPtr(), true)); | |
|
msramek
2016/12/15 15:58:50
|is_upper_limit| is not always true; it depends on
dullweber
2016/12/16 16:41:10
This code is only executed if the timer finishes b
msramek
2016/12/20 01:02:59
Acknowledged.
| |
| 70 } | |
| 71 | |
| 72 void CacheCounter::OnCacheSizeCalculated(bool is_upper_limit, | |
| 73 int64_t result_bytes) { | |
| 74 if (result_bytes == net::ERR_ABORTED) { | |
| 75 return; | |
| 76 } | |
| 38 pending_ = false; | 77 pending_ = false; |
|
msramek
2016/12/15 15:58:50
nit: Please re-add the empty line for readability.
dullweber
2016/12/16 16:41:10
Done.
| |
| 39 | |
| 40 // A value less than 0 means a net error code. | 78 // A value less than 0 means a net error code. |
| 41 if (result_bytes < 0) | 79 if (result_bytes < 0) |
| 42 return; | 80 return; |
| 43 | 81 auto result = |
| 44 ReportResult(result_bytes); | 82 base::MakeUnique<CacheResult>(this, result_bytes, is_upper_limit); |
| 83 ReportResult(std::move(result)); | |
| 45 } | 84 } |
| 46 | 85 |
| 47 bool CacheCounter::Pending() { | 86 bool CacheCounter::Pending() { |
| 48 return pending_; | 87 return pending_; |
| 49 } | 88 } |
| OLD | NEW |