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

Unified Diff: chrome/browser/prefs/pref_metrics_service.cc

Issue 21580002: Add histograms to track synced pref changes. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: small cleanup Created 7 years, 4 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/prefs/pref_metrics_service.cc
diff --git a/chrome/browser/prefs/pref_metrics_service.cc b/chrome/browser/prefs/pref_metrics_service.cc
index bcac81817db093c1ce75ec64fce142892c0ac309..467eee3472582b28b137c66438ffcfabf55544b6 100644
--- a/chrome/browser/prefs/pref_metrics_service.cc
+++ b/chrome/browser/prefs/pref_metrics_service.cc
@@ -4,17 +4,40 @@
#include "chrome/browser/prefs/pref_metrics_service.h"
+#include "base/bind.h"
#include "base/metrics/histogram.h"
#include "base/prefs/pref_service.h"
+#include "chrome/browser/prefs/pref_service_syncable.h"
#include "chrome/browser/prefs/session_startup_pref.h"
+#include "chrome/browser/prefs/synced_pref_change_registrar.h"
#include "chrome/browser/profiles/incognito_helpers.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/pref_names.h"
#include "components/browser_context_keyed_service/browser_context_dependency_manager.h"
+#include "net/base/registry_controlled_domains/registry_controlled_domain.h"
+
+namespace {
+
+// Converts a host name into a domain name for easier matching.
+std::string GetDomainFromHost(const std::string& host) {
+ return net::registry_controlled_domains::GetDomainAndRegistry(
+ host,
+ net::registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES);
+}
+
+} // namespace
+
+// Declare storage for this value so it can be used as an argument to Bind.
+const int SessionStartupPref::kPrefValueMax;
PrefMetricsService::PrefMetricsService(Profile* profile)
: profile_(profile) {
RecordLaunchPrefs();
+
+ PrefServiceSyncable* prefs = PrefServiceSyncable::FromProfile(profile_);
+ synced_pref_change_registrar_.reset(new SyncedPrefChangeRegistrar(prefs));
+
+ RegisterSyncedPrefObservers();
}
PrefMetricsService::~PrefMetricsService() {
@@ -40,6 +63,76 @@ void PrefMetricsService::RecordLaunchPrefs() {
}
}
+void PrefMetricsService::RegisterSyncedPrefObservers() {
+ LogHistogramValueCallback booleanHandler = base::Bind(
+ &PrefMetricsService::LogBooleanPrefChange, base::Unretained(this));
+
+ AddPrefObserver(prefs::kShowHomeButton, "ShowHomeButton", booleanHandler);
+ AddPrefObserver(prefs::kHomePageIsNewTabPage, "HomePageIsNewTabPage",
+ booleanHandler);
+
+ AddPrefObserver(prefs::kRestoreOnStartup, "StartupPageLoadSettings",
+ base::Bind(&PrefMetricsService::LogIntegerPrefChange,
+ base::Unretained(this),
+ SessionStartupPref::kPrefValueMax));
+}
+
+void PrefMetricsService::AddPrefObserver(
+ const std::string& path,
+ const std::string& histogram_name_prefix,
+ const LogHistogramValueCallback& callback) {
+ synced_pref_change_registrar_->Add(path.c_str(),
+ base::Bind(&PrefMetricsService::OnPrefChanged,
+ base::Unretained(this),
+ histogram_name_prefix, callback));
+}
+
+void PrefMetricsService::OnPrefChanged(
+ const std::string& histogram_name_prefix,
+ const LogHistogramValueCallback& callback,
+ const std::string& path,
+ bool from_sync) {
+ PrefServiceSyncable* prefs = PrefServiceSyncable::FromProfile(profile_);
+ const PrefService::Preference* pref = prefs->FindPreference(path.c_str());
+ DCHECK(pref);
+ std::string source_name(
+ from_sync ? ".PulledFromSync" : ".PushedToSync");
+ std::string histogram_name("Settings." + histogram_name_prefix + source_name);
+ callback.Run(histogram_name, pref->GetValue());
+};
+
+void PrefMetricsService::LogBooleanPrefChange(const std::string& histogram_name,
+ const Value* value) {
+ bool boolean_value = false;
+ if (!value->GetAsBoolean(&boolean_value))
+ return;
+ UMA_HISTOGRAM_BOOLEAN(histogram_name.c_str(), boolean_value);
+}
+
+void PrefMetricsService::LogIntegerPrefChange(int boundary_value,
+ const std::string& histogram_name,
+ const Value* value) {
+ int integer_value = 0;
+ if (!value->GetAsInteger(&integer_value))
+ return;
+ UMA_HISTOGRAM_ENUMERATION(histogram_name.c_str(), integer_value,
+ boundary_value);
+}
+
+void PrefMetricsService::LogListPrefChange(
+ const LogHistogramValueCallback& item_callback,
+ const std::string& histogram_name,
+ const Value* value) {
+ const ListValue* items = NULL;
+ if (!value->GetAsList(&items))
+ return;
+ for (size_t i = 0; i < items->GetSize(); ++i) {
+ const Value *item_value = NULL;
+ if (items->Get(i, &item_value))
+ item_callback.Run(histogram_name, item_value);
+ }
+}
+
// static
PrefMetricsService::Factory* PrefMetricsService::Factory::GetInstance() {
return Singleton<PrefMetricsService::Factory>::get();

Powered by Google App Engine
This is Rietveld 408576698