icon_cacher_impl.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // Copyright 2016 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_TILES_ICON_CACHER_IMPL_H_
  5. #define COMPONENTS_NTP_TILES_ICON_CACHER_IMPL_H_
  6. #include <memory>
  7. #include <set>
  8. #include <vector>
  9. #include "base/callback.h"
  10. #include "base/cancelable_callback.h"
  11. #include "base/memory/raw_ptr.h"
  12. #include "base/memory/weak_ptr.h"
  13. #include "base/task/cancelable_task_tracker.h"
  14. #include "components/ntp_tiles/icon_cacher.h"
  15. #include "components/ntp_tiles/popular_sites.h"
  16. namespace data_decoder {
  17. class DataDecoder;
  18. } // namespace data_decoder
  19. namespace favicon {
  20. class FaviconService;
  21. class LargeIconService;
  22. } // namespace favicon
  23. namespace favicon_base {
  24. struct FaviconImageResult;
  25. struct LargeIconResult;
  26. enum class GoogleFaviconServerRequestStatus;
  27. } // namespace favicon_base
  28. namespace gfx {
  29. class Image;
  30. } // namespace gfx
  31. namespace image_fetcher {
  32. class ImageFetcher;
  33. struct RequestMetadata;
  34. } // namespace image_fetcher
  35. namespace ntp_tiles {
  36. class IconCacherImpl : public IconCacher {
  37. public:
  38. // TODO(jkrcal): Make this eventually use only LargeIconService.
  39. // crbug.com/696563
  40. IconCacherImpl(favicon::FaviconService* favicon_service,
  41. favicon::LargeIconService* large_icon_service,
  42. std::unique_ptr<image_fetcher::ImageFetcher> image_fetcher,
  43. std::unique_ptr<data_decoder::DataDecoder> data_decoder);
  44. IconCacherImpl(const IconCacherImpl&) = delete;
  45. IconCacherImpl& operator=(const IconCacherImpl&) = delete;
  46. ~IconCacherImpl() override;
  47. void StartFetchPopularSites(
  48. PopularSites::Site site,
  49. base::OnceClosure icon_available,
  50. base::OnceClosure preliminary_icon_available) override;
  51. // TODO(jkrcal): Rename all instances of "MostLikely" to "ChromeSuggestions".
  52. void StartFetchMostLikely(const GURL& page_url,
  53. base::OnceClosure icon_available) override;
  54. private:
  55. using CancelableImageCallback =
  56. base::CancelableOnceCallback<void(const gfx::Image&)>;
  57. void OnGetFaviconImageForPageURLFinished(
  58. PopularSites::Site site,
  59. base::OnceClosure preliminary_icon_available,
  60. const favicon_base::FaviconImageResult& result);
  61. void OnPopularSitesFaviconDownloaded(
  62. PopularSites::Site site,
  63. std::unique_ptr<CancelableImageCallback> preliminary_callback,
  64. const gfx::Image& fetched_image,
  65. const image_fetcher::RequestMetadata& metadata);
  66. std::unique_ptr<CancelableImageCallback> MaybeProvideDefaultIcon(
  67. const PopularSites::Site& site,
  68. base::OnceClosure preliminary_icon_available);
  69. void SaveAndNotifyDefaultIconForSite(
  70. const PopularSites::Site& site,
  71. base::OnceClosure preliminary_icon_available,
  72. const gfx::Image& image);
  73. void SaveIconForSite(const PopularSites::Site& site, const gfx::Image& image);
  74. void OnGetLargeIconOrFallbackStyleFinished(
  75. const GURL& page_url,
  76. const favicon_base::LargeIconResult& result);
  77. void OnMostLikelyFaviconDownloaded(
  78. const GURL& request_url,
  79. favicon_base::GoogleFaviconServerRequestStatus status);
  80. bool StartRequest(const GURL& request_url, base::OnceClosure icon_available);
  81. void FinishRequestAndNotifyIconAvailable(const GURL& request_url,
  82. bool newly_available);
  83. base::CancelableTaskTracker tracker_;
  84. const raw_ptr<favicon::FaviconService> favicon_service_;
  85. const raw_ptr<favicon::LargeIconService> large_icon_service_;
  86. std::unique_ptr<image_fetcher::ImageFetcher> const image_fetcher_;
  87. std::map<GURL, std::vector<base::OnceClosure>> in_flight_requests_;
  88. std::unique_ptr<data_decoder::DataDecoder> data_decoder_;
  89. base::WeakPtrFactory<IconCacherImpl> weak_ptr_factory_{this};
  90. };
  91. } // namespace ntp_tiles
  92. #endif // COMPONENTS_NTP_TILES_ICON_CACHER_IMPL_H_