OLD | NEW |
| (Empty) |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/ui/views/location_bar/origin_chip_view.h" | |
6 | |
7 #include "base/files/file_path.h" | |
8 #include "base/metrics/histogram.h" | |
9 #include "base/strings/string_util.h" | |
10 #include "base/strings/utf_string_conversions.h" | |
11 #include "chrome/browser/browser_process.h" | |
12 #include "chrome/browser/extensions/extension_util.h" | |
13 #include "chrome/browser/favicon/favicon_tab_helper.h" | |
14 #include "chrome/browser/profiles/profile.h" | |
15 #include "chrome/browser/safe_browsing/safe_browsing_service.h" | |
16 #include "chrome/browser/safe_browsing/ui_manager.h" | |
17 #include "chrome/browser/search/search.h" | |
18 #include "chrome/browser/themes/theme_properties.h" | |
19 #include "chrome/browser/ui/browser.h" | |
20 #include "chrome/browser/ui/elide_url.h" | |
21 #include "chrome/browser/ui/location_bar/origin_chip_info.h" | |
22 #include "chrome/browser/ui/omnibox/omnibox_view.h" | |
23 #include "chrome/browser/ui/toolbar/toolbar_model.h" | |
24 #include "chrome/browser/ui/views/location_bar/location_bar_view.h" | |
25 #include "chrome/common/extensions/extension_constants.h" | |
26 #include "content/public/browser/user_metrics.h" | |
27 #include "content/public/browser/web_contents.h" | |
28 #include "content/public/common/url_constants.h" | |
29 #include "extensions/browser/extension_icon_image.h" | |
30 #include "extensions/browser/extension_registry.h" | |
31 #include "extensions/common/constants.h" | |
32 #include "extensions/common/manifest_handlers/icons_handler.h" | |
33 #include "grit/theme_resources.h" | |
34 #include "ui/base/resource/resource_bundle.h" | |
35 #include "ui/base/theme_provider.h" | |
36 #include "ui/gfx/canvas.h" | |
37 #include "ui/gfx/font_list.h" | |
38 #include "ui/gfx/text_utils.h" | |
39 #include "ui/views/background.h" | |
40 #include "ui/views/controls/button/label_button.h" | |
41 #include "ui/views/controls/button/label_button_border.h" | |
42 #include "ui/views/painter.h" | |
43 | |
44 | |
45 // OriginChipExtensionIcon ---------------------------------------------------- | |
46 | |
47 class OriginChipExtensionIcon : public extensions::IconImage::Observer { | |
48 public: | |
49 OriginChipExtensionIcon(LocationIconView* icon_view, | |
50 Profile* profile, | |
51 const extensions::Extension* extension); | |
52 ~OriginChipExtensionIcon() override; | |
53 | |
54 // IconImage::Observer: | |
55 void OnExtensionIconImageChanged(extensions::IconImage* image) override; | |
56 | |
57 private: | |
58 LocationIconView* icon_view_; | |
59 scoped_ptr<extensions::IconImage> icon_image_; | |
60 | |
61 DISALLOW_COPY_AND_ASSIGN(OriginChipExtensionIcon); | |
62 }; | |
63 | |
64 OriginChipExtensionIcon::OriginChipExtensionIcon( | |
65 LocationIconView* icon_view, | |
66 Profile* profile, | |
67 const extensions::Extension* extension) | |
68 : icon_view_(icon_view), | |
69 icon_image_(new extensions::IconImage( | |
70 profile, | |
71 extension, | |
72 extensions::IconsInfo::GetIcons(extension), | |
73 extension_misc::EXTENSION_ICON_BITTY, | |
74 extensions::util::GetDefaultAppIcon(), | |
75 this)) { | |
76 // Forces load of the image. | |
77 icon_image_->image_skia().GetRepresentation(1.0f); | |
78 | |
79 if (!icon_image_->image_skia().image_reps().empty()) | |
80 OnExtensionIconImageChanged(icon_image_.get()); | |
81 } | |
82 | |
83 OriginChipExtensionIcon::~OriginChipExtensionIcon() { | |
84 } | |
85 | |
86 void OriginChipExtensionIcon::OnExtensionIconImageChanged( | |
87 extensions::IconImage* image) { | |
88 if (icon_view_) | |
89 icon_view_->SetImage(&icon_image_->image_skia()); | |
90 } | |
91 | |
92 | |
93 // OriginChipView ------------------------------------------------------------- | |
94 | |
95 namespace { | |
96 | |
97 const int kEdgeThickness = 5; | |
98 const int k16x16IconLeadingSpacing = 1; | |
99 const int k16x16IconTrailingSpacing = 2; | |
100 const int kIconTextSpacing = 3; | |
101 | |
102 const int kNormalImages[3][9] = { | |
103 IMAGE_GRID(IDR_ORIGIN_CHIP_NORMAL), | |
104 IMAGE_GRID(IDR_ORIGIN_CHIP_HOVER), | |
105 IMAGE_GRID(IDR_ORIGIN_CHIP_PRESSED) | |
106 }; | |
107 | |
108 const int kMalwareImages[3][9] = { | |
109 IMAGE_GRID(IDR_ORIGIN_CHIP_MALWARE_NORMAL), | |
110 IMAGE_GRID(IDR_ORIGIN_CHIP_MALWARE_HOVER), | |
111 IMAGE_GRID(IDR_ORIGIN_CHIP_MALWARE_PRESSED) | |
112 }; | |
113 | |
114 const int kBrokenSSLImages[3][9] = { | |
115 IMAGE_GRID(IDR_ORIGIN_CHIP_BROKENSSL_NORMAL), | |
116 IMAGE_GRID(IDR_ORIGIN_CHIP_BROKENSSL_HOVER), | |
117 IMAGE_GRID(IDR_ORIGIN_CHIP_BROKENSSL_PRESSED) | |
118 }; | |
119 | |
120 const int kEVImages[3][9] = { | |
121 IMAGE_GRID(IDR_ORIGIN_CHIP_EV_NORMAL), | |
122 IMAGE_GRID(IDR_ORIGIN_CHIP_EV_HOVER), | |
123 IMAGE_GRID(IDR_ORIGIN_CHIP_EV_PRESSED) | |
124 }; | |
125 | |
126 const extensions::Extension* GetExtension(const GURL& url, Profile* profile) { | |
127 if (!url.SchemeIs(extensions::kExtensionScheme)) | |
128 return NULL; | |
129 extensions::ExtensionRegistry* registry = | |
130 extensions::ExtensionRegistry::Get(profile); | |
131 return registry->enabled_extensions().GetByID(url.host()); | |
132 } | |
133 | |
134 } // namespace | |
135 | |
136 OriginChipView::OriginChipView(LocationBarView* location_bar_view, | |
137 Profile* profile, | |
138 const gfx::FontList& font_list) | |
139 : LabelButton(this, base::string16()), | |
140 location_bar_view_(location_bar_view), | |
141 profile_(profile), | |
142 showing_16x16_icon_(false), | |
143 fade_in_animation_(this), | |
144 security_level_(ToolbarModel::NONE), | |
145 url_malware_(false) { | |
146 EnableCanvasFlippingForRTLUI(true); | |
147 | |
148 scoped_refptr<SafeBrowsingService> sb_service = | |
149 g_browser_process->safe_browsing_service(); | |
150 // |sb_service| may be NULL in tests. | |
151 if (sb_service.get() && sb_service->ui_manager().get()) | |
152 sb_service->ui_manager()->AddObserver(this); | |
153 | |
154 SetFontList(font_list); | |
155 | |
156 // TODO(gbillock): Would be nice to just use stock LabelButton stuff here. | |
157 location_icon_view_ = new LocationIconView(location_bar_view_); | |
158 // Make location icon hover events count as hovering the origin chip. | |
159 location_icon_view_->set_interactive(false); | |
160 location_icon_view_->ShowTooltip(true); | |
161 AddChildView(location_icon_view_); | |
162 | |
163 ev_label_ = new views::Label(base::string16(), GetFontList()); | |
164 ev_label_->SetHorizontalAlignment(gfx::ALIGN_LEFT); | |
165 ev_label_->SetElideBehavior(gfx::NO_ELIDE); | |
166 AddChildView(ev_label_); | |
167 | |
168 host_label_ = new views::Label(base::string16(), GetFontList()); | |
169 host_label_->SetHorizontalAlignment(gfx::ALIGN_LEFT); | |
170 host_label_->SetElideBehavior(gfx::NO_ELIDE); | |
171 AddChildView(host_label_); | |
172 | |
173 fade_in_animation_.SetTweenType(gfx::Tween::LINEAR_OUT_SLOW_IN); | |
174 fade_in_animation_.SetSlideDuration(175); | |
175 | |
176 // Ensure |pressed_text_color_| and |background_colors_| are initialized. | |
177 SetBorderImages(kNormalImages); | |
178 } | |
179 | |
180 OriginChipView::~OriginChipView() { | |
181 scoped_refptr<SafeBrowsingService> sb_service = | |
182 g_browser_process->safe_browsing_service(); | |
183 if (sb_service.get() && sb_service->ui_manager().get()) | |
184 sb_service->ui_manager()->RemoveObserver(this); | |
185 } | |
186 | |
187 void OriginChipView::OnChanged() { | |
188 content::WebContents* web_contents = location_bar_view_->GetWebContents(); | |
189 if (!web_contents) | |
190 return; | |
191 | |
192 // Note: security level can change async as the connection is made. | |
193 GURL url = location_bar_view_->GetToolbarModel()->GetURL(); | |
194 const ToolbarModel::SecurityLevel security_level = | |
195 location_bar_view_->GetToolbarModel()->GetSecurityLevel(true); | |
196 | |
197 bool url_malware = OriginChip::IsMalware(url, web_contents); | |
198 | |
199 // TODO(gbillock): We persist a malware setting while a new WebContents | |
200 // content is loaded, meaning that we end up transiently marking a safe | |
201 // page as malware. Need to fix that. | |
202 | |
203 if ((url == url_displayed_) && | |
204 (security_level == security_level_) && | |
205 (url_malware == url_malware_)) | |
206 return; | |
207 | |
208 url_displayed_ = url; | |
209 url_malware_ = url_malware; | |
210 security_level_ = security_level; | |
211 | |
212 if (url_malware_) { | |
213 SetBorderImages(kMalwareImages); | |
214 } else if (security_level_ == ToolbarModel::SECURITY_ERROR) { | |
215 SetBorderImages(kBrokenSSLImages); | |
216 } else if (security_level_ == ToolbarModel::EV_SECURE) { | |
217 SetBorderImages(kEVImages); | |
218 } else { | |
219 SetBorderImages(kNormalImages); | |
220 } | |
221 | |
222 ev_label_->SetText(location_bar_view_->GetToolbarModel()->GetEVCertName()); | |
223 ev_label_->SetVisible(security_level_ == ToolbarModel::EV_SECURE); | |
224 | |
225 // TODO(pkasting): Allow the origin chip to shrink, and use ElideHost(). | |
226 base::string16 host = | |
227 OriginChip::LabelFromURLForProfile(url_displayed_, profile_); | |
228 host_label_->SetText(host); | |
229 host_label_->SetTooltipText(base::UTF8ToUTF16(url.spec())); | |
230 | |
231 showing_16x16_icon_ = url_displayed_.is_empty() || | |
232 url_displayed_.SchemeIs(content::kChromeUIScheme); | |
233 int icon = showing_16x16_icon_ ? IDR_PRODUCT_LOGO_16 : | |
234 location_bar_view_->GetToolbarModel()->GetIconForSecurityLevel( | |
235 security_level_); | |
236 const extensions::Extension* extension = | |
237 GetExtension(url_displayed_, profile_); | |
238 if (extension) { | |
239 icon = IDR_EXTENSIONS_FAVICON; | |
240 showing_16x16_icon_ = true; | |
241 extension_icon_.reset( | |
242 new OriginChipExtensionIcon(location_icon_view_, profile_, extension)); | |
243 } else { | |
244 extension_icon_.reset(); | |
245 } | |
246 location_icon_view_->SetImage(GetThemeProvider()->GetImageSkiaNamed(icon)); | |
247 | |
248 if (visible()) { | |
249 CancelFade(); | |
250 Layout(); | |
251 SchedulePaint(); | |
252 } | |
253 } | |
254 | |
255 void OriginChipView::FadeIn() { | |
256 fade_in_animation_.Show(); | |
257 } | |
258 | |
259 void OriginChipView::CancelFade() { | |
260 fade_in_animation_.Stop(); | |
261 } | |
262 | |
263 int OriginChipView::HostLabelOffset() const { | |
264 return host_label_->x() - GetLabelX(); | |
265 } | |
266 | |
267 int OriginChipView::WidthFromStartOfLabels() const { | |
268 return width() - GetLabelX(); | |
269 } | |
270 | |
271 gfx::Size OriginChipView::GetPreferredSize() const { | |
272 // TODO(pkasting): Use of " " here is a horrible hack, to be replaced by | |
273 // splitting the chip into separate pieces for EV/host. | |
274 int label_size = host_label_->GetPreferredSize().width(); | |
275 if (ev_label_->visible()) { | |
276 label_size += ev_label_->GetPreferredSize().width() + | |
277 gfx::GetStringWidth(base::ASCIIToUTF16(" "), GetFontList()); | |
278 } | |
279 return gfx::Size(GetLabelX() + label_size + kEdgeThickness, | |
280 location_icon_view_->GetPreferredSize().height()); | |
281 } | |
282 | |
283 void OriginChipView::Layout() { | |
284 // TODO(gbillock): Eventually we almost certainly want to use | |
285 // LocationBarLayout for leading and trailing decorations. | |
286 | |
287 location_icon_view_->SetBounds( | |
288 kEdgeThickness + (showing_16x16_icon_ ? k16x16IconLeadingSpacing : 0), | |
289 LocationBarView::kNormalEdgeThickness, | |
290 location_icon_view_->GetPreferredSize().width(), | |
291 height() - 2 * LocationBarView::kNormalEdgeThickness); | |
292 | |
293 int label_x = GetLabelX(); | |
294 int label_width = std::max(0, width() - label_x - kEdgeThickness); | |
295 const int label_y = LocationBarView::kNormalEdgeThickness; | |
296 const int label_height = height() - 2 * LocationBarView::kNormalEdgeThickness; | |
297 if (ev_label_->visible()) { | |
298 int ev_label_width = | |
299 std::min(ev_label_->GetPreferredSize().width(), label_width); | |
300 ev_label_->SetBounds(label_x, label_y, ev_label_width, label_height); | |
301 // TODO(pkasting): See comments in GetPreferredSize(). | |
302 ev_label_width += | |
303 gfx::GetStringWidth(base::ASCIIToUTF16(" "), GetFontList()); | |
304 label_x += ev_label_width; | |
305 label_width = std::max(0, label_width - ev_label_width); | |
306 } | |
307 host_label_->SetBounds(label_x, label_y, label_width, label_height); | |
308 } | |
309 | |
310 int OriginChipView::GetLabelX() const { | |
311 const int icon_spacing = showing_16x16_icon_ ? | |
312 (k16x16IconLeadingSpacing + k16x16IconTrailingSpacing) : 0; | |
313 return kEdgeThickness + location_icon_view_->GetPreferredSize().width() + | |
314 icon_spacing + kIconTextSpacing; | |
315 } | |
316 | |
317 void OriginChipView::SetBorderImages(const int images[3][9]) { | |
318 scoped_ptr<views::LabelButtonBorder> border( | |
319 new views::LabelButtonBorder(views::Button::STYLE_BUTTON)); | |
320 | |
321 for (size_t i = 0; i < 3; ++i) { | |
322 views::Painter* painter = views::Painter::CreateImageGridPainter(images[i]); | |
323 border->SetPainter(false, static_cast<Button::ButtonState>(i), painter); | |
324 | |
325 // Calculate a representative background color of the provided image grid to | |
326 // use as the background color of the host label in order to color the text | |
327 // appropriately. We grab the color of the middle pixel of the middle image | |
328 // of the background, which we treat as the representative color of the | |
329 // entire background (reasonable, given the current appearance of these | |
330 // images). | |
331 // | |
332 // NOTE: Because this is called from the constructor, when we're not in a | |
333 // Widget yet, GetThemeProvider() may return NULL, so use the location bar's | |
334 // theme provider instead to be safe. | |
335 const SkBitmap& bitmap( | |
336 location_bar_view_->GetThemeProvider()->GetImageSkiaNamed( | |
337 images[i][4])->GetRepresentation(1.0f).sk_bitmap()); | |
338 SkAutoLockPixels pixel_lock(bitmap); | |
339 background_colors_[i] = | |
340 bitmap.getColor(bitmap.width() / 2, bitmap.height() / 2); | |
341 } | |
342 | |
343 // Calculate the actual text color of the pressed label. | |
344 host_label_->SetBackgroundColor(background_colors_[Button::STATE_PRESSED]); | |
345 pressed_text_color_ = host_label_->enabled_color(); | |
346 host_label_->SetBackgroundColor(background_colors_[state()]); | |
347 | |
348 SetBorder(border.Pass()); | |
349 } | |
350 | |
351 void OriginChipView::AnimationProgressed(const gfx::Animation* animation) { | |
352 if (animation == &fade_in_animation_) | |
353 SchedulePaint(); | |
354 else | |
355 views::LabelButton::AnimationProgressed(animation); | |
356 } | |
357 | |
358 void OriginChipView::AnimationEnded(const gfx::Animation* animation) { | |
359 if (animation == &fade_in_animation_) | |
360 fade_in_animation_.Reset(); | |
361 else | |
362 views::LabelButton::AnimationEnded(animation); | |
363 } | |
364 | |
365 void OriginChipView::OnPaintBorder(gfx::Canvas* canvas) { | |
366 if (fade_in_animation_.is_animating()) { | |
367 canvas->SaveLayerAlpha(static_cast<uint8>( | |
368 fade_in_animation_.CurrentValueBetween(0, 255))); | |
369 views::LabelButton::OnPaintBorder(canvas); | |
370 canvas->Restore(); | |
371 } else { | |
372 views::LabelButton::OnPaintBorder(canvas); | |
373 } | |
374 } | |
375 | |
376 void OriginChipView::StateChanged() { | |
377 DCHECK_LT(state(), 3); | |
378 SkColor background_color = background_colors_[state()]; | |
379 ev_label_->SetBackgroundColor(background_color); | |
380 host_label_->SetBackgroundColor(background_color); | |
381 } | |
382 | |
383 // TODO(gbillock): Make the LocationBarView or OmniboxView the listener for | |
384 // this button. | |
385 void OriginChipView::ButtonPressed(views::Button* sender, | |
386 const ui::Event& event) { | |
387 // See if the event needs to be passed to the LocationIconView. | |
388 if (event.IsMouseEvent() || (event.type() == ui::ET_GESTURE_TAP)) { | |
389 location_icon_view_->set_interactive(true); | |
390 const ui::LocatedEvent& located_event = | |
391 static_cast<const ui::LocatedEvent&>(event); | |
392 if (GetEventHandlerForPoint(located_event.location()) == | |
393 location_icon_view_) { | |
394 location_icon_view_->page_info_helper()->ProcessEvent(located_event); | |
395 location_icon_view_->set_interactive(false); | |
396 return; | |
397 } | |
398 location_icon_view_->set_interactive(false); | |
399 } | |
400 | |
401 UMA_HISTOGRAM_COUNTS("OriginChip.Pressed", 1); | |
402 content::RecordAction(base::UserMetricsAction("OriginChipPress")); | |
403 | |
404 location_bar_view_->ShowURL(); | |
405 } | |
406 | |
407 // Note: When OnSafeBrowsingHit would be called, OnSafeBrowsingMatch will | |
408 // have already been called. | |
409 void OriginChipView::OnSafeBrowsingHit( | |
410 const SafeBrowsingUIManager::UnsafeResource& resource) {} | |
411 | |
412 void OriginChipView::OnSafeBrowsingMatch( | |
413 const SafeBrowsingUIManager::UnsafeResource& resource) { | |
414 OnChanged(); | |
415 } | |
OLD | NEW |