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 { | |
tschumann
2016/12/09 17:27:28
can we try to not inherit / implement yet another
markusheintz_
2016/12/12 14:36:16
+1
markusheintz_
2016/12/12 15:32:28
+1
I would even call the method that is then pass
jkrcal
2016/12/14 09:42:09
Done.
| |
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 // Fetches content suggestions from the Content Suggestions server for all |
95 // remote categories in the background. | |
96 void FetchSnippetsInTheBackground(); | |
97 | |
98 // Fetchs content suggestions from the Content Suggestions server for all | |
99 // remote categories. The request to the server is performed as an interactive | 97 // remote categories. The request to the server is performed as an interactive |
100 // request. Interactive requests are used for actions triggered by the user | 98 // request. Interactive requests are used for actions triggered by the user |
101 // and request lower latency processing. | 99 // and request lower latency processing. |
102 void FetchSnippetsForAllCategories(); | 100 void FetchSnippetsForAllCategories(); |
103 | 101 |
104 // Fetches snippets from the server for specified hosts and adds them to the | 102 // 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 | 103 // current ones. Only called from chrome://snippets-internals, DO NOT USE |
106 // otherwise! Ignored while ready() is false. | 104 // otherwise! Ignored while ready() is false. |
107 void FetchSnippetsFromHosts(const std::set<std::string>& hosts, | 105 void FetchSnippetsFromHosts(const std::set<std::string>& hosts, |
108 bool interactive_request); | 106 bool interactive_request); |
109 | 107 |
110 const NTPSnippetsFetcher* snippets_fetcher() const { | 108 const NTPSnippetsFetcher* snippets_fetcher() const { |
111 return snippets_fetcher_.get(); | 109 return snippets_fetcher_.get(); |
112 } | 110 } |
113 | 111 |
114 // (Re)schedules the periodic fetching of snippets. If |force| is true, the | 112 RemoteSuggestionsScheduler* scheduler() { |
tschumann
2016/12/09 17:27:28
nit: who actually needs access to this scheduler?
jkrcal
2016/12/14 09:42:09
Done.
| |
115 // tasks will be re-scheduled even if they already exist and have the correct | 113 return &scheduler_; |
116 // periods. | 114 } |
117 void RescheduleFetching(bool force); | |
118 | 115 |
119 // ContentSuggestionsProvider implementation | 116 // ContentSuggestionsProvider implementation |
120 CategoryStatus GetCategoryStatus(Category category) override; | 117 CategoryStatus GetCategoryStatus(Category category) override; |
121 CategoryInfo GetCategoryInfo(Category category) override; | 118 CategoryInfo GetCategoryInfo(Category category) override; |
122 void DismissSuggestion(const ContentSuggestion::ID& suggestion_id) override; | 119 void DismissSuggestion(const ContentSuggestion::ID& suggestion_id) override; |
123 void FetchSuggestionImage(const ContentSuggestion::ID& suggestion_id, | 120 void FetchSuggestionImage(const ContentSuggestion::ID& suggestion_id, |
124 const ImageFetchedCallback& callback) override; | 121 const ImageFetchedCallback& callback) override; |
125 void Fetch(const Category& category, | 122 void Fetch(const Category& category, |
126 const std::set<std::string>& known_suggestion_ids, | 123 const std::set<std::string>& known_suggestion_ids, |
127 const FetchDoneCallback& callback) override; | 124 const FetchDoneCallback& callback) override; |
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
252 | 249 |
253 // Returns the URL of the image of a snippet if it is among the current or | 250 // Returns the URL of the image of a snippet if it is among the current or |
254 // among the archived snippets in the matching category. Returns an empty URL | 251 // among the archived snippets in the matching category. Returns an empty URL |
255 // otherwise. | 252 // otherwise. |
256 GURL FindSnippetImageUrl(const ContentSuggestion::ID& suggestion_id) const; | 253 GURL FindSnippetImageUrl(const ContentSuggestion::ID& suggestion_id) const; |
257 | 254 |
258 // image_fetcher::ImageFetcherDelegate implementation. | 255 // image_fetcher::ImageFetcherDelegate implementation. |
259 void OnImageDataFetched(const std::string& id_within_category, | 256 void OnImageDataFetched(const std::string& id_within_category, |
260 const std::string& image_data) override; | 257 const std::string& image_data) override; |
261 | 258 |
259 // RemoteSuggestionsScheduler::Updater implementation | |
260 void UpdateRemoteSuggestionsBySchedule() override; | |
261 | |
262 // Callbacks for the RemoteSuggestionsDatabase. | 262 // Callbacks for the RemoteSuggestionsDatabase. |
263 void OnDatabaseLoaded(NTPSnippet::PtrVector snippets); | 263 void OnDatabaseLoaded(NTPSnippet::PtrVector snippets); |
264 void OnDatabaseError(); | 264 void OnDatabaseError(); |
265 | 265 |
266 // Callback for fetch-more requests with the NTPSnippetsFetcher. | 266 // Callback for fetch-more requests with the NTPSnippetsFetcher. |
267 void OnFetchMoreFinished( | 267 void OnFetchMoreFinished( |
268 const FetchDoneCallback& fetching_callback, | 268 const FetchDoneCallback& fetching_callback, |
269 NTPSnippetsFetcher::FetchResult fetch_result, | 269 NTPSnippetsFetcher::FetchResult fetch_result, |
270 NTPSnippetsFetcher::OptionalFetchedCategories fetched_categories); | 270 NTPSnippetsFetcher::OptionalFetchedCategories fetched_categories); |
271 | 271 |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
380 const Category articles_category_; | 380 const Category articles_category_; |
381 | 381 |
382 std::map<Category, CategoryContent, Category::CompareByID> category_contents_; | 382 std::map<Category, CategoryContent, Category::CompareByID> category_contents_; |
383 | 383 |
384 // The ISO 639-1 code of the language used by the application. | 384 // The ISO 639-1 code of the language used by the application. |
385 const std::string application_language_code_; | 385 const std::string application_language_code_; |
386 | 386 |
387 // Classifier that tells us how active the user is. Not owned. | 387 // Classifier that tells us how active the user is. Not owned. |
388 const UserClassifier* user_classifier_; | 388 const UserClassifier* user_classifier_; |
389 | 389 |
390 // Scheduler for fetching snippets. Not owned. | 390 // Scheduler for fetching snippets. |
391 NTPSnippetsScheduler* scheduler_; | 391 RemoteSuggestionsScheduler scheduler_; |
392 | 392 |
393 // The snippets fetcher. | 393 // The snippets fetcher. |
394 std::unique_ptr<NTPSnippetsFetcher> snippets_fetcher_; | 394 std::unique_ptr<NTPSnippetsFetcher> snippets_fetcher_; |
395 | 395 |
396 std::unique_ptr<image_fetcher::ImageFetcher> image_fetcher_; | 396 std::unique_ptr<image_fetcher::ImageFetcher> image_fetcher_; |
397 std::unique_ptr<image_fetcher::ImageDecoder> image_decoder_; | 397 std::unique_ptr<image_fetcher::ImageDecoder> image_decoder_; |
398 | 398 |
399 // The database for persisting snippets. | 399 // The database for persisting snippets. |
400 std::unique_ptr<RemoteSuggestionsDatabase> database_; | 400 std::unique_ptr<RemoteSuggestionsDatabase> database_; |
401 base::TimeTicks database_load_start_; | 401 base::TimeTicks database_load_start_; |
(...skipping 15 matching lines...) Expand all Loading... | |
417 | 417 |
418 // A clock for getting the time. This allows to inject a tick clock in tests. | 418 // A clock for getting the time. This allows to inject a tick clock in tests. |
419 std::unique_ptr<base::TickClock> tick_clock_; | 419 std::unique_ptr<base::TickClock> tick_clock_; |
420 | 420 |
421 DISALLOW_COPY_AND_ASSIGN(RemoteSuggestionsProvider); | 421 DISALLOW_COPY_AND_ASSIGN(RemoteSuggestionsProvider); |
422 }; | 422 }; |
423 | 423 |
424 } // namespace ntp_snippets | 424 } // namespace ntp_snippets |
425 | 425 |
426 #endif // COMPONENTS_NTP_SNIPPETS_REMOTE_REMOTE_SUGGESTIONS_PROVIDER_H_ | 426 #endif // COMPONENTS_NTP_SNIPPETS_REMOTE_REMOTE_SUGGESTIONS_PROVIDER_H_ |
OLD | NEW |