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_IMPL_H_ |
6 #define COMPONENTS_NTP_SNIPPETS_REMOTE_REMOTE_SUGGESTIONS_PROVIDER_H_ | 6 #define COMPONENTS_NTP_SNIPPETS_REMOTE_REMOTE_SUGGESTIONS_PROVIDER_IMPL_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 <utility> | 14 #include <utility> |
15 #include <vector> | 15 #include <vector> |
16 | 16 |
17 #include "base/callback_forward.h" | 17 #include "base/callback_forward.h" |
18 #include "base/gtest_prod_util.h" | 18 #include "base/gtest_prod_util.h" |
19 #include "base/macros.h" | 19 #include "base/macros.h" |
20 #include "base/time/clock.h" | 20 #include "base/time/clock.h" |
21 #include "base/time/time.h" | 21 #include "base/time/time.h" |
22 #include "components/image_fetcher/image_fetcher_delegate.h" | 22 #include "components/image_fetcher/image_fetcher_delegate.h" |
23 #include "components/ntp_snippets/category.h" | 23 #include "components/ntp_snippets/category.h" |
24 #include "components/ntp_snippets/category_factory.h" | 24 #include "components/ntp_snippets/category_factory.h" |
25 #include "components/ntp_snippets/category_status.h" | 25 #include "components/ntp_snippets/category_status.h" |
26 #include "components/ntp_snippets/content_suggestion.h" | 26 #include "components/ntp_snippets/content_suggestion.h" |
27 #include "components/ntp_snippets/content_suggestions_provider.h" | 27 #include "components/ntp_snippets/content_suggestions_provider.h" |
28 #include "components/ntp_snippets/remote/ntp_snippet.h" | 28 #include "components/ntp_snippets/remote/ntp_snippet.h" |
29 #include "components/ntp_snippets/remote/ntp_snippets_fetcher.h" | 29 #include "components/ntp_snippets/remote/ntp_snippets_fetcher.h" |
30 #include "components/ntp_snippets/remote/ntp_snippets_scheduler.h" | 30 #include "components/ntp_snippets/remote/remote_suggestions_provider.h" |
31 #include "components/ntp_snippets/remote/remote_suggestions_status_service.h" | 31 #include "components/ntp_snippets/remote/remote_suggestions_status_service.h" |
32 #include "components/ntp_snippets/remote/request_throttler.h" | 32 #include "components/ntp_snippets/remote/request_throttler.h" |
33 | 33 |
34 class PrefRegistrySimple; | 34 class PrefRegistrySimple; |
35 class PrefService; | 35 class PrefService; |
36 | 36 |
37 namespace gfx { | 37 namespace gfx { |
38 class Image; | 38 class Image; |
39 } // namespace gfx | 39 } // namespace gfx |
40 | 40 |
41 namespace image_fetcher { | 41 namespace image_fetcher { |
42 class ImageDecoder; | 42 class ImageDecoder; |
43 class ImageFetcher; | 43 class ImageFetcher; |
44 } // namespace image_fetcher | 44 } // namespace image_fetcher |
45 | 45 |
46 namespace ntp_snippets { | 46 namespace ntp_snippets { |
47 | 47 |
48 class RemoteSuggestionsDatabase; | 48 class RemoteSuggestionsDatabase; |
49 class UserClassifier; | |
50 | 49 |
51 // CachedImageFetcher takes care of fetching images from the network and caching | 50 // CachedImageFetcher takes care of fetching images from the network and caching |
52 // them in the database. | 51 // them in the database. |
53 // TODO(tschumann): Move into a separate library and inject the | 52 // TODO(tschumann): Move into a separate library and inject the |
54 // CachedImageFetcher into the RemoteSuggestionsProvider. This allows us to get | 53 // CachedImageFetcher into the RemoteSuggestionsProvider. This allows us to get |
55 // rid of exposing this member for testing and lets us test the caching logic | 54 // rid of exposing this member for testing and lets us test the caching logic |
56 // separately. | 55 // separately. |
57 class CachedImageFetcher : public image_fetcher::ImageFetcherDelegate { | 56 class CachedImageFetcher : public image_fetcher::ImageFetcherDelegate { |
58 public: | 57 public: |
59 // |pref_service| and |database| need to outlive the created image fetcher | 58 // |pref_service| and |database| need to outlive the created image fetcher |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
99 RequestThrottler thumbnail_requests_throttler_; | 98 RequestThrottler thumbnail_requests_throttler_; |
100 | 99 |
101 DISALLOW_COPY_AND_ASSIGN(CachedImageFetcher); | 100 DISALLOW_COPY_AND_ASSIGN(CachedImageFetcher); |
102 }; | 101 }; |
103 | 102 |
104 // Retrieves fresh content data (articles) from the server, stores them and | 103 // Retrieves fresh content data (articles) from the server, stores them and |
105 // provides them as content suggestions. | 104 // provides them as content suggestions. |
106 // This class is final because it does things in its constructor which make it | 105 // This class is final because it does things in its constructor which make it |
107 // unsafe to derive from it. | 106 // unsafe to derive from it. |
108 // TODO(treib): Introduce two-phase initialization and make the class not final? | 107 // TODO(treib): Introduce two-phase initialization and make the class not final? |
109 class RemoteSuggestionsProvider final : public ContentSuggestionsProvider { | 108 class RemoteSuggestionsProviderImpl final : public RemoteSuggestionsProvider { |
110 public: | 109 public: |
110 using FetchStatusCallback = base::Callback<void(Status status_code)>; | |
111 using ProviderActiveCallback = base::RepeatingCallback<void(bool active)>; | |
Marc Treib
2016/12/19 12:59:23
AFAIK Callback and RepeatingCallback are the same
jkrcal
2016/12/20 16:39:46
Removed ProviderActiveCallback. Even FetchStatusCa
| |
111 // |application_language_code| should be a ISO 639-1 compliant string, e.g. | 112 // |application_language_code| should be a ISO 639-1 compliant string, e.g. |
Marc Treib
2016/12/19 12:59:23
nit: empty line before
jkrcal
2016/12/20 16:39:46
Done.
| |
112 // 'en' or 'en-US'. Note that this code should only specify the language, not | 113 // 'en' or 'en-US'. Note that this code should only specify the language, not |
113 // the locale, so 'en_US' (English language with US locale) and 'en-GB_US' | 114 // the locale, so 'en_US' (English language with US locale) and 'en-GB_US' |
114 // (British English person in the US) are not language codes. | 115 // (British English person in the US) are not language codes. |
115 RemoteSuggestionsProvider( | 116 RemoteSuggestionsProviderImpl( |
116 Observer* observer, | 117 Observer* observer, |
117 CategoryFactory* category_factory, | 118 CategoryFactory* category_factory, |
118 PrefService* pref_service, | 119 PrefService* pref_service, |
119 const std::string& application_language_code, | 120 const std::string& application_language_code, |
120 const UserClassifier* user_classifier, | |
121 NTPSnippetsScheduler* scheduler, | |
122 std::unique_ptr<NTPSnippetsFetcher> snippets_fetcher, | 121 std::unique_ptr<NTPSnippetsFetcher> snippets_fetcher, |
123 std::unique_ptr<image_fetcher::ImageFetcher> image_fetcher, | 122 std::unique_ptr<image_fetcher::ImageFetcher> image_fetcher, |
124 std::unique_ptr<image_fetcher::ImageDecoder> image_decoder, | 123 std::unique_ptr<image_fetcher::ImageDecoder> image_decoder, |
125 std::unique_ptr<RemoteSuggestionsDatabase> database, | 124 std::unique_ptr<RemoteSuggestionsDatabase> database, |
126 std::unique_ptr<RemoteSuggestionsStatusService> status_service); | 125 std::unique_ptr<RemoteSuggestionsStatusService> status_service); |
127 | 126 |
128 ~RemoteSuggestionsProvider() override; | 127 ~RemoteSuggestionsProviderImpl() override; |
129 | 128 |
130 static void RegisterProfilePrefs(PrefRegistrySimple* registry); | 129 static void RegisterProfilePrefs(PrefRegistrySimple* registry); |
131 | 130 |
132 // Returns whether the service is ready. While this is false, the list of | 131 // Returns whether the service is ready. While this is false, the list of |
133 // snippets will be empty, and all modifications to it (fetch, dismiss, etc) | 132 // snippets will be empty, and all modifications to it (fetch, dismiss, etc) |
134 // will be ignored. | 133 // will be ignored. |
135 bool ready() const { return state_ == State::READY; } | 134 bool ready() const { return state_ == State::READY; } |
136 | 135 |
137 // Returns whether the service is initialized. While this is false, some | 136 // Returns whether the service is successfuly initialized. While this is |
Marc Treib
2016/12/19 12:59:23
successfully (two "l"s)
jkrcal
2016/12/20 16:39:46
Huh, I have repetitive problems with "success" ;D
| |
138 // calls may trigger DCHECKs. | 137 // false, some calls may trigger DCHECKs. |
139 bool initialized() const { return ready() || state_ == State::DISABLED; } | 138 bool initialized() const { return ready() || state_ == State::DISABLED; } |
140 | 139 |
141 // Fetchs content suggestions from the Content Suggestions server for all | 140 void SetProviderStatusCallback( |
142 // remote categories in the background. | 141 const ProviderStatusCallback& callback) override; |
143 void FetchSnippetsInTheBackground(); | |
144 | 142 |
145 // Fetchs content suggestions from the Content Suggestions server for all | 143 // Fetches snippets from the server for all remote categories and replaces old |
146 // remote categories. The request to the server is performed as an interactive | 144 // snippets by the new ones. The request to the server is performed as an |
147 // request. Interactive requests are used for actions triggered by the user | 145 // background request. Background requests are used for actions not triggered |
148 // and request lower latency processing. | 146 // by the user and have lower priority on the server. After the fetch |
149 void FetchSnippetsForAllCategories(); | 147 // finished, the provided |callback| will be triggered with the status of the |
148 // fetch. | |
149 void RefetchInTheBackground(const FetchStatusCallback& callback) override; | |
150 | 150 |
151 const NTPSnippetsFetcher* snippets_fetcher() const { | 151 const NTPSnippetsFetcher* snippets_fetcher_for_testing_and_debugging() |
152 return snippets_fetcher_.get(); | 152 const override; |
153 } | |
154 | |
155 // (Re)schedules the periodic fetching of snippets. If |force| is true, the | |
156 // tasks will be re-scheduled even if they already exist and have the correct | |
157 // periods. | |
158 void RescheduleFetching(bool force); | |
159 | 153 |
160 // ContentSuggestionsProvider implementation | 154 // ContentSuggestionsProvider implementation |
161 CategoryStatus GetCategoryStatus(Category category) override; | 155 CategoryStatus GetCategoryStatus(Category category) override; |
162 CategoryInfo GetCategoryInfo(Category category) override; | 156 CategoryInfo GetCategoryInfo(Category category) override; |
163 void DismissSuggestion(const ContentSuggestion::ID& suggestion_id) override; | 157 void DismissSuggestion(const ContentSuggestion::ID& suggestion_id) override; |
164 void FetchSuggestionImage(const ContentSuggestion::ID& suggestion_id, | 158 void FetchSuggestionImage(const ContentSuggestion::ID& suggestion_id, |
165 const ImageFetchedCallback& callback) override; | 159 const ImageFetchedCallback& callback) override; |
166 void Fetch(const Category& category, | 160 void Fetch(const Category& category, |
167 const std::set<std::string>& known_suggestion_ids, | 161 const std::set<std::string>& known_suggestion_ids, |
168 const FetchDoneCallback& callback) override; | 162 const FetchDoneCallback& callback) override; |
163 void ReloadSuggestions() override; | |
169 void ClearHistory( | 164 void ClearHistory( |
170 base::Time begin, | 165 base::Time begin, |
171 base::Time end, | 166 base::Time end, |
172 const base::Callback<bool(const GURL& url)>& filter) override; | 167 const base::Callback<bool(const GURL& url)>& filter) override; |
173 void ClearCachedSuggestions(Category category) override; | 168 void ClearCachedSuggestions(Category category) override; |
174 void OnSignInStateChanged() override; | 169 void OnSignInStateChanged() override; |
175 void GetDismissedSuggestionsForDebugging( | 170 void GetDismissedSuggestionsForDebugging( |
176 Category category, | 171 Category category, |
177 const DismissedSuggestionsCallback& callback) override; | 172 const DismissedSuggestionsCallback& callback) override; |
178 void ClearDismissedSuggestionsForDebugging(Category category) override; | 173 void ClearDismissedSuggestionsForDebugging(Category category) override; |
(...skipping 19 matching lines...) Expand all Loading... | |
198 } | 193 } |
199 | 194 |
200 // TODO(tschumann): remove this method as soon as we inject the fetcher into | 195 // TODO(tschumann): remove this method as soon as we inject the fetcher into |
201 // the constructor. | 196 // the constructor. |
202 CachedImageFetcher& GetImageFetcherForTesting() { return image_fetcher_; } | 197 CachedImageFetcher& GetImageFetcherForTesting() { return image_fetcher_; } |
203 | 198 |
204 private: | 199 private: |
205 friend class RemoteSuggestionsProviderTest; | 200 friend class RemoteSuggestionsProviderTest; |
206 | 201 |
207 FRIEND_TEST_ALL_PREFIXES(RemoteSuggestionsProviderTest, | 202 FRIEND_TEST_ALL_PREFIXES(RemoteSuggestionsProviderTest, |
203 CallsProviderActiveCallbackWhenReady); | |
204 FRIEND_TEST_ALL_PREFIXES(RemoteSuggestionsProviderTest, | |
205 CallsProviderActiveCallbackOnError); | |
206 FRIEND_TEST_ALL_PREFIXES(RemoteSuggestionsProviderTest, | |
207 CallsProviderActiveCallbackWhenDisabled); | |
208 FRIEND_TEST_ALL_PREFIXES(RemoteSuggestionsProviderTest, | |
208 DontNotifyIfNotAvailable); | 209 DontNotifyIfNotAvailable); |
209 FRIEND_TEST_ALL_PREFIXES(RemoteSuggestionsProviderTest, | 210 FRIEND_TEST_ALL_PREFIXES(RemoteSuggestionsProviderTest, |
210 RemoveExpiredDismissedContent); | 211 RemoveExpiredDismissedContent); |
211 FRIEND_TEST_ALL_PREFIXES(RemoteSuggestionsProviderTest, | |
212 RescheduleOnStateChange); | |
213 FRIEND_TEST_ALL_PREFIXES(RemoteSuggestionsProviderTest, StatusChanges); | 212 FRIEND_TEST_ALL_PREFIXES(RemoteSuggestionsProviderTest, StatusChanges); |
214 FRIEND_TEST_ALL_PREFIXES(RemoteSuggestionsProviderTest, | 213 FRIEND_TEST_ALL_PREFIXES(RemoteSuggestionsProviderTest, |
215 SuggestionsFetchedOnSignInAndSignOut); | 214 SuggestionsFetchedOnSignInAndSignOut); |
216 | 215 |
217 // Possible state transitions: | 216 // Possible state transitions: |
218 // NOT_INITED --------+ | 217 // NOT_INITED --------+ |
219 // / \ | | 218 // / \ | |
220 // v v | | 219 // v v | |
221 // READY <--> DISABLED | | 220 // READY <--> DISABLED | |
222 // \ / | | 221 // \ / | |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
285 | 284 |
286 explicit CategoryContent(const CategoryInfo& info); | 285 explicit CategoryContent(const CategoryInfo& info); |
287 CategoryContent(CategoryContent&&); | 286 CategoryContent(CategoryContent&&); |
288 ~CategoryContent(); | 287 ~CategoryContent(); |
289 CategoryContent& operator=(CategoryContent&&); | 288 CategoryContent& operator=(CategoryContent&&); |
290 }; | 289 }; |
291 | 290 |
292 // Fetches snippets from the server and replaces old snippets by the new ones. | 291 // Fetches snippets from the server and replaces old snippets by the new ones. |
293 // Requests can be marked more important by setting |interactive_request| to | 292 // Requests can be marked more important by setting |interactive_request| to |
294 // true (such request might circumvent the daily quota for requests, etc.) | 293 // true (such request might circumvent the daily quota for requests, etc.) |
295 // Useful for requests triggered by the user. | 294 // Useful for requests triggered by the user. After the fetch finished, the |
296 void FetchSnippets(bool interactive_request); | 295 // provided |callback| will be triggered with the status of the fetch. |
296 void FetchSnippets(bool interactive_request, | |
297 const FetchStatusCallback& callback); | |
297 | 298 |
298 // Returns the URL of the image of a snippet if it is among the current or | 299 // Returns the URL of the image of a snippet if it is among the current or |
299 // among the archived snippets in the matching category. Returns an empty URL | 300 // among the archived snippets in the matching category. Returns an empty URL |
300 // otherwise. | 301 // otherwise. |
301 GURL FindSnippetImageUrl(const ContentSuggestion::ID& suggestion_id) const; | 302 GURL FindSnippetImageUrl(const ContentSuggestion::ID& suggestion_id) const; |
302 | 303 |
303 // Callbacks for the RemoteSuggestionsDatabase. | 304 // Callbacks for the RemoteSuggestionsDatabase. |
304 void OnDatabaseLoaded(NTPSnippet::PtrVector snippets); | 305 void OnDatabaseLoaded(NTPSnippet::PtrVector snippets); |
305 void OnDatabaseError(); | 306 void OnDatabaseError(); |
306 | 307 |
307 // Callback for fetch-more requests with the NTPSnippetsFetcher. | 308 // Callback for fetch-more requests with the NTPSnippetsFetcher. |
308 void OnFetchMoreFinished( | 309 void OnFetchMoreFinished( |
309 const FetchDoneCallback& fetching_callback, | 310 const FetchDoneCallback& fetching_callback, |
310 Status status, | 311 Status status, |
311 NTPSnippetsFetcher::OptionalFetchedCategories fetched_categories); | 312 NTPSnippetsFetcher::OptionalFetchedCategories fetched_categories); |
312 | 313 |
313 // Callback for regular fetch requests with the NTPSnippetsFetcher. | 314 // Callback for regular fetch requests with the NTPSnippetsFetcher. |
314 void OnFetchFinished( | 315 void OnFetchFinished( |
316 const FetchStatusCallback& callback, | |
315 bool interactive_request, | 317 bool interactive_request, |
316 Status status, | 318 Status status, |
317 NTPSnippetsFetcher::OptionalFetchedCategories fetched_categories); | 319 NTPSnippetsFetcher::OptionalFetchedCategories fetched_categories); |
318 | 320 |
319 // Moves all snippets from |to_archive| into the archive of the |content|. | 321 // Moves all snippets from |to_archive| into the archive of the |content|. |
320 // Clears |to_archive|. As the archive is a FIFO buffer of limited size, this | 322 // Clears |to_archive|. As the archive is a FIFO buffer of limited size, this |
321 // function will also delete images from the database in case the associated | 323 // function will also delete images from the database in case the associated |
322 // snippet gets evicted from the archive. | 324 // snippet gets evicted from the archive. |
323 void ArchiveSnippets(CategoryContent* content, | 325 void ArchiveSnippets(CategoryContent* content, |
324 NTPSnippet::PtrVector* to_archive); | 326 NTPSnippet::PtrVector* to_archive); |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
401 | 403 |
402 PrefService* pref_service_; | 404 PrefService* pref_service_; |
403 | 405 |
404 const Category articles_category_; | 406 const Category articles_category_; |
405 | 407 |
406 std::map<Category, CategoryContent, Category::CompareByID> category_contents_; | 408 std::map<Category, CategoryContent, Category::CompareByID> category_contents_; |
407 | 409 |
408 // The ISO 639-1 code of the language used by the application. | 410 // The ISO 639-1 code of the language used by the application. |
409 const std::string application_language_code_; | 411 const std::string application_language_code_; |
410 | 412 |
411 // Classifier that tells us how active the user is. Not owned. | |
412 const UserClassifier* user_classifier_; | |
413 | |
414 // Scheduler for fetching snippets. Not owned. | |
415 NTPSnippetsScheduler* scheduler_; | |
416 | |
417 // The snippets fetcher. | 413 // The snippets fetcher. |
418 std::unique_ptr<NTPSnippetsFetcher> snippets_fetcher_; | 414 std::unique_ptr<NTPSnippetsFetcher> snippets_fetcher_; |
419 | 415 |
420 // The database for persisting snippets. | 416 // The database for persisting snippets. |
421 std::unique_ptr<RemoteSuggestionsDatabase> database_; | 417 std::unique_ptr<RemoteSuggestionsDatabase> database_; |
422 base::TimeTicks database_load_start_; | 418 base::TimeTicks database_load_start_; |
423 | 419 |
424 // The image fetcher. | 420 // The image fetcher. |
425 CachedImageFetcher image_fetcher_; | 421 CachedImageFetcher image_fetcher_; |
426 | 422 |
427 // The service that provides events and data about the signin and sync state. | 423 // The service that provides events and data about the signin and sync state. |
428 std::unique_ptr<RemoteSuggestionsStatusService> status_service_; | 424 std::unique_ptr<RemoteSuggestionsStatusService> status_service_; |
429 | 425 |
430 // Set to true if FetchSnippets is called while the service isn't ready. | 426 // Set to true if FetchSnippets is called while the service isn't ready. |
431 // The fetch will be executed once the service enters the READY state. | 427 // The fetch will be executed once the service enters the READY state. |
432 bool fetch_when_ready_; | 428 bool fetch_when_ready_; |
429 // The parameters for the fetch to perform later. | |
430 bool fetch_when_ready_interactive_; | |
431 FetchStatusCallback fetch_when_ready_callback_; | |
Marc Treib
2016/12/19 12:59:23
Maybe package all these into a struct, and have a
jkrcal
2016/12/20 16:39:46
I added a TODO. I really want to finish this piece
| |
432 | |
433 ProviderStatusCallback provider_status_callback_; | |
433 | 434 |
434 // Set to true if NukeAllSnippets is called while the service isn't ready. | 435 // Set to true if NukeAllSnippets is called while the service isn't ready. |
435 // The nuke will be executed once the service finishes initialization or | 436 // The nuke will be executed once the service finishes initialization or |
436 // enters the READY state. | 437 // enters the READY state. |
437 bool nuke_when_initialized_; | 438 bool nuke_when_initialized_; |
438 | 439 |
439 // A clock for getting the time. This allows to inject a clock in tests. | 440 // A clock for getting the time. This allows to inject a clock in tests. |
440 std::unique_ptr<base::Clock> clock_; | 441 std::unique_ptr<base::Clock> clock_; |
441 | 442 |
442 DISALLOW_COPY_AND_ASSIGN(RemoteSuggestionsProvider); | 443 DISALLOW_COPY_AND_ASSIGN(RemoteSuggestionsProviderImpl); |
443 }; | 444 }; |
444 | 445 |
445 } // namespace ntp_snippets | 446 } // namespace ntp_snippets |
446 | 447 |
447 #endif // COMPONENTS_NTP_SNIPPETS_REMOTE_REMOTE_SUGGESTIONS_PROVIDER_H_ | 448 #endif // COMPONENTS_NTP_SNIPPETS_REMOTE_REMOTE_SUGGESTIONS_PROVIDER_IMPL_H_ |
OLD | NEW |