Chromium Code Reviews| 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 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 157 if (source_list->GetBoolean(i, &is_personal) && !is_personal) | 157 if (source_list->GetBoolean(i, &is_personal) && !is_personal) |
| 158 return true; | 158 return true; |
| 159 } | 159 } |
| 160 // The whole grid is already filled with personal suggestions, no point in | 160 // The whole grid is already filled with personal suggestions, no point in |
| 161 // bothering with popular ones. | 161 // bothering with popular ones. |
| 162 return false; | 162 return false; |
| 163 } | 163 } |
| 164 | 164 |
| 165 } // namespace | 165 } // namespace |
| 166 | 166 |
| 167 MostVisitedSites::Suggestion::Suggestion(const base::string16& title, | 167 namespace { |
| 168 const std::string& url, | 168 class SuggestionBuilder { |
|
Marc Treib
2016/03/16 11:06:03
nit: empty line after "namespace {" please.
atanasova
2016/03/16 17:58:17
Done.
| |
| 169 MostVisitedSource source) | 169 public: |
| 170 : title(title), url(url), source(source), provider_index(-1) {} | 170 SuggestionBuilder(const base::string16& title, |
| 171 const GURL& url, | |
| 172 MostVisitedSites::MostVisitedSource source); | |
| 173 ~SuggestionBuilder(); | |
| 171 | 174 |
| 172 MostVisitedSites::Suggestion::Suggestion(const base::string16& title, | 175 void SetWhitelistIconPath(const base::FilePath& whitelist_icon_path); |
| 173 const GURL& url, | |
| 174 MostVisitedSource source) | |
| 175 : title(title), url(url), source(source), provider_index(-1) {} | |
| 176 | 176 |
| 177 MostVisitedSites::Suggestion::Suggestion(const base::string16& title, | 177 void SetProviderIndex(int provider_index); |
| 178 const std::string& url, | 178 |
| 179 MostVisitedSource source, | 179 scoped_ptr<MostVisitedSites::Suggestion> Build(); |
| 180 int provider_index) | 180 |
| 181 : title(title), url(url), source(source), provider_index(provider_index) { | 181 private: |
| 182 DCHECK_EQ(MostVisitedSites::SUGGESTIONS_SERVICE, source); | 182 base::string16 title_; |
| 183 GURL url_; | |
|
Marc Treib
2016/03/16 11:06:03
Maybe this should store references to the title an
| |
| 184 MostVisitedSites::MostVisitedSource source_; | |
| 185 base::FilePath whitelist_icon_path_; | |
| 186 int provider_index_; | |
| 187 }; | |
| 188 | |
| 189 SuggestionBuilder::SuggestionBuilder(const base::string16& title, | |
| 190 const GURL& url, | |
| 191 MostVisitedSites::MostVisitedSource source) | |
| 192 : title_(title), url_(url), source_(source), provider_index_(-1) {} | |
| 193 | |
| 194 SuggestionBuilder::~SuggestionBuilder() {} | |
| 195 | |
| 196 void SuggestionBuilder::SetWhitelistIconPath( | |
| 197 const base::FilePath& whitelist_icon_path) { | |
| 198 whitelist_icon_path_ = whitelist_icon_path; | |
| 183 } | 199 } |
| 184 | 200 |
| 201 void SuggestionBuilder::SetProviderIndex(int provider_index) { | |
| 202 provider_index_ = provider_index; | |
| 203 } | |
| 204 | |
| 205 scoped_ptr<MostVisitedSites::Suggestion> SuggestionBuilder::Build() { | |
| 206 return make_scoped_ptr(new MostVisitedSites::Suggestion( | |
| 207 title_, url_, source_, whitelist_icon_path_, provider_index_)); | |
| 208 } | |
| 209 | |
| 210 } // namespace | |
| 211 | |
| 212 MostVisitedSites::Suggestion::Suggestion( | |
| 213 const base::string16& title, | |
| 214 const GURL& url, | |
| 215 MostVisitedSource source, | |
| 216 const base::FilePath& whitelist_icon_path, | |
| 217 int provider_index) | |
| 218 : title(title), | |
| 219 url(url), | |
| 220 source(source), | |
| 221 whitelist_icon_path(whitelist_icon_path), | |
| 222 provider_index(provider_index) {} | |
| 223 | |
| 185 MostVisitedSites::Suggestion::~Suggestion() {} | 224 MostVisitedSites::Suggestion::~Suggestion() {} |
| 186 | 225 |
| 187 std::string MostVisitedSites::Suggestion::GetSourceHistogramName() const { | 226 std::string MostVisitedSites::Suggestion::GetSourceHistogramName() const { |
| 188 switch (source) { | 227 switch (source) { |
| 189 case MostVisitedSites::TOP_SITES: | 228 case MostVisitedSites::TOP_SITES: |
| 190 return kHistogramClientName; | 229 return kHistogramClientName; |
| 191 case MostVisitedSites::POPULAR: | 230 case MostVisitedSites::POPULAR: |
| 192 return kHistogramPopularName; | 231 return kHistogramPopularName; |
| 193 case MostVisitedSites::WHITELIST: | 232 case MostVisitedSites::WHITELIST: |
| 194 return kHistogramWhitelistName; | 233 return kHistogramWhitelistName; |
| (...skipping 291 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 486 const history::MostVisitedURL& visited = visited_list[i]; | 525 const history::MostVisitedURL& visited = visited_list[i]; |
| 487 if (visited.url.is_empty()) { | 526 if (visited.url.is_empty()) { |
| 488 num_tiles = i; | 527 num_tiles = i; |
| 489 break; // This is the signal that there are no more real visited sites. | 528 break; // This is the signal that there are no more real visited sites. |
| 490 } | 529 } |
| 491 if (url_filter->GetFilteringBehaviorForURL(visited.url) == | 530 if (url_filter->GetFilteringBehaviorForURL(visited.url) == |
| 492 SupervisedUserURLFilter::FilteringBehavior::BLOCK) { | 531 SupervisedUserURLFilter::FilteringBehavior::BLOCK) { |
| 493 continue; | 532 continue; |
| 494 } | 533 } |
| 495 | 534 |
| 496 suggestions.push_back(make_scoped_ptr( | 535 SuggestionBuilder* builder = |
| 497 new Suggestion(visited.title, visited.url.spec(), TOP_SITES))); | 536 new SuggestionBuilder(visited.title, visited.url, TOP_SITES); |
|
Marc Treib
2016/03/16 11:06:03
Memleak! Just
SuggestionBuilder builder(...);
plea
atanasova
2016/03/16 17:58:17
Done.
| |
| 537 | |
| 538 suggestions.push_back(builder->Build()); | |
| 498 } | 539 } |
| 499 | 540 |
| 500 received_most_visited_sites_ = true; | 541 received_most_visited_sites_ = true; |
| 501 mv_source_ = TOP_SITES; | 542 mv_source_ = TOP_SITES; |
| 502 SaveNewNTPSuggestions(&suggestions); | 543 SaveNewNTPSuggestions(&suggestions); |
| 503 NotifyMostVisitedURLsObserver(); | 544 NotifyMostVisitedURLsObserver(); |
| 504 } | 545 } |
| 505 | 546 |
| 506 void MostVisitedSites::OnSuggestionsProfileAvailable( | 547 void MostVisitedSites::OnSuggestionsProfileAvailable( |
| 507 const SuggestionsProfile& suggestions_profile) { | 548 const SuggestionsProfile& suggestions_profile) { |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 518 SupervisedUserServiceFactory::GetForProfile(profile_) | 559 SupervisedUserServiceFactory::GetForProfile(profile_) |
| 519 ->GetURLFilterForUIThread(); | 560 ->GetURLFilterForUIThread(); |
| 520 MostVisitedSites::SuggestionsVector suggestions; | 561 MostVisitedSites::SuggestionsVector suggestions; |
| 521 for (int i = 0; i < num_tiles; ++i) { | 562 for (int i = 0; i < num_tiles; ++i) { |
| 522 const ChromeSuggestion& suggestion = suggestions_profile.suggestions(i); | 563 const ChromeSuggestion& suggestion = suggestions_profile.suggestions(i); |
| 523 if (url_filter->GetFilteringBehaviorForURL(GURL(suggestion.url())) == | 564 if (url_filter->GetFilteringBehaviorForURL(GURL(suggestion.url())) == |
| 524 SupervisedUserURLFilter::FilteringBehavior::BLOCK) { | 565 SupervisedUserURLFilter::FilteringBehavior::BLOCK) { |
| 525 continue; | 566 continue; |
| 526 } | 567 } |
| 527 | 568 |
| 528 suggestions.push_back(make_scoped_ptr(new Suggestion( | 569 const base::string16& title = base::UTF8ToUTF16(suggestion.title()); |
| 529 base::UTF8ToUTF16(suggestion.title()), suggestion.url(), | 570 SuggestionBuilder* builder = new SuggestionBuilder( |
| 530 SUGGESTIONS_SERVICE, | 571 title, GURL(suggestion.url()), SUGGESTIONS_SERVICE); |
| 531 suggestion.providers_size() > 0 ? suggestion.providers(0) : -1))); | 572 builder->SetProviderIndex( |
| 573 suggestion.providers_size() > 0 ? suggestion.providers(0) : -1); | |
| 574 | |
| 575 suggestions.push_back(builder->Build()); | |
| 532 } | 576 } |
| 533 | 577 |
| 534 received_most_visited_sites_ = true; | 578 received_most_visited_sites_ = true; |
| 535 mv_source_ = SUGGESTIONS_SERVICE; | 579 mv_source_ = SUGGESTIONS_SERVICE; |
| 536 SaveNewNTPSuggestions(&suggestions); | 580 SaveNewNTPSuggestions(&suggestions); |
| 537 NotifyMostVisitedURLsObserver(); | 581 NotifyMostVisitedURLsObserver(); |
| 538 } | 582 } |
| 539 | 583 |
| 540 MostVisitedSites::SuggestionsVector | 584 MostVisitedSites::SuggestionsVector |
| 541 MostVisitedSites::CreateWhitelistEntryPointSuggestions( | 585 MostVisitedSites::CreateWhitelistEntryPointSuggestions( |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 565 if (personal_hosts.find(whitelist->entry_point().host()) != | 609 if (personal_hosts.find(whitelist->entry_point().host()) != |
| 566 personal_hosts.end()) | 610 personal_hosts.end()) |
| 567 continue; | 611 continue; |
| 568 | 612 |
| 569 // Skip whitelist entry points that are manually blocked. | 613 // Skip whitelist entry points that are manually blocked. |
| 570 if (url_filter->GetFilteringBehaviorForURL(whitelist->entry_point()) == | 614 if (url_filter->GetFilteringBehaviorForURL(whitelist->entry_point()) == |
| 571 SupervisedUserURLFilter::FilteringBehavior::BLOCK) { | 615 SupervisedUserURLFilter::FilteringBehavior::BLOCK) { |
| 572 continue; | 616 continue; |
| 573 } | 617 } |
| 574 | 618 |
| 575 whitelist_suggestions.push_back(make_scoped_ptr(new Suggestion( | 619 SuggestionBuilder* builder = new SuggestionBuilder( |
| 576 whitelist->title(), whitelist->entry_point(), WHITELIST))); | 620 whitelist->title(), whitelist->entry_point(), WHITELIST); |
| 621 builder->SetWhitelistIconPath(whitelist->large_icon_path()); | |
| 622 | |
| 623 whitelist_suggestions.push_back(builder->Build()); | |
| 577 if (whitelist_suggestions.size() >= num_whitelist_suggestions) | 624 if (whitelist_suggestions.size() >= num_whitelist_suggestions) |
| 578 break; | 625 break; |
| 579 } | 626 } |
| 580 | 627 |
| 581 return whitelist_suggestions; | 628 return whitelist_suggestions; |
| 582 } | 629 } |
| 583 | 630 |
| 584 MostVisitedSites::SuggestionsVector | 631 MostVisitedSites::SuggestionsVector |
| 585 MostVisitedSites::CreatePopularSitesSuggestions( | 632 MostVisitedSites::CreatePopularSitesSuggestions( |
| 586 const MostVisitedSites::SuggestionsVector& personal_suggestions, | 633 const MostVisitedSites::SuggestionsVector& personal_suggestions, |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 607 scoped_refptr<TopSites> top_sites(TopSitesFactory::GetForProfile(profile_)); | 654 scoped_refptr<TopSites> top_sites(TopSitesFactory::GetForProfile(profile_)); |
| 608 for (const PopularSites::Site& popular_site : popular_sites_->sites()) { | 655 for (const PopularSites::Site& popular_site : popular_sites_->sites()) { |
| 609 // Skip blacklisted sites. | 656 // Skip blacklisted sites. |
| 610 if (top_sites && top_sites->IsBlacklisted(popular_site.url)) | 657 if (top_sites && top_sites->IsBlacklisted(popular_site.url)) |
| 611 continue; | 658 continue; |
| 612 std::string host = popular_site.url.host(); | 659 std::string host = popular_site.url.host(); |
| 613 // Skip suggestions already present in personal or whitelists. | 660 // Skip suggestions already present in personal or whitelists. |
| 614 if (hosts.find(host) != hosts.end()) | 661 if (hosts.find(host) != hosts.end()) |
| 615 continue; | 662 continue; |
| 616 | 663 |
| 617 popular_sites_suggestions.push_back(make_scoped_ptr( | 664 SuggestionBuilder* builder = new SuggestionBuilder( |
| 618 new Suggestion(popular_site.title, popular_site.url, POPULAR))); | 665 popular_site.title, GURL(popular_site.url), POPULAR); |
| 666 | |
| 667 popular_sites_suggestions.push_back(builder->Build()); | |
| 619 if (popular_sites_suggestions.size() >= num_popular_sites_suggestions) | 668 if (popular_sites_suggestions.size() >= num_popular_sites_suggestions) |
| 620 break; | 669 break; |
| 621 } | 670 } |
| 622 } | 671 } |
| 623 return popular_sites_suggestions; | 672 return popular_sites_suggestions; |
| 624 } | 673 } |
| 625 | 674 |
| 626 void MostVisitedSites::SaveNewNTPSuggestions( | 675 void MostVisitedSites::SaveNewNTPSuggestions( |
| 627 MostVisitedSites::SuggestionsVector* personal_suggestions) { | 676 MostVisitedSites::SuggestionsVector* personal_suggestions) { |
| 628 MostVisitedSites::SuggestionsVector whitelist_suggestions = | 677 MostVisitedSites::SuggestionsVector whitelist_suggestions = |
| (...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 795 void MostVisitedSites::NotifyMostVisitedURLsObserver() { | 844 void MostVisitedSites::NotifyMostVisitedURLsObserver() { |
| 796 size_t num_suggestions = current_suggestions_.size(); | 845 size_t num_suggestions = current_suggestions_.size(); |
| 797 if (received_most_visited_sites_ && received_popular_sites_ && | 846 if (received_most_visited_sites_ && received_popular_sites_ && |
| 798 !recorded_uma_) { | 847 !recorded_uma_) { |
| 799 RecordImpressionUMAMetrics(); | 848 RecordImpressionUMAMetrics(); |
| 800 UMA_HISTOGRAM_SPARSE_SLOWLY("NewTabPage.NumberOfTiles", num_suggestions); | 849 UMA_HISTOGRAM_SPARSE_SLOWLY("NewTabPage.NumberOfTiles", num_suggestions); |
| 801 recorded_uma_ = true; | 850 recorded_uma_ = true; |
| 802 } | 851 } |
| 803 std::vector<base::string16> titles; | 852 std::vector<base::string16> titles; |
| 804 std::vector<std::string> urls; | 853 std::vector<std::string> urls; |
| 854 std::vector<std::string> whitelist_icon_paths; | |
| 805 titles.reserve(num_suggestions); | 855 titles.reserve(num_suggestions); |
| 806 urls.reserve(num_suggestions); | 856 urls.reserve(num_suggestions); |
| 807 for (const auto& suggestion : current_suggestions_) { | 857 for (const auto& suggestion : current_suggestions_) { |
| 808 titles.push_back(suggestion->title); | 858 titles.push_back(suggestion->title); |
| 809 urls.push_back(suggestion->url.spec()); | 859 urls.push_back(suggestion->url.spec()); |
| 860 whitelist_icon_paths.push_back(suggestion->whitelist_icon_path.value()); | |
| 810 } | 861 } |
| 811 JNIEnv* env = AttachCurrentThread(); | 862 JNIEnv* env = AttachCurrentThread(); |
| 812 DCHECK_EQ(titles.size(), urls.size()); | 863 DCHECK_EQ(titles.size(), urls.size()); |
| 813 Java_MostVisitedURLsObserver_onMostVisitedURLsAvailable( | 864 Java_MostVisitedURLsObserver_onMostVisitedURLsAvailable( |
| 814 env, observer_.obj(), ToJavaArrayOfStrings(env, titles).obj(), | 865 env, observer_.obj(), ToJavaArrayOfStrings(env, titles).obj(), |
| 815 ToJavaArrayOfStrings(env, urls).obj()); | 866 ToJavaArrayOfStrings(env, urls).obj(), |
| 867 ToJavaArrayOfStrings(env, whitelist_icon_paths).obj()); | |
| 816 } | 868 } |
| 817 | 869 |
| 818 void MostVisitedSites::OnPopularSitesAvailable(bool success) { | 870 void MostVisitedSites::OnPopularSitesAvailable(bool success) { |
| 819 received_popular_sites_ = true; | 871 received_popular_sites_ = true; |
| 820 | 872 |
| 821 if (!success) { | 873 if (!success) { |
| 822 LOG(WARNING) << "Download of popular sites failed"; | 874 LOG(WARNING) << "Download of popular sites failed"; |
| 823 return; | 875 return; |
| 824 } | 876 } |
| 825 | 877 |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 862 } | 914 } |
| 863 } | 915 } |
| 864 | 916 |
| 865 static jlong Init(JNIEnv* env, | 917 static jlong Init(JNIEnv* env, |
| 866 const JavaParamRef<jobject>& obj, | 918 const JavaParamRef<jobject>& obj, |
| 867 const JavaParamRef<jobject>& jprofile) { | 919 const JavaParamRef<jobject>& jprofile) { |
| 868 MostVisitedSites* most_visited_sites = | 920 MostVisitedSites* most_visited_sites = |
| 869 new MostVisitedSites(ProfileAndroid::FromProfileAndroid(jprofile)); | 921 new MostVisitedSites(ProfileAndroid::FromProfileAndroid(jprofile)); |
| 870 return reinterpret_cast<intptr_t>(most_visited_sites); | 922 return reinterpret_cast<intptr_t>(most_visited_sites); |
| 871 } | 923 } |
| OLD | NEW |