| 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/engagement/important_sites_util.h" | 5 #include "chrome/browser/engagement/important_sites_util.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <map> | 8 #include <map> |
| 9 #include <memory> | 9 #include <memory> |
| 10 #include <set> | 10 #include <set> |
| 11 #include <utility> | 11 #include <utility> |
| 12 | 12 |
| 13 #include "base/containers/hash_tables.h" | 13 #include "base/containers/hash_tables.h" |
| 14 #include "base/memory/ptr_util.h" | 14 #include "base/memory/ptr_util.h" |
| 15 #include "base/metrics/histogram_macros.h" | 15 #include "base/metrics/histogram_macros.h" |
| 16 #include "base/stl_util.h" | 16 #include "base/stl_util.h" |
| 17 #include "base/time/time.h" | 17 #include "base/time/time.h" |
| 18 #include "base/values.h" | 18 #include "base/values.h" |
| 19 #include "chrome/browser/banners/app_banner_settings_helper.h" | 19 #include "chrome/browser/banners/app_banner_settings_helper.h" |
| 20 #include "chrome/browser/bookmarks/bookmark_model_factory.h" | 20 #include "chrome/browser/bookmarks/bookmark_model_factory.h" |
| 21 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" | 21 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" |
| 22 #include "chrome/browser/engagement/site_engagement_score.h" | 22 #include "chrome/browser/engagement/site_engagement_score.h" |
| 23 #include "chrome/browser/engagement/site_engagement_service.h" | 23 #include "chrome/browser/engagement/site_engagement_service.h" |
| 24 #include "chrome/browser/profiles/profile.h" | 24 #include "chrome/browser/profiles/profile.h" |
| 25 #include "components/bookmarks/browser/bookmark_model.h" | 25 #include "components/bookmarks/browser/bookmark_model.h" |
| 26 #include "components/content_settings/core/browser/host_content_settings_map.h" | 26 #include "components/content_settings/core/browser/host_content_settings_map.h" |
| 27 #include "components/content_settings/core/common/content_settings.h" | 27 #include "components/content_settings/core/common/content_settings.h" |
| 28 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" | 28 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" |
| 29 #include "third_party/WebKit/public/platform/site_engagement.mojom.h" |
| 29 #include "url/gurl.h" | 30 #include "url/gurl.h" |
| 30 | 31 |
| 31 namespace { | 32 namespace { |
| 32 using bookmarks::BookmarkModel; | 33 using bookmarks::BookmarkModel; |
| 33 using ImportantDomainInfo = ImportantSitesUtil::ImportantDomainInfo; | 34 using ImportantDomainInfo = ImportantSitesUtil::ImportantDomainInfo; |
| 34 | 35 |
| 35 static const char kNumTimesIgnoredName[] = "NumTimesIgnored"; | 36 static const char kNumTimesIgnoredName[] = "NumTimesIgnored"; |
| 36 static const int kTimesIgnoredForBlacklist = 3; | 37 static const int kTimesIgnoredForBlacklist = 3; |
| 37 | 38 |
| 38 // These are the maximum # of bookmarks we can use as signals. If the user has | 39 // These are the maximum # of bookmarks we can use as signals. If the user has |
| (...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 203 continue; | 204 continue; |
| 204 } | 205 } |
| 205 | 206 |
| 206 ignoring_domains.insert(origin.host()); | 207 ignoring_domains.insert(origin.host()); |
| 207 } | 208 } |
| 208 return ignoring_domains; | 209 return ignoring_domains; |
| 209 } | 210 } |
| 210 | 211 |
| 211 void PopulateInfoMapWithSiteEngagement( | 212 void PopulateInfoMapWithSiteEngagement( |
| 212 Profile* profile, | 213 Profile* profile, |
| 213 SiteEngagementService::EngagementLevel minimum_engagement, | 214 blink::mojom::EngagementLevel minimum_engagement, |
| 214 std::map<GURL, double>* engagement_map, | 215 std::map<GURL, double>* engagement_map, |
| 215 base::hash_map<std::string, ImportantDomainInfo>* output) { | 216 base::hash_map<std::string, ImportantDomainInfo>* output) { |
| 216 SiteEngagementService* service = SiteEngagementService::Get(profile); | 217 SiteEngagementService* service = SiteEngagementService::Get(profile); |
| 217 *engagement_map = service->GetScoreMap(); | 218 *engagement_map = service->GetScoreMap(); |
| 218 // We can have multiple origins for a single domain, so we record the one | 219 // We can have multiple origins for a single domain, so we record the one |
| 219 // with the highest engagement score. | 220 // with the highest engagement score. |
| 220 for (const auto& url_engagement_pair : *engagement_map) { | 221 for (const auto& url_engagement_pair : *engagement_map) { |
| 221 if (!service->IsEngagementAtLeast(url_engagement_pair.first, | 222 if (!service->IsEngagementAtLeast(url_engagement_pair.first, |
| 222 minimum_engagement)) { | 223 minimum_engagement)) { |
| 223 continue; | 224 continue; |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 268 model->GetBookmarks(&untrimmed_bookmarks); | 269 model->GetBookmarks(&untrimmed_bookmarks); |
| 269 | 270 |
| 270 // Process the bookmarks and optionally trim them if we have too many. | 271 // Process the bookmarks and optionally trim them if we have too many. |
| 271 std::vector<BookmarkModel::URLAndTitle> result_bookmarks; | 272 std::vector<BookmarkModel::URLAndTitle> result_bookmarks; |
| 272 if (untrimmed_bookmarks.size() > kMaxBookmarks) { | 273 if (untrimmed_bookmarks.size() > kMaxBookmarks) { |
| 273 std::copy_if(untrimmed_bookmarks.begin(), untrimmed_bookmarks.end(), | 274 std::copy_if(untrimmed_bookmarks.begin(), untrimmed_bookmarks.end(), |
| 274 std::back_inserter(result_bookmarks), | 275 std::back_inserter(result_bookmarks), |
| 275 [service](const BookmarkModel::URLAndTitle& entry) { | 276 [service](const BookmarkModel::URLAndTitle& entry) { |
| 276 return service->IsEngagementAtLeast( | 277 return service->IsEngagementAtLeast( |
| 277 entry.url.GetOrigin(), | 278 entry.url.GetOrigin(), |
| 278 SiteEngagementService::ENGAGEMENT_LEVEL_LOW); | 279 blink::mojom::EngagementLevel::LOW); |
| 279 }); | 280 }); |
| 280 std::sort(result_bookmarks.begin(), result_bookmarks.end(), | 281 std::sort(result_bookmarks.begin(), result_bookmarks.end(), |
| 281 [&engagement_map](const BookmarkModel::URLAndTitle& a, | 282 [&engagement_map](const BookmarkModel::URLAndTitle& a, |
| 282 const BookmarkModel::URLAndTitle& b) { | 283 const BookmarkModel::URLAndTitle& b) { |
| 283 double a_score = engagement_map.at(a.url.GetOrigin()); | 284 double a_score = engagement_map.at(a.url.GetOrigin()); |
| 284 double b_score = engagement_map.at(b.url.GetOrigin()); | 285 double b_score = engagement_map.at(b.url.GetOrigin()); |
| 285 return a_score > b_score; | 286 return a_score > b_score; |
| 286 }); | 287 }); |
| 287 if (result_bookmarks.size() > kMaxBookmarks) | 288 if (result_bookmarks.size() > kMaxBookmarks) |
| 288 result_bookmarks.resize(kMaxBookmarks); | 289 result_bookmarks.resize(kMaxBookmarks); |
| (...skipping 30 matching lines...) Expand all Loading... |
| 319 | 320 |
| 320 } // namespace | 321 } // namespace |
| 321 | 322 |
| 322 std::vector<ImportantDomainInfo> | 323 std::vector<ImportantDomainInfo> |
| 323 ImportantSitesUtil::GetImportantRegisterableDomains(Profile* profile, | 324 ImportantSitesUtil::GetImportantRegisterableDomains(Profile* profile, |
| 324 size_t max_results) { | 325 size_t max_results) { |
| 325 base::hash_map<std::string, ImportantDomainInfo> important_info; | 326 base::hash_map<std::string, ImportantDomainInfo> important_info; |
| 326 std::map<GURL, double> engagement_map; | 327 std::map<GURL, double> engagement_map; |
| 327 | 328 |
| 328 PopulateInfoMapWithSiteEngagement( | 329 PopulateInfoMapWithSiteEngagement( |
| 329 profile, SiteEngagementService::ENGAGEMENT_LEVEL_MEDIUM, &engagement_map, | 330 profile, blink::mojom::EngagementLevel::MEDIUM, &engagement_map, |
| 330 &important_info); | 331 &important_info); |
| 331 | 332 |
| 332 PopulateInfoMapWithContentTypeAllowed( | 333 PopulateInfoMapWithContentTypeAllowed( |
| 333 profile, CONTENT_SETTINGS_TYPE_NOTIFICATIONS, | 334 profile, CONTENT_SETTINGS_TYPE_NOTIFICATIONS, |
| 334 ImportantReason::NOTIFICATIONS, &important_info); | 335 ImportantReason::NOTIFICATIONS, &important_info); |
| 335 | 336 |
| 336 PopulateInfoMapWithContentTypeAllowed( | 337 PopulateInfoMapWithContentTypeAllowed( |
| 337 profile, CONTENT_SETTINGS_TYPE_DURABLE_STORAGE, ImportantReason::DURABLE, | 338 profile, CONTENT_SETTINGS_TYPE_DURABLE_STORAGE, ImportantReason::DURABLE, |
| 338 &important_info); | 339 &important_info); |
| 339 | 340 |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 431 | 432 |
| 432 void ImportantSitesUtil::MarkOriginAsImportantForTesting(Profile* profile, | 433 void ImportantSitesUtil::MarkOriginAsImportantForTesting(Profile* profile, |
| 433 const GURL& origin) { | 434 const GURL& origin) { |
| 434 SiteEngagementScore::SetParamValuesForTesting(); | 435 SiteEngagementScore::SetParamValuesForTesting(); |
| 435 // First get data from site engagement. | 436 // First get data from site engagement. |
| 436 SiteEngagementService* site_engagement_service = | 437 SiteEngagementService* site_engagement_service = |
| 437 SiteEngagementService::Get(profile); | 438 SiteEngagementService::Get(profile); |
| 438 site_engagement_service->ResetScoreForURL( | 439 site_engagement_service->ResetScoreForURL( |
| 439 origin, SiteEngagementScore::GetMediumEngagementBoundary()); | 440 origin, SiteEngagementScore::GetMediumEngagementBoundary()); |
| 440 DCHECK(site_engagement_service->IsEngagementAtLeast( | 441 DCHECK(site_engagement_service->IsEngagementAtLeast( |
| 441 origin, SiteEngagementService::ENGAGEMENT_LEVEL_MEDIUM)); | 442 origin, blink::mojom::EngagementLevel::MEDIUM)); |
| 442 } | 443 } |
| OLD | NEW |