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

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

Powered by Google App Engine
This is Rietveld 408576698