Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(205)

Side by Side Diff: chrome/browser/android/vr_shell/textures/url_bar_texture.cc

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

Powered by Google App Engine
This is Rietveld 408576698