blob: 14745d8f30150a0d78c82ac57e5517a9d51eaab9 [file] [log] [blame]
[email protected]b544c1ae2013-07-24 18:34:131// 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.
4
5#include "chrome/browser/prefs/pref_metrics_service.h"
6
[email protected]0ebc4032013-08-10 07:07:117#include "base/bind.h"
[email protected]8456bec2013-09-09 04:06:238#include "base/command_line.h"
[email protected]b544c1ae2013-07-24 18:34:139#include "base/metrics/histogram.h"
[email protected]8456bec2013-09-09 04:06:2310#include "base/prefs/pref_registry_simple.h"
[email protected]b544c1ae2013-07-24 18:34:1311#include "base/prefs/pref_service.h"
[email protected]8456bec2013-09-09 04:06:2312#include "base/strings/string_number_conversions.h"
13#include "chrome/browser/browser_process.h"
sdefresneed27d86b2015-09-14 11:02:3814#include "chrome/browser/prefs/pref_service_syncable_util.h"
[email protected]16f2ab12013-07-30 20:24:1815#include "chrome/browser/prefs/session_startup_pref.h"
[email protected]b544c1ae2013-07-24 18:34:1316#include "chrome/browser/profiles/incognito_helpers.h"
17#include "chrome/browser/profiles/profile.h"
sdefresne15a87092014-10-16 14:41:5118#include "chrome/browser/search_engines/template_url_service_factory.h"
[email protected]354b7ab2013-09-11 20:48:5619#include "chrome/browser/ui/tabs/pinned_tab_codec.h"
[email protected]b544c1ae2013-07-24 18:34:1320#include "chrome/common/pref_names.h"
[email protected]a5eaafe2014-06-16 23:19:2121#include "chrome/common/url_constants.h"
[email protected]540380fc2014-03-14 10:10:3422#include "components/keyed_service/content/browser_context_dependency_manager.h"
mathp46284f32015-03-11 13:06:4123#include "components/rappor/rappor_utils.h"
[email protected]0915b352014-06-25 19:58:1424#include "components/search_engines/template_url_prepopulate_data.h"
sdefresne875d0782015-09-16 12:01:2825#include "components/syncable_prefs/pref_service_syncable.h"
26#include "components/syncable_prefs/synced_pref_change_registrar.h"
[email protected]a5eaafe2014-06-16 23:19:2127#include "content/public/browser/browser_url_handler.h"
[email protected]8456bec2013-09-09 04:06:2328#include "crypto/hmac.h"
[email protected]0ebc4032013-08-10 07:07:1129#include "net/base/registry_controlled_domains/registry_controlled_domain.h"
30
31namespace {
32
[email protected]0ebc4032013-08-10 07:07:1133const int kSessionStartupPrefValueMax = SessionStartupPref::kPrefValueMax;
34
[email protected]a5eaafe2014-06-16 23:19:2135// Record a sample for the Settings.NewTabPage rappor metric.
36void SampleNewTabPageURL(Profile* profile) {
37 GURL ntp_url(chrome::kChromeUINewTabURL);
38 bool reverse_on_redirect = false;
39 content::BrowserURLHandler::GetInstance()->RewriteURLIfNecessary(
40 &ntp_url,
41 profile,
42 &reverse_on_redirect);
mathp46284f32015-03-11 13:06:4143 if (ntp_url.is_valid()) {
44 rappor::SampleDomainAndRegistryFromGURL(g_browser_process->rappor_service(),
45 "Settings.NewTabPage", ntp_url);
46 }
[email protected]a5eaafe2014-06-16 23:19:2147}
48
[email protected]0ebc4032013-08-10 07:07:1149} // namespace
[email protected]b544c1ae2013-07-24 18:34:1350
51PrefMetricsService::PrefMetricsService(Profile* profile)
[email protected]8456bec2013-09-09 04:06:2352 : profile_(profile),
53 prefs_(profile_->GetPrefs()),
54 local_state_(g_browser_process->local_state()),
[email protected]8456bec2013-09-09 04:06:2355 weak_factory_(this) {
[email protected]b544c1ae2013-07-24 18:34:1356 RecordLaunchPrefs();
[email protected]0ebc4032013-08-10 07:07:1157
sdefresne50c1e522015-09-18 09:47:5158 syncable_prefs::PrefServiceSyncable* prefs =
59 PrefServiceSyncableFromProfile(profile_);
60 synced_pref_change_registrar_.reset(
61 new syncable_prefs::SyncedPrefChangeRegistrar(prefs));
[email protected]0ebc4032013-08-10 07:07:1162
63 RegisterSyncedPrefObservers();
[email protected]8456bec2013-09-09 04:06:2364}
65
66// For unit testing only.
67PrefMetricsService::PrefMetricsService(Profile* profile,
[email protected]56cbcb3a2013-12-23 21:24:4668 PrefService* local_state)
[email protected]8456bec2013-09-09 04:06:2369 : profile_(profile),
70 prefs_(profile->GetPrefs()),
71 local_state_(local_state),
[email protected]8456bec2013-09-09 04:06:2372 weak_factory_(this) {
[email protected]b544c1ae2013-07-24 18:34:1373}
74
75PrefMetricsService::~PrefMetricsService() {
76}
77
78void PrefMetricsService::RecordLaunchPrefs() {
[email protected]8456bec2013-09-09 04:06:2379 bool show_home_button = prefs_->GetBoolean(prefs::kShowHomeButton);
80 bool home_page_is_ntp = prefs_->GetBoolean(prefs::kHomePageIsNewTabPage);
[email protected]fd6de9732013-08-06 18:26:3581 UMA_HISTOGRAM_BOOLEAN("Settings.ShowHomeButton", show_home_button);
82 if (show_home_button) {
83 UMA_HISTOGRAM_BOOLEAN("Settings.GivenShowHomeButton_HomePageIsNewTabPage",
[email protected]6fc30f02013-08-20 19:55:1684 home_page_is_ntp);
[email protected]fd6de9732013-08-06 18:26:3585 }
[email protected]6fc30f02013-08-20 19:55:1686
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
[email protected]2a172e42014-02-21 04:06:1092 // pages, e.g. plus.google.com). Additionally, record the TLD+1 of non-NTP
93 // homepages through the privacy-preserving Rappor service.
[email protected]6fc30f02013-08-20 19:55:1694 if (!home_page_is_ntp) {
[email protected]8456bec2013-09-09 04:06:2395 GURL homepage_url(prefs_->GetString(prefs::kHomePage));
[email protected]6fc30f02013-08-20 19:55:1696 if (homepage_url.is_valid()) {
97 UMA_HISTOGRAM_ENUMERATION(
98 "Settings.HomePageEngineType",
99 TemplateURLPrepopulateData::GetEngineType(homepage_url),
100 SEARCH_ENGINE_MAX);
mathp46284f32015-03-11 13:06:41101 rappor::SampleDomainAndRegistryFromGURL(
102 g_browser_process->rappor_service(), "Settings.HomePage2",
103 homepage_url);
[email protected]6fc30f02013-08-20 19:55:16104 }
105 }
106
[email protected]a5eaafe2014-06-16 23:19:21107 SampleNewTabPageURL(profile_);
108
[email protected]8456bec2013-09-09 04:06:23109 int restore_on_startup = prefs_->GetInteger(prefs::kRestoreOnStartup);
[email protected]16f2ab12013-07-30 20:24:18110 UMA_HISTOGRAM_ENUMERATION("Settings.StartupPageLoadSettings",
[email protected]6fc30f02013-08-20 19:55:16111 restore_on_startup, kSessionStartupPrefValueMax);
[email protected]16f2ab12013-07-30 20:24:18112 if (restore_on_startup == SessionStartupPref::kPrefValueURLs) {
[email protected]cb1078de2013-12-23 20:04:22113 const base::ListValue* url_list =
114 prefs_->GetList(prefs::kURLsToRestoreOnStartup);
[email protected]6fc30f02013-08-20 19:55:16115 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;
[email protected]354b7ab2013-09-11 20:48:56119 for (size_t i = 0; i < url_list->GetSize(); ++i) {
[email protected]6fc30f02013-08-20 19:55:16120 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),
126 SEARCH_ENGINE_MAX);
[email protected]d6fba162014-06-11 11:58:49127 if (i == 0) {
mathp46284f32015-03-11 13:06:41128 rappor::SampleDomainAndRegistryFromGURL(
129 g_browser_process->rappor_service(),
130 "Settings.FirstStartupPage", start_url);
[email protected]d6fba162014-06-11 11:58:49131 }
[email protected]6fc30f02013-08-20 19:55:16132 }
133 }
134 }
[email protected]16f2ab12013-07-30 20:24:18135 }
[email protected]354b7ab2013-09-11 20:48:56136
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),
147 SEARCH_ENGINE_MAX);
148 }
149 }
150#endif
[email protected]b544c1ae2013-07-24 18:34:13151}
152
[email protected]0ebc4032013-08-10 07:07:11153void PrefMetricsService::RegisterSyncedPrefObservers() {
154 LogHistogramValueCallback booleanHandler = base::Bind(
155 &PrefMetricsService::LogBooleanPrefChange, base::Unretained(this));
156
157 AddPrefObserver(prefs::kShowHomeButton, "ShowHomeButton", booleanHandler);
158 AddPrefObserver(prefs::kHomePageIsNewTabPage, "HomePageIsNewTabPage",
159 booleanHandler);
160
161 AddPrefObserver(prefs::kRestoreOnStartup, "StartupPageLoadSettings",
162 base::Bind(&PrefMetricsService::LogIntegerPrefChange,
163 base::Unretained(this),
164 kSessionStartupPrefValueMax));
165}
166
167void 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));
175}
176
177void PrefMetricsService::OnPrefChanged(
178 const std::string& histogram_name_prefix,
179 const LogHistogramValueCallback& callback,
180 const std::string& path,
181 bool from_sync) {
sdefresne50c1e522015-09-18 09:47:51182 syncable_prefs::PrefServiceSyncable* prefs =
183 PrefServiceSyncableFromProfile(profile_);
[email protected]0ebc4032013-08-10 07:07:11184 const PrefService::Preference* pref = prefs->FindPreference(path.c_str());
185 DCHECK(pref);
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());
[email protected]d6fba162014-06-11 11:58:49190}
[email protected]0ebc4032013-08-10 07:07:11191
192void PrefMetricsService::LogBooleanPrefChange(const std::string& histogram_name,
[email protected]cb1078de2013-12-23 20:04:22193 const base::Value* value) {
[email protected]0ebc4032013-08-10 07:07:11194 bool boolean_value = false;
195 if (!value->GetAsBoolean(&boolean_value))
196 return;
197 base::HistogramBase* histogram = base::BooleanHistogram::FactoryGet(
198 histogram_name, base::HistogramBase::kUmaTargetedHistogramFlag);
199 histogram->Add(boolean_value);
200}
201
202void PrefMetricsService::LogIntegerPrefChange(int boundary_value,
203 const std::string& histogram_name,
[email protected]cb1078de2013-12-23 20:04:22204 const base::Value* value) {
[email protected]0ebc4032013-08-10 07:07:11205 int integer_value = 0;
206 if (!value->GetAsInteger(&integer_value))
207 return;
208 base::HistogramBase* histogram = base::LinearHistogram::FactoryGet(
209 histogram_name,
210 1,
211 boundary_value,
212 boundary_value + 1,
213 base::HistogramBase::kUmaTargetedHistogramFlag);
214 histogram->Add(integer_value);
215}
216
[email protected]b544c1ae2013-07-24 18:34:13217// static
218PrefMetricsService::Factory* PrefMetricsService::Factory::GetInstance() {
olli.raula36aa8be2015-09-10 11:14:22219 return base::Singleton<PrefMetricsService::Factory>::get();
[email protected]b544c1ae2013-07-24 18:34:13220}
221
222// static
223PrefMetricsService* PrefMetricsService::Factory::GetForProfile(
224 Profile* profile) {
225 return static_cast<PrefMetricsService*>(
226 GetInstance()->GetServiceForBrowserContext(profile, true));
227}
228
229PrefMetricsService::Factory::Factory()
230 : BrowserContextKeyedServiceFactory(
231 "PrefMetricsService",
232 BrowserContextDependencyManager::GetInstance()) {
sdefresne15a87092014-10-16 14:41:51233 DependsOn(TemplateURLServiceFactory::GetInstance());
[email protected]b544c1ae2013-07-24 18:34:13234}
235
236PrefMetricsService::Factory::~Factory() {
237}
238
[email protected]540380fc2014-03-14 10:10:34239KeyedService* PrefMetricsService::Factory::BuildServiceInstanceFor(
[email protected]b544c1ae2013-07-24 18:34:13240 content::BrowserContext* profile) const {
241 return new PrefMetricsService(static_cast<Profile*>(profile));
242}
243
244bool PrefMetricsService::Factory::ServiceIsCreatedWithBrowserContext() const {
245 return true;
246}
247
248bool PrefMetricsService::Factory::ServiceIsNULLWhileTesting() const {
249 return false;
250}
251
252content::BrowserContext* PrefMetricsService::Factory::GetBrowserContextToUse(
253 content::BrowserContext* context) const {
254 return chrome::GetBrowserContextRedirectedInIncognito(context);
255}