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 <set> | |
7 #include "base/bind.h" | 8 #include "base/bind.h" |
8 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
9 #include "base/prefs/pref_service.h" | 10 #include "base/prefs/pref_service.h" |
10 #include "base/task_runner.h" | 11 #include "base/task_runner.h" |
11 #include "base/time/time.h" | 12 #include "base/time/time.h" |
12 #include "chrome/browser/bookmarks/bookmark_model_factory.h" | 13 #include "chrome/browser/bookmarks/bookmark_model_factory.h" |
13 #include "chrome/browser/browser_process.h" | 14 #include "chrome/browser/browser_process.h" |
14 #include "chrome/browser/history/history_service_factory.h" | 15 #include "chrome/browser/history/history_service_factory.h" |
15 #include "chrome/browser/password_manager/password_store_factory.h" | 16 #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_entry.h" |
17 #include "chrome/browser/profiles/profile_attributes_storage.h" | 18 #include "chrome/browser/profiles/profile_attributes_storage.h" |
18 #include "chrome/browser/profiles/profile_info_cache.h" | 19 #include "chrome/browser/profiles/profile_info_cache.h" |
19 #include "chrome/browser/profiles/profile_manager.h" | 20 #include "chrome/browser/profiles/profile_manager.h" |
20 #include "components/bookmarks/browser/bookmark_model.h" | 21 #include "components/bookmarks/browser/bookmark_model.h" |
22 #include "components/bookmarks/browser/bookmark_model_observer.h" | |
21 #include "components/history/core/browser/history_service.h" | 23 #include "components/history/core/browser/history_service.h" |
22 #include "components/password_manager/core/browser/password_store.h" | 24 #include "components/password_manager/core/browser/password_store.h" |
23 #include "components/password_manager/core/browser/password_store_consumer.h" | 25 #include "components/password_manager/core/browser/password_store_consumer.h" |
24 #include "content/public/browser/browser_thread.h" | 26 #include "content/public/browser/browser_thread.h" |
25 | 27 |
26 using content::BrowserThread; | |
27 | |
28 namespace { | 28 namespace { |
29 | 29 |
30 struct ProfileStatValue { | 30 struct ProfileStatValue { |
31 int count; | 31 int count; |
32 bool success; // false means the statistics failed to load | 32 bool success; // false means the statistics failed to load |
33 }; | 33 }; |
34 | 34 |
35 int CountBookmarksFromNode(const bookmarks::BookmarkNode* node) { | 35 int CountBookmarksFromNode(const bookmarks::BookmarkNode* node) { |
36 int count = 0; | 36 int count = 0; |
37 if (node->is_url()) { | 37 if (node->is_url()) { |
38 ++count; | 38 ++count; |
39 } else { | 39 } else { |
40 for (int i = 0; i < node->child_count(); ++i) | 40 for (int i = 0; i < node->child_count(); ++i) |
41 count += CountBookmarksFromNode(node->GetChild(i)); | 41 count += CountBookmarksFromNode(node->GetChild(i)); |
42 } | 42 } |
43 return count; | 43 return count; |
44 } | 44 } |
45 | 45 |
46 class ProfileStatisticsAggregator | 46 class ProfileStatisticsAggregator |
47 : public base::RefCountedThreadSafe<ProfileStatisticsAggregator> { | 47 : public base::RefCountedThreadSafe<ProfileStatisticsAggregator> { |
48 // This class collects statistical information about the profile and returns | 48 // This class is used internally by GetProfileStatistics and |
49 // StoreProfileStatisticsToCache. | |
50 // | |
51 // The class collects statistical information about the profile and returns | |
49 // the information via a callback function. Currently bookmarks, history, | 52 // the information via a callback function. Currently bookmarks, history, |
50 // logins and preferences are counted. | 53 // logins and preferences are counted. |
51 // | 54 // |
52 // The class is RefCounted because this is needed for CancelableTaskTracker | 55 // The class is RefCounted because this is needed for CancelableTaskTracker |
53 // to function properly. Once all tasks are run (or cancelled) the instance is | 56 // to function properly. Once all tasks are run (or cancelled) the instance is |
54 // automatically destructed. | 57 // automatically destructed. |
55 // | |
56 // The class is used internally by GetProfileStatistics function. | |
57 | 58 |
58 public: | 59 public: |
59 explicit ProfileStatisticsAggregator(Profile* profile, | 60 ProfileStatisticsAggregator(Profile* profile, |
60 const profiles::ProfileStatisticsCallback& callback, | 61 const profiles::ProfileStatisticsCallback& callback, |
61 base::CancelableTaskTracker* tracker); | 62 base::CancelableTaskTracker* tracker); |
62 | 63 |
63 private: | 64 private: |
64 friend class base::RefCountedThreadSafe<ProfileStatisticsAggregator>; | 65 friend class base::RefCountedThreadSafe<ProfileStatisticsAggregator>; |
65 ~ProfileStatisticsAggregator() {} | 66 ~ProfileStatisticsAggregator() {} |
66 | 67 |
67 void Init(); | 68 void Init(); |
68 | 69 |
69 // Callback functions | 70 // Callback functions |
70 // Normal callback. Appends result to |profile_category_stats_|, and then call | 71 // Normal callback. Appends result to |profile_category_stats_|, and then call |
71 // the external callback. All other callbacks call this function. | 72 // the external callback. All other callbacks call this function. |
72 void StatisticsCallback(const char* category, ProfileStatValue result); | 73 void StatisticsCallback(const char* category, ProfileStatValue result); |
73 // Callback for reporting success. | 74 // Callback for reporting success. |
74 void StatisticsCallbackSuccess(const char* category, int count); | 75 void StatisticsCallbackSuccess(const char* category, int count); |
75 // Callback for reporting failure. | 76 // Callback for reporting failure. |
76 void StatisticsCallbackFailure(const char* category); | 77 void StatisticsCallbackFailure(const char* category); |
77 // Callback for history. | 78 // Callback for history. |
78 void StatisticsCallbackHistory(history::HistoryCountResult result); | 79 void StatisticsCallbackHistory(history::HistoryCountResult result); |
79 | 80 |
80 // Bookmark counting. | 81 // Bookmark counting. |
81 ProfileStatValue CountBookmarks() const; | 82 void WaitOrCountBookmarks(); |
83 void CountBookmarks(bookmarks::BookmarkModel* bookmark_model); | |
82 | 84 |
83 // Preference counting. | 85 // Preference counting. |
84 ProfileStatValue CountPrefs() const; | 86 ProfileStatValue CountPrefs() const; |
85 | 87 |
86 Profile* profile_; | 88 Profile* profile_; |
87 profiles::ProfileCategoryStats profile_category_stats_; | 89 profiles::ProfileCategoryStats profile_category_stats_; |
88 | 90 |
89 // Callback function to be called when results arrive. Will be called | 91 // Callback function to be called when results arrive. Will be called |
90 // multiple times (once for each statistics). | 92 // multiple times (once for each statistics). |
91 const profiles::ProfileStatisticsCallback callback_; | 93 const profiles::ProfileStatisticsCallback callback_; |
92 | 94 |
93 base::CancelableTaskTracker* tracker_; | 95 base::CancelableTaskTracker* tracker_; |
96 scoped_ptr<base::CancelableTaskTracker> default_tracker_; | |
97 | |
98 // Bookmark counting | |
99 class BookmarkModelHelper | |
100 : public bookmarks::BookmarkModelObserver { | |
101 public: | |
102 explicit BookmarkModelHelper(ProfileStatisticsAggregator* parent) | |
103 : parent_(parent) {} | |
104 | |
105 void BookmarkModelLoaded(bookmarks::BookmarkModel* model, | |
106 bool ids_reassigned) | |
107 override { | |
108 // Remove observer before release, otherwise it may become a dangling | |
109 // reference. | |
110 model->RemoveObserver(this); | |
111 parent_->CountBookmarks(model); | |
112 parent_->Release(); | |
113 } | |
114 | |
115 void BookmarkNodeMoved(bookmarks::BookmarkModel* model, | |
Mike Lerman
2015/12/07 16:21:20
nit: when more than 2 lines, one per line, please.
| |
116 const bookmarks::BookmarkNode* old_parent, int old_index, | |
117 const bookmarks::BookmarkNode* new_parent, int new_index) override {} | |
118 | |
119 void BookmarkNodeAdded(bookmarks::BookmarkModel* model, | |
120 const bookmarks::BookmarkNode* parent, | |
121 int index) override {} | |
122 | |
123 void BookmarkNodeRemoved(bookmarks::BookmarkModel* model, | |
124 const bookmarks::BookmarkNode* parent, | |
125 int old_index, const bookmarks::BookmarkNode* node, | |
126 const std::set<GURL>& no_longer_bookmarked) override {} | |
127 | |
128 void BookmarkNodeChanged(bookmarks::BookmarkModel* model, | |
129 const bookmarks::BookmarkNode* node) override {} | |
130 | |
131 void BookmarkNodeFaviconChanged(bookmarks::BookmarkModel* model, | |
132 const bookmarks::BookmarkNode* node) override {} | |
133 | |
134 void BookmarkNodeChildrenReordered(bookmarks::BookmarkModel* model, | |
135 const bookmarks::BookmarkNode* node) override {} | |
136 | |
137 void BookmarkAllUserNodesRemoved(bookmarks::BookmarkModel* model, | |
138 const std::set<GURL>& removed_urls) override {} | |
139 | |
140 | |
141 private: | |
142 ProfileStatisticsAggregator* parent_ = nullptr; | |
143 }; | |
144 scoped_ptr<BookmarkModelHelper> bookmark_model_helper_; | |
94 | 145 |
95 // Password counting. | 146 // Password counting. |
96 class PasswordStoreConsumerHelper | 147 class PasswordStoreConsumerHelper |
97 : public password_manager::PasswordStoreConsumer { | 148 : public password_manager::PasswordStoreConsumer { |
98 public: | 149 public: |
99 explicit PasswordStoreConsumerHelper(ProfileStatisticsAggregator* parent) | 150 explicit PasswordStoreConsumerHelper(ProfileStatisticsAggregator* parent) |
100 : parent_(parent) {} | 151 : parent_(parent) {} |
101 | 152 |
102 void OnGetPasswordStoreResults( | 153 void OnGetPasswordStoreResults( |
103 ScopedVector<autofill::PasswordForm> results) override { | 154 ScopedVector<autofill::PasswordForm> results) override { |
(...skipping 12 matching lines...) Expand all Loading... | |
116 }; | 167 }; |
117 | 168 |
118 ProfileStatisticsAggregator::ProfileStatisticsAggregator( | 169 ProfileStatisticsAggregator::ProfileStatisticsAggregator( |
119 Profile* profile, | 170 Profile* profile, |
120 const profiles::ProfileStatisticsCallback& callback, | 171 const profiles::ProfileStatisticsCallback& callback, |
121 base::CancelableTaskTracker* tracker) | 172 base::CancelableTaskTracker* tracker) |
122 : profile_(profile), | 173 : profile_(profile), |
123 callback_(callback), | 174 callback_(callback), |
124 tracker_(tracker), | 175 tracker_(tracker), |
125 password_store_consumer_helper_(this) { | 176 password_store_consumer_helper_(this) { |
177 if (!tracker_) { | |
178 default_tracker_.reset(new base::CancelableTaskTracker); | |
179 tracker_ = default_tracker_.get(); | |
180 } | |
126 Init(); | 181 Init(); |
127 } | 182 } |
128 | 183 |
129 void ProfileStatisticsAggregator::Init() { | 184 void ProfileStatisticsAggregator::Init() { |
185 DCHECK(profile_); | |
186 | |
130 // Initiate bookmark counting (async). Post to UI thread. | 187 // Initiate bookmark counting (async). Post to UI thread. |
131 tracker_->PostTaskAndReplyWithResult( | 188 tracker_->PostTask( |
132 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI).get(), | 189 content::BrowserThread::GetMessageLoopProxyForThread( |
133 FROM_HERE, | 190 content::BrowserThread::UI).get(), |
134 base::Bind(&ProfileStatisticsAggregator::CountBookmarks, this), | 191 FROM_HERE, |
135 base::Bind(&ProfileStatisticsAggregator::StatisticsCallback, | 192 base::Bind(&ProfileStatisticsAggregator::WaitOrCountBookmarks, this)); |
136 this, profiles::kProfileStatisticsBookmarks)); | |
137 | 193 |
138 // Initiate history counting (async). | 194 // Initiate history counting (async). |
139 history::HistoryService* history_service = | 195 history::HistoryService* history_service = |
140 HistoryServiceFactory::GetForProfileWithoutCreating(profile_); | 196 HistoryServiceFactory::GetForProfileWithoutCreating(profile_); |
141 | 197 |
142 if (history_service) { | 198 if (history_service) { |
143 history_service->GetHistoryCount( | 199 history_service->GetHistoryCount( |
144 base::Time(), | 200 base::Time(), |
145 base::Time::Max(), | 201 base::Time::Max(), |
146 base::Bind(&ProfileStatisticsAggregator::StatisticsCallbackHistory, | 202 base::Bind(&ProfileStatisticsAggregator::StatisticsCallbackHistory, |
147 this), | 203 this), |
148 tracker_); | 204 tracker_); |
149 } else { | 205 } else { |
150 StatisticsCallbackFailure(profiles::kProfileStatisticsBrowsingHistory); | 206 StatisticsCallbackFailure(profiles::kProfileStatisticsBrowsingHistory); |
151 } | 207 } |
152 | 208 |
153 // Initiate stored password counting (async). | 209 // Initiate stored password counting (async). |
154 // TODO(anthonyvd): make password task cancellable. | 210 // TODO(anthonyvd): make password task cancellable. |
155 scoped_refptr<password_manager::PasswordStore> password_store = | 211 scoped_refptr<password_manager::PasswordStore> password_store = |
156 PasswordStoreFactory::GetForProfile( | 212 PasswordStoreFactory::GetForProfile( |
157 profile_, ServiceAccessType::EXPLICIT_ACCESS); | 213 profile_, ServiceAccessType::EXPLICIT_ACCESS); |
158 if (password_store) { | 214 if (password_store) { |
159 password_store->GetAutofillableLogins(&password_store_consumer_helper_); | 215 password_store->GetAutofillableLogins(&password_store_consumer_helper_); |
160 } else { | 216 } else { |
161 StatisticsCallbackFailure(profiles::kProfileStatisticsPasswords); | 217 StatisticsCallbackFailure(profiles::kProfileStatisticsPasswords); |
162 } | 218 } |
163 | 219 |
164 // Initiate preference counting (async). Post to UI thread. | 220 // Initiate preference counting (async). Post to UI thread. |
165 tracker_->PostTaskAndReplyWithResult( | 221 tracker_->PostTaskAndReplyWithResult( |
166 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI).get(), | 222 content::BrowserThread::GetMessageLoopProxyForThread( |
167 FROM_HERE, | 223 content::BrowserThread::UI).get(), |
168 base::Bind(&ProfileStatisticsAggregator::CountPrefs, this), | 224 FROM_HERE, |
169 base::Bind(&ProfileStatisticsAggregator::StatisticsCallback, | 225 base::Bind(&ProfileStatisticsAggregator::CountPrefs, this), |
170 this, profiles::kProfileStatisticsSettings)); | 226 base::Bind(&ProfileStatisticsAggregator::StatisticsCallback, |
227 this, profiles::kProfileStatisticsSettings)); | |
171 } | 228 } |
172 | 229 |
173 void ProfileStatisticsAggregator::StatisticsCallback( | 230 void ProfileStatisticsAggregator::StatisticsCallback( |
174 const char* category, ProfileStatValue result) { | 231 const char* category, ProfileStatValue result) { |
175 profiles::ProfileCategoryStat datum; | 232 profiles::ProfileCategoryStat datum; |
176 datum.category = category; | 233 datum.category = category; |
177 datum.count = result.count; | 234 datum.count = result.count; |
178 datum.success = result.success; | 235 datum.success = result.success; |
179 profile_category_stats_.push_back(datum); | 236 profile_category_stats_.push_back(datum); |
180 callback_.Run(profile_category_stats_); | 237 if (!callback_.is_null()) |
238 callback_.Run(profile_category_stats_); | |
239 | |
240 profiles::SetProfileStatisticsInCache(profile_->GetPath(), datum.category, | |
241 result.count); | |
181 } | 242 } |
182 | 243 |
183 void ProfileStatisticsAggregator::StatisticsCallbackSuccess( | 244 void ProfileStatisticsAggregator::StatisticsCallbackSuccess( |
184 const char* category, int count) { | 245 const char* category, int count) { |
185 ProfileStatValue result; | 246 ProfileStatValue result; |
186 result.count = count; | 247 result.count = count; |
187 result.success = true; | 248 result.success = true; |
188 StatisticsCallback(category, result); | 249 StatisticsCallback(category, result); |
189 } | 250 } |
190 | 251 |
191 void ProfileStatisticsAggregator::StatisticsCallbackFailure( | 252 void ProfileStatisticsAggregator::StatisticsCallbackFailure( |
192 const char* category) { | 253 const char* category) { |
193 ProfileStatValue result; | 254 ProfileStatValue result; |
194 result.count = 0; | 255 result.count = 0; |
195 result.success = false; | 256 result.success = false; |
196 StatisticsCallback(category, result); | 257 StatisticsCallback(category, result); |
197 } | 258 } |
198 | 259 |
199 void ProfileStatisticsAggregator::StatisticsCallbackHistory( | 260 void ProfileStatisticsAggregator::StatisticsCallbackHistory( |
200 history::HistoryCountResult result) { | 261 history::HistoryCountResult result) { |
201 ProfileStatValue result_converted; | 262 ProfileStatValue result_converted; |
202 result_converted.count = result.count; | 263 result_converted.count = result.count; |
203 result_converted.success = result.success; | 264 result_converted.success = result.success; |
204 StatisticsCallback(profiles::kProfileStatisticsBrowsingHistory, | 265 StatisticsCallback(profiles::kProfileStatisticsBrowsingHistory, |
205 result_converted); | 266 result_converted); |
206 } | 267 } |
207 | 268 |
208 ProfileStatValue ProfileStatisticsAggregator::CountBookmarks() const { | 269 void ProfileStatisticsAggregator::CountBookmarks( |
270 bookmarks::BookmarkModel* bookmark_model) { | |
271 int count = CountBookmarksFromNode(bookmark_model->bookmark_bar_node()) + | |
272 CountBookmarksFromNode(bookmark_model->other_node()) + | |
273 CountBookmarksFromNode(bookmark_model->mobile_node()); | |
274 | |
275 StatisticsCallbackSuccess(profiles::kProfileStatisticsBookmarks, count); | |
276 } | |
277 | |
278 void ProfileStatisticsAggregator::WaitOrCountBookmarks() { | |
209 bookmarks::BookmarkModel* bookmark_model = | 279 bookmarks::BookmarkModel* bookmark_model = |
210 BookmarkModelFactory::GetForProfileIfExists(profile_); | 280 BookmarkModelFactory::GetForProfileIfExists(profile_); |
211 | 281 |
212 ProfileStatValue result; | |
213 if (bookmark_model) { | 282 if (bookmark_model) { |
214 result.count = CountBookmarksFromNode(bookmark_model->bookmark_bar_node()) + | 283 if (bookmark_model->loaded()) { |
215 CountBookmarksFromNode(bookmark_model->other_node()) + | 284 CountBookmarks(bookmark_model); |
216 CountBookmarksFromNode(bookmark_model->mobile_node()); | 285 } else { |
217 result.success = true; | 286 AddRef(); |
287 bookmark_model_helper_.reset(new BookmarkModelHelper(this)); | |
288 bookmark_model->AddObserver(bookmark_model_helper_.get()); | |
289 } | |
218 } else { | 290 } else { |
219 result.count = 0; | 291 StatisticsCallbackFailure(profiles::kProfileStatisticsBookmarks); |
220 result.success = false; | |
221 } | 292 } |
222 return result; | |
223 } | 293 } |
224 | 294 |
225 ProfileStatValue ProfileStatisticsAggregator::CountPrefs() const { | 295 ProfileStatValue ProfileStatisticsAggregator::CountPrefs() const { |
226 const PrefService* pref_service = profile_->GetPrefs(); | 296 const PrefService* pref_service = profile_->GetPrefs(); |
227 | 297 |
228 ProfileStatValue result; | 298 ProfileStatValue result; |
229 if (pref_service) { | 299 if (pref_service) { |
230 scoped_ptr<base::DictionaryValue> prefs = | 300 scoped_ptr<base::DictionaryValue> prefs = |
231 pref_service->GetPreferenceValuesWithoutPathExpansion(); | 301 pref_service->GetPreferenceValuesWithoutPathExpansion(); |
232 | 302 |
(...skipping 28 matching lines...) Expand all Loading... | |
261 const char kProfileStatisticsBookmarks[] = "Bookmarks"; | 331 const char kProfileStatisticsBookmarks[] = "Bookmarks"; |
262 const char kProfileStatisticsSettings[] = "Settings"; | 332 const char kProfileStatisticsSettings[] = "Settings"; |
263 | 333 |
264 void GetProfileStatistics(Profile* profile, | 334 void GetProfileStatistics(Profile* profile, |
265 const ProfileStatisticsCallback& callback, | 335 const ProfileStatisticsCallback& callback, |
266 base::CancelableTaskTracker* tracker) { | 336 base::CancelableTaskTracker* tracker) { |
267 scoped_refptr<ProfileStatisticsAggregator> aggregator = | 337 scoped_refptr<ProfileStatisticsAggregator> aggregator = |
268 new ProfileStatisticsAggregator(profile, callback, tracker); | 338 new ProfileStatisticsAggregator(profile, callback, tracker); |
269 } | 339 } |
270 | 340 |
341 void StoreProfileStatisticsToCache(Profile* profile) { | |
342 GetProfileStatistics(profile, profiles::ProfileStatisticsCallback(), nullptr); | |
343 } | |
344 | |
271 ProfileCategoryStats GetProfileStatisticsFromCache( | 345 ProfileCategoryStats GetProfileStatisticsFromCache( |
272 const base::FilePath& profile_path) { | 346 const base::FilePath& profile_path) { |
273 ProfileInfoCache& profile_info_cache = | 347 ProfileInfoCache& profile_info_cache = |
274 g_browser_process->profile_manager()->GetProfileInfoCache(); | 348 g_browser_process->profile_manager()->GetProfileInfoCache(); |
275 ProfileAttributesEntry* entry = nullptr; | 349 ProfileAttributesEntry* entry = nullptr; |
276 bool has_entry = profile_info_cache. | 350 bool has_entry = profile_info_cache. |
277 GetProfileAttributesWithPath(profile_path, &entry); | 351 GetProfileAttributesWithPath(profile_path, &entry); |
278 | 352 |
279 ProfileCategoryStats stats; | 353 ProfileCategoryStats stats; |
280 ProfileCategoryStat stat; | 354 ProfileCategoryStat stat; |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
321 } else if (category == kProfileStatisticsBookmarks) { | 395 } else if (category == kProfileStatisticsBookmarks) { |
322 entry->SetStatsBookmarks(count); | 396 entry->SetStatsBookmarks(count); |
323 } else if (category == kProfileStatisticsSettings) { | 397 } else if (category == kProfileStatisticsSettings) { |
324 entry->SetStatsSettings(count); | 398 entry->SetStatsSettings(count); |
325 } else { | 399 } else { |
326 NOTREACHED(); | 400 NOTREACHED(); |
327 } | 401 } |
328 } | 402 } |
329 | 403 |
330 } // namespace profiles | 404 } // namespace profiles |
OLD | NEW |