| 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" | |
| 19 #include "base/timer/timer.h" | 18 #include "base/timer/timer.h" |
| 20 #include "components/image_fetcher/image_fetcher_delegate.h" | 19 #include "components/image_fetcher/image_fetcher_delegate.h" |
| 21 #include "components/keyed_service/core/keyed_service.h" | 20 #include "components/keyed_service/core/keyed_service.h" |
| 22 #include "components/ntp_snippets/ntp_snippet.h" | 21 #include "components/ntp_snippets/ntp_snippet.h" |
| 23 #include "components/ntp_snippets/ntp_snippets_fetcher.h" | 22 #include "components/ntp_snippets/ntp_snippets_fetcher.h" |
| 24 #include "components/ntp_snippets/ntp_snippets_scheduler.h" | 23 #include "components/ntp_snippets/ntp_snippets_scheduler.h" |
| 24 #include "components/ntp_snippets/ntp_snippets_status_service.h" |
| 25 #include "components/suggestions/suggestions_service.h" | 25 #include "components/suggestions/suggestions_service.h" |
| 26 #include "components/sync_driver/sync_service_observer.h" | 26 #include "components/sync_driver/sync_service_observer.h" |
| 27 | 27 |
| 28 class PrefRegistrySimple; | 28 class PrefRegistrySimple; |
| 29 class PrefService; | 29 class PrefService; |
| 30 class SigninManagerBase; |
| 30 | 31 |
| 31 namespace base { | 32 namespace base { |
| 32 class RefCountedMemory; | 33 class RefCountedMemory; |
| 33 class Value; | 34 class Value; |
| 34 } | 35 } |
| 35 | 36 |
| 36 namespace gfx { | 37 namespace gfx { |
| 37 class Image; | 38 class Image; |
| 38 } | 39 } |
| 39 | 40 |
| 40 namespace image_fetcher { | 41 namespace image_fetcher { |
| 41 class ImageDecoder; | 42 class ImageDecoder; |
| 42 class ImageFetcher; | 43 class ImageFetcher; |
| 43 } | 44 } |
| 44 | 45 |
| 45 namespace suggestions { | 46 namespace suggestions { |
| 46 class SuggestionsProfile; | 47 class SuggestionsProfile; |
| 47 } | 48 } |
| 48 | 49 |
| 49 namespace sync_driver { | 50 namespace sync_driver { |
| 50 class SyncService; | 51 class SyncService; |
| 51 } | 52 } |
| 52 | 53 |
| 53 namespace ntp_snippets { | 54 namespace ntp_snippets { |
| 54 | 55 |
| 55 enum class DisabledReason { | |
| 56 // Snippets are enabled | |
| 57 NONE, | |
| 58 // Snippets have been disabled as part of the service configuration. | |
| 59 EXPLICITLY_DISABLED, | |
| 60 // History sync is not enabled, and the service requires it to be enabled. | |
| 61 HISTORY_SYNC_DISABLED, | |
| 62 // The sync service is not completely initialized, and the status is unknown. | |
| 63 HISTORY_SYNC_STATE_UNKNOWN | |
| 64 }; | |
| 65 | |
| 66 class NTPSnippetsDatabase; | 56 class NTPSnippetsDatabase; |
| 67 class NTPSnippetsServiceObserver; | 57 class NTPSnippetsServiceObserver; |
| 68 | 58 |
| 69 // Stores and vends fresh content data for the NTP. | 59 // Stores and vends fresh content data for the NTP. |
| 70 class NTPSnippetsService : public KeyedService, | 60 class NTPSnippetsService : public KeyedService, |
| 71 public sync_driver::SyncServiceObserver, | |
| 72 public image_fetcher::ImageFetcherDelegate { | 61 public image_fetcher::ImageFetcherDelegate { |
| 73 public: | 62 public: |
| 74 using ImageFetchedCallback = | 63 using ImageFetchedCallback = |
| 75 base::Callback<void(const std::string& snippet_id, const gfx::Image&)>; | 64 base::Callback<void(const std::string& snippet_id, const gfx::Image&)>; |
| 76 | 65 |
| 77 // |application_language_code| should be a ISO 639-1 compliant string, e.g. | 66 // |application_language_code| should be a ISO 639-1 compliant string, e.g. |
| 78 // 'en' or 'en-US'. Note that this code should only specify the language, not | 67 // 'en' or 'en-US'. Note that this code should only specify the language, not |
| 79 // the locale, so 'en_US' (English language with US locale) and 'en-GB_US' | 68 // the locale, so 'en_US' (English language with US locale) and 'en-GB_US' |
| 80 // (British English person in the US) are not language codes. | 69 // (British English person in the US) are not language codes. |
| 81 NTPSnippetsService(bool enabled, | 70 NTPSnippetsService(bool enabled, |
| 82 PrefService* pref_service, | 71 PrefService* pref_service, |
| 83 sync_driver::SyncService* sync_service, | |
| 84 suggestions::SuggestionsService* suggestions_service, | 72 suggestions::SuggestionsService* suggestions_service, |
| 85 const std::string& application_language_code, | 73 const std::string& application_language_code, |
| 86 NTPSnippetsScheduler* scheduler, | 74 NTPSnippetsScheduler* scheduler, |
| 87 std::unique_ptr<NTPSnippetsFetcher> snippets_fetcher, | 75 std::unique_ptr<NTPSnippetsFetcher> snippets_fetcher, |
| 88 std::unique_ptr<image_fetcher::ImageFetcher> image_fetcher, | 76 std::unique_ptr<image_fetcher::ImageFetcher> image_fetcher, |
| 89 std::unique_ptr<image_fetcher::ImageDecoder> image_decoder, | 77 std::unique_ptr<image_fetcher::ImageDecoder> image_decoder, |
| 90 std::unique_ptr<NTPSnippetsDatabase> database); | 78 std::unique_ptr<NTPSnippetsDatabase> database, |
| 79 std::unique_ptr<NTPSnippetsStatusService> status_service); |
| 80 |
| 91 ~NTPSnippetsService() override; | 81 ~NTPSnippetsService() override; |
| 92 | 82 |
| 93 static void RegisterProfilePrefs(PrefRegistrySimple* registry); | 83 static void RegisterProfilePrefs(PrefRegistrySimple* registry); |
| 94 | 84 |
| 95 // Inherited from KeyedService. | 85 // Inherited from KeyedService. |
| 96 void Shutdown() override; | 86 void Shutdown() override; |
| 97 | 87 |
| 98 // Returns whether the service is ready. While this is false, the list of | 88 // Returns whether the service is ready. While this is false, the list of |
| 99 // snippets will be empty, and all modifications to it (fetch, discard, etc) | 89 // snippets will be empty, and all modifications to it (fetch, discard, etc) |
| 100 // will be ignored. | 90 // will be ignored. |
| (...skipping 17 matching lines...) Expand all Loading... |
| 118 // Returns the list of snippets previously discarded by the user (that are | 108 // Returns the list of snippets previously discarded by the user (that are |
| 119 // not expired yet). | 109 // not expired yet). |
| 120 const NTPSnippet::PtrVector& discarded_snippets() const { | 110 const NTPSnippet::PtrVector& discarded_snippets() const { |
| 121 return discarded_snippets_; | 111 return discarded_snippets_; |
| 122 } | 112 } |
| 123 | 113 |
| 124 const NTPSnippetsFetcher* snippets_fetcher() const { | 114 const NTPSnippetsFetcher* snippets_fetcher() const { |
| 125 return snippets_fetcher_.get(); | 115 return snippets_fetcher_.get(); |
| 126 } | 116 } |
| 127 | 117 |
| 118 // Returns a reason why the service is disabled, or DisabledReason::NONE |
| 119 // if it's not. |
| 120 DisabledReason disabled_reason() const { |
| 121 return snippets_status_service_->disabled_reason(); |
| 122 } |
| 123 |
| 128 // (Re)schedules the periodic fetching of snippets. This is necessary because | 124 // (Re)schedules the periodic fetching of snippets. This is necessary because |
| 129 // the schedule depends on the time of day. | 125 // the schedule depends on the time of day. |
| 130 void RescheduleFetching(); | 126 void RescheduleFetching(); |
| 131 | 127 |
| 132 // Fetches the image for the snippet with the given |snippet_id| and runs the | 128 // Fetches the image for the snippet with the given |snippet_id| and runs the |
| 133 // |callback|. If that snippet doesn't exist or the fetch fails, the callback | 129 // |callback|. If that snippet doesn't exist or the fetch fails, the callback |
| 134 // gets a null image. | 130 // gets a null image. |
| 135 void FetchSnippetImage(const std::string& snippet_id, | 131 void FetchSnippetImage(const std::string& snippet_id, |
| 136 const ImageFetchedCallback& callback); | 132 const ImageFetchedCallback& callback); |
| 137 | 133 |
| 138 // Deletes all currently stored snippets. | 134 // Deletes all currently stored snippets. |
| 139 void ClearSnippets(); | 135 void ClearSnippets(); |
| 140 | 136 |
| 141 // Discards the snippet with the given |snippet_id|, if it exists. Returns | 137 // Discards the snippet with the given |snippet_id|, if it exists. Returns |
| 142 // true iff a snippet was discarded. | 138 // true iff a snippet was discarded. |
| 143 bool DiscardSnippet(const std::string& snippet_id); | 139 bool DiscardSnippet(const std::string& snippet_id); |
| 144 | 140 |
| 145 // Clears the lists of snippets previously discarded by the user. | 141 // Clears the lists of snippets previously discarded by the user. |
| 146 void ClearDiscardedSnippets(); | 142 void ClearDiscardedSnippets(); |
| 147 | 143 |
| 148 // Returns the lists of suggestion hosts the snippets are restricted to. | 144 // Returns the lists of suggestion hosts the snippets are restricted to. |
| 149 std::set<std::string> GetSuggestionsHosts() const; | 145 std::set<std::string> GetSuggestionsHosts() const; |
| 150 | 146 |
| 151 // Observer accessors. | 147 // Observer accessors. |
| 152 void AddObserver(NTPSnippetsServiceObserver* observer); | 148 void AddObserver(NTPSnippetsServiceObserver* observer); |
| 153 void RemoveObserver(NTPSnippetsServiceObserver* observer); | 149 void RemoveObserver(NTPSnippetsServiceObserver* observer); |
| 154 | 150 |
| 155 // Returns a reason why the service could be disabled, or DisabledReason::NONE | |
| 156 // if it's not. | |
| 157 DisabledReason GetDisabledReason() const; | |
| 158 | |
| 159 // 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. |
| 160 static int GetMaxSnippetCountForTesting(); | 152 static int GetMaxSnippetCountForTesting(); |
| 161 | 153 |
| 162 private: | 154 private: |
| 163 FRIEND_TEST_ALL_PREFIXES(NTPSnippetsServiceTest, SyncStateCompatibility); | |
| 164 FRIEND_TEST_ALL_PREFIXES(NTPSnippetsServiceTest, HistorySyncStateChanges); | 155 FRIEND_TEST_ALL_PREFIXES(NTPSnippetsServiceTest, HistorySyncStateChanges); |
| 165 | 156 |
| 166 // Possible state transitions: | 157 // Possible state transitions: |
| 167 // +------- NOT_INITED ------+ | 158 // +------- NOT_INITED ------+ |
| 168 // | / \ | | 159 // | / \ | |
| 169 // | READY <--> DISABLED <-+ | 160 // | READY <--> DISABLED <-+ |
| 170 // | \ / | 161 // | \ / |
| 171 // +-----> SHUT_DOWN | 162 // +-----> SHUT_DOWN |
| 172 enum class State { | 163 enum class State { |
| 173 // The service has just been created. Can change to states: | 164 // The service has just been created. Can change to states: |
| (...skipping 18 matching lines...) Expand all Loading... |
| 192 // - READY: when the global Chrome state changes, for example after | 183 // - READY: when the global Chrome state changes, for example after |
| 193 // |OnStateChanged| is called and sync is enabled. | 184 // |OnStateChanged| is called and sync is enabled. |
| 194 // - SHUT_DOWN: when |Shutdown| is called, during the browser shutdown. | 185 // - SHUT_DOWN: when |Shutdown| is called, during the browser shutdown. |
| 195 DISABLED, | 186 DISABLED, |
| 196 | 187 |
| 197 // The service shutdown and can't be used anymore. This state is checked | 188 // The service shutdown and can't be used anymore. This state is checked |
| 198 // for early exit in callbacks from observers. | 189 // for early exit in callbacks from observers. |
| 199 SHUT_DOWN | 190 SHUT_DOWN |
| 200 }; | 191 }; |
| 201 | 192 |
| 202 // sync_driver::SyncServiceObserver implementation. | |
| 203 void OnStateChanged() override; | |
| 204 | |
| 205 // image_fetcher::ImageFetcherDelegate implementation. | 193 // image_fetcher::ImageFetcherDelegate implementation. |
| 206 void OnImageDataFetched(const std::string& snippet_id, | 194 void OnImageDataFetched(const std::string& snippet_id, |
| 207 const std::string& image_data) override; | 195 const std::string& image_data) override; |
| 208 | 196 |
| 209 // Callbacks for the NTPSnippetsDatabase. | 197 // Callbacks for the NTPSnippetsDatabase. |
| 210 void OnDatabaseLoaded(NTPSnippet::PtrVector snippets); | 198 void OnDatabaseLoaded(NTPSnippet::PtrVector snippets); |
| 211 void OnDatabaseError(); | 199 void OnDatabaseError(); |
| 212 | 200 |
| 213 // Callback for the SuggestionsService. | 201 // Callback for the SuggestionsService. |
| 214 void OnSuggestionsChanged(const suggestions::SuggestionsProfile& suggestions); | 202 void OnSuggestionsChanged(const suggestions::SuggestionsProfile& suggestions); |
| (...skipping 21 matching lines...) Expand all Loading... |
| 236 const ImageFetchedCallback& callback, | 224 const ImageFetchedCallback& callback, |
| 237 std::string data); | 225 std::string data); |
| 238 | 226 |
| 239 void OnSnippetImageDecoded(const std::string& snippet_id, | 227 void OnSnippetImageDecoded(const std::string& snippet_id, |
| 240 const ImageFetchedCallback& callback, | 228 const ImageFetchedCallback& callback, |
| 241 const gfx::Image& image); | 229 const gfx::Image& image); |
| 242 | 230 |
| 243 void FetchSnippetImageFromNetwork(const std::string& snippet_id, | 231 void FetchSnippetImageFromNetwork(const std::string& snippet_id, |
| 244 const ImageFetchedCallback& callback); | 232 const ImageFetchedCallback& callback); |
| 245 | 233 |
| 246 // Returns whether the service should be enabled or disable depending on its | 234 // Triggers a state transition depending on the provided reason to be |
| 247 // internal state and the state of its dependencies. | 235 // disabled (or lack thereof). This method is called when a change is detected |
| 248 State GetStateForDependenciesStatus(); | 236 // by |snippets_status_service_| |
| 237 void UpdateStateForStatus(DisabledReason disabled_reason); |
| 249 | 238 |
| 250 // Verifies state transitions (see |State|'s documentation) and applies them. | 239 // Verifies state transitions (see |State|'s documentation) and applies them. |
| 251 // Does nothing if called with the current state. | 240 // Does nothing if called with the current state. |
| 252 void EnterState(State state); | 241 void EnterState(State state); |
| 253 | 242 |
| 254 // Enables the service and triggers a fetch if required. Do not call directly, | 243 // Enables the service and triggers a fetch if required. Do not call directly, |
| 255 // use |EnterState| instead. | 244 // use |EnterState| instead. |
| 256 void EnterStateEnabled(bool fetch_snippets); | 245 void EnterStateEnabled(bool fetch_snippets); |
| 257 | 246 |
| 258 // Disables the service. Do not call directly, use |EnterState| instead. | 247 // Disables the service. Do not call directly, use |EnterState| instead. |
| 259 void EnterStateDisabled(); | 248 void EnterStateDisabled(); |
| 260 | 249 |
| 261 // 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 |
| 262 // directly, use |EnterState| instead. | 251 // directly, use |EnterState| instead. |
| 263 void EnterStateShutdown(); | 252 void EnterStateShutdown(); |
| 264 | 253 |
| 265 void ClearDeprecatedPrefs(); | 254 void ClearDeprecatedPrefs(); |
| 266 | 255 |
| 267 State state_; | 256 State state_; |
| 268 | 257 |
| 269 // The service was set up to be disabled and should not be enabled by any | |
| 270 // state change. We track this to make sure we clear scheduled tasks, which | |
| 271 // persist even when Chrome is stopped. | |
| 272 bool explicitly_disabled_; | |
| 273 | |
| 274 PrefService* pref_service_; | 258 PrefService* pref_service_; |
| 275 | 259 |
| 276 sync_driver::SyncService* sync_service_; | |
| 277 | |
| 278 // The observer for the SyncService. When the sync state changes, | |
| 279 // SyncService will call |OnStateChanged|, which is propagated to the | |
| 280 // snippet observers. | |
| 281 ScopedObserver<sync_driver::SyncService, sync_driver::SyncServiceObserver> | |
| 282 sync_service_observer_; | |
| 283 | |
| 284 suggestions::SuggestionsService* suggestions_service_; | 260 suggestions::SuggestionsService* suggestions_service_; |
| 285 | 261 |
| 286 // All current suggestions (i.e. not discarded ones). | 262 // All current suggestions (i.e. not discarded ones). |
| 287 NTPSnippet::PtrVector snippets_; | 263 NTPSnippet::PtrVector snippets_; |
| 288 | 264 |
| 289 // Suggestions that the user discarded. We keep these around until they expire | 265 // Suggestions that the user discarded. We keep these around until they expire |
| 290 // so we won't re-add them on the next fetch. | 266 // so we won't re-add them on the next fetch. |
| 291 NTPSnippet::PtrVector discarded_snippets_; | 267 NTPSnippet::PtrVector discarded_snippets_; |
| 292 | 268 |
| 293 // The ISO 639-1 code of the language used by the application. | 269 // The ISO 639-1 code of the language used by the application. |
| (...skipping 17 matching lines...) Expand all Loading... |
| 311 | 287 |
| 312 // Timer that calls us back when the next snippet expires. | 288 // Timer that calls us back when the next snippet expires. |
| 313 base::OneShotTimer expiry_timer_; | 289 base::OneShotTimer expiry_timer_; |
| 314 | 290 |
| 315 std::unique_ptr<image_fetcher::ImageFetcher> image_fetcher_; | 291 std::unique_ptr<image_fetcher::ImageFetcher> image_fetcher_; |
| 316 std::unique_ptr<image_fetcher::ImageDecoder> image_decoder_; | 292 std::unique_ptr<image_fetcher::ImageDecoder> image_decoder_; |
| 317 | 293 |
| 318 // The database for persisting snippets. | 294 // The database for persisting snippets. |
| 319 std::unique_ptr<NTPSnippetsDatabase> database_; | 295 std::unique_ptr<NTPSnippetsDatabase> database_; |
| 320 | 296 |
| 297 // The service that provides events and data about the signin and sync state. |
| 298 std::unique_ptr<NTPSnippetsStatusService> snippets_status_service_; |
| 299 |
| 321 // Set to true if FetchSnippets is called before the database has been loaded. | 300 // Set to true if FetchSnippets is called before the database has been loaded. |
| 322 // The fetch will be executed after the database load finishes. | 301 // The fetch will be executed after the database load finishes. |
| 323 bool fetch_after_load_; | 302 bool fetch_after_load_; |
| 324 | 303 |
| 325 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsService); | 304 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsService); |
| 326 }; | 305 }; |
| 327 | 306 |
| 328 class NTPSnippetsServiceObserver { | 307 class NTPSnippetsServiceObserver { |
| 329 public: | 308 public: |
| 330 // Sent every time the service loads a new set of data. | 309 // Sent every time the service loads a new set of data. |
| 331 virtual void NTPSnippetsServiceLoaded() = 0; | 310 virtual void NTPSnippetsServiceLoaded() = 0; |
| 311 |
| 332 // Sent when the service is shutting down. | 312 // Sent when the service is shutting down. |
| 333 virtual void NTPSnippetsServiceShutdown() = 0; | 313 virtual void NTPSnippetsServiceShutdown() = 0; |
| 334 // Sent when the service has been disabled. Can be from explicit user action | 314 |
| 335 // or because a requirement (e.g. History Sync) is not fulfilled anymore. | 315 // Sent when the state of the service is changing. Something changed in its |
| 336 virtual void NTPSnippetsServiceDisabled() = 0; | 316 // dependencies so it's notifying observers about incoming data changes. |
| 317 // If the service might be enabled, DisabledReason::NONE will be provided. |
| 318 virtual void NTPSnippetsServiceDisabledReasonChanged(DisabledReason) = 0; |
| 337 | 319 |
| 338 protected: | 320 protected: |
| 339 virtual ~NTPSnippetsServiceObserver() {} | 321 virtual ~NTPSnippetsServiceObserver() {} |
| 340 }; | 322 }; |
| 341 | 323 |
| 342 } // namespace ntp_snippets | 324 } // namespace ntp_snippets |
| 343 | 325 |
| 344 #endif // COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_SERVICE_H_ | 326 #endif // COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_SERVICE_H_ |
| OLD | NEW |