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 <cmath> | 7 #include <cmath> |
| 8 | 8 |
| 9 #include "base/hash.h" | 9 #include "base/hash.h" |
| 10 #include "base/single_thread_task_runner.h" | 10 #include "base/single_thread_task_runner.h" |
| 11 #include "base/thread_task_runner_handle.h" | 11 #include "base/thread_task_runner_handle.h" |
| 12 #include "components/favicon/core/favicon_client.h" | 12 #include "components/favicon/core/favicon_client.h" |
| 13 #include "components/favicon_base/favicon_util.h" | 13 #include "components/favicon_base/favicon_util.h" |
| 14 #include "components/favicon_base/select_favicon_frames.h" | 14 #include "components/favicon_base/select_favicon_frames.h" |
| 15 #include "components/history/core/browser/history_service.h" | 15 #include "components/history/core/browser/history_service.h" |
| 16 #include "third_party/skia/include/core/SkBitmap.h" | 16 #include "third_party/skia/include/core/SkBitmap.h" |
| 17 #include "ui/gfx/codec/png_codec.h" | 17 #include "ui/gfx/codec/png_codec.h" |
| 18 #include "ui/gfx/color_analysis.h" | |
| 19 #include "ui/gfx/color_utils.h" | |
| 18 #include "ui/gfx/favicon_size.h" | 20 #include "ui/gfx/favicon_size.h" |
| 19 #include "ui/gfx/image/image_skia.h" | 21 #include "ui/gfx/image/image_skia.h" |
| 20 #include "url/gurl.h" | 22 #include "url/gurl.h" |
| 21 | 23 |
| 22 namespace favicon { | 24 namespace favicon { |
| 23 namespace { | 25 namespace { |
| 24 | 26 |
| 27 const double kMaxDominantColorLuminance = 0.67; | |
| 28 | |
| 25 // Helper to run callback with empty results if we cannot get the history | 29 // Helper to run callback with empty results if we cannot get the history |
| 26 // service. | 30 // service. |
| 27 base::CancelableTaskTracker::TaskId RunWithEmptyResultAsync( | 31 base::CancelableTaskTracker::TaskId RunWithEmptyResultAsync( |
| 28 const favicon_base::FaviconResultsCallback& callback, | 32 const favicon_base::FaviconResultsCallback& callback, |
| 29 base::CancelableTaskTracker* tracker) { | 33 base::CancelableTaskTracker* tracker) { |
| 30 scoped_refptr<base::SingleThreadTaskRunner> thread_runner( | 34 scoped_refptr<base::SingleThreadTaskRunner> thread_runner( |
| 31 base::ThreadTaskRunnerHandle::Get()); | 35 base::ThreadTaskRunnerHandle::Get()); |
| 32 return tracker->PostTask( | 36 return tracker->PostTask( |
| 33 thread_runner.get(), FROM_HERE, | 37 thread_runner.get(), FROM_HERE, |
| 34 base::Bind(callback, | 38 base::Bind(callback, |
| 35 std::vector<favicon_base::FaviconRawBitmapResult>())); | 39 std::vector<favicon_base::FaviconRawBitmapResult>())); |
| 36 } | 40 } |
| 37 | 41 |
| 38 // Returns a vector of pixel edge sizes from |size_in_dip| and | 42 // Returns a vector of pixel edge sizes from |size_in_dip| and |
| 39 // favicon_base::GetFaviconScales(). | 43 // favicon_base::GetFaviconScales(). |
| 40 std::vector<int> GetPixelSizesForFaviconScales(int size_in_dip) { | 44 std::vector<int> GetPixelSizesForFaviconScales(int size_in_dip) { |
| 41 std::vector<float> scales = favicon_base::GetFaviconScales(); | 45 std::vector<float> scales = favicon_base::GetFaviconScales(); |
| 42 std::vector<int> sizes_in_pixel; | 46 std::vector<int> sizes_in_pixel; |
| 43 for (size_t i = 0; i < scales.size(); ++i) { | 47 for (size_t i = 0; i < scales.size(); ++i) { |
| 44 sizes_in_pixel.push_back(std::ceil(size_in_dip * scales[i])); | 48 sizes_in_pixel.push_back(std::ceil(size_in_dip * scales[i])); |
| 45 } | 49 } |
| 46 return sizes_in_pixel; | 50 return sizes_in_pixel; |
| 47 } | 51 } |
| 48 | 52 |
| 49 } // namespace | 53 } // namespace |
| 50 | 54 |
| 51 FaviconService::FaviconService(FaviconClient* favicon_client, | 55 FaviconService::FaviconService(FaviconClient* favicon_client, |
| 52 history::HistoryService* history_service) | 56 history::HistoryService* history_service) |
| 53 : history_service_(history_service), favicon_client_(favicon_client) { | 57 : history_service_(history_service), favicon_client_(favicon_client) { |
| 58 large_icon_types_.push_back(favicon_base::IconType::FAVICON); | |
| 59 large_icon_types_.push_back(favicon_base::IconType::TOUCH_ICON); | |
| 60 large_icon_types_.push_back(favicon_base::IconType::TOUCH_PRECOMPOSED_ICON); | |
| 54 } | 61 } |
| 55 | 62 |
| 56 FaviconService::~FaviconService() { | 63 FaviconService::~FaviconService() { |
| 57 } | 64 } |
| 58 | 65 |
| 59 // static | 66 // static |
| 60 void FaviconService::FaviconResultsCallbackRunner( | 67 void FaviconService::FaviconResultsCallbackRunner( |
| 61 const favicon_base::FaviconResultsCallback& callback, | 68 const favicon_base::FaviconResultsCallback& callback, |
| 62 const std::vector<favicon_base::FaviconRawBitmapResult>* results) { | 69 const std::vector<favicon_base::FaviconRawBitmapResult>* results) { |
| 63 callback.Run(*results); | 70 callback.Run(*results); |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 148 page_url, icon_types, desired_sizes_in_pixel, | 155 page_url, icon_types, desired_sizes_in_pixel, |
| 149 base::Bind(&FaviconService::RunFaviconRawBitmapCallbackWithBitmapResults, | 156 base::Bind(&FaviconService::RunFaviconRawBitmapCallbackWithBitmapResults, |
| 150 base::Unretained(this), callback, desired_size_in_pixel), | 157 base::Unretained(this), callback, desired_size_in_pixel), |
| 151 tracker); | 158 tracker); |
| 152 } | 159 } |
| 153 | 160 |
| 154 base::CancelableTaskTracker::TaskId | 161 base::CancelableTaskTracker::TaskId |
| 155 FaviconService::GetLargestRawFaviconForPageURL( | 162 FaviconService::GetLargestRawFaviconForPageURL( |
| 156 const GURL& page_url, | 163 const GURL& page_url, |
| 157 const std::vector<int>& icon_types, | 164 const std::vector<int>& icon_types, |
| 158 int minimum_size_in_pixels, | 165 int minimum_size_in_pixel, |
| 159 const favicon_base::FaviconRawBitmapCallback& callback, | 166 const favicon_base::FaviconRawBitmapCallback& callback, |
| 160 base::CancelableTaskTracker* tracker) { | 167 base::CancelableTaskTracker* tracker) { |
| 161 favicon_base::FaviconResultsCallback favicon_results_callback = | 168 favicon_base::FaviconResultsCallback favicon_results_callback = |
| 162 base::Bind(&FaviconService::RunFaviconRawBitmapCallbackWithBitmapResults, | 169 base::Bind(&FaviconService::RunFaviconRawBitmapCallbackWithBitmapResults, |
| 163 base::Unretained(this), callback, 0); | 170 base::Unretained(this), callback, 0); |
| 164 if (favicon_client_ && favicon_client_->IsNativeApplicationURL(page_url)) { | 171 if (favicon_client_ && favicon_client_->IsNativeApplicationURL(page_url)) { |
| 165 std::vector<int> desired_sizes_in_pixel; | 172 std::vector<int> desired_sizes_in_pixel; |
| 166 desired_sizes_in_pixel.push_back(0); | 173 desired_sizes_in_pixel.push_back(0); |
| 167 return favicon_client_->GetFaviconForNativeApplicationURL( | 174 return favicon_client_->GetFaviconForNativeApplicationURL( |
| 168 page_url, desired_sizes_in_pixel, favicon_results_callback, tracker); | 175 page_url, desired_sizes_in_pixel, favicon_results_callback, tracker); |
| 169 } | 176 } |
| 170 if (history_service_) { | 177 if (history_service_) { |
| 171 return history_service_->GetLargestFaviconForURL(page_url, icon_types, | 178 return history_service_->GetLargestFaviconForURL(page_url, icon_types, |
| 172 minimum_size_in_pixels, callback, tracker); | 179 minimum_size_in_pixel, callback, tracker); |
| 173 } | 180 } |
| 174 return RunWithEmptyResultAsync(favicon_results_callback, tracker); | 181 return RunWithEmptyResultAsync(favicon_results_callback, tracker); |
| 175 } | 182 } |
| 176 | 183 |
| 184 base::CancelableTaskTracker::TaskId FaviconService::getLargeIcon( | |
| 185 const GURL& pageURL, | |
|
huangs
2015/04/17 03:55:17
NIT: pageURL --> page_url
beaudoin
2015/04/17 14:50:52
Done.
| |
| 186 int desired_size_in_pixel, | |
| 187 const favicon_base::LargeIconCallback& callback, | |
| 188 base::CancelableTaskTracker* tracker) { | |
| 189 // TODO(beaudoin): For now this is just a wrapper around | |
| 190 // GetLargestRawFaviconForPageURL. Add the logic required to select the best | |
| 191 // possible large icon. Also add logic to fetch-on-demand when the URL of | |
| 192 // a large icon is known but its bitmap is not available. | |
| 193 return GetLargestRawFaviconForPageURL( | |
| 194 pageURL, | |
| 195 large_icon_types_, | |
| 196 desired_size_in_pixel, | |
| 197 base::Bind(&FaviconService::RunLargeIconCallback, | |
| 198 base::Unretained(this), callback, desired_size_in_pixel), | |
| 199 tracker); | |
| 200 } | |
| 201 | |
| 177 base::CancelableTaskTracker::TaskId FaviconService::GetFaviconForPageURL( | 202 base::CancelableTaskTracker::TaskId FaviconService::GetFaviconForPageURL( |
| 178 const GURL& page_url, | 203 const GURL& page_url, |
| 179 int icon_types, | 204 int icon_types, |
| 180 int desired_size_in_dip, | 205 int desired_size_in_dip, |
| 181 const favicon_base::FaviconResultsCallback& callback, | 206 const favicon_base::FaviconResultsCallback& callback, |
| 182 base::CancelableTaskTracker* tracker) { | 207 base::CancelableTaskTracker* tracker) { |
| 183 return GetFaviconForPageURLImpl( | 208 return GetFaviconForPageURLImpl( |
| 184 page_url, | 209 page_url, |
| 185 icon_types, | 210 icon_types, |
| 186 GetPixelSizesForFaviconScales(desired_size_in_dip), | 211 GetPixelSizesForFaviconScales(desired_size_in_dip), |
| (...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 364 &resized_bitmap_data)) { | 389 &resized_bitmap_data)) { |
| 365 callback.Run(favicon_base::FaviconRawBitmapResult()); | 390 callback.Run(favicon_base::FaviconRawBitmapResult()); |
| 366 return; | 391 return; |
| 367 } | 392 } |
| 368 | 393 |
| 369 bitmap_result.bitmap_data = base::RefCountedBytes::TakeVector( | 394 bitmap_result.bitmap_data = base::RefCountedBytes::TakeVector( |
| 370 &resized_bitmap_data); | 395 &resized_bitmap_data); |
| 371 callback.Run(bitmap_result); | 396 callback.Run(bitmap_result); |
| 372 } | 397 } |
| 373 | 398 |
| 399 void FaviconService::RunLargeIconCallback( | |
| 400 const favicon_base::LargeIconCallback& callback, | |
| 401 int desired_size_in_pixel, | |
| 402 const favicon_base::FaviconRawBitmapResult& bitmap_result) { | |
| 403 | |
|
huangs
2015/04/17 03:55:17
NIT: remove space
beaudoin
2015/04/17 14:50:52
Done.
| |
| 404 favicon_base::LargeIconResult result; | |
| 405 | |
| 406 if (!bitmap_result.is_valid()) { | |
|
huangs
2015/04/17 03:55:17
Semantically one would expect
result.bitmap = bi
beaudoin
2015/04/17 14:50:52
Done.
| |
| 407 callback.Run(result); | |
| 408 return; | |
| 409 } | |
| 410 | |
| 411 // If we found a bitmap, but it's smaller than the requested size, we | |
| 412 // compute the dominant color from the too-small bitmap. We adjust the | |
| 413 // luminance so it can be used as background for light text. | |
| 414 if (bitmap_result.pixel_size.width() < desired_size_in_pixel || | |
| 415 bitmap_result.pixel_size.height() < desired_size_in_pixel) { | |
| 416 result.dominant_color = | |
| 417 color_utils::CalculateKMeanColorOfPNG(bitmap_result.bitmap_data); | |
| 418 color_utils::HSL color_hsl; | |
|
huangs
2015/04/17 03:55:17
I think this logic should be moved to FallbackIcon
beaudoin
2015/04/17 14:50:52
Done.
| |
| 419 color_utils::SkColorToHSL(result.dominant_color, &color_hsl); | |
| 420 color_hsl.l = std::min(color_hsl.l, kMaxDominantColorLuminance); | |
| 421 result.dominant_color = | |
| 422 color_utils::HSLToSkColor(color_hsl, SK_AlphaOPAQUE); | |
| 423 callback.Run(result); | |
| 424 return; | |
| 425 } | |
| 426 | |
| 427 // The bitmap is the right size, use it. | |
| 428 result.bitmap = bitmap_result; | |
| 429 callback.Run(result); | |
| 430 } | |
| 431 | |
| 374 } // namespace favicon | 432 } // namespace favicon |
| OLD | NEW |