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

Side by Side Diff: components/favicon/core/large_icon_service.cc

Issue 2721363002: Extend LargeIconService to fetch missing favicons from a Google server (Closed)
Patch Set: Addressed comments. Created 3 years, 9 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 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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/favicon/core/large_icon_service.h" 5 #include "components/favicon/core/large_icon_service.h"
6 6
7 #include <memory> 7 #include <memory>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/location.h" 10 #include "base/location.h"
11 #include "base/logging.h" 11 #include "base/logging.h"
12 #include "base/macros.h" 12 #include "base/macros.h"
13 #include "base/memory/ptr_util.h"
13 #include "base/memory/ref_counted.h" 14 #include "base/memory/ref_counted.h"
15 #include "base/strings/stringprintf.h"
14 #include "base/task_runner.h" 16 #include "base/task_runner.h"
15 #include "base/threading/sequenced_worker_pool.h" 17 #include "base/threading/sequenced_worker_pool.h"
18 #include "base/threading/thread_task_runner_handle.h"
19 #include "components/data_use_measurement/core/data_use_user_data.h"
16 #include "components/favicon/core/favicon_service.h" 20 #include "components/favicon/core/favicon_service.h"
17 #include "components/favicon_base/fallback_icon_style.h" 21 #include "components/favicon_base/fallback_icon_style.h"
18 #include "components/favicon_base/favicon_types.h" 22 #include "components/favicon_base/favicon_types.h"
23 #include "components/favicon_base/favicon_util.h"
24 #include "components/image_fetcher/image_fetcher.h"
19 #include "skia/ext/image_operations.h" 25 #include "skia/ext/image_operations.h"
20 #include "ui/gfx/codec/png_codec.h" 26 #include "ui/gfx/codec/png_codec.h"
21 #include "ui/gfx/geometry/size.h" 27 #include "ui/gfx/geometry/size.h"
22 28
29 namespace favicon {
23 namespace { 30 namespace {
24 31
32 // Type APPLE_TOUCH prefers TOUCH_ICON and TOUCH_PRECOMPOSED_ICON. If neither is
33 // available, FAVICON is used as fallback.
34 // TODO(crbug.com/698671): Reconsider of type=APPLE_TOUCH should be specific to
35 // iOS, and if so expose a method like PreferTouchIcon() in FaviconService.
36 const char kGoogleServerV2RequestFormat[] =
37 "https://t0.gstatic.com/"
38 "faviconV2?user=chrome&url=%s&type=APPLE_TOUCH&size=%d&min_size=%d"
39 "&max_size=%d";
40 const int kGoogleServerV2MaxSizeInPixel = 256;
41 const int kGoogleServerV2DesiredSizeInPixel = 192;
42
43 GURL GetIconUrlForGoogleServerV2(const GURL& page_url,
44 int min_source_size_in_pixel) {
45 return GURL(base::StringPrintf(
46 kGoogleServerV2RequestFormat, page_url.spec().c_str(),
47 kGoogleServerV2DesiredSizeInPixel, min_source_size_in_pixel,
48 kGoogleServerV2MaxSizeInPixel));
49 }
50
25 // Processes the bitmap data returned from the FaviconService as part of a 51 // Processes the bitmap data returned from the FaviconService as part of a
26 // LargeIconService request. 52 // LargeIconService request.
27 class LargeIconWorker : public base::RefCountedThreadSafe<LargeIconWorker> { 53 class LargeIconWorker : public base::RefCountedThreadSafe<LargeIconWorker> {
28 public: 54 public:
29 LargeIconWorker(int min_source_size_in_pixel, 55 LargeIconWorker(int min_source_size_in_pixel,
30 int desired_size_in_pixel, 56 int desired_size_in_pixel,
31 favicon_base::LargeIconCallback callback, 57 favicon_base::LargeIconCallback callback,
32 scoped_refptr<base::TaskRunner> background_task_runner, 58 scoped_refptr<base::TaskRunner> background_task_runner,
33 base::CancelableTaskTracker* tracker); 59 base::CancelableTaskTracker* tracker);
34 60
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after
153 gfx::Size(desired_size_in_pixel_, desired_size_in_pixel_); 179 gfx::Size(desired_size_in_pixel_, desired_size_in_pixel_);
154 resized_bitmap_result->bitmap_data = 180 resized_bitmap_result->bitmap_data =
155 base::RefCountedBytes::TakeVector(&bitmap_data); 181 base::RefCountedBytes::TakeVector(&bitmap_data);
156 return true; 182 return true;
157 } 183 }
158 184
159 void LargeIconWorker::OnIconProcessingComplete() { 185 void LargeIconWorker::OnIconProcessingComplete() {
160 callback_.Run(*result_); 186 callback_.Run(*result_);
161 } 187 }
162 188
189 void OnFetchIconFromGoogleServerComplete(
190 FaviconService* favicon_service,
191 const GURL& page_url,
192 const base::Callback<void(bool success)>& callback,
193 const std::string& icon_url,
194 const gfx::Image& image) {
195 if (image.IsEmpty()) {
196 DLOG(WARNING) << "large icon server fetch empty " << icon_url;
197 favicon_service->UnableToDownloadFavicon(GURL(icon_url));
198 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
199 base::Bind(callback, false));
200 return;
201 }
202
203 // TODO(jkrcal): Extract the original icon url from the response headers if
204 // available and use it instead of |icon_url|.
205
206 // Write fetched icons to FaviconService's cache, but only if no icon was
207 // available (clients are encouraged to do this in advance, but meanwhile
208 // something else could've been written). By marking the icons initially
209 // expired (out-of-date), they will be refetched when we visit the original
210 // page any time in the future.
211 favicon_service->SetExpiredFaviconsIfNoneKnown(
212 page_url, GURL(icon_url), favicon_base::IconType::TOUCH_ICON, image);
213 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
214 base::Bind(callback, true));
215 }
216
163 } // namespace 217 } // namespace
164 218
165 namespace favicon {
166
167 LargeIconService::LargeIconService( 219 LargeIconService::LargeIconService(
168 FaviconService* favicon_service, 220 FaviconService* favicon_service,
169 const scoped_refptr<base::TaskRunner>& background_task_runner) 221 const scoped_refptr<base::TaskRunner>& background_task_runner,
222 std::unique_ptr<image_fetcher::ImageFetcher> image_fetcher)
170 : favicon_service_(favicon_service), 223 : favicon_service_(favicon_service),
171 background_task_runner_(background_task_runner) { 224 background_task_runner_(background_task_runner),
225 image_fetcher_(std::move(image_fetcher)) {
172 large_icon_types_.push_back(favicon_base::IconType::FAVICON); 226 large_icon_types_.push_back(favicon_base::IconType::FAVICON);
173 large_icon_types_.push_back(favicon_base::IconType::TOUCH_ICON); 227 large_icon_types_.push_back(favicon_base::IconType::TOUCH_ICON);
174 large_icon_types_.push_back(favicon_base::IconType::TOUCH_PRECOMPOSED_ICON); 228 large_icon_types_.push_back(favicon_base::IconType::TOUCH_PRECOMPOSED_ICON);
175 } 229 }
176 230
177 LargeIconService::~LargeIconService() { 231 LargeIconService::~LargeIconService() {
178 } 232 }
179 233
180 base::CancelableTaskTracker::TaskId 234 base::CancelableTaskTracker::TaskId
181 LargeIconService::GetLargeIconOrFallbackStyle( 235 LargeIconService::GetLargeIconOrFallbackStyle(
(...skipping 12 matching lines...) Expand all
194 // TODO(beaudoin): For now this is just a wrapper around 248 // TODO(beaudoin): For now this is just a wrapper around
195 // GetLargestRawFaviconForPageURL. Add the logic required to select the best 249 // GetLargestRawFaviconForPageURL. Add the logic required to select the best
196 // possible large icon. Also add logic to fetch-on-demand when the URL of 250 // possible large icon. Also add logic to fetch-on-demand when the URL of
197 // a large icon is known but its bitmap is not available. 251 // a large icon is known but its bitmap is not available.
198 return favicon_service_->GetLargestRawFaviconForPageURL( 252 return favicon_service_->GetLargestRawFaviconForPageURL(
199 page_url, large_icon_types_, min_source_size_in_pixel, 253 page_url, large_icon_types_, min_source_size_in_pixel,
200 base::Bind(&LargeIconWorker::OnIconLookupComplete, worker), 254 base::Bind(&LargeIconWorker::OnIconLookupComplete, worker),
201 tracker); 255 tracker);
202 } 256 }
203 257
258 void LargeIconService::
259 GetLargeIconOrFallbackStyleFromGoogleServerSkippingLocalCache(
260 const GURL& page_url,
261 int min_source_size_in_pixel,
262 const base::Callback<void(bool success)>& callback) {
263 DCHECK_LE(0, min_source_size_in_pixel);
264
265 const GURL icon_url =
266 GetIconUrlForGoogleServerV2(page_url, min_source_size_in_pixel);
267
268 // Do not download if there is a previous cache miss recorded for |icon_url|.
269 if (!image_fetcher_ ||
270 favicon_service_->WasUnableToDownloadFavicon(icon_url)) {
271 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
272 base::Bind(callback, false));
273 return;
274 }
275
276 image_fetcher_->SetDataUseServiceName(
277 data_use_measurement::DataUseUserData::LARGE_ICON_SERVICE);
278 image_fetcher_->StartOrQueueNetworkRequest(
279 icon_url.spec(), icon_url,
280 base::Bind(&OnFetchIconFromGoogleServerComplete, favicon_service_,
281 page_url, callback));
282 }
283
204 } // namespace favicon 284 } // namespace favicon
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698