| 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 <memory> | 9 #include <memory> |
| 10 #include <set> | 10 #include <set> |
| (...skipping 17 matching lines...) Expand all Loading... |
| 28 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" | 28 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" |
| 29 #include "url/gurl.h" | 29 #include "url/gurl.h" |
| 30 | 30 |
| 31 namespace { | 31 namespace { |
| 32 using bookmarks::BookmarkModel; | 32 using bookmarks::BookmarkModel; |
| 33 using ImportantDomainInfo = ImportantSitesUtil::ImportantDomainInfo; | 33 using ImportantDomainInfo = ImportantSitesUtil::ImportantDomainInfo; |
| 34 | 34 |
| 35 static const char kNumTimesIgnoredName[] = "NumTimesIgnored"; | 35 static const char kNumTimesIgnoredName[] = "NumTimesIgnored"; |
| 36 static const int kTimesIgnoredForBlacklist = 3; | 36 static const int kTimesIgnoredForBlacklist = 3; |
| 37 | 37 |
| 38 // These are the maximum # of bookmarks we can use as signals. If the user has |
| 39 // <= kMaxBookmarks, then we just use those bookmarks. Otherwise we filter all |
| 40 // bookmarks on site engagement > 0, sort, and trim to kMaxBookmarks. |
| 41 static const int kMaxBookmarks = 5; |
| 42 |
| 38 // Do not change the values here, as they are used for UMA histograms. | 43 // Do not change the values here, as they are used for UMA histograms. |
| 39 enum ImportantReason { | 44 enum ImportantReason { |
| 40 ENGAGEMENT = 0, | 45 ENGAGEMENT = 0, |
| 41 DURABLE = 1, | 46 DURABLE = 1, |
| 42 BOOKMARKS = 2, | 47 BOOKMARKS = 2, |
| 43 HOME_SCREEN = 3, | 48 HOME_SCREEN = 3, |
| 44 NOTIFICATIONS = 4, | 49 NOTIFICATIONS = 4, |
| 45 REASON_BOUNDARY | 50 REASON_BOUNDARY |
| 46 }; | 51 }; |
| 47 | 52 |
| (...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 198 } | 203 } |
| 199 | 204 |
| 200 ignoring_domains.insert(origin.host()); | 205 ignoring_domains.insert(origin.host()); |
| 201 } | 206 } |
| 202 return ignoring_domains; | 207 return ignoring_domains; |
| 203 } | 208 } |
| 204 | 209 |
| 205 void PopulateInfoMapWithSiteEngagement( | 210 void PopulateInfoMapWithSiteEngagement( |
| 206 Profile* profile, | 211 Profile* profile, |
| 207 SiteEngagementService::EngagementLevel minimum_engagement, | 212 SiteEngagementService::EngagementLevel minimum_engagement, |
| 213 std::map<GURL, double>* engagement_map, |
| 208 base::hash_map<std::string, ImportantDomainInfo>* output) { | 214 base::hash_map<std::string, ImportantDomainInfo>* output) { |
| 209 SiteEngagementService* service = SiteEngagementService::Get(profile); | 215 SiteEngagementService* service = SiteEngagementService::Get(profile); |
| 210 std::map<GURL, double> engagement_map = service->GetScoreMap(); | 216 *engagement_map = service->GetScoreMap(); |
| 211 // We can have multiple origins for a single domain, so we record the one | 217 // We can have multiple origins for a single domain, so we record the one |
| 212 // with the highest engagement score. | 218 // with the highest engagement score. |
| 213 for (const auto& url_engagement_pair : engagement_map) { | 219 for (const auto& url_engagement_pair : *engagement_map) { |
| 214 if (!service->IsEngagementAtLeast(url_engagement_pair.first, | 220 if (!service->IsEngagementAtLeast(url_engagement_pair.first, |
| 215 minimum_engagement)) { | 221 minimum_engagement)) { |
| 216 continue; | 222 continue; |
| 217 } | 223 } |
| 218 std::string registerable_domain = | 224 std::string registerable_domain = |
| 219 GetRegisterableDomainOrIP(url_engagement_pair.first); | 225 GetRegisterableDomainOrIP(url_engagement_pair.first); |
| 220 ImportantDomainInfo& info = (*output)[registerable_domain]; | 226 ImportantDomainInfo& info = (*output)[registerable_domain]; |
| 221 if (url_engagement_pair.second > info.engagement_score) { | 227 if (url_engagement_pair.second > info.engagement_score) { |
| 222 info.registerable_domain = registerable_domain; | 228 info.registerable_domain = registerable_domain; |
| 223 info.engagement_score = url_engagement_pair.second; | 229 info.engagement_score = url_engagement_pair.second; |
| (...skipping 19 matching lines...) Expand all Loading... |
| 243 for (const ContentSettingPatternSource& site : content_settings_list) { | 249 for (const ContentSettingPatternSource& site : content_settings_list) { |
| 244 if (site.setting != CONTENT_SETTING_ALLOW) | 250 if (site.setting != CONTENT_SETTING_ALLOW) |
| 245 continue; | 251 continue; |
| 246 MaybePopulateImportantInfoForReason(GURL(site.primary_pattern.ToString()), | 252 MaybePopulateImportantInfoForReason(GURL(site.primary_pattern.ToString()), |
| 247 &content_origins, reason, output); | 253 &content_origins, reason, output); |
| 248 } | 254 } |
| 249 } | 255 } |
| 250 | 256 |
| 251 void PopulateInfoMapWithBookmarks( | 257 void PopulateInfoMapWithBookmarks( |
| 252 Profile* profile, | 258 Profile* profile, |
| 259 const std::map<GURL, double>& engagement_map, |
| 253 base::hash_map<std::string, ImportantDomainInfo>* output) { | 260 base::hash_map<std::string, ImportantDomainInfo>* output) { |
| 261 SiteEngagementService* service = SiteEngagementService::Get(profile); |
| 254 BookmarkModel* model = | 262 BookmarkModel* model = |
| 255 BookmarkModelFactory::GetForBrowserContextIfExists(profile); | 263 BookmarkModelFactory::GetForBrowserContextIfExists(profile); |
| 256 if (!model) | 264 if (!model) |
| 257 return; | 265 return; |
| 258 std::vector<BookmarkModel::URLAndTitle> bookmarks; | 266 std::vector<BookmarkModel::URLAndTitle> untrimmed_bookmarks; |
| 259 model->GetBookmarks(&bookmarks); | 267 model->GetBookmarks(&untrimmed_bookmarks); |
| 268 |
| 269 // Process the bookmarks and optionally trim them if we have too many. |
| 270 std::vector<BookmarkModel::URLAndTitle> result_bookmarks; |
| 271 if (untrimmed_bookmarks.size() > kMaxBookmarks) { |
| 272 std::copy_if(untrimmed_bookmarks.begin(), untrimmed_bookmarks.end(), |
| 273 std::back_inserter(result_bookmarks), |
| 274 [service](const BookmarkModel::URLAndTitle& entry) { |
| 275 return service->IsEngagementAtLeast( |
| 276 entry.url.GetOrigin(), |
| 277 SiteEngagementService::ENGAGEMENT_LEVEL_LOW); |
| 278 }); |
| 279 std::sort(result_bookmarks.begin(), result_bookmarks.end(), |
| 280 [&engagement_map](const BookmarkModel::URLAndTitle& a, |
| 281 const BookmarkModel::URLAndTitle& b) { |
| 282 double a_score = engagement_map.at(a.url.GetOrigin()); |
| 283 double b_score = engagement_map.at(b.url.GetOrigin()); |
| 284 return a_score > b_score; |
| 285 }); |
| 286 if (result_bookmarks.size() > kMaxBookmarks) |
| 287 result_bookmarks.resize(kMaxBookmarks); |
| 288 } else { |
| 289 result_bookmarks = std::move(untrimmed_bookmarks); |
| 290 } |
| 291 |
| 260 std::set<GURL> content_origins; | 292 std::set<GURL> content_origins; |
| 261 for (const BookmarkModel::URLAndTitle& bookmark : bookmarks) { | 293 for (const BookmarkModel::URLAndTitle& bookmark : result_bookmarks) { |
| 262 MaybePopulateImportantInfoForReason(bookmark.url, &content_origins, | 294 MaybePopulateImportantInfoForReason(bookmark.url, &content_origins, |
| 263 ImportantReason::BOOKMARKS, output); | 295 ImportantReason::BOOKMARKS, output); |
| 264 } | 296 } |
| 265 } | 297 } |
| 266 | 298 |
| 267 void PopulateInfoMapWithHomeScreen( | 299 void PopulateInfoMapWithHomeScreen( |
| 268 Profile* profile, | 300 Profile* profile, |
| 269 base::hash_map<std::string, ImportantDomainInfo>* output) { | 301 base::hash_map<std::string, ImportantDomainInfo>* output) { |
| 270 ContentSettingsForOneType content_settings_list; | 302 ContentSettingsForOneType content_settings_list; |
| 271 HostContentSettingsMapFactory::GetForProfile(profile)->GetSettingsForOneType( | 303 HostContentSettingsMapFactory::GetForProfile(profile)->GetSettingsForOneType( |
| (...skipping 11 matching lines...) Expand all Loading... |
| 283 ImportantReason::HOME_SCREEN, output); | 315 ImportantReason::HOME_SCREEN, output); |
| 284 } | 316 } |
| 285 } | 317 } |
| 286 | 318 |
| 287 } // namespace | 319 } // namespace |
| 288 | 320 |
| 289 std::vector<ImportantDomainInfo> | 321 std::vector<ImportantDomainInfo> |
| 290 ImportantSitesUtil::GetImportantRegisterableDomains(Profile* profile, | 322 ImportantSitesUtil::GetImportantRegisterableDomains(Profile* profile, |
| 291 size_t max_results) { | 323 size_t max_results) { |
| 292 base::hash_map<std::string, ImportantDomainInfo> important_info; | 324 base::hash_map<std::string, ImportantDomainInfo> important_info; |
| 325 std::map<GURL, double> engagement_map; |
| 293 | 326 |
| 294 PopulateInfoMapWithSiteEngagement( | 327 PopulateInfoMapWithSiteEngagement( |
| 295 profile, SiteEngagementService::ENGAGEMENT_LEVEL_MEDIUM, &important_info); | 328 profile, SiteEngagementService::ENGAGEMENT_LEVEL_MEDIUM, &engagement_map, |
| 329 &important_info); |
| 296 | 330 |
| 297 PopulateInfoMapWithContentTypeAllowed( | 331 PopulateInfoMapWithContentTypeAllowed( |
| 298 profile, CONTENT_SETTINGS_TYPE_NOTIFICATIONS, | 332 profile, CONTENT_SETTINGS_TYPE_NOTIFICATIONS, |
| 299 ImportantReason::NOTIFICATIONS, &important_info); | 333 ImportantReason::NOTIFICATIONS, &important_info); |
| 300 | 334 |
| 301 PopulateInfoMapWithContentTypeAllowed( | 335 PopulateInfoMapWithContentTypeAllowed( |
| 302 profile, CONTENT_SETTINGS_TYPE_DURABLE_STORAGE, ImportantReason::DURABLE, | 336 profile, CONTENT_SETTINGS_TYPE_DURABLE_STORAGE, ImportantReason::DURABLE, |
| 303 &important_info); | 337 &important_info); |
| 304 | 338 |
| 305 PopulateInfoMapWithBookmarks(profile, &important_info); | 339 PopulateInfoMapWithBookmarks(profile, engagement_map, &important_info); |
| 306 | 340 |
| 307 PopulateInfoMapWithHomeScreen(profile, &important_info); | 341 PopulateInfoMapWithHomeScreen(profile, &important_info); |
| 308 | 342 |
| 309 base::hash_set<std::string> blacklisted_domains = | 343 base::hash_set<std::string> blacklisted_domains = |
| 310 GetBlacklistedImportantDomains(profile); | 344 GetBlacklistedImportantDomains(profile); |
| 311 | 345 |
| 312 std::vector<std::pair<std::string, ImportantDomainInfo>> items( | 346 std::vector<std::pair<std::string, ImportantDomainInfo>> items( |
| 313 important_info.begin(), important_info.end()); | 347 important_info.begin(), important_info.end()); |
| 314 std::sort(items.begin(), items.end(), &CompareDescendingImportantInfo); | 348 std::sort(items.begin(), items.end(), &CompareDescendingImportantInfo); |
| 315 | 349 |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 397 void ImportantSitesUtil::MarkOriginAsImportantForTesting(Profile* profile, | 431 void ImportantSitesUtil::MarkOriginAsImportantForTesting(Profile* profile, |
| 398 const GURL& origin) { | 432 const GURL& origin) { |
| 399 // First get data from site engagement. | 433 // First get data from site engagement. |
| 400 SiteEngagementService* site_engagement_service = | 434 SiteEngagementService* site_engagement_service = |
| 401 SiteEngagementService::Get(profile); | 435 SiteEngagementService::Get(profile); |
| 402 site_engagement_service->ResetScoreForURL( | 436 site_engagement_service->ResetScoreForURL( |
| 403 origin, SiteEngagementScore::GetMediumEngagementBoundary()); | 437 origin, SiteEngagementScore::GetMediumEngagementBoundary()); |
| 404 DCHECK(site_engagement_service->IsEngagementAtLeast( | 438 DCHECK(site_engagement_service->IsEngagementAtLeast( |
| 405 origin, SiteEngagementService::ENGAGEMENT_LEVEL_MEDIUM)); | 439 origin, SiteEngagementService::ENGAGEMENT_LEVEL_MEDIUM)); |
| 406 } | 440 } |
| OLD | NEW |