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; | |
Bernhard Bauer
2016/02/25 11:14:48
I would add braces around this if the condition is
atanasova
2016/02/25 11:31:32
Done here and the other places that have the compa
| |
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) { |
499 int num_tiles = suggestions_profile.suggestions_size(); | 507 int num_tiles = suggestions_profile.suggestions_size(); |
500 // With no server suggestions, fall back to local Most Visited. | 508 // With no server suggestions, fall back to local Most Visited. |
501 if (num_tiles == 0) { | 509 if (num_tiles == 0) { |
502 InitiateTopSitesQuery(); | 510 InitiateTopSitesQuery(); |
503 return; | 511 return; |
504 } | 512 } |
505 if (num_sites_ < num_tiles) | 513 if (num_sites_ < num_tiles) |
506 num_tiles = num_sites_; | 514 num_tiles = num_sites_; |
507 | 515 |
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 | |
511 suggestions.push_back(make_scoped_ptr(new Suggestion( | 527 suggestions.push_back(make_scoped_ptr(new Suggestion( |
512 base::UTF8ToUTF16(suggestion.title()), suggestion.url(), | 528 base::UTF8ToUTF16(suggestion.title()), suggestion.url(), |
513 SUGGESTIONS_SERVICE, | 529 SUGGESTIONS_SERVICE, |
514 suggestion.providers_size() > 0 ? suggestion.providers(0) : -1))); | 530 suggestion.providers_size() > 0 ? suggestion.providers(0) : -1))); |
515 } | 531 } |
516 | 532 |
517 received_most_visited_sites_ = true; | 533 received_most_visited_sites_ = true; |
518 mv_source_ = SUGGESTIONS_SERVICE; | 534 mv_source_ = SUGGESTIONS_SERVICE; |
519 SaveNewNTPSuggestions(&suggestions); | 535 SaveNewNTPSuggestions(&suggestions); |
520 NotifyMostVisitedURLsObserver(); | 536 NotifyMostVisitedURLsObserver(); |
521 } | 537 } |
522 | 538 |
523 MostVisitedSites::SuggestionsVector | 539 MostVisitedSites::SuggestionsVector |
524 MostVisitedSites::CreateWhitelistEntryPointSuggestions( | 540 MostVisitedSites::CreateWhitelistEntryPointSuggestions( |
525 const MostVisitedSites::SuggestionsVector& personal_suggestions) { | 541 const MostVisitedSites::SuggestionsVector& personal_suggestions) { |
526 size_t num_personal_suggestions = personal_suggestions.size(); | 542 size_t num_personal_suggestions = personal_suggestions.size(); |
527 DCHECK_LE(num_personal_suggestions, static_cast<size_t>(num_sites_)); | 543 DCHECK_LE(num_personal_suggestions, static_cast<size_t>(num_sites_)); |
528 | 544 |
529 size_t num_whitelist_suggestions = num_sites_ - num_personal_suggestions; | 545 size_t num_whitelist_suggestions = num_sites_ - num_personal_suggestions; |
530 MostVisitedSites::SuggestionsVector whitelist_suggestions; | 546 MostVisitedSites::SuggestionsVector whitelist_suggestions; |
531 | 547 |
532 SupervisedUserService* supervised_user_service = | 548 SupervisedUserService* supervised_user_service = |
533 SupervisedUserServiceFactory::GetForProfile(profile_); | 549 SupervisedUserServiceFactory::GetForProfile(profile_); |
550 SupervisedUserURLFilter* url_filter = | |
551 supervised_user_service->GetURLFilterForUIThread(); | |
552 | |
553 std::set<std::string> personal_hosts; | |
554 for (const auto& suggestion : personal_suggestions) | |
555 personal_hosts.insert(suggestion->url.host()); | |
556 scoped_refptr<TopSites> top_sites(TopSitesFactory::GetForProfile(profile_)); | |
534 | 557 |
535 for (const auto& whitelist : supervised_user_service->whitelists()) { | 558 for (const auto& whitelist : supervised_user_service->whitelists()) { |
559 // Skip blacklisted sites. | |
560 if (top_sites && top_sites->IsBlacklisted(whitelist->entry_point())) | |
561 continue; | |
562 | |
563 // Skip suggestions already present. | |
564 if (personal_hosts.find(whitelist->entry_point().host()) != | |
565 personal_hosts.end()) | |
566 continue; | |
567 | |
568 // Skip whitelist entry points that are manually blocked. | |
569 if (url_filter->GetFilteringBehaviorForURL(whitelist->entry_point()) == | |
570 SupervisedUserURLFilter::FilteringBehavior::BLOCK) | |
571 continue; | |
572 | |
536 whitelist_suggestions.push_back(make_scoped_ptr(new Suggestion( | 573 whitelist_suggestions.push_back(make_scoped_ptr(new Suggestion( |
537 whitelist->title(), whitelist->entry_point(), WHITELIST))); | 574 whitelist->title(), whitelist->entry_point(), WHITELIST))); |
538 if (whitelist_suggestions.size() >= num_whitelist_suggestions) | 575 if (whitelist_suggestions.size() >= num_whitelist_suggestions) |
539 break; | 576 break; |
540 } | 577 } |
541 | 578 |
542 return whitelist_suggestions; | 579 return whitelist_suggestions; |
543 } | 580 } |
544 | 581 |
545 MostVisitedSites::SuggestionsVector | 582 MostVisitedSites::SuggestionsVector |
546 MostVisitedSites::CreatePopularSitesSuggestions( | 583 MostVisitedSites::CreatePopularSitesSuggestions( |
547 const MostVisitedSites::SuggestionsVector& personal_suggestions, | 584 const MostVisitedSites::SuggestionsVector& personal_suggestions, |
548 const MostVisitedSites::SuggestionsVector& whitelist_suggestions) { | 585 const MostVisitedSites::SuggestionsVector& whitelist_suggestions) { |
586 MostVisitedSites::SuggestionsVector popular_sites_suggestions; | |
Bernhard Bauer
2016/02/25 11:14:48
I think I'd move this back down and just return an
atanasova
2016/02/25 11:31:32
Done.
| |
587 // For child accounts popular sites suggestions will not be added. | |
588 if (profile_->IsChild()) { | |
Bernhard Bauer
2016/02/25 11:14:48
Key this off IsSupervised()? Most of the code here
Marc Treib
2016/02/25 11:23:13
Well, it's essentially a product decision. Do we w
atanasova
2016/02/25 11:31:32
I will ping Patrick about this, I though the agree
Bernhard Bauer
2016/02/25 11:43:29
As I mentioned on the email thread, on Android the
| |
589 return popular_sites_suggestions; | |
590 } | |
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; | |
557 | 599 |
558 if (num_popular_sites_suggestions > 0 && popular_sites_) { | 600 if (num_popular_sites_suggestions > 0 && popular_sites_) { |
559 std::set<std::string> personal_hosts; | 601 std::set<std::string> hosts; |
560 for (const auto& suggestion : personal_suggestions) | 602 for (const auto& suggestion : personal_suggestions) |
561 personal_hosts.insert(suggestion->url.host()); | 603 hosts.insert(suggestion->url.host()); |
604 for (const auto& suggestion : whitelist_suggestions) | |
605 hosts.insert(suggestion->url.host()); | |
562 scoped_refptr<TopSites> top_sites(TopSitesFactory::GetForProfile(profile_)); | 606 scoped_refptr<TopSites> top_sites(TopSitesFactory::GetForProfile(profile_)); |
563 for (const PopularSites::Site& popular_site : popular_sites_->sites()) { | 607 for (const PopularSites::Site& popular_site : popular_sites_->sites()) { |
564 // Skip blacklisted sites. | 608 // Skip blacklisted sites. |
565 if (top_sites && top_sites->IsBlacklisted(popular_site.url)) | 609 if (top_sites && top_sites->IsBlacklisted(popular_site.url)) |
566 continue; | 610 continue; |
567 std::string host = popular_site.url.host(); | 611 std::string host = popular_site.url.host(); |
568 // Skip suggestions already present in personal. | 612 // Skip suggestions already present in personal or whitelists. |
569 if (personal_hosts.find(host) != personal_hosts.end()) | 613 if (hosts.find(host) != hosts.end()) |
570 continue; | 614 continue; |
571 | 615 |
572 popular_sites_suggestions.push_back(make_scoped_ptr( | 616 popular_sites_suggestions.push_back(make_scoped_ptr( |
573 new Suggestion(popular_site.title, popular_site.url, POPULAR))); | 617 new Suggestion(popular_site.title, popular_site.url, POPULAR))); |
574 if (popular_sites_suggestions.size() >= num_popular_sites_suggestions) | 618 if (popular_sites_suggestions.size() >= num_popular_sites_suggestions) |
575 break; | 619 break; |
576 } | 620 } |
577 } | 621 } |
578 return popular_sites_suggestions; | 622 return popular_sites_suggestions; |
579 } | 623 } |
(...skipping 237 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
817 } | 861 } |
818 } | 862 } |
819 | 863 |
820 static jlong Init(JNIEnv* env, | 864 static jlong Init(JNIEnv* env, |
821 const JavaParamRef<jobject>& obj, | 865 const JavaParamRef<jobject>& obj, |
822 const JavaParamRef<jobject>& jprofile) { | 866 const JavaParamRef<jobject>& jprofile) { |
823 MostVisitedSites* most_visited_sites = | 867 MostVisitedSites* most_visited_sites = |
824 new MostVisitedSites(ProfileAndroid::FromProfileAndroid(jprofile)); | 868 new MostVisitedSites(ProfileAndroid::FromProfileAndroid(jprofile)); |
825 return reinterpret_cast<intptr_t>(most_visited_sites); | 869 return reinterpret_cast<intptr_t>(most_visited_sites); |
826 } | 870 } |
OLD | NEW |