Chromium Code Reviews| OLD | NEW |
|---|---|
| 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=%s&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) | |
| 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 page_url, | |
| 48 const std::string& client_id, | |
| 49 int supported_size) { | |
| 50 return GURL(base::StringPrintf(kGoogleFaviconServiceRequestFormat, | |
| 51 page_url.spec().c_str(), | |
| 52 client_id.c_str(), | |
| 53 supported_size)); | |
| 54 } | |
| 55 | |
| 28 // Helper to run callback with empty results if we cannot get the history | 56 // Helper to run callback with empty results if we cannot get the history |
| 29 // service. | 57 // service. |
| 30 base::CancelableTaskTracker::TaskId RunWithEmptyResultAsync( | 58 base::CancelableTaskTracker::TaskId RunWithEmptyResultAsync( |
| 31 const favicon_base::FaviconResultsCallback& callback, | 59 const favicon_base::FaviconResultsCallback& callback, |
| 32 base::CancelableTaskTracker* tracker) { | 60 base::CancelableTaskTracker* tracker) { |
| 33 scoped_refptr<base::SingleThreadTaskRunner> thread_runner( | 61 scoped_refptr<base::SingleThreadTaskRunner> thread_runner( |
| 34 base::ThreadTaskRunnerHandle::Get()); | 62 base::ThreadTaskRunnerHandle::Get()); |
| 35 return tracker->PostTask( | 63 return tracker->PostTask( |
| 36 thread_runner.get(), FROM_HERE, | 64 thread_runner.get(), FROM_HERE, |
| 37 base::Bind(callback, | 65 base::Bind(callback, |
| 38 std::vector<favicon_base::FaviconRawBitmapResult>())); | 66 std::vector<favicon_base::FaviconRawBitmapResult>())); |
| 39 } | 67 } |
| 40 | 68 |
| 41 // Returns a vector of pixel edge sizes from |size_in_dip| and | 69 // Returns a vector of pixel edge sizes from |size_in_dip| and |
| 42 // favicon_base::GetFaviconScales(). | 70 // favicon_base::GetFaviconScales(). |
| 43 std::vector<int> GetPixelSizesForFaviconScales(int size_in_dip) { | 71 std::vector<int> GetPixelSizesForFaviconScales(int size_in_dip) { |
| 44 std::vector<float> scales = favicon_base::GetFaviconScales(); | 72 std::vector<float> scales = favicon_base::GetFaviconScales(); |
| 45 std::vector<int> sizes_in_pixel; | 73 std::vector<int> sizes_in_pixel; |
| 46 for (size_t i = 0; i < scales.size(); ++i) { | 74 for (size_t i = 0; i < scales.size(); ++i) { |
| 47 sizes_in_pixel.push_back(std::ceil(size_in_dip * scales[i])); | 75 sizes_in_pixel.push_back(std::ceil(size_in_dip * scales[i])); |
| 48 } | 76 } |
| 49 return sizes_in_pixel; | 77 return sizes_in_pixel; |
| 50 } | 78 } |
| 51 | 79 |
| 52 } // namespace | 80 } // namespace |
| 53 | 81 |
| 54 FaviconService::FaviconService(std::unique_ptr<FaviconClient> favicon_client, | 82 FaviconService::FaviconService( |
| 55 history::HistoryService* history_service) | 83 std::unique_ptr<FaviconClient> favicon_client, |
| 84 history::HistoryService* history_service, | |
| 85 std::unique_ptr<image_fetcher::ImageFetcher> image_fetcher) | |
| 56 : favicon_client_(std::move(favicon_client)), | 86 : favicon_client_(std::move(favicon_client)), |
| 57 history_service_(history_service) {} | 87 history_service_(history_service), |
| 88 image_fetcher_(std::move(image_fetcher)) {} | |
| 58 | 89 |
| 59 FaviconService::~FaviconService() { | 90 FaviconService::~FaviconService() { |
| 60 } | 91 } |
| 61 | 92 |
| 62 // static | 93 // static |
| 63 void FaviconService::FaviconResultsCallbackRunner( | 94 void FaviconService::FaviconResultsCallbackRunner( |
| 64 const favicon_base::FaviconResultsCallback& callback, | 95 const favicon_base::FaviconResultsCallback& callback, |
| 65 const std::vector<favicon_base::FaviconRawBitmapResult>* results) { | 96 const std::vector<favicon_base::FaviconRawBitmapResult>* results) { |
| 66 callback.Run(*results); | 97 callback.Run(*results); |
| 67 } | 98 } |
| (...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 191 base::CancelableTaskTracker* tracker) { | 222 base::CancelableTaskTracker* tracker) { |
| 192 TRACE_EVENT0("browser", "FaviconService::GetFaviconForPageURL"); | 223 TRACE_EVENT0("browser", "FaviconService::GetFaviconForPageURL"); |
| 193 return GetFaviconForPageURLImpl( | 224 return GetFaviconForPageURLImpl( |
| 194 page_url, | 225 page_url, |
| 195 icon_types, | 226 icon_types, |
| 196 GetPixelSizesForFaviconScales(desired_size_in_dip), | 227 GetPixelSizesForFaviconScales(desired_size_in_dip), |
| 197 callback, | 228 callback, |
| 198 tracker); | 229 tracker); |
| 199 } | 230 } |
| 200 | 231 |
| 232 void FaviconService::DownloadFromGoogleServerFaviconImageForPageURL( | |
| 233 const GURL& page_url, | |
| 234 int desired_size_in_pixel, | |
| 235 std::string client_id, | |
| 236 data_use_measurement::DataUseUserData::ServiceName data_use_service_name, | |
| 237 const favicon_base::FaviconImageCallback& callback) { | |
| 238 if (!image_fetcher_.get()) { | |
| 239 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
| 240 FROM_HERE, base::Bind(callback, favicon_base::FaviconImageResult())); | |
| 241 return; | |
| 242 } | |
| 243 | |
| 244 int supported_size = | |
| 245 GetGoogleFaviconServiceSupportedSize(desired_size_in_pixel); | |
| 246 GURL image_url = GetGoogleFaviconServiceURL(page_url, client_id, | |
| 247 supported_size); | |
| 248 | |
| 249 image_fetcher_->SetDataUseServiceName(data_use_service_name); | |
| 250 image_fetcher_->StartOrQueueNetworkRequest( | |
|
pkotwicz
2016/09/21 20:55:53
I think that ImageFetcherImpl::StartOrQueueNetwork
jkrcal
2016/12/09 13:57:44
This would probably affect Chrome's legitimate req
pkotwicz
2016/12/09 19:46:30
I don't know of a mechanism which would not be pro
jkrcal
2016/12/14 15:18:50
Acknowledged.
| |
| 251 image_url.spec(), image_url, | |
| 252 base::Bind(&FaviconService:: | |
| 253 StoreFaviconFromGoogleServiceAndRunFaviconImageCallback, | |
| 254 base::Unretained(this), callback, page_url)); | |
| 255 } | |
| 256 | |
| 201 base::CancelableTaskTracker::TaskId | 257 base::CancelableTaskTracker::TaskId |
| 202 FaviconService::UpdateFaviconMappingsAndFetch( | 258 FaviconService::UpdateFaviconMappingsAndFetch( |
| 203 const GURL& page_url, | 259 const GURL& page_url, |
| 204 const std::vector<GURL>& icon_urls, | 260 const std::vector<GURL>& icon_urls, |
| 205 int icon_types, | 261 int icon_types, |
| 206 int desired_size_in_dip, | 262 int desired_size_in_dip, |
| 207 const favicon_base::FaviconResultsCallback& callback, | 263 const favicon_base::FaviconResultsCallback& callback, |
| 208 base::CancelableTaskTracker* tracker) { | 264 base::CancelableTaskTracker* tracker) { |
| 209 if (history_service_) { | 265 if (history_service_) { |
| 210 return history_service_->UpdateFaviconMappingsAndFetch( | 266 return history_service_->UpdateFaviconMappingsAndFetch( |
| (...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 341 const favicon_base::FaviconRawBitmapCallback& callback, | 397 const favicon_base::FaviconRawBitmapCallback& callback, |
| 342 int desired_size_in_pixel, | 398 int desired_size_in_pixel, |
| 343 const std::vector<favicon_base::FaviconRawBitmapResult>& | 399 const std::vector<favicon_base::FaviconRawBitmapResult>& |
| 344 favicon_bitmap_results) { | 400 favicon_bitmap_results) { |
| 345 TRACE_EVENT0("browser", | 401 TRACE_EVENT0("browser", |
| 346 "FaviconService::RunFaviconRawBitmapCallbackWithBitmapResults"); | 402 "FaviconService::RunFaviconRawBitmapCallbackWithBitmapResults"); |
| 347 callback.Run( | 403 callback.Run( |
| 348 ResizeFaviconBitmapResult(favicon_bitmap_results, desired_size_in_pixel)); | 404 ResizeFaviconBitmapResult(favicon_bitmap_results, desired_size_in_pixel)); |
| 349 } | 405 } |
| 350 | 406 |
| 407 void FaviconService::StoreFaviconFromGoogleServiceAndRunFaviconImageCallback( | |
| 408 const favicon_base::FaviconImageCallback& callback, | |
| 409 const GURL& page_url, | |
| 410 const std::string& icon_url, | |
| 411 const gfx::Image& image) { | |
| 412 favicon_base::FaviconImageResult image_result; | |
| 413 image_result.icon_url = GURL(icon_url); | |
| 414 image_result.image = image; | |
| 415 if (image_result.image.IsEmpty()) { | |
|
pkotwicz
2016/12/07 04:31:22
Can you move this if to line 412
I am suggesting:
jkrcal
2016/12/09 13:57:44
Sure. It's much better this way!
I was not sure wh
| |
| 416 callback.Run(image_result); | |
| 417 return; | |
| 418 } | |
| 419 | |
| 420 favicon_base::SetFaviconColorSpace(&image_result.image); | |
| 421 // Store the favicon in the cache. | |
| 422 SetFavicons(page_url, image_result.icon_url, | |
| 423 favicon_base::IconType::FAVICON, image_result.image); | |
| 424 // Mark them as out-of-date so that they are refetched when we visit the | |
| 425 // original page any time in the future. | |
| 426 SetFaviconOutOfDateForPage(page_url); | |
| 427 | |
| 428 callback.Run(image_result); | |
| 429 } | |
| 430 | |
| 351 } // namespace favicon | 431 } // namespace favicon |
| OLD | NEW |