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

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

Powered by Google App Engine
This is Rietveld 408576698