OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/most_visited_sites.h" | 5 #include "chrome/browser/android/most_visited_sites.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/android/jni_android.h" | 9 #include "base/android/jni_android.h" |
10 #include "base/android/jni_array.h" | 10 #include "base/android/jni_array.h" |
(...skipping 11 matching lines...) Expand all Loading... |
22 #include "base/time/time.h" | 22 #include "base/time/time.h" |
23 #include "chrome/browser/android/popular_sites.h" | 23 #include "chrome/browser/android/popular_sites.h" |
24 #include "chrome/browser/history/top_sites_factory.h" | 24 #include "chrome/browser/history/top_sites_factory.h" |
25 #include "chrome/browser/profiles/profile.h" | 25 #include "chrome/browser/profiles/profile.h" |
26 #include "chrome/browser/profiles/profile_android.h" | 26 #include "chrome/browser/profiles/profile_android.h" |
27 #include "chrome/browser/search/suggestions/suggestions_service_factory.h" | 27 #include "chrome/browser/search/suggestions/suggestions_service_factory.h" |
28 #include "chrome/browser/search/suggestions/suggestions_source.h" | 28 #include "chrome/browser/search/suggestions/suggestions_source.h" |
29 #include "chrome/browser/search/suggestions/suggestions_utils.h" | 29 #include "chrome/browser/search/suggestions/suggestions_utils.h" |
30 #include "chrome/browser/supervised_user/supervised_user_service.h" | 30 #include "chrome/browser/supervised_user/supervised_user_service.h" |
31 #include "chrome/browser/supervised_user/supervised_user_service_factory.h" | 31 #include "chrome/browser/supervised_user/supervised_user_service_factory.h" |
| 32 #include "chrome/browser/supervised_user/supervised_user_url_filter.h" |
32 #include "chrome/browser/sync/profile_sync_service_factory.h" | 33 #include "chrome/browser/sync/profile_sync_service_factory.h" |
33 #include "chrome/browser/thumbnails/thumbnail_list_source.h" | 34 #include "chrome/browser/thumbnails/thumbnail_list_source.h" |
34 #include "chrome/common/chrome_switches.h" | 35 #include "chrome/common/chrome_switches.h" |
35 #include "chrome/common/pref_names.h" | 36 #include "chrome/common/pref_names.h" |
36 #include "components/browser_sync/browser/profile_sync_service.h" | 37 #include "components/browser_sync/browser/profile_sync_service.h" |
37 #include "components/history/core/browser/top_sites.h" | 38 #include "components/history/core/browser/top_sites.h" |
38 #include "components/pref_registry/pref_registry_syncable.h" | 39 #include "components/pref_registry/pref_registry_syncable.h" |
39 #include "components/prefs/pref_service.h" | 40 #include "components/prefs/pref_service.h" |
40 #include "components/suggestions/suggestions_service.h" | 41 #include "components/suggestions/suggestions_service.h" |
41 #include "components/variations/variations_associated_data.h" | 42 #include "components/variations/variations_associated_data.h" |
(...skipping 426 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
468 return; | 469 return; |
469 | 470 |
470 top_sites->GetMostVisitedURLs( | 471 top_sites->GetMostVisitedURLs( |
471 base::Bind(&MostVisitedSites::OnMostVisitedURLsAvailable, | 472 base::Bind(&MostVisitedSites::OnMostVisitedURLsAvailable, |
472 weak_ptr_factory_.GetWeakPtr()), | 473 weak_ptr_factory_.GetWeakPtr()), |
473 false); | 474 false); |
474 } | 475 } |
475 | 476 |
476 void MostVisitedSites::OnMostVisitedURLsAvailable( | 477 void MostVisitedSites::OnMostVisitedURLsAvailable( |
477 const history::MostVisitedURLList& visited_list) { | 478 const history::MostVisitedURLList& visited_list) { |
| 479 SupervisedUserURLFilter* url_filter = |
| 480 SupervisedUserServiceFactory::GetForProfile(profile_) |
| 481 ->GetURLFilterForUIThread(); |
478 MostVisitedSites::SuggestionsVector suggestions; | 482 MostVisitedSites::SuggestionsVector suggestions; |
479 size_t num_tiles = | 483 size_t num_tiles = |
480 std::min(visited_list.size(), static_cast<size_t>(num_sites_)); | 484 std::min(visited_list.size(), static_cast<size_t>(num_sites_)); |
481 for (size_t i = 0; i < num_tiles; ++i) { | 485 for (size_t i = 0; i < num_tiles; ++i) { |
482 const history::MostVisitedURL& visited = visited_list[i]; | 486 const history::MostVisitedURL& visited = visited_list[i]; |
483 if (visited.url.is_empty()) { | 487 if (visited.url.is_empty()) { |
484 num_tiles = i; | 488 num_tiles = i; |
485 break; // This is the signal that there are no more real visited sites. | 489 break; // This is the signal that there are no more real visited sites. |
486 } | 490 } |
| 491 if (url_filter->GetFilteringBehaviorForURL(visited.url) == |
| 492 SupervisedUserURLFilter::FilteringBehavior::BLOCK) { |
| 493 continue; |
| 494 } |
| 495 |
487 suggestions.push_back(make_scoped_ptr( | 496 suggestions.push_back(make_scoped_ptr( |
488 new Suggestion(visited.title, visited.url.spec(), TOP_SITES))); | 497 new Suggestion(visited.title, visited.url.spec(), TOP_SITES))); |
489 } | 498 } |
490 | 499 |
491 received_most_visited_sites_ = true; | 500 received_most_visited_sites_ = true; |
492 mv_source_ = TOP_SITES; | 501 mv_source_ = TOP_SITES; |
493 SaveNewNTPSuggestions(&suggestions); | 502 SaveNewNTPSuggestions(&suggestions); |
494 NotifyMostVisitedURLsObserver(); | 503 NotifyMostVisitedURLsObserver(); |
495 } | 504 } |
496 | 505 |
497 void MostVisitedSites::OnSuggestionsProfileAvailable( | 506 void MostVisitedSites::OnSuggestionsProfileAvailable( |
498 const SuggestionsProfile& suggestions_profile) { | 507 const SuggestionsProfile& suggestions_profile) { |
499 int num_tiles = suggestions_profile.suggestions_size(); | 508 int num_tiles = suggestions_profile.suggestions_size(); |
500 // With no server suggestions, fall back to local Most Visited. | 509 // With no server suggestions, fall back to local Most Visited. |
501 if (num_tiles == 0) { | 510 if (num_tiles == 0) { |
502 InitiateTopSitesQuery(); | 511 InitiateTopSitesQuery(); |
503 return; | 512 return; |
504 } | 513 } |
505 if (num_sites_ < num_tiles) | 514 if (num_sites_ < num_tiles) |
506 num_tiles = num_sites_; | 515 num_tiles = num_sites_; |
507 | 516 |
| 517 SupervisedUserURLFilter* url_filter = |
| 518 SupervisedUserServiceFactory::GetForProfile(profile_) |
| 519 ->GetURLFilterForUIThread(); |
508 MostVisitedSites::SuggestionsVector suggestions; | 520 MostVisitedSites::SuggestionsVector suggestions; |
509 for (int i = 0; i < num_tiles; ++i) { | 521 for (int i = 0; i < num_tiles; ++i) { |
510 const ChromeSuggestion& suggestion = suggestions_profile.suggestions(i); | 522 const ChromeSuggestion& suggestion = suggestions_profile.suggestions(i); |
| 523 if (url_filter->GetFilteringBehaviorForURL(GURL(suggestion.url())) == |
| 524 SupervisedUserURLFilter::FilteringBehavior::BLOCK) { |
| 525 continue; |
| 526 } |
| 527 |
511 suggestions.push_back(make_scoped_ptr(new Suggestion( | 528 suggestions.push_back(make_scoped_ptr(new Suggestion( |
512 base::UTF8ToUTF16(suggestion.title()), suggestion.url(), | 529 base::UTF8ToUTF16(suggestion.title()), suggestion.url(), |
513 SUGGESTIONS_SERVICE, | 530 SUGGESTIONS_SERVICE, |
514 suggestion.providers_size() > 0 ? suggestion.providers(0) : -1))); | 531 suggestion.providers_size() > 0 ? suggestion.providers(0) : -1))); |
515 } | 532 } |
516 | 533 |
517 received_most_visited_sites_ = true; | 534 received_most_visited_sites_ = true; |
518 mv_source_ = SUGGESTIONS_SERVICE; | 535 mv_source_ = SUGGESTIONS_SERVICE; |
519 SaveNewNTPSuggestions(&suggestions); | 536 SaveNewNTPSuggestions(&suggestions); |
520 NotifyMostVisitedURLsObserver(); | 537 NotifyMostVisitedURLsObserver(); |
521 } | 538 } |
522 | 539 |
523 MostVisitedSites::SuggestionsVector | 540 MostVisitedSites::SuggestionsVector |
524 MostVisitedSites::CreateWhitelistEntryPointSuggestions( | 541 MostVisitedSites::CreateWhitelistEntryPointSuggestions( |
525 const MostVisitedSites::SuggestionsVector& personal_suggestions) { | 542 const MostVisitedSites::SuggestionsVector& personal_suggestions) { |
526 size_t num_personal_suggestions = personal_suggestions.size(); | 543 size_t num_personal_suggestions = personal_suggestions.size(); |
527 DCHECK_LE(num_personal_suggestions, static_cast<size_t>(num_sites_)); | 544 DCHECK_LE(num_personal_suggestions, static_cast<size_t>(num_sites_)); |
528 | 545 |
529 size_t num_whitelist_suggestions = num_sites_ - num_personal_suggestions; | 546 size_t num_whitelist_suggestions = num_sites_ - num_personal_suggestions; |
530 MostVisitedSites::SuggestionsVector whitelist_suggestions; | 547 MostVisitedSites::SuggestionsVector whitelist_suggestions; |
531 | 548 |
532 SupervisedUserService* supervised_user_service = | 549 SupervisedUserService* supervised_user_service = |
533 SupervisedUserServiceFactory::GetForProfile(profile_); | 550 SupervisedUserServiceFactory::GetForProfile(profile_); |
| 551 SupervisedUserURLFilter* url_filter = |
| 552 supervised_user_service->GetURLFilterForUIThread(); |
| 553 |
| 554 std::set<std::string> personal_hosts; |
| 555 for (const auto& suggestion : personal_suggestions) |
| 556 personal_hosts.insert(suggestion->url.host()); |
| 557 scoped_refptr<TopSites> top_sites(TopSitesFactory::GetForProfile(profile_)); |
534 | 558 |
535 for (const auto& whitelist : supervised_user_service->whitelists()) { | 559 for (const auto& whitelist : supervised_user_service->whitelists()) { |
| 560 // Skip blacklisted sites. |
| 561 if (top_sites && top_sites->IsBlacklisted(whitelist->entry_point())) |
| 562 continue; |
| 563 |
| 564 // Skip suggestions already present. |
| 565 if (personal_hosts.find(whitelist->entry_point().host()) != |
| 566 personal_hosts.end()) |
| 567 continue; |
| 568 |
| 569 // Skip whitelist entry points that are manually blocked. |
| 570 if (url_filter->GetFilteringBehaviorForURL(whitelist->entry_point()) == |
| 571 SupervisedUserURLFilter::FilteringBehavior::BLOCK) { |
| 572 continue; |
| 573 } |
| 574 |
536 whitelist_suggestions.push_back(make_scoped_ptr(new Suggestion( | 575 whitelist_suggestions.push_back(make_scoped_ptr(new Suggestion( |
537 whitelist->title(), whitelist->entry_point(), WHITELIST))); | 576 whitelist->title(), whitelist->entry_point(), WHITELIST))); |
538 if (whitelist_suggestions.size() >= num_whitelist_suggestions) | 577 if (whitelist_suggestions.size() >= num_whitelist_suggestions) |
539 break; | 578 break; |
540 } | 579 } |
541 | 580 |
542 return whitelist_suggestions; | 581 return whitelist_suggestions; |
543 } | 582 } |
544 | 583 |
545 MostVisitedSites::SuggestionsVector | 584 MostVisitedSites::SuggestionsVector |
546 MostVisitedSites::CreatePopularSitesSuggestions( | 585 MostVisitedSites::CreatePopularSitesSuggestions( |
547 const MostVisitedSites::SuggestionsVector& personal_suggestions, | 586 const MostVisitedSites::SuggestionsVector& personal_suggestions, |
548 const MostVisitedSites::SuggestionsVector& whitelist_suggestions) { | 587 const MostVisitedSites::SuggestionsVector& whitelist_suggestions) { |
| 588 // For child accounts popular sites suggestions will not be added. |
| 589 if (profile_->IsChild()) |
| 590 return MostVisitedSites::SuggestionsVector(); |
| 591 |
549 size_t num_suggestions = | 592 size_t num_suggestions = |
550 personal_suggestions.size() + whitelist_suggestions.size(); | 593 personal_suggestions.size() + whitelist_suggestions.size(); |
551 DCHECK_LE(num_suggestions, static_cast<size_t>(num_sites_)); | 594 DCHECK_LE(num_suggestions, static_cast<size_t>(num_sites_)); |
552 | 595 |
553 // Collect non-blacklisted popular suggestions, skipping those already present | 596 // Collect non-blacklisted popular suggestions, skipping those already present |
554 // in the personal suggestions. | 597 // in the personal suggestions. |
555 size_t num_popular_sites_suggestions = num_sites_ - num_suggestions; | 598 size_t num_popular_sites_suggestions = num_sites_ - num_suggestions; |
556 MostVisitedSites::SuggestionsVector popular_sites_suggestions; | 599 MostVisitedSites::SuggestionsVector popular_sites_suggestions; |
557 | 600 |
558 if (num_popular_sites_suggestions > 0 && popular_sites_) { | 601 if (num_popular_sites_suggestions > 0 && popular_sites_) { |
559 std::set<std::string> personal_hosts; | 602 std::set<std::string> hosts; |
560 for (const auto& suggestion : personal_suggestions) | 603 for (const auto& suggestion : personal_suggestions) |
561 personal_hosts.insert(suggestion->url.host()); | 604 hosts.insert(suggestion->url.host()); |
| 605 for (const auto& suggestion : whitelist_suggestions) |
| 606 hosts.insert(suggestion->url.host()); |
562 scoped_refptr<TopSites> top_sites(TopSitesFactory::GetForProfile(profile_)); | 607 scoped_refptr<TopSites> top_sites(TopSitesFactory::GetForProfile(profile_)); |
563 for (const PopularSites::Site& popular_site : popular_sites_->sites()) { | 608 for (const PopularSites::Site& popular_site : popular_sites_->sites()) { |
564 // Skip blacklisted sites. | 609 // Skip blacklisted sites. |
565 if (top_sites && top_sites->IsBlacklisted(popular_site.url)) | 610 if (top_sites && top_sites->IsBlacklisted(popular_site.url)) |
566 continue; | 611 continue; |
567 std::string host = popular_site.url.host(); | 612 std::string host = popular_site.url.host(); |
568 // Skip suggestions already present in personal. | 613 // Skip suggestions already present in personal or whitelists. |
569 if (personal_hosts.find(host) != personal_hosts.end()) | 614 if (hosts.find(host) != hosts.end()) |
570 continue; | 615 continue; |
571 | 616 |
572 popular_sites_suggestions.push_back(make_scoped_ptr( | 617 popular_sites_suggestions.push_back(make_scoped_ptr( |
573 new Suggestion(popular_site.title, popular_site.url, POPULAR))); | 618 new Suggestion(popular_site.title, popular_site.url, POPULAR))); |
574 if (popular_sites_suggestions.size() >= num_popular_sites_suggestions) | 619 if (popular_sites_suggestions.size() >= num_popular_sites_suggestions) |
575 break; | 620 break; |
576 } | 621 } |
577 } | 622 } |
578 return popular_sites_suggestions; | 623 return popular_sites_suggestions; |
579 } | 624 } |
(...skipping 237 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
817 } | 862 } |
818 } | 863 } |
819 | 864 |
820 static jlong Init(JNIEnv* env, | 865 static jlong Init(JNIEnv* env, |
821 const JavaParamRef<jobject>& obj, | 866 const JavaParamRef<jobject>& obj, |
822 const JavaParamRef<jobject>& jprofile) { | 867 const JavaParamRef<jobject>& jprofile) { |
823 MostVisitedSites* most_visited_sites = | 868 MostVisitedSites* most_visited_sites = |
824 new MostVisitedSites(ProfileAndroid::FromProfileAndroid(jprofile)); | 869 new MostVisitedSites(ProfileAndroid::FromProfileAndroid(jprofile)); |
825 return reinterpret_cast<intptr_t>(most_visited_sites); | 870 return reinterpret_cast<intptr_t>(most_visited_sites); |
826 } | 871 } |
OLD | NEW |