Index: components/ntp_snippets/content_suggestions_service.cc |
diff --git a/components/ntp_snippets/content_suggestions_service.cc b/components/ntp_snippets/content_suggestions_service.cc |
index 1da49f5cfd53dda8781c150fa113ec6be6c49cc9..5a983f423832d440025859ada0dec20eeb99ca3f 100644 |
--- a/components/ntp_snippets/content_suggestions_service.cc |
+++ b/components/ntp_snippets/content_suggestions_service.cc |
@@ -179,6 +179,11 @@ void ContentSuggestionsService::OnNewSuggestions( |
suggestions_by_category_[category] = std::move(new_suggestions); |
+ // The positioning of the bookmarks category depends on whether it's empty. |
+ // TODO(treib): Remove this temporary hack, crbug.com/640568. |
+ if (category.IsKnownCategory(KnownCategories::BOOKMARKS)) |
+ SortCategories(); |
+ |
FOR_EACH_OBSERVER(Observer, observers_, OnNewSuggestions(category)); |
} |
@@ -227,10 +232,7 @@ bool ContentSuggestionsService::RegisterCategoryIfRequired( |
providers_by_category_[category] = provider; |
categories_.push_back(category); |
- std::sort(categories_.begin(), categories_.end(), |
- [this](const Category& left, const Category& right) { |
- return category_factory_.CompareCategories(left, right); |
- }); |
+ SortCategories(); |
if (IsCategoryStatusAvailable(provider->GetCategoryStatus(category))) { |
suggestions_by_category_.insert( |
std::make_pair(category, std::vector<ContentSuggestion>())); |
@@ -252,6 +254,12 @@ bool ContentSuggestionsService::RemoveSuggestionByID( |
if (position == suggestions->end()) |
return false; |
suggestions->erase(position); |
+ |
+ // The positioning of the bookmarks category depends on whether it's empty. |
+ // TODO(treib): Remove this temporary hack, crbug.com/640568. |
+ if (category.IsKnownCategory(KnownCategories::BOOKMARKS)) |
+ SortCategories(); |
+ |
return true; |
} |
@@ -261,4 +269,24 @@ void ContentSuggestionsService::NotifyCategoryStatusChanged(Category category) { |
OnCategoryStatusChanged(category, GetCategoryStatus(category))); |
} |
+void ContentSuggestionsService::SortCategories() { |
+ auto it = suggestions_by_category_.find( |
+ category_factory_.FromKnownCategory(KnownCategories::BOOKMARKS)); |
+ bool bookmarks_empty = |
+ (it == suggestions_by_category_.end() || it->second.empty()); |
+ std::sort( |
+ categories_.begin(), categories_.end(), |
+ [this, bookmarks_empty](const Category& left, const Category& right) { |
+ // If the bookmarks section is empty, put it at the end. |
+ // TODO(treib): This is a temporary hack, see crbug.com/640568. |
+ if (bookmarks_empty) { |
+ if (left.IsKnownCategory(KnownCategories::BOOKMARKS)) |
+ return false; |
+ if (right.IsKnownCategory(KnownCategories::BOOKMARKS)) |
+ return true; |
+ } |
+ return category_factory_.CompareCategories(left, right); |
+ }); |
+} |
+ |
} // namespace ntp_snippets |