mastiz | 73693f1 | 2017-01-27 15:52:57 | [diff] [blame] | 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 | |
| 5 | #ifndef COMPONENTS_NTP_TILES_ICON_CACHER_IMPL_H_ |
| 6 | #define COMPONENTS_NTP_TILES_ICON_CACHER_IMPL_H_ |
| 7 | |
| 8 | #include <memory> |
jkrcal | fc015b3 | 2017-05-18 17:43:21 | [diff] [blame] | 9 | #include <set> |
mastiz | 73693f1 | 2017-01-27 15:52:57 | [diff] [blame] | 10 | #include <string> |
jkrcal | fc015b3 | 2017-05-18 17:43:21 | [diff] [blame] | 11 | #include <vector> |
mastiz | 73693f1 | 2017-01-27 15:52:57 | [diff] [blame] | 12 | |
| 13 | #include "base/callback.h" |
fhorschig | 85e2d0b9 | 2017-03-23 11:16:36 | [diff] [blame] | 14 | #include "base/cancelable_callback.h" |
mastiz | 73693f1 | 2017-01-27 15:52:57 | [diff] [blame] | 15 | #include "base/memory/weak_ptr.h" |
| 16 | #include "base/task/cancelable_task_tracker.h" |
| 17 | #include "components/ntp_tiles/icon_cacher.h" |
| 18 | #include "components/ntp_tiles/popular_sites.h" |
| 19 | |
| 20 | namespace favicon { |
| 21 | class FaviconService; |
jkrcal | 43f24f8 | 2017-05-12 18:01:52 | [diff] [blame] | 22 | class LargeIconService; |
mastiz | 73693f1 | 2017-01-27 15:52:57 | [diff] [blame] | 23 | } // namespace favicon |
| 24 | |
| 25 | namespace favicon_base { |
| 26 | struct FaviconImageResult; |
jkrcal | 43f24f8 | 2017-05-12 18:01:52 | [diff] [blame] | 27 | struct LargeIconResult; |
Jan Krcal | f3e5733e | 2017-07-10 09:06:11 | [diff] [blame] | 28 | enum class GoogleFaviconServerRequestStatus; |
mastiz | 73693f1 | 2017-01-27 15:52:57 | [diff] [blame] | 29 | } // namespace favicon_base |
| 30 | |
| 31 | namespace gfx { |
| 32 | class Image; |
| 33 | } // namespace gfx |
| 34 | |
| 35 | namespace image_fetcher { |
| 36 | class ImageFetcher; |
treib | 0a6cfc6 | 2017-03-20 13:10:30 | [diff] [blame] | 37 | struct RequestMetadata; |
mastiz | 73693f1 | 2017-01-27 15:52:57 | [diff] [blame] | 38 | } // namespace image_fetcher |
| 39 | |
| 40 | namespace ntp_tiles { |
| 41 | |
| 42 | class IconCacherImpl : public IconCacher { |
| 43 | public: |
jkrcal | 43f24f8 | 2017-05-12 18:01:52 | [diff] [blame] | 44 | // TODO(jkrcal): Make this eventually use only LargeIconService. |
| 45 | // crbug.com/696563 |
mastiz | 73693f1 | 2017-01-27 15:52:57 | [diff] [blame] | 46 | IconCacherImpl(favicon::FaviconService* favicon_service, |
jkrcal | 43f24f8 | 2017-05-12 18:01:52 | [diff] [blame] | 47 | favicon::LargeIconService* large_icon_service, |
mastiz | 73693f1 | 2017-01-27 15:52:57 | [diff] [blame] | 48 | std::unique_ptr<image_fetcher::ImageFetcher> image_fetcher); |
| 49 | ~IconCacherImpl() override; |
| 50 | |
jkrcal | 43f24f8 | 2017-05-12 18:01:52 | [diff] [blame] | 51 | void StartFetchPopularSites( |
| 52 | PopularSites::Site site, |
| 53 | const base::Closure& icon_available, |
| 54 | const base::Closure& preliminary_icon_available) override; |
| 55 | void StartFetchMostLikely(const GURL& page_url, |
| 56 | const base::Closure& icon_available) override; |
mastiz | 73693f1 | 2017-01-27 15:52:57 | [diff] [blame] | 57 | |
| 58 | private: |
fhorschig | 85e2d0b9 | 2017-03-23 11:16:36 | [diff] [blame] | 59 | using CancelableImageCallback = |
| 60 | base::CancelableCallback<void(const gfx::Image&)>; |
| 61 | |
mastiz | 73693f1 | 2017-01-27 15:52:57 | [diff] [blame] | 62 | void OnGetFaviconImageForPageURLFinished( |
| 63 | PopularSites::Site site, |
fhorschig | fed34be | 2017-03-02 23:16:09 | [diff] [blame] | 64 | const base::Closure& preliminary_icon_available, |
mastiz | 73693f1 | 2017-01-27 15:52:57 | [diff] [blame] | 65 | const favicon_base::FaviconImageResult& result); |
| 66 | |
jkrcal | 43f24f8 | 2017-05-12 18:01:52 | [diff] [blame] | 67 | void OnPopularSitesFaviconDownloaded( |
fhorschig | 85e2d0b9 | 2017-03-23 11:16:36 | [diff] [blame] | 68 | PopularSites::Site site, |
| 69 | std::unique_ptr<CancelableImageCallback> preliminary_callback, |
fhorschig | 85e2d0b9 | 2017-03-23 11:16:36 | [diff] [blame] | 70 | const std::string& id, |
| 71 | const gfx::Image& fetched_image, |
| 72 | const image_fetcher::RequestMetadata& metadata); |
mastiz | 73693f1 | 2017-01-27 15:52:57 | [diff] [blame] | 73 | |
fhorschig | 85e2d0b9 | 2017-03-23 11:16:36 | [diff] [blame] | 74 | std::unique_ptr<CancelableImageCallback> MaybeProvideDefaultIcon( |
| 75 | const PopularSites::Site& site, |
jkrcal | fc015b3 | 2017-05-18 17:43:21 | [diff] [blame] | 76 | const base::Closure& preliminary_icon_available); |
| 77 | void SaveAndNotifyDefaultIconForSite( |
| 78 | const PopularSites::Site& site, |
| 79 | const base::Closure& preliminary_icon_available, |
| 80 | const gfx::Image& image); |
| 81 | void SaveIconForSite(const PopularSites::Site& site, const gfx::Image& image); |
fhorschig | fed34be | 2017-03-02 23:16:09 | [diff] [blame] | 82 | |
jkrcal | 43f24f8 | 2017-05-12 18:01:52 | [diff] [blame] | 83 | void OnGetLargeIconOrFallbackStyleFinished( |
| 84 | const GURL& page_url, |
jkrcal | 43f24f8 | 2017-05-12 18:01:52 | [diff] [blame] | 85 | const favicon_base::LargeIconResult& result); |
| 86 | |
Jan Krcal | f3e5733e | 2017-07-10 09:06:11 | [diff] [blame] | 87 | void OnMostLikelyFaviconDownloaded( |
| 88 | const GURL& request_url, |
| 89 | favicon_base::GoogleFaviconServerRequestStatus status); |
jkrcal | 00addb83 | 2017-05-23 16:54:04 | [diff] [blame] | 90 | |
jkrcal | fc015b3 | 2017-05-18 17:43:21 | [diff] [blame] | 91 | bool StartRequest(const GURL& request_url, |
| 92 | const base::Closure& icon_available); |
| 93 | void FinishRequestAndNotifyIconAvailable(const GURL& request_url, |
| 94 | bool newly_available); |
jkrcal | 43f24f8 | 2017-05-12 18:01:52 | [diff] [blame] | 95 | |
mastiz | 73693f1 | 2017-01-27 15:52:57 | [diff] [blame] | 96 | base::CancelableTaskTracker tracker_; |
| 97 | favicon::FaviconService* const favicon_service_; |
jkrcal | 43f24f8 | 2017-05-12 18:01:52 | [diff] [blame] | 98 | favicon::LargeIconService* const large_icon_service_; |
mastiz | 73693f1 | 2017-01-27 15:52:57 | [diff] [blame] | 99 | std::unique_ptr<image_fetcher::ImageFetcher> const image_fetcher_; |
jkrcal | fc015b3 | 2017-05-18 17:43:21 | [diff] [blame] | 100 | std::map<GURL, std::vector<base::Closure>> in_flight_requests_; |
mastiz | 73693f1 | 2017-01-27 15:52:57 | [diff] [blame] | 101 | |
fhorschig | 85e2d0b9 | 2017-03-23 11:16:36 | [diff] [blame] | 102 | base::WeakPtrFactory<IconCacherImpl> weak_ptr_factory_; |
| 103 | |
mastiz | 73693f1 | 2017-01-27 15:52:57 | [diff] [blame] | 104 | DISALLOW_COPY_AND_ASSIGN(IconCacherImpl); |
| 105 | }; |
| 106 | |
| 107 | } // namespace ntp_tiles |
| 108 | |
| 109 | #endif // COMPONENTS_NTP_TILES_ICON_CACHER_IMPL_H_ |