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

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

Issue 2557363002: [NTP Snippets] Refactor background scheduling for remote suggestions (Closed)
Patch Set: Tim's comments and splitting RemoteSuggestionsProvider and RemoteSuggestionsProviderImpl Created 4 years 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>
(...skipping 14 matching lines...) Expand all
25 #include "components/ntp_snippets/content_suggestions_provider.h" 25 #include "components/ntp_snippets/content_suggestions_provider.h"
26 #include "components/ntp_snippets/user_classifier.h" 26 #include "components/ntp_snippets/user_classifier.h"
27 #include "components/signin/core/browser/signin_manager.h" 27 #include "components/signin/core/browser/signin_manager.h"
28 28
29 class PrefService; 29 class PrefService;
30 class PrefRegistrySimple; 30 class PrefRegistrySimple;
31 31
32 namespace ntp_snippets { 32 namespace ntp_snippets {
33 33
34 class RemoteSuggestionsProvider; 34 class RemoteSuggestionsProvider;
35 class RemoteSuggestionsScheduler;
35 36
36 // Retrieves suggestions from a number of ContentSuggestionsProviders and serves 37 // Retrieves suggestions from a number of ContentSuggestionsProviders and serves
37 // them grouped into categories. There can be at most one provider per category. 38 // them grouped into categories. There can be at most one provider per category.
38 class ContentSuggestionsService : public KeyedService, 39 class ContentSuggestionsService : public KeyedService,
39 public ContentSuggestionsProvider::Observer, 40 public ContentSuggestionsProvider::Observer,
40 public SigninManagerBase::Observer, 41 public SigninManagerBase::Observer,
41 public history::HistoryServiceObserver { 42 public history::HistoryServiceObserver {
42 public: 43 public:
43 class Observer { 44 class Observer {
44 public: 45 public:
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
133 // Restores all dismissed categories. 134 // Restores all dismissed categories.
134 // This will not trigger an update through the observers. 135 // This will not trigger an update through the observers.
135 void RestoreDismissedCategories(); 136 void RestoreDismissedCategories();
136 137
137 // Returns whether |category| is dismissed. 138 // Returns whether |category| is dismissed.
138 bool IsCategoryDismissed(Category category) const; 139 bool IsCategoryDismissed(Category category) const;
139 140
140 // Fetches additional contents for the given |category|. If the fetch was 141 // Fetches additional contents for the given |category|. If the fetch was
141 // completed, the given |callback| is called with the updated content. 142 // completed, the given |callback| is called with the updated content.
142 // This includes new and old data. 143 // This includes new and old data.
144 // TODO(jkrcal): Consider either renaming this to FetchMore or unify the ways
145 // to get suggestions to just this async Fetch() API.
143 void Fetch(const Category& category, 146 void Fetch(const Category& category,
144 const std::set<std::string>& known_suggestion_ids, 147 const std::set<std::string>& known_suggestion_ids,
145 const FetchDoneCallback& callback); 148 const FetchDoneCallback& callback);
146 149
150 // Reloads suggestions from all categories, from all providers. If a provider
151 // naturally has some ability to generate fresh suggestions, it may provide a
152 // completely new set of suggestions. If the provider has no ability to
153 // generate fresh suggestions on demand, it may only fill in any vacant space
154 // by suggestions that were previously not included due to space limits (there
155 // may be vacant space because of the user dismissing suggestions in the
156 // meantime).
157 void ReloadSuggestions();
158
147 // Observer accessors. 159 // Observer accessors.
148 void AddObserver(Observer* observer); 160 void AddObserver(Observer* observer);
149 void RemoveObserver(Observer* observer); 161 void RemoveObserver(Observer* observer);
150 162
151 // Registers a new ContentSuggestionsProvider. It must be ensured that at most 163 // Registers a new ContentSuggestionsProvider. It must be ensured that at most
152 // one provider is registered for every category and that this method is 164 // one provider is registered for every category and that this method is
153 // called only once per provider. 165 // called only once per provider.
154 void RegisterProvider(std::unique_ptr<ContentSuggestionsProvider> provider); 166 void RegisterProvider(std::unique_ptr<ContentSuggestionsProvider> provider);
155 167
156 // Removes history from the specified time range where the URL matches the 168 // Removes history from the specified time range where the URL matches the
(...skipping 29 matching lines...) Expand all
186 198
187 // Only for debugging use through the internals page. Some providers 199 // Only for debugging use through the internals page. Some providers
188 // internally store a list of dismissed suggestions to prevent them from 200 // internally store a list of dismissed suggestions to prevent them from
189 // reappearing. This function clears all suggestions of the given |category| 201 // reappearing. This function clears all suggestions of the given |category|
190 // from such lists, making dismissed suggestions reappear (if the provider 202 // from such lists, making dismissed suggestions reappear (if the provider
191 // supports it). 203 // supports it).
192 void ClearDismissedSuggestionsForDebugging(Category category); 204 void ClearDismissedSuggestionsForDebugging(Category category);
193 205
194 CategoryFactory* category_factory() { return &category_factory_; } 206 CategoryFactory* category_factory() { return &category_factory_; }
195 207
196 // The reference to the RemoteSuggestionsProvider provider should only be set 208 // The reference to the RemoteSuggestionsProvider provider should
197 // by the factory and only be used for scheduling, periodic fetching and 209 // only be set by the factory and only used for debugging.
198 // debugging. 210 void set_remote_suggestions_provider(
199 RemoteSuggestionsProvider* ntp_snippets_service() { 211 RemoteSuggestionsProvider* remote_suggestions_provider) {
200 return ntp_snippets_service_; 212 remote_suggestions_provider_ = remote_suggestions_provider;
201 } 213 }
202 void set_ntp_snippets_service( 214 RemoteSuggestionsProvider* remote_suggestions_provider_for_debugging() {
203 RemoteSuggestionsProvider* ntp_snippets_service) { 215 return remote_suggestions_provider_;
204 ntp_snippets_service_ = ntp_snippets_service; 216 }
217
218 // The reference to RemoteSuggestionsScheduler should only be set by the
219 // factory. The interface is suited for informing about external events that
220 // have influence on scheduling remote fetches.
221 void set_remote_suggestions_scheduler(
222 ntp_snippets::RemoteSuggestionsScheduler* remote_suggestions_scheduler) {
223 remote_suggestions_scheduler_ = remote_suggestions_scheduler;
224 }
225 RemoteSuggestionsScheduler* remote_suggestions_scheduler() {
226 return remote_suggestions_scheduler_;
205 } 227 }
206 228
207 UserClassifier* user_classifier() { return &user_classifier_; } 229 UserClassifier* user_classifier() { return &user_classifier_; }
208 230
209 private: 231 private:
210 friend class ContentSuggestionsServiceTest; 232 friend class ContentSuggestionsServiceTest;
211 233
212 // Implementation of ContentSuggestionsProvider::Observer. 234 // Implementation of ContentSuggestionsProvider::Observer.
213 void OnNewSuggestions(ContentSuggestionsProvider* provider, 235 void OnNewSuggestions(ContentSuggestionsProvider* provider,
214 Category category, 236 Category category,
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
305 327
306 // Observer for the HistoryService. All providers are notified when history is 328 // Observer for the HistoryService. All providers are notified when history is
307 // deleted. 329 // deleted.
308 ScopedObserver<history::HistoryService, history::HistoryServiceObserver> 330 ScopedObserver<history::HistoryService, history::HistoryServiceObserver>
309 history_service_observer_; 331 history_service_observer_;
310 332
311 base::ObserverList<Observer> observers_; 333 base::ObserverList<Observer> observers_;
312 334
313 const std::vector<ContentSuggestion> no_suggestions_; 335 const std::vector<ContentSuggestion> no_suggestions_;
314 336
315 // Keep a direct reference to this special provider to redirect scheduling, 337 // Keep a direct reference to this special provider to redirect debugging
316 // background fetching and debugging calls to it. If the 338 // calls to it. If the RemoteSuggestionsProvider is loaded, it is also present
317 // RemoteSuggestionsProvider is loaded, it is also present in |providers_|, 339 // in |providers_|, otherwise this is a nullptr.
318 // otherwise this is a nullptr. 340 RemoteSuggestionsProvider* remote_suggestions_provider_;
319 RemoteSuggestionsProvider* ntp_snippets_service_; 341
342 // Interface for informing about external events that have influence on
343 // scheduling remote fetches. Not owned.
344 RemoteSuggestionsScheduler* remote_suggestions_scheduler_;
320 345
321 PrefService* pref_service_; 346 PrefService* pref_service_;
322 347
323 UserClassifier user_classifier_; 348 UserClassifier user_classifier_;
324 349
325 DISALLOW_COPY_AND_ASSIGN(ContentSuggestionsService); 350 DISALLOW_COPY_AND_ASSIGN(ContentSuggestionsService);
326 }; 351 };
327 352
328 } // namespace ntp_snippets 353 } // namespace ntp_snippets
329 354
330 #endif // COMPONENTS_NTP_SNIPPETS_CONTENT_SUGGESTIONS_SERVICE_H_ 355 #endif // COMPONENTS_NTP_SNIPPETS_CONTENT_SUGGESTIONS_SERVICE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698