Index: chrome/browser/profiles/profile_statistics.cc |
diff --git a/chrome/browser/profiles/profile_statistics.cc b/chrome/browser/profiles/profile_statistics.cc |
index 88b48f933b5e55983e991439d07c35d720664c00..2bc1905ac6d60792e7ee3ceab74377614b204c7c 100644 |
--- a/chrome/browser/profiles/profile_statistics.cc |
+++ b/chrome/browser/profiles/profile_statistics.cc |
@@ -10,8 +10,13 @@ |
#include "base/task_runner.h" |
#include "base/time/time.h" |
#include "chrome/browser/bookmarks/bookmark_model_factory.h" |
+#include "chrome/browser/browser_process.h" |
#include "chrome/browser/history/history_service_factory.h" |
#include "chrome/browser/password_manager/password_store_factory.h" |
+#include "chrome/browser/profiles/profile_attributes_entry.h" |
+#include "chrome/browser/profiles/profile_attributes_storage.h" |
+#include "chrome/browser/profiles/profile_info_cache.h" |
+#include "chrome/browser/profiles/profile_manager.h" |
#include "components/bookmarks/browser/bookmark_model.h" |
#include "components/history/core/browser/history_service.h" |
#include "components/password_manager/core/browser/password_store.h" |
@@ -27,6 +32,8 @@ struct ProfileStatValue { |
bool success; // false means the statistics failed to load |
}; |
+void NoOp(profiles::ProfileCategoryStats result) {} |
+ |
int CountBookmarksFromNode(const bookmarks::BookmarkNode* node) { |
int count = 0; |
if (node->is_url()) { |
@@ -40,18 +47,19 @@ int CountBookmarksFromNode(const bookmarks::BookmarkNode* node) { |
class ProfileStatisticsAggregator |
: public base::RefCountedThreadSafe<ProfileStatisticsAggregator> { |
- // This class collects statistical information about the profile and returns |
+ // This class is used internally by GetProfileStatistics and |
+ // StoreProfileStatisticsToCache. |
+ // |
+ // The class collects statistical information about the profile and returns |
// the information via a callback function. Currently bookmarks, history, |
// logins and preferences are counted. |
// |
// The class is RefCounted because this is needed for CancelableTaskTracker |
// to function properly. Once all tasks are run (or cancelled) the instance is |
// automatically destructed. |
- // |
- // The class is used internally by GetProfileStatistics function. |
public: |
- explicit ProfileStatisticsAggregator(Profile* profile, |
+ ProfileStatisticsAggregator(Profile* profile, |
const profiles::ProfileStatisticsCallback& callback, |
base::CancelableTaskTracker* tracker); |
@@ -86,6 +94,7 @@ class ProfileStatisticsAggregator |
const profiles::ProfileStatisticsCallback callback_; |
base::CancelableTaskTracker* tracker_; |
+ scoped_ptr<base::CancelableTaskTracker> default_tracker_; |
// Password counting. |
class PasswordStoreConsumerHelper |
@@ -118,10 +127,16 @@ ProfileStatisticsAggregator::ProfileStatisticsAggregator( |
callback_(callback), |
tracker_(tracker), |
password_store_consumer_helper_(this) { |
+ if (!tracker_) { |
+ default_tracker_.reset(new base::CancelableTaskTracker); |
+ tracker_ = default_tracker_.get(); |
+ } |
Init(); |
} |
void ProfileStatisticsAggregator::Init() { |
+ DCHECK(profile_); |
+ |
// Initiate bookmark counting (async). Post to UI thread. |
tracker_->PostTaskAndReplyWithResult( |
BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI).get(), |
@@ -173,6 +188,18 @@ void ProfileStatisticsAggregator::StatisticsCallback( |
datum.success = result.success; |
profile_category_stats_.push_back(datum); |
callback_.Run(profile_category_stats_); |
+ |
+ // Set statistic in ProfileInfoCache if possible. |
+ // Note: if local_state() is null, profile_manager() will seg-fault. |
+ if (result.success && g_browser_process && g_browser_process->local_state()) { |
+ ProfileInfoCache& profile_info_cache = |
+ g_browser_process->profile_manager()->GetProfileInfoCache(); |
+ ProfileAttributesEntry* entry = nullptr; |
+ if (profile_info_cache.GetProfileAttributesWithPath( |
+ profile_->GetPath(), &entry)) { |
+ entry->SetStatistic(datum.category, result.count); |
+ } |
+ } |
} |
void ProfileStatisticsAggregator::StatisticsCallbackSuccess( |
@@ -251,10 +278,10 @@ ProfileStatValue ProfileStatisticsAggregator::CountPrefs() const { |
namespace profiles { |
// Constants for the categories in ProfileCategoryStats |
-const char kProfileStatisticsBrowsingHistory[] = "BrowsingHistory"; |
-const char kProfileStatisticsPasswords[] = "Passwords"; |
-const char kProfileStatisticsBookmarks[] = "Bookmarks"; |
-const char kProfileStatisticsSettings[] = "Settings"; |
+const char kProfileStatisticsBrowsingHistory[] = "browsing_history"; |
+const char kProfileStatisticsPasswords[] = "passwords"; |
+const char kProfileStatisticsBookmarks[] = "bookmarks"; |
+const char kProfileStatisticsSettings[] = "settings"; |
void GetProfileStatistics(Profile* profile, |
const ProfileStatisticsCallback& callback, |
@@ -263,4 +290,8 @@ void GetProfileStatistics(Profile* profile, |
new ProfileStatisticsAggregator(profile, callback, tracker); |
} |
+void StoreProfileStatisticsToCache(Profile* profile) { |
+ GetProfileStatistics(profile, base::Bind(&NoOp), nullptr); |
+} |
+ |
} // namespace profiles |