Chromium Code Reviews| 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 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 340 GURL origin(site.primary_pattern.ToString()); | 334 GURL origin(site.primary_pattern.ToString()); |
| 341 if (!AppBannerSettingsHelper::WasLaunchedRecently(profile, origin, now)) | 335 if (!AppBannerSettingsHelper::WasLaunchedRecently(profile, origin, now)) |
| 342 continue; | 336 continue; |
| 343 MaybePopulateImportantInfoForReason(origin, &content_origins, | 337 MaybePopulateImportantInfoForReason(origin, &content_origins, |
| 344 ImportantReason::HOME_SCREEN, output); | 338 ImportantReason::HOME_SCREEN, output); |
| 345 } | 339 } |
| 346 } | 340 } |
| 347 | 341 |
| 348 } // namespace | 342 } // namespace |
| 349 | 343 |
| 344 std::string ImportantSitesUtil::GetRegisterableDomainOrIP(const GURL& url) { | |
| 345 return GetRegisterableDomainOrIPFromHost(url.host_piece()); | |
| 346 } | |
| 347 | |
| 348 std::string ImportantSitesUtil::GetRegisterableDomainOrIPFromHost( | |
| 349 base::StringPiece host) { | |
| 350 std::string registerable_domain = | |
| 351 net::registry_controlled_domains::GetDomainAndRegistry( | |
| 352 host, net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES); | |
| 353 if (registerable_domain.empty() && url::HostIsIPAddress(host)) | |
| 354 registerable_domain = std::string(host); | |
|
Bernhard Bauer
2017/05/04 14:16:45
I would just directly return std::string(host).
dullweber
2017/05/05 08:35:28
Done.
| |
| 355 return registerable_domain; | |
| 356 } | |
| 357 | |
| 350 bool ImportantSitesUtil::IsDialogDisabled(Profile* profile) { | 358 bool ImportantSitesUtil::IsDialogDisabled(Profile* profile) { |
| 351 PrefService* service = profile->GetPrefs(); | 359 PrefService* service = profile->GetPrefs(); |
| 352 DictionaryPrefUpdate update(service, prefs::kImportantSitesDialogHistory); | 360 DictionaryPrefUpdate update(service, prefs::kImportantSitesDialogHistory); |
| 353 | 361 |
| 354 return ShouldSuppressItem(update.Get()); | 362 return ShouldSuppressItem(update.Get()); |
| 355 } | 363 } |
| 356 | 364 |
| 357 void ImportantSitesUtil::RegisterProfilePrefs( | 365 void ImportantSitesUtil::RegisterProfilePrefs( |
| 358 user_prefs::PrefRegistrySyncable* registry) { | 366 user_prefs::PrefRegistrySyncable* registry) { |
| 359 registry->RegisterDictionaryPref(prefs::kImportantSitesDialogHistory); | 367 registry->RegisterDictionaryPref(prefs::kImportantSitesDialogHistory); |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 478 const GURL& origin) { | 486 const GURL& origin) { |
| 479 SiteEngagementScore::SetParamValuesForTesting(); | 487 SiteEngagementScore::SetParamValuesForTesting(); |
| 480 // First get data from site engagement. | 488 // First get data from site engagement. |
| 481 SiteEngagementService* site_engagement_service = | 489 SiteEngagementService* site_engagement_service = |
| 482 SiteEngagementService::Get(profile); | 490 SiteEngagementService::Get(profile); |
| 483 site_engagement_service->ResetBaseScoreForURL( | 491 site_engagement_service->ResetBaseScoreForURL( |
| 484 origin, SiteEngagementScore::GetMediumEngagementBoundary()); | 492 origin, SiteEngagementScore::GetMediumEngagementBoundary()); |
| 485 DCHECK(site_engagement_service->IsEngagementAtLeast( | 493 DCHECK(site_engagement_service->IsEngagementAtLeast( |
| 486 origin, blink::mojom::EngagementLevel::MEDIUM)); | 494 origin, blink::mojom::EngagementLevel::MEDIUM)); |
| 487 } | 495 } |
| OLD | NEW |