| 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_category.h" |
| 24 #include "components/ntp_snippets/content_suggestions_category_status.h" |
| 25 #include "components/ntp_snippets/content_suggestions_provider.h" |
| 22 #include "components/ntp_snippets/ntp_snippet.h" | 26 #include "components/ntp_snippets/ntp_snippet.h" |
| 23 #include "components/ntp_snippets/ntp_snippets_fetcher.h" | 27 #include "components/ntp_snippets/ntp_snippets_fetcher.h" |
| 24 #include "components/ntp_snippets/ntp_snippets_scheduler.h" | 28 #include "components/ntp_snippets/ntp_snippets_scheduler.h" |
| 25 #include "components/ntp_snippets/ntp_snippets_status_service.h" | 29 #include "components/ntp_snippets/ntp_snippets_status_service.h" |
| 26 #include "components/suggestions/suggestions_service.h" | 30 #include "components/suggestions/suggestions_service.h" |
| 27 #include "components/sync_driver/sync_service_observer.h" | 31 #include "components/sync_driver/sync_service_observer.h" |
| 28 | 32 |
| 29 class PrefRegistrySimple; | 33 class PrefRegistrySimple; |
| 30 class PrefService; | 34 class PrefService; |
| 31 class SigninManagerBase; | 35 class SigninManagerBase; |
| (...skipping 19 matching lines...) Expand all Loading... |
| 51 namespace sync_driver { | 55 namespace sync_driver { |
| 52 class SyncService; | 56 class SyncService; |
| 53 } | 57 } |
| 54 | 58 |
| 55 namespace ntp_snippets { | 59 namespace ntp_snippets { |
| 56 | 60 |
| 57 class NTPSnippetsDatabase; | 61 class NTPSnippetsDatabase; |
| 58 class NTPSnippetsServiceObserver; | 62 class NTPSnippetsServiceObserver; |
| 59 | 63 |
| 60 // Stores and vends fresh content data for the NTP. | 64 // Stores and vends fresh content data for the NTP. |
| 65 // TODO(pke): Rename this service to ArticleSuggestionsService and move to |
| 66 // a subdirectory. |
| 61 class NTPSnippetsService : public KeyedService, | 67 class NTPSnippetsService : public KeyedService, |
| 62 public image_fetcher::ImageFetcherDelegate { | 68 public image_fetcher::ImageFetcherDelegate, |
| 69 public ContentSuggestionsProvider { |
| 63 public: | 70 public: |
| 64 using ImageFetchedCallback = | 71 using ImageFetchedCallback = |
| 65 base::Callback<void(const std::string& snippet_id, const gfx::Image&)>; | 72 base::Callback<void(const std::string& suggestion_id, const gfx::Image&)>; |
| 66 | 73 |
| 67 // |application_language_code| should be a ISO 639-1 compliant string, e.g. | 74 // |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 | 75 // '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' | 76 // 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. | 77 // (British English person in the US) are not language codes. |
| 71 NTPSnippetsService(bool enabled, | 78 NTPSnippetsService(bool enabled, |
| 72 PrefService* pref_service, | 79 PrefService* pref_service, |
| 73 suggestions::SuggestionsService* suggestions_service, | 80 suggestions::SuggestionsService* suggestions_service, |
| 74 const std::string& application_language_code, | 81 const std::string& application_language_code, |
| 75 NTPSnippetsScheduler* scheduler, | 82 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 | 126 // Returns a reason why the service is disabled, or DisabledReason::NONE |
| 120 // if it's not. | 127 // if it's not. |
| 121 DisabledReason disabled_reason() const { | 128 DisabledReason disabled_reason() const { |
| 122 return snippets_status_service_->disabled_reason(); | 129 return snippets_status_service_->disabled_reason(); |
| 123 } | 130 } |
| 124 | 131 |
| 125 // (Re)schedules the periodic fetching of snippets. This is necessary because | 132 // (Re)schedules the periodic fetching of snippets. This is necessary because |
| 126 // the schedule depends on the time of day. | 133 // the schedule depends on the time of day. |
| 127 void RescheduleFetching(); | 134 void RescheduleFetching(); |
| 128 | 135 |
| 129 // Fetches the image for the snippet with the given |snippet_id| and runs the | 136 // ContentSuggestionsProvider implementation |
| 130 // |callback|. If that snippet doesn't exist or the fetch fails, the callback | 137 // TODO(pke): At some point reorder the implementations in the .cc file |
| 131 // gets a null image. | 138 // accordingly. |
| 132 void FetchSnippetImage(const std::string& snippet_id, | 139 void SetObserver(Observer* observer) override; |
| 133 const ImageFetchedCallback& callback); | 140 ContentSuggestionsCategoryStatus GetCategoryStatus( |
| 134 | 141 ContentSuggestionsCategory category) override; |
| 135 // Deletes all currently stored snippets. | 142 void DiscardSuggestion(const std::string& suggestion_id) override; |
| 136 void ClearSnippets(); | 143 void FetchSuggestionImage(const std::string& suggestion_id, |
| 137 | 144 const ImageFetchedCallback& callback) override; |
| 138 // Discards the snippet with the given |snippet_id|, if it exists. Returns | 145 void ClearCachedSuggestionsForDebugging() override; |
| 139 // true iff a snippet was discarded. | 146 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 | 147 |
| 145 // Returns the lists of suggestion hosts the snippets are restricted to. | 148 // Returns the lists of suggestion hosts the snippets are restricted to. |
| 146 std::set<std::string> GetSuggestionsHosts() const; | 149 std::set<std::string> GetSuggestionsHosts() const; |
| 147 | 150 |
| 148 // Observer accessors. | 151 // Observer accessors. |
| 149 void AddObserver(NTPSnippetsServiceObserver* observer); | 152 void AddObserver(NTPSnippetsServiceObserver* observer); |
| 150 void RemoveObserver(NTPSnippetsServiceObserver* observer); | 153 void RemoveObserver(NTPSnippetsServiceObserver* observer); |
| 151 | 154 |
| 152 // Returns the maximum number of snippets that will be shown at once. | 155 // Returns the maximum number of snippets that will be shown at once. |
| 153 static int GetMaxSnippetCountForTesting(); | 156 static int GetMaxSnippetCountForTesting(); |
| 154 | 157 |
| 155 private: | 158 private: |
| 156 FRIEND_TEST_ALL_PREFIXES(NTPSnippetsServiceTest, HistorySyncStateChanges); | 159 FRIEND_TEST_ALL_PREFIXES(NTPSnippetsServiceTest, HistorySyncStateChanges); |
| 157 | 160 |
| 161 // TODO(pke): As soon as the DisabledReason is replaced with the new status, |
| 162 // also remove the old State enum and replace it with |
| 163 // ContentSuggestionsCategoryStatus and a similar status diagram. |
| 158 // Possible state transitions: | 164 // Possible state transitions: |
| 159 // +------- NOT_INITED ------+ | 165 // +------- NOT_INITED ------+ |
| 160 // | / \ | | 166 // | / \ | |
| 161 // | READY <--> DISABLED <-+ | 167 // | READY <--> DISABLED <-+ |
| 162 // | \ / | 168 // | \ / |
| 163 // +-----> SHUT_DOWN | 169 // +-----> SHUT_DOWN |
| 164 enum class State { | 170 enum class State { |
| 165 // The service has just been created. Can change to states: | 171 // The service has just been created. Can change to states: |
| 166 // - DISABLED: if the constructor was called with |enabled == false| . In | 172 // - DISABLED: if the constructor was called with |enabled == false| . In |
| 167 // that case the service will stay disabled until it is shut | 173 // that case the service will stay disabled until it is shut |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 231 | 237 |
| 232 void FetchSnippetImageFromNetwork(const std::string& snippet_id, | 238 void FetchSnippetImageFromNetwork(const std::string& snippet_id, |
| 233 const ImageFetchedCallback& callback); | 239 const ImageFetchedCallback& callback); |
| 234 | 240 |
| 235 // Triggers a state transition depending on the provided reason to be | 241 // Triggers a state transition depending on the provided reason to be |
| 236 // disabled (or lack thereof). This method is called when a change is detected | 242 // disabled (or lack thereof). This method is called when a change is detected |
| 237 // by |snippets_status_service_| | 243 // by |snippets_status_service_| |
| 238 void UpdateStateForStatus(DisabledReason disabled_reason); | 244 void UpdateStateForStatus(DisabledReason disabled_reason); |
| 239 | 245 |
| 240 // Verifies state transitions (see |State|'s documentation) and applies them. | 246 // Verifies state transitions (see |State|'s documentation) and applies them. |
| 241 // Does nothing if called with the current state. | 247 // Also updates the provider status. Does nothing except updating the provider |
| 242 void EnterState(State state); | 248 // status if called with the current state. |
| 249 void EnterState(State state, ContentSuggestionsCategoryStatus status); |
| 243 | 250 |
| 244 // Enables the service and triggers a fetch if required. Do not call directly, | 251 // Enables the service and triggers a fetch if required. Do not call directly, |
| 245 // use |EnterState| instead. | 252 // use |EnterState| instead. |
| 246 void EnterStateEnabled(bool fetch_snippets); | 253 void EnterStateEnabled(bool fetch_snippets); |
| 247 | 254 |
| 248 // Disables the service. Do not call directly, use |EnterState| instead. | 255 // Disables the service. Do not call directly, use |EnterState| instead. |
| 249 void EnterStateDisabled(); | 256 void EnterStateDisabled(); |
| 250 | 257 |
| 251 // Applies the effects of the transition to the SHUT_DOWN state. Do not call | 258 // Applies the effects of the transition to the SHUT_DOWN state. Do not call |
| 252 // directly, use |EnterState| instead. | 259 // directly, use |EnterState| instead. |
| 253 void EnterStateShutdown(); | 260 void EnterStateShutdown(); |
| 254 | 261 |
| 262 // Converts the cached snippets to article content suggestions and notifies |
| 263 // the observers. |
| 264 void NotifyNewSuggestions(); |
| 265 |
| 266 // Notifies the content suggestions observer about a change in the |
| 267 // |category_status_|. |
| 268 void NotifyCategoryStatusChanged(); |
| 269 |
| 255 State state_; | 270 State state_; |
| 256 | 271 |
| 272 ContentSuggestionsCategoryStatus category_status_; |
| 273 |
| 257 PrefService* pref_service_; | 274 PrefService* pref_service_; |
| 258 | 275 |
| 259 suggestions::SuggestionsService* suggestions_service_; | 276 suggestions::SuggestionsService* suggestions_service_; |
| 260 | 277 |
| 261 // All current suggestions (i.e. not discarded ones). | 278 // All current suggestions (i.e. not discarded ones). |
| 262 NTPSnippet::PtrVector snippets_; | 279 NTPSnippet::PtrVector snippets_; |
| 263 | 280 |
| 264 // Suggestions that the user discarded. We keep these around until they expire | 281 // Suggestions that the user discarded. We keep these around until they expire |
| 265 // so we won't re-add them on the next fetch. | 282 // so we won't re-add them on the next fetch. |
| 266 NTPSnippet::PtrVector discarded_snippets_; | 283 NTPSnippet::PtrVector discarded_snippets_; |
| 267 | 284 |
| 268 // The ISO 639-1 code of the language used by the application. | 285 // The ISO 639-1 code of the language used by the application. |
| 269 const std::string application_language_code_; | 286 const std::string application_language_code_; |
| 270 | 287 |
| 271 // The observers. | 288 // The observers. |
| 289 // TODO(pke): This is an old kind of observers that will be removed. |
| 272 base::ObserverList<NTPSnippetsServiceObserver> observers_; | 290 base::ObserverList<NTPSnippetsServiceObserver> observers_; |
| 273 | 291 |
| 292 // The observer to deliver data to. |
| 293 Observer* observer_; |
| 294 |
| 274 // Scheduler for fetching snippets. Not owned. | 295 // Scheduler for fetching snippets. Not owned. |
| 275 NTPSnippetsScheduler* scheduler_; | 296 NTPSnippetsScheduler* scheduler_; |
| 276 | 297 |
| 277 // The subscription to the SuggestionsService. When the suggestions change, | 298 // The subscription to the SuggestionsService. When the suggestions change, |
| 278 // SuggestionsService will call |OnSuggestionsChanged|, which triggers an | 299 // SuggestionsService will call |OnSuggestionsChanged|, which triggers an |
| 279 // update to the set of snippets. | 300 // update to the set of snippets. |
| 280 using SuggestionsSubscription = | 301 using SuggestionsSubscription = |
| 281 suggestions::SuggestionsService::ResponseCallbackList::Subscription; | 302 suggestions::SuggestionsService::ResponseCallbackList::Subscription; |
| 282 std::unique_ptr<SuggestionsSubscription> suggestions_service_subscription_; | 303 std::unique_ptr<SuggestionsSubscription> suggestions_service_subscription_; |
| 283 | 304 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 296 // The service that provides events and data about the signin and sync state. | 317 // The service that provides events and data about the signin and sync state. |
| 297 std::unique_ptr<NTPSnippetsStatusService> snippets_status_service_; | 318 std::unique_ptr<NTPSnippetsStatusService> snippets_status_service_; |
| 298 | 319 |
| 299 // Set to true if FetchSnippets is called before the database has been loaded. | 320 // Set to true if FetchSnippets is called before the database has been loaded. |
| 300 // The fetch will be executed after the database load finishes. | 321 // The fetch will be executed after the database load finishes. |
| 301 bool fetch_after_load_; | 322 bool fetch_after_load_; |
| 302 | 323 |
| 303 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsService); | 324 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsService); |
| 304 }; | 325 }; |
| 305 | 326 |
| 327 // TODO(pke): Remove this when snippets internals don't access this service |
| 328 // directly anymore. |
| 306 class NTPSnippetsServiceObserver { | 329 class NTPSnippetsServiceObserver { |
| 307 public: | 330 public: |
| 308 // Sent every time the service loads a new set of data. | 331 // Sent every time the service loads a new set of data. |
| 309 virtual void NTPSnippetsServiceLoaded() = 0; | 332 virtual void NTPSnippetsServiceLoaded() = 0; |
| 310 | 333 |
| 311 // Sent when the service is shutting down. | 334 // Sent when the service is shutting down. |
| 312 virtual void NTPSnippetsServiceShutdown() = 0; | 335 virtual void NTPSnippetsServiceShutdown() = 0; |
| 313 | 336 |
| 314 // Sent when the state of the service is changing. Something changed in its | 337 // Sent when the state of the service is changing. Something changed in its |
| 315 // dependencies so it's notifying observers about incoming data changes. | 338 // dependencies so it's notifying observers about incoming data changes. |
| 316 // If the service might be enabled, DisabledReason::NONE will be provided. | 339 // If the service might be enabled, DisabledReason::NONE will be provided. |
| 317 virtual void NTPSnippetsServiceDisabledReasonChanged(DisabledReason) = 0; | 340 virtual void NTPSnippetsServiceDisabledReasonChanged(DisabledReason) = 0; |
| 318 | 341 |
| 319 protected: | 342 protected: |
| 320 virtual ~NTPSnippetsServiceObserver() {} | 343 virtual ~NTPSnippetsServiceObserver() {} |
| 321 }; | 344 }; |
| 322 | 345 |
| 323 } // namespace ntp_snippets | 346 } // namespace ntp_snippets |
| 324 | 347 |
| 325 #endif // COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_SERVICE_H_ | 348 #endif // COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_SERVICE_H_ |
| OLD | NEW |