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

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

Issue 2902783002: VR: Add URL bar security-related coloring and emphasis. (Closed)
Patch Set: Another test case for HTTPS + not secure. Created 3 years, 7 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 // TODO(mthiesse): These values are all wrong. The UX spec is unclear. 27 // TODO(mthiesse): These values are all wrong. The UX spec is unclear.
26 static constexpr SkColor kBackground = 0x66D6D6D6; 28 static constexpr SkColor kBackground = 0x66D6D6D6;
27 static constexpr SkColor kBackgroundHover = 0x6EF0F0F0; 29 static constexpr SkColor kBackgroundHover = 0x6EF0F0F0;
28 static constexpr SkColor kBackgroundDown = 0x76F6F6F6; 30 static constexpr SkColor kBackgroundDown = 0x76F6F6F6;
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(vr_shell::RenderTextWrapper* render_text,
90 bool emphasis,
91 const gfx::Range& range) {
92 SkColor color = emphasis ? kUrlTextColor : kUrlDeemphasizedTextColor;
93 if (range.IsValid()) {
Ian Vollick 2017/05/26 01:22:19 OOC, when is the range invalid?
cjgrant 2017/05/26 15:58:59 Check out the call sites below. An invalid range j
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
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);
256
257 vr_shell::RenderTextWrapper vr_render_text(render_text.get());
258 ApplyUrlStyling(text, parsed, security_level_, &vr_render_text);
259
260 url_render_text_ = std::move(render_text);
261 }
262
263 // This method replicates behavior in OmniboxView::UpdateTextStyle(), and
264 // attempts to maintain similar code structure.
Ian Vollick 2017/05/26 01:22:19 As was discussed earlier, it'll be nice to use Omn
cjgrant 2017/05/26 15:58:59 Done. crbug/726745
265 void UrlBarTexture::ApplyUrlStyling(
266 const base::string16& formatted_url,
267 const url::Parsed& parsed,
268 const security_state::SecurityLevel security_level,
269 vr_shell::RenderTextWrapper* render_text) {
270 const url::Component& scheme = parsed.scheme;
271 const url::Component& host = parsed.host;
272
273 enum DeemphasizeComponents {
274 EVERYTHING,
275 ALL_BUT_SCHEME,
276 ALL_BUT_HOST,
277 NOTHING,
278 } deemphasize = NOTHING;
279
280 const base::string16 url_scheme =
281 formatted_url.substr(scheme.begin, scheme.len);
282
283 // Data URLs are rarely human-readable and can be used for spoofing, so draw
284 // attention to the scheme to emphasize "this is just a bunch of data". For
285 // normal URLs, the host is the best proxy for "identity".
286 // TODO(cjgrant): Handle extensions, if required, for desktop.
287 if (url_scheme == base::UTF8ToUTF16(url::kDataScheme))
288 deemphasize = ALL_BUT_SCHEME;
289 else if (host.is_nonempty())
290 deemphasize = ALL_BUT_HOST;
291
292 gfx::Range scheme_range = scheme.is_nonempty()
293 ? gfx::Range(scheme.begin, scheme.end())
294 : gfx::Range::InvalidRange();
295 switch (deemphasize) {
296 case EVERYTHING:
297 setEmphasis(render_text, false, gfx::Range::InvalidRange());
298 break;
299 case NOTHING:
300 setEmphasis(render_text, true, gfx::Range::InvalidRange());
301 break;
302 case ALL_BUT_SCHEME:
303 DCHECK(scheme_range.IsValid());
304 setEmphasis(render_text, false, gfx::Range::InvalidRange());
305 setEmphasis(render_text, true, scheme_range);
306 break;
307 case ALL_BUT_HOST:
308 setEmphasis(render_text, false, gfx::Range::InvalidRange());
309 setEmphasis(render_text, true, gfx::Range(host.begin, host.end()));
310 break;
311 }
312
313 // Only SECURE and DANGEROUS levels (pages served over HTTPS or flagged by
314 // SafeBrowsing) get a special scheme color treatment. If the security level
315 // is NONE or HTTP_SHOW_WARNING, we do not override the text style previously
316 // applied to the scheme text range by setEmphasis().
317 if (scheme_range.IsValid() && security_level != security_state::NONE &&
318 security_level != security_state::HTTP_SHOW_WARNING) {
319 render_text->ApplyColor(getSchemeColor(security_level), scheme_range);
320 if (security_level == SecurityLevel::DANGEROUS) {
321 render_text->ApplyStyle(gfx::TextStyle::DIAGONAL_STRIKE, true,
322 scheme_range);
323 }
324 }
325 }
326
224 gfx::Size UrlBarTexture::GetPreferredTextureSize(int maximum_width) const { 327 gfx::Size UrlBarTexture::GetPreferredTextureSize(int maximum_width) const {
225 return gfx::Size(maximum_width, maximum_width * kHeight / kWidth); 328 return gfx::Size(maximum_width, maximum_width * kHeight / kWidth);
226 } 329 }
227 330
228 gfx::SizeF UrlBarTexture::GetDrawnSize() const { 331 gfx::SizeF UrlBarTexture::GetDrawnSize() const {
229 return size_; 332 return size_;
230 } 333 }
231 334
232 } // namespace vr_shell 335 } // namespace vr_shell
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698