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

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>
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_factory.h" 23 #include "components/ntp_snippets/category_factory.h"
24 #include "components/ntp_snippets/category_status.h" 24 #include "components/ntp_snippets/category_status.h"
25 #include "components/ntp_snippets/content_suggestions_provider.h" 25 #include "components/ntp_snippets/content_suggestions_provider.h"
26 #include "components/ntp_snippets/remote/persistent_scheduler.h"
26 #include "components/ntp_snippets/user_classifier.h" 27 #include "components/ntp_snippets/user_classifier.h"
27 #include "components/signin/core/browser/signin_manager.h" 28 #include "components/signin/core/browser/signin_manager.h"
28 29
29 class PrefService; 30 class PrefService;
30 class PrefRegistrySimple; 31 class PrefRegistrySimple;
31 32
32 namespace ntp_snippets { 33 namespace ntp_snippets {
33 34
34 class RemoteSuggestionsProvider; 35 class RemoteSuggestionsProvider;
35 36
(...skipping 97 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. Makes sense to call only after
tschumann 2016/12/15 19:27:00 what does "reload" mean? Would this include fetchi
jkrcal 2016/12/19 09:40:24 I've rewritten the comment. Hope it is clearer now
151 // some suggestions have been dismissed. The provider may fill in the vacant
152 // space by new suggestions that haven't been included previously due to space
153 // limits.
154 void ReloadSuggestions();
155
147 // Observer accessors. 156 // Observer accessors.
148 void AddObserver(Observer* observer); 157 void AddObserver(Observer* observer);
149 void RemoveObserver(Observer* observer); 158 void RemoveObserver(Observer* observer);
150 159
151 // Registers a new ContentSuggestionsProvider. It must be ensured that at most 160 // Registers a new ContentSuggestionsProvider. It must be ensured that at most
152 // one provider is registered for every category and that this method is 161 // one provider is registered for every category and that this method is
153 // called only once per provider. 162 // called only once per provider.
154 void RegisterProvider(std::unique_ptr<ContentSuggestionsProvider> provider); 163 void RegisterProvider(std::unique_ptr<ContentSuggestionsProvider> provider);
155 164
156 // Removes history from the specified time range where the URL matches the 165 // Removes history from the specified time range where the URL matches the
(...skipping 29 matching lines...) Expand all
186 195
187 // Only for debugging use through the internals page. Some providers 196 // Only for debugging use through the internals page. Some providers
188 // internally store a list of dismissed suggestions to prevent them from 197 // internally store a list of dismissed suggestions to prevent them from
189 // reappearing. This function clears all suggestions of the given |category| 198 // reappearing. This function clears all suggestions of the given |category|
190 // from such lists, making dismissed suggestions reappear (if the provider 199 // from such lists, making dismissed suggestions reappear (if the provider
191 // supports it). 200 // supports it).
192 void ClearDismissedSuggestionsForDebugging(Category category); 201 void ClearDismissedSuggestionsForDebugging(Category category);
193 202
194 CategoryFactory* category_factory() { return &category_factory_; } 203 CategoryFactory* category_factory() { return &category_factory_; }
195 204
196 // The reference to the RemoteSuggestionsProvider provider should only be set 205 // The reference to the RemoteSuggestionsProvider provider should
197 // by the factory and only be used for scheduling, periodic fetching and 206 // only be set by the factory and only used for debugging.
198 // debugging. 207 void set_remote_suggestions_provider(
tschumann 2016/12/15 19:27:00 please add a TODO() to remove this setter. These i
jkrcal 2016/12/19 09:40:24 Yes, there is a cyclic dependency. Adding a setter
tschumann 2016/12/19 11:19:23 The depdencies just feel wrong: Needing the servic
jkrcal 2016/12/20 16:39:45 Thanks for the explanation. Still not totally conv
199 RemoteSuggestionsProvider* ntp_snippets_service() { 208 RemoteSuggestionsProvider* remote_suggestions_provider) {
200 return ntp_snippets_service_; 209 remote_suggestions_provider_ = remote_suggestions_provider;
201 } 210 }
202 void set_ntp_snippets_service( 211 RemoteSuggestionsProvider* remote_suggestions_provider_for_debugging() {
203 RemoteSuggestionsProvider* ntp_snippets_service) { 212 return remote_suggestions_provider_;
204 ntp_snippets_service_ = ntp_snippets_service; 213 }
214
215 // The reference to PersistentScheduler::Listener should
216 // only be set by the factory and only used for scheduling, periodic fetching.
217 void set_persistent_scheduler_listener(
218 PersistentScheduler::Listener* persistent_scheduler_listener) {
219 persistent_scheduler_listener_ = persistent_scheduler_listener;
220 }
221 PersistentScheduler::Listener* persistent_scheduler_listener() {
222 return persistent_scheduler_listener_;
205 } 223 }
206 224
207 UserClassifier* user_classifier() { return &user_classifier_; } 225 UserClassifier* user_classifier() { return &user_classifier_; }
208 226
209 private: 227 private:
210 friend class ContentSuggestionsServiceTest; 228 friend class ContentSuggestionsServiceTest;
211 229
212 // Implementation of ContentSuggestionsProvider::Observer. 230 // Implementation of ContentSuggestionsProvider::Observer.
213 void OnNewSuggestions(ContentSuggestionsProvider* provider, 231 void OnNewSuggestions(ContentSuggestionsProvider* provider,
214 Category category, 232 Category category,
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
305 323
306 // Observer for the HistoryService. All providers are notified when history is 324 // Observer for the HistoryService. All providers are notified when history is
307 // deleted. 325 // deleted.
308 ScopedObserver<history::HistoryService, history::HistoryServiceObserver> 326 ScopedObserver<history::HistoryService, history::HistoryServiceObserver>
309 history_service_observer_; 327 history_service_observer_;
310 328
311 base::ObserverList<Observer> observers_; 329 base::ObserverList<Observer> observers_;
312 330
313 const std::vector<ContentSuggestion> no_suggestions_; 331 const std::vector<ContentSuggestion> no_suggestions_;
314 332
315 // Keep a direct reference to this special provider to redirect scheduling, 333 // Keep a direct reference to this special provider to redirect debugging
316 // background fetching and debugging calls to it. If the 334 // calls to it. If the RemoteSuggestionsProvider is loaded, it is also present
317 // RemoteSuggestionsProvider is loaded, it is also present in |providers_|, 335 // in |providers_|, otherwise this is a nullptr.
318 // otherwise this is a nullptr. 336 RemoteSuggestionsProvider* remote_suggestions_provider_;
319 RemoteSuggestionsProvider* ntp_snippets_service_; 337
338 // Interface for OS-dependent scheduling and background fetching.
339 PersistentScheduler::Listener* persistent_scheduler_listener_;
320 340
321 PrefService* pref_service_; 341 PrefService* pref_service_;
322 342
323 UserClassifier user_classifier_; 343 UserClassifier user_classifier_;
324 344
325 DISALLOW_COPY_AND_ASSIGN(ContentSuggestionsService); 345 DISALLOW_COPY_AND_ASSIGN(ContentSuggestionsService);
326 }; 346 };
327 347
328 } // namespace ntp_snippets 348 } // namespace ntp_snippets
329 349
330 #endif // COMPONENTS_NTP_SNIPPETS_CONTENT_SUGGESTIONS_SERVICE_H_ 350 #endif // COMPONENTS_NTP_SNIPPETS_CONTENT_SUGGESTIONS_SERVICE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698