| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/ntp_tiles/icon_cacher_impl.h" | 5 #include "components/ntp_tiles/icon_cacher_impl.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "components/favicon/core/favicon_service.h" | 9 #include "components/favicon/core/favicon_service.h" |
| 10 #include "components/favicon/core/favicon_util.h" | 10 #include "components/favicon/core/favicon_util.h" |
| 11 #include "components/favicon/core/large_icon_service.h" |
| 12 #include "components/favicon_base/fallback_icon_style.h" |
| 11 #include "components/favicon_base/favicon_types.h" | 13 #include "components/favicon_base/favicon_types.h" |
| 12 #include "components/favicon_base/favicon_util.h" | 14 #include "components/favicon_base/favicon_util.h" |
| 13 #include "components/image_fetcher/image_fetcher.h" | 15 #include "components/image_fetcher/image_fetcher.h" |
| 14 #include "ui/gfx/image/image.h" | 16 #include "ui/gfx/image/image.h" |
| 15 #include "url/gurl.h" | 17 #include "url/gurl.h" |
| 16 | 18 |
| 17 namespace ntp_tiles { | 19 namespace ntp_tiles { |
| 18 | 20 |
| 19 namespace { | 21 namespace { |
| 20 | 22 |
| 21 favicon_base::IconType IconType(const PopularSites::Site& site) { | 23 favicon_base::IconType IconType(const PopularSites::Site& site) { |
| 22 return site.large_icon_url.is_valid() ? favicon_base::TOUCH_ICON | 24 return site.large_icon_url.is_valid() ? favicon_base::TOUCH_ICON |
| 23 : favicon_base::FAVICON; | 25 : favicon_base::FAVICON; |
| 24 } | 26 } |
| 25 | 27 |
| 26 const GURL& IconURL(const PopularSites::Site& site) { | 28 const GURL& IconURL(const PopularSites::Site& site) { |
| 27 return site.large_icon_url.is_valid() ? site.large_icon_url | 29 return site.large_icon_url.is_valid() ? site.large_icon_url |
| 28 : site.favicon_url; | 30 : site.favicon_url; |
| 29 } | 31 } |
| 30 | 32 |
| 33 bool LargeIconResultEmpty(const favicon_base::LargeIconResult& result) { |
| 34 // We should not fetch anything if the cache already contains a favicon of any |
| 35 // size and type. Any icon results either in a bitmap or in a non-default |
| 36 // fallback color. |
| 37 return (!result.bitmap.is_valid() && |
| 38 (!result.fallback_icon_style || |
| 39 result.fallback_icon_style->is_default_background_color)); |
| 40 } |
| 41 |
| 31 } // namespace | 42 } // namespace |
| 32 | 43 |
| 33 IconCacherImpl::IconCacherImpl( | 44 IconCacherImpl::IconCacherImpl( |
| 34 favicon::FaviconService* favicon_service, | 45 favicon::FaviconService* favicon_service, |
| 46 favicon::LargeIconService* large_icon_service, |
| 35 std::unique_ptr<image_fetcher::ImageFetcher> image_fetcher) | 47 std::unique_ptr<image_fetcher::ImageFetcher> image_fetcher) |
| 36 : favicon_service_(favicon_service), | 48 : favicon_service_(favicon_service), |
| 49 large_icon_service_(large_icon_service), |
| 37 image_fetcher_(std::move(image_fetcher)) { | 50 image_fetcher_(std::move(image_fetcher)) { |
| 38 image_fetcher_->SetDataUseServiceName( | 51 image_fetcher_->SetDataUseServiceName( |
| 39 data_use_measurement::DataUseUserData::NTP_TILES); | 52 data_use_measurement::DataUseUserData::NTP_TILES); |
| 40 } | 53 } |
| 41 | 54 |
| 42 IconCacherImpl::~IconCacherImpl() = default; | 55 IconCacherImpl::~IconCacherImpl() = default; |
| 43 | 56 |
| 44 void IconCacherImpl::StartFetch(PopularSites::Site site, | 57 void IconCacherImpl::StartFetchPopularSites( |
| 45 const base::Callback<void(bool)>& done) { | 58 PopularSites::Site site, |
| 59 const base::Callback<void(bool)>& done) { |
| 46 favicon::GetFaviconImageForPageURL( | 60 favicon::GetFaviconImageForPageURL( |
| 47 favicon_service_, site.url, IconType(site), | 61 favicon_service_, site.url, IconType(site), |
| 48 base::Bind(&IconCacherImpl::OnGetFaviconImageForPageURLFinished, | 62 base::Bind(&IconCacherImpl::OnGetFaviconImageForPageURLFinished, |
| 49 base::Unretained(this), std::move(site), done), | 63 base::Unretained(this), std::move(site), done), |
| 50 &tracker_); | 64 &tracker_); |
| 51 } | 65 } |
| 52 | 66 |
| 53 void IconCacherImpl::OnGetFaviconImageForPageURLFinished( | 67 void IconCacherImpl::OnGetFaviconImageForPageURLFinished( |
| 54 PopularSites::Site site, | 68 PopularSites::Site site, |
| 55 const base::Callback<void(bool)>& done, | 69 const base::Callback<void(bool)>& done, |
| 56 const favicon_base::FaviconImageResult& result) { | 70 const favicon_base::FaviconImageResult& result) { |
| 57 if (!result.image.IsEmpty()) { | 71 if (!result.image.IsEmpty()) { |
| 58 done.Run(false); | 72 done.Run(false); |
| 59 return; | 73 return; |
| 60 } | 74 } |
| 61 | 75 |
| 62 image_fetcher_->StartOrQueueNetworkRequest( | 76 image_fetcher_->StartOrQueueNetworkRequest( |
| 63 std::string(), IconURL(site), | 77 std::string(), IconURL(site), |
| 64 base::Bind(&IconCacherImpl::OnFaviconDownloaded, base::Unretained(this), | 78 base::Bind(&IconCacherImpl::OnPopularSitesFaviconDownloaded, |
| 65 site, done)); | 79 base::Unretained(this), site, done)); |
| 66 } | 80 } |
| 67 | 81 |
| 68 void IconCacherImpl::OnFaviconDownloaded(PopularSites::Site site, | 82 void IconCacherImpl::OnPopularSitesFaviconDownloaded( |
| 69 const base::Callback<void(bool)>& done, | 83 PopularSites::Site site, |
| 70 const std::string& id, | 84 const base::Callback<void(bool)>& done, |
| 71 const gfx::Image& fetched_image) { | 85 const std::string& id, |
| 86 const gfx::Image& fetched_image) { |
| 72 if (fetched_image.IsEmpty()) { | 87 if (fetched_image.IsEmpty()) { |
| 73 done.Run(false); | 88 done.Run(false); |
| 74 return; | 89 return; |
| 75 } | 90 } |
| 76 | 91 |
| 77 gfx::Image image = fetched_image; | 92 gfx::Image image = fetched_image; |
| 78 favicon_base::SetFaviconColorSpace(&image); | 93 favicon_base::SetFaviconColorSpace(&image); |
| 79 favicon_service_->SetFavicons(site.url, IconURL(site), IconType(site), image); | 94 favicon_service_->SetFavicons(site.url, IconURL(site), IconType(site), image); |
| 80 done.Run(true); | 95 done.Run(true); |
| 81 } | 96 } |
| 82 | 97 |
| 98 void IconCacherImpl::StartFetchMostLikely( |
| 99 const GURL& page_url, |
| 100 const base::Callback<void(bool)>& done) { |
| 101 large_icon_service_->GetLargeIconOrFallbackStyle( |
| 102 page_url, 48, 0, |
| 103 base::Bind(&IconCacherImpl::OnGetLargeIconOrFallbackStyleFinished, |
| 104 base::Unretained(this), page_url, done), |
| 105 &tracker_); |
| 106 } |
| 107 |
| 108 void IconCacherImpl::OnGetLargeIconOrFallbackStyleFinished( |
| 109 const GURL& page_url, |
| 110 const base::Callback<void(bool)>& done, |
| 111 const favicon_base::LargeIconResult& result) { |
| 112 if (!LargeIconResultEmpty(result)) { |
| 113 DLOG(WARNING) << "large cache HIT for " << page_url.spec(); |
| 114 done.Run(false); |
| 115 return; |
| 116 } |
| 117 |
| 118 DLOG(WARNING) << "large cache MISS for " << page_url.spec(); |
| 119 |
| 120 large_icon_service_ |
| 121 ->GetLargeIconOrFallbackStyleFromGoogleServerSkippingLocalCache( |
| 122 page_url, 48, 0, |
| 123 base::Bind(&IconCacherImpl::OnMostLikelyFaviconDownloaded, |
| 124 base::Unretained(this), page_url, done), |
| 125 &tracker_); |
| 126 } |
| 127 |
| 128 void IconCacherImpl::OnMostLikelyFaviconDownloaded( |
| 129 const GURL& page_url, |
| 130 const base::Callback<void(bool)>& done, |
| 131 const favicon_base::LargeIconResult& result) { |
| 132 if (LargeIconResultEmpty(result)) { |
| 133 DLOG(WARNING) << "large Google MISS for " << page_url.spec(); |
| 134 done.Run(false); |
| 135 return; |
| 136 } |
| 137 |
| 138 DLOG(WARNING) << "large Google HIT for " << page_url.spec(); |
| 139 |
| 140 done.Run(true); |
| 141 } |
| 142 |
| 83 } // namespace ntp_tiles | 143 } // namespace ntp_tiles |
| OLD | NEW |