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