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"
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/metrics/rappor/sampling.h"
16 #include "chrome/browser/prefs/pref_service_syncable.h"
17 #include "chrome/browser/prefs/session_startup_pref.h"
18 #include "chrome/browser/prefs/synced_pref_change_registrar.h"
19 #include "chrome/browser/profiles/incognito_helpers.h"
20 #include "chrome/browser/profiles/profile.h"
21 #include "chrome/browser/search_engines/template_url_service_factory.h"
22 #include "chrome/browser/ui/tabs/pinned_tab_codec.h"
23 #include "chrome/common/chrome_switches.h"
24 #include "chrome/common/pref_names.h"
25 #include "chrome/common/url_constants.h"
26 #include "components/keyed_service/content/browser_context_dependency_manager.h"
27 #include "components/search_engines/template_url_prepopulate_data.h"
28 #include "content/public/browser/browser_url_handler.h"
29 #include "crypto/hmac.h"
30 #include "net/base/registry_controlled_domains/registry_controlled_domain.h"
34 const int kSessionStartupPrefValueMax
= SessionStartupPref::kPrefValueMax
;
36 // Record a sample for the Settings.NewTabPage rappor metric.
37 void SampleNewTabPageURL(Profile
* profile
) {
38 GURL
ntp_url(chrome::kChromeUINewTabURL
);
39 bool reverse_on_redirect
= false;
40 content::BrowserURLHandler::GetInstance()->RewriteURLIfNecessary(
43 &reverse_on_redirect
);
44 if (ntp_url
.is_valid())
45 rappor::SampleDomainAndRegistryFromGURL("Settings.NewTabPage", ntp_url
);
50 PrefMetricsService::PrefMetricsService(Profile
* profile
)
52 prefs_(profile_
->GetPrefs()),
53 local_state_(g_browser_process
->local_state()),
57 PrefServiceSyncable
* prefs
= PrefServiceSyncable::FromProfile(profile_
);
58 synced_pref_change_registrar_
.reset(new SyncedPrefChangeRegistrar(prefs
));
60 RegisterSyncedPrefObservers();
63 // For unit testing only.
64 PrefMetricsService::PrefMetricsService(Profile
* profile
,
65 PrefService
* local_state
)
67 prefs_(profile
->GetPrefs()),
68 local_state_(local_state
),
72 PrefMetricsService::~PrefMetricsService() {
75 void PrefMetricsService::RecordLaunchPrefs() {
76 bool show_home_button
= prefs_
->GetBoolean(prefs::kShowHomeButton
);
77 bool home_page_is_ntp
= prefs_
->GetBoolean(prefs::kHomePageIsNewTabPage
);
78 UMA_HISTOGRAM_BOOLEAN("Settings.ShowHomeButton", show_home_button
);
79 if (show_home_button
) {
80 UMA_HISTOGRAM_BOOLEAN("Settings.GivenShowHomeButton_HomePageIsNewTabPage",
84 // For non-NTP homepages, see if the URL comes from the same TLD+1 as a known
85 // search engine. Note that this is only an approximation of search engine
86 // use, due to both false negatives (pages that come from unknown TLD+1 X but
87 // consist of a search box that sends to known TLD+1 Y) and false positives
88 // (pages that share a TLD+1 with a known engine but aren't actually search
89 // pages, e.g. plus.google.com). Additionally, record the TLD+1 of non-NTP
90 // homepages through the privacy-preserving Rappor service.
91 if (!home_page_is_ntp
) {
92 GURL
homepage_url(prefs_
->GetString(prefs::kHomePage
));
93 if (homepage_url
.is_valid()) {
94 UMA_HISTOGRAM_ENUMERATION(
95 "Settings.HomePageEngineType",
96 TemplateURLPrepopulateData::GetEngineType(homepage_url
),
98 rappor::SampleDomainAndRegistryFromGURL("Settings.HomePage2",
103 SampleNewTabPageURL(profile_
);
105 int restore_on_startup
= prefs_
->GetInteger(prefs::kRestoreOnStartup
);
106 UMA_HISTOGRAM_ENUMERATION("Settings.StartupPageLoadSettings",
107 restore_on_startup
, kSessionStartupPrefValueMax
);
108 if (restore_on_startup
== SessionStartupPref::kPrefValueURLs
) {
109 const base::ListValue
* url_list
=
110 prefs_
->GetList(prefs::kURLsToRestoreOnStartup
);
111 UMA_HISTOGRAM_CUSTOM_COUNTS("Settings.StartupPageLoadURLs",
112 url_list
->GetSize(), 1, 50, 20);
113 // Similarly, check startup pages for known search engine TLD+1s.
114 std::string url_text
;
115 for (size_t i
= 0; i
< url_list
->GetSize(); ++i
) {
116 if (url_list
->GetString(i
, &url_text
)) {
117 GURL
start_url(url_text
);
118 if (start_url
.is_valid()) {
119 UMA_HISTOGRAM_ENUMERATION(
120 "Settings.StartupPageEngineTypes",
121 TemplateURLPrepopulateData::GetEngineType(start_url
),
124 rappor::SampleDomainAndRegistryFromGURL("Settings.FirstStartupPage",
132 #if !defined(OS_ANDROID)
133 StartupTabs startup_tabs
= PinnedTabCodec::ReadPinnedTabs(profile_
);
134 UMA_HISTOGRAM_CUSTOM_COUNTS("Settings.PinnedTabs",
135 startup_tabs
.size(), 1, 50, 20);
136 for (size_t i
= 0; i
< startup_tabs
.size(); ++i
) {
137 GURL
start_url(startup_tabs
.at(i
).url
);
138 if (start_url
.is_valid()) {
139 UMA_HISTOGRAM_ENUMERATION(
140 "Settings.PinnedTabEngineTypes",
141 TemplateURLPrepopulateData::GetEngineType(start_url
),
148 void PrefMetricsService::RegisterSyncedPrefObservers() {
149 LogHistogramValueCallback booleanHandler
= base::Bind(
150 &PrefMetricsService::LogBooleanPrefChange
, base::Unretained(this));
152 AddPrefObserver(prefs::kShowHomeButton
, "ShowHomeButton", booleanHandler
);
153 AddPrefObserver(prefs::kHomePageIsNewTabPage
, "HomePageIsNewTabPage",
156 AddPrefObserver(prefs::kRestoreOnStartup
, "StartupPageLoadSettings",
157 base::Bind(&PrefMetricsService::LogIntegerPrefChange
,
158 base::Unretained(this),
159 kSessionStartupPrefValueMax
));
162 void PrefMetricsService::AddPrefObserver(
163 const std::string
& path
,
164 const std::string
& histogram_name_prefix
,
165 const LogHistogramValueCallback
& callback
) {
166 synced_pref_change_registrar_
->Add(path
.c_str(),
167 base::Bind(&PrefMetricsService::OnPrefChanged
,
168 base::Unretained(this),
169 histogram_name_prefix
, callback
));
172 void PrefMetricsService::OnPrefChanged(
173 const std::string
& histogram_name_prefix
,
174 const LogHistogramValueCallback
& callback
,
175 const std::string
& path
,
177 PrefServiceSyncable
* prefs
= PrefServiceSyncable::FromProfile(profile_
);
178 const PrefService::Preference
* pref
= prefs
->FindPreference(path
.c_str());
180 std::string
source_name(
181 from_sync
? ".PulledFromSync" : ".PushedToSync");
182 std::string
histogram_name("Settings." + histogram_name_prefix
+ source_name
);
183 callback
.Run(histogram_name
, pref
->GetValue());
186 void PrefMetricsService::LogBooleanPrefChange(const std::string
& histogram_name
,
187 const base::Value
* value
) {
188 bool boolean_value
= false;
189 if (!value
->GetAsBoolean(&boolean_value
))
191 base::HistogramBase
* histogram
= base::BooleanHistogram::FactoryGet(
192 histogram_name
, base::HistogramBase::kUmaTargetedHistogramFlag
);
193 histogram
->Add(boolean_value
);
196 void PrefMetricsService::LogIntegerPrefChange(int boundary_value
,
197 const std::string
& histogram_name
,
198 const base::Value
* value
) {
199 int integer_value
= 0;
200 if (!value
->GetAsInteger(&integer_value
))
202 base::HistogramBase
* histogram
= base::LinearHistogram::FactoryGet(
207 base::HistogramBase::kUmaTargetedHistogramFlag
);
208 histogram
->Add(integer_value
);
212 PrefMetricsService::Factory
* PrefMetricsService::Factory::GetInstance() {
213 return Singleton
<PrefMetricsService::Factory
>::get();
217 PrefMetricsService
* PrefMetricsService::Factory::GetForProfile(
219 return static_cast<PrefMetricsService
*>(
220 GetInstance()->GetServiceForBrowserContext(profile
, true));
223 PrefMetricsService::Factory::Factory()
224 : BrowserContextKeyedServiceFactory(
225 "PrefMetricsService",
226 BrowserContextDependencyManager::GetInstance()) {
227 DependsOn(TemplateURLServiceFactory::GetInstance());
230 PrefMetricsService::Factory::~Factory() {
233 KeyedService
* PrefMetricsService::Factory::BuildServiceInstanceFor(
234 content::BrowserContext
* profile
) const {
235 return new PrefMetricsService(static_cast<Profile
*>(profile
));
238 bool PrefMetricsService::Factory::ServiceIsCreatedWithBrowserContext() const {
242 bool PrefMetricsService::Factory::ServiceIsNULLWhileTesting() const {
246 content::BrowserContext
* PrefMetricsService::Factory::GetBrowserContextToUse(
247 content::BrowserContext
* context
) const {
248 return chrome::GetBrowserContextRedirectedInIncognito(context
);