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/conditional_cache_counting_helper.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 "content/public/browser/browser_thread.h" |
10 #include "net/base/net_errors.h" | 10 #include "net/base/net_errors.h" |
11 | 11 |
| 12 namespace { |
| 13 base::TimeDelta kDefaultCacheCounterTimeout = base::TimeDelta::FromSeconds(3); |
| 14 } |
| 15 |
12 CacheCounter::CacheResult::CacheResult(const CacheCounter* source, | 16 CacheCounter::CacheResult::CacheResult(const CacheCounter* source, |
13 int64_t cache_size, | 17 int64_t cache_size, |
14 bool is_upper_limit) | 18 bool is_upper_limit) |
15 : FinishedResult(source, cache_size), | 19 : FinishedResult(source, cache_size), |
16 cache_size_(cache_size), | 20 cache_size_(cache_size), |
17 is_upper_limit_(is_upper_limit) {} | 21 is_upper_limit_(is_upper_limit) {} |
18 | 22 |
19 CacheCounter::CacheResult::~CacheResult() {} | 23 CacheCounter::CacheResult::~CacheResult() {} |
20 | 24 |
21 CacheCounter::CacheCounter(Profile* profile) | 25 CacheCounter::CacheCounter(Profile* profile) |
22 : profile_(profile), | 26 : profile_(profile), |
23 pending_(false), | 27 pending_(false), |
| 28 timeout_(kDefaultCacheCounterTimeout), |
24 weak_ptr_factory_(this) {} | 29 weak_ptr_factory_(this) {} |
25 | 30 |
26 CacheCounter::~CacheCounter() { | 31 CacheCounter::~CacheCounter() { |
27 } | 32 } |
28 | 33 |
29 const char* CacheCounter::GetPrefName() const { | 34 const char* CacheCounter::GetPrefName() const { |
30 return browsing_data::prefs::kDeleteCache; | 35 return browsing_data::prefs::kDeleteCache; |
31 } | 36 } |
32 | 37 |
33 void CacheCounter::Count() { | 38 void CacheCounter::Count() { |
34 bool is_upper_limit = !GetPeriodStart().is_null(); | |
35 base::WeakPtr<browsing_data::ConditionalCacheCountingHelper> counter = | 39 base::WeakPtr<browsing_data::ConditionalCacheCountingHelper> counter = |
36 browsing_data::ConditionalCacheCountingHelper::CreateForRange( | 40 browsing_data::ConditionalCacheCountingHelper::CreateForRange( |
37 content::BrowserContext::GetDefaultStoragePartition(profile_), | 41 content::BrowserContext::GetDefaultStoragePartition(profile_), |
38 base::Time(), base::Time::Max()) | 42 GetPeriodStart(), base::Time::Max()) |
39 ->CountAndDestroySelfWhenFinished( | 43 ->CountAndDestroySelfWhenFinished( |
40 base::Bind(&CacheCounter::OnCacheSizeCalculated, | 44 base::Bind(&CacheCounter::OnCacheSizeCalculated, |
41 weak_ptr_factory_.GetWeakPtr(), is_upper_limit)); | 45 weak_ptr_factory_.GetWeakPtr(), false)); |
| 46 // Counting a subset of the cache can take a long time. If it doesn't succeed |
| 47 // before |cache_counter_timeout| runs out, get the total cache size as an |
| 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 } |
42 pending_ = true; | 57 pending_ = true; |
43 } | 58 } |
44 | 59 |
| 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 |
45 void CacheCounter::OnCacheSizeCalculated(bool is_upper_limit, | 78 void CacheCounter::OnCacheSizeCalculated(bool is_upper_limit, |
46 int64_t result_bytes) { | 79 int64_t result_bytes) { |
| 80 if (result_bytes == net::ERR_ABORTED) { |
| 81 return; |
| 82 } |
47 pending_ = false; | 83 pending_ = false; |
48 | 84 |
49 // A value less than 0 means a net error code. | 85 // A value less than 0 means a net error code. |
50 if (result_bytes < 0) | 86 if (result_bytes < 0) |
51 return; | 87 return; |
52 auto result = | 88 auto result = |
53 base::MakeUnique<CacheResult>(this, result_bytes, is_upper_limit); | 89 base::MakeUnique<CacheResult>(this, result_bytes, is_upper_limit); |
54 ReportResult(std::move(result)); | 90 ReportResult(std::move(result)); |
55 } | 91 } |
56 | 92 |
57 bool CacheCounter::Pending() { | 93 bool CacheCounter::Pending() { |
58 return pending_; | 94 return pending_; |
59 } | 95 } |
OLD | NEW |