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 "components/favicon/core/favicon_service.h" | 10 #include "components/favicon/core/favicon_service.h" |
11 #include "components/favicon/core/favicon_util.h" | 11 #include "components/favicon/core/favicon_util.h" |
| 12 #include "components/favicon/core/large_icon_service.h" |
| 13 #include "components/favicon_base/fallback_icon_style.h" |
12 #include "components/favicon_base/favicon_types.h" | 14 #include "components/favicon_base/favicon_types.h" |
13 #include "components/favicon_base/favicon_util.h" | 15 #include "components/favicon_base/favicon_util.h" |
14 #include "components/image_fetcher/core/image_decoder.h" | 16 #include "components/image_fetcher/core/image_decoder.h" |
15 #include "components/image_fetcher/core/image_fetcher.h" | 17 #include "components/image_fetcher/core/image_fetcher.h" |
16 #include "ui/base/resource/resource_bundle.h" | 18 #include "ui/base/resource/resource_bundle.h" |
17 #include "ui/gfx/geometry/size.h" | 19 #include "ui/gfx/geometry/size.h" |
18 #include "ui/gfx/image/image.h" | 20 #include "ui/gfx/image/image.h" |
19 #include "url/gurl.h" | 21 #include "url/gurl.h" |
20 | 22 |
21 namespace ntp_tiles { | 23 namespace ntp_tiles { |
22 | 24 |
23 namespace { | 25 namespace { |
24 | 26 |
25 constexpr int kDesiredFrameSize = 128; | 27 constexpr int kDesiredFrameSize = 128; |
26 | 28 |
| 29 // 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 // See crbug.com/696563. |
| 32 constexpr int kTileIconMinSizePx = 48; |
| 33 constexpr int kTileIconDesiredSizePx = 96; |
| 34 |
27 favicon_base::IconType IconType(const PopularSites::Site& site) { | 35 favicon_base::IconType IconType(const PopularSites::Site& site) { |
28 return site.large_icon_url.is_valid() ? favicon_base::TOUCH_ICON | 36 return site.large_icon_url.is_valid() ? favicon_base::TOUCH_ICON |
29 : favicon_base::FAVICON; | 37 : favicon_base::FAVICON; |
30 } | 38 } |
31 | 39 |
32 const GURL& IconURL(const PopularSites::Site& site) { | 40 const GURL& IconURL(const PopularSites::Site& site) { |
33 return site.large_icon_url.is_valid() ? site.large_icon_url | 41 return site.large_icon_url.is_valid() ? site.large_icon_url |
34 : site.favicon_url; | 42 : site.favicon_url; |
35 } | 43 } |
36 | 44 |
37 } // namespace | 45 } // namespace |
38 | 46 |
39 IconCacherImpl::IconCacherImpl( | 47 IconCacherImpl::IconCacherImpl( |
40 favicon::FaviconService* favicon_service, | 48 favicon::FaviconService* favicon_service, |
| 49 favicon::LargeIconService* large_icon_service, |
41 std::unique_ptr<image_fetcher::ImageFetcher> image_fetcher) | 50 std::unique_ptr<image_fetcher::ImageFetcher> image_fetcher) |
42 : favicon_service_(favicon_service), | 51 : favicon_service_(favicon_service), |
| 52 large_icon_service_(large_icon_service), |
43 image_fetcher_(std::move(image_fetcher)), | 53 image_fetcher_(std::move(image_fetcher)), |
44 weak_ptr_factory_(this) { | 54 weak_ptr_factory_(this) { |
45 image_fetcher_->SetDataUseServiceName( | 55 image_fetcher_->SetDataUseServiceName( |
46 data_use_measurement::DataUseUserData::NTP_TILES); | 56 data_use_measurement::DataUseUserData::NTP_TILES); |
47 // For images with multiple frames, prefer one of size 128x128px. | 57 // For images with multiple frames, prefer one of size 128x128px. |
48 image_fetcher_->SetDesiredImageFrameSize( | 58 image_fetcher_->SetDesiredImageFrameSize( |
49 gfx::Size(kDesiredFrameSize, kDesiredFrameSize)); | 59 gfx::Size(kDesiredFrameSize, kDesiredFrameSize)); |
50 } | 60 } |
51 | 61 |
52 IconCacherImpl::~IconCacherImpl() = default; | 62 IconCacherImpl::~IconCacherImpl() = default; |
53 | 63 |
54 void IconCacherImpl::StartFetch( | 64 void IconCacherImpl::StartFetchPopularSites( |
55 PopularSites::Site site, | 65 PopularSites::Site site, |
56 const base::Closure& icon_available, | 66 const base::Closure& icon_available, |
57 const base::Closure& preliminary_icon_available) { | 67 const base::Closure& preliminary_icon_available) { |
58 // Copy values from |site| before it is moved. | 68 // Copy values from |site| before it is moved. |
59 GURL site_url = site.url; | 69 GURL site_url = site.url; |
60 favicon_base::IconType icon_type = IconType(site); | 70 favicon_base::IconType icon_type = IconType(site); |
61 favicon::GetFaviconImageForPageURL( | 71 favicon::GetFaviconImageForPageURL( |
62 favicon_service_, site_url, icon_type, | 72 favicon_service_, site_url, icon_type, |
63 base::Bind(&IconCacherImpl::OnGetFaviconImageForPageURLFinished, | 73 base::Bind(&IconCacherImpl::OnGetFaviconImageForPageURLFinished, |
64 base::Unretained(this), std::move(site), icon_available, | 74 base::Unretained(this), std::move(site), icon_available, |
65 preliminary_icon_available), | 75 preliminary_icon_available), |
66 &tracker_); | 76 &tracker_); |
67 } | 77 } |
68 | 78 |
69 void IconCacherImpl::OnGetFaviconImageForPageURLFinished( | 79 void IconCacherImpl::OnGetFaviconImageForPageURLFinished( |
70 PopularSites::Site site, | 80 PopularSites::Site site, |
71 const base::Closure& icon_available, | 81 const base::Closure& icon_available, |
72 const base::Closure& preliminary_icon_available, | 82 const base::Closure& preliminary_icon_available, |
73 const favicon_base::FaviconImageResult& result) { | 83 const favicon_base::FaviconImageResult& result) { |
74 if (!result.image.IsEmpty()) { | 84 if (!result.image.IsEmpty()) { |
75 return; | 85 return; |
76 } | 86 } |
77 | 87 |
78 std::unique_ptr<CancelableImageCallback> preliminary_callback = | 88 std::unique_ptr<CancelableImageCallback> preliminary_callback = |
79 MaybeProvideDefaultIcon(site, preliminary_icon_available); | 89 MaybeProvideDefaultIcon(site, preliminary_icon_available); |
80 | 90 |
81 image_fetcher_->StartOrQueueNetworkRequest( | 91 image_fetcher_->StartOrQueueNetworkRequest( |
82 std::string(), IconURL(site), | 92 std::string(), IconURL(site), |
83 base::Bind(&IconCacherImpl::OnFaviconDownloaded, base::Unretained(this), | 93 base::Bind(&IconCacherImpl::OnPopularSitesFaviconDownloaded, |
84 site, base::Passed(std::move(preliminary_callback)), | 94 base::Unretained(this), site, |
| 95 base::Passed(std::move(preliminary_callback)), |
85 icon_available)); | 96 icon_available)); |
86 } | 97 } |
87 | 98 |
88 void IconCacherImpl::OnFaviconDownloaded( | 99 void IconCacherImpl::OnPopularSitesFaviconDownloaded( |
89 PopularSites::Site site, | 100 PopularSites::Site site, |
90 std::unique_ptr<CancelableImageCallback> preliminary_callback, | 101 std::unique_ptr<CancelableImageCallback> preliminary_callback, |
91 const base::Closure& icon_available, | 102 const base::Closure& icon_available, |
92 const std::string& id, | 103 const std::string& id, |
93 const gfx::Image& fetched_image, | 104 const gfx::Image& fetched_image, |
94 const image_fetcher::RequestMetadata& metadata) { | 105 const image_fetcher::RequestMetadata& metadata) { |
95 if (fetched_image.IsEmpty()) { | 106 if (fetched_image.IsEmpty()) { |
96 return; | 107 return; |
97 } | 108 } |
98 | 109 |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
133 weak_ptr_factory_.GetWeakPtr(), site, icon_available))); | 144 weak_ptr_factory_.GetWeakPtr(), site, icon_available))); |
134 image_fetcher_->GetImageDecoder()->DecodeImage( | 145 image_fetcher_->GetImageDecoder()->DecodeImage( |
135 ResourceBundle::GetSharedInstance() | 146 ResourceBundle::GetSharedInstance() |
136 .GetRawDataResource(site.default_icon_resource) | 147 .GetRawDataResource(site.default_icon_resource) |
137 .as_string(), | 148 .as_string(), |
138 gfx::Size(kDesiredFrameSize, kDesiredFrameSize), | 149 gfx::Size(kDesiredFrameSize, kDesiredFrameSize), |
139 preliminary_callback->callback()); | 150 preliminary_callback->callback()); |
140 return preliminary_callback; | 151 return preliminary_callback; |
141 } | 152 } |
142 | 153 |
| 154 void IconCacherImpl::StartFetchMostLikely(const GURL& page_url, |
| 155 const base::Closure& icon_available) { |
| 156 // Desired size 0 means that we do not want the service to resize the image |
| 157 // (as we will not use it anyway). |
| 158 large_icon_service_->GetLargeIconOrFallbackStyle( |
| 159 page_url, kTileIconMinSizePx, /*desired_size_in_pixel=*/0, |
| 160 base::Bind(&IconCacherImpl::OnGetLargeIconOrFallbackStyleFinished, |
| 161 weak_ptr_factory_.GetWeakPtr(), page_url, icon_available), |
| 162 &tracker_); |
| 163 } |
| 164 |
| 165 void IconCacherImpl::OnGetLargeIconOrFallbackStyleFinished( |
| 166 const GURL& page_url, |
| 167 const base::Closure& icon_available, |
| 168 const favicon_base::LargeIconResult& result) { |
| 169 if (!result.is_empty()) { |
| 170 // We should not fetch if the cache already contains a favicon of any size |
| 171 // and type. |
| 172 return; |
| 173 } |
| 174 |
| 175 large_icon_service_ |
| 176 ->GetLargeIconOrFallbackStyleFromGoogleServerSkippingLocalCache( |
| 177 page_url, kTileIconMinSizePx, kTileIconDesiredSizePx, |
| 178 base::Bind(&IconCacherImpl::OnMostLikelyFaviconDownloaded, |
| 179 weak_ptr_factory_.GetWeakPtr(), icon_available)); |
| 180 } |
| 181 |
| 182 void IconCacherImpl::OnMostLikelyFaviconDownloaded( |
| 183 const base::Closure& icon_available, |
| 184 bool success) { |
| 185 if (!success) { |
| 186 return; |
| 187 } |
| 188 if (icon_available) { |
| 189 icon_available.Run(); |
| 190 } |
| 191 } |
| 192 |
143 } // namespace ntp_tiles | 193 } // namespace ntp_tiles |
OLD | NEW |