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