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(); | |
Marc Treib
2016/02/25 10:34:32
Like my late comment on the other CL: Please only
atanasova
2016/02/25 11:09:10
For non-child accounts the behavior will always be
Marc Treib
2016/02/25 11:23:12
Well, my policy has always been to not even get a
| |
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::ALLOW) | |
Marc Treib
2016/02/25 10:34:32
This should probably be "== BLOCK". (WARN isn't ac
atanasova
2016/02/25 11:09:10
Done.
| |
493 continue; | |
494 | |
487 suggestions.push_back(make_scoped_ptr( | 495 suggestions.push_back(make_scoped_ptr( |
488 new Suggestion(visited.title, visited.url.spec(), TOP_SITES))); | 496 new Suggestion(visited.title, visited.url.spec(), TOP_SITES))); |
489 } | 497 } |
490 | 498 |
491 received_most_visited_sites_ = true; | 499 received_most_visited_sites_ = true; |
492 mv_source_ = TOP_SITES; | 500 mv_source_ = TOP_SITES; |
493 SaveNewNTPSuggestions(&suggestions); | 501 SaveNewNTPSuggestions(&suggestions); |
494 NotifyMostVisitedURLsObserver(); | 502 NotifyMostVisitedURLsObserver(); |
495 } | 503 } |
496 | 504 |
497 void MostVisitedSites::OnSuggestionsProfileAvailable( | 505 void MostVisitedSites::OnSuggestionsProfileAvailable( |
498 const SuggestionsProfile& suggestions_profile) { | 506 const SuggestionsProfile& suggestions_profile) { |
507 SupervisedUserURLFilter* url_filter = | |
508 SupervisedUserServiceFactory::GetForProfile(profile_) | |
509 ->GetURLFilterForUIThread(); | |
Marc Treib
2016/02/25 10:34:32
nit: Move this down to where it's actually needed.
atanasova
2016/02/25 11:09:10
Done.
| |
499 int num_tiles = suggestions_profile.suggestions_size(); | 510 int num_tiles = suggestions_profile.suggestions_size(); |
500 // With no server suggestions, fall back to local Most Visited. | 511 // With no server suggestions, fall back to local Most Visited. |
501 if (num_tiles == 0) { | 512 if (num_tiles == 0) { |
502 InitiateTopSitesQuery(); | 513 InitiateTopSitesQuery(); |
503 return; | 514 return; |
504 } | 515 } |
505 if (num_sites_ < num_tiles) | 516 if (num_sites_ < num_tiles) |
506 num_tiles = num_sites_; | 517 num_tiles = num_sites_; |
507 | 518 |
508 MostVisitedSites::SuggestionsVector suggestions; | 519 MostVisitedSites::SuggestionsVector suggestions; |
509 for (int i = 0; i < num_tiles; ++i) { | 520 for (int i = 0; i < num_tiles; ++i) { |
510 const ChromeSuggestion& suggestion = suggestions_profile.suggestions(i); | 521 const ChromeSuggestion& suggestion = suggestions_profile.suggestions(i); |
522 if (url_filter->GetFilteringBehaviorForURL(GURL(suggestion.url())) != | |
523 SupervisedUserURLFilter::FilteringBehavior::ALLOW) | |
524 continue; | |
525 | |
511 suggestions.push_back(make_scoped_ptr(new Suggestion( | 526 suggestions.push_back(make_scoped_ptr(new Suggestion( |
512 base::UTF8ToUTF16(suggestion.title()), suggestion.url(), | 527 base::UTF8ToUTF16(suggestion.title()), suggestion.url(), |
513 SUGGESTIONS_SERVICE, | 528 SUGGESTIONS_SERVICE, |
514 suggestion.providers_size() > 0 ? suggestion.providers(0) : -1))); | 529 suggestion.providers_size() > 0 ? suggestion.providers(0) : -1))); |
515 } | 530 } |
516 | 531 |
517 received_most_visited_sites_ = true; | 532 received_most_visited_sites_ = true; |
518 mv_source_ = SUGGESTIONS_SERVICE; | 533 mv_source_ = SUGGESTIONS_SERVICE; |
519 SaveNewNTPSuggestions(&suggestions); | 534 SaveNewNTPSuggestions(&suggestions); |
520 NotifyMostVisitedURLsObserver(); | 535 NotifyMostVisitedURLsObserver(); |
521 } | 536 } |
522 | 537 |
523 MostVisitedSites::SuggestionsVector | 538 MostVisitedSites::SuggestionsVector |
524 MostVisitedSites::CreateWhitelistEntryPointSuggestions( | 539 MostVisitedSites::CreateWhitelistEntryPointSuggestions( |
525 const MostVisitedSites::SuggestionsVector& personal_suggestions) { | 540 const MostVisitedSites::SuggestionsVector& personal_suggestions) { |
526 size_t num_personal_suggestions = personal_suggestions.size(); | 541 size_t num_personal_suggestions = personal_suggestions.size(); |
527 DCHECK_LE(num_personal_suggestions, static_cast<size_t>(num_sites_)); | 542 DCHECK_LE(num_personal_suggestions, static_cast<size_t>(num_sites_)); |
528 | 543 |
529 size_t num_whitelist_suggestions = num_sites_ - num_personal_suggestions; | 544 size_t num_whitelist_suggestions = num_sites_ - num_personal_suggestions; |
530 MostVisitedSites::SuggestionsVector whitelist_suggestions; | 545 MostVisitedSites::SuggestionsVector whitelist_suggestions; |
531 | 546 |
532 SupervisedUserService* supervised_user_service = | 547 SupervisedUserService* supervised_user_service = |
533 SupervisedUserServiceFactory::GetForProfile(profile_); | 548 SupervisedUserServiceFactory::GetForProfile(profile_); |
549 SupervisedUserURLFilter* url_filter = | |
550 supervised_user_service->GetURLFilterForUIThread(); | |
551 | |
552 std::set<std::string> personal_hosts; | |
553 for (const auto& suggestion : personal_suggestions) | |
554 personal_hosts.insert(suggestion->url.host()); | |
555 scoped_refptr<TopSites> top_sites(TopSitesFactory::GetForProfile(profile_)); | |
534 | 556 |
535 for (const auto& whitelist : supervised_user_service->whitelists()) { | 557 for (const auto& whitelist : supervised_user_service->whitelists()) { |
558 // Skip blacklisted sites. | |
559 if (top_sites && top_sites->IsBlacklisted(whitelist->entry_point())) | |
560 continue; | |
561 | |
562 // Skip suggestions already present. | |
563 if (personal_hosts.find(whitelist->entry_point().host()) != | |
564 personal_hosts.end()) | |
565 continue; | |
566 | |
567 // Skip whitelist entry points that are manually blocked. | |
568 if (url_filter->GetFilteringBehaviorForURL(whitelist->entry_point()) != | |
569 SupervisedUserURLFilter::FilteringBehavior::ALLOW) | |
570 continue; | |
571 | |
536 whitelist_suggestions.push_back(make_scoped_ptr(new Suggestion( | 572 whitelist_suggestions.push_back(make_scoped_ptr(new Suggestion( |
537 whitelist->title(), whitelist->entry_point(), WHITELIST))); | 573 whitelist->title(), whitelist->entry_point(), WHITELIST))); |
538 if (whitelist_suggestions.size() >= num_whitelist_suggestions) | 574 if (whitelist_suggestions.size() >= num_whitelist_suggestions) |
539 break; | 575 break; |
540 } | 576 } |
541 | 577 |
542 return whitelist_suggestions; | 578 return whitelist_suggestions; |
543 } | 579 } |
544 | 580 |
545 MostVisitedSites::SuggestionsVector | 581 MostVisitedSites::SuggestionsVector |
546 MostVisitedSites::CreatePopularSitesSuggestions( | 582 MostVisitedSites::CreatePopularSitesSuggestions( |
547 const MostVisitedSites::SuggestionsVector& personal_suggestions, | 583 const MostVisitedSites::SuggestionsVector& personal_suggestions, |
548 const MostVisitedSites::SuggestionsVector& whitelist_suggestions) { | 584 const MostVisitedSites::SuggestionsVector& whitelist_suggestions) { |
585 MostVisitedSites::SuggestionsVector popular_sites_suggestions; | |
586 // For unicorn child accounts popular sites suggestions will not be added. | |
Marc Treib
2016/02/25 10:34:32
What's this "unicorn" thing you speak of? ;) (Just
atanasova
2016/02/25 11:09:10
Done.
| |
587 if (profile_->IsChild()) { | |
588 return popular_sites_suggestions; | |
589 } | |
590 | |
549 size_t num_suggestions = | 591 size_t num_suggestions = |
550 personal_suggestions.size() + whitelist_suggestions.size(); | 592 personal_suggestions.size() + whitelist_suggestions.size(); |
551 DCHECK_LE(num_suggestions, static_cast<size_t>(num_sites_)); | 593 DCHECK_LE(num_suggestions, static_cast<size_t>(num_sites_)); |
552 | 594 |
553 // Collect non-blacklisted popular suggestions, skipping those already present | 595 // Collect non-blacklisted popular suggestions, skipping those already present |
554 // in the personal suggestions. | 596 // in the personal suggestions. |
555 size_t num_popular_sites_suggestions = num_sites_ - num_suggestions; | 597 size_t num_popular_sites_suggestions = num_sites_ - num_suggestions; |
556 MostVisitedSites::SuggestionsVector popular_sites_suggestions; | |
557 | 598 |
558 if (num_popular_sites_suggestions > 0 && popular_sites_) { | 599 if (num_popular_sites_suggestions > 0 && popular_sites_) { |
559 std::set<std::string> personal_hosts; | 600 std::set<std::string> hosts; |
560 for (const auto& suggestion : personal_suggestions) | 601 for (const auto& suggestion : personal_suggestions) |
561 personal_hosts.insert(suggestion->url.host()); | 602 hosts.insert(suggestion->url.host()); |
603 for (const auto& suggestion : whitelist_suggestions) | |
604 hosts.insert(suggestion->url.host()); | |
562 scoped_refptr<TopSites> top_sites(TopSitesFactory::GetForProfile(profile_)); | 605 scoped_refptr<TopSites> top_sites(TopSitesFactory::GetForProfile(profile_)); |
563 for (const PopularSites::Site& popular_site : popular_sites_->sites()) { | 606 for (const PopularSites::Site& popular_site : popular_sites_->sites()) { |
564 // Skip blacklisted sites. | 607 // Skip blacklisted sites. |
565 if (top_sites && top_sites->IsBlacklisted(popular_site.url)) | 608 if (top_sites && top_sites->IsBlacklisted(popular_site.url)) |
566 continue; | 609 continue; |
567 std::string host = popular_site.url.host(); | 610 std::string host = popular_site.url.host(); |
568 // Skip suggestions already present in personal. | 611 // Skip suggestions already present in personal. |
Marc Treib
2016/02/25 10:34:32
or whitelists
atanasova
2016/02/25 11:09:10
Done.
| |
569 if (personal_hosts.find(host) != personal_hosts.end()) | 612 if (hosts.find(host) != hosts.end()) |
570 continue; | 613 continue; |
571 | 614 |
572 popular_sites_suggestions.push_back(make_scoped_ptr( | 615 popular_sites_suggestions.push_back(make_scoped_ptr( |
573 new Suggestion(popular_site.title, popular_site.url, POPULAR))); | 616 new Suggestion(popular_site.title, popular_site.url, POPULAR))); |
574 if (popular_sites_suggestions.size() >= num_popular_sites_suggestions) | 617 if (popular_sites_suggestions.size() >= num_popular_sites_suggestions) |
575 break; | 618 break; |
576 } | 619 } |
577 } | 620 } |
578 return popular_sites_suggestions; | 621 return popular_sites_suggestions; |
579 } | 622 } |
(...skipping 237 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
817 } | 860 } |
818 } | 861 } |
819 | 862 |
820 static jlong Init(JNIEnv* env, | 863 static jlong Init(JNIEnv* env, |
821 const JavaParamRef<jobject>& obj, | 864 const JavaParamRef<jobject>& obj, |
822 const JavaParamRef<jobject>& jprofile) { | 865 const JavaParamRef<jobject>& jprofile) { |
823 MostVisitedSites* most_visited_sites = | 866 MostVisitedSites* most_visited_sites = |
824 new MostVisitedSites(ProfileAndroid::FromProfileAndroid(jprofile)); | 867 new MostVisitedSites(ProfileAndroid::FromProfileAndroid(jprofile)); |
825 return reinterpret_cast<intptr_t>(most_visited_sites); | 868 return reinterpret_cast<intptr_t>(most_visited_sites); |
826 } | 869 } |
OLD | NEW |