| 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 "chrome/browser/content_settings/host_content_settings_map_factory.h" | 11 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" |
| 12 #include "chrome/browser/engagement/site_engagement_score.h" |
| 12 #include "chrome/browser/engagement/site_engagement_service.h" | 13 #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/browser/host_content_settings_map.h" |
| 14 #include "components/content_settings/core/common/content_settings.h" | 15 #include "components/content_settings/core/common/content_settings.h" |
| 15 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" | 16 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" |
| 16 #include "url/gurl.h" | 17 #include "url/gurl.h" |
| 17 | 18 |
| 18 namespace { | 19 namespace { |
| 19 | 20 |
| 20 std::vector<std::pair<GURL, double>> GetSortedTopEngagementOrigins( | 21 std::vector<std::pair<GURL, double>> GetSortedTopEngagementOrigins( |
| 21 const SiteEngagementService* site_engagement_service, | 22 const SiteEngagementService* site_engagement_service, |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 69 top_ranking_origins.begin(), top_ranking_origins.end(), | 70 top_ranking_origins.begin(), top_ranking_origins.end(), |
| 70 [](const std::pair<GURL, double>& a, const std::pair<GURL, double>& b) { | 71 [](const std::pair<GURL, double>& a, const std::pair<GURL, double>& b) { |
| 71 return a.second > b.second; | 72 return a.second > b.second; |
| 72 }); | 73 }); |
| 73 return top_ranking_origins; | 74 return top_ranking_origins; |
| 74 } | 75 } |
| 75 | 76 |
| 76 void FillTopRegisterableDomains( | 77 void FillTopRegisterableDomains( |
| 77 const std::vector<std::pair<GURL, double>>& sorted_new_origins, | 78 const std::vector<std::pair<GURL, double>>& sorted_new_origins, |
| 78 size_t max_important_domains, | 79 size_t max_important_domains, |
| 79 std::vector<std::string>* final_list) { | 80 std::vector<std::string>* final_list, |
| 81 std::vector<GURL>* optional_example_origins) { |
| 80 for (const auto& pair : sorted_new_origins) { | 82 for (const auto& pair : sorted_new_origins) { |
| 81 if (final_list->size() >= max_important_domains) | 83 if (final_list->size() >= max_important_domains) |
| 82 return; | 84 return; |
| 83 std::string registerable_domain = | 85 std::string registerable_domain = |
| 84 net::registry_controlled_domains::GetDomainAndRegistry( | 86 net::registry_controlled_domains::GetDomainAndRegistry( |
| 85 pair.first, | 87 pair.first, |
| 86 net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES); | 88 net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES); |
| 89 if (registerable_domain.empty() && pair.first.HostIsIPAddress()) |
| 90 registerable_domain = pair.first.host(); |
| 87 // Just iterate to find, as we assume our size is small. | 91 // Just iterate to find, as we assume our size is small. |
| 88 if (std::find(final_list->begin(), final_list->end(), | 92 if (std::find(final_list->begin(), final_list->end(), |
| 89 registerable_domain) == final_list->end()) { | 93 registerable_domain) == final_list->end()) { |
| 90 final_list->push_back(registerable_domain); | 94 final_list->push_back(registerable_domain); |
| 95 if (optional_example_origins) |
| 96 optional_example_origins->push_back(pair.first); |
| 91 } | 97 } |
| 92 } | 98 } |
| 93 } | 99 } |
| 94 | 100 |
| 95 } // namespace | 101 } // namespace |
| 96 | 102 |
| 97 std::vector<std::string> ImportantSitesUtil::GetImportantRegisterableDomains( | 103 std::vector<std::string> ImportantSitesUtil::GetImportantRegisterableDomains( |
| 98 Profile* profile, | 104 Profile* profile, |
| 99 size_t max_results) { | 105 size_t max_results, |
| 106 std::vector<GURL>* optional_example_origins) { |
| 100 // First get data from site engagement. | 107 // First get data from site engagement. |
| 101 SiteEngagementService* site_engagement_service = | 108 SiteEngagementService* site_engagement_service = |
| 102 SiteEngagementService::Get(profile); | 109 SiteEngagementService::Get(profile); |
| 103 // Engagement data. | 110 // Engagement data. |
| 104 std::map<GURL, double> engagement_map = | 111 std::map<GURL, double> engagement_map = |
| 105 site_engagement_service->GetScoreMap(); | 112 site_engagement_service->GetScoreMap(); |
| 106 std::vector<std::pair<GURL, double>> sorted_engagement_origins = | 113 std::vector<std::pair<GURL, double>> sorted_engagement_origins = |
| 107 GetSortedTopEngagementOrigins( | 114 GetSortedTopEngagementOrigins( |
| 108 site_engagement_service, engagement_map, | 115 site_engagement_service, engagement_map, |
| 109 SiteEngagementService::ENGAGEMENT_LEVEL_MEDIUM); | 116 SiteEngagementService::ENGAGEMENT_LEVEL_MEDIUM); |
| 110 | 117 |
| 111 // Second we grab origins with push notifications. | 118 // Second we grab origins with push notifications. |
| 112 std::vector<std::pair<GURL, double>> sorted_notification_origins = | 119 std::vector<std::pair<GURL, double>> sorted_notification_origins = |
| 113 GenerateSortedOriginsForContentTypeAllowed( | 120 GenerateSortedOriginsForContentTypeAllowed( |
| 114 profile, CONTENT_SETTINGS_TYPE_NOTIFICATIONS, engagement_map); | 121 profile, CONTENT_SETTINGS_TYPE_NOTIFICATIONS, engagement_map); |
| 115 | 122 |
| 116 // Now we transform them into registerable domains, and add them into our | 123 // Now we transform them into registerable domains, and add them into our |
| 117 // final list. Since our # is small, we just iterate our vector to de-dup. | 124 // final list. Since our # is small, we just iterate our vector to de-dup. |
| 118 // Otherwise we can add a set later. | 125 // Otherwise we can add a set later. |
| 119 std::vector<std::string> final_list; | 126 std::vector<std::string> final_list; |
| 120 // We start with notifications. | 127 // We start with notifications. |
| 121 FillTopRegisterableDomains(sorted_notification_origins, max_results, | 128 FillTopRegisterableDomains(sorted_notification_origins, max_results, |
| 122 &final_list); | 129 &final_list, optional_example_origins); |
| 123 // And now we fill the rest with high engagement sites. | 130 // And now we fill the rest with high engagement sites. |
| 124 FillTopRegisterableDomains(sorted_engagement_origins, max_results, | 131 FillTopRegisterableDomains(sorted_engagement_origins, max_results, |
| 125 &final_list); | 132 &final_list, optional_example_origins); |
| 126 return final_list; | 133 return final_list; |
| 127 } | 134 } |
| 135 |
| 136 void ImportantSitesUtil::MarkOriginAsImportantForTesting(Profile* profile, |
| 137 const GURL& origin) { |
| 138 // First get data from site engagement. |
| 139 SiteEngagementService* site_engagement_service = |
| 140 SiteEngagementService::Get(profile); |
| 141 site_engagement_service->ResetScoreForURL( |
| 142 origin, SiteEngagementScore::GetMediumEngagementBoundary()); |
| 143 DCHECK(site_engagement_service->IsEngagementAtLeast( |
| 144 origin, SiteEngagementService::ENGAGEMENT_LEVEL_MEDIUM)); |
| 145 } |
| OLD | NEW |