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