Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(260)

Side by Side Diff: components/ntp_tiles/most_visited_sites.cc

Issue 2131863002: MostVisitedSites: simplify prefs (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@remove_provider_id
Patch Set: undo random cleanup Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « components/ntp_tiles/most_visited_sites.h ('k') | components/ntp_tiles/pref_names.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 "components/ntp_tiles/most_visited_sites.h" 5 #include "components/ntp_tiles/most_visited_sites.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <set> 8 #include <set>
9 #include <utility> 9 #include <utility>
10 10
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
70 if (cmd_line->HasSwitch(ntp_tiles::switches::kDisableNTPPopularSites)) 70 if (cmd_line->HasSwitch(ntp_tiles::switches::kDisableNTPPopularSites))
71 return false; 71 return false;
72 if (cmd_line->HasSwitch(ntp_tiles::switches::kEnableNTPPopularSites)) 72 if (cmd_line->HasSwitch(ntp_tiles::switches::kEnableNTPPopularSites))
73 return true; 73 return true;
74 return base::StartsWith(group_name, "Enabled", 74 return base::StartsWith(group_name, "Enabled",
75 base::CompareCase::INSENSITIVE_ASCII); 75 base::CompareCase::INSENSITIVE_ASCII);
76 } 76 }
77 77
78 // Determine whether we need any popular suggestions to fill up a grid of 78 // Determine whether we need any popular suggestions to fill up a grid of
79 // |num_tiles| tiles. 79 // |num_tiles| tiles.
80 bool NeedPopularSites(const PrefService* prefs, size_t num_tiles) { 80 bool NeedPopularSites(const PrefService* prefs, int num_tiles) {
81 if (num_tiles <= prefs->GetInteger(prefs::kNumPersonalSuggestions))
82 return false;
83
84 // TODO(treib): Remove after M55.
81 const base::ListValue* source_list = 85 const base::ListValue* source_list =
82 prefs->GetList(ntp_tiles::prefs::kNTPSuggestionsIsPersonal); 86 prefs->GetList(ntp_tiles::prefs::kDeprecatedNTPSuggestionsIsPersonal);
sfiera 2016/07/08 09:59:15 How important is this to preserve? Seems like a us
Marc Treib 2016/07/08 10:02:17 Well, accumulated, it'd still be a few hundred mil
83 // If there aren't enough previous suggestions to fill the grid, we need 87 // If there aren't enough previous suggestions to fill the grid, we need
84 // popular suggestions. 88 // popular suggestions.
85 if (source_list->GetSize() < num_tiles) 89 if (static_cast<int>(source_list->GetSize()) < num_tiles)
86 return true; 90 return true;
87 // Otherwise, if any of the previous suggestions is not personal, then also 91 // Otherwise, if any of the previous suggestions is not personal, then also
88 // get popular suggestions. 92 // get popular suggestions.
89 for (size_t i = 0; i < num_tiles; ++i) { 93 for (int i = 0; i < num_tiles; ++i) {
90 bool is_personal = false; 94 bool is_personal = false;
91 if (source_list->GetBoolean(i, &is_personal) && !is_personal) 95 if (source_list->GetBoolean(i, &is_personal) && !is_personal)
92 return true; 96 return true;
93 } 97 }
94 // The whole grid is already filled with personal suggestions, no point in 98 // The whole grid is already filled with personal suggestions, no point in
95 // bothering with popular ones. 99 // bothering with popular ones.
96 return false; 100 return false;
97 } 101 }
98 102
99 bool AreURLsEquivalent(const GURL& url1, const GURL& url2) { 103 bool AreURLsEquivalent(const GURL& url1, const GURL& url2) {
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after
264 LogHistogramEvent(histogram, tile_type, NUM_TILE_TYPES); 268 LogHistogramEvent(histogram, tile_type, NUM_TILE_TYPES);
265 } 269 }
266 270
267 void MostVisitedSites::OnBlockedSitesChanged() { 271 void MostVisitedSites::OnBlockedSitesChanged() {
268 BuildCurrentSuggestions(); 272 BuildCurrentSuggestions();
269 } 273 }
270 274
271 // static 275 // static
272 void MostVisitedSites::RegisterProfilePrefs( 276 void MostVisitedSites::RegisterProfilePrefs(
273 user_prefs::PrefRegistrySyncable* registry) { 277 user_prefs::PrefRegistrySyncable* registry) {
274 // TODO(treib): Remove this, it's unused. Do we need migration code to clean 278 registry->RegisterIntegerPref(prefs::kNumPersonalSuggestions, 0);
275 // up existing entries? 279 // TODO(treib): Remove after M55.
276 registry->RegisterListPref(ntp_tiles::prefs::kNTPSuggestionsURL); 280 registry->RegisterListPref(prefs::kDeprecatedNTPSuggestionsURL);
277 // TODO(treib): Remove this. It's only used to determine if we need 281 registry->RegisterListPref(prefs::kDeprecatedNTPSuggestionsIsPersonal);
278 // PopularSites at all. Find a way to do that without prefs, or failing that,
279 // replace this list pref by a simple bool.
280 registry->RegisterListPref(ntp_tiles::prefs::kNTPSuggestionsIsPersonal);
281 } 282 }
282 283
283 void MostVisitedSites::BuildCurrentSuggestions() { 284 void MostVisitedSites::BuildCurrentSuggestions() {
284 // Get the current suggestions from cache. If the cache is empty, this will 285 // Get the current suggestions from cache. If the cache is empty, this will
285 // fall back to TopSites. 286 // fall back to TopSites.
286 OnSuggestionsProfileAvailable( 287 OnSuggestionsProfileAvailable(
287 suggestions_service_->GetSuggestionsDataFromCache()); 288 suggestions_service_->GetSuggestionsDataFromCache());
288 } 289 }
289 290
290 void MostVisitedSites::InitiateTopSitesQuery() { 291 void MostVisitedSites::InitiateTopSitesQuery() {
(...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after
464 size_t num_actual_tiles = personal_suggestions.size() + 465 size_t num_actual_tiles = personal_suggestions.size() +
465 whitelist_suggestions.size() + 466 whitelist_suggestions.size() +
466 popular_sites_suggestions.size(); 467 popular_sites_suggestions.size();
467 DCHECK_LE(num_actual_tiles, static_cast<size_t>(num_sites_)); 468 DCHECK_LE(num_actual_tiles, static_cast<size_t>(num_sites_));
468 469
469 current_suggestions_ = MergeSuggestions(std::move(personal_suggestions), 470 current_suggestions_ = MergeSuggestions(std::move(personal_suggestions),
470 std::move(whitelist_suggestions), 471 std::move(whitelist_suggestions),
471 std::move(popular_sites_suggestions)); 472 std::move(popular_sites_suggestions));
472 DCHECK_EQ(num_actual_tiles, current_suggestions_.size()); 473 DCHECK_EQ(num_actual_tiles, current_suggestions_.size());
473 474
474 if (received_popular_sites_) 475 int num_personal_suggestions = 0;
475 SaveCurrentSuggestionsToPrefs(); 476 for (const auto& suggestion : current_suggestions_) {
477 if (suggestion.source != POPULAR)
478 num_personal_suggestions++;
479 }
480 prefs_->SetInteger(prefs::kNumPersonalSuggestions, num_personal_suggestions);
481 // TODO(treib): Remove after M55.
482 prefs_->ClearPref(prefs::kDeprecatedNTPSuggestionsIsPersonal);
483 prefs_->ClearPref(prefs::kDeprecatedNTPSuggestionsURL);
476 } 484 }
477 485
478 // static 486 // static
479 MostVisitedSites::SuggestionsVector MostVisitedSites::MergeSuggestions( 487 MostVisitedSites::SuggestionsVector MostVisitedSites::MergeSuggestions(
480 SuggestionsVector personal_suggestions, 488 SuggestionsVector personal_suggestions,
481 SuggestionsVector whitelist_suggestions, 489 SuggestionsVector whitelist_suggestions,
482 SuggestionsVector popular_suggestions) { 490 SuggestionsVector popular_suggestions) {
483 SuggestionsVector merged_suggestions; 491 SuggestionsVector merged_suggestions;
484 AppendSuggestions(std::move(personal_suggestions), &merged_suggestions); 492 AppendSuggestions(std::move(personal_suggestions), &merged_suggestions);
485 AppendSuggestions(std::move(whitelist_suggestions), &merged_suggestions); 493 AppendSuggestions(std::move(whitelist_suggestions), &merged_suggestions);
486 AppendSuggestions(std::move(popular_suggestions), &merged_suggestions); 494 AppendSuggestions(std::move(popular_suggestions), &merged_suggestions);
487 return merged_suggestions; 495 return merged_suggestions;
488 } 496 }
489 497
490 void MostVisitedSites::SaveCurrentSuggestionsToPrefs() {
491 base::ListValue url_list;
492 base::ListValue source_list;
493 for (const auto& suggestion : current_suggestions_) {
494 url_list.AppendString(suggestion.url.spec());
495 source_list.AppendBoolean(suggestion.source != POPULAR);
496 }
497 prefs_->Set(ntp_tiles::prefs::kNTPSuggestionsIsPersonal, source_list);
498 prefs_->Set(ntp_tiles::prefs::kNTPSuggestionsURL, url_list);
499 }
500
501 void MostVisitedSites::NotifyMostVisitedURLsObserver() { 498 void MostVisitedSites::NotifyMostVisitedURLsObserver() {
502 if (received_most_visited_sites_ && received_popular_sites_ && 499 if (received_most_visited_sites_ && received_popular_sites_ &&
503 !recorded_uma_) { 500 !recorded_uma_) {
504 RecordImpressionUMAMetrics(); 501 RecordImpressionUMAMetrics();
505 UMA_HISTOGRAM_SPARSE_SLOWLY("NewTabPage.NumberOfTiles", 502 UMA_HISTOGRAM_SPARSE_SLOWLY("NewTabPage.NumberOfTiles",
506 current_suggestions_.size()); 503 current_suggestions_.size());
507 recorded_uma_ = true; 504 recorded_uma_ = true;
508 } 505 }
509 506
510 if (!observer_) 507 if (!observer_)
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
542 539
543 void MostVisitedSites::TopSitesChanged(TopSites* top_sites, 540 void MostVisitedSites::TopSitesChanged(TopSites* top_sites,
544 ChangeReason change_reason) { 541 ChangeReason change_reason) {
545 if (mv_source_ == TOP_SITES) { 542 if (mv_source_ == TOP_SITES) {
546 // The displayed suggestions are invalidated. 543 // The displayed suggestions are invalidated.
547 InitiateTopSitesQuery(); 544 InitiateTopSitesQuery();
548 } 545 }
549 } 546 }
550 547
551 } // namespace ntp_tiles 548 } // namespace ntp_tiles
OLDNEW
« no previous file with comments | « components/ntp_tiles/most_visited_sites.h ('k') | components/ntp_tiles/pref_names.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698