OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/android/preferences/important_sites_util.h" | 5 #include "chrome/browser/android/preferences/important_sites_util.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <map> | 8 #include <map> |
9 #include <set> | 9 #include <set> |
10 | 10 |
| 11 #include "base/metrics/histogram_macros.h" |
11 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" | 12 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" |
12 #include "chrome/browser/engagement/site_engagement_score.h" | 13 #include "chrome/browser/engagement/site_engagement_score.h" |
13 #include "chrome/browser/engagement/site_engagement_service.h" | 14 #include "chrome/browser/engagement/site_engagement_service.h" |
14 #include "components/content_settings/core/browser/host_content_settings_map.h" | 15 #include "components/content_settings/core/browser/host_content_settings_map.h" |
15 #include "components/content_settings/core/common/content_settings.h" | 16 #include "components/content_settings/core/common/content_settings.h" |
16 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" | 17 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" |
17 #include "url/gurl.h" | 18 #include "url/gurl.h" |
18 | 19 |
19 namespace { | 20 namespace { |
20 | 21 |
| 22 // Do not change the values here, as they are used for UMA histograms. |
| 23 enum ReasonStatTypes { |
| 24 DURABLE = 0, |
| 25 NOTIFICATIONS, |
| 26 ENGAGEMENT, |
| 27 NOTIFICATIONS_AND_ENGAGEMENT, |
| 28 DURABLE_AND_ENGAGEMENT, |
| 29 NOTIFICATIONS_AND_DURABLE, |
| 30 NOTIFICATIONS_AND_DURABLE_AND_ENGAGEMENT, |
| 31 REASON_UNKNOWN, |
| 32 REASON_BOUNDARY |
| 33 }; |
| 34 |
| 35 struct ImportantReason { |
| 36 bool engagement = false; |
| 37 bool notifications = false; |
| 38 bool durable = false; |
| 39 }; |
| 40 |
21 std::vector<std::pair<GURL, double>> GetSortedTopEngagementOrigins( | 41 std::vector<std::pair<GURL, double>> GetSortedTopEngagementOrigins( |
22 const SiteEngagementService* site_engagement_service, | 42 const SiteEngagementService* site_engagement_service, |
23 const std::map<GURL, double>& engagement_map, | 43 const std::map<GURL, double>& engagement_map, |
24 SiteEngagementService::EngagementLevel minimum_engagement) { | 44 SiteEngagementService::EngagementLevel minimum_engagement) { |
25 std::vector<std::pair<GURL, double>> top_ranking_origins; | 45 std::vector<std::pair<GURL, double>> top_ranking_origins; |
26 for (const auto& url_engagement_pair : engagement_map) { | 46 for (const auto& url_engagement_pair : engagement_map) { |
27 if (!site_engagement_service->IsEngagementAtLeast(url_engagement_pair.first, | 47 if (!site_engagement_service->IsEngagementAtLeast(url_engagement_pair.first, |
28 minimum_engagement)) { | 48 minimum_engagement)) { |
29 continue; | 49 continue; |
30 } | 50 } |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
135 &final_list, optional_example_origins); | 155 &final_list, optional_example_origins); |
136 FillTopRegisterableDomains(sorted_notification_origins, max_results, | 156 FillTopRegisterableDomains(sorted_notification_origins, max_results, |
137 &final_list, optional_example_origins); | 157 &final_list, optional_example_origins); |
138 | 158 |
139 // And now we fill the rest with high engagement sites. | 159 // And now we fill the rest with high engagement sites. |
140 FillTopRegisterableDomains(sorted_engagement_origins, max_results, | 160 FillTopRegisterableDomains(sorted_engagement_origins, max_results, |
141 &final_list, optional_example_origins); | 161 &final_list, optional_example_origins); |
142 return final_list; | 162 return final_list; |
143 } | 163 } |
144 | 164 |
| 165 void ImportantSitesUtil::RecordMetricsForBlacklistedSites( |
| 166 Profile* profile, |
| 167 std::vector<std::string> blacklisted_sites) { |
| 168 SiteEngagementService* site_engagement_service = |
| 169 SiteEngagementService::Get(profile); |
| 170 |
| 171 std::map<std::string, ImportantReason> reason_map; |
| 172 |
| 173 std::map<GURL, double> engagement_map = |
| 174 site_engagement_service->GetScoreMap(); |
| 175 |
| 176 // Site engagement. |
| 177 for (const auto& url_score_pair : engagement_map) { |
| 178 if (url_score_pair.second < |
| 179 SiteEngagementScore::GetMediumEngagementBoundary()) { |
| 180 continue; |
| 181 } |
| 182 const std::string& host = url_score_pair.first.host(); |
| 183 for (const std::string& blacklisted_site : blacklisted_sites) { |
| 184 if (host.find(blacklisted_site) != std::string::npos) { |
| 185 reason_map[blacklisted_site].engagement |= true; |
| 186 break; |
| 187 } |
| 188 } |
| 189 } |
| 190 |
| 191 // Durable. |
| 192 ContentSettingsForOneType content_settings_list; |
| 193 HostContentSettingsMapFactory::GetForProfile(profile)->GetSettingsForOneType( |
| 194 CONTENT_SETTINGS_TYPE_DURABLE_STORAGE, |
| 195 content_settings::ResourceIdentifier(), &content_settings_list); |
| 196 for (const ContentSettingPatternSource& site : content_settings_list) { |
| 197 if (site.setting != CONTENT_SETTING_ALLOW) |
| 198 continue; |
| 199 GURL origin(site.primary_pattern.ToString()); |
| 200 if (!origin.is_valid()) |
| 201 continue; |
| 202 const std::string& host = origin.host(); |
| 203 for (const std::string& blacklisted_site : blacklisted_sites) { |
| 204 if (host.find(blacklisted_site) != std::string::npos) { |
| 205 reason_map[blacklisted_site].durable |= true; |
| 206 break; |
| 207 } |
| 208 } |
| 209 } |
| 210 |
| 211 // Notifications. |
| 212 content_settings_list.clear(); |
| 213 HostContentSettingsMapFactory::GetForProfile(profile)->GetSettingsForOneType( |
| 214 CONTENT_SETTINGS_TYPE_NOTIFICATIONS, |
| 215 content_settings::ResourceIdentifier(), &content_settings_list); |
| 216 for (const ContentSettingPatternSource& site : content_settings_list) { |
| 217 if (site.setting != CONTENT_SETTING_ALLOW) |
| 218 continue; |
| 219 GURL origin(site.primary_pattern.ToString()); |
| 220 if (!origin.is_valid()) |
| 221 continue; |
| 222 const std::string& host = origin.host(); |
| 223 for (const std::string& blacklisted_site : blacklisted_sites) { |
| 224 if (host.find(blacklisted_site) != std::string::npos) { |
| 225 reason_map[blacklisted_site].notifications |= true; |
| 226 break; |
| 227 } |
| 228 } |
| 229 } |
| 230 |
| 231 // Note: we don't plan on adding new metrics here, this is just for the finch |
| 232 // experiment to give us initial data on what signals actually mattered. |
| 233 for (const auto& reason_pair : reason_map) { |
| 234 const ImportantReason& reason = reason_pair.second; |
| 235 if (reason.notifications && reason.durable && reason.engagement) { |
| 236 UMA_HISTOGRAM_ENUMERATION("Storage.BlacklistedImportantSites.Reason", |
| 237 NOTIFICATIONS_AND_DURABLE_AND_ENGAGEMENT, |
| 238 REASON_BOUNDARY); |
| 239 } else if (reason.notifications && reason.durable) { |
| 240 UMA_HISTOGRAM_ENUMERATION("Storage.BlacklistedImportantSites.Reason", |
| 241 NOTIFICATIONS_AND_DURABLE, REASON_BOUNDARY); |
| 242 } else if (reason.notifications && reason.engagement) { |
| 243 UMA_HISTOGRAM_ENUMERATION("Storage.BlacklistedImportantSites.Reason", |
| 244 NOTIFICATIONS_AND_ENGAGEMENT, REASON_BOUNDARY); |
| 245 } else if (reason.durable && reason.engagement) { |
| 246 UMA_HISTOGRAM_ENUMERATION("Storage.BlacklistedImportantSites.Reason", |
| 247 DURABLE_AND_ENGAGEMENT, REASON_BOUNDARY); |
| 248 } else if (reason.notifications) { |
| 249 UMA_HISTOGRAM_ENUMERATION("Storage.BlacklistedImportantSites.Reason", |
| 250 NOTIFICATIONS, REASON_BOUNDARY); |
| 251 } else if (reason.durable) { |
| 252 UMA_HISTOGRAM_ENUMERATION("Storage.BlacklistedImportantSites.Reason", |
| 253 DURABLE, REASON_BOUNDARY); |
| 254 } else if (reason.engagement) { |
| 255 UMA_HISTOGRAM_ENUMERATION("Storage.BlacklistedImportantSites.Reason", |
| 256 ENGAGEMENT, REASON_BOUNDARY); |
| 257 } else { |
| 258 UMA_HISTOGRAM_ENUMERATION("Storage.BlacklistedImportantSites.Reason", |
| 259 REASON_UNKNOWN, REASON_BOUNDARY); |
| 260 |
| 261 } |
| 262 } |
| 263 } |
| 264 |
145 void ImportantSitesUtil::MarkOriginAsImportantForTesting(Profile* profile, | 265 void ImportantSitesUtil::MarkOriginAsImportantForTesting(Profile* profile, |
146 const GURL& origin) { | 266 const GURL& origin) { |
147 // First get data from site engagement. | 267 // First get data from site engagement. |
148 SiteEngagementService* site_engagement_service = | 268 SiteEngagementService* site_engagement_service = |
149 SiteEngagementService::Get(profile); | 269 SiteEngagementService::Get(profile); |
150 site_engagement_service->ResetScoreForURL( | 270 site_engagement_service->ResetScoreForURL( |
151 origin, SiteEngagementScore::GetMediumEngagementBoundary()); | 271 origin, SiteEngagementScore::GetMediumEngagementBoundary()); |
152 DCHECK(site_engagement_service->IsEngagementAtLeast( | 272 DCHECK(site_engagement_service->IsEngagementAtLeast( |
153 origin, SiteEngagementService::ENGAGEMENT_LEVEL_MEDIUM)); | 273 origin, SiteEngagementService::ENGAGEMENT_LEVEL_MEDIUM)); |
154 } | 274 } |
OLD | NEW |