Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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_NTP_SNIPPETS_SERVICE_H_ | 5 #ifndef COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_SERVICE_H_ |
| 6 #define COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_SERVICE_H_ | 6 #define COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_SERVICE_H_ |
| 7 | 7 |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 | 9 |
| 10 #include <memory> | 10 #include <memory> |
| 11 #include <set> | 11 #include <set> |
| 12 #include <string> | 12 #include <string> |
| 13 #include <vector> | 13 #include <vector> |
| 14 | 14 |
| 15 #include "base/callback_forward.h" | 15 #include "base/callback_forward.h" |
| 16 #include "base/gtest_prod_util.h" | 16 #include "base/gtest_prod_util.h" |
| 17 #include "base/macros.h" | 17 #include "base/macros.h" |
| 18 #include "base/observer_list.h" | 18 #include "base/observer_list.h" |
| 19 #include "base/timer/timer.h" | 19 #include "base/timer/timer.h" |
| 20 #include "components/image_fetcher/image_fetcher_delegate.h" | 20 #include "components/image_fetcher/image_fetcher_delegate.h" |
| 21 #include "components/keyed_service/core/keyed_service.h" | 21 #include "components/keyed_service/core/keyed_service.h" |
| 22 #include "components/ntp_snippets/content_suggestion.h" | |
| 23 #include "components/ntp_snippets/content_suggestions_provider.h" | |
| 22 #include "components/ntp_snippets/ntp_snippet.h" | 24 #include "components/ntp_snippets/ntp_snippet.h" |
| 23 #include "components/ntp_snippets/ntp_snippets_fetcher.h" | 25 #include "components/ntp_snippets/ntp_snippets_fetcher.h" |
| 24 #include "components/ntp_snippets/ntp_snippets_scheduler.h" | 26 #include "components/ntp_snippets/ntp_snippets_scheduler.h" |
| 25 #include "components/ntp_snippets/ntp_snippets_status_service.h" | 27 #include "components/ntp_snippets/ntp_snippets_status_service.h" |
| 26 #include "components/suggestions/suggestions_service.h" | 28 #include "components/suggestions/suggestions_service.h" |
| 27 #include "components/sync_driver/sync_service_observer.h" | 29 #include "components/sync_driver/sync_service_observer.h" |
| 28 | 30 |
| 29 class PrefRegistrySimple; | 31 class PrefRegistrySimple; |
| 30 class PrefService; | 32 class PrefService; |
| 31 class SigninManagerBase; | 33 class SigninManagerBase; |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 52 class SyncService; | 54 class SyncService; |
| 53 } | 55 } |
| 54 | 56 |
| 55 namespace ntp_snippets { | 57 namespace ntp_snippets { |
| 56 | 58 |
| 57 class NTPSnippetsDatabase; | 59 class NTPSnippetsDatabase; |
| 58 class NTPSnippetsServiceObserver; | 60 class NTPSnippetsServiceObserver; |
| 59 | 61 |
| 60 // Stores and vends fresh content data for the NTP. | 62 // Stores and vends fresh content data for the NTP. |
| 61 class NTPSnippetsService : public KeyedService, | 63 class NTPSnippetsService : public KeyedService, |
| 62 public image_fetcher::ImageFetcherDelegate { | 64 public image_fetcher::ImageFetcherDelegate, |
| 65 public ContentSuggestionsProvider { | |
| 63 public: | 66 public: |
| 64 using ImageFetchedCallback = | 67 using ImageFetchedCallback = |
| 65 base::Callback<void(const std::string& snippet_id, const gfx::Image&)>; | 68 base::Callback<void(const std::string& suggestion_id, const gfx::Image&)>; |
| 66 | 69 |
| 67 // |application_language_code| should be a ISO 639-1 compliant string, e.g. | 70 // |application_language_code| should be a ISO 639-1 compliant string, e.g. |
| 68 // 'en' or 'en-US'. Note that this code should only specify the language, not | 71 // 'en' or 'en-US'. Note that this code should only specify the language, not |
| 69 // the locale, so 'en_US' (English language with US locale) and 'en-GB_US' | 72 // the locale, so 'en_US' (English language with US locale) and 'en-GB_US' |
| 70 // (British English person in the US) are not language codes. | 73 // (British English person in the US) are not language codes. |
| 71 NTPSnippetsService(bool enabled, | 74 NTPSnippetsService(bool enabled, |
| 72 PrefService* pref_service, | 75 PrefService* pref_service, |
| 73 suggestions::SuggestionsService* suggestions_service, | 76 suggestions::SuggestionsService* suggestions_service, |
| 74 const std::string& application_language_code, | 77 const std::string& application_language_code, |
| 75 NTPSnippetsScheduler* scheduler, | 78 NTPSnippetsScheduler* scheduler, |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 119 // Returns a reason why the service is disabled, or DisabledReason::NONE | 122 // Returns a reason why the service is disabled, or DisabledReason::NONE |
| 120 // if it's not. | 123 // if it's not. |
| 121 DisabledReason disabled_reason() const { | 124 DisabledReason disabled_reason() const { |
| 122 return snippets_status_service_->disabled_reason(); | 125 return snippets_status_service_->disabled_reason(); |
| 123 } | 126 } |
| 124 | 127 |
| 125 // (Re)schedules the periodic fetching of snippets. This is necessary because | 128 // (Re)schedules the periodic fetching of snippets. This is necessary because |
| 126 // the schedule depends on the time of day. | 129 // the schedule depends on the time of day. |
| 127 void RescheduleFetching(); | 130 void RescheduleFetching(); |
| 128 | 131 |
| 129 // Fetches the image for the snippet with the given |snippet_id| and runs the | 132 // ContentSuggestionsProvider implementation |
| 130 // |callback|. If that snippet doesn't exist or the fetch fails, the callback | 133 // TODO(pke): At some point reorder the implementations in the .cc file |
| 131 // gets a null image. | 134 // accordingly. |
| 132 void FetchSnippetImage(const std::string& snippet_id, | 135 void SetObserver(Observer* observer) override; |
| 133 const ImageFetchedCallback& callback); | 136 ContentSuggestionsCategoryStatus GetCategoryStatus( |
| 134 | 137 ContentSuggestionsCategory category) override; |
| 135 // Deletes all currently stored snippets. | 138 void DiscardSuggestion(const std::string& suggestion_id) override; |
| 136 void ClearSnippets(); | 139 void FetchSuggestionImage(const std::string& suggestion_id, |
| 137 | 140 const ImageFetchedCallback& callback) override; |
| 138 // Discards the snippet with the given |snippet_id|, if it exists. Returns | 141 void ClearCachedSuggestionsForDebugging() override; |
| 139 // true iff a snippet was discarded. | 142 void ClearDiscardedSuggestionsForDebugging() override; |
| 140 bool DiscardSnippet(const std::string& snippet_id); | |
| 141 | |
| 142 // Clears the lists of snippets previously discarded by the user. | |
| 143 void ClearDiscardedSnippets(); | |
| 144 | 143 |
| 145 // Returns the lists of suggestion hosts the snippets are restricted to. | 144 // Returns the lists of suggestion hosts the snippets are restricted to. |
| 146 std::set<std::string> GetSuggestionsHosts() const; | 145 std::set<std::string> GetSuggestionsHosts() const; |
| 147 | 146 |
| 148 // Observer accessors. | 147 // Observer accessors. |
| 149 void AddObserver(NTPSnippetsServiceObserver* observer); | 148 void AddObserver(NTPSnippetsServiceObserver* observer); |
| 150 void RemoveObserver(NTPSnippetsServiceObserver* observer); | 149 void RemoveObserver(NTPSnippetsServiceObserver* observer); |
| 151 | 150 |
| 152 // Returns the maximum number of snippets that will be shown at once. | 151 // Returns the maximum number of snippets that will be shown at once. |
| 153 static int GetMaxSnippetCountForTesting(); | 152 static int GetMaxSnippetCountForTesting(); |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 247 | 246 |
| 248 // Disables the service. Do not call directly, use |EnterState| instead. | 247 // Disables the service. Do not call directly, use |EnterState| instead. |
| 249 void EnterStateDisabled(); | 248 void EnterStateDisabled(); |
| 250 | 249 |
| 251 // Applies the effects of the transition to the SHUT_DOWN state. Do not call | 250 // Applies the effects of the transition to the SHUT_DOWN state. Do not call |
| 252 // directly, use |EnterState| instead. | 251 // directly, use |EnterState| instead. |
| 253 void EnterStateShutdown(); | 252 void EnterStateShutdown(); |
| 254 | 253 |
| 255 void ClearDeprecatedPrefs(); | 254 void ClearDeprecatedPrefs(); |
| 256 | 255 |
| 256 // Converts the cached snippets to article content suggestions and notifies | |
| 257 // the observers. | |
| 258 void NotifyNewSuggestions(); | |
| 259 | |
| 260 // Notifies the content suggestions observer about a change in the | |
| 261 // |category_status_|. | |
| 262 void NotifyCategoryStatusChanged(); | |
| 263 | |
| 257 State state_; | 264 State state_; |
| 258 | 265 |
| 266 ContentSuggestionsCategoryStatus category_status_; | |
| 267 | |
| 259 PrefService* pref_service_; | 268 PrefService* pref_service_; |
| 260 | 269 |
| 261 suggestions::SuggestionsService* suggestions_service_; | 270 suggestions::SuggestionsService* suggestions_service_; |
| 262 | 271 |
| 263 // All current suggestions (i.e. not discarded ones). | 272 // All current suggestions (i.e. not discarded ones). |
| 264 NTPSnippet::PtrVector snippets_; | 273 NTPSnippet::PtrVector snippets_; |
| 265 | 274 |
| 266 // Suggestions that the user discarded. We keep these around until they expire | 275 // Suggestions that the user discarded. We keep these around until they expire |
| 267 // so we won't re-add them on the next fetch. | 276 // so we won't re-add them on the next fetch. |
| 268 NTPSnippet::PtrVector discarded_snippets_; | 277 NTPSnippet::PtrVector discarded_snippets_; |
| 269 | 278 |
| 270 // The ISO 639-1 code of the language used by the application. | 279 // The ISO 639-1 code of the language used by the application. |
| 271 const std::string application_language_code_; | 280 const std::string application_language_code_; |
| 272 | 281 |
| 273 // The observers. | 282 // The observers. |
| 283 // TODO(pke): This is an old kind of observers that will be removed. | |
| 274 base::ObserverList<NTPSnippetsServiceObserver> observers_; | 284 base::ObserverList<NTPSnippetsServiceObserver> observers_; |
| 275 | 285 |
| 286 // The observer to deliver data to. | |
| 287 Observer* observer_; | |
| 288 | |
| 276 // Scheduler for fetching snippets. Not owned. | 289 // Scheduler for fetching snippets. Not owned. |
| 277 NTPSnippetsScheduler* scheduler_; | 290 NTPSnippetsScheduler* scheduler_; |
| 278 | 291 |
| 279 // The subscription to the SuggestionsService. When the suggestions change, | 292 // The subscription to the SuggestionsService. When the suggestions change, |
| 280 // SuggestionsService will call |OnSuggestionsChanged|, which triggers an | 293 // SuggestionsService will call |OnSuggestionsChanged|, which triggers an |
| 281 // update to the set of snippets. | 294 // update to the set of snippets. |
| 282 using SuggestionsSubscription = | 295 using SuggestionsSubscription = |
| 283 suggestions::SuggestionsService::ResponseCallbackList::Subscription; | 296 suggestions::SuggestionsService::ResponseCallbackList::Subscription; |
| 284 std::unique_ptr<SuggestionsSubscription> suggestions_service_subscription_; | 297 std::unique_ptr<SuggestionsSubscription> suggestions_service_subscription_; |
| 285 | 298 |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 298 // The service that provides events and data about the signin and sync state. | 311 // The service that provides events and data about the signin and sync state. |
| 299 std::unique_ptr<NTPSnippetsStatusService> snippets_status_service_; | 312 std::unique_ptr<NTPSnippetsStatusService> snippets_status_service_; |
| 300 | 313 |
| 301 // Set to true if FetchSnippets is called before the database has been loaded. | 314 // Set to true if FetchSnippets is called before the database has been loaded. |
| 302 // The fetch will be executed after the database load finishes. | 315 // The fetch will be executed after the database load finishes. |
| 303 bool fetch_after_load_; | 316 bool fetch_after_load_; |
| 304 | 317 |
| 305 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsService); | 318 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsService); |
| 306 }; | 319 }; |
| 307 | 320 |
| 321 // TODO(pke): Remove this when snippets internals don't access this service | |
| 322 // directly anymore. | |
| 308 class NTPSnippetsServiceObserver { | 323 class NTPSnippetsServiceObserver { |
| 309 public: | 324 public: |
| 310 // Sent every time the service loads a new set of data. | 325 // Sent every time the service loads a new set of data. |
| 326 // This is called in addition to the ContentSuggestionsProvider::Observer and | |
| 327 // should only be used by snippets-internals. | |
|
Marc Treib
2016/07/11 08:41:33
nit: I'd remove this comment. As of now, this meth
Philipp Keck
2016/07/11 09:41:49
Done.
| |
| 311 virtual void NTPSnippetsServiceLoaded() = 0; | 328 virtual void NTPSnippetsServiceLoaded() = 0; |
| 312 | 329 |
| 313 // Sent when the service is shutting down. | 330 // Sent when the service is shutting down. |
| 314 virtual void NTPSnippetsServiceShutdown() = 0; | 331 virtual void NTPSnippetsServiceShutdown() = 0; |
| 315 | 332 |
| 316 // Sent when the state of the service is changing. Something changed in its | 333 // Sent when the state of the service is changing. Something changed in its |
| 317 // dependencies so it's notifying observers about incoming data changes. | 334 // dependencies so it's notifying observers about incoming data changes. |
| 318 // If the service might be enabled, DisabledReason::NONE will be provided. | 335 // If the service might be enabled, DisabledReason::NONE will be provided. |
| 319 virtual void NTPSnippetsServiceDisabledReasonChanged(DisabledReason) = 0; | 336 virtual void NTPSnippetsServiceDisabledReasonChanged(DisabledReason) = 0; |
| 320 | 337 |
| 321 protected: | 338 protected: |
| 322 virtual ~NTPSnippetsServiceObserver() {} | 339 virtual ~NTPSnippetsServiceObserver() {} |
| 323 }; | 340 }; |
| 324 | 341 |
| 325 } // namespace ntp_snippets | 342 } // namespace ntp_snippets |
| 326 | 343 |
| 327 #endif // COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_SERVICE_H_ | 344 #endif // COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_SERVICE_H_ |
| OLD | NEW |