Chromium Code Reviews| 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 778bb9eed2e5164cb34abb3885edb37186865443..c7e1d3f3594f906b76e9d02a319200c13b13c110 100644 |
| --- a/components/ntp_snippets/content_suggestions_service.cc |
| +++ b/components/ntp_snippets/content_suggestions_service.cc |
| @@ -13,6 +13,12 @@ |
| namespace ntp_snippets { |
| +bool ContentSuggestionsService::CompareCategoriesByID::operator()( |
| + const ContentSuggestionsCategory& left, |
| + const ContentSuggestionsCategory& right) const { |
| + return left.id() < right.id(); |
| +} |
| + |
| ContentSuggestionsService::ContentSuggestionsService(State state) |
| : state_(state) {} |
| @@ -58,10 +64,10 @@ void ContentSuggestionsService::FetchSuggestionImage( |
| callback.Run(suggestion_id, gfx::Image()); |
| return; |
| } |
| - ContentSuggestionsCategory category = id_category_map_[suggestion_id]; |
| + ContentSuggestionsCategory category = id_category_map_.at(suggestion_id); |
| if (!providers_.count(category)) { |
| LOG(WARNING) << "Requested image for suggestion " << suggestion_id |
| - << " for unavailable category " << static_cast<int>(category); |
| + << " for unavailable category " << category.id(); |
| callback.Run(suggestion_id, gfx::Image()); |
| return; |
| } |
| @@ -89,10 +95,10 @@ void ContentSuggestionsService::DismissSuggestion( |
| LOG(WARNING) << "Dismissed unknown suggestion " << suggestion_id; |
| return; |
| } |
| - ContentSuggestionsCategory category = id_category_map_[suggestion_id]; |
| + ContentSuggestionsCategory category = id_category_map_.at(suggestion_id); |
| if (!providers_.count(category)) { |
| LOG(WARNING) << "Dismissed suggestion " << suggestion_id |
| - << " for unavailable category " << static_cast<int>(category); |
| + << " for unavailable category " << category.id(); |
| return; |
| } |
| providers_[category]->DismissSuggestion(suggestion_id); |
| @@ -131,8 +137,6 @@ void ContentSuggestionsService::RegisterProvider( |
| for (ContentSuggestionsCategory category : provider->provided_categories()) { |
| DCHECK_EQ(0ul, providers_.count(category)); |
| providers_[category] = provider; |
| - // TODO(pke): In the future, make sure that the categories have some useful |
| - // (maybe constant, at least consistent) ordering for the UI. |
| categories_.push_back(category); |
| if (IsContentSuggestionsCategoryStatusAvailable( |
| provider->GetCategoryStatus(category))) { |
| @@ -140,6 +144,11 @@ void ContentSuggestionsService::RegisterProvider( |
| } |
| NotifyCategoryStatusChanged(category); |
| } |
| + std::sort(categories_.begin(), categories_.end(), |
| + [this](const ContentSuggestionsCategory& left, |
| + const ContentSuggestionsCategory& right) { |
| + return category_factory_.CompareCategories(left, right); |
| + }); |
| provider->SetObserver(this); |
| } |
| @@ -156,8 +165,8 @@ void ContentSuggestionsService::OnNewSuggestions( |
| id_category_map_.erase(suggestion.id()); |
| } |
| - for (const ContentSuggestion& suggestion : new_suggestions) { |
| - id_category_map_[suggestion.id()] = changed_category; |
| + for (ContentSuggestion& suggestion : new_suggestions) { |
|
Marc Treib
2016/07/28 11:41:46
Why'd you remove the "const"?
Philipp Keck
2016/07/28 13:50:54
That was unintentional.
|
| + id_category_map_.insert(std::make_pair(suggestion.id(), changed_category)); |
| } |
| suggestions_by_category_[changed_category] = std::move(new_suggestions); |