Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chrome / browser / prefs / pref_metrics_service.cc
blob5b767aaf034a536b2ba3558d38f05fdc2b8b8a3f
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_service_factory.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 "chrome/common/url_constants.h"
25 #include "components/keyed_service/content/browser_context_dependency_manager.h"
26 #include "components/rappor/rappor_utils.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"
32 namespace {
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(
41 &ntp_url,
42 profile,
43 &reverse_on_redirect);
44 if (ntp_url.is_valid()) {
45 rappor::SampleDomainAndRegistryFromGURL(g_browser_process->rappor_service(),
46 "Settings.NewTabPage", ntp_url);
50 } // namespace
52 PrefMetricsService::PrefMetricsService(Profile* profile)
53 : profile_(profile),
54 prefs_(profile_->GetPrefs()),
55 local_state_(g_browser_process->local_state()),
56 weak_factory_(this) {
57 RecordLaunchPrefs();
59 PrefServiceSyncable* prefs = PrefServiceSyncable::FromProfile(profile_);
60 synced_pref_change_registrar_.reset(new SyncedPrefChangeRegistrar(prefs));
62 RegisterSyncedPrefObservers();
65 // For unit testing only.
66 PrefMetricsService::PrefMetricsService(Profile* profile,
67 PrefService* local_state)
68 : profile_(profile),
69 prefs_(profile->GetPrefs()),
70 local_state_(local_state),
71 weak_factory_(this) {
74 PrefMetricsService::~PrefMetricsService() {
77 void PrefMetricsService::RecordLaunchPrefs() {
78 bool show_home_button = prefs_->GetBoolean(prefs::kShowHomeButton);
79 bool home_page_is_ntp = prefs_->GetBoolean(prefs::kHomePageIsNewTabPage);
80 UMA_HISTOGRAM_BOOLEAN("Settings.ShowHomeButton", show_home_button);
81 if (show_home_button) {
82 UMA_HISTOGRAM_BOOLEAN("Settings.GivenShowHomeButton_HomePageIsNewTabPage",
83 home_page_is_ntp);
86 // For non-NTP homepages, see if the URL comes from the same TLD+1 as a known
87 // search engine. Note that this is only an approximation of search engine
88 // use, due to both false negatives (pages that come from unknown TLD+1 X but
89 // consist of a search box that sends to known TLD+1 Y) and false positives
90 // (pages that share a TLD+1 with a known engine but aren't actually search
91 // pages, e.g. plus.google.com). Additionally, record the TLD+1 of non-NTP
92 // homepages through the privacy-preserving Rappor service.
93 if (!home_page_is_ntp) {
94 GURL homepage_url(prefs_->GetString(prefs::kHomePage));
95 if (homepage_url.is_valid()) {
96 UMA_HISTOGRAM_ENUMERATION(
97 "Settings.HomePageEngineType",
98 TemplateURLPrepopulateData::GetEngineType(homepage_url),
99 SEARCH_ENGINE_MAX);
100 rappor::SampleDomainAndRegistryFromGURL(
101 g_browser_process->rappor_service(), "Settings.HomePage2",
102 homepage_url);
106 SampleNewTabPageURL(profile_);
108 int restore_on_startup = prefs_->GetInteger(prefs::kRestoreOnStartup);
109 UMA_HISTOGRAM_ENUMERATION("Settings.StartupPageLoadSettings",
110 restore_on_startup, kSessionStartupPrefValueMax);
111 if (restore_on_startup == SessionStartupPref::kPrefValueURLs) {
112 const base::ListValue* url_list =
113 prefs_->GetList(prefs::kURLsToRestoreOnStartup);
114 UMA_HISTOGRAM_CUSTOM_COUNTS("Settings.StartupPageLoadURLs",
115 url_list->GetSize(), 1, 50, 20);
116 // Similarly, check startup pages for known search engine TLD+1s.
117 std::string url_text;
118 for (size_t i = 0; i < url_list->GetSize(); ++i) {
119 if (url_list->GetString(i, &url_text)) {
120 GURL start_url(url_text);
121 if (start_url.is_valid()) {
122 UMA_HISTOGRAM_ENUMERATION(
123 "Settings.StartupPageEngineTypes",
124 TemplateURLPrepopulateData::GetEngineType(start_url),
125 SEARCH_ENGINE_MAX);
126 if (i == 0) {
127 rappor::SampleDomainAndRegistryFromGURL(
128 g_browser_process->rappor_service(),
129 "Settings.FirstStartupPage", start_url);
136 #if !defined(OS_ANDROID)
137 StartupTabs startup_tabs = PinnedTabCodec::ReadPinnedTabs(profile_);
138 UMA_HISTOGRAM_CUSTOM_COUNTS("Settings.PinnedTabs",
139 startup_tabs.size(), 1, 50, 20);
140 for (size_t i = 0; i < startup_tabs.size(); ++i) {
141 GURL start_url(startup_tabs.at(i).url);
142 if (start_url.is_valid()) {
143 UMA_HISTOGRAM_ENUMERATION(
144 "Settings.PinnedTabEngineTypes",
145 TemplateURLPrepopulateData::GetEngineType(start_url),
146 SEARCH_ENGINE_MAX);
149 #endif
152 void PrefMetricsService::RegisterSyncedPrefObservers() {
153 LogHistogramValueCallback booleanHandler = base::Bind(
154 &PrefMetricsService::LogBooleanPrefChange, base::Unretained(this));
156 AddPrefObserver(prefs::kShowHomeButton, "ShowHomeButton", booleanHandler);
157 AddPrefObserver(prefs::kHomePageIsNewTabPage, "HomePageIsNewTabPage",
158 booleanHandler);
160 AddPrefObserver(prefs::kRestoreOnStartup, "StartupPageLoadSettings",
161 base::Bind(&PrefMetricsService::LogIntegerPrefChange,
162 base::Unretained(this),
163 kSessionStartupPrefValueMax));
166 void PrefMetricsService::AddPrefObserver(
167 const std::string& path,
168 const std::string& histogram_name_prefix,
169 const LogHistogramValueCallback& callback) {
170 synced_pref_change_registrar_->Add(path.c_str(),
171 base::Bind(&PrefMetricsService::OnPrefChanged,
172 base::Unretained(this),
173 histogram_name_prefix, callback));
176 void PrefMetricsService::OnPrefChanged(
177 const std::string& histogram_name_prefix,
178 const LogHistogramValueCallback& callback,
179 const std::string& path,
180 bool from_sync) {
181 PrefServiceSyncable* prefs = PrefServiceSyncable::FromProfile(profile_);
182 const PrefService::Preference* pref = prefs->FindPreference(path.c_str());
183 DCHECK(pref);
184 std::string source_name(
185 from_sync ? ".PulledFromSync" : ".PushedToSync");
186 std::string histogram_name("Settings." + histogram_name_prefix + source_name);
187 callback.Run(histogram_name, pref->GetValue());
190 void PrefMetricsService::LogBooleanPrefChange(const std::string& histogram_name,
191 const base::Value* value) {
192 bool boolean_value = false;
193 if (!value->GetAsBoolean(&boolean_value))
194 return;
195 base::HistogramBase* histogram = base::BooleanHistogram::FactoryGet(
196 histogram_name, base::HistogramBase::kUmaTargetedHistogramFlag);
197 histogram->Add(boolean_value);
200 void PrefMetricsService::LogIntegerPrefChange(int boundary_value,
201 const std::string& histogram_name,
202 const base::Value* value) {
203 int integer_value = 0;
204 if (!value->GetAsInteger(&integer_value))
205 return;
206 base::HistogramBase* histogram = base::LinearHistogram::FactoryGet(
207 histogram_name,
209 boundary_value,
210 boundary_value + 1,
211 base::HistogramBase::kUmaTargetedHistogramFlag);
212 histogram->Add(integer_value);
215 // static
216 PrefMetricsService::Factory* PrefMetricsService::Factory::GetInstance() {
217 return Singleton<PrefMetricsService::Factory>::get();
220 // static
221 PrefMetricsService* PrefMetricsService::Factory::GetForProfile(
222 Profile* profile) {
223 return static_cast<PrefMetricsService*>(
224 GetInstance()->GetServiceForBrowserContext(profile, true));
227 PrefMetricsService::Factory::Factory()
228 : BrowserContextKeyedServiceFactory(
229 "PrefMetricsService",
230 BrowserContextDependencyManager::GetInstance()) {
231 DependsOn(TemplateURLServiceFactory::GetInstance());
234 PrefMetricsService::Factory::~Factory() {
237 KeyedService* PrefMetricsService::Factory::BuildServiceInstanceFor(
238 content::BrowserContext* profile) const {
239 return new PrefMetricsService(static_cast<Profile*>(profile));
242 bool PrefMetricsService::Factory::ServiceIsCreatedWithBrowserContext() const {
243 return true;
246 bool PrefMetricsService::Factory::ServiceIsNULLWhileTesting() const {
247 return false;
250 content::BrowserContext* PrefMetricsService::Factory::GetBrowserContextToUse(
251 content::BrowserContext* context) const {
252 return chrome::GetBrowserContextRedirectedInIncognito(context);