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

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

Issue 2557363002: [NTP Snippets] Refactor background scheduling for remote suggestions (Closed)
Patch Set: Addressing many comments 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 // TODO(jkrcal) The way we deal with the circular dependency feels wrong to
199 RemoteSuggestionsProvider* ntp_snippets_service() { 211 // Tim. Consider swapping the dependencies: first constructing all providers,
200 return ntp_snippets_service_; 212 // then constructing the service (passing the remote provider as arg), finally
213 // registering the service as an observer of all providers?
214 void set_remote_suggestions_provider(
215 RemoteSuggestionsProvider* remote_suggestions_provider) {
216 remote_suggestions_provider_ = remote_suggestions_provider;
201 } 217 }
202 void set_ntp_snippets_service( 218 RemoteSuggestionsProvider* remote_suggestions_provider_for_debugging() {
203 RemoteSuggestionsProvider* ntp_snippets_service) { 219 return remote_suggestions_provider_;
204 ntp_snippets_service_ = ntp_snippets_service; 220 }
221
222 // The reference to RemoteSuggestionsScheduler should only be set by the
223 // factory. The interface is suited for informing about external events that
224 // have influence on scheduling remote fetches.
225 void set_remote_suggestions_scheduler(
226 ntp_snippets::RemoteSuggestionsScheduler* remote_suggestions_scheduler) {
227 remote_suggestions_scheduler_ = remote_suggestions_scheduler;
228 }
229 RemoteSuggestionsScheduler* remote_suggestions_scheduler() {
230 return remote_suggestions_scheduler_;
205 } 231 }
206 232
207 UserClassifier* user_classifier() { return &user_classifier_; } 233 UserClassifier* user_classifier() { return &user_classifier_; }
208 234
209 private: 235 private:
210 friend class ContentSuggestionsServiceTest; 236 friend class ContentSuggestionsServiceTest;
211 237
212 // Implementation of ContentSuggestionsProvider::Observer. 238 // Implementation of ContentSuggestionsProvider::Observer.
213 void OnNewSuggestions(ContentSuggestionsProvider* provider, 239 void OnNewSuggestions(ContentSuggestionsProvider* provider,
214 Category category, 240 Category category,
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
305 331
306 // Observer for the HistoryService. All providers are notified when history is 332 // Observer for the HistoryService. All providers are notified when history is
307 // deleted. 333 // deleted.
308 ScopedObserver<history::HistoryService, history::HistoryServiceObserver> 334 ScopedObserver<history::HistoryService, history::HistoryServiceObserver>
309 history_service_observer_; 335 history_service_observer_;
310 336
311 base::ObserverList<Observer> observers_; 337 base::ObserverList<Observer> observers_;
312 338
313 const std::vector<ContentSuggestion> no_suggestions_; 339 const std::vector<ContentSuggestion> no_suggestions_;
314 340
315 // Keep a direct reference to this special provider to redirect scheduling, 341 // Keep a direct reference to this special provider to redirect debugging
316 // background fetching and debugging calls to it. If the 342 // calls to it. If the RemoteSuggestionsProvider is loaded, it is also present
317 // RemoteSuggestionsProvider is loaded, it is also present in |providers_|, 343 // in |providers_|, otherwise this is a nullptr.
318 // otherwise this is a nullptr. 344 RemoteSuggestionsProvider* remote_suggestions_provider_;
319 RemoteSuggestionsProvider* ntp_snippets_service_; 345
346 // Interface for informing about external events that have influence on
347 // scheduling remote fetches. Not owned.
348 RemoteSuggestionsScheduler* remote_suggestions_scheduler_;
320 349
321 PrefService* pref_service_; 350 PrefService* pref_service_;
322 351
323 UserClassifier user_classifier_; 352 UserClassifier user_classifier_;
324 353
325 DISALLOW_COPY_AND_ASSIGN(ContentSuggestionsService); 354 DISALLOW_COPY_AND_ASSIGN(ContentSuggestionsService);
326 }; 355 };
327 356
328 } // namespace ntp_snippets 357 } // namespace ntp_snippets
329 358
330 #endif // COMPONENTS_NTP_SNIPPETS_CONTENT_SUGGESTIONS_SERVICE_H_ 359 #endif // COMPONENTS_NTP_SNIPPETS_CONTENT_SUGGESTIONS_SERVICE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698