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.
5 #include "chrome/browser/ui/views/location_bar/origin_chip_view.h"
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_service.h"
13 #include "chrome/browser/extensions/extension_util.h"
14 #include "chrome/browser/favicon/favicon_tab_helper.h"
15 #include "chrome/browser/profiles/profile.h"
16 #include "chrome/browser/safe_browsing/safe_browsing_service.h"
17 #include "chrome/browser/safe_browsing/ui_manager.h"
18 #include "chrome/browser/search/search.h"
19 #include "chrome/browser/themes/theme_properties.h"
20 #include "chrome/browser/ui/browser.h"
21 #include "chrome/browser/ui/elide_url.h"
22 #include "chrome/browser/ui/location_bar/origin_chip_info.h"
23 #include "chrome/browser/ui/omnibox/omnibox_view.h"
24 #include "chrome/browser/ui/toolbar/toolbar_model.h"
25 #include "chrome/browser/ui/views/location_bar/location_bar_view.h"
26 #include "chrome/common/extensions/extension_constants.h"
27 #include "content/public/browser/user_metrics.h"
28 #include "content/public/browser/web_contents.h"
29 #include "content/public/common/url_constants.h"
30 #include "extensions/browser/extension_icon_image.h"
31 #include "extensions/browser/extension_system.h"
32 #include "extensions/common/constants.h"
33 #include "extensions/common/manifest_handlers/icons_handler.h"
34 #include "grit/generated_resources.h"
35 #include "grit/theme_resources.h"
36 #include "ui/base/resource/resource_bundle.h"
37 #include "ui/base/theme_provider.h"
38 #include "ui/gfx/canvas.h"
39 #include "ui/gfx/font_list.h"
40 #include "ui/gfx/text_utils.h"
41 #include "ui/views/background.h"
42 #include "ui/views/controls/button/label_button.h"
43 #include "ui/views/controls/button/label_button_border.h"
44 #include "ui/views/painter.h"
47 // OriginChipExtensionIcon ----------------------------------------------------
49 class OriginChipExtensionIcon : public extensions::IconImage::Observer {
51 OriginChipExtensionIcon(LocationIconView* icon_view,
53 const extensions::Extension* extension);
54 virtual ~OriginChipExtensionIcon();
56 // IconImage::Observer:
57 virtual void OnExtensionIconImageChanged(
58 extensions::IconImage* image) OVERRIDE;
61 LocationIconView* icon_view_;
62 scoped_ptr<extensions::IconImage> icon_image_;
64 DISALLOW_COPY_AND_ASSIGN(OriginChipExtensionIcon);
67 OriginChipExtensionIcon::OriginChipExtensionIcon(
68 LocationIconView* icon_view,
70 const extensions::Extension* extension)
71 : icon_view_(icon_view),
72 icon_image_(new extensions::IconImage(
75 extensions::IconsInfo::GetIcons(extension),
76 extension_misc::EXTENSION_ICON_BITTY,
77 extensions::util::GetDefaultAppIcon(),
79 // Forces load of the image.
80 icon_image_->image_skia().GetRepresentation(1.0f);
82 if (!icon_image_->image_skia().image_reps().empty())
83 OnExtensionIconImageChanged(icon_image_.get());
86 OriginChipExtensionIcon::~OriginChipExtensionIcon() {
89 void OriginChipExtensionIcon::OnExtensionIconImageChanged(
90 extensions::IconImage* image) {
92 icon_view_->SetImage(&icon_image_->image_skia());
96 // OriginChipView -------------------------------------------------------------
100 const int kEdgeThickness = 5;
101 const int k16x16IconLeadingSpacing = 1;
102 const int k16x16IconTrailingSpacing = 2;
103 const int kIconTextSpacing = 3;
105 const int kNormalImages[3][9] = {
106 IMAGE_GRID(IDR_ORIGIN_CHIP_NORMAL),
107 IMAGE_GRID(IDR_ORIGIN_CHIP_HOVER),
108 IMAGE_GRID(IDR_ORIGIN_CHIP_PRESSED)
111 const int kMalwareImages[3][9] = {
112 IMAGE_GRID(IDR_ORIGIN_CHIP_MALWARE_NORMAL),
113 IMAGE_GRID(IDR_ORIGIN_CHIP_MALWARE_HOVER),
114 IMAGE_GRID(IDR_ORIGIN_CHIP_MALWARE_PRESSED)
117 const int kBrokenSSLImages[3][9] = {
118 IMAGE_GRID(IDR_ORIGIN_CHIP_BROKENSSL_NORMAL),
119 IMAGE_GRID(IDR_ORIGIN_CHIP_BROKENSSL_HOVER),
120 IMAGE_GRID(IDR_ORIGIN_CHIP_BROKENSSL_PRESSED)
123 const int kEVImages[3][9] = {
124 IMAGE_GRID(IDR_ORIGIN_CHIP_EV_NORMAL),
125 IMAGE_GRID(IDR_ORIGIN_CHIP_EV_HOVER),
126 IMAGE_GRID(IDR_ORIGIN_CHIP_EV_PRESSED)
129 const extensions::Extension* GetExtension(const GURL& url, Profile* profile) {
130 if (!url.SchemeIs(extensions::kExtensionScheme))
132 ExtensionService* service =
133 extensions::ExtensionSystem::Get(profile)->extension_service();
134 return service->extensions()->GetExtensionOrAppByURL(url);
139 OriginChipView::OriginChipView(LocationBarView* location_bar_view,
141 const gfx::FontList& font_list)
142 : LabelButton(this, base::string16()),
143 location_bar_view_(location_bar_view),
145 showing_16x16_icon_(false),
146 fade_in_animation_(this),
147 security_level_(ToolbarModel::NONE),
148 url_malware_(false) {
149 EnableCanvasFlippingForRTLUI(true);
151 scoped_refptr<SafeBrowsingService> sb_service =
152 g_browser_process->safe_browsing_service();
153 // |sb_service| may be NULL in tests.
154 if (sb_service && sb_service->ui_manager())
155 sb_service->ui_manager()->AddObserver(this);
157 SetFontList(font_list);
159 // TODO(gbillock): Would be nice to just use stock LabelButton stuff here.
160 location_icon_view_ = new LocationIconView(location_bar_view_);
161 // Make location icon hover events count as hovering the origin chip.
162 location_icon_view_->set_interactive(false);
163 location_icon_view_->ShowTooltip(true);
164 AddChildView(location_icon_view_);
166 ev_label_ = new views::Label(base::string16(), GetFontList());
167 ev_label_->SetHorizontalAlignment(gfx::ALIGN_LEFT);
168 ev_label_->SetElideBehavior(gfx::NO_ELIDE);
169 AddChildView(ev_label_);
171 host_label_ = new views::Label(base::string16(), GetFontList());
172 host_label_->SetHorizontalAlignment(gfx::ALIGN_LEFT);
173 host_label_->SetElideBehavior(gfx::NO_ELIDE);
174 AddChildView(host_label_);
176 fade_in_animation_.SetTweenType(gfx::Tween::LINEAR_OUT_SLOW_IN);
177 fade_in_animation_.SetSlideDuration(175);
179 // Ensure |pressed_text_color_| and |background_colors_| are initialized.
180 SetBorderImages(kNormalImages);
183 OriginChipView::~OriginChipView() {
184 scoped_refptr<SafeBrowsingService> sb_service =
185 g_browser_process->safe_browsing_service();
186 if (sb_service.get() && sb_service->ui_manager())
187 sb_service->ui_manager()->RemoveObserver(this);
190 void OriginChipView::OnChanged() {
191 content::WebContents* web_contents = location_bar_view_->GetWebContents();
195 // Note: security level can change async as the connection is made.
196 GURL url = location_bar_view_->GetToolbarModel()->GetURL();
197 const ToolbarModel::SecurityLevel security_level =
198 location_bar_view_->GetToolbarModel()->GetSecurityLevel(true);
200 bool url_malware = OriginChip::IsMalware(url, web_contents);
202 // TODO(gbillock): We persist a malware setting while a new WebContents
203 // content is loaded, meaning that we end up transiently marking a safe
204 // page as malware. Need to fix that.
206 if ((url == url_displayed_) &&
207 (security_level == security_level_) &&
208 (url_malware == url_malware_))
211 url_displayed_ = url;
212 url_malware_ = url_malware;
213 security_level_ = security_level;
216 SetBorderImages(kMalwareImages);
217 } else if (security_level_ == ToolbarModel::SECURITY_ERROR) {
218 SetBorderImages(kBrokenSSLImages);
219 } else if (security_level_ == ToolbarModel::EV_SECURE) {
220 SetBorderImages(kEVImages);
222 SetBorderImages(kNormalImages);
225 ev_label_->SetText(location_bar_view_->GetToolbarModel()->GetEVCertName());
226 ev_label_->SetVisible(security_level_ == ToolbarModel::EV_SECURE);
228 // TODO(pkasting): Allow the origin chip to shrink, and use ElideHost().
229 base::string16 host =
230 OriginChip::LabelFromURLForProfile(url_displayed_, profile_);
231 host_label_->SetText(host);
232 host_label_->SetTooltipText(base::UTF8ToUTF16(url.spec()));
234 showing_16x16_icon_ = url_displayed_.is_empty() ||
235 url_displayed_.SchemeIs(content::kChromeUIScheme);
236 int icon = showing_16x16_icon_ ? IDR_PRODUCT_LOGO_16 :
237 location_bar_view_->GetToolbarModel()->GetIconForSecurityLevel(
239 const extensions::Extension* extension =
240 GetExtension(url_displayed_, profile_);
242 icon = IDR_EXTENSIONS_FAVICON;
243 showing_16x16_icon_ = true;
244 extension_icon_.reset(
245 new OriginChipExtensionIcon(location_icon_view_, profile_, extension));
247 extension_icon_.reset();
249 location_icon_view_->SetImage(GetThemeProvider()->GetImageSkiaNamed(icon));
258 void OriginChipView::FadeIn() {
259 fade_in_animation_.Show();
262 void OriginChipView::CancelFade() {
263 fade_in_animation_.Stop();
266 int OriginChipView::HostLabelOffset() const {
267 return host_label_->x() - GetLabelX();
270 int OriginChipView::WidthFromStartOfLabels() const {
271 return width() - GetLabelX();
274 gfx::Size OriginChipView::GetPreferredSize() const {
275 // TODO(pkasting): Use of " " here is a horrible hack, to be replaced by
276 // splitting the chip into separate pieces for EV/host.
277 int label_size = host_label_->GetPreferredSize().width();
278 if (ev_label_->visible()) {
279 label_size += ev_label_->GetPreferredSize().width() +
280 gfx::GetStringWidth(base::ASCIIToUTF16(" "), GetFontList());
282 return gfx::Size(GetLabelX() + label_size + kEdgeThickness,
283 location_icon_view_->GetPreferredSize().height());
286 void OriginChipView::Layout() {
287 // TODO(gbillock): Eventually we almost certainly want to use
288 // LocationBarLayout for leading and trailing decorations.
290 location_icon_view_->SetBounds(
291 kEdgeThickness + (showing_16x16_icon_ ? k16x16IconLeadingSpacing : 0),
292 LocationBarView::kNormalEdgeThickness,
293 location_icon_view_->GetPreferredSize().width(),
294 height() - 2 * LocationBarView::kNormalEdgeThickness);
296 int label_x = GetLabelX();
297 int label_width = std::max(0, width() - label_x - kEdgeThickness);
298 const int label_y = LocationBarView::kNormalEdgeThickness;
299 const int label_height = height() - 2 * LocationBarView::kNormalEdgeThickness;
300 if (ev_label_->visible()) {
302 std::min(ev_label_->GetPreferredSize().width(), label_width);
303 ev_label_->SetBounds(label_x, label_y, ev_label_width, label_height);
304 // TODO(pkasting): See comments in GetPreferredSize().
306 gfx::GetStringWidth(base::ASCIIToUTF16(" "), GetFontList());
307 label_x += ev_label_width;
308 label_width = std::max(0, label_width - ev_label_width);
310 host_label_->SetBounds(label_x, label_y, label_width, label_height);
313 int OriginChipView::GetLabelX() const {
314 const int icon_spacing = showing_16x16_icon_ ?
315 (k16x16IconLeadingSpacing + k16x16IconTrailingSpacing) : 0;
316 return kEdgeThickness + location_icon_view_->GetPreferredSize().width() +
317 icon_spacing + kIconTextSpacing;
320 void OriginChipView::SetBorderImages(const int images[3][9]) {
321 scoped_ptr<views::LabelButtonBorder> border(
322 new views::LabelButtonBorder(views::Button::STYLE_BUTTON));
324 for (size_t i = 0; i < 3; ++i) {
325 views::Painter* painter = views::Painter::CreateImageGridPainter(images[i]);
326 border->SetPainter(false, static_cast<Button::ButtonState>(i), painter);
328 // Calculate a representative background color of the provided image grid to
329 // use as the background color of the host label in order to color the text
330 // appropriately. We grab the color of the middle pixel of the middle image
331 // of the background, which we treat as the representative color of the
332 // entire background (reasonable, given the current appearance of these
335 // NOTE: Because this is called from the constructor, when we're not in a
336 // Widget yet, GetThemeProvider() may return NULL, so use the location bar's
337 // theme provider instead to be safe.
338 const SkBitmap& bitmap(
339 location_bar_view_->GetThemeProvider()->GetImageSkiaNamed(
340 images[i][4])->GetRepresentation(1.0f).sk_bitmap());
341 SkAutoLockPixels pixel_lock(bitmap);
342 background_colors_[i] =
343 bitmap.getColor(bitmap.width() / 2, bitmap.height() / 2);
346 // Calculate the actual text color of the pressed label.
347 host_label_->SetBackgroundColor(background_colors_[Button::STATE_PRESSED]);
348 pressed_text_color_ = host_label_->enabled_color();
349 host_label_->SetBackgroundColor(background_colors_[state()]);
351 SetBorder(border.PassAs<views::Border>());
354 void OriginChipView::AnimationProgressed(const gfx::Animation* animation) {
355 if (animation == &fade_in_animation_)
358 views::LabelButton::AnimationProgressed(animation);
361 void OriginChipView::AnimationEnded(const gfx::Animation* animation) {
362 if (animation == &fade_in_animation_)
363 fade_in_animation_.Reset();
365 views::LabelButton::AnimationEnded(animation);
368 void OriginChipView::OnPaintBorder(gfx::Canvas* canvas) {
369 if (fade_in_animation_.is_animating()) {
370 canvas->SaveLayerAlpha(static_cast<uint8>(
371 fade_in_animation_.CurrentValueBetween(0, 255)));
372 views::LabelButton::OnPaintBorder(canvas);
375 views::LabelButton::OnPaintBorder(canvas);
379 void OriginChipView::StateChanged() {
380 DCHECK_LT(state(), 3);
381 SkColor background_color = background_colors_[state()];
382 ev_label_->SetBackgroundColor(background_color);
383 host_label_->SetBackgroundColor(background_color);
386 // TODO(gbillock): Make the LocationBarView or OmniboxView the listener for
388 void OriginChipView::ButtonPressed(views::Button* sender,
389 const ui::Event& event) {
390 // See if the event needs to be passed to the LocationIconView.
391 if (event.IsMouseEvent() || (event.type() == ui::ET_GESTURE_TAP)) {
392 location_icon_view_->set_interactive(true);
393 const ui::LocatedEvent& located_event =
394 static_cast<const ui::LocatedEvent&>(event);
395 if (GetEventHandlerForPoint(located_event.location()) ==
396 location_icon_view_) {
397 location_icon_view_->page_info_helper()->ProcessEvent(located_event);
398 location_icon_view_->set_interactive(false);
401 location_icon_view_->set_interactive(false);
404 UMA_HISTOGRAM_COUNTS("OriginChip.Pressed", 1);
405 content::RecordAction(base::UserMetricsAction("OriginChipPress"));
407 location_bar_view_->ShowURL();
410 // Note: When OnSafeBrowsingHit would be called, OnSafeBrowsingMatch will
411 // have already been called.
412 void OriginChipView::OnSafeBrowsingHit(
413 const SafeBrowsingUIManager::UnsafeResource& resource) {}
415 void OriginChipView::OnSafeBrowsingMatch(
416 const SafeBrowsingUIManager::UnsafeResource& resource) {