| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/profiles/profile_statistics_aggregator.h" | 5 #include "chrome/browser/profiles/profile_statistics_aggregator.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/macros.h" | 10 #include "base/macros.h" |
| 11 #include "base/time/time.h" | 11 #include "base/time/time.h" |
| 12 #include "chrome/browser/bookmarks/bookmark_model_factory.h" | 12 #include "chrome/browser/bookmarks/bookmark_model_factory.h" |
| 13 #include "chrome/browser/browser_process.h" | 13 #include "chrome/browser/browser_process.h" |
| 14 #include "chrome/browser/history/history_service_factory.h" | 14 #include "chrome/browser/history/history_service_factory.h" |
| 15 #include "chrome/browser/password_manager/password_store_factory.h" | 15 #include "chrome/browser/password_manager/password_store_factory.h" |
| 16 #include "chrome/browser/profiles/profile.h" | 16 #include "chrome/browser/profiles/profile.h" |
| 17 #include "chrome/browser/profiles/profile_manager.h" | 17 #include "chrome/browser/profiles/profile_manager.h" |
| 18 #include "chrome/browser/profiles/profile_statistics.h" | 18 #include "chrome/browser/profiles/profile_statistics.h" |
| 19 #include "chrome/browser/profiles/profile_statistics_factory.h" | 19 #include "chrome/browser/profiles/profile_statistics_factory.h" |
| 20 #include "chrome/browser/web_data_service_factory.h" |
| 21 #include "components/browsing_data/core/counters/autofill_counter.h" |
| 20 #include "components/browsing_data/core/counters/bookmark_counter.h" | 22 #include "components/browsing_data/core/counters/bookmark_counter.h" |
| 21 #include "components/browsing_data/core/counters/history_counter.h" | 23 #include "components/browsing_data/core/counters/history_counter.h" |
| 22 #include "components/browsing_data/core/counters/passwords_counter.h" | 24 #include "components/browsing_data/core/counters/passwords_counter.h" |
| 23 #include "components/browsing_data/core/pref_names.h" | 25 #include "components/browsing_data/core/pref_names.h" |
| 24 #include "components/prefs/pref_service.h" | |
| 25 #include "content/public/browser/browser_thread.h" | 26 #include "content/public/browser/browser_thread.h" |
| 26 | 27 |
| 27 using browsing_data::BrowsingDataCounter; | 28 using browsing_data::BrowsingDataCounter; |
| 28 | 29 |
| 29 namespace { | |
| 30 | |
| 31 // Callback for each pref. Every one that should be counted as a changed | |
| 32 // user pref will cause *count to be incremented. | |
| 33 void AccumulatePrefStats(const PrefService* pref_service, | |
| 34 int* count, | |
| 35 const std::string& key, | |
| 36 const base::Value& value) { | |
| 37 const PrefService::Preference* pref = pref_service->FindPreference(key); | |
| 38 // Skip all dictionaries, only want to count values. | |
| 39 if (!value.is_dict() && pref && pref->IsUserControlled() && | |
| 40 !pref->IsDefaultValue()) | |
| 41 ++(*count); | |
| 42 } | |
| 43 | |
| 44 } // namespace | |
| 45 | |
| 46 ProfileStatisticsAggregator::ProfileStatisticsAggregator( | 30 ProfileStatisticsAggregator::ProfileStatisticsAggregator( |
| 47 Profile* profile, | 31 Profile* profile, |
| 48 const base::Closure& done_callback) | 32 const base::Closure& done_callback) |
| 49 : profile_(profile), | 33 : profile_(profile), |
| 50 profile_path_(profile_->GetPath()), | 34 profile_path_(profile_->GetPath()), |
| 51 done_callback_(done_callback) {} | 35 done_callback_(done_callback) {} |
| 52 | 36 |
| 53 ProfileStatisticsAggregator::~ProfileStatisticsAggregator() {} | 37 ProfileStatisticsAggregator::~ProfileStatisticsAggregator() {} |
| 54 | 38 |
| 55 size_t ProfileStatisticsAggregator::GetCallbackCount() { | |
| 56 return stats_callbacks_.size(); | |
| 57 } | |
| 58 | |
| 59 void ProfileStatisticsAggregator::AddCallbackAndStartAggregator( | 39 void ProfileStatisticsAggregator::AddCallbackAndStartAggregator( |
| 60 const profiles::ProfileStatisticsCallback& stats_callback) { | 40 const profiles::ProfileStatisticsCallback& stats_callback) { |
| 61 if (stats_callback) | 41 if (stats_callback) |
| 62 stats_callbacks_.push_back(stats_callback); | 42 stats_callbacks_.push_back(stats_callback); |
| 63 StartAggregator(); | 43 StartAggregator(); |
| 64 } | 44 } |
| 65 | 45 |
| 66 void ProfileStatisticsAggregator::AddCounter( | 46 void ProfileStatisticsAggregator::AddCounter( |
| 67 std::unique_ptr<BrowsingDataCounter> counter) { | 47 std::unique_ptr<BrowsingDataCounter> counter) { |
| 68 counter->InitWithoutPref( | 48 counter->InitWithoutPref( |
| 69 base::Time(), base::Bind(&ProfileStatisticsAggregator::OnCounterResult, | 49 base::Time(), base::Bind(&ProfileStatisticsAggregator::OnCounterResult, |
| 70 base::Unretained(this))); | 50 base::Unretained(this))); |
| 71 counter->Restart(); | 51 counter->Restart(); |
| 72 counters_.push_back(std::move(counter)); | 52 counters_.push_back(std::move(counter)); |
| 73 } | 53 } |
| 74 | 54 |
| 75 void ProfileStatisticsAggregator::StartAggregator() { | 55 void ProfileStatisticsAggregator::StartAggregator() { |
| 76 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 56 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 77 DCHECK(g_browser_process->profile_manager()->IsValidProfile(profile_)); | 57 DCHECK(g_browser_process->profile_manager()->IsValidProfile(profile_)); |
| 78 profile_category_stats_.clear(); | 58 profile_category_stats_.clear(); |
| 79 | 59 |
| 80 // Try to cancel tasks. | 60 // Cancel tasks. |
| 81 tracker_.TryCancelAll(); | |
| 82 counters_.clear(); | 61 counters_.clear(); |
| 62 |
| 83 // Initiate bookmark counting. | 63 // Initiate bookmark counting. |
| 84 bookmarks::BookmarkModel* bookmark_model = | 64 bookmarks::BookmarkModel* bookmark_model = |
| 85 BookmarkModelFactory::GetForBrowserContext(profile_); | 65 BookmarkModelFactory::GetForBrowserContext(profile_); |
| 86 AddCounter(base::MakeUnique<browsing_data::BookmarkCounter>(bookmark_model)); | 66 AddCounter(base::MakeUnique<browsing_data::BookmarkCounter>(bookmark_model)); |
| 87 | 67 |
| 88 // Initiate history counting. | 68 // Initiate history counting. |
| 89 history::HistoryService* history_service = | 69 history::HistoryService* history_service = |
| 90 HistoryServiceFactory::GetForProfile(profile_, | 70 HistoryServiceFactory::GetForProfile(profile_, |
| 91 ServiceAccessType::EXPLICIT_ACCESS); | 71 ServiceAccessType::EXPLICIT_ACCESS); |
| 92 AddCounter(base::MakeUnique<browsing_data::HistoryCounter>( | 72 AddCounter(base::MakeUnique<browsing_data::HistoryCounter>( |
| 93 history_service, | 73 history_service, |
| 94 browsing_data::HistoryCounter::GetUpdatedWebHistoryServiceCallback(), | 74 browsing_data::HistoryCounter::GetUpdatedWebHistoryServiceCallback(), |
| 95 /*sync_service=*/nullptr)); | 75 /*sync_service=*/nullptr)); |
| 96 | 76 |
| 97 // Initiate stored password counting. | 77 // Initiate stored password counting. |
| 98 scoped_refptr<password_manager::PasswordStore> password_store = | 78 scoped_refptr<password_manager::PasswordStore> password_store = |
| 99 PasswordStoreFactory::GetForProfile( | 79 PasswordStoreFactory::GetForProfile( |
| 100 profile_, ServiceAccessType::EXPLICIT_ACCESS); | 80 profile_, ServiceAccessType::EXPLICIT_ACCESS); |
| 101 AddCounter(base::MakeUnique<browsing_data::PasswordsCounter>( | 81 AddCounter(base::MakeUnique<browsing_data::PasswordsCounter>( |
| 102 password_store, /*sync_service=*/nullptr)); | 82 password_store, /*sync_service=*/nullptr)); |
| 103 | 83 |
| 104 // Initiate preference counting (async). | 84 // Initiate autofill counting. |
| 105 tracker_.PostTaskAndReplyWithResult( | 85 scoped_refptr<autofill::AutofillWebDataService> autofill_service = |
| 106 content::BrowserThread::GetTaskRunnerForThread(content::BrowserThread::UI) | 86 WebDataServiceFactory::GetAutofillWebDataForProfile( |
| 107 .get(), | 87 profile_, ServiceAccessType::EXPLICIT_ACCESS); |
| 108 FROM_HERE, base::Bind(&ProfileStatisticsAggregator::CountPrefs, this), | 88 AddCounter(base::MakeUnique<browsing_data::AutofillCounter>( |
| 109 base::Bind(&ProfileStatisticsAggregator::StatisticsCallback, this, | 89 autofill_service, /*sync_service=*/nullptr)); |
| 110 profiles::kProfileStatisticsSettings)); | |
| 111 } | 90 } |
| 112 | 91 |
| 113 void ProfileStatisticsAggregator::OnCounterResult( | 92 void ProfileStatisticsAggregator::OnCounterResult( |
| 114 std::unique_ptr<BrowsingDataCounter::Result> result) { | 93 std::unique_ptr<BrowsingDataCounter::Result> result) { |
| 115 if (!result->Finished()) | 94 if (!result->Finished()) |
| 116 return; | 95 return; |
| 117 const char* pref_name = result->source()->GetPrefName(); | 96 const char* pref_name = result->source()->GetPrefName(); |
| 118 auto* finished_result = | 97 auto* finished_result = |
| 119 static_cast<BrowsingDataCounter::FinishedResult*>(result.get()); | 98 static_cast<BrowsingDataCounter::FinishedResult*>(result.get()); |
| 120 int count = finished_result->Value(); | 99 int count = finished_result->Value(); |
| 121 if (pref_name == browsing_data::BookmarkCounter::kPrefName) { | 100 if (pref_name == browsing_data::BookmarkCounter::kPrefName) { |
| 122 StatisticsCallbackSuccess(profiles::kProfileStatisticsBookmarks, count); | 101 StatisticsCallback(profiles::kProfileStatisticsBookmarks, count); |
| 123 } else if (pref_name == browsing_data::prefs::kDeleteBrowsingHistory) { | 102 } else if (pref_name == browsing_data::prefs::kDeleteBrowsingHistory) { |
| 124 StatisticsCallbackSuccess(profiles::kProfileStatisticsBrowsingHistory, | 103 StatisticsCallback(profiles::kProfileStatisticsBrowsingHistory, count); |
| 125 count); | |
| 126 } else if (pref_name == browsing_data::prefs::kDeletePasswords) { | 104 } else if (pref_name == browsing_data::prefs::kDeletePasswords) { |
| 127 StatisticsCallbackSuccess(profiles::kProfileStatisticsPasswords, count); | 105 StatisticsCallback(profiles::kProfileStatisticsPasswords, count); |
| 106 } else if (pref_name == browsing_data::prefs::kDeleteFormData) { |
| 107 StatisticsCallback(profiles::kProfileStatisticsAutofill, count); |
| 128 } else { | 108 } else { |
| 129 // TODO(dullweber): Add more cases when the other statistics are replaced. | |
| 130 NOTREACHED(); | 109 NOTREACHED(); |
| 131 } | 110 } |
| 132 } | 111 } |
| 133 | 112 |
| 134 void ProfileStatisticsAggregator::StatisticsCallback( | 113 void ProfileStatisticsAggregator::StatisticsCallback(const char* category, |
| 135 const char* category, ProfileStatValue result) { | 114 int count) { |
| 136 profiles::ProfileCategoryStat datum; | 115 profiles::ProfileCategoryStat datum; |
| 137 datum.category = category; | 116 datum.category = category; |
| 138 datum.count = result.count; | 117 datum.count = count; |
| 139 datum.success = result.success; | |
| 140 profile_category_stats_.push_back(datum); | 118 profile_category_stats_.push_back(datum); |
| 141 for (const auto& stats_callback : stats_callbacks_) { | 119 for (const auto& stats_callback : stats_callbacks_) { |
| 142 DCHECK(stats_callback); | 120 DCHECK(stats_callback); |
| 143 stats_callback.Run(profile_category_stats_); | 121 stats_callback.Run(profile_category_stats_); |
| 144 } | 122 } |
| 145 | 123 |
| 146 if (profile_category_stats_.size() == | 124 if (profile_category_stats_.size() == |
| 147 profiles::kProfileStatisticsCategories.size()) { | 125 profiles::kProfileStatisticsCategories.size()) { |
| 148 if (done_callback_) | 126 if (done_callback_) |
| 149 done_callback_.Run(); | 127 done_callback_.Run(); |
| 150 } | 128 } |
| 151 } | 129 } |
| 152 | |
| 153 void ProfileStatisticsAggregator::StatisticsCallbackSuccess( | |
| 154 const char* category, int count) { | |
| 155 ProfileStatValue result; | |
| 156 result.count = count; | |
| 157 result.success = true; | |
| 158 StatisticsCallback(category, result); | |
| 159 } | |
| 160 | |
| 161 void ProfileStatisticsAggregator::StatisticsCallbackFailure( | |
| 162 const char* category) { | |
| 163 ProfileStatValue result; | |
| 164 result.count = 0; | |
| 165 result.success = false; | |
| 166 StatisticsCallback(category, result); | |
| 167 } | |
| 168 | |
| 169 ProfileStatisticsAggregator::ProfileStatValue | |
| 170 ProfileStatisticsAggregator::CountPrefs() const { | |
| 171 const PrefService* pref_service = profile_->GetPrefs(); | |
| 172 | |
| 173 ProfileStatValue result; | |
| 174 if (pref_service) { | |
| 175 pref_service->IteratePreferenceValues(base::BindRepeating( | |
| 176 &AccumulatePrefStats, pref_service, base::Unretained(&result.count))); | |
| 177 result.success = true; | |
| 178 } | |
| 179 return result; | |
| 180 } | |
| OLD | NEW |