origin_chip_view.cc revision 6e8cce623b6e4fe0c9e4af605d675dd9d0338c38
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_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"
45
46
47// OriginChipExtensionIcon ----------------------------------------------------
48
49class OriginChipExtensionIcon : public extensions::IconImage::Observer {
50 public:
51  OriginChipExtensionIcon(LocationIconView* icon_view,
52                          Profile* profile,
53                          const extensions::Extension* extension);
54  virtual ~OriginChipExtensionIcon();
55
56  // IconImage::Observer:
57  virtual void OnExtensionIconImageChanged(
58      extensions::IconImage* image) OVERRIDE;
59
60 private:
61  LocationIconView* icon_view_;
62  scoped_ptr<extensions::IconImage> icon_image_;
63
64  DISALLOW_COPY_AND_ASSIGN(OriginChipExtensionIcon);
65};
66
67OriginChipExtensionIcon::OriginChipExtensionIcon(
68    LocationIconView* icon_view,
69    Profile* profile,
70    const extensions::Extension* extension)
71    : icon_view_(icon_view),
72      icon_image_(new extensions::IconImage(
73          profile,
74          extension,
75          extensions::IconsInfo::GetIcons(extension),
76          extension_misc::EXTENSION_ICON_BITTY,
77          extensions::util::GetDefaultAppIcon(),
78          this)) {
79  // Forces load of the image.
80  icon_image_->image_skia().GetRepresentation(1.0f);
81
82  if (!icon_image_->image_skia().image_reps().empty())
83    OnExtensionIconImageChanged(icon_image_.get());
84}
85
86OriginChipExtensionIcon::~OriginChipExtensionIcon() {
87}
88
89void OriginChipExtensionIcon::OnExtensionIconImageChanged(
90    extensions::IconImage* image) {
91  if (icon_view_)
92    icon_view_->SetImage(&icon_image_->image_skia());
93}
94
95
96// OriginChipView -------------------------------------------------------------
97
98namespace {
99
100const int kEdgeThickness = 5;
101const int k16x16IconLeadingSpacing = 1;
102const int k16x16IconTrailingSpacing = 2;
103const int kIconTextSpacing = 3;
104
105const 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)
109};
110
111const 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)
115};
116
117const 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)
121};
122
123const 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)
127};
128
129const extensions::Extension* GetExtension(const GURL& url, Profile* profile) {
130  if (!url.SchemeIs(extensions::kExtensionScheme))
131    return NULL;
132  ExtensionService* service =
133      extensions::ExtensionSystem::Get(profile)->extension_service();
134  return service->extensions()->GetExtensionOrAppByURL(url);
135}
136
137}  // namespace
138
139OriginChipView::OriginChipView(LocationBarView* location_bar_view,
140                               Profile* profile,
141                               const gfx::FontList& font_list)
142    : LabelButton(this, base::string16()),
143      location_bar_view_(location_bar_view),
144      profile_(profile),
145      showing_16x16_icon_(false),
146      fade_in_animation_(this),
147      security_level_(ToolbarModel::NONE),
148      url_malware_(false) {
149  EnableCanvasFlippingForRTLUI(true);
150
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);
156
157  SetFontList(font_list);
158
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_);
165
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_);
170
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_);
175
176  fade_in_animation_.SetTweenType(gfx::Tween::LINEAR_OUT_SLOW_IN);
177  fade_in_animation_.SetSlideDuration(175);
178
179  // Ensure |pressed_text_color_| and |background_colors_| are initialized.
180  SetBorderImages(kNormalImages);
181}
182
183OriginChipView::~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);
188}
189
190void OriginChipView::OnChanged() {
191  content::WebContents* web_contents = location_bar_view_->GetWebContents();
192  if (!web_contents)
193    return;
194
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);
199
200  bool url_malware = OriginChip::IsMalware(url, web_contents);
201
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.
205
206  if ((url == url_displayed_) &&
207      (security_level == security_level_) &&
208      (url_malware == url_malware_))
209    return;
210
211  url_displayed_ = url;
212  url_malware_ = url_malware;
213  security_level_ = security_level;
214
215  if (url_malware_) {
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);
221  } else {
222    SetBorderImages(kNormalImages);
223  }
224
225  ev_label_->SetText(location_bar_view_->GetToolbarModel()->GetEVCertName());
226  ev_label_->SetVisible(security_level_ == ToolbarModel::EV_SECURE);
227
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()));
233
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(
238          security_level_);
239  const extensions::Extension* extension =
240      GetExtension(url_displayed_, profile_);
241  if (extension) {
242    icon = IDR_EXTENSIONS_FAVICON;
243    showing_16x16_icon_ = true;
244    extension_icon_.reset(
245        new OriginChipExtensionIcon(location_icon_view_, profile_, extension));
246  } else {
247    extension_icon_.reset();
248  }
249  location_icon_view_->SetImage(GetThemeProvider()->GetImageSkiaNamed(icon));
250
251  if (visible()) {
252    CancelFade();
253    Layout();
254    SchedulePaint();
255  }
256}
257
258void OriginChipView::FadeIn() {
259  fade_in_animation_.Show();
260}
261
262void OriginChipView::CancelFade() {
263  fade_in_animation_.Stop();
264}
265
266int OriginChipView::HostLabelOffset() const {
267  return host_label_->x() - GetLabelX();
268}
269
270int OriginChipView::WidthFromStartOfLabels() const {
271  return width() - GetLabelX();
272}
273
274gfx::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());
281  }
282  return gfx::Size(GetLabelX() + label_size + kEdgeThickness,
283                   location_icon_view_->GetPreferredSize().height());
284}
285
286void OriginChipView::Layout() {
287  // TODO(gbillock): Eventually we almost certainly want to use
288  // LocationBarLayout for leading and trailing decorations.
289
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);
295
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()) {
301    int ev_label_width =
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().
305    ev_label_width +=
306        gfx::GetStringWidth(base::ASCIIToUTF16(" "), GetFontList());
307    label_x += ev_label_width;
308    label_width = std::max(0, label_width - ev_label_width);
309  }
310  host_label_->SetBounds(label_x, label_y, label_width, label_height);
311}
312
313int 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;
318}
319
320void OriginChipView::SetBorderImages(const int images[3][9]) {
321  scoped_ptr<views::LabelButtonBorder> border(
322      new views::LabelButtonBorder(views::Button::STYLE_BUTTON));
323
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);
327
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
333    // images).
334    //
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);
344  }
345
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()]);
350
351  SetBorder(border.PassAs<views::Border>());
352}
353
354void OriginChipView::AnimationProgressed(const gfx::Animation* animation) {
355  if (animation == &fade_in_animation_)
356    SchedulePaint();
357  else
358    views::LabelButton::AnimationProgressed(animation);
359}
360
361void OriginChipView::AnimationEnded(const gfx::Animation* animation) {
362  if (animation == &fade_in_animation_)
363    fade_in_animation_.Reset();
364  else
365    views::LabelButton::AnimationEnded(animation);
366}
367
368void 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);
373    canvas->Restore();
374  } else {
375    views::LabelButton::OnPaintBorder(canvas);
376  }
377}
378
379void 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);
384}
385
386// TODO(gbillock): Make the LocationBarView or OmniboxView the listener for
387// this button.
388void 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);
399      return;
400    }
401    location_icon_view_->set_interactive(false);
402  }
403
404  UMA_HISTOGRAM_COUNTS("OriginChip.Pressed", 1);
405  content::RecordAction(base::UserMetricsAction("OriginChipPress"));
406
407  location_bar_view_->ShowURL();
408}
409
410// Note: When OnSafeBrowsingHit would be called, OnSafeBrowsingMatch will
411// have already been called.
412void OriginChipView::OnSafeBrowsingHit(
413    const SafeBrowsingUIManager::UnsafeResource& resource) {}
414
415void OriginChipView::OnSafeBrowsingMatch(
416    const SafeBrowsingUIManager::UnsafeResource& resource) {
417  OnChanged();
418}
419