Chromium Code Reviews| 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> | |
| 8 | |
| 7 #include "base/metrics/histogram.h" | 9 #include "base/metrics/histogram.h" |
| 8 #include "base/prefs/pref_service.h" | 10 #include "base/prefs/pref_service.h" |
| 11 #include "chrome/browser/prefs/session_startup_pref.h" | |
| 9 #include "chrome/browser/profiles/incognito_helpers.h" | 12 #include "chrome/browser/profiles/incognito_helpers.h" |
| 10 #include "chrome/browser/profiles/profile.h" | 13 #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_service.h" | |
| 16 #include "chrome/browser/search_engines/template_url_service_factory.h" | |
| 11 #include "chrome/common/pref_names.h" | 17 #include "chrome/common/pref_names.h" |
| 12 #include "components/browser_context_keyed_service/browser_context_dependency_ma nager.h" | 18 #include "components/browser_context_keyed_service/browser_context_dependency_ma nager.h" |
| 19 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" | |
| 20 | |
| 21 using TemplateURLPrepopulateData::kMaxPrepopulatedEngineID; | |
| 22 | |
| 23 namespace { | |
| 24 | |
| 25 // Use a map to convert domains to their histogram identifiers. Ids are defined | |
| 26 // in tools/metrics/histograms/histograms.xml. | |
|
Mark P
2013/07/30 19:02:38
and (often) also prepopulated_...
bbudge
2013/07/30 19:49:49
Done.
| |
| 27 typedef std::map<std::string, int> DomainIdMap; | |
| 28 | |
| 29 // Returns the domain of the host name for easier matching. | |
| 30 std::string GetDomain(const std::string& host) { | |
| 31 return net::registry_controlled_domains::GetDomainAndRegistry( | |
| 32 host, | |
| 33 net::registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES); | |
| 34 } | |
| 35 | |
| 36 void AddDomain(const std::string& domain, | |
| 37 int domain_id, | |
| 38 DomainIdMap* domain_id_map) { | |
| 39 domain_id_map->insert(std::make_pair(domain, domain_id)); | |
| 40 } | |
| 41 | |
| 42 // Builds a map that associates domain name strings with histogram enum values, | |
| 43 // for prepopulated DSEs and select non-prepopulated ones. | |
| 44 void BuildDomainIdMap(Profile* profile, DomainIdMap* domain_id_map) { | |
| 45 // Add prepopulated search engine hosts to the map. | |
| 46 ScopedVector<TemplateURL> prepopulated_urls; | |
| 47 size_t default_search_index; // unused | |
| 48 TemplateURLPrepopulateData::GetPrepopulatedEngines(profile, | |
| 49 &prepopulated_urls.get(), &default_search_index); | |
| 50 for (size_t i = 0; i < prepopulated_urls.size(); ++i) { | |
| 51 AddDomain(GetDomain(prepopulated_urls[i]->url_ref().GetHost()), | |
| 52 prepopulated_urls[i]->prepopulate_id(), | |
| 53 domain_id_map); | |
| 54 } | |
| 55 // Add some common search engine domains that are not prepopulated. Assign | |
| 56 // these domains id numbers 102-114 which extend the prepopulated engines | |
| 57 // histogram enum. | |
| 58 AddDomain("searchnu.com", 102, domain_id_map); | |
| 59 AddDomain("babylon.com", 103, domain_id_map); | |
| 60 AddDomain("delta-search.com", 104, domain_id_map); | |
| 61 AddDomain("iminent.com", 105, domain_id_map); | |
| 62 AddDomain("hao123.com", 106, domain_id_map); | |
| 63 AddDomain("sweetim.com", 107, domain_id_map); | |
| 64 AddDomain("snap.do", 108, domain_id_map); | |
| 65 AddDomain("snapdo.com", 109, domain_id_map); | |
| 66 AddDomain("softonic.com", 110, domain_id_map); | |
| 67 AddDomain("searchfunmoods.com", 111, domain_id_map); | |
| 68 AddDomain("incredibar.com", 112, domain_id_map); | |
| 69 AddDomain("sweetpacks.com", 113, domain_id_map); | |
| 70 AddDomain("imesh.net", 114, domain_id_map); | |
| 71 // IMPORTANT: If you add more domains here, be sure to update the | |
| 72 // kMaxPrepopulatedEngineID and available ids in prepopulated_engines.json. | |
| 73 | |
| 74 // The following hosts may not be prepopulated, depending on the country | |
| 75 // settings. Add them here, using their existing ids. See histograms.xml. | |
| 76 AddDomain("conduit.com", 36, domain_id_map); | |
| 77 AddDomain("avg.com", 50, domain_id_map); | |
| 78 AddDomain("mail.ru", 83, domain_id_map); | |
| 79 } | |
| 80 | |
| 81 // Maps a host name to a histogram enum value. The enum value '0' represents | |
| 82 // 'Unknown', i.e. an unrecognized host. | |
| 83 int MapHostToId(const DomainIdMap& domain_id_map, const std::string& host) { | |
| 84 DomainIdMap::const_iterator it = domain_id_map.find(GetDomain(host)); | |
| 85 if (it != domain_id_map.end()) | |
| 86 return it->second; | |
| 87 return 0; | |
| 88 } | |
| 89 | |
| 90 } // namespace | |
| 13 | 91 |
| 14 PrefMetricsService::PrefMetricsService(Profile* profile) | 92 PrefMetricsService::PrefMetricsService(Profile* profile) |
| 15 : profile_(profile) { | 93 : profile_(profile) { |
| 16 RecordLaunchPrefs(); | 94 RecordLaunchPrefs(); |
| 17 } | 95 } |
| 18 | 96 |
| 19 PrefMetricsService::~PrefMetricsService() { | 97 PrefMetricsService::~PrefMetricsService() { |
| 20 } | 98 } |
| 21 | 99 |
| 22 void PrefMetricsService::RecordLaunchPrefs() { | 100 void PrefMetricsService::RecordLaunchPrefs() { |
| 23 UMA_HISTOGRAM_BOOLEAN("Settings.ShowHomeButton", | 101 PrefService* prefs = profile_->GetPrefs(); |
| 24 profile_->GetPrefs()->GetBoolean(prefs::kShowHomeButton)); | 102 const bool show_home_button = prefs->GetBoolean(prefs::kShowHomeButton); |
| 25 UMA_HISTOGRAM_BOOLEAN("Settings.HomePageIsNewTabPage", | 103 const bool home_page_is_ntp = prefs->GetBoolean(prefs::kHomePageIsNewTabPage); |
| 26 profile_->GetPrefs()->GetBoolean(prefs::kHomePageIsNewTabPage)); | 104 |
| 105 UMA_HISTOGRAM_BOOLEAN("Settings.ShowHomeButton", show_home_button); | |
| 106 UMA_HISTOGRAM_BOOLEAN("Settings.HomePageIsNewTabPage", home_page_is_ntp); | |
| 107 | |
| 108 DomainIdMap domain_id_map; | |
| 109 BuildDomainIdMap(profile_, &domain_id_map); | |
| 110 | |
| 111 // Record the default search engine id. | |
| 112 TemplateURLService* template_url_service = | |
| 113 TemplateURLServiceFactory::GetForProfile(profile_); | |
| 114 if (template_url_service) { | |
| 115 TemplateURL* template_url = | |
| 116 template_url_service->GetDefaultSearchProvider(); | |
| 117 if (template_url) { | |
| 118 const int domain_id = | |
| 119 MapHostToId(domain_id_map, template_url->url_ref().GetHost()); | |
| 120 UMA_HISTOGRAM_ENUMERATION("Settings.DefaultSearchProvider", | |
| 121 domain_id, kMaxPrepopulatedEngineID); | |
| 122 } | |
| 123 } | |
| 124 // If the home page isn't the NTP, record the home page domain id. | |
| 125 if (!home_page_is_ntp) { | |
| 126 GURL homepage_url(prefs->GetString(prefs::kHomePage)); | |
| 127 if (homepage_url.is_valid()) { | |
| 128 const int domain_id = MapHostToId(domain_id_map, homepage_url.host()); | |
| 129 UMA_HISTOGRAM_ENUMERATION("Settings.HomePageDomain", | |
| 130 domain_id, kMaxPrepopulatedEngineID); | |
| 131 } | |
| 132 } | |
| 133 // If startup pages are set, record all startup page domain ids. | |
| 134 int restore_on_startup = prefs->GetInteger(prefs::kRestoreOnStartup); | |
| 135 if (restore_on_startup == SessionStartupPref::kPrefValueURLs) { | |
| 136 const ListValue* url_list = prefs->GetList(prefs::kURLsToRestoreOnStartup); | |
| 137 for (size_t i = 0; i < url_list->GetSize(); i++) { | |
| 138 std::string url_text; | |
| 139 if (url_list->GetString(i, &url_text)) { | |
| 140 GURL start_url(url_text); | |
| 141 if (start_url.is_valid()) { | |
| 142 const int domain_id = MapHostToId(domain_id_map, start_url.host()); | |
| 143 UMA_HISTOGRAM_ENUMERATION("Settings.StartPageDomains", | |
| 144 domain_id, kMaxPrepopulatedEngineID); | |
| 145 } | |
| 146 } | |
| 147 } | |
| 148 } | |
| 27 } | 149 } |
| 28 | 150 |
| 29 // static | 151 // static |
| 30 PrefMetricsService::Factory* PrefMetricsService::Factory::GetInstance() { | 152 PrefMetricsService::Factory* PrefMetricsService::Factory::GetInstance() { |
| 31 return Singleton<PrefMetricsService::Factory>::get(); | 153 return Singleton<PrefMetricsService::Factory>::get(); |
| 32 } | 154 } |
| 33 | 155 |
| 34 // static | 156 // static |
| 35 PrefMetricsService* PrefMetricsService::Factory::GetForProfile( | 157 PrefMetricsService* PrefMetricsService::Factory::GetForProfile( |
| 36 Profile* profile) { | 158 Profile* profile) { |
| 37 return static_cast<PrefMetricsService*>( | 159 return static_cast<PrefMetricsService*>( |
| 38 GetInstance()->GetServiceForBrowserContext(profile, true)); | 160 GetInstance()->GetServiceForBrowserContext(profile, true)); |
| 39 } | 161 } |
| 40 | 162 |
| 41 PrefMetricsService::Factory::Factory() | 163 PrefMetricsService::Factory::Factory() |
| 42 : BrowserContextKeyedServiceFactory( | 164 : BrowserContextKeyedServiceFactory( |
| 43 "PrefMetricsService", | 165 "PrefMetricsService", |
| 44 BrowserContextDependencyManager::GetInstance()) { | 166 BrowserContextDependencyManager::GetInstance()) { |
| 167 DependsOn(TemplateURLServiceFactory::GetInstance()); | |
| 45 } | 168 } |
| 46 | 169 |
| 47 PrefMetricsService::Factory::~Factory() { | 170 PrefMetricsService::Factory::~Factory() { |
| 48 } | 171 } |
| 49 | 172 |
| 50 BrowserContextKeyedService* | 173 BrowserContextKeyedService* |
| 51 PrefMetricsService::Factory::BuildServiceInstanceFor( | 174 PrefMetricsService::Factory::BuildServiceInstanceFor( |
| 52 content::BrowserContext* profile) const { | 175 content::BrowserContext* profile) const { |
| 53 return new PrefMetricsService(static_cast<Profile*>(profile)); | 176 return new PrefMetricsService(static_cast<Profile*>(profile)); |
| 54 } | 177 } |
| 55 | 178 |
| 56 bool PrefMetricsService::Factory::ServiceIsCreatedWithBrowserContext() const { | 179 bool PrefMetricsService::Factory::ServiceIsCreatedWithBrowserContext() const { |
| 57 return true; | 180 return true; |
| 58 } | 181 } |
| 59 | 182 |
| 60 bool PrefMetricsService::Factory::ServiceIsNULLWhileTesting() const { | 183 bool PrefMetricsService::Factory::ServiceIsNULLWhileTesting() const { |
| 61 return false; | 184 return false; |
| 62 } | 185 } |
| 63 | 186 |
| 64 content::BrowserContext* PrefMetricsService::Factory::GetBrowserContextToUse( | 187 content::BrowserContext* PrefMetricsService::Factory::GetBrowserContextToUse( |
| 65 content::BrowserContext* context) const { | 188 content::BrowserContext* context) const { |
| 66 return chrome::GetBrowserContextRedirectedInIncognito(context); | 189 return chrome::GetBrowserContextRedirectedInIncognito(context); |
| 67 } | 190 } |
| OLD | NEW |