| 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> |
| (...skipping 14 matching lines...) Expand all Loading... |
| 25 #include "chrome/common/pref_names.h" | 25 #include "chrome/common/pref_names.h" |
| 26 #include "components/bookmarks/browser/bookmark_model.h" | 26 #include "components/bookmarks/browser/bookmark_model.h" |
| 27 #include "components/content_settings/core/browser/host_content_settings_map.h" | 27 #include "components/content_settings/core/browser/host_content_settings_map.h" |
| 28 #include "components/content_settings/core/common/content_settings.h" | 28 #include "components/content_settings/core/common/content_settings.h" |
| 29 #include "components/pref_registry/pref_registry_syncable.h" | 29 #include "components/pref_registry/pref_registry_syncable.h" |
| 30 #include "components/prefs/pref_service.h" | 30 #include "components/prefs/pref_service.h" |
| 31 #include "components/prefs/scoped_user_pref_update.h" | 31 #include "components/prefs/scoped_user_pref_update.h" |
| 32 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" | 32 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" |
| 33 #include "third_party/WebKit/public/platform/site_engagement.mojom.h" | 33 #include "third_party/WebKit/public/platform/site_engagement.mojom.h" |
| 34 #include "url/gurl.h" | 34 #include "url/gurl.h" |
| 35 #include "url/url_util.h" |
| 35 | 36 |
| 36 namespace { | 37 namespace { |
| 37 using bookmarks::BookmarkModel; | 38 using bookmarks::BookmarkModel; |
| 38 using ImportantDomainInfo = ImportantSitesUtil::ImportantDomainInfo; | 39 using ImportantDomainInfo = ImportantSitesUtil::ImportantDomainInfo; |
| 39 using ImportantReason = ImportantSitesUtil::ImportantReason; | 40 using ImportantReason = ImportantSitesUtil::ImportantReason; |
| 40 | 41 |
| 41 // Note: These values are stored on both the per-site content settings | 42 // Note: These values are stored on both the per-site content settings |
| 42 // dictionary and the dialog preference dictionary. | 43 // dictionary and the dialog preference dictionary. |
| 43 | 44 |
| 44 static const char kTimeLastIgnored[] = "TimeLastIgnored"; | 45 static const char kTimeLastIgnored[] = "TimeLastIgnored"; |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 125 return CROSSED_DURABLE_AND_ENGAGEMENT; | 126 return CROSSED_DURABLE_AND_ENGAGEMENT; |
| 126 else if (notifications) | 127 else if (notifications) |
| 127 return CROSSED_NOTIFICATIONS; | 128 return CROSSED_NOTIFICATIONS; |
| 128 else if (durable) | 129 else if (durable) |
| 129 return CROSSED_DURABLE; | 130 return CROSSED_DURABLE; |
| 130 else if (engagement) | 131 else if (engagement) |
| 131 return CROSSED_ENGAGEMENT; | 132 return CROSSED_ENGAGEMENT; |
| 132 return CROSSED_REASON_UNKNOWN; | 133 return CROSSED_REASON_UNKNOWN; |
| 133 } | 134 } |
| 134 | 135 |
| 135 std::string GetRegisterableDomainOrIP(const GURL& url) { | |
| 136 std::string registerable_domain = | |
| 137 net::registry_controlled_domains::GetDomainAndRegistry( | |
| 138 url, net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES); | |
| 139 if (registerable_domain.empty() && url.HostIsIPAddress()) | |
| 140 registerable_domain = url.host(); | |
| 141 return registerable_domain; | |
| 142 } | |
| 143 | |
| 144 void MaybePopulateImportantInfoForReason( | 136 void MaybePopulateImportantInfoForReason( |
| 145 const GURL& origin, | 137 const GURL& origin, |
| 146 std::set<GURL>* visited_origins, | 138 std::set<GURL>* visited_origins, |
| 147 ImportantReason reason, | 139 ImportantReason reason, |
| 148 base::hash_map<std::string, ImportantDomainInfo>* output) { | 140 base::hash_map<std::string, ImportantDomainInfo>* output) { |
| 149 if (!origin.is_valid() || !visited_origins->insert(origin).second) | 141 if (!origin.is_valid() || !visited_origins->insert(origin).second) |
| 150 return; | 142 return; |
| 151 std::string registerable_domain = GetRegisterableDomainOrIP(origin); | 143 std::string registerable_domain = |
| 144 ImportantSitesUtil::GetRegisterableDomainOrIP(origin); |
| 152 ImportantDomainInfo& info = (*output)[registerable_domain]; | 145 ImportantDomainInfo& info = (*output)[registerable_domain]; |
| 153 info.reason_bitfield |= 1 << reason; | 146 info.reason_bitfield |= 1 << reason; |
| 154 if (info.example_origin.is_empty()) { | 147 if (info.example_origin.is_empty()) { |
| 155 info.registerable_domain = registerable_domain; | 148 info.registerable_domain = registerable_domain; |
| 156 info.example_origin = origin; | 149 info.example_origin = origin; |
| 157 } | 150 } |
| 158 } | 151 } |
| 159 | 152 |
| 160 // Returns the score associated with the given reason. The order of | 153 // Returns the score associated with the given reason. The order of |
| 161 // ImportantReason does not need to correspond to the score order. The higher | 154 // ImportantReason does not need to correspond to the score order. The higher |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 238 SiteEngagementService* service = SiteEngagementService::Get(profile); | 231 SiteEngagementService* service = SiteEngagementService::Get(profile); |
| 239 *engagement_map = service->GetScoreMap(); | 232 *engagement_map = service->GetScoreMap(); |
| 240 // We can have multiple origins for a single domain, so we record the one | 233 // We can have multiple origins for a single domain, so we record the one |
| 241 // with the highest engagement score. | 234 // with the highest engagement score. |
| 242 for (const auto& url_engagement_pair : *engagement_map) { | 235 for (const auto& url_engagement_pair : *engagement_map) { |
| 243 if (!service->IsEngagementAtLeast(url_engagement_pair.first, | 236 if (!service->IsEngagementAtLeast(url_engagement_pair.first, |
| 244 minimum_engagement)) { | 237 minimum_engagement)) { |
| 245 continue; | 238 continue; |
| 246 } | 239 } |
| 247 std::string registerable_domain = | 240 std::string registerable_domain = |
| 248 GetRegisterableDomainOrIP(url_engagement_pair.first); | 241 ImportantSitesUtil::GetRegisterableDomainOrIP( |
| 242 url_engagement_pair.first); |
| 249 ImportantDomainInfo& info = (*output)[registerable_domain]; | 243 ImportantDomainInfo& info = (*output)[registerable_domain]; |
| 250 if (url_engagement_pair.second > info.engagement_score) { | 244 if (url_engagement_pair.second > info.engagement_score) { |
| 251 info.registerable_domain = registerable_domain; | 245 info.registerable_domain = registerable_domain; |
| 252 info.engagement_score = url_engagement_pair.second; | 246 info.engagement_score = url_engagement_pair.second; |
| 253 info.example_origin = url_engagement_pair.first; | 247 info.example_origin = url_engagement_pair.first; |
| 254 info.reason_bitfield |= 1 << ImportantReason::ENGAGEMENT; | 248 info.reason_bitfield |= 1 << ImportantReason::ENGAGEMENT; |
| 255 } | 249 } |
| 256 } | 250 } |
| 257 } | 251 } |
| 258 | 252 |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 334 GURL origin(site.primary_pattern.ToString()); | 328 GURL origin(site.primary_pattern.ToString()); |
| 335 if (!AppBannerSettingsHelper::WasLaunchedRecently(profile, origin, now)) | 329 if (!AppBannerSettingsHelper::WasLaunchedRecently(profile, origin, now)) |
| 336 continue; | 330 continue; |
| 337 MaybePopulateImportantInfoForReason(origin, &content_origins, | 331 MaybePopulateImportantInfoForReason(origin, &content_origins, |
| 338 ImportantReason::HOME_SCREEN, output); | 332 ImportantReason::HOME_SCREEN, output); |
| 339 } | 333 } |
| 340 } | 334 } |
| 341 | 335 |
| 342 } // namespace | 336 } // namespace |
| 343 | 337 |
| 338 std::string ImportantSitesUtil::GetRegisterableDomainOrIP(const GURL& url) { |
| 339 return GetRegisterableDomainOrIPFromHost(url.host_piece()); |
| 340 } |
| 341 |
| 342 std::string ImportantSitesUtil::GetRegisterableDomainOrIPFromHost( |
| 343 base::StringPiece host) { |
| 344 std::string registerable_domain = |
| 345 net::registry_controlled_domains::GetDomainAndRegistry( |
| 346 host, net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES); |
| 347 if (registerable_domain.empty() && url::HostIsIPAddress(host)) |
| 348 registerable_domain = std::string(host); |
| 349 return registerable_domain; |
| 350 } |
| 351 |
| 344 bool ImportantSitesUtil::IsDialogDisabled(Profile* profile) { | 352 bool ImportantSitesUtil::IsDialogDisabled(Profile* profile) { |
| 345 PrefService* service = profile->GetPrefs(); | 353 PrefService* service = profile->GetPrefs(); |
| 346 DictionaryPrefUpdate update(service, prefs::kImportantSitesDialogHistory); | 354 DictionaryPrefUpdate update(service, prefs::kImportantSitesDialogHistory); |
| 347 | 355 |
| 348 return ShouldSuppressItem(update.Get()); | 356 return ShouldSuppressItem(update.Get()); |
| 349 } | 357 } |
| 350 | 358 |
| 351 void ImportantSitesUtil::RegisterProfilePrefs( | 359 void ImportantSitesUtil::RegisterProfilePrefs( |
| 352 user_prefs::PrefRegistrySyncable* registry) { | 360 user_prefs::PrefRegistrySyncable* registry) { |
| 353 registry->RegisterDictionaryPref(prefs::kImportantSitesDialogHistory); | 361 registry->RegisterDictionaryPref(prefs::kImportantSitesDialogHistory); |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 472 const GURL& origin) { | 480 const GURL& origin) { |
| 473 SiteEngagementScore::SetParamValuesForTesting(); | 481 SiteEngagementScore::SetParamValuesForTesting(); |
| 474 // First get data from site engagement. | 482 // First get data from site engagement. |
| 475 SiteEngagementService* site_engagement_service = | 483 SiteEngagementService* site_engagement_service = |
| 476 SiteEngagementService::Get(profile); | 484 SiteEngagementService::Get(profile); |
| 477 site_engagement_service->ResetBaseScoreForURL( | 485 site_engagement_service->ResetBaseScoreForURL( |
| 478 origin, SiteEngagementScore::GetMediumEngagementBoundary()); | 486 origin, SiteEngagementScore::GetMediumEngagementBoundary()); |
| 479 DCHECK(site_engagement_service->IsEngagementAtLeast( | 487 DCHECK(site_engagement_service->IsEngagementAtLeast( |
| 480 origin, blink::mojom::EngagementLevel::MEDIUM)); | 488 origin, blink::mojom::EngagementLevel::MEDIUM)); |
| 481 } | 489 } |
| OLD | NEW |