OLD | NEW |
---|---|
(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 class ProfileStatisticsAggregator | |
25 : public base::RefCountedThreadSafe<ProfileStatisticsAggregator> { | |
26 // This class collect 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 ProfileStatisticsCallback& callback, | |
40 base::CancelableTaskTracker* tracker); | |
41 | |
42 private: | |
43 // Destructor | |
44 friend class base::RefCountedThreadSafe<ProfileStatisticsAggregator>; | |
45 ~ProfileStatisticsAggregator() {} | |
46 | |
47 // Private variables | |
48 Profile* profile_; | |
49 ProfileStatisticsValues profile_stats_values_; | |
50 | |
51 // Callback function to be called when results arrive. Will be called | |
52 // multiple times (once for each statistics). | |
53 const ProfileStatisticsCallback callback_; | |
54 | |
55 base::CancelableTaskTracker* tracker_; | |
56 | |
57 // Initialization. Called by constructors. | |
58 void Init(); | |
59 | |
60 // Internal callback. Appends result to profile_stats_values_, and then | |
61 // call the external callback. | |
62 void StatisticsCallback(std::string category, int count); | |
63 | |
64 // Bookmark counting. | |
65 static int CountURLsFromNode(const bookmarks::BookmarkNode* node); | |
66 int CountURLs() const; | |
67 | |
68 // Preference counting. | |
69 int CountPrefs() const; | |
70 | |
71 // Password counting | |
72 class PasswordStoreConsumerHelper | |
73 : public password_manager::PasswordStoreConsumer { | |
74 public: | |
75 explicit PasswordStoreConsumerHelper(ProfileStatisticsAggregator* parent) | |
76 : parent_(parent) {} | |
Mike Lerman
2015/08/05 19:26:09
nit: add a newline below
lwchkg
2015/08/06 04:32:58
Acknowledged.
| |
77 void OnGetPasswordStoreResults( | |
78 ScopedVector<autofill::PasswordForm> results) override { | |
79 parent_->StatisticsCallback("Passwords", results.size()); | |
80 } | |
81 private: | |
Mike Lerman
2015/08/05 19:26:09
newline above private. Also, DISALLOW_COPY_AND_ASS
lwchkg
2015/08/06 04:32:58
Acknowledged.
| |
82 ProfileStatisticsAggregator* parent_; | |
83 }; | |
84 PasswordStoreConsumerHelper password_store_consumer_helper_; | |
85 | |
86 DISALLOW_COPY_AND_ASSIGN(ProfileStatisticsAggregator); | |
87 }; | |
88 | |
89 ProfileStatisticsAggregator::ProfileStatisticsAggregator(Profile* profile, | |
90 const ProfileStatisticsCallback& callback, | |
91 base::CancelableTaskTracker* tracker) | |
92 : profile_(profile), callback_(callback), tracker_(tracker), | |
Mike Lerman
2015/08/05 19:26:09
nit: 1 initialization per line
lwchkg
2015/08/06 04:32:58
Acknowledged.
| |
93 password_store_consumer_helper_(this) { | |
94 Init(); | |
95 } | |
96 | |
97 void ProfileStatisticsAggregator::Init() { | |
98 // Initiate bookmark counting (async). Post to UI thread. | |
99 tracker_->PostTaskAndReplyWithResult( | |
100 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI).get(), | |
101 FROM_HERE, | |
102 base::Bind(&ProfileStatisticsAggregator::CountURLs, this), | |
103 base::Bind(&ProfileStatisticsAggregator::StatisticsCallback, | |
104 this, "Bookmarks")); | |
Mike Lerman
2015/08/05 19:26:09
all these string parameters to StatisticsCallback
lwchkg
2015/08/06 04:32:58
Acknowledged.
| |
105 | |
106 // Initiate history counting (async). | |
107 history::HistoryService* history_service = | |
108 HistoryServiceFactory::GetForProfileWithoutCreating(profile_); | |
109 | |
110 if (history_service) { | |
111 history_service->CountURLs( | |
112 base::Bind(&ProfileStatisticsAggregator::StatisticsCallback, | |
113 this, "BrowsingHistory"), | |
114 tracker_); | |
115 } else { | |
116 StatisticsCallback("BrowsingHistory", 0); | |
Mike Lerman
2015/08/05 19:26:09
If there's no history service, does that necessari
lwchkg
2015/08/06 04:32:58
For chrome://user-manager the absence of history i
Mike Lerman
2015/08/07 19:37:12
There are a variety of strange profiles - while yo
lwchkg
2015/08/10 12:03:37
I see. Then I'd like to keep it here (and everywhe
Mike Lerman
2015/08/13 14:56:33
My preference, rather than using special values, w
lwchkg
2015/08/13 16:20:17
Sounds good to me. I'll submit a revision tomorrow
| |
117 } | |
118 | |
119 // Initiate stored password counting (async). | |
120 // TODO(lwchkg): make password task cancellable. | |
Mike Lerman
2015/08/05 19:26:09
I think all TODOs should be assigned to @chromium
lwchkg
2015/08/06 04:32:58
Acknowledged. Anyway, by design password tasks are
| |
121 scoped_refptr<password_manager::PasswordStore> password_store = | |
122 PasswordStoreFactory::GetForProfile( | |
123 profile_, ServiceAccessType::EXPLICIT_ACCESS); | |
124 password_store->GetAutofillableLogins(&password_store_consumer_helper_); | |
125 | |
126 // Initiate preference counting (async). Post to UI thread. | |
127 tracker_->PostTaskAndReplyWithResult( | |
128 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI).get(), | |
129 FROM_HERE, | |
130 base::Bind(&ProfileStatisticsAggregator::CountPrefs, this), | |
131 base::Bind(&ProfileStatisticsAggregator::StatisticsCallback, | |
132 this, "Settings")); | |
133 } | |
134 | |
135 void ProfileStatisticsAggregator::StatisticsCallback(std::string category, | |
136 int count) { | |
137 profile_stats_values_.push_back(std::make_pair(category, count)); | |
138 callback_.Run(profile_stats_values_); | |
139 } | |
140 | |
141 int ProfileStatisticsAggregator::CountURLsFromNode( | |
142 const bookmarks::BookmarkNode* node) { | |
143 int count = 0; | |
144 if (node->is_url()) { | |
145 ++count; | |
146 } else { | |
147 for (int i = 0; i < node->child_count(); ++i) | |
148 count += CountURLsFromNode(node->GetChild(i)); | |
149 } | |
150 return count; | |
151 } | |
152 | |
153 int ProfileStatisticsAggregator::CountURLs() const { | |
Mike Lerman
2015/08/05 19:26:09
since this method counts bookmarks, could we call
lwchkg
2015/08/06 04:32:58
Yes. Let me change the name.
| |
154 bookmarks::BookmarkModel* bookmark_model = | |
155 BookmarkModelFactory::GetForProfileIfExists(profile_); | |
156 | |
157 if (bookmark_model) { | |
158 return CountURLsFromNode(bookmark_model->bookmark_bar_node()) + | |
159 CountURLsFromNode(bookmark_model->other_node()) + | |
160 CountURLsFromNode(bookmark_model->mobile_node()); | |
161 } else { | |
162 return 0; | |
163 } | |
164 } | |
165 | |
166 int ProfileStatisticsAggregator::CountPrefs() const { | |
167 const PrefService* pref_service = profile_->GetPrefs(); | |
168 | |
169 if (!pref_service) | |
170 return 0; | |
171 | |
172 scoped_ptr<base::DictionaryValue> prefs = | |
173 pref_service->GetPreferenceValuesWithoutPathExpansion(); | |
174 | |
175 int count = 0; | |
176 for (base::DictionaryValue::Iterator it(*(prefs.get())); | |
177 !it.IsAtEnd(); it.Advance()) { | |
178 const PrefService::Preference* pref = pref_service-> | |
179 FindPreference(it.key()); | |
180 // Skip all dictionaries (which must be empty by the function call above). | |
181 if (it.value().GetType() != base::Value::TYPE_DICTIONARY && | |
182 pref && pref->IsUserControlled() && !pref->IsDefaultValue()) { | |
183 ++count; | |
184 } | |
185 } | |
186 return count; | |
187 } | |
188 | |
189 void GetProfileStatistics(Profile* profile, | |
190 const ProfileStatisticsCallback& callback, | |
191 base::CancelableTaskTracker* tracker) { | |
192 new ProfileStatisticsAggregator(profile, callback, tracker); | |
193 } | |
194 | |
195 } // namespace profiles | |
OLD | NEW |