Commit cd3d2ef0 authored by treib's avatar treib Committed by Commit bot

MostVisitedSites: simplify prefs

BUG=none

Review-Url: https://codereview.chromium.org/2131863002
Cr-Commit-Position: refs/heads/master@{#405193}
parent 5a7cadf1
...@@ -77,16 +77,20 @@ bool ShouldShowPopularSites() { ...@@ -77,16 +77,20 @@ bool ShouldShowPopularSites() {
// Determine whether we need any popular suggestions to fill up a grid of // Determine whether we need any popular suggestions to fill up a grid of
// |num_tiles| tiles. // |num_tiles| tiles.
bool NeedPopularSites(const PrefService* prefs, size_t num_tiles) { bool NeedPopularSites(const PrefService* prefs, int num_tiles) {
if (num_tiles <= prefs->GetInteger(prefs::kNumPersonalSuggestions))
return false;
// TODO(treib): Remove after M55.
const base::ListValue* source_list = const base::ListValue* source_list =
prefs->GetList(ntp_tiles::prefs::kNTPSuggestionsIsPersonal); prefs->GetList(ntp_tiles::prefs::kDeprecatedNTPSuggestionsIsPersonal);
// If there aren't enough previous suggestions to fill the grid, we need // If there aren't enough previous suggestions to fill the grid, we need
// popular suggestions. // popular suggestions.
if (source_list->GetSize() < num_tiles) if (static_cast<int>(source_list->GetSize()) < num_tiles)
return true; return true;
// Otherwise, if any of the previous suggestions is not personal, then also // Otherwise, if any of the previous suggestions is not personal, then also
// get popular suggestions. // get popular suggestions.
for (size_t i = 0; i < num_tiles; ++i) { for (int i = 0; i < num_tiles; ++i) {
bool is_personal = false; bool is_personal = false;
if (source_list->GetBoolean(i, &is_personal) && !is_personal) if (source_list->GetBoolean(i, &is_personal) && !is_personal)
return true; return true;
...@@ -268,13 +272,10 @@ void MostVisitedSites::OnBlockedSitesChanged() { ...@@ -268,13 +272,10 @@ void MostVisitedSites::OnBlockedSitesChanged() {
// static // static
void MostVisitedSites::RegisterProfilePrefs( void MostVisitedSites::RegisterProfilePrefs(
user_prefs::PrefRegistrySyncable* registry) { user_prefs::PrefRegistrySyncable* registry) {
// TODO(treib): Remove this, it's unused. Do we need migration code to clean registry->RegisterIntegerPref(prefs::kNumPersonalSuggestions, 0);
// up existing entries? // TODO(treib): Remove after M55.
registry->RegisterListPref(ntp_tiles::prefs::kNTPSuggestionsURL); registry->RegisterListPref(prefs::kDeprecatedNTPSuggestionsURL);
// TODO(treib): Remove this. It's only used to determine if we need registry->RegisterListPref(prefs::kDeprecatedNTPSuggestionsIsPersonal);
// PopularSites at all. Find a way to do that without prefs, or failing that,
// replace this list pref by a simple bool.
registry->RegisterListPref(ntp_tiles::prefs::kNTPSuggestionsIsPersonal);
} }
void MostVisitedSites::BuildCurrentSuggestions() { void MostVisitedSites::BuildCurrentSuggestions() {
...@@ -468,8 +469,15 @@ void MostVisitedSites::SaveNewSuggestions( ...@@ -468,8 +469,15 @@ void MostVisitedSites::SaveNewSuggestions(
std::move(popular_sites_suggestions)); std::move(popular_sites_suggestions));
DCHECK_EQ(num_actual_tiles, current_suggestions_.size()); DCHECK_EQ(num_actual_tiles, current_suggestions_.size());
if (received_popular_sites_) int num_personal_suggestions = 0;
SaveCurrentSuggestionsToPrefs(); for (const auto& suggestion : current_suggestions_) {
if (suggestion.source != POPULAR)
num_personal_suggestions++;
}
prefs_->SetInteger(prefs::kNumPersonalSuggestions, num_personal_suggestions);
// TODO(treib): Remove after M55.
prefs_->ClearPref(prefs::kDeprecatedNTPSuggestionsIsPersonal);
prefs_->ClearPref(prefs::kDeprecatedNTPSuggestionsURL);
} }
// static // static
...@@ -484,17 +492,6 @@ MostVisitedSites::SuggestionsVector MostVisitedSites::MergeSuggestions( ...@@ -484,17 +492,6 @@ MostVisitedSites::SuggestionsVector MostVisitedSites::MergeSuggestions(
return merged_suggestions; return merged_suggestions;
} }
void MostVisitedSites::SaveCurrentSuggestionsToPrefs() {
base::ListValue url_list;
base::ListValue source_list;
for (const auto& suggestion : current_suggestions_) {
url_list.AppendString(suggestion.url.spec());
source_list.AppendBoolean(suggestion.source != POPULAR);
}
prefs_->Set(ntp_tiles::prefs::kNTPSuggestionsIsPersonal, source_list);
prefs_->Set(ntp_tiles::prefs::kNTPSuggestionsURL, url_list);
}
void MostVisitedSites::NotifyMostVisitedURLsObserver() { void MostVisitedSites::NotifyMostVisitedURLsObserver() {
if (received_most_visited_sites_ && received_popular_sites_ && if (received_most_visited_sites_ && received_popular_sites_ &&
!recorded_uma_) { !recorded_uma_) {
......
...@@ -221,8 +221,6 @@ class MostVisitedSites : public history::TopSitesObserver, ...@@ -221,8 +221,6 @@ class MostVisitedSites : public history::TopSitesObserver,
SuggestionsVector whitelist_suggestions, SuggestionsVector whitelist_suggestions,
SuggestionsVector popular_suggestions); SuggestionsVector popular_suggestions);
void SaveCurrentSuggestionsToPrefs();
// Notifies the observer about the availability of suggestions. // Notifies the observer about the availability of suggestions.
// Also records impressions UMA if not done already. // Also records impressions UMA if not done already.
void NotifyMostVisitedURLsObserver(); void NotifyMostVisitedURLsObserver();
......
...@@ -7,12 +7,13 @@ ...@@ -7,12 +7,13 @@
namespace ntp_tiles { namespace ntp_tiles {
namespace prefs { namespace prefs {
// Ordered list of website suggestions shown on the new tab page that will allow const char kDeprecatedNTPSuggestionsURL[] = "ntp.suggestions_url";
// retaining the order even if the suggestions change over time. const char kDeprecatedNTPSuggestionsIsPersonal[] =
const char kNTPSuggestionsURL[] = "ntp.suggestions_url"; "ntp.suggestions_is_personal";
// Whether the suggestion was derived from personal data. // The number of personal suggestions we had previously. Used to figure out
const char kNTPSuggestionsIsPersonal[] = "ntp.suggestions_is_personal"; // whether we need popular sites.
const char kNumPersonalSuggestions[] = "ntp.num_personal_suggestions";
// If set, overrides the URL for popular sites, including the individual // If set, overrides the URL for popular sites, including the individual
// overrides for country and version below. // overrides for country and version below.
......
...@@ -8,8 +8,11 @@ ...@@ -8,8 +8,11 @@
namespace ntp_tiles { namespace ntp_tiles {
namespace prefs { namespace prefs {
extern const char kNTPSuggestionsURL[]; // TODO(treib): Remove after M55.
extern const char kNTPSuggestionsIsPersonal[]; extern const char kDeprecatedNTPSuggestionsURL[];
extern const char kDeprecatedNTPSuggestionsIsPersonal[];
extern const char kNumPersonalSuggestions[];
extern const char kPopularSitesOverrideURL[]; extern const char kPopularSitesOverrideURL[];
extern const char kPopularSitesOverrideCountry[]; extern const char kPopularSitesOverrideCountry[];
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment