Chromium Code Reviews| 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 "base/memory/ptr_util.h" | 9 #include "base/memory/ptr_util.h" |
| 10 #include "base/metrics/histogram_macros.h" | |
| 10 #include "components/favicon/core/favicon_service.h" | 11 #include "components/favicon/core/favicon_service.h" |
| 11 #include "components/favicon/core/favicon_util.h" | 12 #include "components/favicon/core/favicon_util.h" |
| 12 #include "components/favicon/core/large_icon_service.h" | 13 #include "components/favicon/core/large_icon_service.h" |
| 13 #include "components/favicon_base/fallback_icon_style.h" | 14 #include "components/favicon_base/fallback_icon_style.h" |
| 14 #include "components/favicon_base/favicon_types.h" | 15 #include "components/favicon_base/favicon_types.h" |
| 15 #include "components/favicon_base/favicon_util.h" | 16 #include "components/favicon_base/favicon_util.h" |
| 16 #include "components/image_fetcher/core/image_decoder.h" | 17 #include "components/image_fetcher/core/image_decoder.h" |
| 17 #include "components/image_fetcher/core/image_fetcher.h" | 18 #include "components/image_fetcher/core/image_fetcher.h" |
| 18 #include "ui/base/resource/resource_bundle.h" | 19 #include "ui/base/resource/resource_bundle.h" |
| 19 #include "ui/gfx/geometry/size.h" | 20 #include "ui/gfx/geometry/size.h" |
| 20 #include "ui/gfx/image/image.h" | 21 #include "ui/gfx/image/image.h" |
| 21 #include "url/gurl.h" | 22 #include "url/gurl.h" |
| 22 | 23 |
| 23 namespace ntp_tiles { | 24 namespace ntp_tiles { |
| 24 | 25 |
| 25 namespace { | 26 namespace { |
| 26 | 27 |
| 27 constexpr int kDesiredFrameSize = 128; | 28 constexpr int kDesiredFrameSize = 128; |
| 28 | 29 |
| 29 // TODO(jkrcal): Make the size in dip and the scale factor be passed as | 30 // TODO(jkrcal): Make the size in dip and the scale factor be passed as |
| 30 // arguments from the UI so that we desire for the right size on a given device. | 31 // arguments from the UI so that we desire for the right size on a given device. |
| 31 // See crbug.com/696563. | 32 // See crbug.com/696563. |
| 32 constexpr int kTileIconMinSizePx = 48; | 33 constexpr int kTileIconMinSizePx = 48; |
| 33 constexpr int kTileIconDesiredSizePx = 96; | 34 constexpr int kTileIconDesiredSizePx = 96; |
| 34 | 35 |
| 36 // Enumeration listing all possible outcomes for fetch attempts of favicons for | |
| 37 // NTP tiles. Used for UMA histograms, so do not change existing | |
| 38 // values. Insert new values at the end, and update the histogram definition. | |
| 39 enum class TileFaviconFetchResult { SUCCESS = 0, FAILURE = 1, COUNT = 2 }; | |
| 40 | |
| 41 void RecordPopularSitesFaviconFetchResult(TileFaviconFetchResult result) { | |
| 42 UMA_HISTOGRAM_ENUMERATION("NewTabPage.TileFaviconFetched.popular", result, | |
|
rkaplow
2017/05/19 20:19:11
instead of using an enum, can you use a boolean hi
jkrcal
2017/05/22 07:59:42
Done.
| |
| 43 TileFaviconFetchResult::COUNT); | |
| 44 } | |
| 45 | |
| 46 void RecordMostLikelyFaviconFetchResult(TileFaviconFetchResult result) { | |
| 47 UMA_HISTOGRAM_ENUMERATION("NewTabPage.TileFaviconFetched.server", result, | |
| 48 TileFaviconFetchResult::COUNT); | |
| 49 } | |
| 50 | |
| 35 favicon_base::IconType IconType(const PopularSites::Site& site) { | 51 favicon_base::IconType IconType(const PopularSites::Site& site) { |
| 36 return site.large_icon_url.is_valid() ? favicon_base::TOUCH_ICON | 52 return site.large_icon_url.is_valid() ? favicon_base::TOUCH_ICON |
| 37 : favicon_base::FAVICON; | 53 : favicon_base::FAVICON; |
| 38 } | 54 } |
| 39 | 55 |
| 40 const GURL& IconURL(const PopularSites::Site& site) { | 56 const GURL& IconURL(const PopularSites::Site& site) { |
| 41 return site.large_icon_url.is_valid() ? site.large_icon_url | 57 return site.large_icon_url.is_valid() ? site.large_icon_url |
| 42 : site.favicon_url; | 58 : site.favicon_url; |
| 43 } | 59 } |
| 44 | 60 |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 108 } | 124 } |
| 109 | 125 |
| 110 void IconCacherImpl::OnPopularSitesFaviconDownloaded( | 126 void IconCacherImpl::OnPopularSitesFaviconDownloaded( |
| 111 PopularSites::Site site, | 127 PopularSites::Site site, |
| 112 std::unique_ptr<CancelableImageCallback> preliminary_callback, | 128 std::unique_ptr<CancelableImageCallback> preliminary_callback, |
| 113 const std::string& id, | 129 const std::string& id, |
| 114 const gfx::Image& fetched_image, | 130 const gfx::Image& fetched_image, |
| 115 const image_fetcher::RequestMetadata& metadata) { | 131 const image_fetcher::RequestMetadata& metadata) { |
| 116 if (fetched_image.IsEmpty()) { | 132 if (fetched_image.IsEmpty()) { |
| 117 FinishRequestAndNotifyIconAvailable(site.url, /*newly_available=*/false); | 133 FinishRequestAndNotifyIconAvailable(site.url, /*newly_available=*/false); |
| 134 RecordPopularSitesFaviconFetchResult(TileFaviconFetchResult::FAILURE); | |
| 118 return; | 135 return; |
| 119 } | 136 } |
| 120 | 137 |
| 121 // Avoid invoking callback about preliminary icon to be triggered. The best | 138 // Avoid invoking callback about preliminary icon to be triggered. The best |
| 122 // possible icon has already been downloaded. | 139 // possible icon has already been downloaded. |
| 123 if (preliminary_callback) { | 140 if (preliminary_callback) { |
| 124 preliminary_callback->Cancel(); | 141 preliminary_callback->Cancel(); |
| 125 } | 142 } |
| 126 SaveIconForSite(site, fetched_image); | 143 SaveIconForSite(site, fetched_image); |
| 127 FinishRequestAndNotifyIconAvailable(site.url, /*newly_available=*/true); | 144 FinishRequestAndNotifyIconAvailable(site.url, /*newly_available=*/true); |
| 145 RecordPopularSitesFaviconFetchResult(TileFaviconFetchResult::SUCCESS); | |
| 128 } | 146 } |
| 129 | 147 |
| 130 void IconCacherImpl::SaveAndNotifyDefaultIconForSite( | 148 void IconCacherImpl::SaveAndNotifyDefaultIconForSite( |
| 131 const PopularSites::Site& site, | 149 const PopularSites::Site& site, |
| 132 const base::Closure& preliminary_icon_available, | 150 const base::Closure& preliminary_icon_available, |
| 133 const gfx::Image& image) { | 151 const gfx::Image& image) { |
| 134 SaveIconForSite(site, image); | 152 SaveIconForSite(site, image); |
| 135 if (preliminary_icon_available) { | 153 if (preliminary_icon_available) { |
| 136 preliminary_icon_available.Run(); | 154 preliminary_icon_available.Run(); |
| 137 } | 155 } |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 189 if (!HasResultDefaultBackgroundColor(result)) { | 207 if (!HasResultDefaultBackgroundColor(result)) { |
| 190 // We should only fetch for default "gray" tiles so that we never overrite | 208 // We should only fetch for default "gray" tiles so that we never overrite |
| 191 // any favicon of any size. | 209 // any favicon of any size. |
| 192 FinishRequestAndNotifyIconAvailable(page_url, /*newly_available=*/false); | 210 FinishRequestAndNotifyIconAvailable(page_url, /*newly_available=*/false); |
| 193 return; | 211 return; |
| 194 } | 212 } |
| 195 | 213 |
| 196 large_icon_service_ | 214 large_icon_service_ |
| 197 ->GetLargeIconOrFallbackStyleFromGoogleServerSkippingLocalCache( | 215 ->GetLargeIconOrFallbackStyleFromGoogleServerSkippingLocalCache( |
| 198 page_url, kTileIconMinSizePx, kTileIconDesiredSizePx, | 216 page_url, kTileIconMinSizePx, kTileIconDesiredSizePx, |
| 199 base::Bind(&IconCacherImpl::FinishRequestAndNotifyIconAvailable, | 217 base::Bind(&IconCacherImpl::OnMostLikelyFaviconDownloaded, |
| 200 weak_ptr_factory_.GetWeakPtr(), page_url)); | 218 weak_ptr_factory_.GetWeakPtr(), page_url)); |
| 201 } | 219 } |
| 202 | 220 |
| 221 void IconCacherImpl::OnMostLikelyFaviconDownloaded(const GURL& request_url, | |
| 222 bool success) { | |
| 223 RecordMostLikelyFaviconFetchResult(success ? TileFaviconFetchResult::SUCCESS | |
| 224 : TileFaviconFetchResult::FAILURE); | |
| 225 FinishRequestAndNotifyIconAvailable(request_url, success); | |
| 226 } | |
| 227 | |
| 203 bool IconCacherImpl::StartRequest(const GURL& request_url, | 228 bool IconCacherImpl::StartRequest(const GURL& request_url, |
| 204 const base::Closure& icon_available) { | 229 const base::Closure& icon_available) { |
| 205 bool in_flight = in_flight_requests_.count(request_url) > 0; | 230 bool in_flight = in_flight_requests_.count(request_url) > 0; |
| 206 in_flight_requests_[request_url].push_back(icon_available); | 231 in_flight_requests_[request_url].push_back(icon_available); |
| 207 return !in_flight; | 232 return !in_flight; |
| 208 } | 233 } |
| 209 | 234 |
| 210 void IconCacherImpl::FinishRequestAndNotifyIconAvailable( | 235 void IconCacherImpl::FinishRequestAndNotifyIconAvailable( |
| 211 const GURL& request_url, | 236 const GURL& request_url, |
| 212 bool newly_available) { | 237 bool newly_available) { |
| 213 std::vector<base::Closure> callbacks = | 238 std::vector<base::Closure> callbacks = |
| 214 std::move(in_flight_requests_[request_url]); | 239 std::move(in_flight_requests_[request_url]); |
| 215 in_flight_requests_.erase(request_url); | 240 in_flight_requests_.erase(request_url); |
| 216 if (!newly_available) { | 241 if (!newly_available) { |
| 217 return; | 242 return; |
| 218 } | 243 } |
| 219 for (const base::Closure& callback : callbacks) { | 244 for (const base::Closure& callback : callbacks) { |
| 220 if (callback) { | 245 if (callback) { |
| 221 callback.Run(); | 246 callback.Run(); |
| 222 } | 247 } |
| 223 } | 248 } |
| 224 } | 249 } |
| 225 | 250 |
| 226 } // namespace ntp_tiles | 251 } // namespace ntp_tiles |
| OLD | NEW |