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

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

Issue 1248613003: Issue 501916 : Add data type counts to profile deletion flow (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Eighth draft - added "bool success" to all returns Created 5 years, 4 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.h"
6
7 #include "base/bind.h"
8 #include "base/memory/ref_counted.h"
9 #include "base/prefs/pref_service.h"
10 #include "base/task_runner.h"
11 #include "chrome/browser/bookmarks/bookmark_model_factory.h"
12 #include "chrome/browser/history/history_service_factory.h"
13 #include "chrome/browser/password_manager/password_store_factory.h"
14 #include "components/bookmarks/browser/bookmark_model.h"
15 #include "components/history/core/browser/history_service.h"
16 #include "components/password_manager/core/browser/password_store.h"
17 #include "components/password_manager/core/browser/password_store_consumer.h"
18 #include "content/public/browser/browser_thread.h"
19
20 using content::BrowserThread;
21
22 namespace {
23
24 struct ProfileStatisticsValue {
25 int count;
26 bool success; // false means the statistics failed to load
27 };
28
29 class ProfileStatisticsAggregator
30 : public base::RefCountedThreadSafe<ProfileStatisticsAggregator> {
31 // This class collects statistical information about the profile and returns
32 // the information via a callback function. Currently bookmarks, history,
33 // logins and preferences are counted.
34 //
35 // The class is RefCounted because this is needed for CancelableTaskTracker
36 // to function properly. Once all tasks are run (or cancelled) the instance is
37 // automatically destructed.
38 //
39 // The class is used internally by GetProfileStatistics function.
40
41 public:
42 // Constructor
43 explicit ProfileStatisticsAggregator(Profile* profile,
44 const profiles::ProfileStatisticsCallback& callback,
45 base::CancelableTaskTracker* tracker);
46
47 private:
48 // Destructor
49 friend class base::RefCountedThreadSafe<ProfileStatisticsAggregator>;
50 ~ProfileStatisticsAggregator() {}
51
52 Profile* profile_;
53 profiles::ProfileStatisticsReturn profile_stats_values_;
54
55 // Callback function to be called when results arrive. Will be called
56 // multiple times (once for each statistics).
57 const profiles::ProfileStatisticsCallback callback_;
58
59 base::CancelableTaskTracker* tracker_;
60
61 // Initialization. Called by constructors.
62 void Init();
63
64 // Callback functions
65 // Normal callback
Mike Lerman 2015/08/17 14:23:38 period at the end.
66 void StatisticsCallback(std::string category, ProfileStatisticsValue result);
Mike Lerman 2015/08/17 14:23:38 If you're going to call the other method "Statisti
lwchkg 2015/08/19 06:19:45 Good idea. This will be used in the password code.
67 // Callback for reporting failure.
68 void StatisticsCallbackFailure(std::string category);
69 // For history callbacks.
70 void StatisticsCallbackHistory(history::HistoryCountResult result);
71
72 // Implementation of the callbacks. Appends result to profile_stats_values_,
73 // and then call the external callback.
74 void StatisticsCallbackImpl(
Mike Lerman 2015/08/17 14:23:38 I'm not a fan of the suffix Impl, it seems odd giv
lwchkg 2015/08/19 06:19:45 We still need to have a generic method to handle b
Mike Lerman 2015/08/20 14:52:11 If this were a class I'd go with StatisticsCallbac
lwchkg 2015/08/20 16:06:24 Base sounds good. BTW, I've got a new idea. We ca
Mike Lerman 2015/08/20 16:14:28 sure, sgtm.
75 const std::string& category, const ProfileStatisticsValue& result);
76
77 // Bookmark counting.
78 static int CountBookmarksFromNode(const bookmarks::BookmarkNode* node);
79 ProfileStatisticsValue CountBookmarks() const;
80
81 // Preference counting.
82 ProfileStatisticsValue CountPrefs() const;
83
84 // Password counting
85 class PasswordStoreConsumerHelper
86 : public password_manager::PasswordStoreConsumer {
87 public:
88 explicit PasswordStoreConsumerHelper(ProfileStatisticsAggregator* parent)
89 : parent_(parent) {}
90
91 void OnGetPasswordStoreResults(
92 ScopedVector<autofill::PasswordForm> results) override {
93 ProfileStatisticsValue result;
94 result.count = results.size();
95 result.success = true;
96 parent_->StatisticsCallback(profiles::kProfileStatisticsPasswords,
97 result);
98 }
99
100 private:
101 ProfileStatisticsAggregator* parent_;
102
103 DISALLOW_COPY_AND_ASSIGN(PasswordStoreConsumerHelper);
104 };
105 PasswordStoreConsumerHelper password_store_consumer_helper_;
106
107 DISALLOW_COPY_AND_ASSIGN(ProfileStatisticsAggregator);
108 };
109
110 ProfileStatisticsAggregator::ProfileStatisticsAggregator(
111 Profile* profile,
112 const profiles::ProfileStatisticsCallback& callback,
113 base::CancelableTaskTracker* tracker)
114 : profile_(profile),
115 callback_(callback),
116 tracker_(tracker),
117 password_store_consumer_helper_(this) {
118 Init();
119 }
120
121 void ProfileStatisticsAggregator::Init() {
122 // Initiate bookmark counting (async). Post to UI thread.
123 tracker_->PostTaskAndReplyWithResult(
124 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI).get(),
125 FROM_HERE,
126 base::Bind(&ProfileStatisticsAggregator::CountBookmarks, this),
127 base::Bind(&ProfileStatisticsAggregator::StatisticsCallback,
128 this, profiles::kProfileStatisticsBookmarks));
129
130 // Initiate history counting (async).
131 history::HistoryService* history_service =
132 HistoryServiceFactory::GetForProfileWithoutCreating(profile_);
133
134 if (history_service) {
135 history_service->GetHistoryCount(
136 base::Bind(&ProfileStatisticsAggregator::StatisticsCallbackHistory,
137 this),
138 tracker_);
139 } else {
140 StatisticsCallbackFailure(profiles::kProfileStatisticsBrowsingHistory);
141 }
142
143 // Initiate stored password counting (async).
144 // TODO(anthonyvd): make password task cancellable.
145 scoped_refptr<password_manager::PasswordStore> password_store =
146 PasswordStoreFactory::GetForProfile(
147 profile_, ServiceAccessType::EXPLICIT_ACCESS);
148 if (password_store) {
149 password_store->GetAutofillableLogins(&password_store_consumer_helper_);
150 } else {
151 StatisticsCallbackFailure(profiles::kProfileStatisticsPasswords);
152 }
153
154 // Initiate preference counting (async). Post to UI thread.
155 tracker_->PostTaskAndReplyWithResult(
156 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI).get(),
157 FROM_HERE,
158 base::Bind(&ProfileStatisticsAggregator::CountPrefs, this),
159 base::Bind(&ProfileStatisticsAggregator::StatisticsCallback,
160 this, profiles::kProfileStatisticsSettings));
161 }
162
163 void ProfileStatisticsAggregator::StatisticsCallback(
164 std::string category, ProfileStatisticsValue result) {
165 StatisticsCallbackImpl(category, result);
166 }
167
168 void ProfileStatisticsAggregator::StatisticsCallbackFailure(
169 std::string category) {
170 ProfileStatisticsValue result;
171 result.count = 0;
172 result.success = false;
173 StatisticsCallbackImpl(category, result);
174 }
175
176 void ProfileStatisticsAggregator::StatisticsCallbackHistory(
177 history::HistoryCountResult result) {
178 ProfileStatisticsValue result_converted;
179 result_converted.count = result.count;
180 result_converted.success = result.success;
181 StatisticsCallbackImpl(profiles::kProfileStatisticsBrowsingHistory,
182 result_converted);
183 }
184
185 void ProfileStatisticsAggregator::StatisticsCallbackImpl(
186 const std::string& category,
187 const ProfileStatisticsValue& result) {
188 profiles::ProfileStatisticsDatum datum;
189 datum.category = category;
190 datum.count = result.count;
191 datum.success = result.success;
192 profile_stats_values_.push_back(datum);
193 callback_.Run(profile_stats_values_);
194 }
195
196 int ProfileStatisticsAggregator::CountBookmarksFromNode(
197 const bookmarks::BookmarkNode* node) {
198 int count = 0;
199 if (node->is_url()) {
200 ++count;
201 } else {
202 for (int i = 0; i < node->child_count(); ++i)
203 count += CountBookmarksFromNode(node->GetChild(i));
204 }
205 return count;
206 }
207
208 ProfileStatisticsValue ProfileStatisticsAggregator::CountBookmarks() const {
209 bookmarks::BookmarkModel* bookmark_model =
210 BookmarkModelFactory::GetForProfileIfExists(profile_);
211
212 ProfileStatisticsValue result;
213 if (bookmark_model) {
214 result.count = CountBookmarksFromNode(bookmark_model->bookmark_bar_node()) +
215 CountBookmarksFromNode(bookmark_model->other_node()) +
216 CountBookmarksFromNode(bookmark_model->mobile_node());
217 result.success = true;
218 } else {
219 result.count = 0;
220 result.success = false;
221 }
222 return result;
223 }
224
225 ProfileStatisticsValue ProfileStatisticsAggregator::CountPrefs() const {
226 const PrefService* pref_service = profile_->GetPrefs();
227
228 ProfileStatisticsValue result;
229 if (pref_service) {
230 scoped_ptr<base::DictionaryValue> prefs =
231 pref_service->GetPreferenceValuesWithoutPathExpansion();
232
233 int count = 0;
234 for (base::DictionaryValue::Iterator it(*(prefs.get()));
235 !it.IsAtEnd(); it.Advance()) {
236 const PrefService::Preference* pref = pref_service->
237 FindPreference(it.key());
238 // Skip all dictionaries (which must be empty by the function call above).
239 if (it.value().GetType() != base::Value::TYPE_DICTIONARY &&
240 pref && pref->IsUserControlled() && !pref->IsDefaultValue()) {
241 ++count;
242 }
243 }
244
245 result.count = count;
246 result.success = true;
247 } else {
248 result.count = 0;
249 result.success = false;
250 }
251 return result;
252 }
253
254 } // namespace
255
256 namespace profiles {
257
258 // Constants for the categories in ProfileStatisticsReturn
259 const char kProfileStatisticsBrowsingHistory[] = "BrowsingHistory";
260 const char kProfileStatisticsPasswords[] = "Passwords";
261 const char kProfileStatisticsBookmarks[] = "Bookmarks";
262 const char kProfileStatisticsSettings[] = "Settings";
263
264 void GetProfileStatistics(Profile* profile,
265 const ProfileStatisticsCallback& callback,
266 base::CancelableTaskTracker* tracker) {
267 scoped_refptr<ProfileStatisticsAggregator> aggregator =
268 new ProfileStatisticsAggregator(profile, callback, tracker);
269 }
270
271 } // namespace profiles
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698