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

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: Sixth draft 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 profiles {
23
24 // Constants for the categories in ProfileStatisticsValues
25 const char kProfileStatisticsBrowsingHistory[] = "BrowsingHistory";
26 const char kProfileStatisticsPasswords[] = "Passwords";
27 const char kProfileStatisticsBookmarks[] = "Bookmarks";
28 const char kProfileStatisticsSettings[] = "Settings";
29
30 class ProfileStatisticsAggregator
lwchkg 2015/08/07 10:43:48 Should I move the class into to anonymous namespac
Mike Lerman 2015/08/07 19:37:12 Yes please. Anything not publicly exposed in the h
lwchkg 2015/08/09 16:11:49 Acknowledged.
31 : public base::RefCountedThreadSafe<ProfileStatisticsAggregator> {
32 // This class collect statistical information about the profile and returns
Mike Lerman 2015/08/07 19:37:12 nit: collect needs an s
lwchkg 2015/08/09 16:11:49 Acknowledged.
33 // the information via a callback function. Currently bookmarks, history,
34 // logins and preferences are counted.
35 //
36 // The class is RefCounted because this is needed for CancelableTaskTracker
37 // to function properly. Once all tasks are run (or cancelled) the instance is
38 // automatically destructed.
39 //
40 // The class is used internally by GetProfileStatistics function.
41
42 public:
43 // Constructor
44 explicit ProfileStatisticsAggregator(Profile* profile,
45 const ProfileStatisticsCallback& callback,
46 base::CancelableTaskTracker* tracker);
47
48 private:
49 // Destructor
50 friend class base::RefCountedThreadSafe<ProfileStatisticsAggregator>;
51 ~ProfileStatisticsAggregator() {}
52
53 // Private variables
Mike Lerman 2015/08/07 19:37:12 nit: don't need to specify private variables in a
lwchkg 2015/08/09 16:11:49 Acknowledged.
54 Profile* profile_;
55 ProfileStatisticsValues profile_stats_values_;
56
57 // Callback function to be called when results arrive. Will be called
58 // multiple times (once for each statistics).
59 const ProfileStatisticsCallback callback_;
60
61 base::CancelableTaskTracker* tracker_;
62
63 // Initialization. Called by constructors.
64 void Init();
65
66 // Internal callback. Appends result to profile_stats_values_, and then
67 // call the external callback.
68 void StatisticsCallback(std::string category, int count);
69
70 // Bookmark counting.
71 static int CountBookmarksFromNode(const bookmarks::BookmarkNode* node);
72 int CountBookmarks() const;
73
74 // Preference counting.
75 int CountPrefs() const;
76
77 // Password counting
78 class PasswordStoreConsumerHelper
79 : public password_manager::PasswordStoreConsumer {
80 public:
81 explicit PasswordStoreConsumerHelper(ProfileStatisticsAggregator* parent)
82 : parent_(parent) {}
83
84 void OnGetPasswordStoreResults(
85 ScopedVector<autofill::PasswordForm> results) override {
86 parent_->StatisticsCallback(kProfileStatisticsPasswords, results.size());
87 }
88
89 private:
90 ProfileStatisticsAggregator* parent_;
91
92 DISALLOW_COPY_AND_ASSIGN(PasswordStoreConsumerHelper);
93 };
94 PasswordStoreConsumerHelper password_store_consumer_helper_;
95
96 DISALLOW_COPY_AND_ASSIGN(ProfileStatisticsAggregator);
97 };
98
99 ProfileStatisticsAggregator::ProfileStatisticsAggregator(Profile* profile,
100 const ProfileStatisticsCallback& callback,
101 base::CancelableTaskTracker* tracker)
102 : profile_(profile),
103 callback_(callback),
104 tracker_(tracker),
105 password_store_consumer_helper_(this) {
106 Init();
107 }
108
109 void ProfileStatisticsAggregator::Init() {
110 // Initiate bookmark counting (async). Post to UI thread.
111 tracker_->PostTaskAndReplyWithResult(
112 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI).get(),
113 FROM_HERE,
114 base::Bind(&ProfileStatisticsAggregator::CountBookmarks, this),
115 base::Bind(&ProfileStatisticsAggregator::StatisticsCallback,
116 this, kProfileStatisticsBookmarks));
117
118 // Initiate history counting (async).
119 history::HistoryService* history_service =
120 HistoryServiceFactory::GetForProfileWithoutCreating(profile_);
121
122 if (history_service) {
123 history_service->CountURLs(
124 base::Bind(&ProfileStatisticsAggregator::StatisticsCallback,
125 this, kProfileStatisticsBrowsingHistory),
126 tracker_);
127 } else {
128 StatisticsCallback(kProfileStatisticsBrowsingHistory, 0);
lwchkg 2015/08/07 10:43:48 Still waiting for reply here. Let's reach a decisi
129 }
130
131 // Initiate stored password counting (async).
132 // TODO(anthonyvd): make password task cancellable.
133 scoped_refptr<password_manager::PasswordStore> password_store =
134 PasswordStoreFactory::GetForProfile(
135 profile_, ServiceAccessType::EXPLICIT_ACCESS);
136 password_store->GetAutofillableLogins(&password_store_consumer_helper_);
137
138 // Initiate preference counting (async). Post to UI thread.
139 tracker_->PostTaskAndReplyWithResult(
140 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI).get(),
141 FROM_HERE,
142 base::Bind(&ProfileStatisticsAggregator::CountPrefs, this),
143 base::Bind(&ProfileStatisticsAggregator::StatisticsCallback,
144 this, kProfileStatisticsSettings));
145 }
146
147 void ProfileStatisticsAggregator::StatisticsCallback(std::string category,
148 int count) {
Mike Lerman 2015/08/07 19:37:12 nit: indent 3 spaces so that "int" is aligned with
lwchkg 2015/08/09 16:11:49 Acknowledged.
149 profile_stats_values_.push_back(std::make_pair(category, count));
150 callback_.Run(profile_stats_values_);
151 }
152
153 int ProfileStatisticsAggregator::CountBookmarksFromNode(
154 const bookmarks::BookmarkNode* node) {
155 int count = 0;
156 if (node->is_url()) {
157 ++count;
158 } else {
159 for (int i = 0; i < node->child_count(); ++i)
160 count += CountBookmarksFromNode(node->GetChild(i));
161 }
162 return count;
163 }
164
165 int ProfileStatisticsAggregator::CountBookmarks() const {
166 bookmarks::BookmarkModel* bookmark_model =
167 BookmarkModelFactory::GetForProfileIfExists(profile_);
168
169 if (bookmark_model) {
170 return CountBookmarksFromNode(bookmark_model->bookmark_bar_node()) +
171 CountBookmarksFromNode(bookmark_model->other_node()) +
172 CountBookmarksFromNode(bookmark_model->mobile_node());
173 }
174 return 0;
175 }
176
177 int ProfileStatisticsAggregator::CountPrefs() const {
178 const PrefService* pref_service = profile_->GetPrefs();
179
180 if (!pref_service)
181 return 0;
182
183 scoped_ptr<base::DictionaryValue> prefs =
184 pref_service->GetPreferenceValuesWithoutPathExpansion();
185
186 int count = 0;
187 for (base::DictionaryValue::Iterator it(*(prefs.get()));
188 !it.IsAtEnd(); it.Advance()) {
189 const PrefService::Preference* pref = pref_service->
190 FindPreference(it.key());
191 // Skip all dictionaries (which must be empty by the function call above).
192 if (it.value().GetType() != base::Value::TYPE_DICTIONARY &&
193 pref && pref->IsUserControlled() && !pref->IsDefaultValue()) {
194 ++count;
195 }
196 }
197 return count;
198 }
199
200 void GetProfileStatistics(Profile* profile,
201 const ProfileStatisticsCallback& callback,
202 base::CancelableTaskTracker* tracker) {
203 new ProfileStatisticsAggregator(profile, callback, tracker);
Mike Lerman 2015/08/07 19:37:12 what deletes this object?
lwchkg 2015/08/09 16:11:49 This object is now RefcountedThreadSafe. This is n
204 }
205
206 } // namespace profiles
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698