| OLD | NEW |
| 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 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 134 } | 134 } |
| 135 Category category = id_category_map_.at(suggestion_id); | 135 Category category = id_category_map_.at(suggestion_id); |
| 136 if (!providers_by_category_.count(category)) { | 136 if (!providers_by_category_.count(category)) { |
| 137 LOG(WARNING) << "Dismissed suggestion " << suggestion_id | 137 LOG(WARNING) << "Dismissed suggestion " << suggestion_id |
| 138 << " for unavailable category " << category; | 138 << " for unavailable category " << category; |
| 139 return; | 139 return; |
| 140 } | 140 } |
| 141 providers_by_category_[category]->DismissSuggestion(suggestion_id); | 141 providers_by_category_[category]->DismissSuggestion(suggestion_id); |
| 142 | 142 |
| 143 // Remove the suggestion locally. | 143 // Remove the suggestion locally. |
| 144 id_category_map_.erase(suggestion_id); | 144 bool removed = RemoveSuggestionByID(category, suggestion_id); |
| 145 std::vector<ContentSuggestion>* suggestions = | 145 DCHECK(removed) << "The dismissed suggestion " << suggestion_id |
| 146 &suggestions_by_category_[category]; | 146 << " has already been removed. Providers must not call" |
| 147 auto position = | 147 << " OnNewSuggestions in response to DismissSuggestion."; |
| 148 std::find_if(suggestions->begin(), suggestions->end(), | |
| 149 [&suggestion_id](const ContentSuggestion& suggestion) { | |
| 150 return suggestion_id == suggestion.id(); | |
| 151 }); | |
| 152 DCHECK(position != suggestions->end()) | |
| 153 << "The dismissed suggestion " << suggestion_id | |
| 154 << " has already been removed. Providers must not call OnNewSuggestions" | |
| 155 " in response to DismissSuggestion."; | |
| 156 suggestions->erase(position); | |
| 157 } | 148 } |
| 158 | 149 |
| 159 void ContentSuggestionsService::AddObserver(Observer* observer) { | 150 void ContentSuggestionsService::AddObserver(Observer* observer) { |
| 160 observers_.AddObserver(observer); | 151 observers_.AddObserver(observer); |
| 161 } | 152 } |
| 162 | 153 |
| 163 void ContentSuggestionsService::RemoveObserver(Observer* observer) { | 154 void ContentSuggestionsService::RemoveObserver(Observer* observer) { |
| 164 observers_.RemoveObserver(observer); | 155 observers_.RemoveObserver(observer); |
| 165 } | 156 } |
| 166 | 157 |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 220 providers_by_category_.erase(providers_it); | 211 providers_by_category_.erase(providers_it); |
| 221 categories_.erase( | 212 categories_.erase( |
| 222 std::find(categories_.begin(), categories_.end(), category)); | 213 std::find(categories_.begin(), categories_.end(), category)); |
| 223 } else { | 214 } else { |
| 224 RegisterCategoryIfRequired(provider, category); | 215 RegisterCategoryIfRequired(provider, category); |
| 225 DCHECK_EQ(new_status, provider->GetCategoryStatus(category)); | 216 DCHECK_EQ(new_status, provider->GetCategoryStatus(category)); |
| 226 } | 217 } |
| 227 NotifyCategoryStatusChanged(category); | 218 NotifyCategoryStatusChanged(category); |
| 228 } | 219 } |
| 229 | 220 |
| 221 void ContentSuggestionsService::OnSuggestionInvalidated( |
| 222 ContentSuggestionsProvider* provider, |
| 223 Category category, |
| 224 const std::string& suggestion_id) { |
| 225 RemoveSuggestionByID(category, suggestion_id); |
| 226 FOR_EACH_OBSERVER(Observer, observers_, |
| 227 OnSuggestionInvalidated(category, suggestion_id)); |
| 228 } |
| 229 |
| 230 bool ContentSuggestionsService::RegisterCategoryIfRequired( | 230 bool ContentSuggestionsService::RegisterCategoryIfRequired( |
| 231 ContentSuggestionsProvider* provider, | 231 ContentSuggestionsProvider* provider, |
| 232 Category category) { | 232 Category category) { |
| 233 auto it = providers_by_category_.find(category); | 233 auto it = providers_by_category_.find(category); |
| 234 if (it != providers_by_category_.end()) { | 234 if (it != providers_by_category_.end()) { |
| 235 DCHECK_EQ(it->second, provider); | 235 DCHECK_EQ(it->second, provider); |
| 236 return false; | 236 return false; |
| 237 } | 237 } |
| 238 | 238 |
| 239 providers_by_category_[category] = provider; | 239 providers_by_category_[category] = provider; |
| 240 categories_.push_back(category); | 240 categories_.push_back(category); |
| 241 std::sort(categories_.begin(), categories_.end(), | 241 std::sort(categories_.begin(), categories_.end(), |
| 242 [this](const Category& left, const Category& right) { | 242 [this](const Category& left, const Category& right) { |
| 243 return category_factory_.CompareCategories(left, right); | 243 return category_factory_.CompareCategories(left, right); |
| 244 }); | 244 }); |
| 245 if (IsCategoryStatusAvailable(provider->GetCategoryStatus(category))) { | 245 if (IsCategoryStatusAvailable(provider->GetCategoryStatus(category))) { |
| 246 suggestions_by_category_.insert( | 246 suggestions_by_category_.insert( |
| 247 std::make_pair(category, std::vector<ContentSuggestion>())); | 247 std::make_pair(category, std::vector<ContentSuggestion>())); |
| 248 } | 248 } |
| 249 return true; | 249 return true; |
| 250 } | 250 } |
| 251 | 251 |
| 252 bool ContentSuggestionsService::RemoveSuggestionByID( |
| 253 Category category, |
| 254 const std::string& suggestion_id) { |
| 255 id_category_map_.erase(suggestion_id); |
| 256 std::vector<ContentSuggestion>* suggestions = |
| 257 &suggestions_by_category_[category]; |
| 258 auto position = |
| 259 std::find_if(suggestions->begin(), suggestions->end(), |
| 260 [&suggestion_id](const ContentSuggestion& suggestion) { |
| 261 return suggestion_id == suggestion.id(); |
| 262 }); |
| 263 if (position == suggestions->end()) |
| 264 return false; |
| 265 suggestions->erase(position); |
| 266 return true; |
| 267 } |
| 268 |
| 252 void ContentSuggestionsService::NotifyCategoryStatusChanged(Category category) { | 269 void ContentSuggestionsService::NotifyCategoryStatusChanged(Category category) { |
| 253 FOR_EACH_OBSERVER( | 270 FOR_EACH_OBSERVER( |
| 254 Observer, observers_, | 271 Observer, observers_, |
| 255 OnCategoryStatusChanged(category, GetCategoryStatus(category))); | 272 OnCategoryStatusChanged(category, GetCategoryStatus(category))); |
| 256 } | 273 } |
| 257 | 274 |
| 258 } // namespace ntp_snippets | 275 } // namespace ntp_snippets |
| OLD | NEW |