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