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/prefs/pref_service_syncable.h"
15 #include "chrome/browser/prefs/session_startup_pref.h"
16 #include "chrome/browser/prefs/synced_pref_change_registrar.h"
17 #include "chrome/browser/profiles/incognito_helpers.h"
18 #include "chrome/browser/profiles/profile.h"
19 #include "chrome/browser/search_engines/template_url_service_factory.h"
20 #include "chrome/browser/ui/tabs/pinned_tab_codec.h"
21 #include "chrome/common/pref_names.h"
22 #include "chrome/common/url_constants.h"
23 #include "components/keyed_service/content/browser_context_dependency_manager.h"
24 #include "components/rappor/rappor_utils.h"
25 #include "components/search_engines/template_url_prepopulate_data.h"
26 #include "content/public/browser/browser_url_handler.h"
27 #include "crypto/hmac.h"
28 #include "net/base/registry_controlled_domains/registry_controlled_domain.h"
32 const int kSessionStartupPrefValueMax
= SessionStartupPref::kPrefValueMax
;
34 // Record a sample for the Settings.NewTabPage rappor metric.
35 void SampleNewTabPageURL(Profile
* profile
) {
36 GURL
ntp_url(chrome::kChromeUINewTabURL
);
37 bool reverse_on_redirect
= false;
38 content::BrowserURLHandler::GetInstance()->RewriteURLIfNecessary(
41 &reverse_on_redirect
);
42 if (ntp_url
.is_valid()) {
43 rappor::SampleDomainAndRegistryFromGURL(g_browser_process
->rappor_service(),
44 "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(
99 g_browser_process
->rappor_service(), "Settings.HomePage2",
104 SampleNewTabPageURL(profile_
);
106 int restore_on_startup
= prefs_
->GetInteger(prefs::kRestoreOnStartup
);
107 UMA_HISTOGRAM_ENUMERATION("Settings.StartupPageLoadSettings",
108 restore_on_startup
, kSessionStartupPrefValueMax
);
109 if (restore_on_startup
== SessionStartupPref::kPrefValueURLs
) {
110 const base::ListValue
* url_list
=
111 prefs_
->GetList(prefs::kURLsToRestoreOnStartup
);
112 UMA_HISTOGRAM_CUSTOM_COUNTS("Settings.StartupPageLoadURLs",
113 url_list
->GetSize(), 1, 50, 20);
114 // Similarly, check startup pages for known search engine TLD+1s.
115 std::string url_text
;
116 for (size_t i
= 0; i
< url_list
->GetSize(); ++i
) {
117 if (url_list
->GetString(i
, &url_text
)) {
118 GURL
start_url(url_text
);
119 if (start_url
.is_valid()) {
120 UMA_HISTOGRAM_ENUMERATION(
121 "Settings.StartupPageEngineTypes",
122 TemplateURLPrepopulateData::GetEngineType(start_url
),
125 rappor::SampleDomainAndRegistryFromGURL(
126 g_browser_process
->rappor_service(),
127 "Settings.FirstStartupPage", start_url
);
134 #if !defined(OS_ANDROID)
135 StartupTabs startup_tabs
= PinnedTabCodec::ReadPinnedTabs(profile_
);
136 UMA_HISTOGRAM_CUSTOM_COUNTS("Settings.PinnedTabs",
137 startup_tabs
.size(), 1, 50, 20);
138 for (size_t i
= 0; i
< startup_tabs
.size(); ++i
) {
139 GURL
start_url(startup_tabs
.at(i
).url
);
140 if (start_url
.is_valid()) {
141 UMA_HISTOGRAM_ENUMERATION(
142 "Settings.PinnedTabEngineTypes",
143 TemplateURLPrepopulateData::GetEngineType(start_url
),
150 void PrefMetricsService::RegisterSyncedPrefObservers() {
151 LogHistogramValueCallback booleanHandler
= base::Bind(
152 &PrefMetricsService::LogBooleanPrefChange
, base::Unretained(this));
154 AddPrefObserver(prefs::kShowHomeButton
, "ShowHomeButton", booleanHandler
);
155 AddPrefObserver(prefs::kHomePageIsNewTabPage
, "HomePageIsNewTabPage",
158 AddPrefObserver(prefs::kRestoreOnStartup
, "StartupPageLoadSettings",
159 base::Bind(&PrefMetricsService::LogIntegerPrefChange
,
160 base::Unretained(this),
161 kSessionStartupPrefValueMax
));
164 void PrefMetricsService::AddPrefObserver(
165 const std::string
& path
,
166 const std::string
& histogram_name_prefix
,
167 const LogHistogramValueCallback
& callback
) {
168 synced_pref_change_registrar_
->Add(path
.c_str(),
169 base::Bind(&PrefMetricsService::OnPrefChanged
,
170 base::Unretained(this),
171 histogram_name_prefix
, callback
));
174 void PrefMetricsService::OnPrefChanged(
175 const std::string
& histogram_name_prefix
,
176 const LogHistogramValueCallback
& callback
,
177 const std::string
& path
,
179 PrefServiceSyncable
* prefs
= PrefServiceSyncable::FromProfile(profile_
);
180 const PrefService::Preference
* pref
= prefs
->FindPreference(path
.c_str());
182 std::string
source_name(
183 from_sync
? ".PulledFromSync" : ".PushedToSync");
184 std::string
histogram_name("Settings." + histogram_name_prefix
+ source_name
);
185 callback
.Run(histogram_name
, pref
->GetValue());
188 void PrefMetricsService::LogBooleanPrefChange(const std::string
& histogram_name
,
189 const base::Value
* value
) {
190 bool boolean_value
= false;
191 if (!value
->GetAsBoolean(&boolean_value
))
193 base::HistogramBase
* histogram
= base::BooleanHistogram::FactoryGet(
194 histogram_name
, base::HistogramBase::kUmaTargetedHistogramFlag
);
195 histogram
->Add(boolean_value
);
198 void PrefMetricsService::LogIntegerPrefChange(int boundary_value
,
199 const std::string
& histogram_name
,
200 const base::Value
* value
) {
201 int integer_value
= 0;
202 if (!value
->GetAsInteger(&integer_value
))
204 base::HistogramBase
* histogram
= base::LinearHistogram::FactoryGet(
209 base::HistogramBase::kUmaTargetedHistogramFlag
);
210 histogram
->Add(integer_value
);
214 PrefMetricsService::Factory
* PrefMetricsService::Factory::GetInstance() {
215 return base::Singleton
<PrefMetricsService::Factory
>::get();
219 PrefMetricsService
* PrefMetricsService::Factory::GetForProfile(
221 return static_cast<PrefMetricsService
*>(
222 GetInstance()->GetServiceForBrowserContext(profile
, true));
225 PrefMetricsService::Factory::Factory()
226 : BrowserContextKeyedServiceFactory(
227 "PrefMetricsService",
228 BrowserContextDependencyManager::GetInstance()) {
229 DependsOn(TemplateURLServiceFactory::GetInstance());
232 PrefMetricsService::Factory::~Factory() {
235 KeyedService
* PrefMetricsService::Factory::BuildServiceInstanceFor(
236 content::BrowserContext
* profile
) const {
237 return new PrefMetricsService(static_cast<Profile
*>(profile
));
240 bool PrefMetricsService::Factory::ServiceIsCreatedWithBrowserContext() const {
244 bool PrefMetricsService::Factory::ServiceIsNULLWhileTesting() const {
248 content::BrowserContext
* PrefMetricsService::Factory::GetBrowserContextToUse(
249 content::BrowserContext
* context
) const {
250 return chrome::GetBrowserContextRedirectedInIncognito(context
);