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 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
200 // - READY: when the global Chrome state changes, for example after | 197 // - READY: when the global Chrome state changes, for example after |
201 // |OnStateChanged| is called and sync is enabled. | 198 // |OnStateChanged| is called and sync is enabled. |
202 // - ERROR_OCCURRED: when an unrecoverable error occurred. | 199 // - ERROR_OCCURRED: when an unrecoverable error occurred. |
203 DISABLED, | 200 DISABLED, |
204 | 201 |
205 // The service or one of its dependencies encountered an unrecoverable error | 202 // The service or one of its dependencies encountered an unrecoverable error |
206 // and the service can't be used anymore. | 203 // and the service can't be used anymore. |
207 ERROR_OCCURRED | 204 ERROR_OCCURRED |
208 }; | 205 }; |
209 | 206 |
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. | 207 // image_fetcher::ImageFetcherDelegate implementation. |
220 void OnImageDataFetched(const std::string& snippet_id, | 208 void OnImageDataFetched(const std::string& snippet_id, |
221 const std::string& image_data) override; | 209 const std::string& image_data) override; |
222 | 210 |
223 // Callbacks for the NTPSnippetsDatabase. | 211 // Callbacks for the NTPSnippetsDatabase. |
224 void OnDatabaseLoaded(NTPSnippet::PtrVector snippets); | 212 void OnDatabaseLoaded(NTPSnippet::PtrVector snippets); |
225 void OnDatabaseError(); | 213 void OnDatabaseError(); |
226 | 214 |
227 // Callback for the SuggestionsService. | 215 // Callback for the SuggestionsService. |
228 void OnSuggestionsChanged(const suggestions::SuggestionsProfile& suggestions); | 216 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 | 293 // Suggestions that the user dismissed. We keep these around until they expire |
306 // so we won't re-add them on the next fetch. | 294 // so we won't re-add them on the next fetch. |
307 NTPSnippet::PtrVector dismissed_snippets_; | 295 NTPSnippet::PtrVector dismissed_snippets_; |
308 | 296 |
309 // The ISO 639-1 code of the language used by the application. | 297 // The ISO 639-1 code of the language used by the application. |
310 const std::string application_language_code_; | 298 const std::string application_language_code_; |
311 | 299 |
312 // Scheduler for fetching snippets. Not owned. | 300 // Scheduler for fetching snippets. Not owned. |
313 NTPSnippetsScheduler* scheduler_; | 301 NTPSnippetsScheduler* scheduler_; |
314 | 302 |
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, | 303 // The subscription to the SuggestionsService. When the suggestions change, |
321 // SuggestionsService will call |OnSuggestionsChanged|, which triggers an | 304 // SuggestionsService will call |OnSuggestionsChanged|, which triggers an |
322 // update to the set of snippets. | 305 // update to the set of snippets. |
323 using SuggestionsSubscription = | 306 using SuggestionsSubscription = |
324 suggestions::SuggestionsService::ResponseCallbackList::Subscription; | 307 suggestions::SuggestionsService::ResponseCallbackList::Subscription; |
325 std::unique_ptr<SuggestionsSubscription> suggestions_service_subscription_; | 308 std::unique_ptr<SuggestionsSubscription> suggestions_service_subscription_; |
326 | 309 |
327 // The snippets fetcher. | 310 // The snippets fetcher. |
328 std::unique_ptr<NTPSnippetsFetcher> snippets_fetcher_; | 311 std::unique_ptr<NTPSnippetsFetcher> snippets_fetcher_; |
329 | 312 |
(...skipping 21 matching lines...) Expand all Loading... |
351 | 334 |
352 // Request throttler for limiting requests to thumbnail images. | 335 // Request throttler for limiting requests to thumbnail images. |
353 RequestThrottler thumbnail_requests_throttler_; | 336 RequestThrottler thumbnail_requests_throttler_; |
354 | 337 |
355 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsService); | 338 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsService); |
356 }; | 339 }; |
357 | 340 |
358 } // namespace ntp_snippets | 341 } // namespace ntp_snippets |
359 | 342 |
360 #endif // COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_SERVICE_H_ | 343 #endif // COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_SERVICE_H_ |
OLD | NEW |