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

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

Powered by Google App Engine
This is Rietveld 408576698