OLD | NEW |
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 Loading... |
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 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
136 // Restores all dismissed categories. | 137 // Restores all dismissed categories. |
137 // This will not trigger an update through the observers. | 138 // This will not trigger an update through the observers. |
138 void RestoreDismissedCategories(); | 139 void RestoreDismissedCategories(); |
139 | 140 |
140 // Returns whether |category| is dismissed. | 141 // Returns whether |category| is dismissed. |
141 bool IsCategoryDismissed(Category category) const; | 142 bool IsCategoryDismissed(Category category) const; |
142 | 143 |
143 // Fetches additional contents for the given |category|. If the fetch was | 144 // Fetches additional contents for the given |category|. If the fetch was |
144 // completed, the given |callback| is called with the updated content. | 145 // completed, the given |callback| is called with the updated content. |
145 // This includes new and old data. | 146 // This includes new and old data. |
| 147 // TODO(jkrcal): Consider either renaming this to FetchMore or unify the ways |
| 148 // to get suggestions to just this async Fetch() API. |
146 void Fetch(const Category& category, | 149 void Fetch(const Category& category, |
147 const std::set<std::string>& known_suggestion_ids, | 150 const std::set<std::string>& known_suggestion_ids, |
148 const FetchDoneCallback& callback); | 151 const FetchDoneCallback& callback); |
149 | 152 |
| 153 // Reloads suggestions from all categories, from all providers. If a provider |
| 154 // naturally has some ability to generate fresh suggestions, it may provide a |
| 155 // completely new set of suggestions. If the provider has no ability to |
| 156 // generate fresh suggestions on demand, it may only fill in any vacant space |
| 157 // by suggestions that were previously not included due to space limits (there |
| 158 // may be vacant space because of the user dismissing suggestions in the |
| 159 // meantime). |
| 160 void ReloadSuggestions(); |
| 161 |
150 // Observer accessors. | 162 // Observer accessors. |
151 void AddObserver(Observer* observer); | 163 void AddObserver(Observer* observer); |
152 void RemoveObserver(Observer* observer); | 164 void RemoveObserver(Observer* observer); |
153 | 165 |
154 // Registers a new ContentSuggestionsProvider. It must be ensured that at most | 166 // Registers a new ContentSuggestionsProvider. It must be ensured that at most |
155 // one provider is registered for every category and that this method is | 167 // one provider is registered for every category and that this method is |
156 // called only once per provider. | 168 // called only once per provider. |
157 void RegisterProvider(std::unique_ptr<ContentSuggestionsProvider> provider); | 169 void RegisterProvider(std::unique_ptr<ContentSuggestionsProvider> provider); |
158 | 170 |
159 // Removes history from the specified time range where the URL matches the | 171 // Removes history from the specified time range where the URL matches the |
(...skipping 27 matching lines...) Expand all Loading... |
187 Category category, | 199 Category category, |
188 const DismissedSuggestionsCallback& callback); | 200 const DismissedSuggestionsCallback& callback); |
189 | 201 |
190 // Only for debugging use through the internals page. Some providers | 202 // Only for debugging use through the internals page. Some providers |
191 // internally store a list of dismissed suggestions to prevent them from | 203 // internally store a list of dismissed suggestions to prevent them from |
192 // reappearing. This function clears all suggestions of the given |category| | 204 // reappearing. This function clears all suggestions of the given |category| |
193 // from such lists, making dismissed suggestions reappear (if the provider | 205 // from such lists, making dismissed suggestions reappear (if the provider |
194 // supports it). | 206 // supports it). |
195 void ClearDismissedSuggestionsForDebugging(Category category); | 207 void ClearDismissedSuggestionsForDebugging(Category category); |
196 | 208 |
197 // The reference to the RemoteSuggestionsProvider provider should only be set | 209 // The reference to the RemoteSuggestionsProvider provider should |
198 // by the factory and only be used for scheduling, periodic fetching and | 210 // only be set by the factory and only used for debugging. |
199 // debugging. | 211 // TODO(jkrcal) The way we deal with the circular dependency feels wrong. |
200 RemoteSuggestionsProvider* ntp_snippets_service() { | 212 // Consider swapping the dependencies: first constructing all providers, then |
201 return ntp_snippets_service_; | 213 // constructing the service (passing the remote provider as arg), finally |
| 214 // registering the service as an observer of all providers? |
| 215 void set_remote_suggestions_provider( |
| 216 RemoteSuggestionsProvider* remote_suggestions_provider) { |
| 217 remote_suggestions_provider_ = remote_suggestions_provider; |
202 } | 218 } |
203 void set_ntp_snippets_service( | 219 RemoteSuggestionsProvider* remote_suggestions_provider_for_debugging() { |
204 RemoteSuggestionsProvider* ntp_snippets_service) { | 220 return remote_suggestions_provider_; |
205 ntp_snippets_service_ = ntp_snippets_service; | 221 } |
| 222 |
| 223 // The reference to RemoteSuggestionsScheduler should only be set by the |
| 224 // factory. The interface is suited for informing about external events that |
| 225 // have influence on scheduling remote fetches. |
| 226 void set_remote_suggestions_scheduler( |
| 227 ntp_snippets::RemoteSuggestionsScheduler* remote_suggestions_scheduler) { |
| 228 remote_suggestions_scheduler_ = remote_suggestions_scheduler; |
| 229 } |
| 230 RemoteSuggestionsScheduler* remote_suggestions_scheduler() { |
| 231 return remote_suggestions_scheduler_; |
206 } | 232 } |
207 | 233 |
208 UserClassifier* user_classifier() { return &user_classifier_; } | 234 UserClassifier* user_classifier() { return &user_classifier_; } |
209 CategoryRanker* category_ranker() { return category_ranker_.get(); } | 235 CategoryRanker* category_ranker() { return category_ranker_.get(); } |
210 | 236 |
211 private: | 237 private: |
212 friend class ContentSuggestionsServiceTest; | 238 friend class ContentSuggestionsServiceTest; |
213 | 239 |
214 // Implementation of ContentSuggestionsProvider::Observer. | 240 // Implementation of ContentSuggestionsProvider::Observer. |
215 void OnNewSuggestions(ContentSuggestionsProvider* provider, | 241 void OnNewSuggestions(ContentSuggestionsProvider* provider, |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
301 | 327 |
302 // Observer for the HistoryService. All providers are notified when history is | 328 // Observer for the HistoryService. All providers are notified when history is |
303 // deleted. | 329 // deleted. |
304 ScopedObserver<history::HistoryService, history::HistoryServiceObserver> | 330 ScopedObserver<history::HistoryService, history::HistoryServiceObserver> |
305 history_service_observer_; | 331 history_service_observer_; |
306 | 332 |
307 base::ObserverList<Observer> observers_; | 333 base::ObserverList<Observer> observers_; |
308 | 334 |
309 const std::vector<ContentSuggestion> no_suggestions_; | 335 const std::vector<ContentSuggestion> no_suggestions_; |
310 | 336 |
311 // Keep a direct reference to this special provider to redirect scheduling, | 337 // Keep a direct reference to this special provider to redirect debugging |
312 // background fetching and debugging calls to it. If the | 338 // calls to it. If the RemoteSuggestionsProvider is loaded, it is also present |
313 // RemoteSuggestionsProvider is loaded, it is also present in |providers_|, | 339 // in |providers_|, otherwise this is a nullptr. |
314 // otherwise this is a nullptr. | 340 RemoteSuggestionsProvider* remote_suggestions_provider_; |
315 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_; |
316 | 345 |
317 PrefService* pref_service_; | 346 PrefService* pref_service_; |
318 | 347 |
319 UserClassifier user_classifier_; | 348 UserClassifier user_classifier_; |
320 | 349 |
321 // Provides order for categories. | 350 // Provides order for categories. |
322 std::unique_ptr<CategoryRanker> category_ranker_; | 351 std::unique_ptr<CategoryRanker> category_ranker_; |
323 | 352 |
324 DISALLOW_COPY_AND_ASSIGN(ContentSuggestionsService); | 353 DISALLOW_COPY_AND_ASSIGN(ContentSuggestionsService); |
325 }; | 354 }; |
326 | 355 |
327 } // namespace ntp_snippets | 356 } // namespace ntp_snippets |
328 | 357 |
329 #endif // COMPONENTS_NTP_SNIPPETS_CONTENT_SUGGESTIONS_SERVICE_H_ | 358 #endif // COMPONENTS_NTP_SNIPPETS_CONTENT_SUGGESTIONS_SERVICE_H_ |
OLD | NEW |