Chromium Code Reviews| Index: chrome/browser/profiles/profile_statistics.cc |
| diff --git a/chrome/browser/profiles/profile_statistics.cc b/chrome/browser/profiles/profile_statistics.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..857138b3d511cb06fb0d884a8a77410d746abbec |
| --- /dev/null |
| +++ b/chrome/browser/profiles/profile_statistics.cc |
| @@ -0,0 +1,195 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/profiles/profile_statistics.h" |
| + |
| +#include "base/bind.h" |
| +#include "base/memory/ref_counted.h" |
| +#include "base/prefs/pref_service.h" |
| +#include "base/task_runner.h" |
| +#include "chrome/browser/bookmarks/bookmark_model_factory.h" |
| +#include "chrome/browser/history/history_service_factory.h" |
| +#include "chrome/browser/password_manager/password_store_factory.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" |
| +#include "components/password_manager/core/browser/password_store_consumer.h" |
| +#include "content/public/browser/browser_thread.h" |
| + |
| +using content::BrowserThread; |
| + |
| +namespace profiles { |
| + |
| +class ProfileStatisticsAggregator |
| + : public base::RefCountedThreadSafe<ProfileStatisticsAggregator> { |
| + // This class collect 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: |
| + // Constructor |
| + explicit ProfileStatisticsAggregator(Profile* profile, |
| + const ProfileStatisticsCallback& callback, |
| + base::CancelableTaskTracker* tracker); |
| + |
| + private: |
| + // Destructor |
| + friend class base::RefCountedThreadSafe<ProfileStatisticsAggregator>; |
| + ~ProfileStatisticsAggregator() {} |
| + |
| + // Private variables |
| + Profile* profile_; |
| + ProfileStatisticsValues profile_stats_values_; |
| + |
| + // Callback function to be called when results arrive. Will be called |
| + // multiple times (once for each statistics). |
| + const ProfileStatisticsCallback callback_; |
| + |
| + base::CancelableTaskTracker* tracker_; |
| + |
| + // Initialization. Called by constructors. |
| + void Init(); |
| + |
| + // Internal callback. Appends result to profile_stats_values_, and then |
| + // call the external callback. |
| + void StatisticsCallback(std::string category, int count); |
| + |
| + // Bookmark counting. |
| + static int CountURLsFromNode(const bookmarks::BookmarkNode* node); |
| + int CountURLs() const; |
| + |
| + // Preference counting. |
| + int CountPrefs() const; |
| + |
| + // Password counting |
| + class PasswordStoreConsumerHelper |
| + : public password_manager::PasswordStoreConsumer { |
| + public: |
| + explicit PasswordStoreConsumerHelper(ProfileStatisticsAggregator* parent) |
| + : parent_(parent) {} |
|
Mike Lerman
2015/08/05 19:26:09
nit: add a newline below
lwchkg
2015/08/06 04:32:58
Acknowledged.
|
| + void OnGetPasswordStoreResults( |
| + ScopedVector<autofill::PasswordForm> results) override { |
| + parent_->StatisticsCallback("Passwords", results.size()); |
| + } |
| + private: |
|
Mike Lerman
2015/08/05 19:26:09
newline above private. Also, DISALLOW_COPY_AND_ASS
lwchkg
2015/08/06 04:32:58
Acknowledged.
|
| + ProfileStatisticsAggregator* parent_; |
| + }; |
| + PasswordStoreConsumerHelper password_store_consumer_helper_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(ProfileStatisticsAggregator); |
| +}; |
| + |
| +ProfileStatisticsAggregator::ProfileStatisticsAggregator(Profile* profile, |
| + const ProfileStatisticsCallback& callback, |
| + base::CancelableTaskTracker* tracker) |
| + : profile_(profile), callback_(callback), tracker_(tracker), |
|
Mike Lerman
2015/08/05 19:26:09
nit: 1 initialization per line
lwchkg
2015/08/06 04:32:58
Acknowledged.
|
| + password_store_consumer_helper_(this) { |
| + Init(); |
| +} |
| + |
| +void ProfileStatisticsAggregator::Init() { |
| + // Initiate bookmark counting (async). Post to UI thread. |
| + tracker_->PostTaskAndReplyWithResult( |
| + BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI).get(), |
| + FROM_HERE, |
| + base::Bind(&ProfileStatisticsAggregator::CountURLs, this), |
| + base::Bind(&ProfileStatisticsAggregator::StatisticsCallback, |
| + this, "Bookmarks")); |
|
Mike Lerman
2015/08/05 19:26:09
all these string parameters to StatisticsCallback
lwchkg
2015/08/06 04:32:58
Acknowledged.
|
| + |
| + // Initiate history counting (async). |
| + history::HistoryService* history_service = |
| + HistoryServiceFactory::GetForProfileWithoutCreating(profile_); |
| + |
| + if (history_service) { |
| + history_service->CountURLs( |
| + base::Bind(&ProfileStatisticsAggregator::StatisticsCallback, |
| + this, "BrowsingHistory"), |
| + tracker_); |
| + } else { |
| + StatisticsCallback("BrowsingHistory", 0); |
|
Mike Lerman
2015/08/05 19:26:09
If there's no history service, does that necessari
lwchkg
2015/08/06 04:32:58
For chrome://user-manager the absence of history i
Mike Lerman
2015/08/07 19:37:12
There are a variety of strange profiles - while yo
lwchkg
2015/08/10 12:03:37
I see. Then I'd like to keep it here (and everywhe
Mike Lerman
2015/08/13 14:56:33
My preference, rather than using special values, w
lwchkg
2015/08/13 16:20:17
Sounds good to me. I'll submit a revision tomorrow
|
| + } |
| + |
| + // Initiate stored password counting (async). |
| + // TODO(lwchkg): make password task cancellable. |
|
Mike Lerman
2015/08/05 19:26:09
I think all TODOs should be assigned to @chromium
lwchkg
2015/08/06 04:32:58
Acknowledged. Anyway, by design password tasks are
|
| + scoped_refptr<password_manager::PasswordStore> password_store = |
| + PasswordStoreFactory::GetForProfile( |
| + profile_, ServiceAccessType::EXPLICIT_ACCESS); |
| + password_store->GetAutofillableLogins(&password_store_consumer_helper_); |
| + |
| + // Initiate preference counting (async). Post to UI thread. |
| + tracker_->PostTaskAndReplyWithResult( |
| + BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI).get(), |
| + FROM_HERE, |
| + base::Bind(&ProfileStatisticsAggregator::CountPrefs, this), |
| + base::Bind(&ProfileStatisticsAggregator::StatisticsCallback, |
| + this, "Settings")); |
| +} |
| + |
| +void ProfileStatisticsAggregator::StatisticsCallback(std::string category, |
| + int count) { |
| + profile_stats_values_.push_back(std::make_pair(category, count)); |
| + callback_.Run(profile_stats_values_); |
| +} |
| + |
| +int ProfileStatisticsAggregator::CountURLsFromNode( |
| + const bookmarks::BookmarkNode* node) { |
| + int count = 0; |
| + if (node->is_url()) { |
| + ++count; |
| + } else { |
| + for (int i = 0; i < node->child_count(); ++i) |
| + count += CountURLsFromNode(node->GetChild(i)); |
| + } |
| + return count; |
| +} |
| + |
| +int ProfileStatisticsAggregator::CountURLs() const { |
|
Mike Lerman
2015/08/05 19:26:09
since this method counts bookmarks, could we call
lwchkg
2015/08/06 04:32:58
Yes. Let me change the name.
|
| + bookmarks::BookmarkModel* bookmark_model = |
| + BookmarkModelFactory::GetForProfileIfExists(profile_); |
| + |
| + if (bookmark_model) { |
| + return CountURLsFromNode(bookmark_model->bookmark_bar_node()) + |
| + CountURLsFromNode(bookmark_model->other_node()) + |
| + CountURLsFromNode(bookmark_model->mobile_node()); |
| + } else { |
| + return 0; |
| + } |
| +} |
| + |
| +int ProfileStatisticsAggregator::CountPrefs() const { |
| + const PrefService* pref_service = profile_->GetPrefs(); |
| + |
| + if (!pref_service) |
| + return 0; |
| + |
| + scoped_ptr<base::DictionaryValue> prefs = |
| + pref_service->GetPreferenceValuesWithoutPathExpansion(); |
| + |
| + int count = 0; |
| + for (base::DictionaryValue::Iterator it(*(prefs.get())); |
| + !it.IsAtEnd(); it.Advance()) { |
| + const PrefService::Preference* pref = pref_service-> |
| + FindPreference(it.key()); |
| + // Skip all dictionaries (which must be empty by the function call above). |
| + if (it.value().GetType() != base::Value::TYPE_DICTIONARY && |
| + pref && pref->IsUserControlled() && !pref->IsDefaultValue()) { |
| + ++count; |
| + } |
| + } |
| + return count; |
| +} |
| + |
| +void GetProfileStatistics(Profile* profile, |
| + const ProfileStatisticsCallback& callback, |
| + base::CancelableTaskTracker* tracker) { |
| + new ProfileStatisticsAggregator(profile, callback, tracker); |
| +} |
| + |
| +} // namespace profiles |