| 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" | |
| 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" | |
| 22 #include "components/ntp_snippets/category.h" | 20 #include "components/ntp_snippets/category.h" |
| 23 #include "components/ntp_snippets/category_factory.h" | 21 #include "components/ntp_snippets/category_factory.h" |
| 24 #include "components/ntp_snippets/category_status.h" | 22 #include "components/ntp_snippets/category_status.h" |
| 25 #include "components/ntp_snippets/content_suggestion.h" | 23 #include "components/ntp_snippets/content_suggestion.h" |
| 26 #include "components/ntp_snippets/content_suggestions_provider.h" | 24 #include "components/ntp_snippets/content_suggestions_provider.h" |
| 27 #include "components/ntp_snippets/ntp_snippet.h" | 25 #include "components/ntp_snippets/ntp_snippet.h" |
| 28 #include "components/ntp_snippets/ntp_snippets_fetcher.h" | 26 #include "components/ntp_snippets/ntp_snippets_fetcher.h" |
| 29 #include "components/ntp_snippets/ntp_snippets_scheduler.h" | 27 #include "components/ntp_snippets/ntp_snippets_scheduler.h" |
| 30 #include "components/ntp_snippets/ntp_snippets_status_service.h" | 28 #include "components/ntp_snippets/ntp_snippets_status_service.h" |
| 31 #include "components/suggestions/suggestions_service.h" | 29 #include "components/suggestions/suggestions_service.h" |
| (...skipping 27 matching lines...) Expand all Loading... |
| 59 | 57 |
| 60 namespace ntp_snippets { | 58 namespace ntp_snippets { |
| 61 | 59 |
| 62 class NTPSnippetsDatabase; | 60 class NTPSnippetsDatabase; |
| 63 class NTPSnippetsServiceObserver; | 61 class NTPSnippetsServiceObserver; |
| 64 | 62 |
| 65 // Retrieves fresh content data (articles) from the server, stores them and | 63 // Retrieves fresh content data (articles) from the server, stores them and |
| 66 // provides them as content suggestions. | 64 // provides them as content suggestions. |
| 67 // TODO(pke): Rename this service to ArticleSuggestionsProvider and move to | 65 // TODO(pke): Rename this service to ArticleSuggestionsProvider and move to |
| 68 // a subdirectory. | 66 // a subdirectory. |
| 69 class NTPSnippetsService : public KeyedService, | 67 class NTPSnippetsService : public image_fetcher::ImageFetcherDelegate, |
| 70 public image_fetcher::ImageFetcherDelegate, | |
| 71 public ContentSuggestionsProvider { | 68 public ContentSuggestionsProvider { |
| 72 public: | 69 public: |
| 73 // |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. |
| 74 // '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 |
| 75 // 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' |
| 76 // (British English person in the US) are not language codes. | 73 // (British English person in the US) are not language codes. |
| 77 NTPSnippetsService(bool enabled, | 74 NTPSnippetsService(Observer* observer, |
| 75 CategoryFactory* category_factory, |
| 78 PrefService* pref_service, | 76 PrefService* pref_service, |
| 79 suggestions::SuggestionsService* suggestions_service, | 77 suggestions::SuggestionsService* suggestions_service, |
| 80 CategoryFactory* category_factory, | |
| 81 const std::string& application_language_code, | 78 const std::string& application_language_code, |
| 82 NTPSnippetsScheduler* scheduler, | 79 NTPSnippetsScheduler* scheduler, |
| 83 std::unique_ptr<NTPSnippetsFetcher> snippets_fetcher, | 80 std::unique_ptr<NTPSnippetsFetcher> snippets_fetcher, |
| 84 std::unique_ptr<image_fetcher::ImageFetcher> image_fetcher, | 81 std::unique_ptr<image_fetcher::ImageFetcher> image_fetcher, |
| 85 std::unique_ptr<image_fetcher::ImageDecoder> image_decoder, | 82 std::unique_ptr<image_fetcher::ImageDecoder> image_decoder, |
| 86 std::unique_ptr<NTPSnippetsDatabase> database, | 83 std::unique_ptr<NTPSnippetsDatabase> database, |
| 87 std::unique_ptr<NTPSnippetsStatusService> status_service); | 84 std::unique_ptr<NTPSnippetsStatusService> status_service); |
| 88 | 85 |
| 89 ~NTPSnippetsService() override; | 86 ~NTPSnippetsService() override; |
| 90 | 87 |
| 91 static void RegisterProfilePrefs(PrefRegistrySimple* registry); | 88 static void RegisterProfilePrefs(PrefRegistrySimple* registry); |
| 92 | 89 |
| 93 // Inherited from KeyedService. | |
| 94 void Shutdown() override; | |
| 95 | |
| 96 // Returns whether the service is ready. While this is false, the list of | 90 // Returns whether the service is ready. While this is false, the list of |
| 97 // snippets will be empty, and all modifications to it (fetch, dismiss, etc) | 91 // snippets will be empty, and all modifications to it (fetch, dismiss, etc) |
| 98 // will be ignored. | 92 // will be ignored. |
| 99 bool ready() const { return state_ == State::READY; } | 93 bool ready() const { return state_ == State::READY; } |
| 100 | 94 |
| 101 // Returns whether the service is initialized. While this is false, some | 95 // Returns whether the service is initialized. While this is false, some |
| 102 // calls may trigger DCHECKs. | 96 // calls may trigger DCHECKs. |
| 103 bool initialized() const { return ready() || state_ == State::DISABLED; } | 97 bool initialized() const { return ready() || state_ == State::DISABLED; } |
| 104 | 98 |
| 105 // Fetches snippets from the server and adds them to the current ones. | 99 // Fetches snippets from the server and adds them to the current ones. |
| (...skipping 29 matching lines...) Expand all Loading... |
| 135 } | 129 } |
| 136 | 130 |
| 137 // (Re)schedules the periodic fetching of snippets. This is necessary because | 131 // (Re)schedules the periodic fetching of snippets. This is necessary because |
| 138 // the schedule depends on the time of day. | 132 // the schedule depends on the time of day. |
| 139 void RescheduleFetching(); | 133 void RescheduleFetching(); |
| 140 | 134 |
| 141 // ContentSuggestionsProvider implementation | 135 // ContentSuggestionsProvider implementation |
| 142 // TODO(pke): At some point reorder the implementations in the .cc file | 136 // TODO(pke): At some point reorder the implementations in the .cc file |
| 143 // accordingly. | 137 // accordingly. |
| 144 std::vector<Category> GetProvidedCategories() override; | 138 std::vector<Category> GetProvidedCategories() override; |
| 145 void SetObserver(Observer* observer) override; | |
| 146 CategoryStatus GetCategoryStatus(Category category) override; | 139 CategoryStatus GetCategoryStatus(Category category) override; |
| 147 void DismissSuggestion(const std::string& suggestion_id) override; | 140 void DismissSuggestion(const std::string& suggestion_id) override; |
| 148 void FetchSuggestionImage(const std::string& suggestion_id, | 141 void FetchSuggestionImage(const std::string& suggestion_id, |
| 149 const ImageFetchedCallback& callback) override; | 142 const ImageFetchedCallback& callback) override; |
| 150 void ClearCachedSuggestionsForDebugging() override; | 143 void ClearCachedSuggestionsForDebugging() override; |
| 151 void ClearDismissedSuggestionsForDebugging() override; | 144 void ClearDismissedSuggestionsForDebugging() override; |
| 152 | 145 |
| 153 // Returns the lists of suggestion hosts the snippets are restricted to. | 146 // Returns the lists of suggestion hosts the snippets are restricted to. |
| 154 std::set<std::string> GetSuggestionsHosts() const; | 147 std::set<std::string> GetSuggestionsHosts() const; |
| 155 | 148 |
| 156 // Observer accessors. | |
| 157 void AddObserver(NTPSnippetsServiceObserver* observer); | |
| 158 void RemoveObserver(NTPSnippetsServiceObserver* observer); | |
| 159 | |
| 160 // Returns the maximum number of snippets that will be shown at once. | 149 // Returns the maximum number of snippets that will be shown at once. |
| 161 static int GetMaxSnippetCountForTesting(); | 150 static int GetMaxSnippetCountForTesting(); |
| 162 | 151 |
| 163 private: | 152 private: |
| 164 friend class NTPSnippetsServiceTest; | 153 friend class NTPSnippetsServiceTest; |
| 165 FRIEND_TEST_ALL_PREFIXES(NTPSnippetsServiceTest, StatusChanges); | 154 FRIEND_TEST_ALL_PREFIXES(NTPSnippetsServiceTest, StatusChanges); |
| 166 | 155 |
| 167 // TODO(pke): As soon as the DisabledReason is replaced with the new status, | |
| 168 // also remove the old State enum and replace it with CategoryStatus and a | |
| 169 // similar status diagram. | |
| 170 // Possible state transitions: | 156 // Possible state transitions: |
| 171 // +------- NOT_INITED ------+ | 157 // NOT_INITED --------+ |
| 172 // | / \ | | 158 // / \ | |
| 173 // | READY <--> DISABLED <-+ | 159 // v v | |
| 174 // | \ / | 160 // READY <--> DISABLED | |
| 175 // +-----> SHUT_DOWN | 161 // \ / | |
| 162 // v v | |
| 163 // ERROR_OCCURRED <-----+ |
| 176 enum class State { | 164 enum class State { |
| 177 // The service has just been created. Can change to states: | 165 // The service has just been created. Can change to states: |
| 178 // - DISABLED: if the constructor was called with |enabled == false| . In | 166 // - DISABLED: After the database is done loading, |
| 179 // that case the service will stay disabled until it is shut | 167 // GetStateForDependenciesStatus can identify the next state to |
| 180 // down. It can also enter this state after the database is | 168 // be DISABLED. |
| 181 // done loading and GetStateForDependenciesStatus identifies | |
| 182 // the next state to be DISABLED. | |
| 183 // - READY: if GetStateForDependenciesStatus returns it, after the database | 169 // - READY: if GetStateForDependenciesStatus returns it, after the database |
| 184 // is done loading. | 170 // is done loading. |
| 171 // - ERROR_OCCURRED: when an unrecoverable error occurred. |
| 185 NOT_INITED, | 172 NOT_INITED, |
| 186 | 173 |
| 187 // The service registered observers, timers, etc. and is ready to answer to | 174 // The service registered observers, timers, etc. and is ready to answer to |
| 188 // queries, fetch snippets... Can change to states: | 175 // queries, fetch snippets... Can change to states: |
| 189 // - DISABLED: when the global Chrome state changes, for example after | 176 // - DISABLED: when the global Chrome state changes, for example after |
| 190 // |OnStateChanged| is called and sync is disabled. | 177 // |OnStateChanged| is called and sync is disabled. |
| 191 // - SHUT_DOWN: when |Shutdown| is called, during the browser shutdown. | 178 // - ERROR_OCCURRED: when an unrecoverable error occurred. |
| 192 READY, | 179 READY, |
| 193 | 180 |
| 194 // The service is disabled and unregistered the related resources. | 181 // The service is disabled and unregistered the related resources. |
| 195 // Can change to states: | 182 // Can change to states: |
| 196 // - READY: when the global Chrome state changes, for example after | 183 // - READY: when the global Chrome state changes, for example after |
| 197 // |OnStateChanged| is called and sync is enabled. | 184 // |OnStateChanged| is called and sync is enabled. |
| 198 // - SHUT_DOWN: when |Shutdown| is called, during the browser shutdown. | 185 // - ERROR_OCCURRED: when an unrecoverable error occurred. |
| 199 DISABLED, | 186 DISABLED, |
| 200 | 187 |
| 201 // The service shutdown and can't be used anymore. This state is checked | 188 // The service or one of its dependencies encountered an unrecoverable error |
| 202 // for early exit in callbacks from observers. | 189 // and the service can't be used anymore. |
| 203 SHUT_DOWN | 190 ERROR_OCCURRED |
| 204 }; | 191 }; |
| 205 | 192 |
| 206 // image_fetcher::ImageFetcherDelegate implementation. | 193 // image_fetcher::ImageFetcherDelegate implementation. |
| 207 void OnImageDataFetched(const std::string& snippet_id, | 194 void OnImageDataFetched(const std::string& snippet_id, |
| 208 const std::string& image_data) override; | 195 const std::string& image_data) override; |
| 209 | 196 |
| 210 // Callbacks for the NTPSnippetsDatabase. | 197 // Callbacks for the NTPSnippetsDatabase. |
| 211 void OnDatabaseLoaded(NTPSnippet::PtrVector snippets); | 198 void OnDatabaseLoaded(NTPSnippet::PtrVector snippets); |
| 212 void OnDatabaseError(); | 199 void OnDatabaseError(); |
| 213 | 200 |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 252 // status if called with the current state. | 239 // status if called with the current state. |
| 253 void EnterState(State state, CategoryStatus status); | 240 void EnterState(State state, CategoryStatus status); |
| 254 | 241 |
| 255 // Enables the service and triggers a fetch if required. Do not call directly, | 242 // Enables the service and triggers a fetch if required. Do not call directly, |
| 256 // use |EnterState| instead. | 243 // use |EnterState| instead. |
| 257 void EnterStateEnabled(bool fetch_snippets); | 244 void EnterStateEnabled(bool fetch_snippets); |
| 258 | 245 |
| 259 // Disables the service. Do not call directly, use |EnterState| instead. | 246 // Disables the service. Do not call directly, use |EnterState| instead. |
| 260 void EnterStateDisabled(); | 247 void EnterStateDisabled(); |
| 261 | 248 |
| 262 // Applies the effects of the transition to the SHUT_DOWN state. Do not call | 249 // Disables the service permanently because an unrecoverable error occurred. |
| 263 // directly, use |EnterState| instead. | 250 // Do not call directly, use |EnterState| instead. |
| 264 void EnterStateShutdown(); | 251 void EnterStateError(); |
| 265 | 252 |
| 266 // Converts the cached snippets to article content suggestions and notifies | 253 // Converts the cached snippets to article content suggestions and notifies |
| 267 // the observers. | 254 // the observers. |
| 268 void NotifyNewSuggestions(); | 255 void NotifyNewSuggestions(); |
| 269 | 256 |
| 270 // Updates the internal status |category_status_| and notifies the content | 257 // Updates the internal status |category_status_| and notifies the content |
| 271 // suggestions observer if it changed. | 258 // suggestions observer if it changed. |
| 272 void UpdateCategoryStatus(CategoryStatus status); | 259 void UpdateCategoryStatus(CategoryStatus status); |
| 273 | 260 |
| 274 State state_; | 261 State state_; |
| 275 | 262 |
| 276 CategoryStatus category_status_; | 263 CategoryStatus category_status_; |
| 277 | 264 |
| 278 PrefService* pref_service_; | 265 PrefService* pref_service_; |
| 279 | 266 |
| 280 suggestions::SuggestionsService* suggestions_service_; | 267 suggestions::SuggestionsService* suggestions_service_; |
| 281 | 268 |
| 282 // All current suggestions (i.e. not dismissed ones). | 269 // All current suggestions (i.e. not dismissed ones). |
| 283 NTPSnippet::PtrVector snippets_; | 270 NTPSnippet::PtrVector snippets_; |
| 284 | 271 |
| 285 // Suggestions that the user dismissed. We keep these around until they expire | 272 // Suggestions that the user dismissed. We keep these around until they expire |
| 286 // so we won't re-add them on the next fetch. | 273 // so we won't re-add them on the next fetch. |
| 287 NTPSnippet::PtrVector dismissed_snippets_; | 274 NTPSnippet::PtrVector dismissed_snippets_; |
| 288 | 275 |
| 289 // The ISO 639-1 code of the language used by the application. | 276 // The ISO 639-1 code of the language used by the application. |
| 290 const std::string application_language_code_; | 277 const std::string application_language_code_; |
| 291 | 278 |
| 292 // The observers. | |
| 293 // TODO(pke): This is an old kind of observers that will be removed. | |
| 294 base::ObserverList<NTPSnippetsServiceObserver> observers_; | |
| 295 | |
| 296 // The observer to deliver data to. | |
| 297 Observer* observer_; | |
| 298 | |
| 299 // Scheduler for fetching snippets. Not owned. | 279 // Scheduler for fetching snippets. Not owned. |
| 300 NTPSnippetsScheduler* scheduler_; | 280 NTPSnippetsScheduler* scheduler_; |
| 301 | 281 |
| 302 // The subscription to the SuggestionsService. When the suggestions change, | 282 // The subscription to the SuggestionsService. When the suggestions change, |
| 303 // SuggestionsService will call |OnSuggestionsChanged|, which triggers an | 283 // SuggestionsService will call |OnSuggestionsChanged|, which triggers an |
| 304 // update to the set of snippets. | 284 // update to the set of snippets. |
| 305 using SuggestionsSubscription = | 285 using SuggestionsSubscription = |
| 306 suggestions::SuggestionsService::ResponseCallbackList::Subscription; | 286 suggestions::SuggestionsService::ResponseCallbackList::Subscription; |
| 307 std::unique_ptr<SuggestionsSubscription> suggestions_service_subscription_; | 287 std::unique_ptr<SuggestionsSubscription> suggestions_service_subscription_; |
| 308 | 288 |
| (...skipping 14 matching lines...) Expand all Loading... |
| 323 | 303 |
| 324 // Set to true if FetchSnippets is called before the database has been loaded. | 304 // Set to true if FetchSnippets is called before the database has been loaded. |
| 325 // The fetch will be executed after the database load finishes. | 305 // The fetch will be executed after the database load finishes. |
| 326 bool fetch_after_load_; | 306 bool fetch_after_load_; |
| 327 | 307 |
| 328 const Category provided_category_; | 308 const Category provided_category_; |
| 329 | 309 |
| 330 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsService); | 310 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsService); |
| 331 }; | 311 }; |
| 332 | 312 |
| 333 // TODO(pke): Remove this when snippets internals don't access this service | |
| 334 // directly anymore. | |
| 335 class NTPSnippetsServiceObserver { | |
| 336 public: | |
| 337 // Sent every time the service loads a new set of data. | |
| 338 virtual void NTPSnippetsServiceLoaded() = 0; | |
| 339 | |
| 340 // Sent when the service is shutting down. | |
| 341 virtual void NTPSnippetsServiceShutdown() = 0; | |
| 342 | |
| 343 // Sent when the state of the service is changing. Something changed in its | |
| 344 // dependencies so it's notifying observers about incoming data changes. | |
| 345 // If the service might be enabled, DisabledReason::NONE will be provided. | |
| 346 virtual void NTPSnippetsServiceDisabledReasonChanged(DisabledReason) = 0; | |
| 347 | |
| 348 protected: | |
| 349 virtual ~NTPSnippetsServiceObserver() {} | |
| 350 }; | |
| 351 | |
| 352 } // namespace ntp_snippets | 313 } // namespace ntp_snippets |
| 353 | 314 |
| 354 #endif // COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_SERVICE_H_ | 315 #endif // COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_SERVICE_H_ |
| OLD | NEW |