Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(163)

Side by Side Diff: components/ntp_tiles/icon_cacher_impl.cc

Issue 2866033002: [NTP Tiles] Fetch missing MostLikely tiles from a Google server (Closed)
Patch Set: Comments #3 Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
45 bool HasResultDefaultBackgroundColor(
46 const favicon_base::LargeIconResult& result) {
47 if (!result.fallback_icon_style) {
48 return false;
49 }
50 return result.fallback_icon_style->is_default_background_color;
pkotwicz 2017/05/12 15:40:18 Nit: return result.fallback_icon_style && result.
jkrcal 2017/05/12 17:08:58 Even though the logic is simple, I prefer it separ
51 }
52
37 } // namespace 53 } // namespace
38 54
39 IconCacherImpl::IconCacherImpl( 55 IconCacherImpl::IconCacherImpl(
40 favicon::FaviconService* favicon_service, 56 favicon::FaviconService* favicon_service,
57 favicon::LargeIconService* large_icon_service,
41 std::unique_ptr<image_fetcher::ImageFetcher> image_fetcher) 58 std::unique_ptr<image_fetcher::ImageFetcher> image_fetcher)
42 : favicon_service_(favicon_service), 59 : favicon_service_(favicon_service),
60 large_icon_service_(large_icon_service),
43 image_fetcher_(std::move(image_fetcher)), 61 image_fetcher_(std::move(image_fetcher)),
44 weak_ptr_factory_(this) { 62 weak_ptr_factory_(this) {
45 image_fetcher_->SetDataUseServiceName( 63 image_fetcher_->SetDataUseServiceName(
46 data_use_measurement::DataUseUserData::NTP_TILES); 64 data_use_measurement::DataUseUserData::NTP_TILES);
47 // For images with multiple frames, prefer one of size 128x128px. 65 // For images with multiple frames, prefer one of size 128x128px.
48 image_fetcher_->SetDesiredImageFrameSize( 66 image_fetcher_->SetDesiredImageFrameSize(
49 gfx::Size(kDesiredFrameSize, kDesiredFrameSize)); 67 gfx::Size(kDesiredFrameSize, kDesiredFrameSize));
50 } 68 }
51 69
52 IconCacherImpl::~IconCacherImpl() = default; 70 IconCacherImpl::~IconCacherImpl() = default;
53 71
54 void IconCacherImpl::StartFetch( 72 void IconCacherImpl::StartFetchPopularSites(
55 PopularSites::Site site, 73 PopularSites::Site site,
56 const base::Closure& icon_available, 74 const base::Closure& icon_available,
57 const base::Closure& preliminary_icon_available) { 75 const base::Closure& preliminary_icon_available) {
58 // Copy values from |site| before it is moved. 76 // Copy values from |site| before it is moved.
59 GURL site_url = site.url; 77 GURL site_url = site.url;
60 favicon_base::IconType icon_type = IconType(site); 78 favicon_base::IconType icon_type = IconType(site);
61 favicon::GetFaviconImageForPageURL( 79 favicon::GetFaviconImageForPageURL(
62 favicon_service_, site_url, icon_type, 80 favicon_service_, site_url, icon_type,
63 base::Bind(&IconCacherImpl::OnGetFaviconImageForPageURLFinished, 81 base::Bind(&IconCacherImpl::OnGetFaviconImageForPageURLFinished,
64 base::Unretained(this), std::move(site), icon_available, 82 base::Unretained(this), std::move(site), icon_available,
65 preliminary_icon_available), 83 preliminary_icon_available),
66 &tracker_); 84 &tracker_);
67 } 85 }
68 86
69 void IconCacherImpl::OnGetFaviconImageForPageURLFinished( 87 void IconCacherImpl::OnGetFaviconImageForPageURLFinished(
70 PopularSites::Site site, 88 PopularSites::Site site,
71 const base::Closure& icon_available, 89 const base::Closure& icon_available,
72 const base::Closure& preliminary_icon_available, 90 const base::Closure& preliminary_icon_available,
73 const favicon_base::FaviconImageResult& result) { 91 const favicon_base::FaviconImageResult& result) {
74 if (!result.image.IsEmpty()) { 92 if (!result.image.IsEmpty()) {
75 return; 93 return;
76 } 94 }
77 95
78 std::unique_ptr<CancelableImageCallback> preliminary_callback = 96 std::unique_ptr<CancelableImageCallback> preliminary_callback =
79 MaybeProvideDefaultIcon(site, preliminary_icon_available); 97 MaybeProvideDefaultIcon(site, preliminary_icon_available);
80 98
81 image_fetcher_->StartOrQueueNetworkRequest( 99 image_fetcher_->StartOrQueueNetworkRequest(
82 std::string(), IconURL(site), 100 std::string(), IconURL(site),
83 base::Bind(&IconCacherImpl::OnFaviconDownloaded, base::Unretained(this), 101 base::Bind(&IconCacherImpl::OnPopularSitesFaviconDownloaded,
84 site, base::Passed(std::move(preliminary_callback)), 102 base::Unretained(this), site,
103 base::Passed(std::move(preliminary_callback)),
85 icon_available)); 104 icon_available));
86 } 105 }
87 106
88 void IconCacherImpl::OnFaviconDownloaded( 107 void IconCacherImpl::OnPopularSitesFaviconDownloaded(
89 PopularSites::Site site, 108 PopularSites::Site site,
90 std::unique_ptr<CancelableImageCallback> preliminary_callback, 109 std::unique_ptr<CancelableImageCallback> preliminary_callback,
91 const base::Closure& icon_available, 110 const base::Closure& icon_available,
92 const std::string& id, 111 const std::string& id,
93 const gfx::Image& fetched_image, 112 const gfx::Image& fetched_image,
94 const image_fetcher::RequestMetadata& metadata) { 113 const image_fetcher::RequestMetadata& metadata) {
95 if (fetched_image.IsEmpty()) { 114 if (fetched_image.IsEmpty()) {
96 return; 115 return;
97 } 116 }
98 117
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
133 weak_ptr_factory_.GetWeakPtr(), site, icon_available))); 152 weak_ptr_factory_.GetWeakPtr(), site, icon_available)));
134 image_fetcher_->GetImageDecoder()->DecodeImage( 153 image_fetcher_->GetImageDecoder()->DecodeImage(
135 ResourceBundle::GetSharedInstance() 154 ResourceBundle::GetSharedInstance()
136 .GetRawDataResource(site.default_icon_resource) 155 .GetRawDataResource(site.default_icon_resource)
137 .as_string(), 156 .as_string(),
138 gfx::Size(kDesiredFrameSize, kDesiredFrameSize), 157 gfx::Size(kDesiredFrameSize, kDesiredFrameSize),
139 preliminary_callback->callback()); 158 preliminary_callback->callback());
140 return preliminary_callback; 159 return preliminary_callback;
141 } 160 }
142 161
162 void IconCacherImpl::StartFetchMostLikely(const GURL& page_url,
163 const base::Closure& icon_available) {
164 // Desired size 0 means that we do not want the service to resize the image
165 // (as we will not use it anyway).
166 large_icon_service_->GetLargeIconOrFallbackStyle(
167 page_url, kTileIconMinSizePx, /*desired_size_in_pixel=*/0,
168 base::Bind(&IconCacherImpl::OnGetLargeIconOrFallbackStyleFinished,
169 weak_ptr_factory_.GetWeakPtr(), page_url, icon_available),
170 &tracker_);
171 }
172
173 void IconCacherImpl::OnGetLargeIconOrFallbackStyleFinished(
174 const GURL& page_url,
175 const base::Closure& icon_available,
176 const favicon_base::LargeIconResult& result) {
177 if (!HasResultDefaultBackgroundColor(result)) {
178 // We should only fetch for default "gray" tiles so that we never overrite
179 // any favicon of any size.
180 return;
181 }
182
183 large_icon_service_
184 ->GetLargeIconOrFallbackStyleFromGoogleServerSkippingLocalCache(
185 page_url, kTileIconMinSizePx, kTileIconDesiredSizePx,
186 base::Bind(&IconCacherImpl::OnMostLikelyFaviconDownloaded,
187 weak_ptr_factory_.GetWeakPtr(), icon_available));
188 }
189
190 void IconCacherImpl::OnMostLikelyFaviconDownloaded(
191 const base::Closure& icon_available,
192 bool success) {
193 if (!success) {
194 return;
195 }
196 if (icon_available) {
197 icon_available.Run();
198 }
199 }
200
143 } // namespace ntp_tiles 201 } // namespace ntp_tiles
OLDNEW
« no previous file with comments | « components/ntp_tiles/icon_cacher_impl.h ('k') | components/ntp_tiles/icon_cacher_impl_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698