cached_image_fetcher.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // Copyright 2017 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef COMPONENTS_NTP_SNIPPETS_REMOTE_CACHED_IMAGE_FETCHER_H_
  5. #define COMPONENTS_NTP_SNIPPETS_REMOTE_CACHED_IMAGE_FETCHER_H_
  6. #include <cstddef>
  7. #include <memory>
  8. #include <string>
  9. #include "base/callback_forward.h"
  10. #include "base/gtest_prod_util.h"
  11. #include "base/memory/raw_ptr.h"
  12. #include "base/memory/weak_ptr.h"
  13. #include "components/ntp_snippets/callbacks.h"
  14. #include "components/ntp_snippets/content_suggestion.h"
  15. #include "components/ntp_snippets/remote/request_throttler.h"
  16. class PrefService;
  17. namespace gfx {
  18. class Image;
  19. } // namespace gfx
  20. namespace image_fetcher {
  21. class ImageFetcher;
  22. struct RequestMetadata;
  23. } // namespace image_fetcher
  24. namespace ntp_snippets {
  25. class RemoteSuggestionsDatabase;
  26. // CachedImageFetcher takes care of fetching images from the network and caching
  27. // them in the database.
  28. class CachedImageFetcher {
  29. public:
  30. // |pref_service| and |database| need to outlive the created image fetcher
  31. // instance.
  32. CachedImageFetcher(std::unique_ptr<image_fetcher::ImageFetcher> image_fetcher,
  33. PrefService* pref_service,
  34. RemoteSuggestionsDatabase* database);
  35. CachedImageFetcher(const CachedImageFetcher&) = delete;
  36. CachedImageFetcher& operator=(const CachedImageFetcher&) = delete;
  37. virtual ~CachedImageFetcher();
  38. // Fetches the image for a suggestion. The fetcher will first issue a lookup
  39. // to the underlying cache with a fallback to the network.
  40. virtual void FetchSuggestionImage(
  41. const ContentSuggestion::ID& suggestion_id,
  42. const GURL& image_url,
  43. ImageDataFetchedCallback image_data_callback,
  44. ImageFetchedCallback image_callback);
  45. private:
  46. void OnImageDataFetched(const std::string& id_within_category,
  47. const std::string& image_data,
  48. const image_fetcher::RequestMetadata& metadata);
  49. void OnImageDecodingDone(ImageFetchedCallback callback,
  50. const std::string& id_within_category,
  51. const gfx::Image& image,
  52. const image_fetcher::RequestMetadata& metadata);
  53. void OnImageFetchingDone(ImageFetchedCallback callback,
  54. const gfx::Image& image,
  55. const image_fetcher::RequestMetadata& metadata);
  56. void OnImageFetchedFromDatabase(
  57. ImageDataFetchedCallback image_data_callback,
  58. ImageFetchedCallback image_callback,
  59. const ContentSuggestion::ID& suggestion_id,
  60. const GURL& image_url,
  61. // SnippetImageCallback requires by-value (not const ref).
  62. std::string data);
  63. void OnImageDecodedFromDatabase(ImageFetchedCallback callback,
  64. const ContentSuggestion::ID& suggestion_id,
  65. const GURL& url,
  66. const gfx::Image& image);
  67. void FetchImageFromNetwork(const ContentSuggestion::ID& suggestion_id,
  68. const GURL& url,
  69. ImageDataFetchedCallback image_data_callback,
  70. ImageFetchedCallback image_callback);
  71. void SaveImageAndInvokeDataCallback(
  72. const std::string& id_within_category,
  73. ImageDataFetchedCallback callback,
  74. const std::string& image_data,
  75. const image_fetcher::RequestMetadata& request_metadata);
  76. std::unique_ptr<image_fetcher::ImageFetcher> image_fetcher_;
  77. raw_ptr<RemoteSuggestionsDatabase> database_;
  78. // Request throttler for limiting requests to thumbnail images.
  79. RequestThrottler thumbnail_requests_throttler_;
  80. base::WeakPtrFactory<CachedImageFetcher> weak_ptr_factory_{this};
  81. };
  82. } // namespace ntp_snippets
  83. #endif // COMPONENTS_NTP_SNIPPETS_REMOTE_CACHED_IMAGE_FETCHER_H_