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 #include <set> | 9 #include <set> |
10 #include <utility> | 10 #include <utility> |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
143 DCHECK(removed) << "The dismissed suggestion " << suggestion_id | 143 DCHECK(removed) << "The dismissed suggestion " << suggestion_id |
144 << " has already been removed. Providers must not call" | 144 << " has already been removed. Providers must not call" |
145 << " OnNewSuggestions in response to DismissSuggestion."; | 145 << " OnNewSuggestions in response to DismissSuggestion."; |
146 } | 146 } |
147 | 147 |
148 void ContentSuggestionsService::DismissCategory(Category category) { | 148 void ContentSuggestionsService::DismissCategory(Category category) { |
149 auto providers_it = providers_by_category_.find(category); | 149 auto providers_it = providers_by_category_.find(category); |
150 if (providers_it == providers_by_category_.end()) | 150 if (providers_it == providers_by_category_.end()) |
151 return; | 151 return; |
152 | 152 |
153 suggestions_by_category_[category].clear(); | |
154 dismissed_providers_by_category_[providers_it->first] = providers_it->second; | |
153 providers_by_category_.erase(providers_it); | 155 providers_by_category_.erase(providers_it); |
154 categories_.erase( | 156 categories_.erase( |
155 std::find(categories_.begin(), categories_.end(), category)); | 157 std::find(categories_.begin(), categories_.end(), category)); |
156 } | 158 } |
157 | 159 |
160 void ContentSuggestionsService::RestoreDismissedCategories() { | |
161 // Make a copy as the original will be modified during iteration. | |
162 auto dismissed_providers_by_category_copy = dismissed_providers_by_category_; | |
163 for (const auto& category_provider_pair : | |
164 dismissed_providers_by_category_copy) { | |
165 RegisterCategoryIfRequired(category_provider_pair.second, | |
166 category_provider_pair.first); | |
167 } | |
168 DCHECK(dismissed_providers_by_category_.empty()); | |
169 dismissed_providers_by_category_.clear(); | |
Marc Treib
2016/10/11 15:29:24
nit: No need for this - we don't handle DCHECK fai
Michael van Ouwerkerk
2016/10/11 15:37:43
Done.
| |
170 } | |
171 | |
158 void ContentSuggestionsService::AddObserver(Observer* observer) { | 172 void ContentSuggestionsService::AddObserver(Observer* observer) { |
159 observers_.AddObserver(observer); | 173 observers_.AddObserver(observer); |
160 } | 174 } |
161 | 175 |
162 void ContentSuggestionsService::RemoveObserver(Observer* observer) { | 176 void ContentSuggestionsService::RemoveObserver(Observer* observer) { |
163 observers_.RemoveObserver(observer); | 177 observers_.RemoveObserver(observer); |
164 } | 178 } |
165 | 179 |
166 void ContentSuggestionsService::RegisterProvider( | 180 void ContentSuggestionsService::RegisterProvider( |
167 std::unique_ptr<ContentSuggestionsProvider> provider) { | 181 std::unique_ptr<ContentSuggestionsProvider> provider) { |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
267 | 281 |
268 bool ContentSuggestionsService::RegisterCategoryIfRequired( | 282 bool ContentSuggestionsService::RegisterCategoryIfRequired( |
269 ContentSuggestionsProvider* provider, | 283 ContentSuggestionsProvider* provider, |
270 Category category) { | 284 Category category) { |
271 auto it = providers_by_category_.find(category); | 285 auto it = providers_by_category_.find(category); |
272 if (it != providers_by_category_.end()) { | 286 if (it != providers_by_category_.end()) { |
273 DCHECK_EQ(it->second, provider); | 287 DCHECK_EQ(it->second, provider); |
274 return false; | 288 return false; |
275 } | 289 } |
276 | 290 |
291 auto dismissed_it = dismissed_providers_by_category_.find(category); | |
292 if (dismissed_it != dismissed_providers_by_category_.end()) { | |
293 DCHECK_EQ(dismissed_it->second, provider); | |
294 dismissed_providers_by_category_.erase(dismissed_it); | |
295 } | |
296 | |
277 providers_by_category_[category] = provider; | 297 providers_by_category_[category] = provider; |
278 categories_.push_back(category); | 298 categories_.push_back(category); |
279 SortCategories(); | 299 SortCategories(); |
280 if (IsCategoryStatusAvailable(provider->GetCategoryStatus(category))) { | 300 if (IsCategoryStatusAvailable(provider->GetCategoryStatus(category))) { |
281 suggestions_by_category_.insert( | 301 suggestions_by_category_.insert( |
282 std::make_pair(category, std::vector<ContentSuggestion>())); | 302 std::make_pair(category, std::vector<ContentSuggestion>())); |
283 } | 303 } |
284 return true; | 304 return true; |
285 } | 305 } |
286 | 306 |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
325 if (left.IsKnownCategory(KnownCategories::BOOKMARKS)) | 345 if (left.IsKnownCategory(KnownCategories::BOOKMARKS)) |
326 return false; | 346 return false; |
327 if (right.IsKnownCategory(KnownCategories::BOOKMARKS)) | 347 if (right.IsKnownCategory(KnownCategories::BOOKMARKS)) |
328 return true; | 348 return true; |
329 } | 349 } |
330 return category_factory_.CompareCategories(left, right); | 350 return category_factory_.CompareCategories(left, right); |
331 }); | 351 }); |
332 } | 352 } |
333 | 353 |
334 } // namespace ntp_snippets | 354 } // namespace ntp_snippets |
OLD | NEW |