Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / views / toolbar / origin_chip_view.cc
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/toolbar/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_icon_image.h"
13 #include "chrome/browser/extensions/extension_service.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/omnibox/omnibox_view.h"
23 #include "chrome/browser/ui/toolbar/origin_chip.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/browser/ui/views/toolbar/toolbar_view.h"
27 #include "chrome/common/extensions/extension_constants.h"
28 #include "chrome/common/extensions/manifest_handlers/icons_handler.h"
29 #include "content/public/browser/navigation_controller.h"
30 #include "content/public/browser/navigation_entry.h"
31 #include "content/public/browser/user_metrics.h"
32 #include "content/public/browser/web_contents.h"
33 #include "content/public/common/url_constants.h"
34 #include "extensions/browser/extension_system.h"
35 #include "extensions/common/constants.h"
36 #include "grit/generated_resources.h"
37 #include "grit/theme_resources.h"
38 #include "ui/base/l10n/l10n_util.h"
39 #include "ui/base/resource/resource_bundle.h"
40 #include "ui/base/theme_provider.h"
41 #include "ui/views/background.h"
42 #include "ui/views/button_drag_utils.h"
43 #include "ui/views/controls/button/label_button.h"
44 #include "ui/views/controls/button/label_button_border.h"
45 #include "ui/views/controls/label.h"
46 #include "ui/views/painter.h"
47
48
49 // OriginChipExtensionIcon ----------------------------------------------------
50
51 class OriginChipExtensionIcon : public extensions::IconImage::Observer {
52  public:
53   OriginChipExtensionIcon(LocationIconView* icon_view,
54                           Profile* profile,
55                           const extensions::Extension* extension);
56   virtual ~OriginChipExtensionIcon();
57
58   // IconImage::Observer:
59   virtual void OnExtensionIconImageChanged(
60       extensions::IconImage* image) OVERRIDE;
61
62  private:
63   LocationIconView* icon_view_;
64   scoped_ptr<extensions::IconImage> icon_image_;
65
66   DISALLOW_COPY_AND_ASSIGN(OriginChipExtensionIcon);
67 };
68
69 OriginChipExtensionIcon::OriginChipExtensionIcon(
70     LocationIconView* icon_view,
71     Profile* profile,
72     const extensions::Extension* extension)
73     : icon_view_(icon_view),
74       icon_image_(new extensions::IconImage(
75           profile,
76           extension,
77           extensions::IconsInfo::GetIcons(extension),
78           extension_misc::EXTENSION_ICON_BITTY,
79           extensions::IconsInfo::GetDefaultAppIcon(),
80           this)) {
81   // Forces load of the image.
82   icon_image_->image_skia().GetRepresentation(1.0f);
83
84   if (!icon_image_->image_skia().image_reps().empty())
85     OnExtensionIconImageChanged(icon_image_.get());
86 }
87
88 OriginChipExtensionIcon::~OriginChipExtensionIcon() {
89 }
90
91 void OriginChipExtensionIcon::OnExtensionIconImageChanged(
92     extensions::IconImage* image) {
93   if (icon_view_)
94     icon_view_->SetImage(&icon_image_->image_skia());
95 }
96
97
98 // OriginChipView -------------------------------------------------------------
99
100 namespace {
101
102 const int kEdgeThickness = 5;
103 const int k16x16IconLeadingSpacing = 1;
104 const int k16x16IconTrailingSpacing = 2;
105 const int kIconTextSpacing = 3;
106 const int kTrailingLabelMargin = 0;
107
108 const SkColor kEVBackgroundColor = SkColorSetRGB(163, 226, 120);
109 const SkColor kMalwareBackgroundColor = SkColorSetRGB(145, 0, 0);
110 const SkColor kBrokenSSLBackgroundColor = SkColorSetRGB(253, 196, 36);
111
112 }  // namespace
113
114 OriginChipView::OriginChipView(ToolbarView* toolbar_view)
115     : ToolbarButton(this, NULL),
116       toolbar_view_(toolbar_view),
117       painter_(NULL),
118       showing_16x16_icon_(false) {
119   scoped_refptr<SafeBrowsingService> sb_service =
120       g_browser_process->safe_browsing_service();
121   // May not be set for unit tests.
122   if (sb_service.get() && sb_service->ui_manager())
123     sb_service->ui_manager()->AddObserver(this);
124
125   set_drag_controller(this);
126 }
127
128 OriginChipView::~OriginChipView() {
129   scoped_refptr<SafeBrowsingService> sb_service =
130       g_browser_process->safe_browsing_service();
131   if (sb_service.get() && sb_service->ui_manager())
132     sb_service->ui_manager()->RemoveObserver(this);
133 }
134
135 void OriginChipView::Init() {
136   ToolbarButton::Init();
137   image()->EnableCanvasFlippingForRTLUI(false);
138
139   // TODO(gbillock): Would be nice to just use stock LabelButton stuff here.
140   location_icon_view_ = new LocationIconView(toolbar_view_->location_bar());
141   // Make location icon hover events count as hovering the origin chip.
142   location_icon_view_->set_interactive(false);
143
144   host_label_ = new views::Label();
145   ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
146   host_label_->SetFontList(rb.GetFontList(ui::ResourceBundle::MediumFont));
147
148   AddChildView(location_icon_view_);
149   AddChildView(host_label_);
150
151   location_icon_view_->SetImage(GetThemeProvider()->GetImageSkiaNamed(
152       IDR_LOCATION_BAR_HTTP));
153   location_icon_view_->ShowTooltip(true);
154
155   const int kEVBackgroundImages[] = IMAGE_GRID(IDR_SITE_CHIP_EV);
156   ev_background_painter_.reset(
157       views::Painter::CreateImageGridPainter(kEVBackgroundImages));
158   const int kBrokenSSLBackgroundImages[] = IMAGE_GRID(IDR_SITE_CHIP_BROKENSSL);
159   broken_ssl_background_painter_.reset(
160       views::Painter::CreateImageGridPainter(kBrokenSSLBackgroundImages));
161   const int kMalwareBackgroundImages[] = IMAGE_GRID(IDR_SITE_CHIP_MALWARE);
162   malware_background_painter_.reset(
163       views::Painter::CreateImageGridPainter(kMalwareBackgroundImages));
164 }
165
166 bool OriginChipView::ShouldShow() {
167   return chrome::ShouldDisplayOriginChip() ||
168       (toolbar_view_->GetToolbarModel()->WouldOmitURLDueToOriginChip() &&
169        toolbar_view_->GetToolbarModel()->origin_chip_enabled());
170 }
171
172 void OriginChipView::Update(content::WebContents* web_contents) {
173   if (!web_contents)
174     return;
175
176   // Note: security level can change async as the connection is made.
177   GURL url = toolbar_view_->GetToolbarModel()->GetURL();
178   const ToolbarModel::SecurityLevel security_level =
179       toolbar_view_->GetToolbarModel()->GetSecurityLevel(true);
180
181   bool url_malware = OriginChip::IsMalware(url, web_contents);
182
183   // TODO(gbillock): We persist a malware setting while a new WebContents
184   // content is loaded, meaning that we end up transiently marking a safe
185   // page as malware. Need to fix that.
186
187   if ((url == url_displayed_) &&
188       (security_level == security_level_) &&
189       (url_malware == url_malware_))
190     return;
191
192   url_displayed_ = url;
193   url_malware_ = url_malware;
194   security_level_ = security_level;
195
196   SkColor label_background =
197       GetThemeProvider()->GetColor(ThemeProperties::COLOR_TOOLBAR);
198   if (url_malware_) {
199     painter_ = malware_background_painter_.get();
200     label_background = kMalwareBackgroundColor;
201   } else if (security_level_ == ToolbarModel::SECURITY_ERROR) {
202     painter_ = broken_ssl_background_painter_.get();
203     label_background = kBrokenSSLBackgroundColor;
204   } else if (security_level_ == ToolbarModel::EV_SECURE) {
205     painter_ = ev_background_painter_.get();
206     label_background = kEVBackgroundColor;
207   } else {
208     painter_ = NULL;
209   }
210
211   base::string16 host =
212       OriginChip::LabelFromURLForProfile(url_displayed_,
213                                          toolbar_view_->browser()->profile());
214   if (security_level_ == ToolbarModel::EV_SECURE) {
215     host = l10n_util::GetStringFUTF16(IDS_SITE_CHIP_EV_SSL_LABEL,
216         toolbar_view_->GetToolbarModel()->GetEVCertName(),
217         host);
218   }
219
220   host_label_->SetText(host);
221   host_label_->SetTooltipText(host);
222   host_label_->SetBackgroundColor(label_background);
223   host_label_->SetElideBehavior(views::Label::NO_ELIDE);
224
225   int icon = toolbar_view_->GetToolbarModel()->GetIconForSecurityLevel(
226       security_level_);
227   showing_16x16_icon_ = false;
228
229   if (url_displayed_.is_empty() ||
230       url_displayed_.SchemeIs(content::kChromeUIScheme)) {
231     icon = IDR_PRODUCT_LOGO_16;
232     showing_16x16_icon_ = true;
233   }
234
235   location_icon_view_->SetImage(GetThemeProvider()->GetImageSkiaNamed(icon));
236
237   if (url_displayed_.SchemeIs(extensions::kExtensionScheme)) {
238     icon = IDR_EXTENSIONS_FAVICON;
239     showing_16x16_icon_ = true;
240     location_icon_view_->SetImage(GetThemeProvider()->GetImageSkiaNamed(icon));
241
242     ExtensionService* service =
243         extensions::ExtensionSystem::Get(
244             toolbar_view_->browser()->profile())->extension_service();
245     const extensions::Extension* extension =
246         service->extensions()->GetExtensionOrAppByURL(url_displayed_);
247     extension_icon_.reset(
248         new OriginChipExtensionIcon(location_icon_view_,
249                                     toolbar_view_->browser()->profile(),
250                                     extension));
251   } else {
252     extension_icon_.reset();
253   }
254
255   Layout();
256   SchedulePaint();
257 }
258
259 void OriginChipView::OnChanged() {
260   Update(toolbar_view_->GetWebContents());
261   toolbar_view_->Layout();
262   toolbar_view_->SchedulePaint();
263   // TODO(gbillock): Also need to potentially repaint infobars to make sure the
264   // arrows are pointing to the right spot. Only needed for some edge cases.
265 }
266
267 gfx::Size OriginChipView::GetPreferredSize() {
268   gfx::Size label_size = host_label_->GetPreferredSize();
269   gfx::Size icon_size = location_icon_view_->GetPreferredSize();
270   int icon_spacing = showing_16x16_icon_ ?
271       (k16x16IconLeadingSpacing + k16x16IconTrailingSpacing) : 0;
272   return gfx::Size(kEdgeThickness + icon_size.width() + icon_spacing +
273                    kIconTextSpacing + label_size.width() +
274                    kTrailingLabelMargin + kEdgeThickness,
275                    icon_size.height());
276 }
277
278 void OriginChipView::Layout() {
279   // TODO(gbillock): Eventually we almost certainly want to use
280   // LocationBarLayout for leading and trailing decorations.
281
282   location_icon_view_->SetBounds(
283       kEdgeThickness + (showing_16x16_icon_ ? k16x16IconLeadingSpacing : 0),
284       LocationBarView::kNormalEdgeThickness,
285       location_icon_view_->GetPreferredSize().width(),
286       height() - 2 * LocationBarView::kNormalEdgeThickness);
287
288   int host_label_x = location_icon_view_->x() + location_icon_view_->width() +
289       kIconTextSpacing;
290   host_label_x += showing_16x16_icon_ ? k16x16IconTrailingSpacing : 0;
291   int host_label_width =
292       width() - host_label_x - kEdgeThickness - kTrailingLabelMargin;
293   host_label_->SetBounds(host_label_x,
294                          LocationBarView::kNormalEdgeThickness,
295                          host_label_width,
296                          height() - 2 * LocationBarView::kNormalEdgeThickness);
297 }
298
299 void OriginChipView::OnPaint(gfx::Canvas* canvas) {
300   gfx::Rect rect(GetLocalBounds());
301   if (painter_)
302     views::Painter::PaintPainterAt(canvas, painter_, rect);
303
304   ToolbarButton::OnPaint(canvas);
305 }
306
307 int OriginChipView::ElideDomainTarget(int target_max_width) {
308   base::string16 host =
309       OriginChip::LabelFromURLForProfile(url_displayed_,
310                                          toolbar_view_->browser()->profile());
311   host_label_->SetText(host);
312   int width = GetPreferredSize().width();
313   if (width <= target_max_width)
314     return width;
315
316   gfx::Size label_size = host_label_->GetPreferredSize();
317   int padding_width = width - label_size.width();
318
319   host_label_->SetText(ElideHost(
320       toolbar_view_->GetToolbarModel()->GetURL(),
321       host_label_->font_list(), target_max_width - padding_width));
322   return GetPreferredSize().width();
323 }
324
325 // TODO(gbillock): Make the LocationBarView or OmniboxView the listener for
326 // this button.
327 void OriginChipView::ButtonPressed(views::Button* sender,
328                                  const ui::Event& event) {
329   // See if the event needs to be passed to the LocationIconView.
330   if (event.IsMouseEvent() || (event.type() == ui::ET_GESTURE_TAP)) {
331     location_icon_view_->set_interactive(true);
332     const ui::LocatedEvent& located_event =
333         static_cast<const ui::LocatedEvent&>(event);
334     if (GetEventHandlerForPoint(located_event.location()) ==
335             location_icon_view_) {
336       location_icon_view_->page_info_helper()->ProcessEvent(located_event);
337       location_icon_view_->set_interactive(false);
338       return;
339     }
340     location_icon_view_->set_interactive(false);
341   }
342
343   UMA_HISTOGRAM_COUNTS("OriginChip.Pressed", 1);
344   content::RecordAction(base::UserMetricsAction("OriginChipPress"));
345
346   toolbar_view_->location_bar()->GetOmniboxView()->SetFocus();
347   toolbar_view_->location_bar()->GetOmniboxView()->model()->
348       SetCaretVisibility(true);
349   toolbar_view_->location_bar()->GetOmniboxView()->ShowURL();
350 }
351
352 void OriginChipView::WriteDragDataForView(View* sender,
353                                         const gfx::Point& press_pt,
354                                         OSExchangeData* data) {
355   // TODO(gbillock): Consolidate this with the identical logic in
356   // LocationBarView.
357   content::WebContents* web_contents = toolbar_view_->GetWebContents();
358   FaviconTabHelper* favicon_tab_helper =
359       FaviconTabHelper::FromWebContents(web_contents);
360   gfx::ImageSkia favicon = favicon_tab_helper->GetFavicon().AsImageSkia();
361   button_drag_utils::SetURLAndDragImage(web_contents->GetURL(),
362                                         web_contents->GetTitle(),
363                                         favicon,
364                                         data,
365                                         sender->GetWidget());
366 }
367
368 int OriginChipView::GetDragOperationsForView(View* sender,
369                                            const gfx::Point& p) {
370   return ui::DragDropTypes::DRAG_COPY | ui::DragDropTypes::DRAG_LINK;
371 }
372
373 bool OriginChipView::CanStartDragForView(View* sender,
374                                        const gfx::Point& press_pt,
375                                        const gfx::Point& p) {
376   return true;
377 }
378
379 // Note: When OnSafeBrowsingHit would be called, OnSafeBrowsingMatch will
380 // have already been called.
381 void OriginChipView::OnSafeBrowsingHit(
382     const SafeBrowsingUIManager::UnsafeResource& resource) {}
383
384 void OriginChipView::OnSafeBrowsingMatch(
385     const SafeBrowsingUIManager::UnsafeResource& resource) {
386   OnChanged();
387 }