| 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" | |
| 20 #include "chrome/browser/profiles/profile.h" | |
| 21 #include "chrome/browser/search/suggestions/suggestions_service_factory.h" | |
| 22 #include "chrome/browser/search_engines/template_url_service_factory.h" | |
| 23 #include "chrome/browser/supervised_user/supervised_user_service.h" | 19 #include "chrome/browser/supervised_user/supervised_user_service.h" |
| 24 #include "chrome/browser/supervised_user/supervised_user_service_factory.h" | 20 #include "chrome/browser/supervised_user/supervised_user_service_factory.h" |
| 25 #include "chrome/browser/supervised_user/supervised_user_url_filter.h" | 21 #include "chrome/browser/supervised_user/supervised_user_url_filter.h" |
| 26 #include "chrome/browser/thumbnails/thumbnail_list_source.h" | |
| 27 #include "components/history/core/browser/top_sites.h" | 22 #include "components/history/core/browser/top_sites.h" |
| 28 #include "components/ntp_tiles/pref_names.h" | 23 #include "components/ntp_tiles/pref_names.h" |
| 29 #include "components/ntp_tiles/switches.h" | 24 #include "components/ntp_tiles/switches.h" |
| 30 #include "components/pref_registry/pref_registry_syncable.h" | 25 #include "components/pref_registry/pref_registry_syncable.h" |
| 31 #include "components/prefs/pref_service.h" | 26 #include "components/prefs/pref_service.h" |
| 32 #include "components/variations/variations_associated_data.h" | 27 #include "components/variations/variations_associated_data.h" |
| 33 #include "content/public/browser/browser_thread.h" | 28 #include "content/public/browser/browser_thread.h" |
| 34 #include "content/public/browser/url_data_source.h" | |
| 35 #include "third_party/skia/include/core/SkBitmap.h" | 29 #include "third_party/skia/include/core/SkBitmap.h" |
| 36 #include "ui/gfx/codec/jpeg_codec.h" | 30 #include "ui/gfx/codec/jpeg_codec.h" |
| 37 #include "url/gurl.h" | 31 #include "url/gurl.h" |
| 38 | 32 |
| 39 using content::BrowserThread; | 33 using content::BrowserThread; |
| 40 using history::TopSites; | 34 using history::TopSites; |
| 41 using suggestions::ChromeSuggestion; | 35 using suggestions::ChromeSuggestion; |
| 42 using suggestions::SuggestionsProfile; | 36 using suggestions::SuggestionsProfile; |
| 43 using suggestions::SuggestionsServiceFactory; | 37 using suggestions::SuggestionsService; |
| 44 | 38 |
| 45 namespace { | 39 namespace { |
| 46 | 40 |
| 47 // Identifiers for the various tile sources. | 41 // Identifiers for the various tile sources. |
| 48 const char kHistogramClientName[] = "client"; | 42 const char kHistogramClientName[] = "client"; |
| 49 const char kHistogramServerName[] = "server"; | 43 const char kHistogramServerName[] = "server"; |
| 50 const char kHistogramServerFormat[] = "server%d"; | 44 const char kHistogramServerFormat[] = "server%d"; |
| 51 const char kHistogramPopularName[] = "popular"; | 45 const char kHistogramPopularName[] = "popular"; |
| 52 const char kHistogramWhitelistName[] = "whitelist"; | 46 const char kHistogramWhitelistName[] = "whitelist"; |
| 53 | 47 |
| (...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 171 | 165 |
| 172 MostVisitedSites::Suggestion::Suggestion() : provider_index(-1) {} | 166 MostVisitedSites::Suggestion::Suggestion() : provider_index(-1) {} |
| 173 | 167 |
| 174 MostVisitedSites::Suggestion::~Suggestion() {} | 168 MostVisitedSites::Suggestion::~Suggestion() {} |
| 175 | 169 |
| 176 MostVisitedSites::Suggestion::Suggestion(Suggestion&&) = default; | 170 MostVisitedSites::Suggestion::Suggestion(Suggestion&&) = default; |
| 177 MostVisitedSites::Suggestion& | 171 MostVisitedSites::Suggestion& |
| 178 MostVisitedSites::Suggestion::operator=(Suggestion&&) = default; | 172 MostVisitedSites::Suggestion::operator=(Suggestion&&) = default; |
| 179 | 173 |
| 180 MostVisitedSites::MostVisitedSites( | 174 MostVisitedSites::MostVisitedSites( |
| 181 Profile* profile, | 175 PrefService* prefs, |
| 182 variations::VariationsService* variations_service) | 176 const TemplateURLService* template_url_service, |
| 183 : profile_(profile), variations_service_(variations_service), | 177 variations::VariationsService* variations_service, |
| 184 top_sites_(TopSitesFactory::GetForProfile(profile)), | 178 net::URLRequestContextGetter* download_context, |
| 185 suggestions_service_(SuggestionsServiceFactory::GetForProfile(profile_)), | 179 scoped_refptr<history::TopSites> top_sites, |
| 186 observer_(nullptr), num_sites_(0), | 180 SuggestionsService* suggestions, |
| 187 received_most_visited_sites_(false), received_popular_sites_(false), | 181 bool is_child_profile, |
| 188 recorded_uma_(false), scoped_observer_(this), | 182 Profile* profile) |
| 189 mv_source_(SUGGESTIONS_SERVICE), weak_ptr_factory_(this) { | 183 : profile_(profile), prefs_(prefs), |
| 190 // Register the thumbnails debugging page. | 184 template_url_service_(template_url_service), |
| 191 content::URLDataSource::Add(profile_, new ThumbnailListSource(profile_)); | 185 variations_service_(variations_service), |
| 192 | 186 download_context_(download_context), top_sites_(top_sites), |
| 187 suggestions_service_(suggestions), is_child_profile_(is_child_profile), |
| 188 observer_(nullptr), num_sites_(0), received_most_visited_sites_(false), |
| 189 received_popular_sites_(false), recorded_uma_(false), |
| 190 scoped_observer_(this), mv_source_(SUGGESTIONS_SERVICE), |
| 191 weak_ptr_factory_(this) { |
| 193 SupervisedUserService* supervised_user_service = | 192 SupervisedUserService* supervised_user_service = |
| 194 SupervisedUserServiceFactory::GetForProfile(profile_); | 193 SupervisedUserServiceFactory::GetForProfile(profile_); |
| 195 supervised_user_service->AddObserver(this); | 194 supervised_user_service->AddObserver(this); |
| 196 } | 195 } |
| 197 | 196 |
| 198 MostVisitedSites::~MostVisitedSites() { | 197 MostVisitedSites::~MostVisitedSites() { |
| 199 SupervisedUserService* supervised_user_service = | 198 SupervisedUserService* supervised_user_service = |
| 200 SupervisedUserServiceFactory::GetForProfile(profile_); | 199 SupervisedUserServiceFactory::GetForProfile(profile_); |
| 201 supervised_user_service->RemoveObserver(this); | 200 supervised_user_service->RemoveObserver(this); |
| 202 } | 201 } |
| 203 | 202 |
| 204 void MostVisitedSites::SetMostVisitedURLsObserver( | 203 void MostVisitedSites::SetMostVisitedURLsObserver( |
| 205 MostVisitedSites::Observer* observer, int num_sites) { | 204 MostVisitedSites::Observer* observer, int num_sites) { |
| 206 DCHECK(observer); | 205 DCHECK(observer); |
| 207 observer_ = observer; | 206 observer_ = observer; |
| 208 num_sites_ = num_sites; | 207 num_sites_ = num_sites; |
| 209 | 208 |
| 210 if (ShouldShowPopularSites() && | 209 if (ShouldShowPopularSites() && |
| 211 NeedPopularSites(profile_->GetPrefs(), num_sites_)) { | 210 NeedPopularSites(prefs_, num_sites_)) { |
| 212 popular_sites_.reset(new PopularSites( | 211 popular_sites_.reset(new PopularSites( |
| 213 profile_->GetPrefs(), | 212 prefs_, |
| 214 TemplateURLServiceFactory::GetForProfile(profile_), | 213 template_url_service_, |
| 215 variations_service_, | 214 variations_service_, |
| 216 profile_->GetRequestContext(), | 215 download_context_, |
| 217 GetPopularSitesCountry(), | 216 GetPopularSitesCountry(), |
| 218 GetPopularSitesVersion(), | 217 GetPopularSitesVersion(), |
| 219 false, | 218 false, |
| 220 base::Bind(&MostVisitedSites::OnPopularSitesAvailable, | 219 base::Bind(&MostVisitedSites::OnPopularSitesAvailable, |
| 221 base::Unretained(this)))); | 220 base::Unretained(this)))); |
| 222 } else { | 221 } else { |
| 223 received_popular_sites_ = true; | 222 received_popular_sites_ = true; |
| 224 } | 223 } |
| 225 | 224 |
| 226 if (top_sites_) { | 225 if (top_sites_) { |
| (...skipping 285 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 512 } | 511 } |
| 513 | 512 |
| 514 return whitelist_suggestions; | 513 return whitelist_suggestions; |
| 515 } | 514 } |
| 516 | 515 |
| 517 MostVisitedSites::SuggestionsPtrVector | 516 MostVisitedSites::SuggestionsPtrVector |
| 518 MostVisitedSites::CreatePopularSitesSuggestions( | 517 MostVisitedSites::CreatePopularSitesSuggestions( |
| 519 const MostVisitedSites::SuggestionsPtrVector& personal_suggestions, | 518 const MostVisitedSites::SuggestionsPtrVector& personal_suggestions, |
| 520 const MostVisitedSites::SuggestionsPtrVector& whitelist_suggestions) { | 519 const MostVisitedSites::SuggestionsPtrVector& whitelist_suggestions) { |
| 521 // For child accounts popular sites suggestions will not be added. | 520 // For child accounts popular sites suggestions will not be added. |
| 522 if (profile_->IsChild()) | 521 if (is_child_profile_) |
| 523 return MostVisitedSites::SuggestionsPtrVector(); | 522 return MostVisitedSites::SuggestionsPtrVector(); |
| 524 | 523 |
| 525 size_t num_suggestions = | 524 size_t num_suggestions = |
| 526 personal_suggestions.size() + whitelist_suggestions.size(); | 525 personal_suggestions.size() + whitelist_suggestions.size(); |
| 527 DCHECK_LE(num_suggestions, static_cast<size_t>(num_sites_)); | 526 DCHECK_LE(num_suggestions, static_cast<size_t>(num_sites_)); |
| 528 | 527 |
| 529 // Collect non-blacklisted popular suggestions, skipping those already present | 528 // Collect non-blacklisted popular suggestions, skipping those already present |
| 530 // in the personal suggestions. | 529 // in the personal suggestions. |
| 531 size_t num_popular_sites_suggestions = num_sites_ - num_suggestions; | 530 size_t num_popular_sites_suggestions = num_sites_ - num_suggestions; |
| 532 MostVisitedSites::SuggestionsPtrVector popular_sites_suggestions; | 531 MostVisitedSites::SuggestionsPtrVector popular_sites_suggestions; |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 635 // Insert leftover popular suggestions. | 634 // Insert leftover popular suggestions. |
| 636 InsertAllSuggestions(filled_so_far, new_popular_suggestions, | 635 InsertAllSuggestions(filled_so_far, new_popular_suggestions, |
| 637 popular_suggestions, &merged_suggestions); | 636 popular_suggestions, &merged_suggestions); |
| 638 return merged_suggestions; | 637 return merged_suggestions; |
| 639 } | 638 } |
| 640 | 639 |
| 641 void MostVisitedSites::GetPreviousNTPSites( | 640 void MostVisitedSites::GetPreviousNTPSites( |
| 642 size_t num_tiles, | 641 size_t num_tiles, |
| 643 std::vector<std::string>* old_sites_url, | 642 std::vector<std::string>* old_sites_url, |
| 644 std::vector<bool>* old_sites_is_personal) const { | 643 std::vector<bool>* old_sites_is_personal) const { |
| 645 const PrefService* prefs = profile_->GetPrefs(); | 644 const base::ListValue* url_list = prefs_->GetList( |
| 646 const base::ListValue* url_list = prefs->GetList( | |
| 647 ntp_tiles::prefs::kNTPSuggestionsURL); | 645 ntp_tiles::prefs::kNTPSuggestionsURL); |
| 648 const base::ListValue* source_list = | 646 const base::ListValue* source_list = |
| 649 prefs->GetList(ntp_tiles::prefs::kNTPSuggestionsIsPersonal); | 647 prefs_->GetList(ntp_tiles::prefs::kNTPSuggestionsIsPersonal); |
| 650 DCHECK_EQ(url_list->GetSize(), source_list->GetSize()); | 648 DCHECK_EQ(url_list->GetSize(), source_list->GetSize()); |
| 651 if (url_list->GetSize() < num_tiles) | 649 if (url_list->GetSize() < num_tiles) |
| 652 num_tiles = url_list->GetSize(); | 650 num_tiles = url_list->GetSize(); |
| 653 if (num_tiles == 0) { | 651 if (num_tiles == 0) { |
| 654 // No fallback required as Personal suggestions take precedence anyway. | 652 // No fallback required as Personal suggestions take precedence anyway. |
| 655 return; | 653 return; |
| 656 } | 654 } |
| 657 old_sites_url->reserve(num_tiles); | 655 old_sites_url->reserve(num_tiles); |
| 658 old_sites_is_personal->reserve(num_tiles); | 656 old_sites_is_personal->reserve(num_tiles); |
| 659 for (size_t i = 0; i < num_tiles; ++i) { | 657 for (size_t i = 0; i < num_tiles; ++i) { |
| 660 std::string url_string; | 658 std::string url_string; |
| 661 bool success = url_list->GetString(i, &url_string); | 659 bool success = url_list->GetString(i, &url_string); |
| 662 DCHECK(success); | 660 DCHECK(success); |
| 663 old_sites_url->push_back(url_string); | 661 old_sites_url->push_back(url_string); |
| 664 bool is_personal; | 662 bool is_personal; |
| 665 success = source_list->GetBoolean(i, &is_personal); | 663 success = source_list->GetBoolean(i, &is_personal); |
| 666 DCHECK(success); | 664 DCHECK(success); |
| 667 old_sites_is_personal->push_back(is_personal); | 665 old_sites_is_personal->push_back(is_personal); |
| 668 } | 666 } |
| 669 } | 667 } |
| 670 | 668 |
| 671 void MostVisitedSites::SaveCurrentNTPSites() { | 669 void MostVisitedSites::SaveCurrentNTPSites() { |
| 672 base::ListValue url_list; | 670 base::ListValue url_list; |
| 673 base::ListValue source_list; | 671 base::ListValue source_list; |
| 674 for (const auto& suggestion : current_suggestions_) { | 672 for (const auto& suggestion : current_suggestions_) { |
| 675 url_list.AppendString(suggestion.url.spec()); | 673 url_list.AppendString(suggestion.url.spec()); |
| 676 source_list.AppendBoolean(suggestion.source != MostVisitedSites::POPULAR); | 674 source_list.AppendBoolean(suggestion.source != MostVisitedSites::POPULAR); |
| 677 } | 675 } |
| 678 PrefService* prefs = profile_->GetPrefs(); | 676 prefs_->Set(ntp_tiles::prefs::kNTPSuggestionsIsPersonal, source_list); |
| 679 prefs->Set(ntp_tiles::prefs::kNTPSuggestionsIsPersonal, source_list); | 677 prefs_->Set(ntp_tiles::prefs::kNTPSuggestionsURL, url_list); |
| 680 prefs->Set(ntp_tiles::prefs::kNTPSuggestionsURL, url_list); | |
| 681 } | 678 } |
| 682 | 679 |
| 683 // static | 680 // static |
| 684 std::vector<size_t> MostVisitedSites::InsertMatchingSuggestions( | 681 std::vector<size_t> MostVisitedSites::InsertMatchingSuggestions( |
| 685 MostVisitedSites::SuggestionsPtrVector* src_suggestions, | 682 MostVisitedSites::SuggestionsPtrVector* src_suggestions, |
| 686 MostVisitedSites::SuggestionsPtrVector* dst_suggestions, | 683 MostVisitedSites::SuggestionsPtrVector* dst_suggestions, |
| 687 const std::vector<std::string>& match_urls, | 684 const std::vector<std::string>& match_urls, |
| 688 const std::vector<std::string>& match_hosts) { | 685 const std::vector<std::string>& match_hosts) { |
| 689 std::vector<size_t> unmatched_suggestions; | 686 std::vector<size_t> unmatched_suggestions; |
| 690 size_t num_src_suggestions = src_suggestions->size(); | 687 size_t num_src_suggestions = src_suggestions->size(); |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 776 | 773 |
| 777 void MostVisitedSites::TopSitesLoaded(TopSites* top_sites) {} | 774 void MostVisitedSites::TopSitesLoaded(TopSites* top_sites) {} |
| 778 | 775 |
| 779 void MostVisitedSites::TopSitesChanged(TopSites* top_sites, | 776 void MostVisitedSites::TopSitesChanged(TopSites* top_sites, |
| 780 ChangeReason change_reason) { | 777 ChangeReason change_reason) { |
| 781 if (mv_source_ == TOP_SITES) { | 778 if (mv_source_ == TOP_SITES) { |
| 782 // The displayed suggestions are invalidated. | 779 // The displayed suggestions are invalidated. |
| 783 InitiateTopSitesQuery(); | 780 InitiateTopSitesQuery(); |
| 784 } | 781 } |
| 785 } | 782 } |
| OLD | NEW |