Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 "chrome/browser/ui/webui/large_icon_source.h" | 5 #include "chrome/browser/ui/webui/large_icon_source.h" |
| 6 | 6 |
| 7 #include <vector> | |
| 8 | |
| 7 #include "base/memory/ref_counted_memory.h" | 9 #include "base/memory/ref_counted_memory.h" |
| 8 #include "chrome/browser/search/instant_io_context.h" | 10 #include "chrome/browser/search/instant_io_context.h" |
| 9 #include "chrome/common/favicon/large_icon_url_parser.h" | 11 #include "chrome/common/favicon/large_icon_url_parser.h" |
| 10 #include "chrome/common/url_constants.h" | 12 #include "chrome/common/url_constants.h" |
| 11 #include "components/favicon/core/fallback_icon_service.h" | 13 #include "components/favicon/core/fallback_icon_service.h" |
| 12 #include "components/favicon/core/favicon_service.h" | 14 #include "components/favicon/core/favicon_service.h" |
| 13 #include "components/favicon_base/fallback_icon_style.h" | 15 #include "components/favicon_base/fallback_icon_style.h" |
| 14 #include "net/url_request/url_request.h" | 16 #include "net/url_request/url_request.h" |
| 15 #include "third_party/skia/include/core/SkColor.h" | |
| 16 #include "ui/gfx/color_analysis.h" | |
| 17 #include "ui/gfx/color_utils.h" | |
| 18 | 17 |
| 19 namespace { | 18 namespace { |
| 20 | 19 |
| 21 const int kDefaultLargeIconSize = 96; | |
| 22 const int kMaxLargeIconSize = 192; // Arbitrary bound to safeguard endpoint. | 20 const int kMaxLargeIconSize = 192; // Arbitrary bound to safeguard endpoint. |
| 23 | 21 |
| 24 const double kMaxBackgroundLuminance = 0.67; | |
| 25 const SkColor kDarkGray = SkColorSetRGB(0x78, 0x78, 0x78); | |
| 26 const SkColor kTextColor = SK_ColorWHITE; | |
| 27 const SkColor kDefaultBackgroundColor = kDarkGray; | |
| 28 | |
| 29 } // namespace | 22 } // namespace |
| 30 | 23 |
| 31 LargeIconSource::IconRequest::IconRequest() : size(kDefaultLargeIconSize) { | |
| 32 } | |
| 33 | |
| 34 LargeIconSource::IconRequest::IconRequest( | |
| 35 const content::URLDataSource::GotDataCallback& callback_in, | |
| 36 const GURL& url_in, | |
| 37 int size_in) | |
| 38 : callback(callback_in), | |
| 39 url(url_in), | |
| 40 size(size_in) { | |
| 41 } | |
| 42 | |
| 43 LargeIconSource::IconRequest::~IconRequest() { | |
| 44 } | |
| 45 | |
| 46 LargeIconSource::LargeIconSource( | 24 LargeIconSource::LargeIconSource( |
| 47 favicon::FaviconService* favicon_service, | 25 favicon::FaviconService* favicon_service, |
| 48 favicon::FallbackIconService* fallback_icon_service) | 26 favicon::FallbackIconService* fallback_icon_service) |
| 49 : favicon_service_(favicon_service), | 27 : favicon_service_(favicon_service), |
| 50 fallback_icon_service_(fallback_icon_service) { | 28 fallback_icon_service_(fallback_icon_service) { |
| 51 large_icon_types_.push_back(favicon_base::IconType::FAVICON); | |
| 52 large_icon_types_.push_back(favicon_base::IconType::TOUCH_ICON); | |
| 53 large_icon_types_.push_back(favicon_base::IconType::TOUCH_PRECOMPOSED_ICON); | |
| 54 } | 29 } |
| 55 | 30 |
| 56 LargeIconSource::~LargeIconSource() { | 31 LargeIconSource::~LargeIconSource() { |
| 57 } | 32 } |
| 58 | 33 |
| 59 std::string LargeIconSource::GetSource() const { | 34 std::string LargeIconSource::GetSource() const { |
| 60 return chrome::kChromeUILargeIconHost; | 35 return chrome::kChromeUILargeIconHost; |
| 61 } | 36 } |
| 62 | 37 |
| 63 void LargeIconSource::StartDataRequest( | 38 void LargeIconSource::StartDataRequest( |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 78 SendNotFoundResponse(callback); | 53 SendNotFoundResponse(callback); |
| 79 return; | 54 return; |
| 80 } | 55 } |
| 81 | 56 |
| 82 GURL url(parser.url_string()); | 57 GURL url(parser.url_string()); |
| 83 if (!url.is_valid()) { | 58 if (!url.is_valid()) { |
| 84 SendNotFoundResponse(callback); | 59 SendNotFoundResponse(callback); |
| 85 return; | 60 return; |
| 86 } | 61 } |
| 87 | 62 |
| 88 favicon_service_->GetLargestRawFaviconForPageURL( | 63 favicon_service_->GetLargeIconOrFallbackStyle( |
| 89 url, | 64 url, |
| 90 large_icon_types_, | |
| 91 parser.size_in_pixels(), | 65 parser.size_in_pixels(), |
| 92 base::Bind( | 66 base::Bind(&LargeIconSource::OnIconDataAvailable, base::Unretained(this), |
| 93 &LargeIconSource::OnIconDataAvailable, | 67 callback, url, parser.size_in_pixels()), |
| 94 base::Unretained(this), | |
| 95 IconRequest(callback, url, parser.size_in_pixels())), | |
| 96 &cancelable_task_tracker_); | 68 &cancelable_task_tracker_); |
| 97 } | 69 } |
| 98 | 70 |
| 99 std::string LargeIconSource::GetMimeType(const std::string&) const { | 71 std::string LargeIconSource::GetMimeType(const std::string&) const { |
| 100 // We need to explicitly return a mime type, otherwise if the user tries to | 72 // We need to explicitly return a mime type, otherwise if the user tries to |
| 101 // drag the image they get no extension. | 73 // drag the image they get no extension. |
| 102 return "image/png"; | 74 return "image/png"; |
| 103 } | 75 } |
| 104 | 76 |
| 105 bool LargeIconSource::ShouldReplaceExistingSource() const { | 77 bool LargeIconSource::ShouldReplaceExistingSource() const { |
| 106 // Leave the existing DataSource in place, otherwise we'll drop any pending | 78 // Leave the existing DataSource in place, otherwise we'll drop any pending |
| 107 // requests on the floor. | 79 // requests on the floor. |
| 108 return false; | 80 return false; |
| 109 } | 81 } |
| 110 | 82 |
| 111 bool LargeIconSource::ShouldServiceRequest( | 83 bool LargeIconSource::ShouldServiceRequest( |
| 112 const net::URLRequest* request) const { | 84 const net::URLRequest* request) const { |
| 113 if (request->url().SchemeIs(chrome::kChromeSearchScheme)) | 85 if (request->url().SchemeIs(chrome::kChromeSearchScheme)) |
| 114 return InstantIOContext::ShouldServiceRequest(request); | 86 return InstantIOContext::ShouldServiceRequest(request); |
| 115 return URLDataSource::ShouldServiceRequest(request); | 87 return URLDataSource::ShouldServiceRequest(request); |
| 116 } | 88 } |
| 117 | 89 |
| 118 void LargeIconSource::OnIconDataAvailable( | 90 void LargeIconSource::OnIconDataAvailable( |
| 119 const IconRequest& request, | 91 const content::URLDataSource::GotDataCallback& callback, |
| 120 const favicon_base::FaviconRawBitmapResult& bitmap_result) { | 92 const GURL& url, |
| 121 if (!bitmap_result.is_valid()) { | 93 int size, |
| 122 SendDefaultFallbackIcon(request); | 94 const favicon_base::LargeIconResult& result) { |
| 95 if (result.bitmap.is_valid()) { | |
| 96 callback.Run(result.bitmap.bitmap_data.get()); | |
| 123 return; | 97 return; |
| 124 } | 98 } |
| 125 | 99 |
| 126 // If we found a bitmap, but it's smaller than the requested size, we | 100 // Bitmap is invalid, use the fallback. |
|
huangs
2015/04/17 15:30:47
NIT: "...use the fallback if service is available.
beaudoin
2015/04/17 20:29:05
Done.
| |
| 127 // generate a fallback using the dominant color from the too-small bitmap. | 101 if (!fallback_icon_service_) { |
| 128 // We adjust the luminance of the background so we can put light text over it. | 102 SendNotFoundResponse(callback); |
| 129 if (bitmap_result.pixel_size.width() < request.size || | |
| 130 bitmap_result.pixel_size.height() < request.size) { | |
| 131 SkColor background = | |
| 132 color_utils::CalculateKMeanColorOfPNG(bitmap_result.bitmap_data); | |
| 133 color_utils::HSL background_hsl; | |
| 134 color_utils::SkColorToHSL(background, &background_hsl); | |
| 135 background_hsl.l = std::min(background_hsl.l, kMaxBackgroundLuminance); | |
| 136 background = color_utils::HSLToSkColor(background_hsl, SK_AlphaOPAQUE); | |
| 137 | |
| 138 // Now we can construct the fallback icon. | |
| 139 SendFallbackIcon(request, kTextColor, background); | |
| 140 return; | 103 return; |
| 141 } | 104 } |
| 142 | |
| 143 request.callback.Run(bitmap_result.bitmap_data.get()); | |
| 144 } | |
| 145 | |
| 146 void LargeIconSource::SendDefaultFallbackIcon(const IconRequest& request) { | |
| 147 SendFallbackIcon(request, kTextColor, kDefaultBackgroundColor); | |
| 148 } | |
| 149 | |
| 150 void LargeIconSource::SendFallbackIcon(const IconRequest& request, | |
| 151 SkColor text_color, | |
| 152 SkColor background_color) { | |
| 153 if (!fallback_icon_service_) { | |
| 154 SendNotFoundResponse(request.callback); | |
| 155 return; | |
| 156 } | |
| 157 favicon_base::FallbackIconStyle style; | |
| 158 style.background_color = background_color; | |
| 159 style.text_color = text_color; | |
| 160 style.font_size_ratio = 0.44; | |
| 161 style.roundness = 0; // Square. Round corners can be applied by JavaScript. | |
| 162 std::vector<unsigned char> bitmap_data = | 105 std::vector<unsigned char> bitmap_data = |
| 163 fallback_icon_service_->RenderFallbackIconBitmap( | 106 fallback_icon_service_->RenderFallbackIconBitmap( |
| 164 request.url, request.size, style); | 107 url, size, result.fallback_icon_style); |
| 165 request.callback.Run(base::RefCountedBytes::TakeVector(&bitmap_data)); | 108 callback.Run(base::RefCountedBytes::TakeVector(&bitmap_data)); |
| 166 } | 109 } |
| 167 | 110 |
| 168 void LargeIconSource::SendNotFoundResponse( | 111 void LargeIconSource::SendNotFoundResponse( |
| 169 const content::URLDataSource::GotDataCallback& callback) { | 112 const content::URLDataSource::GotDataCallback& callback) { |
| 170 callback.Run(nullptr); | 113 callback.Run(nullptr); |
| 171 } | 114 } |
| OLD | NEW |