Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(449)

Side by Side Diff: components/ntp_snippets/remote/remote_suggestions_provider.h

Issue 2558393004: Refactoring: Moves image fetching and caching into CachedImageFetcher (Closed)
Patch Set: Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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>
(...skipping 28 matching lines...) Expand all
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 UserClassifier; 47 class UserClassifier;
48 48
49 // CachedImageFetcher takes care of fetching images from the network and caching
50 // them in the database.
51 // TODO(tschumann): Move into a separate library and inject the
52 // CachedImageFetcher into the RemoteSuggestionsProvider. This allows us to get
53 // rid of exposing this member for testing and lets us test the caching logic
54 // separately.
55 class CachedImageFetcher : public image_fetcher::ImageFetcherDelegate {
56 public:
57 // |pref_service| and |database| need to outlive the created image fetcher
58 // instance.
59 CachedImageFetcher(std::unique_ptr<image_fetcher::ImageFetcher> image_fetcher,
60 std::unique_ptr<image_fetcher::ImageDecoder> image_decoder,
61 PrefService* pref_service,
62 RemoteSuggestionsDatabase* database);
63 ~CachedImageFetcher() override;
64
65 // Fetches the image for a suggestion. The fetcher will first issue a lookup
66 // to the underlying cache with a fallback to the network.
67 void FetchSuggestionImage(const ContentSuggestion::ID& suggestion_id,
68 const GURL& image_url,
69 const ImageFetchedCallback& callback);
70
71 private:
72 // image_fetcher::ImageFetcherDelegate implementation.
73 void OnImageDataFetched(const std::string& id_within_category,
74 const std::string& image_data) override;
75 void OnImageDecodingDone(const ImageFetchedCallback& callback,
76 const std::string& id_within_category,
77 const gfx::Image& image);
78 void OnSnippetImageFetchedFromDatabase(
79 const ImageFetchedCallback& callback,
80 const ContentSuggestion::ID& suggestion_id,
81 const GURL& image_url,
82 // SnippetImageCallback requires nonconst reference
83 std::string data);
84 void OnSnippetImageDecodedFromDatabase(
85 const ImageFetchedCallback& callback,
86 const ContentSuggestion::ID& suggestion_id,
87 const GURL& url,
88 const gfx::Image& image);
89 void FetchSnippetImageFromNetwork(const ContentSuggestion::ID& suggestion_id,
90 const GURL& url,
91 const ImageFetchedCallback& callback);
92
93 std::unique_ptr<image_fetcher::ImageFetcher> image_fetcher_;
94 std::unique_ptr<image_fetcher::ImageDecoder> image_decoder_;
95 RemoteSuggestionsDatabase* database_;
96 // Request throttler for limiting requests to thumbnail images.
97 RequestThrottler thumbnail_requests_throttler_;
98
99 DISALLOW_COPY_AND_ASSIGN(CachedImageFetcher);
100 };
101
49 // Retrieves fresh content data (articles) from the server, stores them and 102 // Retrieves fresh content data (articles) from the server, stores them and
50 // provides them as content suggestions. 103 // provides them as content suggestions.
51 // This class is final because it does things in its constructor which make it 104 // This class is final because it does things in its constructor which make it
52 // unsafe to derive from it. 105 // unsafe to derive from it.
53 // TODO(treib): Introduce two-phase initialization and make the class not final? 106 // TODO(treib): Introduce two-phase initialization and make the class not final?
54 // TODO(jkrcal): this class grows really, really large. The fact that 107 class RemoteSuggestionsProvider final : public ContentSuggestionsProvider {
55 // NTPSnippetService also implements ImageFetcherDelegate adds unnecessary
56 // complexity (and after all the Service is conceptually not an
57 // ImagerFetcherDeletage ;-)). Instead, the cleaner solution would be to define
58 // a CachedImageFetcher class that handles the caching aspects and looks like an
59 // image fetcher to the NTPSnippetService.
60 class RemoteSuggestionsProvider final
61 : public ContentSuggestionsProvider,
62 public image_fetcher::ImageFetcherDelegate {
63 public: 108 public:
64 // |application_language_code| should be a ISO 639-1 compliant string, e.g. 109 // |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 110 // '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' 111 // 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. 112 // (British English person in the US) are not language codes.
68 RemoteSuggestionsProvider( 113 RemoteSuggestionsProvider(
69 Observer* observer, 114 Observer* observer,
70 CategoryFactory* category_factory, 115 CategoryFactory* category_factory,
71 PrefService* pref_service, 116 PrefService* pref_service,
72 const std::string& application_language_code, 117 const std::string& application_language_code,
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
149 const NTPSnippet::PtrVector& GetDismissedSnippetsForTesting( 194 const NTPSnippet::PtrVector& GetDismissedSnippetsForTesting(
150 Category category) const { 195 Category category) const {
151 return category_contents_.find(category)->second.dismissed; 196 return category_contents_.find(category)->second.dismissed;
152 } 197 }
153 198
154 // Overrides internal clock for testing purposes. 199 // Overrides internal clock for testing purposes.
155 void SetTickClockForTesting(std::unique_ptr<base::TickClock> tick_clock) { 200 void SetTickClockForTesting(std::unique_ptr<base::TickClock> tick_clock) {
156 tick_clock_ = std::move(tick_clock); 201 tick_clock_ = std::move(tick_clock);
157 } 202 }
158 203
204 // TODO(tschumann): remove this method as soon as we inject the fetcher into
205 // the constructor.
206 CachedImageFetcher& GetImageFetcherForTesting() { return image_fetcher_; }
207
159 private: 208 private:
160 friend class RemoteSuggestionsProviderTest; 209 friend class RemoteSuggestionsProviderTest;
161 210
162 FRIEND_TEST_ALL_PREFIXES(RemoteSuggestionsProviderTest, 211 FRIEND_TEST_ALL_PREFIXES(RemoteSuggestionsProviderTest,
163 DontNotifyIfNotAvailable); 212 DontNotifyIfNotAvailable);
164 FRIEND_TEST_ALL_PREFIXES(RemoteSuggestionsProviderTest, 213 FRIEND_TEST_ALL_PREFIXES(RemoteSuggestionsProviderTest,
165 RemoveExpiredDismissedContent); 214 RemoveExpiredDismissedContent);
166 FRIEND_TEST_ALL_PREFIXES(RemoteSuggestionsProviderTest, 215 FRIEND_TEST_ALL_PREFIXES(RemoteSuggestionsProviderTest,
167 RescheduleOnStateChange); 216 RescheduleOnStateChange);
168 FRIEND_TEST_ALL_PREFIXES(RemoteSuggestionsProviderTest, StatusChanges); 217 FRIEND_TEST_ALL_PREFIXES(RemoteSuggestionsProviderTest, StatusChanges);
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
248 // Requests can be marked more important by setting |interactive_request| to 297 // Requests can be marked more important by setting |interactive_request| to
249 // true (such request might circumvent the daily quota for requests, etc.) 298 // true (such request might circumvent the daily quota for requests, etc.)
250 // Useful for requests triggered by the user. 299 // Useful for requests triggered by the user.
251 void FetchSnippets(bool interactive_request); 300 void FetchSnippets(bool interactive_request);
252 301
253 // Returns the URL of the image of a snippet if it is among the current or 302 // 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 303 // among the archived snippets in the matching category. Returns an empty URL
255 // otherwise. 304 // otherwise.
256 GURL FindSnippetImageUrl(const ContentSuggestion::ID& suggestion_id) const; 305 GURL FindSnippetImageUrl(const ContentSuggestion::ID& suggestion_id) const;
257 306
258 // image_fetcher::ImageFetcherDelegate implementation.
259 void OnImageDataFetched(const std::string& id_within_category,
260 const std::string& image_data) override;
261
262 // Callbacks for the RemoteSuggestionsDatabase. 307 // Callbacks for the RemoteSuggestionsDatabase.
263 void OnDatabaseLoaded(NTPSnippet::PtrVector snippets); 308 void OnDatabaseLoaded(NTPSnippet::PtrVector snippets);
264 void OnDatabaseError(); 309 void OnDatabaseError();
265 310
266 // Callback for fetch-more requests with the NTPSnippetsFetcher. 311 // Callback for fetch-more requests with the NTPSnippetsFetcher.
267 void OnFetchMoreFinished( 312 void OnFetchMoreFinished(
268 const FetchDoneCallback& fetching_callback, 313 const FetchDoneCallback& fetching_callback,
269 NTPSnippetsFetcher::FetchResult fetch_result, 314 NTPSnippetsFetcher::FetchResult fetch_result,
270 NTPSnippetsFetcher::OptionalFetchedCategories fetched_categories); 315 NTPSnippetsFetcher::OptionalFetchedCategories fetched_categories);
271 316
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
306 // keep it small but not too often as it still iterates over the file system. 351 // keep it small but not too often as it still iterates over the file system.
307 void ClearOrphanedImages(); 352 void ClearOrphanedImages();
308 353
309 // Clears all stored snippets and updates the observer. 354 // Clears all stored snippets and updates the observer.
310 void NukeAllSnippets(); 355 void NukeAllSnippets();
311 356
312 // Completes the initialization phase of the service, registering the last 357 // Completes the initialization phase of the service, registering the last
313 // observers. This is done after construction, once the database is loaded. 358 // observers. This is done after construction, once the database is loaded.
314 void FinishInitialization(); 359 void FinishInitialization();
315 360
316 void OnSnippetImageFetchedFromDatabase(
317 const ImageFetchedCallback& callback,
318 const ContentSuggestion::ID& suggestion_id,
319 std::string data);
320
321 void OnSnippetImageDecodedFromDatabase(
322 const ImageFetchedCallback& callback,
323 const ContentSuggestion::ID& suggestion_id,
324 const gfx::Image& image);
325
326 void FetchSnippetImageFromNetwork(const ContentSuggestion::ID& suggestion_id,
327 const ImageFetchedCallback& callback);
328
329 void OnSnippetImageDecodedFromNetwork(const ImageFetchedCallback& callback,
330 const std::string& id_within_category,
331 const gfx::Image& image);
332
333 // Triggers a state transition depending on the provided status. This method 361 // Triggers a state transition depending on the provided status. This method
334 // is called when a change is detected by |status_service_|. 362 // is called when a change is detected by |status_service_|.
335 void OnStatusChanged(RemoteSuggestionsStatus old_status, 363 void OnStatusChanged(RemoteSuggestionsStatus old_status,
336 RemoteSuggestionsStatus new_status); 364 RemoteSuggestionsStatus new_status);
337 365
338 // Verifies state transitions (see |State|'s documentation) and applies them. 366 // Verifies state transitions (see |State|'s documentation) and applies them.
339 // Also updates the provider status. Does nothing except updating the provider 367 // Also updates the provider status. Does nothing except updating the provider
340 // status if called with the current state. 368 // status if called with the current state.
341 void EnterState(State state); 369 void EnterState(State state);
342 370
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
386 414
387 // Classifier that tells us how active the user is. Not owned. 415 // Classifier that tells us how active the user is. Not owned.
388 const UserClassifier* user_classifier_; 416 const UserClassifier* user_classifier_;
389 417
390 // Scheduler for fetching snippets. Not owned. 418 // Scheduler for fetching snippets. Not owned.
391 NTPSnippetsScheduler* scheduler_; 419 NTPSnippetsScheduler* scheduler_;
392 420
393 // The snippets fetcher. 421 // The snippets fetcher.
394 std::unique_ptr<NTPSnippetsFetcher> snippets_fetcher_; 422 std::unique_ptr<NTPSnippetsFetcher> snippets_fetcher_;
395 423
396 std::unique_ptr<image_fetcher::ImageFetcher> image_fetcher_;
397 std::unique_ptr<image_fetcher::ImageDecoder> image_decoder_; 424 std::unique_ptr<image_fetcher::ImageDecoder> image_decoder_;
398 425
399 // The database for persisting snippets. 426 // The database for persisting snippets.
400 std::unique_ptr<RemoteSuggestionsDatabase> database_; 427 std::unique_ptr<RemoteSuggestionsDatabase> database_;
428 CachedImageFetcher image_fetcher_;
jkrcal 2016/12/12 09:01:11 Nit: cached_image_fetcher_? (This member name can
Marc Treib 2016/12/12 10:01:03 nit2: This is between |database_| and |database_lo
tschumann 2016/12/13 20:59:09 moved it down. I'd like to push back on renaming i
jkrcal 2016/12/13 21:43:00 Fine for me...
401 base::TimeTicks database_load_start_; 429 base::TimeTicks database_load_start_;
402 430
403 // The service that provides events and data about the signin and sync state. 431 // The service that provides events and data about the signin and sync state.
404 std::unique_ptr<RemoteSuggestionsStatusService> status_service_; 432 std::unique_ptr<RemoteSuggestionsStatusService> status_service_;
405 433
406 // Set to true if FetchSnippets is called while the service isn't ready. 434 // Set to true if FetchSnippets is called while the service isn't ready.
407 // The fetch will be executed once the service enters the READY state. 435 // The fetch will be executed once the service enters the READY state.
408 bool fetch_when_ready_; 436 bool fetch_when_ready_;
409 437
410 // Set to true if NukeAllSnippets is called while the service isn't ready. 438 // Set to true if NukeAllSnippets is called while the service isn't ready.
411 // The nuke will be executed once the service finishes initialization or 439 // The nuke will be executed once the service finishes initialization or
412 // enters the READY state. 440 // enters the READY state.
413 bool nuke_when_initialized_; 441 bool nuke_when_initialized_;
414 442
415 // Request throttler for limiting requests to thumbnail images.
416 RequestThrottler thumbnail_requests_throttler_;
417
418 // A clock for getting the time. This allows to inject a tick clock in tests. 443 // A clock for getting the time. This allows to inject a tick clock in tests.
419 std::unique_ptr<base::TickClock> tick_clock_; 444 std::unique_ptr<base::TickClock> tick_clock_;
420 445
421 DISALLOW_COPY_AND_ASSIGN(RemoteSuggestionsProvider); 446 DISALLOW_COPY_AND_ASSIGN(RemoteSuggestionsProvider);
422 }; 447 };
423 448
424 } // namespace ntp_snippets 449 } // namespace ntp_snippets
425 450
426 #endif // COMPONENTS_NTP_SNIPPETS_REMOTE_REMOTE_SUGGESTIONS_PROVIDER_H_ 451 #endif // COMPONENTS_NTP_SNIPPETS_REMOTE_REMOTE_SUGGESTIONS_PROVIDER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698