OLD | NEW |
(Empty) | |
| 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 #include "components/ntp_tiles/icon_cacher.h" |
| 6 |
| 7 #include "components/favicon/core/favicon_service.h" |
| 8 #include "components/favicon/core/favicon_util.h" |
| 9 #include "components/favicon_base/favicon_types.h" |
| 10 #include "components/favicon_base/favicon_util.h" |
| 11 #include "components/image_fetcher/image_fetcher.h" |
| 12 #include "ui/gfx/image/image.h" |
| 13 #include "url/gurl.h" |
| 14 |
| 15 namespace ntp_tiles { |
| 16 |
| 17 namespace { |
| 18 |
| 19 favicon_base::IconType IconType(const PopularSites::Site& site) { |
| 20 return site.large_icon_url.is_valid() ? favicon_base::TOUCH_ICON |
| 21 : favicon_base::FAVICON; |
| 22 } |
| 23 |
| 24 const GURL& IconURL(const PopularSites::Site& site) { |
| 25 return site.large_icon_url.is_valid() ? site.large_icon_url |
| 26 : site.favicon_url; |
| 27 } |
| 28 |
| 29 } // namespace |
| 30 |
| 31 IconCacher::IconCacher( |
| 32 favicon::FaviconService* favicon_service, |
| 33 std::unique_ptr<image_fetcher::ImageFetcher> image_fetcher) |
| 34 : favicon_service_(favicon_service), |
| 35 image_fetcher_(std::move(image_fetcher)) { |
| 36 image_fetcher_->SetDataUseServiceName( |
| 37 data_use_measurement::DataUseUserData::NTP_TILES); |
| 38 } |
| 39 |
| 40 IconCacher::~IconCacher() = default; |
| 41 |
| 42 void IconCacher::StartFetch(PopularSites::Site site, |
| 43 const base::Callback<void(bool)>& done) { |
| 44 favicon::GetFaviconImageForPageURL( |
| 45 favicon_service_, site.url, IconType(site), |
| 46 base::Bind(&IconCacher::OnGetFaviconImageForPageURLFinished, |
| 47 base::Unretained(this), std::move(site), done), |
| 48 &tracker_); |
| 49 } |
| 50 |
| 51 void IconCacher::OnGetFaviconImageForPageURLFinished( |
| 52 PopularSites::Site site, |
| 53 const base::Callback<void(bool)>& done, |
| 54 const favicon_base::FaviconImageResult& result) { |
| 55 if (!result.image.IsEmpty()) { |
| 56 done.Run(false); |
| 57 return; |
| 58 } |
| 59 |
| 60 image_fetcher_->StartOrQueueNetworkRequest( |
| 61 std::string(), IconURL(site), |
| 62 base::Bind(&IconCacher::OnFaviconDownloaded, base::Unretained(this), site, |
| 63 done)); |
| 64 } |
| 65 |
| 66 void IconCacher::OnFaviconDownloaded(PopularSites::Site site, |
| 67 const base::Callback<void(bool)>& done, |
| 68 const std::string& id, |
| 69 const gfx::Image& fetched_image) { |
| 70 if (fetched_image.IsEmpty()) { |
| 71 done.Run(false); |
| 72 return; |
| 73 } |
| 74 |
| 75 gfx::Image image = fetched_image; |
| 76 favicon_base::SetFaviconColorSpace(&image); |
| 77 favicon_service_->SetFavicons(site.url, IconURL(site), IconType(site), image); |
| 78 done.Run(true); |
| 79 } |
| 80 |
| 81 } // namespace ntp_tiles |
OLD | NEW |