Chromium Code Reviews| 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 } |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 79 ProfileStatValue CountPrefs() const; | 86 ProfileStatValue CountPrefs() const; |
| 80 | 87 |
| 81 Profile* profile_; | 88 Profile* profile_; |
| 82 profiles::ProfileCategoryStats profile_category_stats_; | 89 profiles::ProfileCategoryStats profile_category_stats_; |
| 83 | 90 |
| 84 // Callback function to be called when results arrive. Will be called | 91 // Callback function to be called when results arrive. Will be called |
| 85 // multiple times (once for each statistics). | 92 // multiple times (once for each statistics). |
| 86 const profiles::ProfileStatisticsCallback callback_; | 93 const profiles::ProfileStatisticsCallback callback_; |
| 87 | 94 |
| 88 base::CancelableTaskTracker* tracker_; | 95 base::CancelableTaskTracker* tracker_; |
| 96 scoped_ptr<base::CancelableTaskTracker> default_tracker_; | |
| 89 | 97 |
| 90 // Password counting. | 98 // Password counting. |
| 91 class PasswordStoreConsumerHelper | 99 class PasswordStoreConsumerHelper |
| 92 : public password_manager::PasswordStoreConsumer { | 100 : public password_manager::PasswordStoreConsumer { |
| 93 public: | 101 public: |
| 94 explicit PasswordStoreConsumerHelper(ProfileStatisticsAggregator* parent) | 102 explicit PasswordStoreConsumerHelper(ProfileStatisticsAggregator* parent) |
| 95 : parent_(parent) {} | 103 : parent_(parent) {} |
| 96 | 104 |
| 97 void OnGetPasswordStoreResults( | 105 void OnGetPasswordStoreResults( |
| 98 ScopedVector<autofill::PasswordForm> results) override { | 106 ScopedVector<autofill::PasswordForm> results) override { |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 111 }; | 119 }; |
| 112 | 120 |
| 113 ProfileStatisticsAggregator::ProfileStatisticsAggregator( | 121 ProfileStatisticsAggregator::ProfileStatisticsAggregator( |
| 114 Profile* profile, | 122 Profile* profile, |
| 115 const profiles::ProfileStatisticsCallback& callback, | 123 const profiles::ProfileStatisticsCallback& callback, |
| 116 base::CancelableTaskTracker* tracker) | 124 base::CancelableTaskTracker* tracker) |
| 117 : profile_(profile), | 125 : profile_(profile), |
| 118 callback_(callback), | 126 callback_(callback), |
| 119 tracker_(tracker), | 127 tracker_(tracker), |
| 120 password_store_consumer_helper_(this) { | 128 password_store_consumer_helper_(this) { |
| 129 if (!tracker_) { | |
| 130 default_tracker_.reset(new base::CancelableTaskTracker); | |
| 131 tracker_ = default_tracker_.get(); | |
| 132 } | |
| 121 Init(); | 133 Init(); |
| 122 } | 134 } |
| 123 | 135 |
| 124 void ProfileStatisticsAggregator::Init() { | 136 void ProfileStatisticsAggregator::Init() { |
| 125 // Initiate bookmark counting (async). Post to UI thread. | 137 // Initiate bookmark counting (async). Post to UI thread. |
| 126 tracker_->PostTaskAndReplyWithResult( | 138 tracker_->PostTaskAndReplyWithResult( |
| 127 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI).get(), | 139 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI).get(), |
| 128 FROM_HERE, | 140 FROM_HERE, |
| 129 base::Bind(&ProfileStatisticsAggregator::CountBookmarks, this), | 141 base::Bind(&ProfileStatisticsAggregator::CountBookmarks, this), |
| 130 base::Bind(&ProfileStatisticsAggregator::StatisticsCallback, | 142 base::Bind(&ProfileStatisticsAggregator::StatisticsCallback, |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 166 } | 178 } |
| 167 | 179 |
| 168 void ProfileStatisticsAggregator::StatisticsCallback( | 180 void ProfileStatisticsAggregator::StatisticsCallback( |
| 169 const char* category, ProfileStatValue result) { | 181 const char* category, ProfileStatValue result) { |
| 170 profiles::ProfileCategoryStat datum; | 182 profiles::ProfileCategoryStat datum; |
| 171 datum.category = category; | 183 datum.category = category; |
| 172 datum.count = result.count; | 184 datum.count = result.count; |
| 173 datum.success = result.success; | 185 datum.success = result.success; |
| 174 profile_category_stats_.push_back(datum); | 186 profile_category_stats_.push_back(datum); |
| 175 callback_.Run(profile_category_stats_); | 187 callback_.Run(profile_category_stats_); |
| 188 | |
| 189 // Set statistic in ProfileInfoCache if possible. | |
| 190 // Note: if local_state() is null, profile_manager() will seg-fault. | |
| 191 if (result.success && g_browser_process && g_browser_process->local_state()) { | |
| 192 ProfileInfoCache& profile_info_cache = | |
|
Mike Lerman
2015/11/03 20:50:42
const ProfileInfoCache& please
lwchkg
2015/11/04 00:11:07
Acknowledged.
lwchkg
2015/11/05 22:21:30
I've tried, but the compiler complains.
e:\chromi
Mike Lerman
2015/11/10 15:45:44
Ah - getProfileAttributesWithPath isn't const. Sor
| |
| 193 g_browser_process->profile_manager()->GetProfileInfoCache(); | |
| 194 if (&profile_info_cache) { | |
|
Mike Lerman
2015/11/03 20:50:42
you can assume if you have a ProfileManager than y
lwchkg
2015/11/04 00:11:07
You're right. Thanks.
| |
| 195 ProfileAttributesEntry* entry = nullptr; | |
| 196 if (profile_info_cache.GetProfileAttributesWithPath( | |
| 197 profile_->GetPath(), &entry)) { | |
| 198 entry->SetStatistic(datum.category, result.count); | |
| 199 } | |
| 200 } | |
| 201 } | |
| 176 } | 202 } |
| 177 | 203 |
| 178 void ProfileStatisticsAggregator::StatisticsCallbackSuccess( | 204 void ProfileStatisticsAggregator::StatisticsCallbackSuccess( |
| 179 const char* category, int count) { | 205 const char* category, int count) { |
| 180 ProfileStatValue result; | 206 ProfileStatValue result; |
| 181 result.count = count; | 207 result.count = count; |
| 182 result.success = true; | 208 result.success = true; |
| 183 StatisticsCallback(category, result); | 209 StatisticsCallback(category, result); |
| 184 } | 210 } |
| 185 | 211 |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 256 const char kProfileStatisticsBookmarks[] = "Bookmarks"; | 282 const char kProfileStatisticsBookmarks[] = "Bookmarks"; |
| 257 const char kProfileStatisticsSettings[] = "Settings"; | 283 const char kProfileStatisticsSettings[] = "Settings"; |
| 258 | 284 |
| 259 void GetProfileStatistics(Profile* profile, | 285 void GetProfileStatistics(Profile* profile, |
| 260 const ProfileStatisticsCallback& callback, | 286 const ProfileStatisticsCallback& callback, |
| 261 base::CancelableTaskTracker* tracker) { | 287 base::CancelableTaskTracker* tracker) { |
| 262 scoped_refptr<ProfileStatisticsAggregator> aggregator = | 288 scoped_refptr<ProfileStatisticsAggregator> aggregator = |
| 263 new ProfileStatisticsAggregator(profile, callback, tracker); | 289 new ProfileStatisticsAggregator(profile, callback, tracker); |
| 264 } | 290 } |
| 265 | 291 |
| 292 void StoreProfileStatisticsToCache(Profile* profile) { | |
| 293 GetProfileStatistics(profile, base::Bind(&NoOp), nullptr); | |
|
Mike Lerman
2015/11/03 20:50:42
I think you can just write profiles::ProfileCatgeo
lwchkg
2015/11/04 00:11:07
Not sure about this. Will try tonight.
lwchkg
2015/11/05 22:21:30
Tried but not succeeded. I've tried to make a lamb
Mike Lerman
2015/11/10 15:45:44
Aha! Found an example:
https://code.google.com/p/c
lwchkg
2015/11/10 18:11:07
Thanks. It's working well!
| |
| 294 } | |
| 295 | |
| 266 } // namespace profiles | 296 } // namespace profiles |
| OLD | NEW |