Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(59)

Side by Side Diff: components/ntp_snippets/content_suggestions_service.cc

Issue 2244793002: Remove deleted offline page suggestions from opened NTPs (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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
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 if (RemoveSuggestionByID(category, suggestion_id)) {
226 FOR_EACH_OBSERVER(Observer, observers_,
Marc Treib 2016/08/16 09:03:53 Wouldn't we want to send the invalidation anyway?
Philipp Keck 2016/08/16 10:54:40 Yes, right. Done.
227 OnSuggestionInvalidated(category, suggestion_id));
228 }
229 }
230
230 bool ContentSuggestionsService::RegisterCategoryIfRequired( 231 bool ContentSuggestionsService::RegisterCategoryIfRequired(
231 ContentSuggestionsProvider* provider, 232 ContentSuggestionsProvider* provider,
232 Category category) { 233 Category category) {
233 auto it = providers_by_category_.find(category); 234 auto it = providers_by_category_.find(category);
234 if (it != providers_by_category_.end()) { 235 if (it != providers_by_category_.end()) {
235 DCHECK_EQ(it->second, provider); 236 DCHECK_EQ(it->second, provider);
236 return false; 237 return false;
237 } 238 }
238 239
239 providers_by_category_[category] = provider; 240 providers_by_category_[category] = provider;
240 categories_.push_back(category); 241 categories_.push_back(category);
241 std::sort(categories_.begin(), categories_.end(), 242 std::sort(categories_.begin(), categories_.end(),
242 [this](const Category& left, const Category& right) { 243 [this](const Category& left, const Category& right) {
243 return category_factory_.CompareCategories(left, right); 244 return category_factory_.CompareCategories(left, right);
244 }); 245 });
245 if (IsCategoryStatusAvailable(provider->GetCategoryStatus(category))) { 246 if (IsCategoryStatusAvailable(provider->GetCategoryStatus(category))) {
246 suggestions_by_category_.insert( 247 suggestions_by_category_.insert(
247 std::make_pair(category, std::vector<ContentSuggestion>())); 248 std::make_pair(category, std::vector<ContentSuggestion>()));
248 } 249 }
249 return true; 250 return true;
250 } 251 }
251 252
253 bool ContentSuggestionsService::RemoveSuggestionByID(
254 Category category,
255 const std::string& suggestion_id) {
256 id_category_map_.erase(suggestion_id);
257 std::vector<ContentSuggestion>* suggestions =
258 &suggestions_by_category_[category];
259 auto position =
260 std::find_if(suggestions->begin(), suggestions->end(),
261 [&suggestion_id](const ContentSuggestion& suggestion) {
262 return suggestion_id == suggestion.id();
263 });
264 if (position == suggestions->end())
265 return false;
266 suggestions->erase(position);
267 return true;
268 }
269
252 void ContentSuggestionsService::NotifyCategoryStatusChanged(Category category) { 270 void ContentSuggestionsService::NotifyCategoryStatusChanged(Category category) {
253 FOR_EACH_OBSERVER( 271 FOR_EACH_OBSERVER(
254 Observer, observers_, 272 Observer, observers_,
255 OnCategoryStatusChanged(category, GetCategoryStatus(category))); 273 OnCategoryStatusChanged(category, GetCategoryStatus(category)));
256 } 274 }
257 275
258 } // namespace ntp_snippets 276 } // namespace ntp_snippets
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698