OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/prefs/pref_metrics_service.h" | 5 #include "chrome/browser/prefs/pref_metrics_service.h" |
6 | 6 |
| 7 #include "base/bind.h" |
7 #include "base/metrics/histogram.h" | 8 #include "base/metrics/histogram.h" |
8 #include "base/prefs/pref_service.h" | 9 #include "base/prefs/pref_service.h" |
| 10 #include "chrome/browser/prefs/pref_service_syncable.h" |
9 #include "chrome/browser/prefs/session_startup_pref.h" | 11 #include "chrome/browser/prefs/session_startup_pref.h" |
| 12 #include "chrome/browser/prefs/synced_pref_change_registrar.h" |
10 #include "chrome/browser/profiles/incognito_helpers.h" | 13 #include "chrome/browser/profiles/incognito_helpers.h" |
11 #include "chrome/browser/profiles/profile.h" | 14 #include "chrome/browser/profiles/profile.h" |
12 #include "chrome/common/pref_names.h" | 15 #include "chrome/common/pref_names.h" |
13 #include "components/browser_context_keyed_service/browser_context_dependency_ma
nager.h" | 16 #include "components/browser_context_keyed_service/browser_context_dependency_ma
nager.h" |
| 17 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" |
| 18 |
| 19 namespace { |
| 20 |
| 21 // Converts a host name into a domain name for easier matching. |
| 22 std::string GetDomainFromHost(const std::string& host) { |
| 23 return net::registry_controlled_domains::GetDomainAndRegistry( |
| 24 host, |
| 25 net::registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES); |
| 26 } |
| 27 |
| 28 const int kSessionStartupPrefValueMax = SessionStartupPref::kPrefValueMax; |
| 29 |
| 30 } // namespace |
14 | 31 |
15 PrefMetricsService::PrefMetricsService(Profile* profile) | 32 PrefMetricsService::PrefMetricsService(Profile* profile) |
16 : profile_(profile) { | 33 : profile_(profile) { |
17 RecordLaunchPrefs(); | 34 RecordLaunchPrefs(); |
| 35 |
| 36 PrefServiceSyncable* prefs = PrefServiceSyncable::FromProfile(profile_); |
| 37 synced_pref_change_registrar_.reset(new SyncedPrefChangeRegistrar(prefs)); |
| 38 |
| 39 RegisterSyncedPrefObservers(); |
18 } | 40 } |
19 | 41 |
20 PrefMetricsService::~PrefMetricsService() { | 42 PrefMetricsService::~PrefMetricsService() { |
21 } | 43 } |
22 | 44 |
23 void PrefMetricsService::RecordLaunchPrefs() { | 45 void PrefMetricsService::RecordLaunchPrefs() { |
24 PrefService* prefs = profile_->GetPrefs(); | 46 PrefService* prefs = profile_->GetPrefs(); |
25 bool show_home_button = prefs->GetBoolean(prefs::kShowHomeButton); | 47 bool show_home_button = prefs->GetBoolean(prefs::kShowHomeButton); |
26 bool home_page_is_ntp = prefs->GetBoolean(prefs::kHomePageIsNewTabPage); | 48 bool home_page_is_ntp = prefs->GetBoolean(prefs::kHomePageIsNewTabPage); |
27 UMA_HISTOGRAM_BOOLEAN("Settings.ShowHomeButton", show_home_button); | 49 UMA_HISTOGRAM_BOOLEAN("Settings.ShowHomeButton", show_home_button); |
28 if (show_home_button) { | 50 if (show_home_button) { |
29 UMA_HISTOGRAM_BOOLEAN("Settings.GivenShowHomeButton_HomePageIsNewTabPage", | 51 UMA_HISTOGRAM_BOOLEAN("Settings.GivenShowHomeButton_HomePageIsNewTabPage", |
30 home_page_is_ntp); | 52 home_page_is_ntp); |
31 } | 53 } |
32 int restore_on_startup = prefs->GetInteger(prefs::kRestoreOnStartup); | 54 int restore_on_startup = prefs->GetInteger(prefs::kRestoreOnStartup); |
33 UMA_HISTOGRAM_ENUMERATION("Settings.StartupPageLoadSettings", | 55 UMA_HISTOGRAM_ENUMERATION("Settings.StartupPageLoadSettings", |
34 restore_on_startup, SessionStartupPref::kPrefValueMax); | 56 restore_on_startup, kSessionStartupPrefValueMax); |
35 if (restore_on_startup == SessionStartupPref::kPrefValueURLs) { | 57 if (restore_on_startup == SessionStartupPref::kPrefValueURLs) { |
36 const int url_list_size = prefs->GetList( | 58 const int url_list_size = prefs->GetList( |
37 prefs::kURLsToRestoreOnStartup)->GetSize(); | 59 prefs::kURLsToRestoreOnStartup)->GetSize(); |
38 UMA_HISTOGRAM_CUSTOM_COUNTS( | 60 UMA_HISTOGRAM_CUSTOM_COUNTS( |
39 "Settings.StartupPageLoadURLs", url_list_size, 1, 50, 20); | 61 "Settings.StartupPageLoadURLs", url_list_size, 1, 50, 20); |
40 } | 62 } |
41 } | 63 } |
42 | 64 |
| 65 void PrefMetricsService::RegisterSyncedPrefObservers() { |
| 66 LogHistogramValueCallback booleanHandler = base::Bind( |
| 67 &PrefMetricsService::LogBooleanPrefChange, base::Unretained(this)); |
| 68 |
| 69 AddPrefObserver(prefs::kShowHomeButton, "ShowHomeButton", booleanHandler); |
| 70 AddPrefObserver(prefs::kHomePageIsNewTabPage, "HomePageIsNewTabPage", |
| 71 booleanHandler); |
| 72 |
| 73 AddPrefObserver(prefs::kRestoreOnStartup, "StartupPageLoadSettings", |
| 74 base::Bind(&PrefMetricsService::LogIntegerPrefChange, |
| 75 base::Unretained(this), |
| 76 kSessionStartupPrefValueMax)); |
| 77 } |
| 78 |
| 79 void PrefMetricsService::AddPrefObserver( |
| 80 const std::string& path, |
| 81 const std::string& histogram_name_prefix, |
| 82 const LogHistogramValueCallback& callback) { |
| 83 synced_pref_change_registrar_->Add(path.c_str(), |
| 84 base::Bind(&PrefMetricsService::OnPrefChanged, |
| 85 base::Unretained(this), |
| 86 histogram_name_prefix, callback)); |
| 87 } |
| 88 |
| 89 void PrefMetricsService::OnPrefChanged( |
| 90 const std::string& histogram_name_prefix, |
| 91 const LogHistogramValueCallback& callback, |
| 92 const std::string& path, |
| 93 bool from_sync) { |
| 94 PrefServiceSyncable* prefs = PrefServiceSyncable::FromProfile(profile_); |
| 95 const PrefService::Preference* pref = prefs->FindPreference(path.c_str()); |
| 96 DCHECK(pref); |
| 97 std::string source_name( |
| 98 from_sync ? ".PulledFromSync" : ".PushedToSync"); |
| 99 std::string histogram_name("Settings." + histogram_name_prefix + source_name); |
| 100 callback.Run(histogram_name, pref->GetValue()); |
| 101 }; |
| 102 |
| 103 void PrefMetricsService::LogBooleanPrefChange(const std::string& histogram_name, |
| 104 const Value* value) { |
| 105 bool boolean_value = false; |
| 106 if (!value->GetAsBoolean(&boolean_value)) |
| 107 return; |
| 108 base::HistogramBase* histogram = base::BooleanHistogram::FactoryGet( |
| 109 histogram_name, base::HistogramBase::kUmaTargetedHistogramFlag); |
| 110 histogram->Add(boolean_value); |
| 111 } |
| 112 |
| 113 void PrefMetricsService::LogIntegerPrefChange(int boundary_value, |
| 114 const std::string& histogram_name, |
| 115 const Value* value) { |
| 116 int integer_value = 0; |
| 117 if (!value->GetAsInteger(&integer_value)) |
| 118 return; |
| 119 base::HistogramBase* histogram = base::LinearHistogram::FactoryGet( |
| 120 histogram_name, |
| 121 1, |
| 122 boundary_value, |
| 123 boundary_value + 1, |
| 124 base::HistogramBase::kUmaTargetedHistogramFlag); |
| 125 histogram->Add(integer_value); |
| 126 } |
| 127 |
| 128 void PrefMetricsService::LogListPrefChange( |
| 129 const LogHistogramValueCallback& item_callback, |
| 130 const std::string& histogram_name, |
| 131 const Value* value) { |
| 132 const ListValue* items = NULL; |
| 133 if (!value->GetAsList(&items)) |
| 134 return; |
| 135 for (size_t i = 0; i < items->GetSize(); ++i) { |
| 136 const Value *item_value = NULL; |
| 137 if (items->Get(i, &item_value)) |
| 138 item_callback.Run(histogram_name, item_value); |
| 139 } |
| 140 } |
| 141 |
43 // static | 142 // static |
44 PrefMetricsService::Factory* PrefMetricsService::Factory::GetInstance() { | 143 PrefMetricsService::Factory* PrefMetricsService::Factory::GetInstance() { |
45 return Singleton<PrefMetricsService::Factory>::get(); | 144 return Singleton<PrefMetricsService::Factory>::get(); |
46 } | 145 } |
47 | 146 |
48 // static | 147 // static |
49 PrefMetricsService* PrefMetricsService::Factory::GetForProfile( | 148 PrefMetricsService* PrefMetricsService::Factory::GetForProfile( |
50 Profile* profile) { | 149 Profile* profile) { |
51 return static_cast<PrefMetricsService*>( | 150 return static_cast<PrefMetricsService*>( |
52 GetInstance()->GetServiceForBrowserContext(profile, true)); | 151 GetInstance()->GetServiceForBrowserContext(profile, true)); |
(...skipping 19 matching lines...) Expand all Loading... |
72 } | 171 } |
73 | 172 |
74 bool PrefMetricsService::Factory::ServiceIsNULLWhileTesting() const { | 173 bool PrefMetricsService::Factory::ServiceIsNULLWhileTesting() const { |
75 return false; | 174 return false; |
76 } | 175 } |
77 | 176 |
78 content::BrowserContext* PrefMetricsService::Factory::GetBrowserContextToUse( | 177 content::BrowserContext* PrefMetricsService::Factory::GetBrowserContextToUse( |
79 content::BrowserContext* context) const { | 178 content::BrowserContext* context) const { |
80 return chrome::GetBrowserContextRedirectedInIncognito(context); | 179 return chrome::GetBrowserContextRedirectedInIncognito(context); |
81 } | 180 } |
OLD | NEW |