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

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

Powered by Google App Engine
This is Rietveld 408576698