| 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/scoped_observer.h" | 18 #include "base/scoped_observer.h" |
| 19 #include "base/timer/timer.h" | 19 #include "base/timer/timer.h" |
| 20 #include "components/history/core/browser/history_service_observer.h" | |
| 21 #include "components/image_fetcher/image_fetcher_delegate.h" | 20 #include "components/image_fetcher/image_fetcher_delegate.h" |
| 22 #include "components/ntp_snippets/category.h" | 21 #include "components/ntp_snippets/category.h" |
| 23 #include "components/ntp_snippets/category_factory.h" | 22 #include "components/ntp_snippets/category_factory.h" |
| 24 #include "components/ntp_snippets/category_status.h" | 23 #include "components/ntp_snippets/category_status.h" |
| 25 #include "components/ntp_snippets/content_suggestion.h" | 24 #include "components/ntp_snippets/content_suggestion.h" |
| 26 #include "components/ntp_snippets/content_suggestions_provider.h" | 25 #include "components/ntp_snippets/content_suggestions_provider.h" |
| 27 #include "components/ntp_snippets/ntp_snippet.h" | 26 #include "components/ntp_snippets/ntp_snippet.h" |
| 28 #include "components/ntp_snippets/ntp_snippets_fetcher.h" | 27 #include "components/ntp_snippets/ntp_snippets_fetcher.h" |
| 29 #include "components/ntp_snippets/ntp_snippets_scheduler.h" | 28 #include "components/ntp_snippets/ntp_snippets_scheduler.h" |
| 30 #include "components/ntp_snippets/ntp_snippets_status_service.h" | 29 #include "components/ntp_snippets/ntp_snippets_status_service.h" |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 67 // provides them as content suggestions. | 66 // provides them as content suggestions. |
| 68 // TODO(pke): Rename this service to ArticleSuggestionsProvider and move to | 67 // TODO(pke): Rename this service to ArticleSuggestionsProvider and move to |
| 69 // a subdirectory. | 68 // a subdirectory. |
| 70 // TODO(jkrcal): this class grows really, really large. The fact that | 69 // TODO(jkrcal): this class grows really, really large. The fact that |
| 71 // NTPSnippetService also implements ImageFetcherDelegate adds unnecessary | 70 // NTPSnippetService also implements ImageFetcherDelegate adds unnecessary |
| 72 // complexity (and after all the Service is conceptually not an | 71 // complexity (and after all the Service is conceptually not an |
| 73 // ImagerFetcherDeletage ;-)). Instead, the cleaner solution would be to define | 72 // ImagerFetcherDeletage ;-)). Instead, the cleaner solution would be to define |
| 74 // a CachedImageFetcher class that handles the caching aspects and looks like an | 73 // a CachedImageFetcher class that handles the caching aspects and looks like an |
| 75 // image fetcher to the NTPSnippetService. | 74 // image fetcher to the NTPSnippetService. |
| 76 class NTPSnippetsService : public ContentSuggestionsProvider, | 75 class NTPSnippetsService : public ContentSuggestionsProvider, |
| 77 public image_fetcher::ImageFetcherDelegate, | 76 public image_fetcher::ImageFetcherDelegate { |
| 78 public history::HistoryServiceObserver { | |
| 79 public: | 77 public: |
| 80 // |application_language_code| should be a ISO 639-1 compliant string, e.g. | 78 // |application_language_code| should be a ISO 639-1 compliant string, e.g. |
| 81 // 'en' or 'en-US'. Note that this code should only specify the language, not | 79 // 'en' or 'en-US'. Note that this code should only specify the language, not |
| 82 // the locale, so 'en_US' (English language with US locale) and 'en-GB_US' | 80 // the locale, so 'en_US' (English language with US locale) and 'en-GB_US' |
| 83 // (British English person in the US) are not language codes. | 81 // (British English person in the US) are not language codes. |
| 84 NTPSnippetsService(Observer* observer, | 82 NTPSnippetsService(Observer* observer, |
| 85 CategoryFactory* category_factory, | 83 CategoryFactory* category_factory, |
| 86 PrefService* pref_service, | 84 PrefService* pref_service, |
| 87 history::HistoryService* history_service, | |
| 88 suggestions::SuggestionsService* suggestions_service, | 85 suggestions::SuggestionsService* suggestions_service, |
| 89 const std::string& application_language_code, | 86 const std::string& application_language_code, |
| 90 NTPSnippetsScheduler* scheduler, | 87 NTPSnippetsScheduler* scheduler, |
| 91 std::unique_ptr<NTPSnippetsFetcher> snippets_fetcher, | 88 std::unique_ptr<NTPSnippetsFetcher> snippets_fetcher, |
| 92 std::unique_ptr<image_fetcher::ImageFetcher> image_fetcher, | 89 std::unique_ptr<image_fetcher::ImageFetcher> image_fetcher, |
| 93 std::unique_ptr<image_fetcher::ImageDecoder> image_decoder, | 90 std::unique_ptr<image_fetcher::ImageDecoder> image_decoder, |
| 94 std::unique_ptr<NTPSnippetsDatabase> database, | 91 std::unique_ptr<NTPSnippetsDatabase> database, |
| 95 std::unique_ptr<NTPSnippetsStatusService> status_service); | 92 std::unique_ptr<NTPSnippetsStatusService> status_service); |
| 96 | 93 |
| 97 ~NTPSnippetsService() override; | 94 ~NTPSnippetsService() override; |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 133 // (Re)schedules the periodic fetching of snippets. This is necessary because | 130 // (Re)schedules the periodic fetching of snippets. This is necessary because |
| 134 // the schedule depends on the time of day. | 131 // the schedule depends on the time of day. |
| 135 void RescheduleFetching(); | 132 void RescheduleFetching(); |
| 136 | 133 |
| 137 // ContentSuggestionsProvider implementation | 134 // ContentSuggestionsProvider implementation |
| 138 CategoryStatus GetCategoryStatus(Category category) override; | 135 CategoryStatus GetCategoryStatus(Category category) override; |
| 139 CategoryInfo GetCategoryInfo(Category category) override; | 136 CategoryInfo GetCategoryInfo(Category category) override; |
| 140 void DismissSuggestion(const std::string& suggestion_id) override; | 137 void DismissSuggestion(const std::string& suggestion_id) override; |
| 141 void FetchSuggestionImage(const std::string& suggestion_id, | 138 void FetchSuggestionImage(const std::string& suggestion_id, |
| 142 const ImageFetchedCallback& callback) override; | 139 const ImageFetchedCallback& callback) override; |
| 140 void RemoveURLsFromHistory(bool all_history, |
| 141 const std::vector<GURL>& deleted_urls) override; |
| 143 void ClearHistory( | 142 void ClearHistory( |
| 144 base::Time begin, | 143 base::Time begin, |
| 145 base::Time end, | 144 base::Time end, |
| 146 const base::Callback<bool(const GURL& url)>& filter) override; | 145 const base::Callback<bool(const GURL& url)>& filter) override; |
| 147 void ClearCachedSuggestions(Category category) override; | 146 void ClearCachedSuggestions(Category category) override; |
| 148 void GetDismissedSuggestionsForDebugging( | 147 void GetDismissedSuggestionsForDebugging( |
| 149 Category category, | 148 Category category, |
| 150 const DismissedSuggestionsCallback& callback) override; | 149 const DismissedSuggestionsCallback& callback) override; |
| 151 void ClearDismissedSuggestionsForDebugging(Category category) override; | 150 void ClearDismissedSuggestionsForDebugging(Category category) override; |
| 152 | 151 |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 200 // - READY: when the global Chrome state changes, for example after | 199 // - READY: when the global Chrome state changes, for example after |
| 201 // |OnStateChanged| is called and sync is enabled. | 200 // |OnStateChanged| is called and sync is enabled. |
| 202 // - ERROR_OCCURRED: when an unrecoverable error occurred. | 201 // - ERROR_OCCURRED: when an unrecoverable error occurred. |
| 203 DISABLED, | 202 DISABLED, |
| 204 | 203 |
| 205 // The service or one of its dependencies encountered an unrecoverable error | 204 // The service or one of its dependencies encountered an unrecoverable error |
| 206 // and the service can't be used anymore. | 205 // and the service can't be used anymore. |
| 207 ERROR_OCCURRED | 206 ERROR_OCCURRED |
| 208 }; | 207 }; |
| 209 | 208 |
| 210 // history::HistoryServiceObserver implementation. | |
| 211 void OnURLsDeleted(history::HistoryService* history_service, | |
| 212 bool all_history, | |
| 213 bool expired, | |
| 214 const history::URLRows& deleted_rows, | |
| 215 const std::set<GURL>& favicon_urls) override; | |
| 216 void HistoryServiceBeingDeleted( | |
| 217 history::HistoryService* history_service) override; | |
| 218 | |
| 219 // image_fetcher::ImageFetcherDelegate implementation. | 209 // image_fetcher::ImageFetcherDelegate implementation. |
| 220 void OnImageDataFetched(const std::string& snippet_id, | 210 void OnImageDataFetched(const std::string& snippet_id, |
| 221 const std::string& image_data) override; | 211 const std::string& image_data) override; |
| 222 | 212 |
| 223 // Callbacks for the NTPSnippetsDatabase. | 213 // Callbacks for the NTPSnippetsDatabase. |
| 224 void OnDatabaseLoaded(NTPSnippet::PtrVector snippets); | 214 void OnDatabaseLoaded(NTPSnippet::PtrVector snippets); |
| 225 void OnDatabaseError(); | 215 void OnDatabaseError(); |
| 226 | 216 |
| 227 // Callback for the SuggestionsService. | 217 // Callback for the SuggestionsService. |
| 228 void OnSuggestionsChanged(const suggestions::SuggestionsProfile& suggestions); | 218 void OnSuggestionsChanged(const suggestions::SuggestionsProfile& suggestions); |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 305 // Suggestions that the user dismissed. We keep these around until they expire | 295 // Suggestions that the user dismissed. We keep these around until they expire |
| 306 // so we won't re-add them on the next fetch. | 296 // so we won't re-add them on the next fetch. |
| 307 NTPSnippet::PtrVector dismissed_snippets_; | 297 NTPSnippet::PtrVector dismissed_snippets_; |
| 308 | 298 |
| 309 // The ISO 639-1 code of the language used by the application. | 299 // The ISO 639-1 code of the language used by the application. |
| 310 const std::string application_language_code_; | 300 const std::string application_language_code_; |
| 311 | 301 |
| 312 // Scheduler for fetching snippets. Not owned. | 302 // Scheduler for fetching snippets. Not owned. |
| 313 NTPSnippetsScheduler* scheduler_; | 303 NTPSnippetsScheduler* scheduler_; |
| 314 | 304 |
| 315 // Observer for the HistoryService. We clear all cached suggestions when | |
| 316 // history gets deleted. | |
| 317 ScopedObserver<history::HistoryService, history::HistoryServiceObserver> | |
| 318 history_service_observer_; | |
| 319 | |
| 320 // The subscription to the SuggestionsService. When the suggestions change, | 305 // The subscription to the SuggestionsService. When the suggestions change, |
| 321 // SuggestionsService will call |OnSuggestionsChanged|, which triggers an | 306 // SuggestionsService will call |OnSuggestionsChanged|, which triggers an |
| 322 // update to the set of snippets. | 307 // update to the set of snippets. |
| 323 using SuggestionsSubscription = | 308 using SuggestionsSubscription = |
| 324 suggestions::SuggestionsService::ResponseCallbackList::Subscription; | 309 suggestions::SuggestionsService::ResponseCallbackList::Subscription; |
| 325 std::unique_ptr<SuggestionsSubscription> suggestions_service_subscription_; | 310 std::unique_ptr<SuggestionsSubscription> suggestions_service_subscription_; |
| 326 | 311 |
| 327 // The snippets fetcher. | 312 // The snippets fetcher. |
| 328 std::unique_ptr<NTPSnippetsFetcher> snippets_fetcher_; | 313 std::unique_ptr<NTPSnippetsFetcher> snippets_fetcher_; |
| 329 | 314 |
| (...skipping 21 matching lines...) Expand all Loading... |
| 351 | 336 |
| 352 // Request throttler for limiting requests to thumbnail images. | 337 // Request throttler for limiting requests to thumbnail images. |
| 353 RequestThrottler thumbnail_requests_throttler_; | 338 RequestThrottler thumbnail_requests_throttler_; |
| 354 | 339 |
| 355 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsService); | 340 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsService); |
| 356 }; | 341 }; |
| 357 | 342 |
| 358 } // namespace ntp_snippets | 343 } // namespace ntp_snippets |
| 359 | 344 |
| 360 #endif // COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_SERVICE_H_ | 345 #endif // COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_SERVICE_H_ |
| OLD | NEW |