OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/android/preferences/important_sites_util.h" |
| 6 |
| 7 #include <algorithm> |
| 8 #include <map> |
| 9 #include <set> |
| 10 |
| 11 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" |
| 12 #include "chrome/browser/engagement/site_engagement_service.h" |
| 13 #include "components/content_settings/core/browser/host_content_settings_map.h" |
| 14 #include "components/content_settings/core/common/content_settings.h" |
| 15 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" |
| 16 #include "url/gurl.h" |
| 17 |
| 18 namespace { |
| 19 |
| 20 std::vector<std::pair<GURL, double>> GetSortedTopEngagementOrigins( |
| 21 const SiteEngagementService* site_engagement_service, |
| 22 const std::map<GURL, double>& engagement_map, |
| 23 SiteEngagementService::EngagementLevel minimum_engagement) { |
| 24 std::vector<std::pair<GURL, double>> top_ranking_origins; |
| 25 for (const auto& url_engagement_pair : engagement_map) { |
| 26 if (!site_engagement_service->IsEngagementAtLeast(url_engagement_pair.first, |
| 27 minimum_engagement)) { |
| 28 continue; |
| 29 } |
| 30 top_ranking_origins.push_back(url_engagement_pair); |
| 31 } |
| 32 std::sort( |
| 33 top_ranking_origins.begin(), top_ranking_origins.end(), |
| 34 [](const std::pair<GURL, double>& a, const std::pair<GURL, double>& b) { |
| 35 return a.second > b.second; |
| 36 }); |
| 37 return top_ranking_origins; |
| 38 } |
| 39 |
| 40 std::vector<std::pair<GURL, double>> GenerateSortedOriginsForContentTypeAllowed( |
| 41 Profile* profile, |
| 42 ContentSettingsType content_type, |
| 43 const std::map<GURL, double>& score_map) { |
| 44 // Grab our content settings list. |
| 45 ContentSettingsForOneType content_settings_list; |
| 46 HostContentSettingsMapFactory::GetForProfile(profile)->GetSettingsForOneType( |
| 47 content_type, content_settings::ResourceIdentifier(), |
| 48 &content_settings_list); |
| 49 // Extract a set of urls, using the primary pattern. We don't handle wildcard |
| 50 // patterns. |
| 51 std::set<GURL> content_origins; |
| 52 for (const ContentSettingPatternSource& site : content_settings_list) { |
| 53 GURL origin(site.primary_pattern.ToString()); |
| 54 if (!origin.is_valid() || site.setting != CONTENT_SETTING_ALLOW) |
| 55 continue; |
| 56 content_origins.insert(origin); |
| 57 } |
| 58 std::vector<std::pair<GURL, double>> top_ranking_origins; |
| 59 for (const GURL& notification_origin : content_origins) { |
| 60 double score = 0; |
| 61 auto score_it = score_map.find(notification_origin); |
| 62 if (score_it != score_map.end()) |
| 63 score = score_it->second; |
| 64 top_ranking_origins.push_back(std::make_pair(notification_origin, score)); |
| 65 } |
| 66 std::sort( |
| 67 top_ranking_origins.begin(), top_ranking_origins.end(), |
| 68 [](const std::pair<GURL, double>& a, const std::pair<GURL, double>& b) { |
| 69 return a.second > b.second; |
| 70 }); |
| 71 return top_ranking_origins; |
| 72 } |
| 73 |
| 74 void FillTopRegisterableDomains( |
| 75 const std::vector<std::pair<GURL, double>>& sorted_new_origins, |
| 76 size_t max_important_domains, |
| 77 std::vector<std::string>* final_list) { |
| 78 for (const auto& pair : sorted_new_origins) { |
| 79 if (final_list->size() >= max_important_domains) |
| 80 return; |
| 81 std::string registerable_domain = |
| 82 net::registry_controlled_domains::GetDomainAndRegistry( |
| 83 pair.first, |
| 84 net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES); |
| 85 // Just iterate to find, as we assume our size is small. |
| 86 if (std::find(final_list->begin(), final_list->end(), |
| 87 registerable_domain) == final_list->end()) { |
| 88 final_list->push_back(registerable_domain); |
| 89 } |
| 90 } |
| 91 } |
| 92 |
| 93 } // namespace |
| 94 |
| 95 std::vector<std::string> ImportantSitesUtil::GetImportantRegisterableDomains( |
| 96 Profile* profile, |
| 97 size_t max_results) { |
| 98 // First get data from site engagement. |
| 99 SiteEngagementService* site_engagement_service = |
| 100 SiteEngagementService::Get(profile); |
| 101 // Engagement data. |
| 102 std::map<GURL, double> engagement_map = |
| 103 site_engagement_service->GetScoreMap(); |
| 104 std::vector<std::pair<GURL, double>> sorted_engagement_origins = |
| 105 GetSortedTopEngagementOrigins( |
| 106 site_engagement_service, engagement_map, |
| 107 SiteEngagementService::ENGAGEMENT_LEVEL_MEDIUM); |
| 108 |
| 109 // Second we grab origins with push notifications. |
| 110 std::vector<std::pair<GURL, double>> sorted_notification_origins = |
| 111 GenerateSortedOriginsForContentTypeAllowed( |
| 112 profile, CONTENT_SETTINGS_TYPE_NOTIFICATIONS, engagement_map); |
| 113 |
| 114 // Now we transform them into registerable domains, and add them into our |
| 115 // final list. Since our # is small, we just iterate our vector to de-dup. |
| 116 // Otherwise we can add a set later. |
| 117 std::vector<std::string> final_list; |
| 118 // We start with notifications. |
| 119 FillTopRegisterableDomains(sorted_notification_origins, max_results, |
| 120 &final_list); |
| 121 // And now we fill the rest with high engagement sites. |
| 122 FillTopRegisterableDomains(sorted_engagement_origins, max_results, |
| 123 &final_list); |
| 124 return final_list; |
| 125 } |
OLD | NEW |