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_util.h"
15 #include "chrome/browser/prefs/session_startup_pref.h"
16 #include "chrome/browser/profiles/incognito_helpers.h"
17 #include "chrome/browser/profiles/profile.h"
18 #include "chrome/browser/search_engines/template_url_service_factory.h"
19 #include "chrome/browser/ui/tabs/pinned_tab_codec.h"
20 #include "chrome/common/pref_names.h"
21 #include "chrome/common/url_constants.h"
22 #include "components/keyed_service/content/browser_context_dependency_manager.h"
23 #include "components/rappor/rappor_utils.h"
24 #include "components/search_engines/template_url_prepopulate_data.h"
25 #include "components/syncable_prefs/pref_service_syncable.h"
26 #include "components/syncable_prefs/synced_pref_change_registrar.h"
27 #include "content/public/browser/browser_url_handler.h"
28 #include "crypto/hmac.h"
29 #include "net/base/registry_controlled_domains/registry_controlled_domain.h"
33 const int kSessionStartupPrefValueMax
= SessionStartupPref::kPrefValueMax
;
35 // Record a sample for the Settings.NewTabPage rappor metric.
36 void SampleNewTabPageURL(Profile
* profile
) {
37 GURL
ntp_url(chrome::kChromeUINewTabURL
);
38 bool reverse_on_redirect
= false;
39 content::BrowserURLHandler::GetInstance()->RewriteURLIfNecessary(
42 &reverse_on_redirect
);
43 if (ntp_url
.is_valid()) {
44 rappor::SampleDomainAndRegistryFromGURL(g_browser_process
->rappor_service(),
45 "Settings.NewTabPage", ntp_url
);
51 PrefMetricsService::PrefMetricsService(Profile
* profile
)
53 prefs_(profile_
->GetPrefs()),
54 local_state_(g_browser_process
->local_state()),
58 syncable_prefs::PrefServiceSyncable
* prefs
=
59 PrefServiceSyncableFromProfile(profile_
);
60 synced_pref_change_registrar_
.reset(
61 new syncable_prefs::SyncedPrefChangeRegistrar(prefs
));
63 RegisterSyncedPrefObservers();
66 // For unit testing only.
67 PrefMetricsService::PrefMetricsService(Profile
* profile
,
68 PrefService
* local_state
)
70 prefs_(profile
->GetPrefs()),
71 local_state_(local_state
),
75 PrefMetricsService::~PrefMetricsService() {
78 void PrefMetricsService::RecordLaunchPrefs() {
79 bool show_home_button
= prefs_
->GetBoolean(prefs::kShowHomeButton
);
80 bool home_page_is_ntp
= prefs_
->GetBoolean(prefs::kHomePageIsNewTabPage
);
81 UMA_HISTOGRAM_BOOLEAN("Settings.ShowHomeButton", show_home_button
);
82 if (show_home_button
) {
83 UMA_HISTOGRAM_BOOLEAN("Settings.GivenShowHomeButton_HomePageIsNewTabPage",
87 // For non-NTP homepages, see if the URL comes from the same TLD+1 as a known
88 // search engine. Note that this is only an approximation of search engine
89 // use, due to both false negatives (pages that come from unknown TLD+1 X but
90 // consist of a search box that sends to known TLD+1 Y) and false positives
91 // (pages that share a TLD+1 with a known engine but aren't actually search
92 // pages, e.g. plus.google.com). Additionally, record the TLD+1 of non-NTP
93 // homepages through the privacy-preserving Rappor service.
94 if (!home_page_is_ntp
) {
95 GURL
homepage_url(prefs_
->GetString(prefs::kHomePage
));
96 if (homepage_url
.is_valid()) {
97 UMA_HISTOGRAM_ENUMERATION(
98 "Settings.HomePageEngineType",
99 TemplateURLPrepopulateData::GetEngineType(homepage_url
),
101 rappor::SampleDomainAndRegistryFromGURL(
102 g_browser_process
->rappor_service(), "Settings.HomePage2",
107 SampleNewTabPageURL(profile_
);
109 int restore_on_startup
= prefs_
->GetInteger(prefs::kRestoreOnStartup
);
110 UMA_HISTOGRAM_ENUMERATION("Settings.StartupPageLoadSettings",
111 restore_on_startup
, kSessionStartupPrefValueMax
);
112 if (restore_on_startup
== SessionStartupPref::kPrefValueURLs
) {
113 const base::ListValue
* url_list
=
114 prefs_
->GetList(prefs::kURLsToRestoreOnStartup
);
115 UMA_HISTOGRAM_CUSTOM_COUNTS("Settings.StartupPageLoadURLs",
116 url_list
->GetSize(), 1, 50, 20);
117 // Similarly, check startup pages for known search engine TLD+1s.
118 std::string url_text
;
119 for (size_t i
= 0; i
< url_list
->GetSize(); ++i
) {
120 if (url_list
->GetString(i
, &url_text
)) {
121 GURL
start_url(url_text
);
122 if (start_url
.is_valid()) {
123 UMA_HISTOGRAM_ENUMERATION(
124 "Settings.StartupPageEngineTypes",
125 TemplateURLPrepopulateData::GetEngineType(start_url
),
128 rappor::SampleDomainAndRegistryFromGURL(
129 g_browser_process
->rappor_service(),
130 "Settings.FirstStartupPage", start_url
);
137 #if !defined(OS_ANDROID)
138 StartupTabs startup_tabs
= PinnedTabCodec::ReadPinnedTabs(profile_
);
139 UMA_HISTOGRAM_CUSTOM_COUNTS("Settings.PinnedTabs",
140 startup_tabs
.size(), 1, 50, 20);
141 for (size_t i
= 0; i
< startup_tabs
.size(); ++i
) {
142 GURL
start_url(startup_tabs
.at(i
).url
);
143 if (start_url
.is_valid()) {
144 UMA_HISTOGRAM_ENUMERATION(
145 "Settings.PinnedTabEngineTypes",
146 TemplateURLPrepopulateData::GetEngineType(start_url
),
153 void PrefMetricsService::RegisterSyncedPrefObservers() {
154 LogHistogramValueCallback booleanHandler
= base::Bind(
155 &PrefMetricsService::LogBooleanPrefChange
, base::Unretained(this));
157 AddPrefObserver(prefs::kShowHomeButton
, "ShowHomeButton", booleanHandler
);
158 AddPrefObserver(prefs::kHomePageIsNewTabPage
, "HomePageIsNewTabPage",
161 AddPrefObserver(prefs::kRestoreOnStartup
, "StartupPageLoadSettings",
162 base::Bind(&PrefMetricsService::LogIntegerPrefChange
,
163 base::Unretained(this),
164 kSessionStartupPrefValueMax
));
167 void PrefMetricsService::AddPrefObserver(
168 const std::string
& path
,
169 const std::string
& histogram_name_prefix
,
170 const LogHistogramValueCallback
& callback
) {
171 synced_pref_change_registrar_
->Add(path
.c_str(),
172 base::Bind(&PrefMetricsService::OnPrefChanged
,
173 base::Unretained(this),
174 histogram_name_prefix
, callback
));
177 void PrefMetricsService::OnPrefChanged(
178 const std::string
& histogram_name_prefix
,
179 const LogHistogramValueCallback
& callback
,
180 const std::string
& path
,
182 syncable_prefs::PrefServiceSyncable
* prefs
=
183 PrefServiceSyncableFromProfile(profile_
);
184 const PrefService::Preference
* pref
= prefs
->FindPreference(path
.c_str());
186 std::string
source_name(
187 from_sync
? ".PulledFromSync" : ".PushedToSync");
188 std::string
histogram_name("Settings." + histogram_name_prefix
+ source_name
);
189 callback
.Run(histogram_name
, pref
->GetValue());
192 void PrefMetricsService::LogBooleanPrefChange(const std::string
& histogram_name
,
193 const base::Value
* value
) {
194 bool boolean_value
= false;
195 if (!value
->GetAsBoolean(&boolean_value
))
197 base::HistogramBase
* histogram
= base::BooleanHistogram::FactoryGet(
198 histogram_name
, base::HistogramBase::kUmaTargetedHistogramFlag
);
199 histogram
->Add(boolean_value
);
202 void PrefMetricsService::LogIntegerPrefChange(int boundary_value
,
203 const std::string
& histogram_name
,
204 const base::Value
* value
) {
205 int integer_value
= 0;
206 if (!value
->GetAsInteger(&integer_value
))
208 base::HistogramBase
* histogram
= base::LinearHistogram::FactoryGet(
213 base::HistogramBase::kUmaTargetedHistogramFlag
);
214 histogram
->Add(integer_value
);
218 PrefMetricsService::Factory
* PrefMetricsService::Factory::GetInstance() {
219 return base::Singleton
<PrefMetricsService::Factory
>::get();
223 PrefMetricsService
* PrefMetricsService::Factory::GetForProfile(
225 return static_cast<PrefMetricsService
*>(
226 GetInstance()->GetServiceForBrowserContext(profile
, true));
229 PrefMetricsService::Factory::Factory()
230 : BrowserContextKeyedServiceFactory(
231 "PrefMetricsService",
232 BrowserContextDependencyManager::GetInstance()) {
233 DependsOn(TemplateURLServiceFactory::GetInstance());
236 PrefMetricsService::Factory::~Factory() {
239 KeyedService
* PrefMetricsService::Factory::BuildServiceInstanceFor(
240 content::BrowserContext
* profile
) const {
241 return new PrefMetricsService(static_cast<Profile
*>(profile
));
244 bool PrefMetricsService::Factory::ServiceIsCreatedWithBrowserContext() const {
248 bool PrefMetricsService::Factory::ServiceIsNULLWhileTesting() const {
252 content::BrowserContext
* PrefMetricsService::Factory::GetBrowserContextToUse(
253 content::BrowserContext
* context
) const {
254 return chrome::GetBrowserContextRedirectedInIncognito(context
);