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

Side by Side Diff: components/ntp_snippets/content_suggestions_service.cc

Issue 2277743002: [NTP Snippets] Always show the bookmarks section, but at the end if it's empty (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@fix_proguard
Patch Set: fix Linux debug build Created 4 years, 3 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
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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_snippets/content_suggestions_service.h" 5 #include "components/ntp_snippets/content_suggestions_service.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <iterator> 8 #include <iterator>
9 9
10 #include "base/bind.h" 10 #include "base/bind.h"
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after
172 for (const ContentSuggestion& suggestion : 172 for (const ContentSuggestion& suggestion :
173 suggestions_by_category_[category]) { 173 suggestions_by_category_[category]) {
174 id_category_map_.erase(suggestion.id()); 174 id_category_map_.erase(suggestion.id());
175 } 175 }
176 176
177 for (const ContentSuggestion& suggestion : new_suggestions) 177 for (const ContentSuggestion& suggestion : new_suggestions)
178 id_category_map_.insert(std::make_pair(suggestion.id(), category)); 178 id_category_map_.insert(std::make_pair(suggestion.id(), category));
179 179
180 suggestions_by_category_[category] = std::move(new_suggestions); 180 suggestions_by_category_[category] = std::move(new_suggestions);
181 181
182 // The positioning of the bookmarks category depends on whether it's empty.
183 // TODO(treib): Remove this temporary hack, crbug.com/640568.
184 if (category.IsKnownCategory(KnownCategories::BOOKMARKS))
185 SortCategories();
186
182 FOR_EACH_OBSERVER(Observer, observers_, OnNewSuggestions(category)); 187 FOR_EACH_OBSERVER(Observer, observers_, OnNewSuggestions(category));
183 } 188 }
184 189
185 void ContentSuggestionsService::OnCategoryStatusChanged( 190 void ContentSuggestionsService::OnCategoryStatusChanged(
186 ContentSuggestionsProvider* provider, 191 ContentSuggestionsProvider* provider,
187 Category category, 192 Category category,
188 CategoryStatus new_status) { 193 CategoryStatus new_status) {
189 if (!IsCategoryStatusAvailable(new_status)) { 194 if (!IsCategoryStatusAvailable(new_status)) {
190 for (const ContentSuggestion& suggestion : 195 for (const ContentSuggestion& suggestion :
191 suggestions_by_category_[category]) { 196 suggestions_by_category_[category]) {
(...skipping 28 matching lines...) Expand all
220 ContentSuggestionsProvider* provider, 225 ContentSuggestionsProvider* provider,
221 Category category) { 226 Category category) {
222 auto it = providers_by_category_.find(category); 227 auto it = providers_by_category_.find(category);
223 if (it != providers_by_category_.end()) { 228 if (it != providers_by_category_.end()) {
224 DCHECK_EQ(it->second, provider); 229 DCHECK_EQ(it->second, provider);
225 return false; 230 return false;
226 } 231 }
227 232
228 providers_by_category_[category] = provider; 233 providers_by_category_[category] = provider;
229 categories_.push_back(category); 234 categories_.push_back(category);
230 std::sort(categories_.begin(), categories_.end(), 235 SortCategories();
231 [this](const Category& left, const Category& right) {
232 return category_factory_.CompareCategories(left, right);
233 });
234 if (IsCategoryStatusAvailable(provider->GetCategoryStatus(category))) { 236 if (IsCategoryStatusAvailable(provider->GetCategoryStatus(category))) {
235 suggestions_by_category_.insert( 237 suggestions_by_category_.insert(
236 std::make_pair(category, std::vector<ContentSuggestion>())); 238 std::make_pair(category, std::vector<ContentSuggestion>()));
237 } 239 }
238 return true; 240 return true;
239 } 241 }
240 242
241 bool ContentSuggestionsService::RemoveSuggestionByID( 243 bool ContentSuggestionsService::RemoveSuggestionByID(
242 Category category, 244 Category category,
243 const std::string& suggestion_id) { 245 const std::string& suggestion_id) {
244 id_category_map_.erase(suggestion_id); 246 id_category_map_.erase(suggestion_id);
245 std::vector<ContentSuggestion>* suggestions = 247 std::vector<ContentSuggestion>* suggestions =
246 &suggestions_by_category_[category]; 248 &suggestions_by_category_[category];
247 auto position = 249 auto position =
248 std::find_if(suggestions->begin(), suggestions->end(), 250 std::find_if(suggestions->begin(), suggestions->end(),
249 [&suggestion_id](const ContentSuggestion& suggestion) { 251 [&suggestion_id](const ContentSuggestion& suggestion) {
250 return suggestion_id == suggestion.id(); 252 return suggestion_id == suggestion.id();
251 }); 253 });
252 if (position == suggestions->end()) 254 if (position == suggestions->end())
253 return false; 255 return false;
254 suggestions->erase(position); 256 suggestions->erase(position);
257
258 // The positioning of the bookmarks category depends on whether it's empty.
259 // TODO(treib): Remove this temporary hack, crbug.com/640568.
260 if (category.IsKnownCategory(KnownCategories::BOOKMARKS))
261 SortCategories();
262
255 return true; 263 return true;
256 } 264 }
257 265
258 void ContentSuggestionsService::NotifyCategoryStatusChanged(Category category) { 266 void ContentSuggestionsService::NotifyCategoryStatusChanged(Category category) {
259 FOR_EACH_OBSERVER( 267 FOR_EACH_OBSERVER(
260 Observer, observers_, 268 Observer, observers_,
261 OnCategoryStatusChanged(category, GetCategoryStatus(category))); 269 OnCategoryStatusChanged(category, GetCategoryStatus(category)));
262 } 270 }
263 271
272 void ContentSuggestionsService::SortCategories() {
273 auto it = suggestions_by_category_.find(
274 category_factory_.FromKnownCategory(KnownCategories::BOOKMARKS));
275 bool bookmarks_empty =
276 (it == suggestions_by_category_.end() || it->second.empty());
277 std::sort(
278 categories_.begin(), categories_.end(),
279 [this, bookmarks_empty](const Category& left, const Category& right) {
280 // If the bookmarks section is empty, put it at the end.
281 // TODO(treib): This is a temporary hack, see crbug.com/640568.
282 if (bookmarks_empty) {
283 if (left.IsKnownCategory(KnownCategories::BOOKMARKS))
284 return false;
285 if (right.IsKnownCategory(KnownCategories::BOOKMARKS))
286 return true;
287 }
288 return category_factory_.CompareCategories(left, right);
289 });
290 }
291
264 } // namespace ntp_snippets 292 } // namespace ntp_snippets
OLDNEW
« no previous file with comments | « components/ntp_snippets/content_suggestions_service.h ('k') | components/ntp_snippets/content_suggestions_service_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698