| 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/gtest_prod_util.h" | 15 #include "base/gtest_prod_util.h" |
| 16 #include "base/macros.h" | 16 #include "base/macros.h" |
| 17 #include "base/observer_list.h" | 17 #include "base/observer_list.h" |
| 18 #include "base/scoped_observer.h" | 18 #include "base/scoped_observer.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/keyed_service/core/keyed_service.h" | 21 #include "components/keyed_service/core/keyed_service.h" |
| 21 #include "components/ntp_snippets/ntp_snippet.h" | 22 #include "components/ntp_snippets/ntp_snippet.h" |
| 22 #include "components/ntp_snippets/ntp_snippets_fetcher.h" | 23 #include "components/ntp_snippets/ntp_snippets_fetcher.h" |
| 23 #include "components/ntp_snippets/ntp_snippets_scheduler.h" | 24 #include "components/ntp_snippets/ntp_snippets_scheduler.h" |
| 24 #include "components/suggestions/suggestions_service.h" | 25 #include "components/suggestions/suggestions_service.h" |
| 25 #include "components/sync_driver/sync_service_observer.h" | 26 #include "components/sync_driver/sync_service_observer.h" |
| 26 | 27 |
| 27 class PrefRegistrySimple; | 28 class PrefRegistrySimple; |
| 28 class PrefService; | 29 class PrefService; |
| 29 | 30 |
| 30 namespace base { | 31 namespace base { |
| 32 class RefCountedMemory; |
| 31 class Value; | 33 class Value; |
| 32 } | 34 } |
| 33 | 35 |
| 34 namespace gfx { | 36 namespace gfx { |
| 35 class Image; | 37 class Image; |
| 36 } | 38 } |
| 37 | 39 |
| 38 namespace image_fetcher { | 40 namespace image_fetcher { |
| 41 class ImageDecoder; |
| 39 class ImageFetcher; | 42 class ImageFetcher; |
| 40 } | 43 } |
| 41 | 44 |
| 42 namespace suggestions { | 45 namespace suggestions { |
| 43 class SuggestionsProfile; | 46 class SuggestionsProfile; |
| 44 } | 47 } |
| 45 | 48 |
| 46 namespace sync_driver { | 49 namespace sync_driver { |
| 47 class SyncService; | 50 class SyncService; |
| 48 } | 51 } |
| 49 | 52 |
| 50 namespace ntp_snippets { | 53 namespace ntp_snippets { |
| 51 | 54 |
| 52 enum class DisabledReason { | 55 enum class DisabledReason { |
| 53 // Snippets are enabled | 56 // Snippets are enabled |
| 54 NONE, | 57 NONE, |
| 55 // Snippets have been disabled as part of the service configuration. | 58 // Snippets have been disabled as part of the service configuration. |
| 56 EXPLICITLY_DISABLED, | 59 EXPLICITLY_DISABLED, |
| 57 // History sync is not enabled, and the service requires it to be enabled. | 60 // History sync is not enabled, and the service requires it to be enabled. |
| 58 HISTORY_SYNC_DISABLED, | 61 HISTORY_SYNC_DISABLED, |
| 59 // The sync service is not completely initialized, and the status is unknown. | 62 // The sync service is not completely initialized, and the status is unknown. |
| 60 HISTORY_SYNC_STATE_UNKNOWN | 63 HISTORY_SYNC_STATE_UNKNOWN |
| 61 }; | 64 }; |
| 62 | 65 |
| 63 class NTPSnippetsDatabase; | 66 class NTPSnippetsDatabase; |
| 64 class NTPSnippetsServiceObserver; | 67 class NTPSnippetsServiceObserver; |
| 65 | 68 |
| 66 // Stores and vends fresh content data for the NTP. | 69 // Stores and vends fresh content data for the NTP. |
| 67 class NTPSnippetsService : public KeyedService, | 70 class NTPSnippetsService : public KeyedService, |
| 68 public sync_driver::SyncServiceObserver { | 71 public sync_driver::SyncServiceObserver, |
| 72 public image_fetcher::ImageFetcherDelegate { |
| 69 public: | 73 public: |
| 70 using ImageFetchedCallback = | 74 using ImageFetchedCallback = |
| 71 base::Callback<void(const std::string& snippet_id, const gfx::Image&)>; | 75 base::Callback<void(const std::string& snippet_id, const gfx::Image&)>; |
| 72 | 76 |
| 73 // |application_language_code| should be a ISO 639-1 compliant string, e.g. | 77 // |application_language_code| should be a ISO 639-1 compliant string, e.g. |
| 74 // 'en' or 'en-US'. Note that this code should only specify the language, not | 78 // 'en' or 'en-US'. Note that this code should only specify the language, not |
| 75 // the locale, so 'en_US' (English language with US locale) and 'en-GB_US' | 79 // the locale, so 'en_US' (English language with US locale) and 'en-GB_US' |
| 76 // (British English person in the US) are not language codes. | 80 // (British English person in the US) are not language codes. |
| 77 NTPSnippetsService(bool enabled, | 81 NTPSnippetsService(bool enabled, |
| 78 PrefService* pref_service, | 82 PrefService* pref_service, |
| 79 sync_driver::SyncService* sync_service, | 83 sync_driver::SyncService* sync_service, |
| 80 suggestions::SuggestionsService* suggestions_service, | 84 suggestions::SuggestionsService* suggestions_service, |
| 81 const std::string& application_language_code, | 85 const std::string& application_language_code, |
| 82 NTPSnippetsScheduler* scheduler, | 86 NTPSnippetsScheduler* scheduler, |
| 83 std::unique_ptr<NTPSnippetsFetcher> snippets_fetcher, | 87 std::unique_ptr<NTPSnippetsFetcher> snippets_fetcher, |
| 84 std::unique_ptr<image_fetcher::ImageFetcher> image_fetcher, | 88 std::unique_ptr<image_fetcher::ImageFetcher> image_fetcher, |
| 89 std::unique_ptr<image_fetcher::ImageDecoder> image_decoder, |
| 85 std::unique_ptr<NTPSnippetsDatabase> database); | 90 std::unique_ptr<NTPSnippetsDatabase> database); |
| 86 ~NTPSnippetsService() override; | 91 ~NTPSnippetsService() override; |
| 87 | 92 |
| 88 static void RegisterProfilePrefs(PrefRegistrySimple* registry); | 93 static void RegisterProfilePrefs(PrefRegistrySimple* registry); |
| 89 | 94 |
| 90 // Inherited from KeyedService. | 95 // Inherited from KeyedService. |
| 91 void Shutdown() override; | 96 void Shutdown() override; |
| 92 | 97 |
| 93 // Returns whether the service is ready. While this is false, the list of | 98 // Returns whether the service is ready. While this is false, the list of |
| 94 // snippets will be empty, and all modifications to it (fetch, discard, etc) | 99 // snippets will be empty, and all modifications to it (fetch, discard, etc) |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 190 DISABLED, | 195 DISABLED, |
| 191 | 196 |
| 192 // The service shutdown and can't be used anymore. This state is checked | 197 // The service shutdown and can't be used anymore. This state is checked |
| 193 // for early exit in callbacks from observers. | 198 // for early exit in callbacks from observers. |
| 194 SHUT_DOWN | 199 SHUT_DOWN |
| 195 }; | 200 }; |
| 196 | 201 |
| 197 // sync_driver::SyncServiceObserver implementation. | 202 // sync_driver::SyncServiceObserver implementation. |
| 198 void OnStateChanged() override; | 203 void OnStateChanged() override; |
| 199 | 204 |
| 205 // image_fetcher::ImageFetcherDelegate implementation. |
| 206 void OnImageDataFetched(const std::string& snippet_id, |
| 207 const std::string& image_data) override; |
| 208 |
| 200 // Callback for the NTPSnippetsDatabase. | 209 // Callback for the NTPSnippetsDatabase. |
| 201 void OnDatabaseLoaded(NTPSnippet::PtrVector snippets); | 210 void OnDatabaseLoaded(NTPSnippet::PtrVector snippets); |
| 202 | 211 |
| 203 // Callback for the SuggestionsService. | 212 // Callback for the SuggestionsService. |
| 204 void OnSuggestionsChanged(const suggestions::SuggestionsProfile& suggestions); | 213 void OnSuggestionsChanged(const suggestions::SuggestionsProfile& suggestions); |
| 205 | 214 |
| 206 // Callback for the NTPSnippetsFetcher. | 215 // Callback for the NTPSnippetsFetcher. |
| 207 void OnFetchFinished(NTPSnippetsFetcher::OptionalSnippets snippets); | 216 void OnFetchFinished(NTPSnippetsFetcher::OptionalSnippets snippets); |
| 208 | 217 |
| 209 // Merges newly available snippets with the previously available list. | 218 // Merges newly available snippets with the previously available list. |
| 210 void MergeSnippets(NTPSnippet::PtrVector new_snippets); | 219 void MergeSnippets(NTPSnippet::PtrVector new_snippets); |
| 211 | 220 |
| 212 std::set<std::string> GetSnippetHostsFromPrefs() const; | 221 std::set<std::string> GetSnippetHostsFromPrefs() const; |
| 213 void StoreSnippetHostsToPrefs(const std::set<std::string>& hosts); | 222 void StoreSnippetHostsToPrefs(const std::set<std::string>& hosts); |
| 214 | 223 |
| 215 // Removes the expired snippets (including discarded) from the service and the | 224 // Removes the expired snippets (including discarded) from the service and the |
| 216 // database, and schedules another pass for the next expiration. | 225 // database, and schedules another pass for the next expiration. |
| 217 void ClearExpiredSnippets(); | 226 void ClearExpiredSnippets(); |
| 218 | 227 |
| 219 // Completes the initialization phase of the service, registering the last | 228 // Completes the initialization phase of the service, registering the last |
| 220 // observers. This is done after construction, once the database is loaded. | 229 // observers. This is done after construction, once the database is loaded. |
| 221 void FinishInitialization(); | 230 void FinishInitialization(); |
| 222 | 231 |
| 223 void LoadingSnippetsFinished(); | 232 void LoadingSnippetsFinished(); |
| 224 | 233 |
| 234 void OnSnippetImageFetchedFromDatabase(const std::string& snippet_id, |
| 235 const ImageFetchedCallback& callback, |
| 236 std::string data); |
| 237 |
| 238 void OnSnippetImageDecoded(const std::string& snippet_id, |
| 239 const ImageFetchedCallback& callback, |
| 240 const gfx::Image& image); |
| 241 |
| 242 void FetchSnippetImageFromNetwork(const std::string& snippet_id, |
| 243 const ImageFetchedCallback& callback); |
| 244 |
| 225 // Returns whether the service should be enabled or disable depending on its | 245 // Returns whether the service should be enabled or disable depending on its |
| 226 // internal state and the state of its dependencies. | 246 // internal state and the state of its dependencies. |
| 227 State GetStateForDependenciesStatus(); | 247 State GetStateForDependenciesStatus(); |
| 228 | 248 |
| 229 // Verifies state transitions (see |State|'s documentation) and applies them. | 249 // Verifies state transitions (see |State|'s documentation) and applies them. |
| 230 // Does nothing if called with the current state. | 250 // Does nothing if called with the current state. |
| 231 void EnterState(State state); | 251 void EnterState(State state); |
| 232 | 252 |
| 233 // Enables the service and triggers a fetch if required. Do not call directly, | 253 // Enables the service and triggers a fetch if required. Do not call directly, |
| 234 // use |EnterState| instead. | 254 // use |EnterState| instead. |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 285 suggestions::SuggestionsService::ResponseCallbackList::Subscription; | 305 suggestions::SuggestionsService::ResponseCallbackList::Subscription; |
| 286 std::unique_ptr<SuggestionsSubscription> suggestions_service_subscription_; | 306 std::unique_ptr<SuggestionsSubscription> suggestions_service_subscription_; |
| 287 | 307 |
| 288 // The snippets fetcher. | 308 // The snippets fetcher. |
| 289 std::unique_ptr<NTPSnippetsFetcher> snippets_fetcher_; | 309 std::unique_ptr<NTPSnippetsFetcher> snippets_fetcher_; |
| 290 | 310 |
| 291 // Timer that calls us back when the next snippet expires. | 311 // Timer that calls us back when the next snippet expires. |
| 292 base::OneShotTimer expiry_timer_; | 312 base::OneShotTimer expiry_timer_; |
| 293 | 313 |
| 294 std::unique_ptr<image_fetcher::ImageFetcher> image_fetcher_; | 314 std::unique_ptr<image_fetcher::ImageFetcher> image_fetcher_; |
| 315 std::unique_ptr<image_fetcher::ImageDecoder> image_decoder_; |
| 295 | 316 |
| 296 // The database for persisting snippets. | 317 // The database for persisting snippets. |
| 297 std::unique_ptr<NTPSnippetsDatabase> database_; | 318 std::unique_ptr<NTPSnippetsDatabase> database_; |
| 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 |
| 306 class NTPSnippetsServiceObserver { | 327 class NTPSnippetsServiceObserver { |
| 307 public: | 328 public: |
| 308 // Sent every time the service loads a new set of data. | 329 // Sent every time the service loads a new set of data. |
| 309 virtual void NTPSnippetsServiceLoaded() = 0; | 330 virtual void NTPSnippetsServiceLoaded() = 0; |
| 310 // Sent when the service is shutting down. | 331 // Sent when the service is shutting down. |
| 311 virtual void NTPSnippetsServiceShutdown() = 0; | 332 virtual void NTPSnippetsServiceShutdown() = 0; |
| 312 // Sent when the service has been disabled. Can be from explicit user action | 333 // Sent when the service has been disabled. Can be from explicit user action |
| 313 // or because a requirement (e.g. History Sync) is not fulfilled anymore. | 334 // or because a requirement (e.g. History Sync) is not fulfilled anymore. |
| 314 virtual void NTPSnippetsServiceDisabled() = 0; | 335 virtual void NTPSnippetsServiceDisabled() = 0; |
| 315 | 336 |
| 316 protected: | 337 protected: |
| 317 virtual ~NTPSnippetsServiceObserver() {} | 338 virtual ~NTPSnippetsServiceObserver() {} |
| 318 }; | 339 }; |
| 319 | 340 |
| 320 } // namespace ntp_snippets | 341 } // namespace ntp_snippets |
| 321 | 342 |
| 322 #endif // COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_SERVICE_H_ | 343 #endif // COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_SERVICE_H_ |
| OLD | NEW |