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" |
| 9 #include "chrome/browser/profiles/incognito_helpers.h" | 11 #include "chrome/browser/profiles/incognito_helpers.h" |
| 10 #include "chrome/browser/profiles/profile.h" | 12 #include "chrome/browser/profiles/profile.h" |
| 13 #include "chrome/browser/search_engines/template_url_prepopulate_data.h" | |
| 14 #include "chrome/browser/search_engines/template_url_service.h" | |
| 15 #include "chrome/browser/search_engines/template_url_service_factory.h" | |
| 11 #include "chrome/common/pref_names.h" | 16 #include "chrome/common/pref_names.h" |
| 12 #include "components/browser_context_keyed_service/browser_context_dependency_ma nager.h" | 17 #include "components/browser_context_keyed_service/browser_context_dependency_ma nager.h" |
| 13 | 18 |
| 19 namespace { | |
| 20 | |
| 21 // We are adding some common search engine hosts that are not prepopulated. | |
| 22 // These hosts are assigned id numbers 102-114 which extend the prepopulated | |
| 23 // engines histogram enum. If these new histograms are removed, restore this | |
| 24 // range to the list of available ids in the prepopulated_engines.json file. | |
| 25 static const int kMaxHostHistogramValue = 114; | |
| 26 | |
| 27 typedef std::map<std::string, int> HostIdMap; | |
| 28 | |
| 29 void AddHostToMap(HostIdMap* host_id_map, | |
| 30 const std::string& host, | |
| 31 int host_id) { | |
| 32 host_id_map->insert(std::make_pair(host, host_id)); | |
| 33 } | |
| 34 | |
| 35 // Builds a map that associated host name strings with histogram enum values, | |
| 36 // for prepopulated DSEs and select non-prepopulated ones. | |
| 37 void BuildHostIdMap(Profile* profile, HostIdMap* host_id_map) { | |
| 38 // Add prepopulated search engine hosts to the map. | |
| 39 ScopedVector<TemplateURL> prepopulated_urls; | |
| 40 size_t default_search_index; | |
| 41 TemplateURLPrepopulateData::GetPrepopulatedEngines(profile, | |
| 42 &prepopulated_urls.get(), &default_search_index); | |
| 43 for (size_t i = 0; i < prepopulated_urls.size(); ++i) { | |
| 44 AddHostToMap(host_id_map, | |
| 45 prepopulated_urls[i]->url_ref().GetHost(), | |
| 46 prepopulated_urls[i]->prepopulate_id()); | |
| 47 } | |
| 48 // Add non-prepopulated hosts to the map. | |
| 49 AddHostToMap(host_id_map, "searchnu.com", 102); | |
| 50 AddHostToMap(host_id_map, "babylon.com", 103); | |
| 51 AddHostToMap(host_id_map, "delta-search.com", 104); | |
| 52 AddHostToMap(host_id_map, "iminent.com", 105); | |
| 53 AddHostToMap(host_id_map, "hao123.com", 106); | |
| 54 AddHostToMap(host_id_map, "sweetim.com", 107); | |
| 55 AddHostToMap(host_id_map, "snap.do", 108); | |
| 56 AddHostToMap(host_id_map, "snapdo.com", 109); | |
| 57 AddHostToMap(host_id_map, "softonic.com", 110); | |
| 58 AddHostToMap(host_id_map, "searchfunmoods.com", 111); | |
| 59 AddHostToMap(host_id_map, "incredibar.com", 112); | |
| 60 AddHostToMap(host_id_map, "sweetpacks.com", 113); | |
| 61 AddHostToMap(host_id_map, "imesh.net", 114); | |
| 62 // These hosts may not be prepopulated, depending on the country settings. | |
| 63 // Add them here, using their existing ids. See histograms.xml. | |
| 64 AddHostToMap(host_id_map, "conduit.com", 36); | |
| 65 AddHostToMap(host_id_map, "avg.com", 50); | |
| 66 AddHostToMap(host_id_map, "mail.ru", 83); | |
| 67 } | |
| 68 | |
| 69 // Maps a host name to a histogram enum value. The enum value '0' represents | |
| 70 // 'Unknown', i.e. an unrecognized host. | |
| 71 int MapHostToId(const HostIdMap& host_id_map, const std::string& host) { | |
| 72 HostIdMap::const_iterator it = host_id_map.find(host); | |
| 73 if (it != host_id_map.end()) | |
| 74 return it->second; | |
| 75 return 0; | |
| 76 } | |
| 77 | |
| 78 } // namespace | |
| 79 | |
| 14 PrefMetricsService::PrefMetricsService(Profile* profile) | 80 PrefMetricsService::PrefMetricsService(Profile* profile) |
| 15 : profile_(profile) { | 81 : profile_(profile) { |
| 16 RecordLaunchPrefs(); | 82 RecordLaunchPrefs(); |
| 17 } | 83 } |
| 18 | 84 |
| 19 PrefMetricsService::~PrefMetricsService() { | 85 PrefMetricsService::~PrefMetricsService() { |
| 20 } | 86 } |
| 21 | 87 |
| 22 void PrefMetricsService::RecordLaunchPrefs() { | 88 void PrefMetricsService::RecordLaunchPrefs() { |
| 23 UMA_HISTOGRAM_BOOLEAN("Settings.ShowHomeButton", | 89 PrefService* prefs = profile_->GetPrefs(); |
| 24 profile_->GetPrefs()->GetBoolean(prefs::kShowHomeButton)); | 90 bool show_home_button = prefs->GetBoolean(prefs::kShowHomeButton); |
| 25 UMA_HISTOGRAM_BOOLEAN("Settings.HomePageIsNewTabPage", | 91 bool home_page_is_ntp = prefs->GetBoolean(prefs::kHomePageIsNewTabPage); |
| 26 profile_->GetPrefs()->GetBoolean(prefs::kHomePageIsNewTabPage)); | 92 |
| 93 UMA_HISTOGRAM_BOOLEAN("Settings.ShowHomeButton", show_home_button); | |
| 94 UMA_HISTOGRAM_BOOLEAN("Settings.HomePageIsNewTabPage", home_page_is_ntp); | |
| 95 | |
| 96 HostIdMap host_id_map; | |
| 97 BuildHostIdMap(profile_, &host_id_map); | |
| 98 | |
| 99 // Record the default search engine host. | |
| 100 TemplateURLService* template_url_service = | |
| 101 TemplateURLServiceFactory::GetForProfile(profile_); | |
| 102 if (template_url_service) { | |
| 103 TemplateURL* template_url = | |
| 104 template_url_service->GetDefaultSearchProvider(); | |
| 105 int host_id = MapHostToId(host_id_map, template_url->url_ref().GetHost()); | |
| 106 UMA_HISTOGRAM_ENUMERATION("Settings.DefaultSearchProvider", | |
| 107 host_id, kMaxHostHistogramValue); | |
| 108 } | |
| 109 // Record the home page URL host. | |
| 110 if (!home_page_is_ntp) { | |
| 111 GURL homepage_url(prefs->GetString(prefs::kHomePage)); | |
| 112 if (homepage_url.is_valid()) { | |
| 113 int host_id = MapHostToId(host_id_map, homepage_url.host()); | |
| 114 UMA_HISTOGRAM_ENUMERATION("Settings.HomePageURL", | |
| 115 host_id, kMaxHostHistogramValue); | |
| 116 } | |
| 117 } | |
| 118 // Record the first startup page host. The first start page is displayed in | |
| 119 // the active tab on startup. | |
| 120 const ListValue* url_list = prefs->GetList(prefs::kURLsToRestoreOnStartup); | |
| 121 if (url_list->GetSize() > 0) { | |
| 122 std::string url_text; | |
| 123 if (url_list->GetString(0, &url_text)) { | |
| 124 GURL start_url(url_text); | |
| 125 if (start_url.is_valid()) { | |
| 126 int host_id = MapHostToId(host_id_map, start_url.host()); | |
| 127 UMA_HISTOGRAM_ENUMERATION("Settings.FirstStartPageURL", | |
| 128 host_id, kMaxHostHistogramValue); | |
| 129 } | |
| 130 } | |
| 131 } | |
| 27 } | 132 } |
| 28 | 133 |
| 29 // static | 134 // static |
| 30 PrefMetricsService::Factory* PrefMetricsService::Factory::GetInstance() { | 135 PrefMetricsService::Factory* PrefMetricsService::Factory::GetInstance() { |
| 31 return Singleton<PrefMetricsService::Factory>::get(); | 136 return Singleton<PrefMetricsService::Factory>::get(); |
| 32 } | 137 } |
| 33 | 138 |
| 34 // static | 139 // static |
| 35 PrefMetricsService* PrefMetricsService::Factory::GetForProfile( | 140 PrefMetricsService* PrefMetricsService::Factory::GetForProfile( |
| 36 Profile* profile) { | 141 Profile* profile) { |
| 37 return static_cast<PrefMetricsService*>( | 142 return static_cast<PrefMetricsService*>( |
| 38 GetInstance()->GetServiceForBrowserContext(profile, true)); | 143 GetInstance()->GetServiceForBrowserContext(profile, true)); |
| 39 } | 144 } |
| 40 | 145 |
| 41 PrefMetricsService::Factory::Factory() | 146 PrefMetricsService::Factory::Factory() |
| 42 : BrowserContextKeyedServiceFactory( | 147 : BrowserContextKeyedServiceFactory( |
| 43 "PrefMetricsService", | 148 "PrefMetricsService", |
| 44 BrowserContextDependencyManager::GetInstance()) { | 149 BrowserContextDependencyManager::GetInstance()) { |
|
Ken Rockot(use gerrit already)
2013/07/24 22:14:10
Because PrefMetricsService now depends on Template
| |
| 45 } | 150 } |
| 46 | 151 |
| 47 PrefMetricsService::Factory::~Factory() { | 152 PrefMetricsService::Factory::~Factory() { |
| 48 } | 153 } |
| 49 | 154 |
| 50 BrowserContextKeyedService* | 155 BrowserContextKeyedService* |
| 51 PrefMetricsService::Factory::BuildServiceInstanceFor( | 156 PrefMetricsService::Factory::BuildServiceInstanceFor( |
| 52 content::BrowserContext* profile) const { | 157 content::BrowserContext* profile) const { |
| 53 return new PrefMetricsService(static_cast<Profile*>(profile)); | 158 return new PrefMetricsService(static_cast<Profile*>(profile)); |
| 54 } | 159 } |
| 55 | 160 |
| 56 bool PrefMetricsService::Factory::ServiceIsCreatedWithBrowserContext() const { | 161 bool PrefMetricsService::Factory::ServiceIsCreatedWithBrowserContext() const { |
| 57 return true; | 162 return true; |
| 58 } | 163 } |
| 59 | 164 |
| 60 bool PrefMetricsService::Factory::ServiceIsNULLWhileTesting() const { | 165 bool PrefMetricsService::Factory::ServiceIsNULLWhileTesting() const { |
| 61 return false; | 166 return false; |
| 62 } | 167 } |
| 63 | 168 |
| 64 content::BrowserContext* PrefMetricsService::Factory::GetBrowserContextToUse( | 169 content::BrowserContext* PrefMetricsService::Factory::GetBrowserContextToUse( |
| 65 content::BrowserContext* context) const { | 170 content::BrowserContext* context) const { |
| 66 return chrome::GetBrowserContextRedirectedInIncognito(context); | 171 return chrome::GetBrowserContextRedirectedInIncognito(context); |
| 67 } | 172 } |
| OLD | NEW |