OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/profiles/profile_statistics.h" | 5 #include "chrome/browser/profiles/profile_statistics.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/memory/ref_counted.h" | 8 #include "base/memory/ref_counted.h" |
9 #include "base/prefs/pref_service.h" | 9 #include "base/prefs/pref_service.h" |
10 #include "base/task_runner.h" | 10 #include "base/task_runner.h" |
11 #include "base/time/time.h" | 11 #include "base/time/time.h" |
12 #include "chrome/browser/bookmarks/bookmark_model_factory.h" | 12 #include "chrome/browser/bookmarks/bookmark_model_factory.h" |
| 13 #include "chrome/browser/browser_process.h" |
13 #include "chrome/browser/history/history_service_factory.h" | 14 #include "chrome/browser/history/history_service_factory.h" |
14 #include "chrome/browser/password_manager/password_store_factory.h" | 15 #include "chrome/browser/password_manager/password_store_factory.h" |
| 16 #include "chrome/browser/profiles/profile_attributes_entry.h" |
| 17 #include "chrome/browser/profiles/profile_attributes_storage.h" |
| 18 #include "chrome/browser/profiles/profile_info_cache.h" |
| 19 #include "chrome/browser/profiles/profile_manager.h" |
15 #include "components/bookmarks/browser/bookmark_model.h" | 20 #include "components/bookmarks/browser/bookmark_model.h" |
16 #include "components/history/core/browser/history_service.h" | 21 #include "components/history/core/browser/history_service.h" |
17 #include "components/password_manager/core/browser/password_store.h" | 22 #include "components/password_manager/core/browser/password_store.h" |
18 #include "components/password_manager/core/browser/password_store_consumer.h" | 23 #include "components/password_manager/core/browser/password_store_consumer.h" |
19 #include "content/public/browser/browser_thread.h" | 24 #include "content/public/browser/browser_thread.h" |
20 | 25 |
21 using content::BrowserThread; | 26 using content::BrowserThread; |
22 | 27 |
23 namespace { | 28 namespace { |
24 | 29 |
25 struct ProfileStatValue { | 30 struct ProfileStatValue { |
26 int count; | 31 int count; |
27 bool success; // false means the statistics failed to load | 32 bool success; // false means the statistics failed to load |
28 }; | 33 }; |
29 | 34 |
| 35 void NoOp(profiles::ProfileCategoryStats result) {} |
| 36 |
30 int CountBookmarksFromNode(const bookmarks::BookmarkNode* node) { | 37 int CountBookmarksFromNode(const bookmarks::BookmarkNode* node) { |
31 int count = 0; | 38 int count = 0; |
32 if (node->is_url()) { | 39 if (node->is_url()) { |
33 ++count; | 40 ++count; |
34 } else { | 41 } else { |
35 for (int i = 0; i < node->child_count(); ++i) | 42 for (int i = 0; i < node->child_count(); ++i) |
36 count += CountBookmarksFromNode(node->GetChild(i)); | 43 count += CountBookmarksFromNode(node->GetChild(i)); |
37 } | 44 } |
38 return count; | 45 return count; |
39 } | 46 } |
40 | 47 |
41 class ProfileStatisticsAggregator | 48 class ProfileStatisticsAggregator |
42 : public base::RefCountedThreadSafe<ProfileStatisticsAggregator> { | 49 : public base::RefCountedThreadSafe<ProfileStatisticsAggregator> { |
43 // This class collects statistical information about the profile and returns | 50 // This class is used internally by GetProfileStatistics and |
| 51 // StoreProfileStatisticsToCache. |
| 52 // |
| 53 // The class collects statistical information about the profile and returns |
44 // the information via a callback function. Currently bookmarks, history, | 54 // the information via a callback function. Currently bookmarks, history, |
45 // logins and preferences are counted. | 55 // logins and preferences are counted. |
46 // | 56 // |
47 // The class is RefCounted because this is needed for CancelableTaskTracker | 57 // The class is RefCounted because this is needed for CancelableTaskTracker |
48 // to function properly. Once all tasks are run (or cancelled) the instance is | 58 // to function properly. Once all tasks are run (or cancelled) the instance is |
49 // automatically destructed. | 59 // automatically destructed. |
50 // | |
51 // The class is used internally by GetProfileStatistics function. | |
52 | 60 |
53 public: | 61 public: |
54 explicit ProfileStatisticsAggregator(Profile* profile, | 62 ProfileStatisticsAggregator(Profile* profile, |
55 const profiles::ProfileStatisticsCallback& callback, | 63 const profiles::ProfileStatisticsCallback& callback, |
56 base::CancelableTaskTracker* tracker); | 64 base::CancelableTaskTracker* tracker); |
57 | 65 |
58 private: | 66 private: |
59 friend class base::RefCountedThreadSafe<ProfileStatisticsAggregator>; | 67 friend class base::RefCountedThreadSafe<ProfileStatisticsAggregator>; |
60 ~ProfileStatisticsAggregator() {} | 68 ~ProfileStatisticsAggregator() {} |
61 | 69 |
62 void Init(); | 70 void Init(); |
63 | 71 |
64 // Callback functions | 72 // Callback functions |
(...skipping 14 matching lines...) Expand all Loading... |
79 ProfileStatValue CountPrefs() const; | 87 ProfileStatValue CountPrefs() const; |
80 | 88 |
81 Profile* profile_; | 89 Profile* profile_; |
82 profiles::ProfileCategoryStats profile_category_stats_; | 90 profiles::ProfileCategoryStats profile_category_stats_; |
83 | 91 |
84 // Callback function to be called when results arrive. Will be called | 92 // Callback function to be called when results arrive. Will be called |
85 // multiple times (once for each statistics). | 93 // multiple times (once for each statistics). |
86 const profiles::ProfileStatisticsCallback callback_; | 94 const profiles::ProfileStatisticsCallback callback_; |
87 | 95 |
88 base::CancelableTaskTracker* tracker_; | 96 base::CancelableTaskTracker* tracker_; |
| 97 scoped_ptr<base::CancelableTaskTracker> default_tracker_; |
89 | 98 |
90 // Password counting. | 99 // Password counting. |
91 class PasswordStoreConsumerHelper | 100 class PasswordStoreConsumerHelper |
92 : public password_manager::PasswordStoreConsumer { | 101 : public password_manager::PasswordStoreConsumer { |
93 public: | 102 public: |
94 explicit PasswordStoreConsumerHelper(ProfileStatisticsAggregator* parent) | 103 explicit PasswordStoreConsumerHelper(ProfileStatisticsAggregator* parent) |
95 : parent_(parent) {} | 104 : parent_(parent) {} |
96 | 105 |
97 void OnGetPasswordStoreResults( | 106 void OnGetPasswordStoreResults( |
98 ScopedVector<autofill::PasswordForm> results) override { | 107 ScopedVector<autofill::PasswordForm> results) override { |
(...skipping 12 matching lines...) Expand all Loading... |
111 }; | 120 }; |
112 | 121 |
113 ProfileStatisticsAggregator::ProfileStatisticsAggregator( | 122 ProfileStatisticsAggregator::ProfileStatisticsAggregator( |
114 Profile* profile, | 123 Profile* profile, |
115 const profiles::ProfileStatisticsCallback& callback, | 124 const profiles::ProfileStatisticsCallback& callback, |
116 base::CancelableTaskTracker* tracker) | 125 base::CancelableTaskTracker* tracker) |
117 : profile_(profile), | 126 : profile_(profile), |
118 callback_(callback), | 127 callback_(callback), |
119 tracker_(tracker), | 128 tracker_(tracker), |
120 password_store_consumer_helper_(this) { | 129 password_store_consumer_helper_(this) { |
| 130 if (!tracker_) { |
| 131 default_tracker_.reset(new base::CancelableTaskTracker); |
| 132 tracker_ = default_tracker_.get(); |
| 133 } |
121 Init(); | 134 Init(); |
122 } | 135 } |
123 | 136 |
124 void ProfileStatisticsAggregator::Init() { | 137 void ProfileStatisticsAggregator::Init() { |
| 138 DCHECK(profile_); |
| 139 |
125 // Initiate bookmark counting (async). Post to UI thread. | 140 // Initiate bookmark counting (async). Post to UI thread. |
126 tracker_->PostTaskAndReplyWithResult( | 141 tracker_->PostTaskAndReplyWithResult( |
127 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI).get(), | 142 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI).get(), |
128 FROM_HERE, | 143 FROM_HERE, |
129 base::Bind(&ProfileStatisticsAggregator::CountBookmarks, this), | 144 base::Bind(&ProfileStatisticsAggregator::CountBookmarks, this), |
130 base::Bind(&ProfileStatisticsAggregator::StatisticsCallback, | 145 base::Bind(&ProfileStatisticsAggregator::StatisticsCallback, |
131 this, profiles::kProfileStatisticsBookmarks)); | 146 this, profiles::kProfileStatisticsBookmarks)); |
132 | 147 |
133 // Initiate history counting (async). | 148 // Initiate history counting (async). |
134 history::HistoryService* history_service = | 149 history::HistoryService* history_service = |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
166 } | 181 } |
167 | 182 |
168 void ProfileStatisticsAggregator::StatisticsCallback( | 183 void ProfileStatisticsAggregator::StatisticsCallback( |
169 const char* category, ProfileStatValue result) { | 184 const char* category, ProfileStatValue result) { |
170 profiles::ProfileCategoryStat datum; | 185 profiles::ProfileCategoryStat datum; |
171 datum.category = category; | 186 datum.category = category; |
172 datum.count = result.count; | 187 datum.count = result.count; |
173 datum.success = result.success; | 188 datum.success = result.success; |
174 profile_category_stats_.push_back(datum); | 189 profile_category_stats_.push_back(datum); |
175 callback_.Run(profile_category_stats_); | 190 callback_.Run(profile_category_stats_); |
| 191 |
| 192 // Set statistic in ProfileInfoCache if possible. |
| 193 // Note: if local_state() is null, profile_manager() will seg-fault. |
| 194 if (result.success && g_browser_process && g_browser_process->local_state()) { |
| 195 ProfileInfoCache& profile_info_cache = |
| 196 g_browser_process->profile_manager()->GetProfileInfoCache(); |
| 197 ProfileAttributesEntry* entry = nullptr; |
| 198 if (profile_info_cache.GetProfileAttributesWithPath( |
| 199 profile_->GetPath(), &entry)) { |
| 200 entry->SetStatistic(datum.category, result.count); |
| 201 } |
| 202 } |
176 } | 203 } |
177 | 204 |
178 void ProfileStatisticsAggregator::StatisticsCallbackSuccess( | 205 void ProfileStatisticsAggregator::StatisticsCallbackSuccess( |
179 const char* category, int count) { | 206 const char* category, int count) { |
180 ProfileStatValue result; | 207 ProfileStatValue result; |
181 result.count = count; | 208 result.count = count; |
182 result.success = true; | 209 result.success = true; |
183 StatisticsCallback(category, result); | 210 StatisticsCallback(category, result); |
184 } | 211 } |
185 | 212 |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
244 result.success = false; | 271 result.success = false; |
245 } | 272 } |
246 return result; | 273 return result; |
247 } | 274 } |
248 | 275 |
249 } // namespace | 276 } // namespace |
250 | 277 |
251 namespace profiles { | 278 namespace profiles { |
252 | 279 |
253 // Constants for the categories in ProfileCategoryStats | 280 // Constants for the categories in ProfileCategoryStats |
254 const char kProfileStatisticsBrowsingHistory[] = "BrowsingHistory"; | 281 const char kProfileStatisticsBrowsingHistory[] = "browsing_history"; |
255 const char kProfileStatisticsPasswords[] = "Passwords"; | 282 const char kProfileStatisticsPasswords[] = "passwords"; |
256 const char kProfileStatisticsBookmarks[] = "Bookmarks"; | 283 const char kProfileStatisticsBookmarks[] = "bookmarks"; |
257 const char kProfileStatisticsSettings[] = "Settings"; | 284 const char kProfileStatisticsSettings[] = "settings"; |
258 | 285 |
259 void GetProfileStatistics(Profile* profile, | 286 void GetProfileStatistics(Profile* profile, |
260 const ProfileStatisticsCallback& callback, | 287 const ProfileStatisticsCallback& callback, |
261 base::CancelableTaskTracker* tracker) { | 288 base::CancelableTaskTracker* tracker) { |
262 scoped_refptr<ProfileStatisticsAggregator> aggregator = | 289 scoped_refptr<ProfileStatisticsAggregator> aggregator = |
263 new ProfileStatisticsAggregator(profile, callback, tracker); | 290 new ProfileStatisticsAggregator(profile, callback, tracker); |
264 } | 291 } |
265 | 292 |
| 293 void StoreProfileStatisticsToCache(Profile* profile) { |
| 294 GetProfileStatistics(profile, base::Bind(&NoOp), nullptr); |
| 295 } |
| 296 |
266 } // namespace profiles | 297 } // namespace profiles |
OLD | NEW |