Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(550)

Unified Diff: chrome/browser/profiles/profile_statistics_service.cc

Issue 1248613003: Issue 501916 : Add data type counts to profile deletion flow (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: First draft Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/profiles/profile_statistics_service.cc
diff --git a/chrome/browser/profiles/profile_statistics_service.cc b/chrome/browser/profiles/profile_statistics_service.cc
new file mode 100644
index 0000000000000000000000000000000000000000..4f7ed05145c6de235b464b8d25d57b6d33a8e39d
--- /dev/null
+++ b/chrome/browser/profiles/profile_statistics_service.cc
@@ -0,0 +1,135 @@
+// 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_service.h"
+
+#include "base/bind.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/history/core/browser/history_service.h"
+#include "components/password_manager/core/browser/password_store.h"
+#include "content/public/browser/browser_thread.h"
+
+using content::BrowserThread;
+
+namespace profiles {
+
+ProfileStatisticsService::ProfileStatisticsService
+ (Profile* profile, ProfileStatisticsCallback callback) :
+ profile_(profile), callback_(callback), weak_factory_(this) {
+ init_();
+}
+
+void ProfileStatisticsService::init_() {
+ // Initiate bookmark counting (async).
+ bookmarks::BookmarkModel* bookmark_model =
+ BookmarkModelFactory::GetForProfileIfExists(profile_);
+
+ if (bookmark_model) {
+ // This task does not use a database, but
+ // DB thread is used because most other operations in this class use it.
+ tracker_.PostTaskAndReplyWithResult(
+ BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB).get(),
Mike Lerman 2015/07/22 01:39:26 nit: indent 4 spaces whenever you line wrap.
lwchkg 2015/07/26 17:38:22 Acknowledged.
+ FROM_HERE,
+ base::Bind(&ProfileStatisticsService::CountURLs_,
+ base::Unretained(bookmark_model)),
+ base::Bind(&ProfileStatisticsService::StatsticsCallback_,
+ weak_factory_.GetWeakPtr(),
+ "Bookmarks"));
+ } else {
+ StatsticsCallback_("Bookmarks", 0);
+ }
+
+ // Initiate history counting (async).
+ history::HistoryService* history_service =
+ HistoryServiceFactory::GetForProfileWithoutCreating(profile_);
+
+ if (history_service) {
+ history_service->CountURLs(
+ base::Bind(&ProfileStatisticsService::StatsticsCallback_,
+ weak_factory_.GetWeakPtr(),
+ "BrowsingHistory"),
+ &tracker_);
+ } else {
+ StatsticsCallback_("BrowsingHistory", 0);
+ }
+
+ // Initiate stored password counting (async).
+ scoped_refptr<password_manager::PasswordStore> password_store =
+ PasswordStoreFactory::GetForProfile(
+ profile_, ServiceAccessType::EXPLICIT_ACCESS);
+ if (password_store) {
+ // Currently this task is not cancellable.
+ password_store->GetCountOfLogins(
+ base::Bind(&ProfileStatisticsService::StatsticsCallback_,
+ weak_factory_.GetWeakPtr(),
+ "Passwords"));
+ }
+
+ // Initiate preference counting (async). Post to UI thread.
+ tracker_.PostTaskAndReplyWithResult(
+ BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI).get(),
+ FROM_HERE,
+ base::Bind(&ProfileStatisticsService::CountPrefs_,
+ base::Unretained(this)),
+ base::Bind(&ProfileStatisticsService::StatsticsCallback_,
+ weak_factory_.GetWeakPtr(),
+ "Settings"));
+}
+
+void ProfileStatisticsService::StatsticsCallback_(std::string category,
+ int count) {
+ profile_stats_values_.push_back(std::make_pair(category, count));
+ callback_.Run(profile_stats_values_);
+}
+
+int ProfileStatisticsService::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 ProfileStatisticsService::CountURLs_(
+ const bookmarks::BookmarkModel* bookmark_model) {
+ if (bookmark_model)
Mike Lerman 2015/07/22 01:39:26 nit: use parenthesis anytime the statement block i
lwchkg 2015/07/26 17:38:22 Acknowledged.
+ return CountURLsFromNode_(bookmark_model->bookmark_bar_node()) +
+ CountURLsFromNode_(bookmark_model->other_node()) +
+ CountURLsFromNode_(bookmark_model->mobile_node());
+ else
+ return 0;
+}
+
+int ProfileStatisticsService::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());
Mike Lerman 2015/07/22 01:39:26 indent only 4 spaces.
lwchkg 2015/07/26 17:38:22 Should I have "F" directly under "_"?
+ // 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;
+}
+
+} // namespace profiles

Powered by Google App Engine
This is Rietveld 408576698