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 <map> | 7 #include <map> |
8 | 8 |
9 #include "base/metrics/histogram.h" | 9 #include "base/metrics/histogram.h" |
10 #include "base/prefs/pref_service.h" | 10 #include "base/prefs/pref_service.h" |
11 #include "chrome/browser/prefs/pref_service_syncable.h" | |
11 #include "chrome/browser/prefs/session_startup_pref.h" | 12 #include "chrome/browser/prefs/session_startup_pref.h" |
12 #include "chrome/browser/profiles/incognito_helpers.h" | 13 #include "chrome/browser/profiles/incognito_helpers.h" |
13 #include "chrome/browser/profiles/profile.h" | 14 #include "chrome/browser/profiles/profile.h" |
14 #include "chrome/browser/search_engines/template_url_prepopulate_data.h" | 15 #include "chrome/browser/search_engines/template_url_prepopulate_data.h" |
15 #include "chrome/browser/search_engines/template_url_service.h" | 16 #include "chrome/browser/search_engines/template_url_service.h" |
16 #include "chrome/browser/search_engines/template_url_service_factory.h" | 17 #include "chrome/browser/search_engines/template_url_service_factory.h" |
18 #include "chrome/browser/sync/profile_sync_service.h" | |
19 #include "chrome/browser/sync/profile_sync_service_factory.h" | |
17 #include "chrome/common/pref_names.h" | 20 #include "chrome/common/pref_names.h" |
18 #include "components/browser_context_keyed_service/browser_context_dependency_ma nager.h" | 21 #include "components/browser_context_keyed_service/browser_context_dependency_ma nager.h" |
19 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" | 22 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" |
20 | 23 |
21 using TemplateURLPrepopulateData::kMaxPrepopulatedEngineID; | 24 using TemplateURLPrepopulateData::kMaxPrepopulatedEngineID; |
22 | 25 |
23 namespace { | 26 namespace { |
24 | 27 |
25 // Use a map to convert domains to their histogram identifiers. Ids are defined | |
26 // in tools/metrics/histograms/histograms.xml and (usually) also in | |
27 // chrome/browser/search_engines/prepopulated_engines.json. | |
28 typedef std::map<std::string, int> DomainIdMap; | |
29 | |
30 // Converts a host name into a domain name for easier matching. | 28 // Converts a host name into a domain name for easier matching. |
31 std::string GetDomain(const std::string& host) { | 29 std::string GetDomainFromHost(const std::string& host) { |
32 return net::registry_controlled_domains::GetDomainAndRegistry( | 30 return net::registry_controlled_domains::GetDomainAndRegistry( |
33 host, | 31 host, |
34 net::registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES); | 32 net::registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES); |
35 } | 33 } |
36 | 34 |
37 void AddDomain(const std::string& domain, | |
38 int domain_id, | |
39 DomainIdMap* domain_id_map) { | |
40 domain_id_map->insert(std::make_pair(domain, domain_id)); | |
41 } | |
42 | |
43 // Builds a map that associates domain name strings with histogram enum values, | |
44 // for prepopulated DSEs and select non-prepopulated ones. | |
45 void BuildDomainIdMap(Profile* profile, DomainIdMap* domain_id_map) { | |
46 // Add prepopulated search engine hosts to the map. | |
47 ScopedVector<TemplateURL> prepopulated_urls; | |
48 size_t default_search_index; // unused | |
49 TemplateURLPrepopulateData::GetPrepopulatedEngines(profile, | |
50 &prepopulated_urls.get(), &default_search_index); | |
51 for (size_t i = 0; i < prepopulated_urls.size(); ++i) { | |
52 AddDomain(GetDomain(prepopulated_urls[i]->url_ref().GetHost()), | |
53 prepopulated_urls[i]->prepopulate_id(), | |
54 domain_id_map); | |
55 } | |
56 // Add some common search engine domains that are not prepopulated. Assign | |
57 // these domains id numbers 102-114 which extend the prepopulated engines | |
58 // histogram enum. | |
59 AddDomain("searchnu.com", 102, domain_id_map); | |
60 AddDomain("babylon.com", 103, domain_id_map); | |
61 AddDomain("delta-search.com", 104, domain_id_map); | |
62 AddDomain("iminent.com", 105, domain_id_map); | |
63 AddDomain("hao123.com", 106, domain_id_map); | |
64 AddDomain("sweetim.com", 107, domain_id_map); | |
65 AddDomain("snap.do", 108, domain_id_map); | |
66 AddDomain("snapdo.com", 109, domain_id_map); | |
67 AddDomain("softonic.com", 110, domain_id_map); | |
68 AddDomain("searchfunmoods.com", 111, domain_id_map); | |
69 AddDomain("incredibar.com", 112, domain_id_map); | |
70 AddDomain("sweetpacks.com", 113, domain_id_map); | |
71 AddDomain("imesh.net", 114, domain_id_map); | |
72 // IMPORTANT: If you add more domains here, be sure to update the | |
73 // kMaxPrepopulatedEngineID and available ids in prepopulated_engines.json. | |
74 | |
75 // The following hosts may not be prepopulated, depending on the country | |
76 // settings. Add them here, using their existing ids. See histograms.xml. | |
77 AddDomain("conduit.com", 36, domain_id_map); | |
78 AddDomain("avg.com", 50, domain_id_map); | |
79 AddDomain("mail.ru", 83, domain_id_map); | |
80 } | |
81 | |
82 // Maps a host name to a histogram enum value. The enum value '0' represents | |
83 // 'Unknown', i.e. an unrecognized host. | |
84 int MapHostToId(const DomainIdMap& domain_id_map, const std::string& host) { | |
85 DomainIdMap::const_iterator it = domain_id_map.find(GetDomain(host)); | |
86 if (it != domain_id_map.end()) | |
87 return it->second; | |
88 return 0; | |
89 } | |
90 | |
91 } // namespace | 35 } // namespace |
92 | 36 |
93 PrefMetricsService::PrefMetricsService(Profile* profile) | 37 PrefMetricsService::PrefMetricsService(Profile* profile) |
94 : profile_(profile) { | 38 : profile_(profile) { |
39 BuildDomainIdMap(); | |
95 RecordLaunchPrefs(); | 40 RecordLaunchPrefs(); |
41 | |
42 PrefServiceSyncable* prefs = PrefServiceSyncable::FromProfile(profile_); | |
43 synced_pref_change_registrar_.reset(new SyncedPrefChangeRegistrar(prefs)); | |
44 | |
45 RegisterSyncedPrefObservers(); | |
96 } | 46 } |
97 | 47 |
98 PrefMetricsService::~PrefMetricsService() { | 48 PrefMetricsService::~PrefMetricsService() { |
99 } | 49 } |
100 | 50 |
101 void PrefMetricsService::RecordLaunchPrefs() { | 51 void PrefMetricsService::RecordLaunchPrefs() { |
102 PrefService* prefs = profile_->GetPrefs(); | 52 PrefService* prefs = profile_->GetPrefs(); |
103 const bool show_home_button = prefs->GetBoolean(prefs::kShowHomeButton); | 53 const bool show_home_button = prefs->GetBoolean(prefs::kShowHomeButton); |
104 const bool home_page_is_ntp = prefs->GetBoolean(prefs::kHomePageIsNewTabPage); | 54 const bool home_page_is_ntp = prefs->GetBoolean(prefs::kHomePageIsNewTabPage); |
105 | 55 |
106 UMA_HISTOGRAM_BOOLEAN("Settings.ShowHomeButton", show_home_button); | 56 UMA_HISTOGRAM_BOOLEAN("Settings.ShowHomeButton", show_home_button); |
107 UMA_HISTOGRAM_BOOLEAN("Settings.HomePageIsNewTabPage", home_page_is_ntp); | 57 UMA_HISTOGRAM_BOOLEAN("Settings.HomePageIsNewTabPage", home_page_is_ntp); |
108 | 58 |
109 int restore_on_startup = profile_->GetPrefs()->GetInteger( | 59 int restore_on_startup = profile_->GetPrefs()->GetInteger( |
110 prefs::kRestoreOnStartup); | 60 prefs::kRestoreOnStartup); |
111 UMA_HISTOGRAM_ENUMERATION("Settings.StartupPageLoadSettings", | 61 UMA_HISTOGRAM_ENUMERATION("Settings.StartupPageLoadSettings", |
112 restore_on_startup, SessionStartupPref::kPrefValueMax); | 62 restore_on_startup, SessionStartupPref::kPrefValueMax); |
113 if (restore_on_startup == SessionStartupPref::kPrefValueURLs) { | 63 if (restore_on_startup == SessionStartupPref::kPrefValueURLs) { |
114 const int url_list_size = profile_->GetPrefs()->GetList( | 64 const int url_list_size = profile_->GetPrefs()->GetList( |
115 prefs::kURLsToRestoreOnStartup)->GetSize(); | 65 prefs::kURLsToRestoreOnStartup)->GetSize(); |
116 UMA_HISTOGRAM_CUSTOM_COUNTS( | 66 UMA_HISTOGRAM_CUSTOM_COUNTS( |
117 "Settings.StartupPageLoadURLs", url_list_size, 1, 50, 20); | 67 "Settings.StartupPageLoadURLs", url_list_size, 1, 50, 20); |
118 } | 68 } |
119 | 69 |
120 DomainIdMap domain_id_map; | |
121 BuildDomainIdMap(profile_, &domain_id_map); | |
122 | |
123 // Record the default search engine id. | 70 // Record the default search engine id. |
124 TemplateURLService* template_url_service = | 71 TemplateURLService* template_url_service = |
125 TemplateURLServiceFactory::GetForProfile(profile_); | 72 TemplateURLServiceFactory::GetForProfile(profile_); |
126 if (template_url_service) { | 73 if (template_url_service) { |
127 TemplateURL* template_url = | 74 TemplateURL* template_url = |
128 template_url_service->GetDefaultSearchProvider(); | 75 template_url_service->GetDefaultSearchProvider(); |
129 if (template_url) { | 76 if (template_url) { |
130 const int domain_id = | 77 const int domain_id = MapHostToId(template_url->url_ref().GetHost()); |
131 MapHostToId(domain_id_map, template_url->url_ref().GetHost()); | |
132 UMA_HISTOGRAM_ENUMERATION("Settings.DefaultSearchProvider", | 78 UMA_HISTOGRAM_ENUMERATION("Settings.DefaultSearchProvider", |
133 domain_id, kMaxPrepopulatedEngineID); | 79 domain_id, kMaxPrepopulatedEngineID); |
134 } | 80 } |
135 } | 81 } |
136 // If the home page isn't the NTP, record the home page domain id. | 82 // If the home page isn't the NTP, record the home page domain id. |
137 if (!home_page_is_ntp) { | 83 if (!home_page_is_ntp) { |
138 GURL homepage_url(prefs->GetString(prefs::kHomePage)); | 84 GURL homepage_url(prefs->GetString(prefs::kHomePage)); |
139 if (homepage_url.is_valid()) { | 85 if (homepage_url.is_valid()) { |
140 const int domain_id = MapHostToId(domain_id_map, homepage_url.host()); | 86 const int domain_id = MapHostToId(homepage_url.host()); |
141 UMA_HISTOGRAM_ENUMERATION("Settings.HomePageDomain", | 87 UMA_HISTOGRAM_ENUMERATION("Settings.HomePageDomain", |
142 domain_id, kMaxPrepopulatedEngineID); | 88 domain_id, kMaxPrepopulatedEngineID); |
143 } | 89 } |
144 } | 90 } |
145 // If startup pages are set, record all startup page domain ids. | 91 // If startup pages are set, record all startup page domain ids. |
146 if (restore_on_startup == SessionStartupPref::kPrefValueURLs) { | 92 if (restore_on_startup == SessionStartupPref::kPrefValueURLs) { |
147 const ListValue* url_list = prefs->GetList(prefs::kURLsToRestoreOnStartup); | 93 const ListValue* url_list = prefs->GetList(prefs::kURLsToRestoreOnStartup); |
148 for (size_t i = 0; i < url_list->GetSize(); i++) { | 94 for (size_t i = 0; i < url_list->GetSize(); i++) { |
149 std::string url_text; | 95 std::string url_text; |
150 if (url_list->GetString(i, &url_text)) { | 96 if (url_list->GetString(i, &url_text)) { |
151 GURL start_url(url_text); | 97 GURL start_url(url_text); |
152 if (start_url.is_valid()) { | 98 if (start_url.is_valid()) { |
153 const int domain_id = MapHostToId(domain_id_map, start_url.host()); | 99 const int domain_id = MapHostToId(start_url.host()); |
154 UMA_HISTOGRAM_ENUMERATION("Settings.StartupPageDomains", | 100 UMA_HISTOGRAM_ENUMERATION("Settings.StartupPageDomains", |
155 domain_id, kMaxPrepopulatedEngineID); | 101 domain_id, kMaxPrepopulatedEngineID); |
156 } | 102 } |
157 } | 103 } |
158 } | 104 } |
159 } | 105 } |
160 } | 106 } |
161 | 107 |
108 void PrefMetricsService::RegisterSyncedPrefObservers() { | |
109 ValueHistogramCallback booleanHandler = base::Bind( | |
110 &PrefMetricsService::LogBooleanPrefChange, base::Unretained(this)); | |
111 ValueHistogramCallback urlHandler = base::Bind( | |
112 &PrefMetricsService::LogUrlPrefChange, base::Unretained(this)); | |
113 | |
114 AddPrefObserver(prefs::kShowHomeButton, "ShowHomeButton", booleanHandler); | |
115 AddPrefObserver(prefs::kHomePageIsNewTabPage, "HomePageIsNewTabPage", | |
116 booleanHandler); | |
117 AddPrefObserver(prefs::kHomePage, "HomePageDomain", urlHandler); | |
118 | |
119 const int startupPrefValueMax = SessionStartupPref::kPrefValueMax; | |
120 AddPrefObserver(prefs::kRestoreOnStartup, "StartupPageLoadSettings", | |
121 base::Bind(&PrefMetricsService::LogIntegerPrefChange, | |
122 base::Unretained(this), | |
123 startupPrefValueMax)); | |
124 | |
125 AddPrefObserver(prefs::kURLsToRestoreOnStartup, "StartupPageDomains", | |
126 base::Bind(&PrefMetricsService::LogListPrefChange, | |
127 base::Unretained(this), | |
128 urlHandler)); | |
129 } | |
130 | |
131 void PrefMetricsService::AddPrefObserver( | |
132 const std::string& path, | |
133 const std::string& histogram_base, | |
134 const ValueHistogramCallback& callback) { | |
135 synced_pref_change_registrar_->Add(path.c_str(), | |
136 base::Bind(&PrefMetricsService::OnPrefChanged, | |
137 base::Unretained(this), | |
138 histogram_base, callback)); | |
139 } | |
140 | |
141 void PrefMetricsService::BuildDomainIdMap() { | |
142 // Add prepopulated search engine hosts to the map. | |
143 ScopedVector<TemplateURL> prepopulated_urls; | |
144 size_t default_search_index; // unused | |
145 TemplateURLPrepopulateData::GetPrepopulatedEngines(profile_, | |
146 &prepopulated_urls.get(), &default_search_index); | |
147 for (size_t i = 0; i < prepopulated_urls.size(); ++i) { | |
148 AddToDomainIdMap( | |
149 GetDomainFromHost(prepopulated_urls[i]->url_ref().GetHost()), | |
150 prepopulated_urls[i]->prepopulate_id()); | |
151 } | |
152 // Add some common search engine domains that are not prepopulated. Assign | |
153 // these domains id numbers 102-114 which extend the prepopulated engines | |
154 // histogram enum. | |
155 AddToDomainIdMap("searchnu.com", 102); | |
156 AddToDomainIdMap("babylon.com", 103); | |
157 AddToDomainIdMap("delta-search.com", 104); | |
158 AddToDomainIdMap("iminent.com", 105); | |
159 AddToDomainIdMap("hao123.com", 106); | |
160 AddToDomainIdMap("sweetim.com", 107); | |
161 AddToDomainIdMap("snap.do", 108); | |
162 AddToDomainIdMap("snapdo.com", 109); | |
163 AddToDomainIdMap("softonic.com", 110); | |
164 AddToDomainIdMap("searchfunmoods.com", 111); | |
165 AddToDomainIdMap("incredibar.com", 112); | |
166 AddToDomainIdMap("sweetpacks.com", 113); | |
167 AddToDomainIdMap("imesh.net", 114); | |
168 // IMPORTANT: If you add more domains here, be sure to update the | |
169 // kMaxPrepopulatedEngineID and available ids in prepopulated_engines.json. | |
170 | |
171 // The following hosts may not be prepopulated, depending on the country | |
172 // settings. Add them here, using their existing ids. See histograms.xml. | |
173 AddToDomainIdMap("conduit.com", 36); | |
174 AddToDomainIdMap("avg.com", 50); | |
175 AddToDomainIdMap("mail.ru", 83); | |
176 } | |
177 | |
178 void PrefMetricsService::AddToDomainIdMap(const std::string& domain, | |
179 int domain_id) { | |
180 domain_id_map_.insert(std::make_pair(domain, domain_id)); | |
181 } | |
182 | |
183 int PrefMetricsService::MapHostToId(const std::string& host) const { | |
184 DomainIdMap::const_iterator it = domain_id_map_.find( | |
185 GetDomainFromHost(host)); | |
186 if (it != domain_id_map_.end()) | |
187 return it->second; | |
188 return 0; | |
189 } | |
190 | |
191 void PrefMetricsService::OnPrefChanged(const std::string& histogram_base, | |
192 const ValueHistogramCallback& callback, | |
193 const std::string& path, | |
194 bool from_sync) { | |
195 PrefServiceSyncable* prefs = PrefServiceSyncable::FromProfile(profile_); | |
196 const PrefService::Preference* pref = prefs->FindPreference(path.c_str()); | |
197 DCHECK(pref); | |
198 std::string source_name( | |
199 from_sync ? ".PulledFromSync" : "PushedToSync"); | |
bbudge
2013/08/01 17:48:08
Why is one prefixed with '.' and not the other?
Ken Rockot(use gerrit already)
2013/08/01 17:55:15
Good catch. Fixed.
| |
200 std::string histogram_name("Settings." + histogram_base + source_name); | |
201 callback.Run(histogram_name, pref->GetValue()); | |
202 }; | |
203 | |
204 void PrefMetricsService::LogBooleanPrefChange(const std::string& histogram_name, | |
205 const Value* value) { | |
206 bool boolean_value = false; | |
207 if (!value->GetAsBoolean(&boolean_value)) | |
208 return; | |
209 UMA_HISTOGRAM_BOOLEAN(histogram_name.c_str(), boolean_value); | |
210 } | |
211 | |
212 void PrefMetricsService::LogIntegerPrefChange(int boundary_value, | |
213 const std::string& histogram_name, | |
214 const Value* value) { | |
215 int integer_value = 0; | |
216 if (!value->GetAsInteger(&integer_value)) | |
217 return; | |
218 UMA_HISTOGRAM_ENUMERATION(histogram_name.c_str(), integer_value, | |
219 boundary_value); | |
220 } | |
221 | |
222 void PrefMetricsService::LogUrlPrefChange(const std::string& histogram_name, | |
223 const Value* value) { | |
224 std::string string_value; | |
225 if (!value->GetAsString(&string_value)) | |
226 return; | |
227 GURL url(string_value); | |
228 if (url.is_valid()) { | |
229 int domain_id = MapHostToId(url.host()); | |
230 UMA_HISTOGRAM_ENUMERATION(histogram_name.c_str(), domain_id, | |
231 kMaxPrepopulatedEngineID); | |
232 } | |
233 } | |
234 | |
235 void PrefMetricsService::LogListPrefChange( | |
236 const ValueHistogramCallback& item_callback, | |
237 const std::string& histogram_name, | |
238 const Value* value) { | |
239 const ListValue* items = NULL; | |
240 if (!value->GetAsList(&items)) | |
241 return; | |
242 for (size_t i = 0; i < items->GetSize(); ++i) { | |
243 const Value *item_value = NULL; | |
244 if (items->Get(i, &item_value)) { | |
245 item_callback.Run(histogram_name, item_value); | |
246 } | |
247 } | |
248 } | |
249 | |
162 // static | 250 // static |
163 PrefMetricsService::Factory* PrefMetricsService::Factory::GetInstance() { | 251 PrefMetricsService::Factory* PrefMetricsService::Factory::GetInstance() { |
164 return Singleton<PrefMetricsService::Factory>::get(); | 252 return Singleton<PrefMetricsService::Factory>::get(); |
165 } | 253 } |
166 | 254 |
167 // static | 255 // static |
168 PrefMetricsService* PrefMetricsService::Factory::GetForProfile( | 256 PrefMetricsService* PrefMetricsService::Factory::GetForProfile( |
169 Profile* profile) { | 257 Profile* profile) { |
170 return static_cast<PrefMetricsService*>( | 258 return static_cast<PrefMetricsService*>( |
171 GetInstance()->GetServiceForBrowserContext(profile, true)); | 259 GetInstance()->GetServiceForBrowserContext(profile, true)); |
172 } | 260 } |
173 | 261 |
174 PrefMetricsService::Factory::Factory() | 262 PrefMetricsService::Factory::Factory() |
175 : BrowserContextKeyedServiceFactory( | 263 : BrowserContextKeyedServiceFactory( |
176 "PrefMetricsService", | 264 "PrefMetricsService", |
177 BrowserContextDependencyManager::GetInstance()) { | 265 BrowserContextDependencyManager::GetInstance()) { |
178 DependsOn(TemplateURLServiceFactory::GetInstance()); | 266 DependsOn(TemplateURLServiceFactory::GetInstance()); |
267 DependsOn(ProfileSyncServiceFactory::GetInstance()); | |
179 } | 268 } |
180 | 269 |
181 PrefMetricsService::Factory::~Factory() { | 270 PrefMetricsService::Factory::~Factory() { |
182 } | 271 } |
183 | 272 |
184 BrowserContextKeyedService* | 273 BrowserContextKeyedService* |
185 PrefMetricsService::Factory::BuildServiceInstanceFor( | 274 PrefMetricsService::Factory::BuildServiceInstanceFor( |
186 content::BrowserContext* profile) const { | 275 content::BrowserContext* profile) const { |
187 return new PrefMetricsService(static_cast<Profile*>(profile)); | 276 return new PrefMetricsService(static_cast<Profile*>(profile)); |
188 } | 277 } |
189 | 278 |
190 bool PrefMetricsService::Factory::ServiceIsCreatedWithBrowserContext() const { | 279 bool PrefMetricsService::Factory::ServiceIsCreatedWithBrowserContext() const { |
191 return true; | 280 return true; |
192 } | 281 } |
193 | 282 |
194 bool PrefMetricsService::Factory::ServiceIsNULLWhileTesting() const { | 283 bool PrefMetricsService::Factory::ServiceIsNULLWhileTesting() const { |
195 return false; | 284 return false; |
196 } | 285 } |
197 | 286 |
198 content::BrowserContext* PrefMetricsService::Factory::GetBrowserContextToUse( | 287 content::BrowserContext* PrefMetricsService::Factory::GetBrowserContextToUse( |
199 content::BrowserContext* context) const { | 288 content::BrowserContext* context) const { |
200 return chrome::GetBrowserContextRedirectedInIncognito(context); | 289 return chrome::GetBrowserContextRedirectedInIncognito(context); |
201 } | 290 } |
OLD | NEW |