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