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 #if defined(OS_ANDROID) | |
13 #include "chrome/browser/android/offline_pages/offline_page_utils.h" | |
14 #endif // OS_ANDROID | |
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 weak_ptr_factory_(this) {} | 27 weak_ptr_factory_(this) {} |
24 | 28 |
25 CacheCounter::~CacheCounter() { | 29 CacheCounter::~CacheCounter() { |
26 } | 30 } |
27 | 31 |
28 const char* CacheCounter::GetPrefName() const { | 32 const char* CacheCounter::GetPrefName() const { |
29 return GetTab() == browsing_data::ClearBrowsingDataTab::BASIC | 33 return GetTab() == browsing_data::ClearBrowsingDataTab::BASIC |
30 ? browsing_data::prefs::kDeleteCacheBasic | 34 ? browsing_data::prefs::kDeleteCacheBasic |
31 : browsing_data::prefs::kDeleteCache; | 35 : browsing_data::prefs::kDeleteCache; |
32 } | 36 } |
33 | 37 |
34 void CacheCounter::Count() { | 38 void CacheCounter::Count() { |
35 // Cancel existing requests. | 39 // Cancel existing requests and reset states. |
36 weak_ptr_factory_.InvalidateWeakPtrs(); | 40 weak_ptr_factory_.InvalidateWeakPtrs(); |
41 calculated_size_ = 0; | |
42 is_upper_limit_ = false; | |
43 pending_sources_ = 1; | |
37 base::WeakPtr<browsing_data::ConditionalCacheCountingHelper> counter = | 44 base::WeakPtr<browsing_data::ConditionalCacheCountingHelper> counter = |
38 browsing_data::ConditionalCacheCountingHelper::CreateForRange( | 45 browsing_data::ConditionalCacheCountingHelper::CreateForRange( |
39 content::BrowserContext::GetDefaultStoragePartition(profile_), | 46 content::BrowserContext::GetDefaultStoragePartition(profile_), |
40 GetPeriodStart(), base::Time::Max()) | 47 GetPeriodStart(), base::Time::Max()) |
41 ->CountAndDestroySelfWhenFinished( | 48 ->CountAndDestroySelfWhenFinished( |
42 base::Bind(&CacheCounter::OnCacheSizeCalculated, | 49 base::Bind(&CacheCounter::OnCacheSizeCalculated, |
43 weak_ptr_factory_.GetWeakPtr())); | 50 weak_ptr_factory_.GetWeakPtr())); |
51 #if defined(OS_ANDROID) | |
52 if (offline_pages::OfflinePageUtils::GetCachedOfflinePageSizeBetween( | |
53 profile_, | |
54 base::Bind(&CacheCounter::OnCacheSizeCalculated, | |
55 weak_ptr_factory_.GetWeakPtr(), | |
56 false /* is_upper_limit */), | |
57 GetPeriodStart(), base::Time::Max())) { | |
58 pending_sources_++; | |
59 } | |
60 #endif // OS_ANDROID | |
44 } | 61 } |
45 | 62 |
46 void CacheCounter::OnCacheSizeCalculated(int64_t result_bytes, | 63 void CacheCounter::OnCacheSizeCalculated(bool is_upper_limit, |
47 bool is_upper_limit) { | 64 int64_t cache_bytes) { |
48 // A value less than 0 means a net error code. | 65 // A value less than 0 means a net error code. |
dewittj
2017/05/11 16:58:43
nit: add a DCHECK(pending_sources_ > 0)
| |
49 if (result_bytes < 0) | 66 if (cache_bytes < 0) |
50 return; | 67 return; |
51 auto result = | 68 |
52 base::MakeUnique<CacheResult>(this, result_bytes, is_upper_limit); | 69 pending_sources_--; |
53 ReportResult(std::move(result)); | 70 calculated_size_ += cache_bytes; |
71 is_upper_limit_ = is_upper_limit_ || is_upper_limit; | |
dullweber
2017/05/10 08:13:40
you could use |= here
romax
2017/05/12 00:44:37
Done.
msramek
2017/05/12 09:31:27
I have a vague memory that using bitwise OR with l
| |
72 if (pending_sources_ == 0) { | |
73 auto result = | |
74 base::MakeUnique<CacheResult>(this, calculated_size_, is_upper_limit_); | |
75 ReportResult(std::move(result)); | |
76 } | |
54 } | 77 } |
OLD | NEW |