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 934928607776f56543562bc3d330f4c9054d0a5c..86a859f648d07e9a0cad21ba64645502c02c858e 100644 |
| --- a/components/ntp_snippets/content_suggestions_service.cc |
| +++ b/components/ntp_snippets/content_suggestions_service.cc |
| @@ -132,55 +132,63 @@ void ContentSuggestionsService::RegisterProvider( |
| if (state_ == State::DISABLED) |
| return; |
| + provider->SetObserver(this); |
| for (Category category : provider->GetProvidedCategories()) { |
| - DCHECK_EQ(0ul, providers_.count(category)); |
| - providers_[category] = provider; |
| - categories_.push_back(category); |
| - if (IsCategoryStatusAvailable(provider->GetCategoryStatus(category))) { |
| - suggestions_by_category_[category] = std::vector<ContentSuggestion>(); |
| - } |
| + DCHECK_NE(CategoryStatus::NOT_PROVIDED, |
| + provider->GetCategoryStatus(category)); |
| + EnsureCategoryRegistered(provider, category); |
| NotifyCategoryStatusChanged(category); |
| } |
| - std::sort(categories_.begin(), categories_.end(), |
| - [this](const Category& left, const Category& right) { |
| - return category_factory_.CompareCategories(left, right); |
| - }); |
| - provider->SetObserver(this); |
| } |
| //////////////////////////////////////////////////////////////////////////////// |
| // Private methods |
| void ContentSuggestionsService::OnNewSuggestions( |
| - Category changed_category, |
| + ContentSuggestionsProvider* provider, |
| + Category category, |
| std::vector<ContentSuggestion> new_suggestions) { |
| - DCHECK(IsCategoryRegistered(changed_category)); |
| + if (EnsureCategoryRegistered(provider, category)) { |
| + NotifyCategoryStatusChanged(category); |
|
Marc Treib
2016/08/01 14:23:37
Hm, you call this before the new suggestions are a
Philipp Keck
2016/08/01 14:59:03
Related question:
If a category is registered whi
Marc Treib
2016/08/01 15:17:47
This way; the other way around is even weirder (th
Philipp Keck
2016/08/01 15:58:12
I'm not opposed to that, we could also get rid of
Marc Treib
2016/08/01 16:28:51
I thought it should only be in AVAILABLE_LOADING i
Philipp Keck
2016/08/01 17:04:58
Acknowledged.
|
| + } |
| for (const ContentSuggestion& suggestion : |
| - suggestions_by_category_[changed_category]) { |
| + suggestions_by_category_[category]) { |
| id_category_map_.erase(suggestion.id()); |
| } |
| for (const ContentSuggestion& suggestion : new_suggestions) { |
| - id_category_map_.insert(std::make_pair(suggestion.id(), changed_category)); |
| + id_category_map_.insert(std::make_pair(suggestion.id(), category)); |
| } |
| - suggestions_by_category_[changed_category] = std::move(new_suggestions); |
| + suggestions_by_category_[category] = std::move(new_suggestions); |
| FOR_EACH_OBSERVER(Observer, observers_, OnNewSuggestions()); |
| } |
| void ContentSuggestionsService::OnCategoryStatusChanged( |
| - Category changed_category, |
| + ContentSuggestionsProvider* provider, |
| + Category category, |
| CategoryStatus new_status) { |
| if (!IsCategoryStatusAvailable(new_status)) { |
| for (const ContentSuggestion& suggestion : |
| - suggestions_by_category_[changed_category]) { |
| + suggestions_by_category_[category]) { |
| id_category_map_.erase(suggestion.id()); |
| } |
| - suggestions_by_category_.erase(changed_category); |
| + suggestions_by_category_.erase(category); |
| + } |
| + if (new_status == CategoryStatus::NOT_PROVIDED) { |
| + auto providers_entry = providers_.find(category); |
|
tschumann
2016/08/01 14:21:17
nit: the usual naming would be 'providers_it'.
Philipp Keck
2016/08/01 14:59:04
Done.
|
| + DCHECK(providers_entry != providers_.end()); |
| + DCHECK_EQ(provider, providers_entry->second); |
| + providers_.erase(providers_entry); |
|
Marc Treib
2016/08/01 14:23:37
Hm, maybe providers_ should be renamed to provider
Philipp Keck
2016/08/01 14:59:04
Done.
|
| + categories_.erase( |
| + std::find(categories_.begin(), categories_.end(), category)); |
| + } else { |
| + EnsureCategoryRegistered(provider, category); |
| + DCHECK_EQ(new_status, provider->GetCategoryStatus(category)); |
| } |
| - NotifyCategoryStatusChanged(changed_category); |
| + NotifyCategoryStatusChanged(category); |
| } |
| void ContentSuggestionsService::OnProviderShutdown( |
| @@ -199,9 +207,25 @@ void ContentSuggestionsService::OnProviderShutdown( |
| } |
| } |
| -bool ContentSuggestionsService::IsCategoryRegistered(Category category) const { |
| - return std::find(categories_.begin(), categories_.end(), category) != |
| - categories_.end(); |
| +bool ContentSuggestionsService::EnsureCategoryRegistered( |
|
tschumann
2016/08/01 14:21:17
nit: it feels weird for EnsureCategoryRegistered t
Philipp Keck
2016/08/01 14:59:03
Marc suggested EnsureCategoryIsRegistered.
The lo
Marc Treib
2016/08/01 15:17:47
I'd prefer if it either always or never notified.
Philipp Keck
2016/08/01 15:58:12
How about "RegisterCategory[IfRequired/IfNecessary
Marc Treib
2016/08/01 16:28:51
Sure, that works too.
Philipp Keck
2016/08/01 17:04:58
Done.
|
| + ContentSuggestionsProvider* provider, |
| + Category category) { |
| + auto entry = providers_.find(category); |
| + if (entry != providers_.end()) { |
| + DCHECK_EQ(entry->second, provider); |
| + return false; |
| + } |
| + |
| + providers_[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); |
| + }); |
| + if (IsCategoryStatusAvailable(provider->GetCategoryStatus(category))) { |
| + suggestions_by_category_[category] = std::vector<ContentSuggestion>(); |
| + } |
| + return true; |
| } |
| void ContentSuggestionsService::NotifyCategoryStatusChanged(Category category) { |