| 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/ntp/most_visited_sites.h" | 5 #include "chrome/browser/android/ntp/most_visited_sites.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/callback.h" | 9 #include "base/callback.h" |
| 10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| 11 #include "base/metrics/field_trial.h" | 11 #include "base/metrics/field_trial.h" |
| 12 #include "base/metrics/histogram.h" | 12 #include "base/metrics/histogram.h" |
| 13 #include "base/metrics/sparse_histogram.h" | 13 #include "base/metrics/sparse_histogram.h" |
| 14 #include "base/strings/string_number_conversions.h" | 14 #include "base/strings/string_number_conversions.h" |
| 15 #include "base/strings/string_util.h" | 15 #include "base/strings/string_util.h" |
| 16 #include "base/strings/stringprintf.h" | 16 #include "base/strings/stringprintf.h" |
| 17 #include "base/strings/utf_string_conversions.h" | 17 #include "base/strings/utf_string_conversions.h" |
| 18 #include "base/time/time.h" | 18 #include "base/time/time.h" |
| 19 #include "chrome/browser/history/top_sites_factory.h" | 19 #include "chrome/browser/history/top_sites_factory.h" |
| 20 #include "chrome/browser/profiles/profile.h" | 20 #include "chrome/browser/profiles/profile.h" |
| 21 #include "chrome/browser/search/suggestions/suggestions_service_factory.h" | 21 #include "chrome/browser/search/suggestions/suggestions_service_factory.h" |
| 22 #include "chrome/browser/search_engines/template_url_service_factory.h" | 22 #include "chrome/browser/search_engines/template_url_service_factory.h" |
| 23 #include "chrome/browser/supervised_user/supervised_user_service.h" | 23 #include "chrome/browser/supervised_user/supervised_user_service.h" |
| 24 #include "chrome/browser/supervised_user/supervised_user_service_factory.h" | 24 #include "chrome/browser/supervised_user/supervised_user_service_factory.h" |
| 25 #include "chrome/browser/supervised_user/supervised_user_url_filter.h" | 25 #include "chrome/browser/supervised_user/supervised_user_url_filter.h" |
| 26 #include "chrome/browser/thumbnails/thumbnail_list_source.h" | 26 #include "chrome/browser/thumbnails/thumbnail_list_source.h" |
| 27 #include "chrome/common/chrome_switches.h" | |
| 28 #include "chrome/common/pref_names.h" | |
| 29 #include "components/history/core/browser/top_sites.h" | 27 #include "components/history/core/browser/top_sites.h" |
| 28 #include "components/ntp_tiles/pref_names.h" |
| 29 #include "components/ntp_tiles/switches.h" |
| 30 #include "components/pref_registry/pref_registry_syncable.h" | 30 #include "components/pref_registry/pref_registry_syncable.h" |
| 31 #include "components/prefs/pref_service.h" | 31 #include "components/prefs/pref_service.h" |
| 32 #include "components/variations/variations_associated_data.h" | 32 #include "components/variations/variations_associated_data.h" |
| 33 #include "content/public/browser/browser_thread.h" | 33 #include "content/public/browser/browser_thread.h" |
| 34 #include "content/public/browser/url_data_source.h" | 34 #include "content/public/browser/url_data_source.h" |
| 35 #include "third_party/skia/include/core/SkBitmap.h" | 35 #include "third_party/skia/include/core/SkBitmap.h" |
| 36 #include "ui/gfx/codec/jpeg_codec.h" | 36 #include "ui/gfx/codec/jpeg_codec.h" |
| 37 #include "url/gurl.h" | 37 #include "url/gurl.h" |
| 38 | 38 |
| 39 using content::BrowserThread; | 39 using content::BrowserThread; |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 99 if (counter) | 99 if (counter) |
| 100 counter->Add(position); | 100 counter->Add(position); |
| 101 } | 101 } |
| 102 | 102 |
| 103 bool ShouldShowPopularSites() { | 103 bool ShouldShowPopularSites() { |
| 104 // Note: It's important to query the field trial state first, to ensure that | 104 // Note: It's important to query the field trial state first, to ensure that |
| 105 // UMA reports the correct group. | 105 // UMA reports the correct group. |
| 106 const std::string group_name = | 106 const std::string group_name = |
| 107 base::FieldTrialList::FindFullName(kPopularSitesFieldTrialName); | 107 base::FieldTrialList::FindFullName(kPopularSitesFieldTrialName); |
| 108 base::CommandLine* cmd_line = base::CommandLine::ForCurrentProcess(); | 108 base::CommandLine* cmd_line = base::CommandLine::ForCurrentProcess(); |
| 109 if (cmd_line->HasSwitch(switches::kDisableNTPPopularSites)) | 109 if (cmd_line->HasSwitch(ntp_tiles::switches::kDisableNTPPopularSites)) |
| 110 return false; | 110 return false; |
| 111 if (cmd_line->HasSwitch(switches::kEnableNTPPopularSites)) | 111 if (cmd_line->HasSwitch(ntp_tiles::switches::kEnableNTPPopularSites)) |
| 112 return true; | 112 return true; |
| 113 return base::StartsWith(group_name, "Enabled", | 113 return base::StartsWith(group_name, "Enabled", |
| 114 base::CompareCase::INSENSITIVE_ASCII); | 114 base::CompareCase::INSENSITIVE_ASCII); |
| 115 } | 115 } |
| 116 | 116 |
| 117 std::string GetPopularSitesCountry() { | 117 std::string GetPopularSitesCountry() { |
| 118 return variations::GetVariationParamValue(kPopularSitesFieldTrialName, | 118 return variations::GetVariationParamValue(kPopularSitesFieldTrialName, |
| 119 "country"); | 119 "country"); |
| 120 } | 120 } |
| 121 | 121 |
| 122 std::string GetPopularSitesVersion() { | 122 std::string GetPopularSitesVersion() { |
| 123 return variations::GetVariationParamValue(kPopularSitesFieldTrialName, | 123 return variations::GetVariationParamValue(kPopularSitesFieldTrialName, |
| 124 "version"); | 124 "version"); |
| 125 } | 125 } |
| 126 | 126 |
| 127 // Determine whether we need any popular suggestions to fill up a grid of | 127 // Determine whether we need any popular suggestions to fill up a grid of |
| 128 // |num_tiles| tiles. | 128 // |num_tiles| tiles. |
| 129 bool NeedPopularSites(const PrefService* prefs, size_t num_tiles) { | 129 bool NeedPopularSites(const PrefService* prefs, size_t num_tiles) { |
| 130 const base::ListValue* source_list = | 130 const base::ListValue* source_list = |
| 131 prefs->GetList(prefs::kNTPSuggestionsIsPersonal); | 131 prefs->GetList(ntp_tiles::prefs::kNTPSuggestionsIsPersonal); |
| 132 // If there aren't enough previous suggestions to fill the grid, we need | 132 // If there aren't enough previous suggestions to fill the grid, we need |
| 133 // popular suggestions. | 133 // popular suggestions. |
| 134 if (source_list->GetSize() < num_tiles) | 134 if (source_list->GetSize() < num_tiles) |
| 135 return true; | 135 return true; |
| 136 // Otherwise, if any of the previous suggestions is not personal, then also | 136 // Otherwise, if any of the previous suggestions is not personal, then also |
| 137 // get popular suggestions. | 137 // get popular suggestions. |
| 138 for (size_t i = 0; i < num_tiles; ++i) { | 138 for (size_t i = 0; i < num_tiles; ++i) { |
| 139 bool is_personal = false; | 139 bool is_personal = false; |
| 140 if (source_list->GetBoolean(i, &is_personal) && !is_personal) | 140 if (source_list->GetBoolean(i, &is_personal) && !is_personal) |
| 141 return true; | 141 return true; |
| (...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 356 LogHistogramEvent(histogram, tile_type, NUM_TILE_TYPES); | 356 LogHistogramEvent(histogram, tile_type, NUM_TILE_TYPES); |
| 357 } | 357 } |
| 358 | 358 |
| 359 void MostVisitedSites::OnURLFilterChanged() { | 359 void MostVisitedSites::OnURLFilterChanged() { |
| 360 QueryMostVisitedURLs(); | 360 QueryMostVisitedURLs(); |
| 361 } | 361 } |
| 362 | 362 |
| 363 // static | 363 // static |
| 364 void MostVisitedSites::RegisterProfilePrefs( | 364 void MostVisitedSites::RegisterProfilePrefs( |
| 365 user_prefs::PrefRegistrySyncable* registry) { | 365 user_prefs::PrefRegistrySyncable* registry) { |
| 366 registry->RegisterListPref(prefs::kNTPSuggestionsURL); | 366 registry->RegisterListPref(ntp_tiles::prefs::kNTPSuggestionsURL); |
| 367 registry->RegisterListPref(prefs::kNTPSuggestionsIsPersonal); | 367 registry->RegisterListPref(ntp_tiles::prefs::kNTPSuggestionsIsPersonal); |
| 368 } | 368 } |
| 369 | 369 |
| 370 void MostVisitedSites::QueryMostVisitedURLs() { | 370 void MostVisitedSites::QueryMostVisitedURLs() { |
| 371 SuggestionsService* suggestions_service = | 371 SuggestionsService* suggestions_service = |
| 372 SuggestionsServiceFactory::GetForProfile(profile_); | 372 SuggestionsServiceFactory::GetForProfile(profile_); |
| 373 if (suggestions_service->FetchSuggestionsData()) { | 373 if (suggestions_service->FetchSuggestionsData()) { |
| 374 // A suggestions network request is on its way. We'll be called back via | 374 // A suggestions network request is on its way. We'll be called back via |
| 375 // OnSuggestionsProfileAvailable. | 375 // OnSuggestionsProfileAvailable. |
| 376 return; | 376 return; |
| 377 } | 377 } |
| (...skipping 273 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 651 InsertAllSuggestions(filled_so_far, new_popular_suggestions, | 651 InsertAllSuggestions(filled_so_far, new_popular_suggestions, |
| 652 popular_suggestions, &merged_suggestions); | 652 popular_suggestions, &merged_suggestions); |
| 653 return merged_suggestions; | 653 return merged_suggestions; |
| 654 } | 654 } |
| 655 | 655 |
| 656 void MostVisitedSites::GetPreviousNTPSites( | 656 void MostVisitedSites::GetPreviousNTPSites( |
| 657 size_t num_tiles, | 657 size_t num_tiles, |
| 658 std::vector<std::string>* old_sites_url, | 658 std::vector<std::string>* old_sites_url, |
| 659 std::vector<bool>* old_sites_is_personal) const { | 659 std::vector<bool>* old_sites_is_personal) const { |
| 660 const PrefService* prefs = profile_->GetPrefs(); | 660 const PrefService* prefs = profile_->GetPrefs(); |
| 661 const base::ListValue* url_list = prefs->GetList(prefs::kNTPSuggestionsURL); | 661 const base::ListValue* url_list = prefs->GetList( |
| 662 ntp_tiles::prefs::kNTPSuggestionsURL); |
| 662 const base::ListValue* source_list = | 663 const base::ListValue* source_list = |
| 663 prefs->GetList(prefs::kNTPSuggestionsIsPersonal); | 664 prefs->GetList(ntp_tiles::prefs::kNTPSuggestionsIsPersonal); |
| 664 DCHECK_EQ(url_list->GetSize(), source_list->GetSize()); | 665 DCHECK_EQ(url_list->GetSize(), source_list->GetSize()); |
| 665 if (url_list->GetSize() < num_tiles) | 666 if (url_list->GetSize() < num_tiles) |
| 666 num_tiles = url_list->GetSize(); | 667 num_tiles = url_list->GetSize(); |
| 667 if (num_tiles == 0) { | 668 if (num_tiles == 0) { |
| 668 // No fallback required as Personal suggestions take precedence anyway. | 669 // No fallback required as Personal suggestions take precedence anyway. |
| 669 return; | 670 return; |
| 670 } | 671 } |
| 671 old_sites_url->reserve(num_tiles); | 672 old_sites_url->reserve(num_tiles); |
| 672 old_sites_is_personal->reserve(num_tiles); | 673 old_sites_is_personal->reserve(num_tiles); |
| 673 for (size_t i = 0; i < num_tiles; ++i) { | 674 for (size_t i = 0; i < num_tiles; ++i) { |
| 674 std::string url_string; | 675 std::string url_string; |
| 675 bool success = url_list->GetString(i, &url_string); | 676 bool success = url_list->GetString(i, &url_string); |
| 676 DCHECK(success); | 677 DCHECK(success); |
| 677 old_sites_url->push_back(url_string); | 678 old_sites_url->push_back(url_string); |
| 678 bool is_personal; | 679 bool is_personal; |
| 679 success = source_list->GetBoolean(i, &is_personal); | 680 success = source_list->GetBoolean(i, &is_personal); |
| 680 DCHECK(success); | 681 DCHECK(success); |
| 681 old_sites_is_personal->push_back(is_personal); | 682 old_sites_is_personal->push_back(is_personal); |
| 682 } | 683 } |
| 683 } | 684 } |
| 684 | 685 |
| 685 void MostVisitedSites::SaveCurrentNTPSites() { | 686 void MostVisitedSites::SaveCurrentNTPSites() { |
| 686 base::ListValue url_list; | 687 base::ListValue url_list; |
| 687 base::ListValue source_list; | 688 base::ListValue source_list; |
| 688 for (const auto& suggestion : current_suggestions_) { | 689 for (const auto& suggestion : current_suggestions_) { |
| 689 url_list.AppendString(suggestion.url.spec()); | 690 url_list.AppendString(suggestion.url.spec()); |
| 690 source_list.AppendBoolean(suggestion.source != MostVisitedSites::POPULAR); | 691 source_list.AppendBoolean(suggestion.source != MostVisitedSites::POPULAR); |
| 691 } | 692 } |
| 692 PrefService* prefs = profile_->GetPrefs(); | 693 PrefService* prefs = profile_->GetPrefs(); |
| 693 prefs->Set(prefs::kNTPSuggestionsIsPersonal, source_list); | 694 prefs->Set(ntp_tiles::prefs::kNTPSuggestionsIsPersonal, source_list); |
| 694 prefs->Set(prefs::kNTPSuggestionsURL, url_list); | 695 prefs->Set(ntp_tiles::prefs::kNTPSuggestionsURL, url_list); |
| 695 } | 696 } |
| 696 | 697 |
| 697 // static | 698 // static |
| 698 std::vector<size_t> MostVisitedSites::InsertMatchingSuggestions( | 699 std::vector<size_t> MostVisitedSites::InsertMatchingSuggestions( |
| 699 MostVisitedSites::SuggestionsPtrVector* src_suggestions, | 700 MostVisitedSites::SuggestionsPtrVector* src_suggestions, |
| 700 MostVisitedSites::SuggestionsPtrVector* dst_suggestions, | 701 MostVisitedSites::SuggestionsPtrVector* dst_suggestions, |
| 701 const std::vector<std::string>& match_urls, | 702 const std::vector<std::string>& match_urls, |
| 702 const std::vector<std::string>& match_hosts) { | 703 const std::vector<std::string>& match_hosts) { |
| 703 std::vector<size_t> unmatched_suggestions; | 704 std::vector<size_t> unmatched_suggestions; |
| 704 size_t num_src_suggestions = src_suggestions->size(); | 705 size_t num_src_suggestions = src_suggestions->size(); |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 789 | 790 |
| 790 void MostVisitedSites::TopSitesLoaded(TopSites* top_sites) {} | 791 void MostVisitedSites::TopSitesLoaded(TopSites* top_sites) {} |
| 791 | 792 |
| 792 void MostVisitedSites::TopSitesChanged(TopSites* top_sites, | 793 void MostVisitedSites::TopSitesChanged(TopSites* top_sites, |
| 793 ChangeReason change_reason) { | 794 ChangeReason change_reason) { |
| 794 if (mv_source_ == TOP_SITES) { | 795 if (mv_source_ == TOP_SITES) { |
| 795 // The displayed suggestions are invalidated. | 796 // The displayed suggestions are invalidated. |
| 796 InitiateTopSitesQuery(); | 797 InitiateTopSitesQuery(); |
| 797 } | 798 } |
| 798 } | 799 } |
| OLD | NEW |