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

Side by Side Diff: chrome/browser/profiles/profile_statistics.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
1 // Copyright 2015 The Chromium Authors. All rights reserved. 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 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.h" 5 #include "chrome/browser/profiles/profile_statistics.h"
6 6
7 #include <set>
8
9 #include "base/bind.h"
10 #include "base/macros.h" 7 #include "base/macros.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/prefs/pref_service.h"
13 #include "base/task_runner.h"
14 #include "base/time/time.h"
15 #include "chrome/browser/bookmarks/bookmark_model_factory.h"
16 #include "chrome/browser/browser_process.h" 8 #include "chrome/browser/browser_process.h"
17 #include "chrome/browser/history/history_service_factory.h" 9 #include "chrome/browser/profiles/profile.h"
18 #include "chrome/browser/password_manager/password_store_factory.h"
19 #include "chrome/browser/profiles/profile_attributes_entry.h" 10 #include "chrome/browser/profiles/profile_attributes_entry.h"
20 #include "chrome/browser/profiles/profile_attributes_storage.h" 11 #include "chrome/browser/profiles/profile_attributes_storage.h"
21 #include "chrome/browser/profiles/profile_manager.h" 12 #include "chrome/browser/profiles/profile_manager.h"
22 #include "components/bookmarks/browser/bookmark_model.h" 13 #include "chrome/browser/profiles/profile_statistics_aggregator.h"
23 #include "components/bookmarks/browser/bookmark_model_observer.h" 14 #include "chrome/browser/profiles/profile_statistics_constants.h"
24 #include "components/history/core/browser/history_service.h"
25 #include "components/password_manager/core/browser/password_store.h"
26 #include "components/password_manager/core/browser/password_store_consumer.h"
27 #include "content/public/browser/browser_thread.h"
28 15
29 namespace { 16 // static
30 17 void ProfileStatistics::GatherProfileStatistics(Profile* profile,
31 struct ProfileStatValue { 18 const profiles::ProfileStatisticsCallback& callback) {
32 int count;
33 bool success; // false means the statistics failed to load
34 };
35
36 int CountBookmarksFromNode(const bookmarks::BookmarkNode* node) {
37 int count = 0;
38 if (node->is_url()) {
39 ++count;
40 } else {
41 for (int i = 0; i < node->child_count(); ++i)
42 count += CountBookmarksFromNode(node->GetChild(i));
43 }
44 return count;
45 }
46
47 class ProfileStatisticsAggregator
48 : public base::RefCountedThreadSafe<ProfileStatisticsAggregator> {
49 // This class is used internally by GetProfileStatistics and
50 // StoreProfileStatisticsToCache.
51 //
52 // The class collects statistical information about the profile and returns
53 // the information via a callback function. Currently bookmarks, history,
54 // logins and preferences are counted.
55 //
56 // The class is RefCounted because this is needed for CancelableTaskTracker
57 // to function properly. Once all tasks are run (or cancelled) the instance is
58 // automatically destructed.
59
60 public:
61 ProfileStatisticsAggregator(Profile* profile,
62 const profiles::ProfileStatisticsCallback& callback,
63 base::CancelableTaskTracker* tracker);
64
65 private:
66 friend class base::RefCountedThreadSafe<ProfileStatisticsAggregator>;
67 ~ProfileStatisticsAggregator() {}
68
69 void Init();
70
71 // Callback functions
72 // Normal callback. Appends result to |profile_category_stats_|, and then call
73 // the external callback. All other callbacks call this function.
74 void StatisticsCallback(const char* category, ProfileStatValue result);
75 // Callback for reporting success.
76 void StatisticsCallbackSuccess(const char* category, int count);
77 // Callback for reporting failure.
78 void StatisticsCallbackFailure(const char* category);
79 // Callback for history.
80 void StatisticsCallbackHistory(history::HistoryCountResult result);
81
82 // Bookmark counting.
83 void WaitOrCountBookmarks();
84 void CountBookmarks(bookmarks::BookmarkModel* bookmark_model);
85
86 class BookmarkModelHelper
87 : public bookmarks::BookmarkModelObserver {
88 public:
89 explicit BookmarkModelHelper(ProfileStatisticsAggregator* parent)
90 : parent_(parent) {}
91
92 void BookmarkModelLoaded(bookmarks::BookmarkModel* model,
93 bool ids_reassigned)
94 override {
95 // Remove observer before release, otherwise it may become a dangling
96 // reference.
97 model->RemoveObserver(this);
98 parent_->CountBookmarks(model);
99 parent_->Release();
100 }
101
102 void BookmarkNodeMoved(bookmarks::BookmarkModel* model,
103 const bookmarks::BookmarkNode* old_parent,
104 int old_index,
105 const bookmarks::BookmarkNode* new_parent,
106 int new_index) override {}
107
108 void BookmarkNodeAdded(bookmarks::BookmarkModel* model,
109 const bookmarks::BookmarkNode* parent,
110 int index) override {}
111
112 void BookmarkNodeRemoved(bookmarks::BookmarkModel* model,
113 const bookmarks::BookmarkNode* parent,
114 int old_index, const bookmarks::BookmarkNode* node,
115 const std::set<GURL>& no_longer_bookmarked) override {}
116
117 void BookmarkNodeChanged(bookmarks::BookmarkModel* model,
118 const bookmarks::BookmarkNode* node) override {}
119
120 void BookmarkNodeFaviconChanged(bookmarks::BookmarkModel* model,
121 const bookmarks::BookmarkNode* node) override {}
122
123 void BookmarkNodeChildrenReordered(bookmarks::BookmarkModel* model,
124 const bookmarks::BookmarkNode* node) override {}
125
126 void BookmarkAllUserNodesRemoved(bookmarks::BookmarkModel* model,
127 const std::set<GURL>& removed_urls) override {}
128
129 private:
130 ProfileStatisticsAggregator* parent_ = nullptr;
131 };
132
133 // Password counting
134 class PasswordStoreConsumerHelper
135 : public password_manager::PasswordStoreConsumer {
136 public:
137 explicit PasswordStoreConsumerHelper(ProfileStatisticsAggregator* parent)
138 : parent_(parent) {}
139
140 void OnGetPasswordStoreResults(
141 ScopedVector<autofill::PasswordForm> results) override {
142 parent_->StatisticsCallbackSuccess(profiles::kProfileStatisticsPasswords,
143 results.size());
144 }
145
146 private:
147 ProfileStatisticsAggregator* parent_ = nullptr;
148
149 DISALLOW_COPY_AND_ASSIGN(PasswordStoreConsumerHelper);
150 };
151
152 // Preference counting.
153 ProfileStatValue CountPrefs() const;
154
155 Profile* profile_;
156 profiles::ProfileCategoryStats profile_category_stats_;
157
158 // Callback function to be called when results arrive. Will be called
159 // multiple times (once for each statistics).
160 const profiles::ProfileStatisticsCallback callback_;
161
162 base::CancelableTaskTracker* tracker_;
163 scoped_ptr<base::CancelableTaskTracker> default_tracker_;
164
165 // Bookmark counting
166 scoped_ptr<BookmarkModelHelper> bookmark_model_helper_;
167
168 // Password counting.
169 PasswordStoreConsumerHelper password_store_consumer_helper_;
170
171 DISALLOW_COPY_AND_ASSIGN(ProfileStatisticsAggregator);
172 };
173
174 ProfileStatisticsAggregator::ProfileStatisticsAggregator(
175 Profile* profile,
176 const profiles::ProfileStatisticsCallback& callback,
177 base::CancelableTaskTracker* tracker)
178 : profile_(profile),
179 callback_(callback),
180 tracker_(tracker),
181 password_store_consumer_helper_(this) {
182 if (!tracker_) {
183 default_tracker_.reset(new base::CancelableTaskTracker);
184 tracker_ = default_tracker_.get();
185 }
186 Init();
187 }
188
189 void ProfileStatisticsAggregator::Init() {
190 DCHECK(profile_);
191
192 // Initiate bookmark counting (async). Post to UI thread.
193 tracker_->PostTask(
194 content::BrowserThread::GetMessageLoopProxyForThread(
195 content::BrowserThread::UI).get(),
196 FROM_HERE,
197 base::Bind(&ProfileStatisticsAggregator::WaitOrCountBookmarks, this));
198
199 // Initiate history counting (async).
200 history::HistoryService* history_service =
201 HistoryServiceFactory::GetForProfileWithoutCreating(profile_);
202
203 if (history_service) {
204 history_service->GetHistoryCount(
205 base::Time(),
206 base::Time::Max(),
207 base::Bind(&ProfileStatisticsAggregator::StatisticsCallbackHistory,
208 this),
209 tracker_);
210 } else {
211 StatisticsCallbackFailure(profiles::kProfileStatisticsBrowsingHistory);
212 }
213
214 // Initiate stored password counting (async).
215 // TODO(anthonyvd): make password task cancellable.
216 scoped_refptr<password_manager::PasswordStore> password_store =
217 PasswordStoreFactory::GetForProfile(
218 profile_, ServiceAccessType::EXPLICIT_ACCESS);
219 if (password_store) {
220 password_store->GetAutofillableLogins(&password_store_consumer_helper_);
221 } else {
222 StatisticsCallbackFailure(profiles::kProfileStatisticsPasswords);
223 }
224
225 // Initiate preference counting (async). Post to UI thread.
226 tracker_->PostTaskAndReplyWithResult(
227 content::BrowserThread::GetMessageLoopProxyForThread(
228 content::BrowserThread::UI).get(),
229 FROM_HERE,
230 base::Bind(&ProfileStatisticsAggregator::CountPrefs, this),
231 base::Bind(&ProfileStatisticsAggregator::StatisticsCallback,
232 this, profiles::kProfileStatisticsSettings));
233 }
234
235 void ProfileStatisticsAggregator::StatisticsCallback(
236 const char* category, ProfileStatValue result) {
237 profiles::ProfileCategoryStat datum;
238 datum.category = category;
239 datum.count = result.count;
240 datum.success = result.success;
241 profile_category_stats_.push_back(datum);
242 if (!callback_.is_null())
243 callback_.Run(profile_category_stats_);
244
245 if (result.success) {
246 profiles::SetProfileStatisticsInCache(profile_->GetPath(), datum.category,
247 result.count);
248 }
249 }
250
251 void ProfileStatisticsAggregator::StatisticsCallbackSuccess(
252 const char* category, int count) {
253 ProfileStatValue result;
254 result.count = count;
255 result.success = true;
256 StatisticsCallback(category, result);
257 }
258
259 void ProfileStatisticsAggregator::StatisticsCallbackFailure(
260 const char* category) {
261 ProfileStatValue result;
262 result.count = 0;
263 result.success = false;
264 StatisticsCallback(category, result);
265 }
266
267 void ProfileStatisticsAggregator::StatisticsCallbackHistory(
268 history::HistoryCountResult result) {
269 ProfileStatValue result_converted;
270 result_converted.count = result.count;
271 result_converted.success = result.success;
272 StatisticsCallback(profiles::kProfileStatisticsBrowsingHistory,
273 result_converted);
274 }
275
276 void ProfileStatisticsAggregator::CountBookmarks(
277 bookmarks::BookmarkModel* bookmark_model) {
278 int count = CountBookmarksFromNode(bookmark_model->bookmark_bar_node()) +
279 CountBookmarksFromNode(bookmark_model->other_node()) +
280 CountBookmarksFromNode(bookmark_model->mobile_node());
281
282 StatisticsCallbackSuccess(profiles::kProfileStatisticsBookmarks, count);
283 }
284
285 void ProfileStatisticsAggregator::WaitOrCountBookmarks() {
286 bookmarks::BookmarkModel* bookmark_model =
287 BookmarkModelFactory::GetForProfileIfExists(profile_);
288
289 if (bookmark_model) {
290 if (bookmark_model->loaded()) {
291 CountBookmarks(bookmark_model);
292 } else {
293 AddRef();
294 bookmark_model_helper_.reset(new BookmarkModelHelper(this));
295 bookmark_model->AddObserver(bookmark_model_helper_.get());
296 }
297 } else {
298 StatisticsCallbackFailure(profiles::kProfileStatisticsBookmarks);
299 }
300 }
301
302 ProfileStatValue ProfileStatisticsAggregator::CountPrefs() const {
303 const PrefService* pref_service = profile_->GetPrefs();
304
305 ProfileStatValue result;
306 if (pref_service) {
307 scoped_ptr<base::DictionaryValue> prefs =
308 pref_service->GetPreferenceValuesWithoutPathExpansion();
309
310 int count = 0;
311 for (base::DictionaryValue::Iterator it(*(prefs.get()));
312 !it.IsAtEnd(); it.Advance()) {
313 const PrefService::Preference* pref = pref_service->
314 FindPreference(it.key());
315 // Skip all dictionaries (which must be empty by the function call above).
316 if (it.value().GetType() != base::Value::TYPE_DICTIONARY &&
317 pref && pref->IsUserControlled() && !pref->IsDefaultValue()) {
318 ++count;
319 }
320 }
321
322 result.count = count;
323 result.success = true;
324 } else {
325 result.count = 0;
326 result.success = false;
327 }
328 return result;
329 }
330
331 } // namespace
332
333 namespace profiles {
334
335 // Constants for the categories in ProfileCategoryStats
336 const char kProfileStatisticsBrowsingHistory[] = "BrowsingHistory";
337 const char kProfileStatisticsPasswords[] = "Passwords";
338 const char kProfileStatisticsBookmarks[] = "Bookmarks";
339 const char kProfileStatisticsSettings[] = "Settings";
340
341 void GatherProfileStatistics(Profile* profile,
342 const ProfileStatisticsCallback& callback,
343 base::CancelableTaskTracker* tracker) {
344 DCHECK(profile); 19 DCHECK(profile);
345 if (profile->IsOffTheRecord() || profile->IsSystemProfile()) { 20 if (profile->IsOffTheRecord() || profile->IsSystemProfile()) {
346 NOTREACHED(); 21 NOTREACHED();
347 return; 22 return;
348 } 23 }
349 24
350 scoped_refptr<ProfileStatisticsAggregator> aggregator = 25 if (HasAggregator(profile)) {
351 new ProfileStatisticsAggregator(profile, callback, tracker); 26 GetAggregator(profile)->AddCallbackAndStartAggregator(callback);
27 } else {
28 scoped_refptr<ProfileStatisticsAggregator> aggregator =
29 new ProfileStatisticsAggregator(profile, callback);
30 }
352 } 31 }
353 32
354 ProfileCategoryStats GetProfileStatisticsFromCache( 33 // static
34 profiles::ProfileCategoryStats ProfileStatistics::GetProfileStatisticsFromCache(
355 const base::FilePath& profile_path) { 35 const base::FilePath& profile_path) {
356 ProfileAttributesEntry* entry = nullptr; 36 ProfileAttributesEntry* entry = nullptr;
357 bool has_entry = g_browser_process->profile_manager()-> 37 bool has_entry = g_browser_process->profile_manager()->
358 GetProfileAttributesStorage(). 38 GetProfileAttributesStorage().
359 GetProfileAttributesWithPath(profile_path, &entry); 39 GetProfileAttributesWithPath(profile_path, &entry);
360 40
361 ProfileCategoryStats stats; 41 profiles::ProfileCategoryStats stats;
362 ProfileCategoryStat stat; 42 profiles::ProfileCategoryStat stat;
363 43
364 stat.category = kProfileStatisticsBrowsingHistory; 44 stat.category = profiles::kProfileStatisticsBrowsingHistory;
365 stat.success = has_entry ? entry->HasStatsBrowsingHistory() : false; 45 stat.success = has_entry ? entry->HasStatsBrowsingHistory() : false;
366 stat.count = stat.success ? entry->GetStatsBrowsingHistory() : 0; 46 stat.count = stat.success ? entry->GetStatsBrowsingHistory() : 0;
367 stats.push_back(stat); 47 stats.push_back(stat);
368 48
369 stat.category = kProfileStatisticsPasswords; 49 stat.category = profiles::kProfileStatisticsPasswords;
370 stat.success = has_entry ? entry->HasStatsPasswords() : false; 50 stat.success = has_entry ? entry->HasStatsPasswords() : false;
371 stat.count = stat.success ? entry->GetStatsPasswords() : 0; 51 stat.count = stat.success ? entry->GetStatsPasswords() : 0;
372 stats.push_back(stat); 52 stats.push_back(stat);
373 53
374 stat.category = kProfileStatisticsBookmarks; 54 stat.category = profiles::kProfileStatisticsBookmarks;
375 stat.success = has_entry ? entry->HasStatsBookmarks() : false; 55 stat.success = has_entry ? entry->HasStatsBookmarks() : false;
376 stat.count = stat.success ? entry->GetStatsBookmarks() : 0; 56 stat.count = stat.success ? entry->GetStatsBookmarks() : 0;
377 stats.push_back(stat); 57 stats.push_back(stat);
378 58
379 stat.category = kProfileStatisticsSettings; 59 stat.category = profiles::kProfileStatisticsSettings;
380 stat.success = has_entry ? entry->HasStatsSettings() : false; 60 stat.success = has_entry ? entry->HasStatsSettings() : false;
381 stat.count = stat.success ? entry->GetStatsSettings() : 0; 61 stat.count = stat.success ? entry->GetStatsSettings() : 0;
382 stats.push_back(stat); 62 stats.push_back(stat);
383 63
384 return stats; 64 return stats;
385 } 65 }
386 66
387 void SetProfileStatisticsInCache(const base::FilePath& profile_path, 67 // static
388 const std::string& category, int count) { 68 void ProfileStatistics::SetProfileStatisticsInCache(
69 const base::FilePath& profile_path,
70 const std::string& category,
71 int count) {
389 // If local_state() is null, profile_manager() will seg-fault. 72 // If local_state() is null, profile_manager() will seg-fault.
390 if (!g_browser_process || !g_browser_process->local_state()) 73 if (!g_browser_process || !g_browser_process->local_state())
391 return; 74 return;
392 75
393 // profile_manager() may return a null pointer. 76 // profile_manager() may return a null pointer.
394 ProfileManager* profile_manager = g_browser_process->profile_manager(); 77 ProfileManager* profile_manager = g_browser_process->profile_manager();
395 if (!profile_manager) 78 if (!profile_manager)
396 return; 79 return;
397 80
398 ProfileAttributesEntry* entry = nullptr; 81 ProfileAttributesEntry* entry = nullptr;
399 if (!profile_manager->GetProfileAttributesStorage(). 82 if (!profile_manager->GetProfileAttributesStorage().
400 GetProfileAttributesWithPath(profile_path, &entry)) 83 GetProfileAttributesWithPath(profile_path, &entry))
401 return; 84 return;
402 85
403 if (category == kProfileStatisticsBrowsingHistory) { 86 if (category == profiles::kProfileStatisticsBrowsingHistory) {
404 entry->SetStatsBrowsingHistory(count); 87 entry->SetStatsBrowsingHistory(count);
405 } else if (category == kProfileStatisticsPasswords) { 88 } else if (category == profiles::kProfileStatisticsPasswords) {
406 entry->SetStatsPasswords(count); 89 entry->SetStatsPasswords(count);
407 } else if (category == kProfileStatisticsBookmarks) { 90 } else if (category == profiles::kProfileStatisticsBookmarks) {
408 entry->SetStatsBookmarks(count); 91 entry->SetStatsBookmarks(count);
409 } else if (category == kProfileStatisticsSettings) { 92 } else if (category == profiles::kProfileStatisticsSettings) {
410 entry->SetStatsSettings(count); 93 entry->SetStatsSettings(count);
411 } else { 94 } else {
412 NOTREACHED(); 95 NOTREACHED();
413 } 96 }
414 } 97 }
415 98
416 } // namespace profiles 99 // static
100 std::map<void*, ProfileStatisticsAggregator*>
101 ProfileStatistics::aggregators_ = {};
102
103 // static
104 bool ProfileStatistics::HasAggregator(void* profile) {
105 return aggregators_.count(profile);
106 }
107
108 // static
109 ProfileStatisticsAggregator* const ProfileStatistics::GetAggregator(
110 void* profile) {
111 auto it = aggregators_.find(profile);
112 if (it == aggregators_.end())
113 return nullptr;
114 return it->second;
115 }
116
117 // static
118 void ProfileStatistics::RegisterAggregator(void* profile,
119 ProfileStatisticsAggregator* aggregator) {
120 aggregators_[profile] = aggregator;
121 }
122
123 // static
124 void ProfileStatistics::DeregisterAggregator(void* profile) {
125 auto it = aggregators_.find(profile);
126 if (it != aggregators_.end())
127 aggregators_.erase(it);
128 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698