OLD | NEW |
---|---|
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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/android/vr_shell/textures/url_bar_texture.h" | 5 #include "chrome/browser/android/vr_shell/textures/url_bar_texture.h" |
6 | 6 |
7 #include "base/strings/utf_string_conversions.h" | 7 #include "base/strings/utf_string_conversions.h" |
8 #include "cc/paint/skia_paint_canvas.h" | 8 #include "cc/paint/skia_paint_canvas.h" |
9 #include "components/security_state/core/security_state.h" | 9 #include "components/url_formatter/url_formatter.h" |
10 #include "ui/gfx/canvas.h" | 10 #include "ui/gfx/canvas.h" |
11 #include "ui/gfx/color_palette.h" | |
11 #include "ui/gfx/font.h" | 12 #include "ui/gfx/font.h" |
12 #include "ui/gfx/font_list.h" | 13 #include "ui/gfx/font_list.h" |
13 #include "ui/gfx/geometry/point_f.h" | 14 #include "ui/gfx/geometry/point_f.h" |
14 #include "ui/gfx/geometry/rect.h" | 15 #include "ui/gfx/geometry/rect.h" |
15 #include "ui/gfx/geometry/rect_f.h" | 16 #include "ui/gfx/geometry/rect_f.h" |
16 #include "ui/gfx/paint_vector_icon.h" | 17 #include "ui/gfx/paint_vector_icon.h" |
17 #include "ui/gfx/render_text.h" | 18 #include "ui/gfx/render_text.h" |
18 #include "ui/gfx/vector_icon_types.h" | 19 #include "ui/gfx/vector_icon_types.h" |
19 #include "ui/vector_icons/vector_icons.h" | 20 #include "ui/vector_icons/vector_icons.h" |
20 | 21 |
21 namespace vr_shell { | 22 namespace vr_shell { |
22 | 23 |
23 namespace { | 24 namespace { |
24 | 25 |
25 // TODO(mthiesse): These values are all wrong. The UX spec is unclear. | 26 // TODO(mthiesse,cjgrant): These values are all wrong. The UX spec is unclear, |
26 static constexpr SkColor kBackground = 0x66D6D6D6; | 27 // and some have been adapted to make the URL bar legible. |
27 static constexpr SkColor kBackgroundHover = 0x6EF0F0F0; | 28 static constexpr SkColor kBackground = 0xC0F0F0F0; |
28 static constexpr SkColor kBackgroundDown = 0x76F6F6F6; | 29 static constexpr SkColor kBackgroundHover = 0xE0F8F8F8; |
30 static constexpr SkColor kBackgroundDown = 0xFFFFFFFF; | |
29 static constexpr SkColor kForeground = 0xFF333333; | 31 static constexpr SkColor kForeground = 0xFF333333; |
30 static constexpr SkColor kSeparatorColor = 0x33000000; | 32 static constexpr SkColor kSeparatorColor = 0x33000000; |
31 | 33 |
32 static constexpr SkColor kInfoOutlineIconColor = 0xFF5A5A5A; | 34 static constexpr SkColor kUrlTextColor = 0xFF000000; |
33 static constexpr SkColor kLockIconColor = 0xFF0B8043; | 35 static constexpr SkColor kUrlDeemphasizedTextColor = 0xFF5A5A5A; |
34 static constexpr SkColor kWarningIconColor = 0xFFC73821; | 36 static const SkColor kSecureColor = gfx::kGoogleGreen700; |
37 static const SkColor kWarningColor = gfx::kGoogleRed700; | |
35 | 38 |
36 static constexpr float kWidth = 0.672; | 39 static constexpr float kWidth = 0.672; |
37 static constexpr float kHeight = 0.088; | 40 static constexpr float kHeight = 0.088; |
38 static constexpr float kFontHeight = 0.027; | 41 static constexpr float kFontHeight = 0.027; |
39 static constexpr float kBackButtonWidth = kHeight; | 42 static constexpr float kBackButtonWidth = kHeight; |
40 static constexpr float kBackIconHeight = 0.05; | 43 static constexpr float kBackIconHeight = 0.0375; |
41 static constexpr float kBackIconOffset = 0.005; | 44 static constexpr float kBackIconOffset = 0.005; |
42 static constexpr float kSecurityFieldWidth = 0.06; | 45 static constexpr float kSecurityFieldWidth = 0.06; |
43 static constexpr float kSecurityIconHeight = 0.03; | 46 static constexpr float kSecurityIconHeight = 0.03; |
44 static constexpr float kUrlRightMargin = 0.02; | 47 static constexpr float kUrlRightMargin = 0.02; |
45 static constexpr float kSeparatorWidth = 0.002; | 48 static constexpr float kSeparatorWidth = 0.002; |
46 | 49 |
47 using security_state::SecurityLevel; | 50 using security_state::SecurityLevel; |
48 | 51 |
49 const struct gfx::VectorIcon& getSecurityIcon(int level) { | 52 const struct gfx::VectorIcon& getSecurityIcon(SecurityLevel level) { |
50 switch (level) { | 53 switch (level) { |
51 case SecurityLevel::NONE: | 54 case SecurityLevel::NONE: |
52 case SecurityLevel::HTTP_SHOW_WARNING: | 55 case SecurityLevel::HTTP_SHOW_WARNING: |
53 case SecurityLevel::SECURITY_WARNING: | 56 case SecurityLevel::SECURITY_WARNING: |
54 return ui::kInfoOutlineIcon; | 57 return ui::kInfoOutlineIcon; |
55 case SecurityLevel::SECURE: | 58 case SecurityLevel::SECURE: |
56 case SecurityLevel::EV_SECURE: | 59 case SecurityLevel::EV_SECURE: |
57 return ui::kLockIcon; | 60 return ui::kLockIcon; |
58 case SecurityLevel::SECURE_WITH_POLICY_INSTALLED_CERT: | |
59 case SecurityLevel::DANGEROUS: | 61 case SecurityLevel::DANGEROUS: |
62 return ui::kWarningIcon; | |
63 case SecurityLevel::SECURE_WITH_POLICY_INSTALLED_CERT: // ChromeOS only. | |
60 default: | 64 default: |
65 NOTREACHED(); | |
61 return ui::kWarningIcon; | 66 return ui::kWarningIcon; |
62 } | 67 } |
63 } | 68 } |
64 | 69 |
65 SkColor getSecurityIconColor(int level) { | 70 // See LocationBarView::GetSecureTextColor(). |
71 SkColor getSchemeColor(SecurityLevel level) { | |
66 switch (level) { | 72 switch (level) { |
67 case SecurityLevel::NONE: | 73 case SecurityLevel::NONE: |
68 case SecurityLevel::HTTP_SHOW_WARNING: | 74 case SecurityLevel::HTTP_SHOW_WARNING: |
69 case SecurityLevel::SECURITY_WARNING: | 75 case SecurityLevel::SECURITY_WARNING: |
70 return kInfoOutlineIconColor; | 76 return kUrlDeemphasizedTextColor; |
71 case SecurityLevel::SECURE: | 77 case SecurityLevel::SECURE: |
72 case SecurityLevel::EV_SECURE: | 78 case SecurityLevel::EV_SECURE: |
73 return kLockIconColor; | 79 return kSecureColor; |
74 case SecurityLevel::SECURE_WITH_POLICY_INSTALLED_CERT: | |
75 case SecurityLevel::DANGEROUS: | 80 case SecurityLevel::DANGEROUS: |
81 return kWarningColor; | |
82 case SecurityLevel::SECURE_WITH_POLICY_INSTALLED_CERT: // ChromeOS only. | |
76 default: | 83 default: |
77 return kWarningIconColor; | 84 NOTREACHED(); |
85 return kWarningColor; | |
78 } | 86 } |
79 } | 87 } |
80 | 88 |
89 void SetEmphasis(gfx::RenderText* render_text, | |
90 bool emphasis, | |
91 const gfx::Range& range) { | |
92 SkColor color = emphasis ? kUrlTextColor : kUrlDeemphasizedTextColor; | |
93 if (range.IsValid()) { | |
94 render_text->ApplyColor(color, range); | |
95 } else { | |
96 render_text->SetColor(color); | |
97 } | |
98 } | |
99 | |
81 gfx::PointF percentToMeters(const gfx::PointF& percent) { | 100 gfx::PointF percentToMeters(const gfx::PointF& percent) { |
82 return gfx::PointF(percent.x() * kWidth, percent.y() * kHeight); | 101 return gfx::PointF(percent.x() * kWidth, percent.y() * kHeight); |
83 } | 102 } |
84 | 103 |
85 } // namespace | 104 } // namespace |
86 | 105 |
87 UrlBarTexture::UrlBarTexture() : security_level_(SecurityLevel::DANGEROUS) {} | 106 UrlBarTexture::UrlBarTexture() : security_level_(SecurityLevel::DANGEROUS) {} |
88 | 107 |
89 UrlBarTexture::~UrlBarTexture() = default; | 108 UrlBarTexture::~UrlBarTexture() = default; |
90 | 109 |
91 void UrlBarTexture::SetURL(const GURL& gurl) { | 110 void UrlBarTexture::SetURL(const GURL& gurl) { |
92 if (gurl_ != gurl) | 111 if (gurl_ != gurl) |
93 set_dirty(); | 112 set_dirty(); |
94 gurl_ = gurl; | 113 gurl_ = gurl; |
95 } | 114 } |
96 | 115 |
97 void UrlBarTexture::SetSecurityLevel(int level) { | 116 void UrlBarTexture::SetSecurityLevel(SecurityLevel level) { |
98 if (&getSecurityIcon(security_level_) != &getSecurityIcon(level)) | 117 if (security_level_ != level) |
99 set_dirty(); | 118 set_dirty(); |
100 security_level_ = level; | 119 security_level_ = level; |
101 } | 120 } |
102 | 121 |
103 float UrlBarTexture::ToPixels(float meters) const { | 122 float UrlBarTexture::ToPixels(float meters) const { |
104 return meters * size_.width() / kWidth; | 123 return meters * size_.width() / kWidth; |
105 } | 124 } |
106 | 125 |
107 bool UrlBarTexture::HitsBackButton(const gfx::PointF& position) const { | 126 bool UrlBarTexture::HitsBackButton(const gfx::PointF& position) const { |
108 const gfx::PointF& meters = percentToMeters(position); | 127 const gfx::PointF& meters = percentToMeters(position); |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
186 // Site security state icon. | 205 // Site security state icon. |
187 if (!gurl_.is_empty()) { | 206 if (!gurl_.is_empty()) { |
188 canvas->save(); | 207 canvas->save(); |
189 canvas->translate( | 208 canvas->translate( |
190 kBackButtonWidth + kSeparatorWidth + kSecurityFieldWidth / 2, | 209 kBackButtonWidth + kSeparatorWidth + kSecurityFieldWidth / 2, |
191 kHeight / 2); | 210 kHeight / 2); |
192 canvas->translate(-kSecurityIconHeight / 2, -kSecurityIconHeight / 2); | 211 canvas->translate(-kSecurityIconHeight / 2, -kSecurityIconHeight / 2); |
193 const gfx::VectorIcon& icon = getSecurityIcon(security_level_); | 212 const gfx::VectorIcon& icon = getSecurityIcon(security_level_); |
194 icon_default_height = GetDefaultSizeOfVectorIcon(icon); | 213 icon_default_height = GetDefaultSizeOfVectorIcon(icon); |
195 icon_scale = kSecurityIconHeight / icon_default_height; | 214 icon_scale = kSecurityIconHeight / icon_default_height; |
196 SkColor icon_color = getSecurityIconColor(security_level_); | 215 SkColor icon_color = getSchemeColor(security_level_); |
197 canvas->scale(icon_scale, icon_scale); | 216 canvas->scale(icon_scale, icon_scale); |
198 PaintVectorIcon(&gfx_canvas, icon, icon_color); | 217 PaintVectorIcon(&gfx_canvas, icon, icon_color); |
199 canvas->restore(); | 218 canvas->restore(); |
200 } | 219 } |
201 | 220 |
202 canvas->restore(); | 221 canvas->restore(); |
203 | 222 |
204 if (!gurl_.is_empty()) { | 223 if (!gurl_.is_empty()) { |
205 if (last_drawn_gurl_ != gurl_) { | 224 if (last_drawn_gurl_ != gurl_ || |
206 // Draw text based on pixel sizes rather than meters, for correct font | 225 last_drawn_security_level_ != security_level_) { |
207 // sizing. | |
208 int pixel_font_height = texture_size.height() * kFontHeight / kHeight; | |
209 float url_x = kBackButtonWidth + kSeparatorWidth + kSecurityFieldWidth; | 226 float url_x = kBackButtonWidth + kSeparatorWidth + kSecurityFieldWidth; |
210 float url_width = kWidth - url_x - kUrlRightMargin; | 227 float url_width = kWidth - url_x - kUrlRightMargin; |
211 gfx::Rect text_bounds(ToPixels(url_x), 0, ToPixels(url_width), | 228 gfx::Rect text_bounds(ToPixels(url_x), 0, ToPixels(url_width), |
212 ToPixels(kHeight)); | 229 ToPixels(kHeight)); |
213 gurl_render_texts_ = PrepareDrawStringRect( | 230 RenderUrl(texture_size, text_bounds); |
214 base::UTF8ToUTF16(gurl_.spec()), | |
215 GetDefaultFontList(pixel_font_height), SK_ColorBLACK, &text_bounds, | |
216 kTextAlignmentLeft, kWrappingBehaviorNoWrap); | |
217 last_drawn_gurl_ = gurl_; | 231 last_drawn_gurl_ = gurl_; |
232 last_drawn_security_level_ = security_level_; | |
218 } | 233 } |
219 for (auto& render_text : gurl_render_texts_) | 234 url_render_text_->Draw(&gfx_canvas); |
220 render_text->Draw(&gfx_canvas); | |
221 } | 235 } |
222 } | 236 } |
223 | 237 |
238 void UrlBarTexture::RenderUrl(const gfx::Size& texture_size, | |
239 const gfx::Rect& bounds) { | |
240 url::Parsed parsed; | |
241 const base::string16 text = url_formatter::FormatUrl( | |
242 gurl_, url_formatter::kFormatUrlOmitAll, net::UnescapeRule::NORMAL, | |
243 &parsed, nullptr, nullptr); | |
244 | |
245 int pixel_font_height = texture_size.height() * kFontHeight / kHeight; | |
246 auto font_list = GetFontList(pixel_font_height, text); | |
247 | |
248 std::unique_ptr<gfx::RenderText> render_text( | |
249 gfx::RenderText::CreateInstance()); | |
250 render_text->SetText(text); | |
251 render_text->SetFontList(font_list); | |
252 render_text->SetColor(SK_ColorBLACK); | |
253 render_text->SetHorizontalAlignment(gfx::ALIGN_LEFT); | |
254 render_text->SetDisplayRect(bounds); | |
255 render_text->SetElideBehavior(gfx::TRUNCATE); | |
estark
2017/05/25 01:28:32
Does this just truncate at the end? If so you'll n
cjgrant
2017/05/25 01:55:28
Certainly. Eliding (and other things) are called
| |
256 | |
257 ApplyUrlStyling(text, parsed, render_text.get()); | |
258 | |
259 url_render_text_ = std::move(render_text); | |
260 } | |
261 | |
262 // This method replicates behavior in OmniboxView::UpdateTextStyle(), and | |
estark
2017/05/24 01:11:07
Would it be possible to share any of this code by
cjgrant
2017/05/24 17:40:58
On reuse, I considered this, but the Omnibox code
cjgrant
2017/05/25 00:50:59
After some discussion on our team, we'd like to go
| |
263 // attempts to maintain similar code structure. | |
264 void UrlBarTexture::ApplyUrlStyling(const base::string16& formatted_url, | |
265 const url::Parsed& parsed, | |
266 gfx::RenderText* render_text) { | |
267 const url::Component& scheme = parsed.scheme; | |
268 const url::Component& host = parsed.host; | |
269 | |
270 enum DeemphasizeComponents { | |
271 EVERYTHING, | |
272 ALL_BUT_SCHEME, | |
273 ALL_BUT_HOST, | |
274 NOTHING, | |
275 } deemphasize = NOTHING; | |
276 | |
277 const base::string16 url_scheme = | |
estark
2017/05/25 01:28:32
Have you looked at AutocompleteInput::Parse? Are t
cjgrant
2017/05/25 01:55:28
Addressing your top-level comment and this comment
| |
278 formatted_url.substr(scheme.begin, scheme.len); | |
279 | |
280 // Data URLs are rarely human-readable and can be used for spoofing, so draw | |
281 // attention to the scheme to emphasize "this is just a bunch of data". For | |
282 // normal URLs, the host is the best proxy for "identity". | |
283 // TODO(cjgrant): Handle extensions, if required, for desktop. | |
estark
2017/05/25 01:28:32
Can you please file a bug for this and include its
cjgrant
2017/05/26 15:58:59
Done. crbug/726749
| |
284 if (url_scheme == base::UTF8ToUTF16(url::kDataScheme)) | |
285 deemphasize = ALL_BUT_SCHEME; | |
286 else if (host.is_nonempty()) | |
287 deemphasize = ALL_BUT_HOST; | |
288 | |
289 gfx::Range scheme_range = scheme.is_nonempty() | |
290 ? gfx::Range(scheme.begin, scheme.end()) | |
291 : gfx::Range::InvalidRange(); | |
292 switch (deemphasize) { | |
293 case EVERYTHING: | |
294 SetEmphasis(render_text, false, gfx::Range::InvalidRange()); | |
295 break; | |
296 case NOTHING: | |
297 SetEmphasis(render_text, true, gfx::Range::InvalidRange()); | |
298 break; | |
299 case ALL_BUT_SCHEME: | |
300 DCHECK(scheme_range.IsValid()); | |
301 SetEmphasis(render_text, false, gfx::Range::InvalidRange()); | |
302 SetEmphasis(render_text, true, scheme_range); | |
303 break; | |
304 case ALL_BUT_HOST: | |
305 SetEmphasis(render_text, false, gfx::Range::InvalidRange()); | |
306 SetEmphasis(render_text, true, gfx::Range(host.begin, host.end())); | |
307 break; | |
308 } | |
309 | |
310 // Only SECURE and DANGEROUS levels (pages served over HTTPS or flagged by | |
311 // SafeBrowsing) get a special scheme color treatment. If the security level | |
312 // is NONE or HTTP_SHOW_WARNING, we do not override the text style previously | |
313 // applied to the scheme text range by SetEmphasis(). | |
314 if (scheme_range.IsValid() && security_level_ != security_state::NONE && | |
315 security_level_ != security_state::HTTP_SHOW_WARNING) { | |
316 render_text->ApplyColor(getSchemeColor(security_level_), scheme_range); | |
317 if (security_level_ == SecurityLevel::DANGEROUS) { | |
318 render_text->ApplyStyle(gfx::TextStyle::DIAGONAL_STRIKE, true, | |
319 scheme_range); | |
320 } | |
321 } | |
322 } | |
323 | |
224 gfx::Size UrlBarTexture::GetPreferredTextureSize(int maximum_width) const { | 324 gfx::Size UrlBarTexture::GetPreferredTextureSize(int maximum_width) const { |
225 return gfx::Size(maximum_width, maximum_width * kHeight / kWidth); | 325 return gfx::Size(maximum_width, maximum_width * kHeight / kWidth); |
226 } | 326 } |
227 | 327 |
228 gfx::SizeF UrlBarTexture::GetDrawnSize() const { | 328 gfx::SizeF UrlBarTexture::GetDrawnSize() const { |
229 return size_; | 329 return size_; |
230 } | 330 } |
231 | 331 |
232 } // namespace vr_shell | 332 } // namespace vr_shell |
OLD | NEW |