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