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

Side by Side Diff: chrome/browser/profiles/profile_statistics_aggregator.cc

Issue 1579433002: Make profile statistics tasks inspectable by tests (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Make ProfileStatisticsAggregator tasks trackable Created 4 years, 11 months 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
(Empty)
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "chrome/browser/profiles/profile_statistics_aggregator.h"
6
7 #include <stddef.h>
8
9 #include "base/bind.h"
10 #include "base/macros.h"
11 #include "base/prefs/pref_service.h"
12 #include "base/time/time.h"
13 #include "chrome/browser/bookmarks/bookmark_model_factory.h"
14 #include "chrome/browser/browser_process.h"
15 #include "chrome/browser/history/history_service_factory.h"
16 #include "chrome/browser/password_manager/password_store_factory.h"
17 #include "chrome/browser/profiles/profile_statistics_constants.h"
18 #include "components/bookmarks/browser/bookmark_model.h"
19 #include "components/bookmarks/browser/bookmark_node.h"
20 #include "components/history/core/browser/history_service.h"
21 #include "components/password_manager/core/browser/password_store.h"
22 #include "content/public/browser/browser_thread.h"
23
24 namespace {
25
26 int CountBookmarksFromNode(const bookmarks::BookmarkNode* node) {
27 int count = 0;
28 if (node->is_url()) {
29 ++count;
30 } else {
31 for (int i = 0; i < node->child_count(); ++i)
32 count += CountBookmarksFromNode(node->GetChild(i));
33 }
34 return count;
35 }
36
37 } // namespace
38
39 void ProfileStatisticsAggregator::BookmarkModelHelper::BookmarkModelLoaded(
40 bookmarks::BookmarkModel* model, bool ids_reassigned) {
41 // Remove observer before release, otherwise it may become a dangling
42 // reference.
43 model->RemoveObserver(this);
44 parent_->CountBookmarks(model);
45 parent_->Release();
46 }
47
48 void ProfileStatisticsAggregator::PasswordStoreConsumerHelper::
49 OnGetPasswordStoreResults(ScopedVector<autofill::PasswordForm> results) {
50 parent_->StatisticsCallbackSuccess(
51 profiles::kProfileStatisticsPasswords, results.size());
52 }
53
54 ProfileStatisticsAggregator::ProfileStatisticsAggregator(
55 Profile* profile, const profiles::ProfileStatisticsCallback& callback)
56 : profile_(profile),
57 profile_path_(profile_->GetPath()),
58 password_store_consumer_helper_(this) {
59 ProfileStatistics::RegisterAggregator(profile_, this);
60 AddCallbackAndStartAggregator(callback);
61 }
62
63 ProfileStatisticsAggregator::~ProfileStatisticsAggregator() {
64 ProfileStatistics::DeregisterAggregator(profile_);
65 }
66
67 size_t ProfileStatisticsAggregator::GetCallbackCount() {
68 return callbacks_.size();
69 }
70
71 void ProfileStatisticsAggregator::AddCallbackAndStartAggregator(
72 const profiles::ProfileStatisticsCallback& callback) {
73 if (!callback.is_null())
74 callbacks_.push_back(callback);
75 StartAggregator();
76 }
77
78 void ProfileStatisticsAggregator::StartAggregator() {
79 DCHECK(g_browser_process->profile_manager()->IsValidProfile(profile_));
80 profile_category_stats_.clear();
81
82 // Try to cancel tasks from task trackers.
83 tracker_.TryCancelAll();
84 password_store_consumer_helper_.cancelable_task_tracker()->TryCancelAll();
85
86 // Initiate bookmark counting (async).
87 tracker_.PostTask(
88 content::BrowserThread::GetMessageLoopProxyForThread(
89 content::BrowserThread::UI).get(),
90 FROM_HERE,
91 base::Bind(&ProfileStatisticsAggregator::WaitOrCountBookmarks, this));
92
93 // Initiate history counting (async).
94 history::HistoryService* history_service =
95 HistoryServiceFactory::GetForProfileWithoutCreating(profile_);
96
97 if (history_service) {
98 history_service->GetHistoryCount(
99 base::Time(),
100 base::Time::Max(),
101 base::Bind(&ProfileStatisticsAggregator::StatisticsCallbackHistory,
102 this),
103 &tracker_);
104 } else {
105 StatisticsCallbackFailure(profiles::kProfileStatisticsBrowsingHistory);
106 }
107
108 // Initiate stored password counting (async).
109 scoped_refptr<password_manager::PasswordStore> password_store =
110 PasswordStoreFactory::GetForProfile(
111 profile_, ServiceAccessType::EXPLICIT_ACCESS);
112 if (password_store) {
113 password_store->GetAutofillableLogins(&password_store_consumer_helper_);
114 } else {
115 StatisticsCallbackFailure(profiles::kProfileStatisticsPasswords);
116 }
117
118 // Initiate preference counting (async).
119 tracker_.PostTaskAndReplyWithResult(
120 content::BrowserThread::GetMessageLoopProxyForThread(
121 content::BrowserThread::UI).get(),
122 FROM_HERE,
123 base::Bind(&ProfileStatisticsAggregator::CountPrefs, this),
124 base::Bind(&ProfileStatisticsAggregator::StatisticsCallback,
125 this, profiles::kProfileStatisticsSettings));
126 }
127
128 void ProfileStatisticsAggregator::StatisticsCallback(
129 const char* category, ProfileStatValue result) {
130 profiles::ProfileCategoryStat datum;
131 datum.category = category;
132 datum.count = result.count;
133 datum.success = result.success;
134 profile_category_stats_.push_back(datum);
135 for (const auto& callback : callbacks_) {
136 DCHECK(!callback.is_null());
137 callback.Run(profile_category_stats_);
138 }
139
140 if (result.success) {
141 ProfileStatistics::SetProfileStatisticsInCache(
142 profile_path_, datum.category, result.count);
143 }
144 }
145
146 void ProfileStatisticsAggregator::StatisticsCallbackSuccess(
147 const char* category, int count) {
148 ProfileStatValue result;
149 result.count = count;
150 result.success = true;
151 StatisticsCallback(category, result);
152 }
153
154 void ProfileStatisticsAggregator::StatisticsCallbackFailure(
155 const char* category) {
156 ProfileStatValue result;
157 result.count = 0;
158 result.success = false;
159 StatisticsCallback(category, result);
160 }
161
162 void ProfileStatisticsAggregator::StatisticsCallbackHistory(
163 history::HistoryCountResult result) {
164 ProfileStatValue result_converted;
165 result_converted.count = result.count;
166 result_converted.success = result.success;
167 StatisticsCallback(profiles::kProfileStatisticsBrowsingHistory,
168 result_converted);
169 }
170
171 void ProfileStatisticsAggregator::CountBookmarks(
172 bookmarks::BookmarkModel* bookmark_model) {
173 int count = CountBookmarksFromNode(bookmark_model->bookmark_bar_node()) +
174 CountBookmarksFromNode(bookmark_model->other_node()) +
175 CountBookmarksFromNode(bookmark_model->mobile_node());
176
177 StatisticsCallbackSuccess(profiles::kProfileStatisticsBookmarks, count);
178 }
179
180 void ProfileStatisticsAggregator::WaitOrCountBookmarks() {
181 DCHECK(g_browser_process->profile_manager()->IsValidProfile(profile_));
182
183 bookmarks::BookmarkModel* bookmark_model =
184 BookmarkModelFactory::GetForProfileIfExists(profile_);
185
186 if (bookmark_model) {
187 if (bookmark_model->loaded()) {
188 CountBookmarks(bookmark_model);
189 } else {
190 AddRef();
191 bookmark_model_helper_.reset(new BookmarkModelHelper(this));
192 bookmark_model->AddObserver(bookmark_model_helper_.get());
193 }
194 } else {
195 StatisticsCallbackFailure(profiles::kProfileStatisticsBookmarks);
196 }
197 }
198
199 ProfileStatisticsAggregator::ProfileStatValue
200 ProfileStatisticsAggregator::CountPrefs() const {
201 const PrefService* pref_service = profile_->GetPrefs();
202
203 ProfileStatValue result;
204 if (pref_service) {
205 scoped_ptr<base::DictionaryValue> prefs =
206 pref_service->GetPreferenceValuesWithoutPathExpansion();
207
208 int count = 0;
209 for (base::DictionaryValue::Iterator it(*(prefs.get()));
210 !it.IsAtEnd(); it.Advance()) {
211 const PrefService::Preference* pref = pref_service->
212 FindPreference(it.key());
213 // Skip all dictionaries (which must be empty by the function call above).
214 if (it.value().GetType() != base::Value::TYPE_DICTIONARY &&
215 pref && pref->IsUserControlled() && !pref->IsDefaultValue()) {
216 ++count;
217 }
218 }
219
220 result.count = count;
221 result.success = true;
222 } else {
223 result.count = 0;
224 result.success = false;
225 }
226 return result;
227 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698