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_REMOTE_REMOTE_SUGGESTIONS_PROVIDER_H_ | 5 #ifndef COMPONENTS_NTP_SNIPPETS_REMOTE_REMOTE_SUGGESTIONS_PROVIDER_H_ |
6 #define COMPONENTS_NTP_SNIPPETS_REMOTE_REMOTE_SUGGESTIONS_PROVIDER_H_ | 6 #define COMPONENTS_NTP_SNIPPETS_REMOTE_REMOTE_SUGGESTIONS_PROVIDER_H_ |
7 | 7 |
8 #include <cstddef> | 8 #include <cstddef> |
9 #include <deque> | 9 #include <deque> |
10 #include <map> | 10 #include <map> |
11 #include <memory> | 11 #include <memory> |
12 #include <set> | 12 #include <set> |
13 #include <string> | 13 #include <string> |
14 #include <vector> | 14 #include <vector> |
15 | 15 |
16 #include "base/callback_forward.h" | 16 #include "base/callback_forward.h" |
17 #include "base/gtest_prod_util.h" | 17 #include "base/gtest_prod_util.h" |
18 #include "base/macros.h" | 18 #include "base/macros.h" |
19 #include "base/time/time.h" | 19 #include "base/time/time.h" |
20 #include "components/image_fetcher/image_fetcher_delegate.h" | 20 #include "components/image_fetcher/image_fetcher_delegate.h" |
21 #include "components/ntp_snippets/category.h" | 21 #include "components/ntp_snippets/category.h" |
22 #include "components/ntp_snippets/category_factory.h" | 22 #include "components/ntp_snippets/category_factory.h" |
23 #include "components/ntp_snippets/category_status.h" | 23 #include "components/ntp_snippets/category_status.h" |
24 #include "components/ntp_snippets/content_suggestion.h" | 24 #include "components/ntp_snippets/content_suggestion.h" |
25 #include "components/ntp_snippets/content_suggestions_provider.h" | 25 #include "components/ntp_snippets/content_suggestions_provider.h" |
26 #include "components/ntp_snippets/remote/ntp_snippet.h" | 26 #include "components/ntp_snippets/remote/ntp_snippet.h" |
27 #include "components/ntp_snippets/remote/ntp_snippets_fetcher.h" | 27 #include "components/ntp_snippets/remote/ntp_snippets_fetcher.h" |
28 #include "components/ntp_snippets/remote/ntp_snippets_scheduler.h" | 28 #include "components/ntp_snippets/remote/remote_suggestions_scheduler.h" |
29 #include "components/ntp_snippets/remote/remote_suggestions_status_service.h" | 29 #include "components/ntp_snippets/remote/remote_suggestions_status_service.h" |
30 #include "components/ntp_snippets/remote/request_throttler.h" | 30 #include "components/ntp_snippets/remote/request_throttler.h" |
31 | 31 |
32 class PrefRegistrySimple; | 32 class PrefRegistrySimple; |
33 class PrefService; | 33 class PrefService; |
34 | 34 |
35 namespace gfx { | 35 namespace gfx { |
36 class Image; | 36 class Image; |
37 } // namespace gfx | 37 } // namespace gfx |
38 | 38 |
39 namespace image_fetcher { | 39 namespace image_fetcher { |
40 class ImageDecoder; | 40 class ImageDecoder; |
41 class ImageFetcher; | 41 class ImageFetcher; |
42 } // namespace image_fetcher | 42 } // namespace image_fetcher |
43 | 43 |
44 namespace ntp_snippets { | 44 namespace ntp_snippets { |
45 | 45 |
46 class RemoteSuggestionsDatabase; | 46 class RemoteSuggestionsDatabase; |
47 class RemoteSuggestionsHardScheduler; | |
47 class UserClassifier; | 48 class UserClassifier; |
48 | 49 |
49 // Retrieves fresh content data (articles) from the server, stores them and | 50 // Retrieves fresh content data (articles) from the server, stores them and |
50 // provides them as content suggestions. | 51 // provides them as content suggestions. |
51 // This class is final because it does things in its constructor which make it | 52 // This class is final because it does things in its constructor which make it |
52 // unsafe to derive from it. | 53 // unsafe to derive from it. |
53 // TODO(treib): Introduce two-phase initialization and make the class not final? | 54 // TODO(treib): Introduce two-phase initialization and make the class not final? |
54 // TODO(jkrcal): this class grows really, really large. The fact that | 55 // TODO(jkrcal): this class grows really, really large. The fact that |
55 // NTPSnippetService also implements ImageFetcherDelegate adds unnecessary | 56 // NTPSnippetService also implements ImageFetcherDelegate adds unnecessary |
56 // complexity (and after all the Service is conceptually not an | 57 // complexity (and after all the Service is conceptually not an |
57 // ImagerFetcherDeletage ;-)). Instead, the cleaner solution would be to define | 58 // ImagerFetcherDeletage ;-)). Instead, the cleaner solution would be to define |
58 // a CachedImageFetcher class that handles the caching aspects and looks like an | 59 // a CachedImageFetcher class that handles the caching aspects and looks like an |
59 // image fetcher to the NTPSnippetService. | 60 // image fetcher to the NTPSnippetService. |
60 class RemoteSuggestionsProvider final | 61 class RemoteSuggestionsProvider final |
61 : public ContentSuggestionsProvider, | 62 : public ContentSuggestionsProvider, |
62 public image_fetcher::ImageFetcherDelegate { | 63 public image_fetcher::ImageFetcherDelegate, |
64 public RemoteSuggestionsScheduler::Updater { | |
63 public: | 65 public: |
64 // |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. |
65 // '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 |
66 // 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' |
67 // (British English person in the US) are not language codes. | 69 // (British English person in the US) are not language codes. |
68 RemoteSuggestionsProvider( | 70 RemoteSuggestionsProvider( |
69 Observer* observer, | 71 Observer* observer, |
70 CategoryFactory* category_factory, | 72 CategoryFactory* category_factory, |
71 PrefService* pref_service, | 73 PrefService* pref_service, |
72 const std::string& application_language_code, | 74 const std::string& application_language_code, |
73 const UserClassifier* user_classifier, | 75 const UserClassifier* user_classifier, |
74 NTPSnippetsScheduler* scheduler, | 76 RemoteSuggestionsHardScheduler* hard_scheduler, |
75 std::unique_ptr<NTPSnippetsFetcher> snippets_fetcher, | 77 std::unique_ptr<NTPSnippetsFetcher> snippets_fetcher, |
76 std::unique_ptr<image_fetcher::ImageFetcher> image_fetcher, | 78 std::unique_ptr<image_fetcher::ImageFetcher> image_fetcher, |
77 std::unique_ptr<image_fetcher::ImageDecoder> image_decoder, | 79 std::unique_ptr<image_fetcher::ImageDecoder> image_decoder, |
78 std::unique_ptr<RemoteSuggestionsDatabase> database, | 80 std::unique_ptr<RemoteSuggestionsDatabase> database, |
79 std::unique_ptr<RemoteSuggestionsStatusService> status_service); | 81 std::unique_ptr<RemoteSuggestionsStatusService> status_service); |
80 | 82 |
81 ~RemoteSuggestionsProvider() override; | 83 ~RemoteSuggestionsProvider() override; |
82 | 84 |
83 static void RegisterProfilePrefs(PrefRegistrySimple* registry); | 85 static void RegisterProfilePrefs(PrefRegistrySimple* registry); |
84 | 86 |
85 // Returns whether the service is ready. While this is false, the list of | 87 // Returns whether the service is ready. While this is false, the list of |
86 // snippets will be empty, and all modifications to it (fetch, dismiss, etc) | 88 // snippets will be empty, and all modifications to it (fetch, dismiss, etc) |
87 // will be ignored. | 89 // will be ignored. |
88 bool ready() const { return state_ == State::READY; } | 90 bool ready() const { return state_ == State::READY; } |
89 | 91 |
90 // Returns whether the service is initialized. While this is false, some | 92 // Returns whether the service is initialized. While this is false, some |
91 // calls may trigger DCHECKs. | 93 // calls may trigger DCHECKs. |
92 bool initialized() const { return ready() || state_ == State::DISABLED; } | 94 bool initialized() const { return ready() || state_ == State::DISABLED; } |
93 | 95 |
94 // Fetchs content suggestions from the Content Suggestions server for all | 96 // ContentSuggestionsScheduler::Updater implementation |
Marc Treib
2016/12/08 16:14:58
RemoteSuggestionsScheduler. Also, private?
jkrcal
2016/12/09 09:20:38
Done both.
(thanks, was not aware that implementat
| |
95 // remote categories in the background. | 97 void UpdateRemoteSuggestionsBySchedule() override; |
96 void FetchSnippetsInTheBackground(); | |
97 | 98 |
98 // Fetchs content suggestions from the Content Suggestions server for all | 99 // Fetches content suggestions from the Content Suggestions server for all |
99 // remote categories. The request to the server is performed as an interactive | 100 // remote categories. The request to the server is performed as an interactive |
100 // request. Interactive requests are used for actions triggered by the user | 101 // request. Interactive requests are used for actions triggered by the user |
101 // and request lower latency processing. | 102 // and request lower latency processing. |
102 void FetchSnippetsForAllCategories(); | 103 void FetchSnippetsForAllCategories(); |
103 | 104 |
104 // Fetches snippets from the server for specified hosts and adds them to the | 105 // Fetches snippets from the server for specified hosts and adds them to the |
105 // current ones. Only called from chrome://snippets-internals, DO NOT USE | 106 // current ones. Only called from chrome://snippets-internals, DO NOT USE |
106 // otherwise! Ignored while ready() is false. | 107 // otherwise! Ignored while ready() is false. |
107 void FetchSnippetsFromHosts(const std::set<std::string>& hosts, | 108 void FetchSnippetsFromHosts(const std::set<std::string>& hosts, |
108 bool interactive_request); | 109 bool interactive_request); |
109 | 110 |
110 const NTPSnippetsFetcher* snippets_fetcher() const { | 111 const NTPSnippetsFetcher* snippets_fetcher() const { |
111 return snippets_fetcher_.get(); | 112 return snippets_fetcher_.get(); |
112 } | 113 } |
113 | 114 |
114 // (Re)schedules the periodic fetching of snippets. If |force| is true, the | 115 RemoteSuggestionsScheduler* scheduler() { |
115 // tasks will be re-scheduled even if they already exist and have the correct | 116 return &scheduler_; |
116 // periods. | 117 } |
117 void RescheduleFetching(bool force); | |
118 | 118 |
119 // ContentSuggestionsProvider implementation | 119 // ContentSuggestionsProvider implementation |
120 CategoryStatus GetCategoryStatus(Category category) override; | 120 CategoryStatus GetCategoryStatus(Category category) override; |
121 CategoryInfo GetCategoryInfo(Category category) override; | 121 CategoryInfo GetCategoryInfo(Category category) override; |
122 void DismissSuggestion(const ContentSuggestion::ID& suggestion_id) override; | 122 void DismissSuggestion(const ContentSuggestion::ID& suggestion_id) override; |
123 void FetchSuggestionImage(const ContentSuggestion::ID& suggestion_id, | 123 void FetchSuggestionImage(const ContentSuggestion::ID& suggestion_id, |
124 const ImageFetchedCallback& callback) override; | 124 const ImageFetchedCallback& callback) override; |
125 void Fetch(const Category& category, | 125 void Fetch(const Category& category, |
126 const std::set<std::string>& known_suggestion_ids, | 126 const std::set<std::string>& known_suggestion_ids, |
127 const FetchDoneCallback& callback) override; | 127 const FetchDoneCallback& callback) override; |
(...skipping 244 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
372 const Category articles_category_; | 372 const Category articles_category_; |
373 | 373 |
374 std::map<Category, CategoryContent, Category::CompareByID> category_contents_; | 374 std::map<Category, CategoryContent, Category::CompareByID> category_contents_; |
375 | 375 |
376 // The ISO 639-1 code of the language used by the application. | 376 // The ISO 639-1 code of the language used by the application. |
377 const std::string application_language_code_; | 377 const std::string application_language_code_; |
378 | 378 |
379 // Classifier that tells us how active the user is. Not owned. | 379 // Classifier that tells us how active the user is. Not owned. |
380 const UserClassifier* user_classifier_; | 380 const UserClassifier* user_classifier_; |
381 | 381 |
382 // Scheduler for fetching snippets. Not owned. | 382 // Scheduler for fetching snippets. |
383 NTPSnippetsScheduler* scheduler_; | 383 RemoteSuggestionsScheduler scheduler_; |
384 | 384 |
385 // The snippets fetcher. | 385 // The snippets fetcher. |
386 std::unique_ptr<NTPSnippetsFetcher> snippets_fetcher_; | 386 std::unique_ptr<NTPSnippetsFetcher> snippets_fetcher_; |
387 | 387 |
388 std::unique_ptr<image_fetcher::ImageFetcher> image_fetcher_; | 388 std::unique_ptr<image_fetcher::ImageFetcher> image_fetcher_; |
389 std::unique_ptr<image_fetcher::ImageDecoder> image_decoder_; | 389 std::unique_ptr<image_fetcher::ImageDecoder> image_decoder_; |
390 | 390 |
391 // The database for persisting snippets. | 391 // The database for persisting snippets. |
392 std::unique_ptr<RemoteSuggestionsDatabase> database_; | 392 std::unique_ptr<RemoteSuggestionsDatabase> database_; |
393 base::TimeTicks database_load_start_; | 393 base::TimeTicks database_load_start_; |
(...skipping 12 matching lines...) Expand all Loading... | |
406 | 406 |
407 // Request throttler for limiting requests to thumbnail images. | 407 // Request throttler for limiting requests to thumbnail images. |
408 RequestThrottler thumbnail_requests_throttler_; | 408 RequestThrottler thumbnail_requests_throttler_; |
409 | 409 |
410 DISALLOW_COPY_AND_ASSIGN(RemoteSuggestionsProvider); | 410 DISALLOW_COPY_AND_ASSIGN(RemoteSuggestionsProvider); |
411 }; | 411 }; |
412 | 412 |
413 } // namespace ntp_snippets | 413 } // namespace ntp_snippets |
414 | 414 |
415 #endif // COMPONENTS_NTP_SNIPPETS_REMOTE_REMOTE_SUGGESTIONS_PROVIDER_H_ | 415 #endif // COMPONENTS_NTP_SNIPPETS_REMOTE_REMOTE_SUGGESTIONS_PROVIDER_H_ |
OLD | NEW |