Temporarily re-enabling SizeAfterPrefChange test with traces (this time for Linux...
[chromium-blink-merge.git] / chrome / browser / prefs / pref_metrics_service.cc
blob888655497304d37a38ec618f75140ac56cb5c130
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_prepopulate_data.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 "components/keyed_service/content/browser_context_dependency_manager.h"
25 #include "components/rappor/rappor_service.h"
26 #include "crypto/hmac.h"
27 #include "net/base/registry_controlled_domains/registry_controlled_domain.h"
29 namespace {
31 const int kSessionStartupPrefValueMax = SessionStartupPref::kPrefValueMax;
33 } // namespace
35 PrefMetricsService::PrefMetricsService(Profile* profile)
36 : profile_(profile),
37 prefs_(profile_->GetPrefs()),
38 local_state_(g_browser_process->local_state()),
39 weak_factory_(this) {
40 RecordLaunchPrefs();
42 PrefServiceSyncable* prefs = PrefServiceSyncable::FromProfile(profile_);
43 synced_pref_change_registrar_.reset(new SyncedPrefChangeRegistrar(prefs));
45 RegisterSyncedPrefObservers();
48 // For unit testing only.
49 PrefMetricsService::PrefMetricsService(Profile* profile,
50 PrefService* local_state)
51 : profile_(profile),
52 prefs_(profile->GetPrefs()),
53 local_state_(local_state),
54 weak_factory_(this) {
57 PrefMetricsService::~PrefMetricsService() {
60 void PrefMetricsService::RecordLaunchPrefs() {
61 bool show_home_button = prefs_->GetBoolean(prefs::kShowHomeButton);
62 bool home_page_is_ntp = prefs_->GetBoolean(prefs::kHomePageIsNewTabPage);
63 UMA_HISTOGRAM_BOOLEAN("Settings.ShowHomeButton", show_home_button);
64 if (show_home_button) {
65 UMA_HISTOGRAM_BOOLEAN("Settings.GivenShowHomeButton_HomePageIsNewTabPage",
66 home_page_is_ntp);
69 // For non-NTP homepages, see if the URL comes from the same TLD+1 as a known
70 // search engine. Note that this is only an approximation of search engine
71 // use, due to both false negatives (pages that come from unknown TLD+1 X but
72 // consist of a search box that sends to known TLD+1 Y) and false positives
73 // (pages that share a TLD+1 with a known engine but aren't actually search
74 // pages, e.g. plus.google.com). Additionally, record the TLD+1 of non-NTP
75 // homepages through the privacy-preserving Rappor service.
76 if (!home_page_is_ntp) {
77 GURL homepage_url(prefs_->GetString(prefs::kHomePage));
78 if (homepage_url.is_valid()) {
79 UMA_HISTOGRAM_ENUMERATION(
80 "Settings.HomePageEngineType",
81 TemplateURLPrepopulateData::GetEngineType(homepage_url),
82 SEARCH_ENGINE_MAX);
83 if (g_browser_process->rappor_service()) {
84 g_browser_process->rappor_service()->RecordSample(
85 "Settings.HomePage2",
86 rappor::ETLD_PLUS_ONE_RAPPOR_TYPE,
87 net::registry_controlled_domains::GetDomainAndRegistry(homepage_url,
88 net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES));
93 int restore_on_startup = prefs_->GetInteger(prefs::kRestoreOnStartup);
94 UMA_HISTOGRAM_ENUMERATION("Settings.StartupPageLoadSettings",
95 restore_on_startup, kSessionStartupPrefValueMax);
96 if (restore_on_startup == SessionStartupPref::kPrefValueURLs) {
97 const base::ListValue* url_list =
98 prefs_->GetList(prefs::kURLsToRestoreOnStartup);
99 UMA_HISTOGRAM_CUSTOM_COUNTS("Settings.StartupPageLoadURLs",
100 url_list->GetSize(), 1, 50, 20);
101 // Similarly, check startup pages for known search engine TLD+1s.
102 std::string url_text;
103 for (size_t i = 0; i < url_list->GetSize(); ++i) {
104 if (url_list->GetString(i, &url_text)) {
105 GURL start_url(url_text);
106 if (start_url.is_valid()) {
107 UMA_HISTOGRAM_ENUMERATION(
108 "Settings.StartupPageEngineTypes",
109 TemplateURLPrepopulateData::GetEngineType(start_url),
110 SEARCH_ENGINE_MAX);
116 #if !defined(OS_ANDROID)
117 StartupTabs startup_tabs = PinnedTabCodec::ReadPinnedTabs(profile_);
118 UMA_HISTOGRAM_CUSTOM_COUNTS("Settings.PinnedTabs",
119 startup_tabs.size(), 1, 50, 20);
120 for (size_t i = 0; i < startup_tabs.size(); ++i) {
121 GURL start_url(startup_tabs.at(i).url);
122 if (start_url.is_valid()) {
123 UMA_HISTOGRAM_ENUMERATION(
124 "Settings.PinnedTabEngineTypes",
125 TemplateURLPrepopulateData::GetEngineType(start_url),
126 SEARCH_ENGINE_MAX);
129 #endif
132 void PrefMetricsService::RegisterSyncedPrefObservers() {
133 LogHistogramValueCallback booleanHandler = base::Bind(
134 &PrefMetricsService::LogBooleanPrefChange, base::Unretained(this));
136 AddPrefObserver(prefs::kShowHomeButton, "ShowHomeButton", booleanHandler);
137 AddPrefObserver(prefs::kHomePageIsNewTabPage, "HomePageIsNewTabPage",
138 booleanHandler);
140 AddPrefObserver(prefs::kRestoreOnStartup, "StartupPageLoadSettings",
141 base::Bind(&PrefMetricsService::LogIntegerPrefChange,
142 base::Unretained(this),
143 kSessionStartupPrefValueMax));
146 void PrefMetricsService::AddPrefObserver(
147 const std::string& path,
148 const std::string& histogram_name_prefix,
149 const LogHistogramValueCallback& callback) {
150 synced_pref_change_registrar_->Add(path.c_str(),
151 base::Bind(&PrefMetricsService::OnPrefChanged,
152 base::Unretained(this),
153 histogram_name_prefix, callback));
156 void PrefMetricsService::OnPrefChanged(
157 const std::string& histogram_name_prefix,
158 const LogHistogramValueCallback& callback,
159 const std::string& path,
160 bool from_sync) {
161 PrefServiceSyncable* prefs = PrefServiceSyncable::FromProfile(profile_);
162 const PrefService::Preference* pref = prefs->FindPreference(path.c_str());
163 DCHECK(pref);
164 std::string source_name(
165 from_sync ? ".PulledFromSync" : ".PushedToSync");
166 std::string histogram_name("Settings." + histogram_name_prefix + source_name);
167 callback.Run(histogram_name, pref->GetValue());
170 void PrefMetricsService::LogBooleanPrefChange(const std::string& histogram_name,
171 const base::Value* value) {
172 bool boolean_value = false;
173 if (!value->GetAsBoolean(&boolean_value))
174 return;
175 base::HistogramBase* histogram = base::BooleanHistogram::FactoryGet(
176 histogram_name, base::HistogramBase::kUmaTargetedHistogramFlag);
177 histogram->Add(boolean_value);
180 void PrefMetricsService::LogIntegerPrefChange(int boundary_value,
181 const std::string& histogram_name,
182 const base::Value* value) {
183 int integer_value = 0;
184 if (!value->GetAsInteger(&integer_value))
185 return;
186 base::HistogramBase* histogram = base::LinearHistogram::FactoryGet(
187 histogram_name,
189 boundary_value,
190 boundary_value + 1,
191 base::HistogramBase::kUmaTargetedHistogramFlag);
192 histogram->Add(integer_value);
195 // static
196 PrefMetricsService::Factory* PrefMetricsService::Factory::GetInstance() {
197 return Singleton<PrefMetricsService::Factory>::get();
200 // static
201 PrefMetricsService* PrefMetricsService::Factory::GetForProfile(
202 Profile* profile) {
203 return static_cast<PrefMetricsService*>(
204 GetInstance()->GetServiceForBrowserContext(profile, true));
207 PrefMetricsService::Factory::Factory()
208 : BrowserContextKeyedServiceFactory(
209 "PrefMetricsService",
210 BrowserContextDependencyManager::GetInstance()) {
213 PrefMetricsService::Factory::~Factory() {
216 KeyedService* PrefMetricsService::Factory::BuildServiceInstanceFor(
217 content::BrowserContext* profile) const {
218 return new PrefMetricsService(static_cast<Profile*>(profile));
221 bool PrefMetricsService::Factory::ServiceIsCreatedWithBrowserContext() const {
222 return true;
225 bool PrefMetricsService::Factory::ServiceIsNULLWhileTesting() const {
226 return false;
229 content::BrowserContext* PrefMetricsService::Factory::GetBrowserContextToUse(
230 content::BrowserContext* context) const {
231 return chrome::GetBrowserContextRedirectedInIncognito(context);