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/clock.h" | 19 #include "base/time/clock.h" |
20 #include "base/time/time.h" | 20 #include "base/time/time.h" |
21 #include "components/image_fetcher/image_fetcher_delegate.h" | 21 #include "components/image_fetcher/image_fetcher_delegate.h" |
22 #include "components/ntp_snippets/category.h" | 22 #include "components/ntp_snippets/category.h" |
23 #include "components/ntp_snippets/category_factory.h" | 23 #include "components/ntp_snippets/category_factory.h" |
24 #include "components/ntp_snippets/category_status.h" | 24 #include "components/ntp_snippets/category_status.h" |
25 #include "components/ntp_snippets/content_suggestion.h" | 25 #include "components/ntp_snippets/content_suggestion.h" |
26 #include "components/ntp_snippets/content_suggestions_provider.h" | 26 #include "components/ntp_snippets/content_suggestions_provider.h" |
27 #include "components/ntp_snippets/remote/ntp_snippet.h" | 27 #include "components/ntp_snippets/remote/ntp_snippet.h" |
28 #include "components/ntp_snippets/remote/ntp_snippets_fetcher.h" | 28 #include "components/ntp_snippets/remote/ntp_snippets_fetcher.h" |
29 #include "components/ntp_snippets/remote/ntp_snippets_scheduler.h" | |
30 #include "components/ntp_snippets/remote/remote_suggestions_status_service.h" | 29 #include "components/ntp_snippets/remote/remote_suggestions_status_service.h" |
31 #include "components/ntp_snippets/remote/request_throttler.h" | 30 #include "components/ntp_snippets/remote/request_throttler.h" |
32 | 31 |
33 class PrefRegistrySimple; | 32 class PrefRegistrySimple; |
34 class PrefService; | 33 class PrefService; |
35 | 34 |
36 namespace gfx { | 35 namespace gfx { |
37 class Image; | 36 class Image; |
38 } // namespace gfx | 37 } // namespace gfx |
39 | 38 |
40 namespace image_fetcher { | 39 namespace image_fetcher { |
41 class ImageDecoder; | 40 class ImageDecoder; |
42 class ImageFetcher; | 41 class ImageFetcher; |
43 } // namespace image_fetcher | 42 } // namespace image_fetcher |
44 | 43 |
45 namespace ntp_snippets { | 44 namespace ntp_snippets { |
46 | 45 |
47 class RemoteSuggestionsDatabase; | 46 class RemoteSuggestionsDatabase; |
48 class UserClassifier; | |
49 | 47 |
50 // Retrieves fresh content data (articles) from the server, stores them and | 48 // Retrieves fresh content data (articles) from the server, stores them and |
51 // provides them as content suggestions. | 49 // provides them as content suggestions. |
52 // This class is final because it does things in its constructor which make it | 50 // This class is final because it does things in its constructor which make it |
53 // unsafe to derive from it. | 51 // unsafe to derive from it. |
54 // TODO(treib): Introduce two-phase initialization and make the class not final? | 52 // TODO(treib): Introduce two-phase initialization and make the class not final? |
55 // TODO(jkrcal): this class grows really, really large. The fact that | 53 // TODO(jkrcal): this class grows really, really large. The fact that |
56 // NTPSnippetService also implements ImageFetcherDelegate adds unnecessary | 54 // NTPSnippetService also implements ImageFetcherDelegate adds unnecessary |
57 // complexity (and after all the Service is conceptually not an | 55 // complexity (and after all the Service is conceptually not an |
58 // ImagerFetcherDeletage ;-)). Instead, the cleaner solution would be to define | 56 // ImagerFetcherDeletage ;-)). Instead, the cleaner solution would be to define |
59 // a CachedImageFetcher class that handles the caching aspects and looks like an | 57 // a CachedImageFetcher class that handles the caching aspects and looks like an |
60 // image fetcher to the NTPSnippetService. | 58 // image fetcher to the NTPSnippetService. |
61 class RemoteSuggestionsProvider final | 59 class RemoteSuggestionsProvider final |
62 : public ContentSuggestionsProvider, | 60 : public ContentSuggestionsProvider, |
63 public image_fetcher::ImageFetcherDelegate { | 61 public image_fetcher::ImageFetcherDelegate { |
64 public: | 62 public: |
63 using FetchStatusCallback = base::Callback<void(Status status_code)>; | |
64 using ProviderActiveCallback = base::RepeatingCallback<void(bool active)>; | |
65 // |application_language_code| should be a ISO 639-1 compliant string, e.g. | 65 // |application_language_code| should be a ISO 639-1 compliant string, e.g. |
66 // 'en' or 'en-US'. Note that this code should only specify the language, not | 66 // 'en' or 'en-US'. Note that this code should only specify the language, not |
67 // the locale, so 'en_US' (English language with US locale) and 'en-GB_US' | 67 // the locale, so 'en_US' (English language with US locale) and 'en-GB_US' |
68 // (British English person in the US) are not language codes. | 68 // (British English person in the US) are not language codes. |
69 RemoteSuggestionsProvider( | 69 RemoteSuggestionsProvider( |
70 Observer* observer, | 70 Observer* observer, |
71 CategoryFactory* category_factory, | 71 CategoryFactory* category_factory, |
72 PrefService* pref_service, | 72 PrefService* pref_service, |
73 const std::string& application_language_code, | 73 const std::string& application_language_code, |
74 const UserClassifier* user_classifier, | |
75 NTPSnippetsScheduler* scheduler, | |
76 std::unique_ptr<NTPSnippetsFetcher> snippets_fetcher, | 74 std::unique_ptr<NTPSnippetsFetcher> snippets_fetcher, |
77 std::unique_ptr<image_fetcher::ImageFetcher> image_fetcher, | 75 std::unique_ptr<image_fetcher::ImageFetcher> image_fetcher, |
78 std::unique_ptr<image_fetcher::ImageDecoder> image_decoder, | 76 std::unique_ptr<image_fetcher::ImageDecoder> image_decoder, |
79 std::unique_ptr<RemoteSuggestionsDatabase> database, | 77 std::unique_ptr<RemoteSuggestionsDatabase> database, |
80 std::unique_ptr<RemoteSuggestionsStatusService> status_service); | 78 std::unique_ptr<RemoteSuggestionsStatusService> status_service); |
81 | 79 |
82 ~RemoteSuggestionsProvider() override; | 80 ~RemoteSuggestionsProvider() override; |
83 | 81 |
84 static void RegisterProfilePrefs(PrefRegistrySimple* registry); | 82 static void RegisterProfilePrefs(PrefRegistrySimple* registry); |
85 | 83 |
86 // Returns whether the service is ready. While this is false, the list of | 84 // Returns whether the service is ready. While this is false, the list of |
87 // snippets will be empty, and all modifications to it (fetch, dismiss, etc) | 85 // snippets will be empty, and all modifications to it (fetch, dismiss, etc) |
88 // will be ignored. | 86 // will be ignored. |
89 bool ready() const { return state_ == State::READY; } | 87 bool ready() const { return state_ == State::READY; } |
90 | 88 |
91 // Returns whether the service is initialized. While this is false, some | 89 // Returns whether the service is successfuly initialized. While this is |
92 // calls may trigger DCHECKs. | 90 // false, some calls may trigger DCHECKs. |
93 bool initialized() const { return ready() || state_ == State::DISABLED; } | 91 bool initialized() const { return ready() || state_ == State::DISABLED; } |
94 | 92 |
95 // Fetchs content suggestions from the Content Suggestions server for all | 93 void RegisterActivenessObserver(const ProviderActiveCallback& callback); |
tschumann
2016/12/15 19:27:00
naming is hard. Maybe we change this to
rename th
jkrcal
2016/12/19 09:40:24
Renamed as suggested. Totally agree with both "Set
tschumann
2016/12/19 11:19:23
I see. Well in this case we're in good shape. If t
jkrcal
2016/12/20 16:39:45
Done.
| |
96 // remote categories in the background. | |
97 void FetchSnippetsInTheBackground(); | |
98 | 94 |
99 // Fetchs content suggestions from the Content Suggestions server for all | 95 // Fetches snippets from the server for all remote categories and replaces old |
100 // remote categories. The request to the server is performed as an interactive | 96 // snippets by the new ones. The request to the server is performed as an |
101 // request. Interactive requests are used for actions triggered by the user | 97 // background request. Background requests are used for actions not triggered |
102 // and request lower latency processing. | 98 // by the user and have lower priority on the server. After the fetch |
103 void FetchSnippetsForAllCategories(); | 99 // finished, the provided |callback| will be triggered with the status of the |
100 // fetch. | |
101 void RefetchInTheBackground(const FetchStatusCallback& callback); | |
104 | 102 |
105 const NTPSnippetsFetcher* snippets_fetcher() const { | 103 const NTPSnippetsFetcher* snippets_fetcher() const { |
106 return snippets_fetcher_.get(); | 104 return snippets_fetcher_.get(); |
107 } | 105 } |
108 | 106 |
109 // (Re)schedules the periodic fetching of snippets. If |force| is true, the | |
110 // tasks will be re-scheduled even if they already exist and have the correct | |
111 // periods. | |
112 void RescheduleFetching(bool force); | |
113 | |
114 // ContentSuggestionsProvider implementation | 107 // ContentSuggestionsProvider implementation |
115 CategoryStatus GetCategoryStatus(Category category) override; | 108 CategoryStatus GetCategoryStatus(Category category) override; |
116 CategoryInfo GetCategoryInfo(Category category) override; | 109 CategoryInfo GetCategoryInfo(Category category) override; |
117 void DismissSuggestion(const ContentSuggestion::ID& suggestion_id) override; | 110 void DismissSuggestion(const ContentSuggestion::ID& suggestion_id) override; |
118 void FetchSuggestionImage(const ContentSuggestion::ID& suggestion_id, | 111 void FetchSuggestionImage(const ContentSuggestion::ID& suggestion_id, |
119 const ImageFetchedCallback& callback) override; | 112 const ImageFetchedCallback& callback) override; |
120 void Fetch(const Category& category, | 113 void Fetch(const Category& category, |
121 const std::set<std::string>& known_suggestion_ids, | 114 const std::set<std::string>& known_suggestion_ids, |
122 const FetchDoneCallback& callback) override; | 115 const FetchDoneCallback& callback) override; |
116 void ReloadSuggestions() override; | |
123 void ClearHistory( | 117 void ClearHistory( |
124 base::Time begin, | 118 base::Time begin, |
125 base::Time end, | 119 base::Time end, |
126 const base::Callback<bool(const GURL& url)>& filter) override; | 120 const base::Callback<bool(const GURL& url)>& filter) override; |
127 void ClearCachedSuggestions(Category category) override; | 121 void ClearCachedSuggestions(Category category) override; |
128 void OnSignInStateChanged() override; | 122 void OnSignInStateChanged() override; |
129 void GetDismissedSuggestionsForDebugging( | 123 void GetDismissedSuggestionsForDebugging( |
130 Category category, | 124 Category category, |
131 const DismissedSuggestionsCallback& callback) override; | 125 const DismissedSuggestionsCallback& callback) override; |
132 void ClearDismissedSuggestionsForDebugging(Category category) override; | 126 void ClearDismissedSuggestionsForDebugging(Category category) override; |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
235 | 229 |
236 explicit CategoryContent(const CategoryInfo& info); | 230 explicit CategoryContent(const CategoryInfo& info); |
237 CategoryContent(CategoryContent&&); | 231 CategoryContent(CategoryContent&&); |
238 ~CategoryContent(); | 232 ~CategoryContent(); |
239 CategoryContent& operator=(CategoryContent&&); | 233 CategoryContent& operator=(CategoryContent&&); |
240 }; | 234 }; |
241 | 235 |
242 // Fetches snippets from the server and replaces old snippets by the new ones. | 236 // Fetches snippets from the server and replaces old snippets by the new ones. |
243 // Requests can be marked more important by setting |interactive_request| to | 237 // Requests can be marked more important by setting |interactive_request| to |
244 // true (such request might circumvent the daily quota for requests, etc.) | 238 // true (such request might circumvent the daily quota for requests, etc.) |
245 // Useful for requests triggered by the user. | 239 // Useful for requests triggered by the user. After the fetch finished, the |
246 void FetchSnippets(bool interactive_request); | 240 // provided |callback| will be triggered with the status of the fetch. |
241 void FetchSnippets(bool interactive_request, | |
242 const FetchStatusCallback& callback); | |
247 | 243 |
248 // Returns the URL of the image of a snippet if it is among the current or | 244 // Returns the URL of the image of a snippet if it is among the current or |
249 // among the archived snippets in the matching category. Returns an empty URL | 245 // among the archived snippets in the matching category. Returns an empty URL |
250 // otherwise. | 246 // otherwise. |
251 GURL FindSnippetImageUrl(const ContentSuggestion::ID& suggestion_id) const; | 247 GURL FindSnippetImageUrl(const ContentSuggestion::ID& suggestion_id) const; |
252 | 248 |
253 // image_fetcher::ImageFetcherDelegate implementation. | 249 // image_fetcher::ImageFetcherDelegate implementation. |
254 void OnImageDataFetched(const std::string& id_within_category, | 250 void OnImageDataFetched(const std::string& id_within_category, |
255 const std::string& image_data) override; | 251 const std::string& image_data) override; |
256 | 252 |
257 // Callbacks for the RemoteSuggestionsDatabase. | 253 // Callbacks for the RemoteSuggestionsDatabase. |
258 void OnDatabaseLoaded(NTPSnippet::PtrVector snippets); | 254 void OnDatabaseLoaded(NTPSnippet::PtrVector snippets); |
259 void OnDatabaseError(); | 255 void OnDatabaseError(); |
260 | 256 |
261 // Callback for fetch-more requests with the NTPSnippetsFetcher. | 257 // Callback for fetch-more requests with the NTPSnippetsFetcher. |
262 void OnFetchMoreFinished( | 258 void OnFetchMoreFinished( |
263 const FetchDoneCallback& fetching_callback, | 259 const FetchDoneCallback& fetching_callback, |
264 NTPSnippetsFetcher::FetchResult fetch_result, | 260 NTPSnippetsFetcher::FetchResult fetch_result, |
265 NTPSnippetsFetcher::OptionalFetchedCategories fetched_categories); | 261 NTPSnippetsFetcher::OptionalFetchedCategories fetched_categories); |
266 | 262 |
267 // Callback for regular fetch requests with the NTPSnippetsFetcher. | 263 // Callback for regular fetch requests with the NTPSnippetsFetcher. |
268 void OnFetchFinished( | 264 void OnFetchFinished( |
265 const FetchStatusCallback& callback, | |
269 bool interactive_request, | 266 bool interactive_request, |
270 NTPSnippetsFetcher::FetchResult fetch_status, | 267 NTPSnippetsFetcher::FetchResult fetch_status, |
271 NTPSnippetsFetcher::OptionalFetchedCategories fetched_categories); | 268 NTPSnippetsFetcher::OptionalFetchedCategories fetched_categories); |
272 | 269 |
273 // Moves all snippets from |to_archive| into the archive of the |content|. | 270 // Moves all snippets from |to_archive| into the archive of the |content|. |
274 // Clears |to_archive|. As the archive is a FIFO buffer of limited size, this | 271 // Clears |to_archive|. As the archive is a FIFO buffer of limited size, this |
275 // function will also delete images from the database in case the associated | 272 // function will also delete images from the database in case the associated |
276 // snippet gets evicted from the archive. | 273 // snippet gets evicted from the archive. |
277 void ArchiveSnippets(CategoryContent* content, | 274 void ArchiveSnippets(CategoryContent* content, |
278 NTPSnippet::PtrVector* to_archive); | 275 NTPSnippet::PtrVector* to_archive); |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
372 | 369 |
373 PrefService* pref_service_; | 370 PrefService* pref_service_; |
374 | 371 |
375 const Category articles_category_; | 372 const Category articles_category_; |
376 | 373 |
377 std::map<Category, CategoryContent, Category::CompareByID> category_contents_; | 374 std::map<Category, CategoryContent, Category::CompareByID> category_contents_; |
378 | 375 |
379 // 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. |
380 const std::string application_language_code_; | 377 const std::string application_language_code_; |
381 | 378 |
382 // Classifier that tells us how active the user is. Not owned. | |
383 const UserClassifier* user_classifier_; | |
384 | |
385 // Scheduler for fetching snippets. Not owned. | |
386 NTPSnippetsScheduler* scheduler_; | |
387 | |
388 // The snippets fetcher. | 379 // The snippets fetcher. |
389 std::unique_ptr<NTPSnippetsFetcher> snippets_fetcher_; | 380 std::unique_ptr<NTPSnippetsFetcher> snippets_fetcher_; |
390 | 381 |
391 std::unique_ptr<image_fetcher::ImageFetcher> image_fetcher_; | 382 std::unique_ptr<image_fetcher::ImageFetcher> image_fetcher_; |
392 std::unique_ptr<image_fetcher::ImageDecoder> image_decoder_; | 383 std::unique_ptr<image_fetcher::ImageDecoder> image_decoder_; |
393 | 384 |
394 // The database for persisting snippets. | 385 // The database for persisting snippets. |
395 std::unique_ptr<RemoteSuggestionsDatabase> database_; | 386 std::unique_ptr<RemoteSuggestionsDatabase> database_; |
396 base::TimeTicks database_load_start_; | 387 base::TimeTicks database_load_start_; |
397 | 388 |
398 // The service that provides events and data about the signin and sync state. | 389 // The service that provides events and data about the signin and sync state. |
399 std::unique_ptr<RemoteSuggestionsStatusService> status_service_; | 390 std::unique_ptr<RemoteSuggestionsStatusService> status_service_; |
400 | 391 |
401 // Set to true if FetchSnippets is called while the service isn't ready. | 392 // Set to true if FetchSnippets is called while the service isn't ready. |
402 // The fetch will be executed once the service enters the READY state. | 393 // The fetch will be executed once the service enters the READY state. |
403 bool fetch_when_ready_; | 394 bool fetch_when_ready_; |
395 // The parameters for the fetch to perform later. | |
396 bool fetch_when_ready_interactive_; | |
397 FetchStatusCallback fetch_when_ready_callback_; | |
398 | |
399 ProviderActiveCallback provider_active_callback_; | |
404 | 400 |
405 // Set to true if NukeAllSnippets is called while the service isn't ready. | 401 // Set to true if NukeAllSnippets is called while the service isn't ready. |
406 // The nuke will be executed once the service finishes initialization or | 402 // The nuke will be executed once the service finishes initialization or |
407 // enters the READY state. | 403 // enters the READY state. |
408 bool nuke_when_initialized_; | 404 bool nuke_when_initialized_; |
409 | 405 |
410 // Request throttler for limiting requests to thumbnail images. | 406 // Request throttler for limiting requests to thumbnail images. |
411 RequestThrottler thumbnail_requests_throttler_; | 407 RequestThrottler thumbnail_requests_throttler_; |
412 | 408 |
413 // A clock for getting the time. This allows to inject a clock in tests. | 409 // A clock for getting the time. This allows to inject a clock in tests. |
414 std::unique_ptr<base::Clock> clock_; | 410 std::unique_ptr<base::Clock> clock_; |
415 | 411 |
416 DISALLOW_COPY_AND_ASSIGN(RemoteSuggestionsProvider); | 412 DISALLOW_COPY_AND_ASSIGN(RemoteSuggestionsProvider); |
417 }; | 413 }; |
418 | 414 |
419 } // namespace ntp_snippets | 415 } // namespace ntp_snippets |
420 | 416 |
421 #endif // COMPONENTS_NTP_SNIPPETS_REMOTE_REMOTE_SUGGESTIONS_PROVIDER_H_ | 417 #endif // COMPONENTS_NTP_SNIPPETS_REMOTE_REMOTE_SUGGESTIONS_PROVIDER_H_ |
OLD | NEW |