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

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

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

Powered by Google App Engine
This is Rietveld 408576698