| 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 12 matching lines...) Expand all Loading... |
| 23 | 23 |
| 24 ContentSuggestionsService::ContentSuggestionsService( | 24 ContentSuggestionsService::ContentSuggestionsService( |
| 25 State state, | 25 State state, |
| 26 SigninManagerBase* signin_manager, | 26 SigninManagerBase* signin_manager, |
| 27 history::HistoryService* history_service, | 27 history::HistoryService* history_service, |
| 28 PrefService* pref_service, | 28 PrefService* pref_service, |
| 29 std::unique_ptr<CategoryRanker> category_ranker) | 29 std::unique_ptr<CategoryRanker> category_ranker) |
| 30 : state_(state), | 30 : state_(state), |
| 31 signin_observer_(this), | 31 signin_observer_(this), |
| 32 history_service_observer_(this), | 32 history_service_observer_(this), |
| 33 ntp_snippets_service_(nullptr), | 33 remote_suggestions_provider_(nullptr), |
| 34 remote_suggestions_scheduler_(nullptr), |
| 34 pref_service_(pref_service), | 35 pref_service_(pref_service), |
| 35 user_classifier_(pref_service), | 36 user_classifier_(pref_service), |
| 36 category_ranker_(std::move(category_ranker)) { | 37 category_ranker_(std::move(category_ranker)) { |
| 37 // Can be null in tests. | 38 // Can be null in tests. |
| 38 if (signin_manager) { | 39 if (signin_manager) { |
| 39 signin_observer_.Add(signin_manager); | 40 signin_observer_.Add(signin_manager); |
| 40 } | 41 } |
| 41 | 42 |
| 42 if (history_service) { | 43 if (history_service) { |
| 43 history_service_observer_.Add(history_service); | 44 history_service_observer_.Add(history_service); |
| 44 } | 45 } |
| 45 | 46 |
| 46 RestoreDismissedCategoriesFromPrefs(); | 47 RestoreDismissedCategoriesFromPrefs(); |
| 47 } | 48 } |
| 48 | 49 |
| 49 ContentSuggestionsService::~ContentSuggestionsService() = default; | 50 ContentSuggestionsService::~ContentSuggestionsService() = default; |
| 50 | 51 |
| 51 void ContentSuggestionsService::Shutdown() { | 52 void ContentSuggestionsService::Shutdown() { |
| 52 ntp_snippets_service_ = nullptr; | 53 remote_suggestions_provider_ = nullptr; |
| 54 remote_suggestions_scheduler_ = nullptr; |
| 53 suggestions_by_category_.clear(); | 55 suggestions_by_category_.clear(); |
| 54 providers_by_category_.clear(); | 56 providers_by_category_.clear(); |
| 55 categories_.clear(); | 57 categories_.clear(); |
| 56 providers_.clear(); | 58 providers_.clear(); |
| 57 state_ = State::DISABLED; | 59 state_ = State::DISABLED; |
| 58 for (Observer& observer : observers_) { | 60 for (Observer& observer : observers_) { |
| 59 observer.ContentSuggestionsServiceShutdown(); | 61 observer.ContentSuggestionsServiceShutdown(); |
| 60 } | 62 } |
| 61 } | 63 } |
| 62 | 64 |
| (...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 218 const std::set<std::string>& known_suggestion_ids, | 220 const std::set<std::string>& known_suggestion_ids, |
| 219 const FetchDoneCallback& callback) { | 221 const FetchDoneCallback& callback) { |
| 220 auto providers_it = providers_by_category_.find(category); | 222 auto providers_it = providers_by_category_.find(category); |
| 221 if (providers_it == providers_by_category_.end()) { | 223 if (providers_it == providers_by_category_.end()) { |
| 222 return; | 224 return; |
| 223 } | 225 } |
| 224 | 226 |
| 225 providers_it->second->Fetch(category, known_suggestion_ids, callback); | 227 providers_it->second->Fetch(category, known_suggestion_ids, callback); |
| 226 } | 228 } |
| 227 | 229 |
| 230 void ContentSuggestionsService::ReloadSuggestions() { |
| 231 for (const auto& provider : providers_) { |
| 232 provider->ReloadSuggestions(); |
| 233 } |
| 234 } |
| 235 |
| 228 //////////////////////////////////////////////////////////////////////////////// | 236 //////////////////////////////////////////////////////////////////////////////// |
| 229 // Private methods | 237 // Private methods |
| 230 | 238 |
| 231 void ContentSuggestionsService::OnNewSuggestions( | 239 void ContentSuggestionsService::OnNewSuggestions( |
| 232 ContentSuggestionsProvider* provider, | 240 ContentSuggestionsProvider* provider, |
| 233 Category category, | 241 Category category, |
| 234 std::vector<ContentSuggestion> suggestions) { | 242 std::vector<ContentSuggestion> suggestions) { |
| 235 // Providers shouldn't call this when they're in a non-available state. | 243 // Providers shouldn't call this when they're in a non-available state. |
| 236 DCHECK( | 244 DCHECK( |
| 237 IsCategoryStatusInitOrAvailable(provider->GetCategoryStatus(category))); | 245 IsCategoryStatusInitOrAvailable(provider->GetCategoryStatus(category))); |
| (...skipping 257 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 495 void ContentSuggestionsService::StoreDismissedCategoriesToPrefs() { | 503 void ContentSuggestionsService::StoreDismissedCategoriesToPrefs() { |
| 496 base::ListValue list; | 504 base::ListValue list; |
| 497 for (const auto& category_provider_pair : dismissed_providers_by_category_) { | 505 for (const auto& category_provider_pair : dismissed_providers_by_category_) { |
| 498 list.AppendInteger(category_provider_pair.first.id()); | 506 list.AppendInteger(category_provider_pair.first.id()); |
| 499 } | 507 } |
| 500 | 508 |
| 501 pref_service_->Set(prefs::kDismissedCategories, list); | 509 pref_service_->Set(prefs::kDismissedCategories, list); |
| 502 } | 510 } |
| 503 | 511 |
| 504 } // namespace ntp_snippets | 512 } // namespace ntp_snippets |
| OLD | NEW |