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 | |
msramek
2015/07/29 13:55:01
Please rename the file to match the class name.
lwchkg
2015/07/29 14:46:05
The class is not exposed to the outside.
Only Get
msramek
2015/07/29 18:10:25
Oh, sorry, I didn't realize it wasn't exposed. The
| |
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 base::CancelableTaskTracker* tracker_; | |
49 Profile* profile_; | |
50 ProfileStatisticsValues profile_stats_values_; | |
51 | |
52 // Callback function to be called when results arrive. Will be called | |
53 // multiple times (once for each statistics). | |
54 const ProfileStatisticsCallback callback_; | |
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 CountURLsFromNode(const bookmarks::BookmarkNode* node); | |
65 int CountURLs() 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 void OnGetPasswordStoreResults( | |
77 ScopedVector<autofill::PasswordForm> results) override { | |
78 parent_->StatisticsCallback("Passwords", results.size()); | |
79 } | |
80 private: | |
81 ProfileStatisticsAggregator* parent_; | |
82 }; | |
83 PasswordStoreConsumerHelper password_store_consumer_helper_; | |
84 | |
85 DISALLOW_COPY_AND_ASSIGN(ProfileStatisticsAggregator); | |
86 }; | |
87 | |
88 ProfileStatisticsAggregator::ProfileStatisticsAggregator(Profile* profile, | |
89 const ProfileStatisticsCallback& callback, | |
90 base::CancelableTaskTracker* tracker) | |
91 : profile_(profile), callback_(callback), tracker_(tracker), | |
92 password_store_consumer_helper_(this) { | |
93 Init(); | |
94 } | |
95 | |
96 void ProfileStatisticsAggregator::Init() { | |
97 // Initiate bookmark counting (async). Post to UI thread. | |
98 tracker_->PostTaskAndReplyWithResult( | |
99 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI).get(), | |
100 FROM_HERE, | |
101 base::Bind(&ProfileStatisticsAggregator::CountURLs, this), | |
102 base::Bind(&ProfileStatisticsAggregator::StatisticsCallback, | |
103 this, "Bookmarks")); | |
104 | |
105 // Initiate history counting (async). | |
106 history::HistoryService* history_service = | |
107 HistoryServiceFactory::GetForProfileWithoutCreating(profile_); | |
108 | |
109 if (history_service) { | |
110 history_service->CountURLs( | |
111 base::Bind(&ProfileStatisticsAggregator::StatisticsCallback, | |
112 this, "BrowsingHistory"), | |
113 tracker_); | |
114 } else { | |
115 StatisticsCallback("BrowsingHistory", 0); | |
116 } | |
117 | |
118 // Initiate stored password counting (async). | |
119 // TODO(lwchkg): make password task cancellable. | |
120 scoped_refptr<password_manager::PasswordStore> password_store = | |
121 PasswordStoreFactory::GetForProfile( | |
122 profile_, ServiceAccessType::EXPLICIT_ACCESS); | |
123 password_store->GetAutofillableLogins(&password_store_consumer_helper_); | |
124 | |
125 // Initiate preference counting (async). Post to UI thread. | |
126 tracker_->PostTaskAndReplyWithResult( | |
127 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI).get(), | |
128 FROM_HERE, | |
129 base::Bind(&ProfileStatisticsAggregator::CountPrefs, this), | |
130 base::Bind(&ProfileStatisticsAggregator::StatisticsCallback, | |
131 this, "Settings")); | |
132 } | |
133 | |
134 void ProfileStatisticsAggregator::StatisticsCallback(std::string category, | |
135 int count) { | |
136 profile_stats_values_.push_back(std::make_pair(category, count)); | |
137 callback_.Run(profile_stats_values_); | |
138 } | |
139 | |
140 int ProfileStatisticsAggregator::CountURLsFromNode( | |
141 const bookmarks::BookmarkNode* node) { | |
142 int count = 0; | |
143 if (node->is_url()) { | |
144 ++count; | |
145 } else { | |
146 for (int i = 0; i < node->child_count(); ++i) | |
147 count += CountURLsFromNode(node->GetChild(i)); | |
148 } | |
149 return count; | |
150 } | |
151 | |
152 int ProfileStatisticsAggregator::CountURLs() const { | |
153 bookmarks::BookmarkModel* bookmark_model = | |
154 BookmarkModelFactory::GetForProfileIfExists(profile_); | |
155 | |
156 if (bookmark_model) { | |
157 return CountURLsFromNode(bookmark_model->bookmark_bar_node()) + | |
158 CountURLsFromNode(bookmark_model->other_node()) + | |
159 CountURLsFromNode(bookmark_model->mobile_node()); | |
160 } else { | |
161 return 0; | |
162 } | |
163 } | |
164 | |
165 int ProfileStatisticsAggregator::CountPrefs() const { | |
166 const PrefService* pref_service = profile_->GetPrefs(); | |
167 | |
168 if (!pref_service) | |
169 return 0; | |
170 | |
171 scoped_ptr<base::DictionaryValue> prefs = | |
172 pref_service->GetPreferenceValuesWithoutPathExpansion(); | |
173 | |
174 int count = 0; | |
175 for (base::DictionaryValue::Iterator it(*(prefs.get())); | |
176 !it.IsAtEnd(); it.Advance()) { | |
177 const PrefService::Preference* pref = pref_service-> | |
178 FindPreference(it.key()); | |
179 // Skip all dictionaries (which must be empty by the function call above). | |
180 if (it.value().GetType() != base::Value::TYPE_DICTIONARY && | |
181 pref && pref->IsUserControlled() && !pref->IsDefaultValue()) { | |
182 ++count; | |
183 } | |
184 } | |
185 return count; | |
186 } | |
187 | |
188 void GetProfileStatistics(Profile* profile, | |
189 const ProfileStatisticsCallback& callback, | |
190 base::CancelableTaskTracker* tracker) { | |
191 new ProfileStatisticsAggregator(profile, callback, tracker); | |
192 } | |
193 | |
194 } // namespace profiles | |
OLD | NEW |