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 28 matching lines...) Expand all Loading... |
39 | 39 |
40 ContentSuggestionsService::~ContentSuggestionsService() = default; | 40 ContentSuggestionsService::~ContentSuggestionsService() = default; |
41 | 41 |
42 void ContentSuggestionsService::Shutdown() { | 42 void ContentSuggestionsService::Shutdown() { |
43 ntp_snippets_service_ = nullptr; | 43 ntp_snippets_service_ = nullptr; |
44 suggestions_by_category_.clear(); | 44 suggestions_by_category_.clear(); |
45 providers_by_category_.clear(); | 45 providers_by_category_.clear(); |
46 categories_.clear(); | 46 categories_.clear(); |
47 providers_.clear(); | 47 providers_.clear(); |
48 state_ = State::DISABLED; | 48 state_ = State::DISABLED; |
49 FOR_EACH_OBSERVER(Observer, observers_, ContentSuggestionsServiceShutdown()); | 49 for (Observer& observer : observers_) |
| 50 observer.ContentSuggestionsServiceShutdown(); |
50 } | 51 } |
51 | 52 |
52 // static | 53 // static |
53 void ContentSuggestionsService::RegisterProfilePrefs( | 54 void ContentSuggestionsService::RegisterProfilePrefs( |
54 PrefRegistrySimple* registry) { | 55 PrefRegistrySimple* registry) { |
55 registry->RegisterListPref(prefs::kDismissedCategories); | 56 registry->RegisterListPref(prefs::kDismissedCategories); |
56 } | 57 } |
57 | 58 |
58 CategoryStatus ContentSuggestionsService::GetCategoryStatus( | 59 CategoryStatus ContentSuggestionsService::GetCategoryStatus( |
59 Category category) const { | 60 Category category) const { |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
105 for (const auto& provider : providers_) { | 106 for (const auto& provider : providers_) { |
106 provider->ClearHistory(begin, end, filter); | 107 provider->ClearHistory(begin, end, filter); |
107 } | 108 } |
108 } | 109 } |
109 | 110 |
110 void ContentSuggestionsService::ClearAllCachedSuggestions() { | 111 void ContentSuggestionsService::ClearAllCachedSuggestions() { |
111 suggestions_by_category_.clear(); | 112 suggestions_by_category_.clear(); |
112 for (const auto& category_provider_pair : providers_by_category_) { | 113 for (const auto& category_provider_pair : providers_by_category_) { |
113 category_provider_pair.second->ClearCachedSuggestions( | 114 category_provider_pair.second->ClearCachedSuggestions( |
114 category_provider_pair.first); | 115 category_provider_pair.first); |
115 FOR_EACH_OBSERVER(Observer, observers_, | 116 for (Observer& observer : observers_) |
116 OnNewSuggestions(category_provider_pair.first)); | 117 observer.OnNewSuggestions(category_provider_pair.first); |
117 } | 118 } |
118 } | 119 } |
119 | 120 |
120 void ContentSuggestionsService::ClearCachedSuggestions(Category category) { | 121 void ContentSuggestionsService::ClearCachedSuggestions(Category category) { |
121 suggestions_by_category_[category].clear(); | 122 suggestions_by_category_[category].clear(); |
122 auto iterator = providers_by_category_.find(category); | 123 auto iterator = providers_by_category_.find(category); |
123 if (iterator != providers_by_category_.end()) | 124 if (iterator != providers_by_category_.end()) |
124 iterator->second->ClearCachedSuggestions(category); | 125 iterator->second->ClearCachedSuggestions(category); |
125 } | 126 } |
126 | 127 |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
221 } | 222 } |
222 | 223 |
223 if (!IsCategoryStatusAvailable(provider->GetCategoryStatus(category))) { | 224 if (!IsCategoryStatusAvailable(provider->GetCategoryStatus(category))) { |
224 // A provider shouldn't send us suggestions while it's not available. | 225 // A provider shouldn't send us suggestions while it's not available. |
225 DCHECK(suggestions.empty()); | 226 DCHECK(suggestions.empty()); |
226 return; | 227 return; |
227 } | 228 } |
228 | 229 |
229 suggestions_by_category_[category] = std::move(suggestions); | 230 suggestions_by_category_[category] = std::move(suggestions); |
230 | 231 |
231 FOR_EACH_OBSERVER(Observer, observers_, OnNewSuggestions(category)); | 232 for (Observer& observer : observers_) |
| 233 observer.OnNewSuggestions(category); |
232 } | 234 } |
233 | 235 |
234 void ContentSuggestionsService::OnCategoryStatusChanged( | 236 void ContentSuggestionsService::OnCategoryStatusChanged( |
235 ContentSuggestionsProvider* provider, | 237 ContentSuggestionsProvider* provider, |
236 Category category, | 238 Category category, |
237 CategoryStatus new_status) { | 239 CategoryStatus new_status) { |
238 if (new_status == CategoryStatus::NOT_PROVIDED) { | 240 if (new_status == CategoryStatus::NOT_PROVIDED) { |
239 UnregisterCategory(category, provider); | 241 UnregisterCategory(category, provider); |
240 } else { | 242 } else { |
241 if (!IsCategoryStatusAvailable(new_status)) | 243 if (!IsCategoryStatusAvailable(new_status)) |
242 suggestions_by_category_.erase(category); | 244 suggestions_by_category_.erase(category); |
243 TryRegisterProviderForCategory(provider, category); | 245 TryRegisterProviderForCategory(provider, category); |
244 DCHECK_EQ(new_status, provider->GetCategoryStatus(category)); | 246 DCHECK_EQ(new_status, provider->GetCategoryStatus(category)); |
245 } | 247 } |
246 | 248 |
247 if (!IsCategoryDismissed(category)) | 249 if (!IsCategoryDismissed(category)) |
248 NotifyCategoryStatusChanged(category); | 250 NotifyCategoryStatusChanged(category); |
249 } | 251 } |
250 | 252 |
251 void ContentSuggestionsService::OnSuggestionInvalidated( | 253 void ContentSuggestionsService::OnSuggestionInvalidated( |
252 ContentSuggestionsProvider* provider, | 254 ContentSuggestionsProvider* provider, |
253 const ContentSuggestion::ID& suggestion_id) { | 255 const ContentSuggestion::ID& suggestion_id) { |
254 RemoveSuggestionByID(suggestion_id); | 256 RemoveSuggestionByID(suggestion_id); |
255 FOR_EACH_OBSERVER(Observer, observers_, | 257 for (Observer& observer : observers_) |
256 OnSuggestionInvalidated(suggestion_id)); | 258 observer.OnSuggestionInvalidated(suggestion_id); |
257 } | 259 } |
258 | 260 |
259 // history::HistoryServiceObserver implementation. | 261 // history::HistoryServiceObserver implementation. |
260 void ContentSuggestionsService::OnURLsDeleted( | 262 void ContentSuggestionsService::OnURLsDeleted( |
261 history::HistoryService* history_service, | 263 history::HistoryService* history_service, |
262 bool all_history, | 264 bool all_history, |
263 bool expired, | 265 bool expired, |
264 const history::URLRows& deleted_rows, | 266 const history::URLRows& deleted_rows, |
265 const std::set<GURL>& favicon_urls) { | 267 const std::set<GURL>& favicon_urls) { |
266 // We don't care about expired entries. | 268 // We don't care about expired entries. |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
369 return suggestion_id == suggestion.id(); | 371 return suggestion_id == suggestion.id(); |
370 }); | 372 }); |
371 if (position == suggestions->end()) | 373 if (position == suggestions->end()) |
372 return false; | 374 return false; |
373 suggestions->erase(position); | 375 suggestions->erase(position); |
374 | 376 |
375 return true; | 377 return true; |
376 } | 378 } |
377 | 379 |
378 void ContentSuggestionsService::NotifyCategoryStatusChanged(Category category) { | 380 void ContentSuggestionsService::NotifyCategoryStatusChanged(Category category) { |
379 FOR_EACH_OBSERVER( | 381 for (Observer& observer : observers_) |
380 Observer, observers_, | 382 observer.OnCategoryStatusChanged(category, GetCategoryStatus(category)); |
381 OnCategoryStatusChanged(category, GetCategoryStatus(category))); | |
382 } | 383 } |
383 | 384 |
384 void ContentSuggestionsService::SortCategories() { | 385 void ContentSuggestionsService::SortCategories() { |
385 std::sort(categories_.begin(), categories_.end(), | 386 std::sort(categories_.begin(), categories_.end(), |
386 [this](const Category& left, const Category& right) { | 387 [this](const Category& left, const Category& right) { |
387 return category_factory_.CompareCategories(left, right); | 388 return category_factory_.CompareCategories(left, right); |
388 }); | 389 }); |
389 } | 390 } |
390 | 391 |
391 bool ContentSuggestionsService::IsCategoryDismissed(Category category) const { | 392 bool ContentSuggestionsService::IsCategoryDismissed(Category category) const { |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
428 void ContentSuggestionsService::StoreDismissedCategoriesToPrefs() { | 429 void ContentSuggestionsService::StoreDismissedCategoriesToPrefs() { |
429 base::ListValue list; | 430 base::ListValue list; |
430 for (const auto& category_provider_pair : dismissed_providers_by_category_) { | 431 for (const auto& category_provider_pair : dismissed_providers_by_category_) { |
431 list.AppendInteger(category_provider_pair.first.id()); | 432 list.AppendInteger(category_provider_pair.first.id()); |
432 } | 433 } |
433 | 434 |
434 pref_service_->Set(prefs::kDismissedCategories, list); | 435 pref_service_->Set(prefs::kDismissedCategories, list); |
435 } | 436 } |
436 | 437 |
437 } // namespace ntp_snippets | 438 } // namespace ntp_snippets |
OLD | NEW |