| 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 #ifndef COMPONENTS_NTP_SNIPPETS_CONTENT_SUGGESTIONS_SERVICE_H_ | 5 #ifndef COMPONENTS_NTP_SNIPPETS_CONTENT_SUGGESTIONS_SERVICE_H_ |
| 6 #define COMPONENTS_NTP_SNIPPETS_CONTENT_SUGGESTIONS_SERVICE_H_ | 6 #define COMPONENTS_NTP_SNIPPETS_CONTENT_SUGGESTIONS_SERVICE_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <memory> | 9 #include <memory> |
| 10 #include <set> | 10 #include <set> |
| 11 #include <string> | 11 #include <string> |
| 12 #include <vector> | 12 #include <vector> |
| 13 | 13 |
| 14 #include "base/callback_forward.h" | 14 #include "base/callback_forward.h" |
| 15 #include "base/observer_list.h" | 15 #include "base/observer_list.h" |
| 16 #include "base/optional.h" | 16 #include "base/optional.h" |
| 17 #include "base/scoped_observer.h" | 17 #include "base/scoped_observer.h" |
| 18 #include "base/time/time.h" | 18 #include "base/time/time.h" |
| 19 #include "components/history/core/browser/history_service.h" | 19 #include "components/history/core/browser/history_service.h" |
| 20 #include "components/history/core/browser/history_service_observer.h" | 20 #include "components/history/core/browser/history_service_observer.h" |
| 21 #include "components/keyed_service/core/keyed_service.h" | 21 #include "components/keyed_service/core/keyed_service.h" |
| 22 #include "components/ntp_snippets/callbacks.h" | 22 #include "components/ntp_snippets/callbacks.h" |
| 23 #include "components/ntp_snippets/category.h" | 23 #include "components/ntp_snippets/category.h" |
| 24 #include "components/ntp_snippets/category_rankers/category_ranker.h" | 24 #include "components/ntp_snippets/category_rankers/category_ranker.h" |
| 25 #include "components/ntp_snippets/category_status.h" | 25 #include "components/ntp_snippets/category_status.h" |
| 26 #include "components/ntp_snippets/content_suggestions_provider.h" | 26 #include "components/ntp_snippets/content_suggestions_provider.h" |
| 27 #include "components/ntp_snippets/remote/remote_suggestions_scheduler.h" |
| 27 #include "components/ntp_snippets/user_classifier.h" | 28 #include "components/ntp_snippets/user_classifier.h" |
| 28 #include "components/signin/core/browser/signin_manager.h" | 29 #include "components/signin/core/browser/signin_manager.h" |
| 29 | 30 |
| 30 class PrefService; | 31 class PrefService; |
| 31 class PrefRegistrySimple; | 32 class PrefRegistrySimple; |
| 32 | 33 |
| 33 namespace ntp_snippets { | 34 namespace ntp_snippets { |
| 34 | 35 |
| 35 class RemoteSuggestionsProvider; | 36 class RemoteSuggestionsProvider; |
| 36 class RemoteSuggestionsScheduler; | |
| 37 | 37 |
| 38 // Retrieves suggestions from a number of ContentSuggestionsProviders and serves | 38 // Retrieves suggestions from a number of ContentSuggestionsProviders and serves |
| 39 // them grouped into categories. There can be at most one provider per category. | 39 // them grouped into categories. There can be at most one provider per category. |
| 40 class ContentSuggestionsService : public KeyedService, | 40 class ContentSuggestionsService : public KeyedService, |
| 41 public ContentSuggestionsProvider::Observer, | 41 public ContentSuggestionsProvider::Observer, |
| 42 public SigninManagerBase::Observer, | 42 public SigninManagerBase::Observer, |
| 43 public history::HistoryServiceObserver { | 43 public history::HistoryServiceObserver { |
| 44 public: | 44 public: |
| 45 class Observer { | 45 class Observer { |
| 46 public: | 46 public: |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 79 | 79 |
| 80 protected: | 80 protected: |
| 81 virtual ~Observer() = default; | 81 virtual ~Observer() = default; |
| 82 }; | 82 }; |
| 83 | 83 |
| 84 enum State { | 84 enum State { |
| 85 ENABLED, | 85 ENABLED, |
| 86 DISABLED, | 86 DISABLED, |
| 87 }; | 87 }; |
| 88 | 88 |
| 89 ContentSuggestionsService(State state, | 89 ContentSuggestionsService( |
| 90 SigninManagerBase* signin_manager, | 90 State state, |
| 91 history::HistoryService* history_service, | 91 SigninManagerBase* signin_manager, // Can be nullptr in unittests. |
| 92 PrefService* pref_service, | 92 history::HistoryService* history_service, // Can be nullptr in unittests. |
| 93 std::unique_ptr<CategoryRanker> category_ranker); | 93 PrefService* pref_service, |
| 94 std::unique_ptr<CategoryRanker> category_ranker, |
| 95 std::unique_ptr<UserClassifier> user_classifier, |
| 96 std::unique_ptr<RemoteSuggestionsScheduler> |
| 97 remote_suggestions_scheduler // Can be nullptr in unittests. |
| 98 ); |
| 94 ~ContentSuggestionsService() override; | 99 ~ContentSuggestionsService() override; |
| 95 | 100 |
| 96 // Inherited from KeyedService. | 101 // Inherited from KeyedService. |
| 97 void Shutdown() override; | 102 void Shutdown() override; |
| 98 | 103 |
| 99 static void RegisterProfilePrefs(PrefRegistrySimple* registry); | 104 static void RegisterProfilePrefs(PrefRegistrySimple* registry); |
| 100 | 105 |
| 101 State state() { return state_; } | 106 State state() { return state_; } |
| 102 | 107 |
| 103 // Gets all categories for which a provider is registered. The categories may | 108 // Gets all categories for which a provider is registered. The categories may |
| (...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 214 // TODO(jkrcal) Move the getter into the scheduler interface (the setter is | 219 // TODO(jkrcal) Move the getter into the scheduler interface (the setter is |
| 215 // then not needed any more). crbug.com/695447 | 220 // then not needed any more). crbug.com/695447 |
| 216 void set_remote_suggestions_provider( | 221 void set_remote_suggestions_provider( |
| 217 RemoteSuggestionsProvider* remote_suggestions_provider) { | 222 RemoteSuggestionsProvider* remote_suggestions_provider) { |
| 218 remote_suggestions_provider_ = remote_suggestions_provider; | 223 remote_suggestions_provider_ = remote_suggestions_provider; |
| 219 } | 224 } |
| 220 RemoteSuggestionsProvider* remote_suggestions_provider_for_debugging() { | 225 RemoteSuggestionsProvider* remote_suggestions_provider_for_debugging() { |
| 221 return remote_suggestions_provider_; | 226 return remote_suggestions_provider_; |
| 222 } | 227 } |
| 223 | 228 |
| 224 // The reference to RemoteSuggestionsScheduler should only be set by the | 229 // The interface is suited for informing about external events that have |
| 225 // factory. The interface is suited for informing about external events that | 230 // influence on scheduling remote fetches. Can be nullptr in tests. |
| 226 // have influence on scheduling remote fetches. | |
| 227 void set_remote_suggestions_scheduler( | |
| 228 ntp_snippets::RemoteSuggestionsScheduler* remote_suggestions_scheduler) { | |
| 229 remote_suggestions_scheduler_ = remote_suggestions_scheduler; | |
| 230 } | |
| 231 RemoteSuggestionsScheduler* remote_suggestions_scheduler() { | 231 RemoteSuggestionsScheduler* remote_suggestions_scheduler() { |
| 232 return remote_suggestions_scheduler_; | 232 return remote_suggestions_scheduler_.get(); |
| 233 } | 233 } |
| 234 | 234 |
| 235 UserClassifier* user_classifier() { return &user_classifier_; } | 235 // Can be nullptr in tests. |
| 236 // TODO(jkrcal): The getter is only used from the bridge and from |
| 237 // snippets-internals. Can we get rid of it with the metrics refactoring? |
| 238 UserClassifier* user_classifier() { return user_classifier_.get(); } |
| 239 |
| 236 CategoryRanker* category_ranker() { return category_ranker_.get(); } | 240 CategoryRanker* category_ranker() { return category_ranker_.get(); } |
| 237 | 241 |
| 238 private: | 242 private: |
| 239 friend class ContentSuggestionsServiceTest; | 243 friend class ContentSuggestionsServiceTest; |
| 240 | 244 |
| 241 // Implementation of ContentSuggestionsProvider::Observer. | 245 // Implementation of ContentSuggestionsProvider::Observer. |
| 242 void OnNewSuggestions(ContentSuggestionsProvider* provider, | 246 void OnNewSuggestions(ContentSuggestionsProvider* provider, |
| 243 Category category, | 247 Category category, |
| 244 std::vector<ContentSuggestion> suggestions) override; | 248 std::vector<ContentSuggestion> suggestions) override; |
| 245 void OnCategoryStatusChanged(ContentSuggestionsProvider* provider, | 249 void OnCategoryStatusChanged(ContentSuggestionsProvider* provider, |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 333 | 337 |
| 334 base::ObserverList<Observer> observers_; | 338 base::ObserverList<Observer> observers_; |
| 335 | 339 |
| 336 const std::vector<ContentSuggestion> no_suggestions_; | 340 const std::vector<ContentSuggestion> no_suggestions_; |
| 337 | 341 |
| 338 // Keep a direct reference to this special provider to redirect debugging | 342 // Keep a direct reference to this special provider to redirect debugging |
| 339 // calls to it. If the RemoteSuggestionsProvider is loaded, it is also present | 343 // calls to it. If the RemoteSuggestionsProvider is loaded, it is also present |
| 340 // in |providers_|, otherwise this is a nullptr. | 344 // in |providers_|, otherwise this is a nullptr. |
| 341 RemoteSuggestionsProvider* remote_suggestions_provider_; | 345 RemoteSuggestionsProvider* remote_suggestions_provider_; |
| 342 | 346 |
| 343 // Interface for informing about external events that have influence on | |
| 344 // scheduling remote fetches. Not owned. | |
| 345 RemoteSuggestionsScheduler* remote_suggestions_scheduler_; | |
| 346 | |
| 347 PrefService* pref_service_; | 347 PrefService* pref_service_; |
| 348 | 348 |
| 349 UserClassifier user_classifier_; | 349 // Interface for informing about external events that have influence on |
| 350 // scheduling remote fetches. |
| 351 std::unique_ptr<RemoteSuggestionsScheduler> remote_suggestions_scheduler_; |
| 352 |
| 353 // Classifies the user on the basis of long-term user interactions. |
| 354 std::unique_ptr<UserClassifier> user_classifier_; |
| 350 | 355 |
| 351 // Provides order for categories. | 356 // Provides order for categories. |
| 352 std::unique_ptr<CategoryRanker> category_ranker_; | 357 std::unique_ptr<CategoryRanker> category_ranker_; |
| 353 | 358 |
| 354 DISALLOW_COPY_AND_ASSIGN(ContentSuggestionsService); | 359 DISALLOW_COPY_AND_ASSIGN(ContentSuggestionsService); |
| 355 }; | 360 }; |
| 356 | 361 |
| 357 } // namespace ntp_snippets | 362 } // namespace ntp_snippets |
| 358 | 363 |
| 359 #endif // COMPONENTS_NTP_SNIPPETS_CONTENT_SUGGESTIONS_SERVICE_H_ | 364 #endif // COMPONENTS_NTP_SNIPPETS_CONTENT_SUGGESTIONS_SERVICE_H_ |
| OLD | NEW |