Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / prefs / pref_metrics_service.cc
blob8b6f389f961c1d5cf0a3c24a9865c05651e721a3
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/prefs/pref_metrics_service.h"
7 #include "base/bind.h"
8 #include "base/command_line.h"
9 #include "base/metrics/histogram.h"
10 #include "base/prefs/pref_registry_simple.h"
11 #include "base/prefs/pref_service.h"
12 #include "base/strings/string_number_conversions.h"
13 #include "chrome/browser/browser_process.h"
14 #include "chrome/browser/browser_shutdown.h"
15 #include "chrome/browser/prefs/pref_service_syncable.h"
16 #include "chrome/browser/prefs/session_startup_pref.h"
17 #include "chrome/browser/prefs/synced_pref_change_registrar.h"
18 #include "chrome/browser/profiles/incognito_helpers.h"
19 #include "chrome/browser/profiles/profile.h"
20 #include "chrome/browser/search_engines/template_url_prepopulate_data.h"
21 #include "chrome/browser/ui/tabs/pinned_tab_codec.h"
22 #include "chrome/common/chrome_switches.h"
23 #include "chrome/common/pref_names.h"
24 #include "components/browser_context_keyed_service/browser_context_dependency_manager.h"
25 #include "crypto/hmac.h"
26 #include "net/base/registry_controlled_domains/registry_controlled_domain.h"
28 namespace {
30 const int kSessionStartupPrefValueMax = SessionStartupPref::kPrefValueMax;
32 } // namespace
34 PrefMetricsService::PrefMetricsService(Profile* profile)
35 : profile_(profile),
36 prefs_(profile_->GetPrefs()),
37 local_state_(g_browser_process->local_state()),
38 weak_factory_(this) {
39 RecordLaunchPrefs();
41 PrefServiceSyncable* prefs = PrefServiceSyncable::FromProfile(profile_);
42 synced_pref_change_registrar_.reset(new SyncedPrefChangeRegistrar(prefs));
44 RegisterSyncedPrefObservers();
47 // For unit testing only.
48 PrefMetricsService::PrefMetricsService(Profile* profile,
49 PrefService* local_state)
50 : profile_(profile),
51 prefs_(profile->GetPrefs()),
52 local_state_(local_state),
53 weak_factory_(this) {
56 PrefMetricsService::~PrefMetricsService() {
59 void PrefMetricsService::RecordLaunchPrefs() {
60 bool show_home_button = prefs_->GetBoolean(prefs::kShowHomeButton);
61 bool home_page_is_ntp = prefs_->GetBoolean(prefs::kHomePageIsNewTabPage);
62 UMA_HISTOGRAM_BOOLEAN("Settings.ShowHomeButton", show_home_button);
63 if (show_home_button) {
64 UMA_HISTOGRAM_BOOLEAN("Settings.GivenShowHomeButton_HomePageIsNewTabPage",
65 home_page_is_ntp);
68 // For non-NTP homepages, see if the URL comes from the same TLD+1 as a known
69 // search engine. Note that this is only an approximation of search engine
70 // use, due to both false negatives (pages that come from unknown TLD+1 X but
71 // consist of a search box that sends to known TLD+1 Y) and false positives
72 // (pages that share a TLD+1 with a known engine but aren't actually search
73 // pages, e.g. plus.google.com).
74 if (!home_page_is_ntp) {
75 GURL homepage_url(prefs_->GetString(prefs::kHomePage));
76 if (homepage_url.is_valid()) {
77 UMA_HISTOGRAM_ENUMERATION(
78 "Settings.HomePageEngineType",
79 TemplateURLPrepopulateData::GetEngineType(homepage_url),
80 SEARCH_ENGINE_MAX);
84 int restore_on_startup = prefs_->GetInteger(prefs::kRestoreOnStartup);
85 UMA_HISTOGRAM_ENUMERATION("Settings.StartupPageLoadSettings",
86 restore_on_startup, kSessionStartupPrefValueMax);
87 if (restore_on_startup == SessionStartupPref::kPrefValueURLs) {
88 const base::ListValue* url_list =
89 prefs_->GetList(prefs::kURLsToRestoreOnStartup);
90 UMA_HISTOGRAM_CUSTOM_COUNTS("Settings.StartupPageLoadURLs",
91 url_list->GetSize(), 1, 50, 20);
92 // Similarly, check startup pages for known search engine TLD+1s.
93 std::string url_text;
94 for (size_t i = 0; i < url_list->GetSize(); ++i) {
95 if (url_list->GetString(i, &url_text)) {
96 GURL start_url(url_text);
97 if (start_url.is_valid()) {
98 UMA_HISTOGRAM_ENUMERATION(
99 "Settings.StartupPageEngineTypes",
100 TemplateURLPrepopulateData::GetEngineType(start_url),
101 SEARCH_ENGINE_MAX);
107 #if !defined(OS_ANDROID)
108 StartupTabs startup_tabs = PinnedTabCodec::ReadPinnedTabs(profile_);
109 UMA_HISTOGRAM_CUSTOM_COUNTS("Settings.PinnedTabs",
110 startup_tabs.size(), 1, 50, 20);
111 for (size_t i = 0; i < startup_tabs.size(); ++i) {
112 GURL start_url(startup_tabs.at(i).url);
113 if (start_url.is_valid()) {
114 UMA_HISTOGRAM_ENUMERATION(
115 "Settings.PinnedTabEngineTypes",
116 TemplateURLPrepopulateData::GetEngineType(start_url),
117 SEARCH_ENGINE_MAX);
120 #endif
123 void PrefMetricsService::RegisterSyncedPrefObservers() {
124 LogHistogramValueCallback booleanHandler = base::Bind(
125 &PrefMetricsService::LogBooleanPrefChange, base::Unretained(this));
127 AddPrefObserver(prefs::kShowHomeButton, "ShowHomeButton", booleanHandler);
128 AddPrefObserver(prefs::kHomePageIsNewTabPage, "HomePageIsNewTabPage",
129 booleanHandler);
131 AddPrefObserver(prefs::kRestoreOnStartup, "StartupPageLoadSettings",
132 base::Bind(&PrefMetricsService::LogIntegerPrefChange,
133 base::Unretained(this),
134 kSessionStartupPrefValueMax));
137 void PrefMetricsService::AddPrefObserver(
138 const std::string& path,
139 const std::string& histogram_name_prefix,
140 const LogHistogramValueCallback& callback) {
141 synced_pref_change_registrar_->Add(path.c_str(),
142 base::Bind(&PrefMetricsService::OnPrefChanged,
143 base::Unretained(this),
144 histogram_name_prefix, callback));
147 void PrefMetricsService::OnPrefChanged(
148 const std::string& histogram_name_prefix,
149 const LogHistogramValueCallback& callback,
150 const std::string& path,
151 bool from_sync) {
152 PrefServiceSyncable* prefs = PrefServiceSyncable::FromProfile(profile_);
153 const PrefService::Preference* pref = prefs->FindPreference(path.c_str());
154 DCHECK(pref);
155 std::string source_name(
156 from_sync ? ".PulledFromSync" : ".PushedToSync");
157 std::string histogram_name("Settings." + histogram_name_prefix + source_name);
158 callback.Run(histogram_name, pref->GetValue());
161 void PrefMetricsService::LogBooleanPrefChange(const std::string& histogram_name,
162 const base::Value* value) {
163 bool boolean_value = false;
164 if (!value->GetAsBoolean(&boolean_value))
165 return;
166 base::HistogramBase* histogram = base::BooleanHistogram::FactoryGet(
167 histogram_name, base::HistogramBase::kUmaTargetedHistogramFlag);
168 histogram->Add(boolean_value);
171 void PrefMetricsService::LogIntegerPrefChange(int boundary_value,
172 const std::string& histogram_name,
173 const base::Value* value) {
174 int integer_value = 0;
175 if (!value->GetAsInteger(&integer_value))
176 return;
177 base::HistogramBase* histogram = base::LinearHistogram::FactoryGet(
178 histogram_name,
180 boundary_value,
181 boundary_value + 1,
182 base::HistogramBase::kUmaTargetedHistogramFlag);
183 histogram->Add(integer_value);
186 // static
187 PrefMetricsService::Factory* PrefMetricsService::Factory::GetInstance() {
188 return Singleton<PrefMetricsService::Factory>::get();
191 // static
192 PrefMetricsService* PrefMetricsService::Factory::GetForProfile(
193 Profile* profile) {
194 return static_cast<PrefMetricsService*>(
195 GetInstance()->GetServiceForBrowserContext(profile, true));
198 PrefMetricsService::Factory::Factory()
199 : BrowserContextKeyedServiceFactory(
200 "PrefMetricsService",
201 BrowserContextDependencyManager::GetInstance()) {
204 PrefMetricsService::Factory::~Factory() {
207 BrowserContextKeyedService*
208 PrefMetricsService::Factory::BuildServiceInstanceFor(
209 content::BrowserContext* profile) const {
210 return new PrefMetricsService(static_cast<Profile*>(profile));
213 bool PrefMetricsService::Factory::ServiceIsCreatedWithBrowserContext() const {
214 return true;
217 bool PrefMetricsService::Factory::ServiceIsNULLWhileTesting() const {
218 return false;
221 content::BrowserContext* PrefMetricsService::Factory::GetBrowserContextToUse(
222 content::BrowserContext* context) const {
223 return chrome::GetBrowserContextRedirectedInIncognito(context);