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

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

Issue 2347173002: Extend FaviconService to support fetching favicons from a Google server (Closed)
Patch Set: Minor polish Created 4 years, 3 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 (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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/favicon_service.h" 5 #include "components/favicon/core/favicon_service.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 #include <cmath> 8 #include <cmath>
9 #include <utility> 9 #include <utility>
10 10
11 #include "base/hash.h" 11 #include "base/hash.h"
12 #include "base/single_thread_task_runner.h" 12 #include "base/single_thread_task_runner.h"
13 #include "base/strings/stringprintf.h"
13 #include "base/threading/thread_task_runner_handle.h" 14 #include "base/threading/thread_task_runner_handle.h"
14 #include "base/trace_event/trace_event.h" 15 #include "base/trace_event/trace_event.h"
15 #include "components/favicon/core/favicon_client.h" 16 #include "components/favicon/core/favicon_client.h"
16 #include "components/favicon_base/favicon_util.h" 17 #include "components/favicon_base/favicon_util.h"
17 #include "components/favicon_base/select_favicon_frames.h" 18 #include "components/favicon_base/select_favicon_frames.h"
18 #include "components/history/core/browser/history_service.h" 19 #include "components/history/core/browser/history_service.h"
20 #include "components/image_fetcher/image_fetcher.h"
19 #include "third_party/skia/include/core/SkBitmap.h" 21 #include "third_party/skia/include/core/SkBitmap.h"
20 #include "ui/gfx/codec/png_codec.h" 22 #include "ui/gfx/codec/png_codec.h"
21 #include "ui/gfx/favicon_size.h" 23 #include "ui/gfx/favicon_size.h"
22 #include "ui/gfx/image/image_skia.h" 24 #include "ui/gfx/image/image_skia.h"
23 #include "url/gurl.h" 25 #include "url/gurl.h"
24 26
25 namespace favicon { 27 namespace favicon {
26 namespace { 28 namespace {
27 29
30 const char kGoogleFaviconServiceRequestFormat[] =
31 "https://s2.googleusercontent.com/s2/"
32 "favicons?domain=%s&src=chrome_newtab_mobile&sz=%d&alt=404";
33
34 const int kGoogleFaviconServiceSupportedSizes[] = {16, 24, 32, 48, 64};
35
36 int GetGoogleFaviconServiceSupportedSize(int arbitrary_size) {
37 // Take the smallest size larger than arbitrary_size.
38 for (int size : kGoogleFaviconServiceSupportedSizes) {
39 if (size > arbitrary_size)
Marc Treib 2016/09/19 08:26:39 I think this should be >=
noyau (Ping after 24h) 2016/09/19 09:00:50 Definitely should be >=. I'm surprised that there
jkrcal 2016/09/20 15:52:06 Done.
40 return size;
41 }
42 // Or at least the largest available size.
43 return kGoogleFaviconServiceSupportedSizes
44 [arraysize(kGoogleFaviconServiceSupportedSizes) - 1];
45 }
46
47 GURL GetGoogleFaviconServiceURL(GURL domain_url, int supported_size) {
48 return GURL(base::StringPrintf(kGoogleFaviconServiceRequestFormat,
49 domain_url.spec().c_str(),
50 supported_size));
51 }
52
28 // Helper to run callback with empty results if we cannot get the history 53 // Helper to run callback with empty results if we cannot get the history
29 // service. 54 // service.
30 base::CancelableTaskTracker::TaskId RunWithEmptyResultAsync( 55 base::CancelableTaskTracker::TaskId RunWithEmptyResultAsync(
31 const favicon_base::FaviconResultsCallback& callback, 56 const favicon_base::FaviconResultsCallback& callback,
32 base::CancelableTaskTracker* tracker) { 57 base::CancelableTaskTracker* tracker) {
33 scoped_refptr<base::SingleThreadTaskRunner> thread_runner( 58 scoped_refptr<base::SingleThreadTaskRunner> thread_runner(
34 base::ThreadTaskRunnerHandle::Get()); 59 base::ThreadTaskRunnerHandle::Get());
35 return tracker->PostTask( 60 return tracker->PostTask(
36 thread_runner.get(), FROM_HERE, 61 thread_runner.get(), FROM_HERE,
37 base::Bind(callback, 62 base::Bind(callback,
38 std::vector<favicon_base::FaviconRawBitmapResult>())); 63 std::vector<favicon_base::FaviconRawBitmapResult>()));
39 } 64 }
40 65
41 // Returns a vector of pixel edge sizes from |size_in_dip| and 66 // Returns a vector of pixel edge sizes from |size_in_dip| and
42 // favicon_base::GetFaviconScales(). 67 // favicon_base::GetFaviconScales().
43 std::vector<int> GetPixelSizesForFaviconScales(int size_in_dip) { 68 std::vector<int> GetPixelSizesForFaviconScales(int size_in_dip) {
44 std::vector<float> scales = favicon_base::GetFaviconScales(); 69 std::vector<float> scales = favicon_base::GetFaviconScales();
45 std::vector<int> sizes_in_pixel; 70 std::vector<int> sizes_in_pixel;
46 for (size_t i = 0; i < scales.size(); ++i) { 71 for (size_t i = 0; i < scales.size(); ++i) {
47 sizes_in_pixel.push_back(std::ceil(size_in_dip * scales[i])); 72 sizes_in_pixel.push_back(std::ceil(size_in_dip * scales[i]));
48 } 73 }
49 return sizes_in_pixel; 74 return sizes_in_pixel;
50 } 75 }
51 76
52 } // namespace 77 } // namespace
53 78
54 FaviconService::FaviconService(std::unique_ptr<FaviconClient> favicon_client, 79 FaviconService::FaviconService(
55 history::HistoryService* history_service) 80 std::unique_ptr<FaviconClient> favicon_client,
81 history::HistoryService* history_service,
82 std::unique_ptr<image_fetcher::ImageFetcher> image_fetcher)
56 : favicon_client_(std::move(favicon_client)), 83 : favicon_client_(std::move(favicon_client)),
57 history_service_(history_service) {} 84 history_service_(history_service),
85 image_fetcher_(std::move(image_fetcher)) {}
58 86
59 FaviconService::~FaviconService() { 87 FaviconService::~FaviconService() {
60 } 88 }
61 89
62 // static 90 // static
63 void FaviconService::FaviconResultsCallbackRunner( 91 void FaviconService::FaviconResultsCallbackRunner(
64 const favicon_base::FaviconResultsCallback& callback, 92 const favicon_base::FaviconResultsCallback& callback,
65 const std::vector<favicon_base::FaviconRawBitmapResult>* results) { 93 const std::vector<favicon_base::FaviconRawBitmapResult>* results) {
66 callback.Run(*results); 94 callback.Run(*results);
67 } 95 }
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after
191 base::CancelableTaskTracker* tracker) { 219 base::CancelableTaskTracker* tracker) {
192 TRACE_EVENT0("browser", "FaviconService::GetFaviconForPageURL"); 220 TRACE_EVENT0("browser", "FaviconService::GetFaviconForPageURL");
193 return GetFaviconForPageURLImpl( 221 return GetFaviconForPageURLImpl(
194 page_url, 222 page_url,
195 icon_types, 223 icon_types,
196 GetPixelSizesForFaviconScales(desired_size_in_dip), 224 GetPixelSizesForFaviconScales(desired_size_in_dip),
197 callback, 225 callback,
198 tracker); 226 tracker);
199 } 227 }
200 228
229 void FaviconService::DownloadFromGoogleServerFaviconImageForPageURL(
230 const GURL& domain_url,
231 int desired_size_in_pixel,
232 std::string client_id,
pkotwicz 2016/09/19 20:07:51 |client_id| seems unused. Do you need to add a TOD
jkrcal 2016/09/20 15:52:06 Oh, my mistake, thanks! Used now.
233 data_use_measurement::DataUseUserData::ServiceName data_use_service_name,
234 const favicon_base::FaviconImageCallback& callback) {
235 if (!image_fetcher_.get()) {
236 base::ThreadTaskRunnerHandle::Get()->PostTask(
237 FROM_HERE, base::Bind(callback, favicon_base::FaviconImageResult()));
238 return;
239 }
240
241 int supported_size =
242 GetGoogleFaviconServiceSupportedSize(desired_size_in_pixel);
243 GURL domain_url_sanitized = domain_url.GetWithEmptyPath();
244 GURL image_url =
245 GetGoogleFaviconServiceURL(domain_url_sanitized, supported_size);
246
247 image_fetcher_->SetDataUseServiceName(data_use_service_name);
248 image_fetcher_->StartOrQueueNetworkRequest(
249 image_url.spec(), image_url,
250 base::Bind(&FaviconService::
251 StoreFaviconFromGoogleServiceAndRunFaviconImageCallback,
252 base::Unretained(this), callback, domain_url_sanitized));
253 }
254
201 base::CancelableTaskTracker::TaskId 255 base::CancelableTaskTracker::TaskId
202 FaviconService::UpdateFaviconMappingsAndFetch( 256 FaviconService::UpdateFaviconMappingsAndFetch(
203 const GURL& page_url, 257 const GURL& page_url,
204 const std::vector<GURL>& icon_urls, 258 const std::vector<GURL>& icon_urls,
205 int icon_types, 259 int icon_types,
206 int desired_size_in_dip, 260 int desired_size_in_dip,
207 const favicon_base::FaviconResultsCallback& callback, 261 const favicon_base::FaviconResultsCallback& callback,
208 base::CancelableTaskTracker* tracker) { 262 base::CancelableTaskTracker* tracker) {
209 if (history_service_) { 263 if (history_service_) {
210 return history_service_->UpdateFaviconMappingsAndFetch( 264 return history_service_->UpdateFaviconMappingsAndFetch(
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after
341 const favicon_base::FaviconRawBitmapCallback& callback, 395 const favicon_base::FaviconRawBitmapCallback& callback,
342 int desired_size_in_pixel, 396 int desired_size_in_pixel,
343 const std::vector<favicon_base::FaviconRawBitmapResult>& 397 const std::vector<favicon_base::FaviconRawBitmapResult>&
344 favicon_bitmap_results) { 398 favicon_bitmap_results) {
345 TRACE_EVENT0("browser", 399 TRACE_EVENT0("browser",
346 "FaviconService::RunFaviconRawBitmapCallbackWithBitmapResults"); 400 "FaviconService::RunFaviconRawBitmapCallbackWithBitmapResults");
347 callback.Run( 401 callback.Run(
348 ResizeFaviconBitmapResult(favicon_bitmap_results, desired_size_in_pixel)); 402 ResizeFaviconBitmapResult(favicon_bitmap_results, desired_size_in_pixel));
349 } 403 }
350 404
405 void FaviconService::StoreFaviconFromGoogleServiceAndRunFaviconImageCallback(
406 const favicon_base::FaviconImageCallback& callback,
407 const GURL& domain_url,
408 const std::string& icon_url,
409 const gfx::Image& image) {
410 favicon_base::FaviconImageResult image_result;
411 image_result.image = image;
412 if (image_result.image.IsEmpty()) {
413 image_result.icon_url = GURL();
pkotwicz 2016/09/19 20:07:51 The "icon URL" is used as a key in the database. U
jkrcal 2016/09/20 15:52:06 I do not understand what you mean. Maybe I puzzled
pkotwicz 2016/09/21 20:55:53 Sorry my bad. I did not realize that |icon_url| is
jkrcal 2016/12/09 13:57:44 No problem :)
414 callback.Run(image_result);
415 }
416
417 image_result.icon_url = GURL(icon_url);
418 favicon_base::SetFaviconColorSpace(&image_result.image);
419
420 // Store the favicon in the cache.
421 SetFavicons(domain_url, image_result.icon_url,
422 favicon_base::IconType::FAVICON, image_result.image);
noyau (Ping after 24h) 2016/09/19 09:00:50 The documentation for SetFavicons is very explicit
jkrcal 2016/09/20 15:52:06 I am not sure how to address your comment. - Do
noyau (Ping after 24h) 2016/09/21 09:49:20 I'm not sure how to solve the issue, pkotwicz@ is
pkotwicz 2016/09/21 20:55:53 noyau: Not having image reps for all of ui::GetSup
jkrcal 2016/12/09 13:57:44 Acknowledged.
423 // Mark them as out-of-date so that they are refetched when we visit the
424 // original page any time in the future.
425 SetFaviconOutOfDateForPage(domain_url);
pkotwicz 2016/09/19 20:07:51 - favicon_base::FAVICON is only meant to store 16x
jkrcal 2016/09/20 15:52:06 My observations: - If I correctly understand what
pkotwicz 2016/09/21 20:55:53 We only sync favicons for bookmarks. It is possibl
jkrcal 2016/12/09 13:57:44 As per offline discussion with pkotwicz@, only one
426
427 callback.Run(image_result);
428 }
429
351 } // namespace favicon 430 } // namespace favicon
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698