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

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

Issue 2774663002: [Remote suggestions] Refactor the scheduler (Closed)
Patch Set: Marc's comments #2 Created 3 years, 8 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 #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
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>
96 user_classifier, // Can be nullptr in unittests.
Marc Treib 2017/03/27 14:27:21 Would "Can be null in tests." make it fit on the p
jkrcal 2017/03/29 10:55:57 Removed, anyway.
97 std::unique_ptr<RemoteSuggestionsScheduler>
98 remote_suggestions_scheduler // Can be nullptr in unittests.
99 );
94 ~ContentSuggestionsService() override; 100 ~ContentSuggestionsService() override;
95 101
96 // Inherited from KeyedService. 102 // Inherited from KeyedService.
97 void Shutdown() override; 103 void Shutdown() override;
98 104
99 static void RegisterProfilePrefs(PrefRegistrySimple* registry); 105 static void RegisterProfilePrefs(PrefRegistrySimple* registry);
100 106
101 State state() { return state_; } 107 State state() { return state_; }
102 108
103 // Gets all categories for which a provider is registered. The categories may 109 // Gets all categories for which a provider is registered. The categories may
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after
214 // TODO(jkrcal) Move the getter into the scheduler interface (the setter is 220 // TODO(jkrcal) Move the getter into the scheduler interface (the setter is
215 // then not needed any more). crbug.com/695447 221 // then not needed any more). crbug.com/695447
216 void set_remote_suggestions_provider( 222 void set_remote_suggestions_provider(
217 RemoteSuggestionsProvider* remote_suggestions_provider) { 223 RemoteSuggestionsProvider* remote_suggestions_provider) {
218 remote_suggestions_provider_ = remote_suggestions_provider; 224 remote_suggestions_provider_ = remote_suggestions_provider;
219 } 225 }
220 RemoteSuggestionsProvider* remote_suggestions_provider_for_debugging() { 226 RemoteSuggestionsProvider* remote_suggestions_provider_for_debugging() {
221 return remote_suggestions_provider_; 227 return remote_suggestions_provider_;
222 } 228 }
223 229
224 // The reference to RemoteSuggestionsScheduler should only be set by the 230 // The interface is suited for informing about external events that have
225 // factory. The interface is suited for informing about external events that 231 // 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() { 232 RemoteSuggestionsScheduler* remote_suggestions_scheduler() {
232 return remote_suggestions_scheduler_; 233 return remote_suggestions_scheduler_.get();
233 } 234 }
234 235
235 UserClassifier* user_classifier() { return &user_classifier_; } 236 // Can be nullptr in tests.
237 // TODO(jkrcal): The getter is only used from the bridge and from
238 // snippets-internals. Can we get rid of it with the metrics refactoring?
239 UserClassifier* user_classifier() { return user_classifier_.get(); }
240
236 CategoryRanker* category_ranker() { return category_ranker_.get(); } 241 CategoryRanker* category_ranker() { return category_ranker_.get(); }
237 242
238 private: 243 private:
239 friend class ContentSuggestionsServiceTest; 244 friend class ContentSuggestionsServiceTest;
240 245
241 // Implementation of ContentSuggestionsProvider::Observer. 246 // Implementation of ContentSuggestionsProvider::Observer.
242 void OnNewSuggestions(ContentSuggestionsProvider* provider, 247 void OnNewSuggestions(ContentSuggestionsProvider* provider,
243 Category category, 248 Category category,
244 std::vector<ContentSuggestion> suggestions) override; 249 std::vector<ContentSuggestion> suggestions) override;
245 void OnCategoryStatusChanged(ContentSuggestionsProvider* provider, 250 void OnCategoryStatusChanged(ContentSuggestionsProvider* provider,
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
333 338
334 base::ObserverList<Observer> observers_; 339 base::ObserverList<Observer> observers_;
335 340
336 const std::vector<ContentSuggestion> no_suggestions_; 341 const std::vector<ContentSuggestion> no_suggestions_;
337 342
338 // Keep a direct reference to this special provider to redirect debugging 343 // Keep a direct reference to this special provider to redirect debugging
339 // calls to it. If the RemoteSuggestionsProvider is loaded, it is also present 344 // calls to it. If the RemoteSuggestionsProvider is loaded, it is also present
340 // in |providers_|, otherwise this is a nullptr. 345 // in |providers_|, otherwise this is a nullptr.
341 RemoteSuggestionsProvider* remote_suggestions_provider_; 346 RemoteSuggestionsProvider* remote_suggestions_provider_;
342 347
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_; 348 PrefService* pref_service_;
348 349
349 UserClassifier user_classifier_; 350 // Interface for informing about external events that have influence on
351 // scheduling remote fetches.
352 std::unique_ptr<RemoteSuggestionsScheduler> remote_suggestions_scheduler_;
353
354 // Classifies the user on the basis of long-term user interactions.
355 std::unique_ptr<UserClassifier> user_classifier_;
350 356
351 // Provides order for categories. 357 // Provides order for categories.
352 std::unique_ptr<CategoryRanker> category_ranker_; 358 std::unique_ptr<CategoryRanker> category_ranker_;
353 359
354 DISALLOW_COPY_AND_ASSIGN(ContentSuggestionsService); 360 DISALLOW_COPY_AND_ASSIGN(ContentSuggestionsService);
355 }; 361 };
356 362
357 } // namespace ntp_snippets 363 } // namespace ntp_snippets
358 364
359 #endif // COMPONENTS_NTP_SNIPPETS_CONTENT_SUGGESTIONS_SERVICE_H_ 365 #endif // COMPONENTS_NTP_SNIPPETS_CONTENT_SUGGESTIONS_SERVICE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698