1 // Copyright 2013 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/toolbar/site_chip_view.h"
7 #include "base/files/file_path.h"
8 #include "base/metrics/histogram.h"
9 #include "base/prefs/pref_service.h"
10 #include "base/strings/string_util.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "chrome/browser/browser_process.h"
13 #include "chrome/browser/extensions/extension_icon_image.h"
14 #include "chrome/browser/extensions/extension_service.h"
15 #include "chrome/browser/extensions/extension_system.h"
16 #include "chrome/browser/favicon/favicon_tab_helper.h"
17 #include "chrome/browser/profiles/profile.h"
18 #include "chrome/browser/safe_browsing/client_side_detection_host.h"
19 #include "chrome/browser/safe_browsing/safe_browsing_service.h"
20 #include "chrome/browser/safe_browsing/safe_browsing_tab_observer.h"
21 #include "chrome/browser/safe_browsing/ui_manager.h"
22 #include "chrome/browser/search/search.h"
23 #include "chrome/browser/themes/theme_properties.h"
24 #include "chrome/browser/ui/browser.h"
25 #include "chrome/browser/ui/omnibox/omnibox_view.h"
26 #include "chrome/browser/ui/toolbar/toolbar_model.h"
27 #include "chrome/browser/ui/views/location_bar/location_bar_view.h"
28 #include "chrome/browser/ui/views/toolbar/toolbar_view.h"
29 #include "chrome/common/extensions/extension_constants.h"
30 #include "chrome/common/extensions/manifest_handlers/icons_handler.h"
31 #include "chrome/common/pref_names.h"
32 #include "chrome/common/url_constants.h"
33 #include "content/public/browser/navigation_controller.h"
34 #include "content/public/browser/navigation_entry.h"
35 #include "content/public/browser/user_metrics.h"
36 #include "content/public/browser/web_contents.h"
37 #include "content/public/common/url_constants.h"
38 #include "extensions/common/constants.h"
39 #include "grit/component_strings.h"
40 #include "grit/generated_resources.h"
41 #include "grit/theme_resources.h"
42 #include "net/base/net_util.h"
43 #include "ui/base/l10n/l10n_util.h"
44 #include "ui/base/resource/resource_bundle.h"
45 #include "ui/base/theme_provider.h"
46 #include "ui/views/background.h"
47 #include "ui/views/button_drag_utils.h"
48 #include "ui/views/controls/button/label_button.h"
49 #include "ui/views/controls/button/label_button_border.h"
50 #include "ui/views/controls/label.h"
51 #include "ui/views/painter.h"
54 // SiteChipExtensionIcon ------------------------------------------------------
56 class SiteChipExtensionIcon : public extensions::IconImage::Observer {
58 SiteChipExtensionIcon(LocationIconView* icon_view,
60 const extensions::Extension* extension);
61 virtual ~SiteChipExtensionIcon();
63 // IconImage::Observer:
64 virtual void OnExtensionIconImageChanged(
65 extensions::IconImage* image) OVERRIDE;
68 LocationIconView* icon_view_;
69 scoped_ptr<extensions::IconImage> icon_image_;
71 DISALLOW_COPY_AND_ASSIGN(SiteChipExtensionIcon);
74 SiteChipExtensionIcon::SiteChipExtensionIcon(
75 LocationIconView* icon_view,
77 const extensions::Extension* extension)
78 : icon_view_(icon_view),
79 icon_image_(new extensions::IconImage(
82 extensions::IconsInfo::GetIcons(extension),
83 extension_misc::EXTENSION_ICON_BITTY,
84 extensions::IconsInfo::GetDefaultAppIcon(),
86 // Forces load of the image.
87 icon_image_->image_skia().GetRepresentation(1.0f);
89 if (!icon_image_->image_skia().image_reps().empty())
90 OnExtensionIconImageChanged(icon_image_.get());
93 SiteChipExtensionIcon::~SiteChipExtensionIcon() {
96 void SiteChipExtensionIcon::OnExtensionIconImageChanged(
97 extensions::IconImage* image) {
99 icon_view_->SetImage(&icon_image_->image_skia());
103 // SiteChipView ---------------------------------------------------------------
107 const int kEdgeThickness = 5;
108 const int k16x16IconLeadingSpacing = 1;
109 const int k16x16IconTrailingSpacing = 2;
110 const int kIconTextSpacing = 3;
111 const int kTrailingLabelMargin = 0;
113 const SkColor kEVBackgroundColor = SkColorSetRGB(163, 226, 120);
114 const SkColor kMalwareBackgroundColor = SkColorSetRGB(145, 0, 0);
115 const SkColor kBrokenSSLBackgroundColor = SkColorSetRGB(253, 196, 36);
117 // Detect client-side or SB malware/phishing hits.
118 bool IsMalware(const GURL& url, content::WebContents* tab) {
119 if (tab->GetURL() != url)
122 safe_browsing::SafeBrowsingTabObserver* sb_observer =
123 safe_browsing::SafeBrowsingTabObserver::FromWebContents(tab);
124 return sb_observer && sb_observer->detection_host() &&
125 sb_observer->detection_host()->DidPageReceiveSafeBrowsingMatch();
128 // For selected kChromeUIScheme and kAboutScheme, return the string resource
129 // number for the title of the page. If we don't have a specialized title,
131 int StringForChromeHost(const GURL& url) {
132 DCHECK(url.is_empty() || url.SchemeIs(chrome::kChromeUIScheme));
135 return IDS_NEW_TAB_TITLE;
137 // TODO(gbillock): Just get the page title and special case exceptions?
138 std::string host = url.host();
139 if (host == chrome::kChromeUIAppLauncherPageHost)
140 return IDS_APP_DEFAULT_PAGE_NAME;
141 if (host == chrome::kChromeUIBookmarksHost)
142 return IDS_BOOKMARK_MANAGER_TITLE;
143 if (host == chrome::kChromeUIComponentsHost)
144 return IDS_COMPONENTS_TITLE;
145 if (host == chrome::kChromeUICrashesHost)
146 return IDS_CRASHES_TITLE;
147 if (host == chrome::kChromeUIDevicesHost)
148 return IDS_LOCAL_DISCOVERY_DEVICES_PAGE_TITLE;
149 if (host == chrome::kChromeUIDownloadsHost)
150 return IDS_DOWNLOAD_TITLE;
151 if (host == chrome::kChromeUIExtensionsHost)
152 return IDS_MANAGE_EXTENSIONS_SETTING_WINDOWS_TITLE;
153 if (host == chrome::kChromeUIHelpHost)
154 return IDS_ABOUT_TAB_TITLE;
155 if (host == chrome::kChromeUIHistoryHost)
156 return IDS_HISTORY_TITLE;
157 if (host == chrome::kChromeUINewTabHost)
158 return IDS_NEW_TAB_TITLE;
159 if (host == chrome::kChromeUIPluginsHost)
160 return IDS_PLUGINS_TITLE;
161 if (host == chrome::kChromeUIPolicyHost)
162 return IDS_POLICY_TITLE;
163 if (host == chrome::kChromeUIPrintHost)
164 return IDS_PRINT_PREVIEW_TITLE;
165 if (host == chrome::kChromeUISettingsHost)
166 return IDS_SETTINGS_TITLE;
167 if (host == chrome::kChromeUIVersionHost)
168 return IDS_ABOUT_VERSION_TITLE;
175 base::string16 SiteChipView::SiteLabelFromURL(const GURL& provided_url) {
176 // First, strip view-source: if it appears. Note that GetContent removes
177 // "view-source:" but leaves the original scheme (http, https, ftp, etc).
178 GURL url(provided_url);
179 if (url.SchemeIs(content::kViewSourceScheme))
180 url = GURL(url.GetContent());
182 // About scheme pages. Currently all about: URLs other than about:blank
183 // redirect to chrome: URLs, so this only affects about:blank.
184 if (url.SchemeIs(chrome::kAboutScheme))
185 return base::UTF8ToUTF16(url.spec());
187 // Chrome built-in pages.
188 if (url.is_empty() || url.SchemeIs(chrome::kChromeUIScheme)) {
189 int string_ref = StringForChromeHost(url);
190 if (string_ref == -1)
191 return base::UTF8ToUTF16("Chrome");
192 return l10n_util::GetStringUTF16(string_ref);
195 Profile* profile = toolbar_view_->browser()->profile();
197 // For chrome-extension URLs, return the extension name.
198 if (url.SchemeIs(extensions::kExtensionScheme)) {
199 ExtensionService* service =
200 extensions::ExtensionSystem::Get(profile)->extension_service();
201 const extensions::Extension* extension =
202 service->extensions()->GetExtensionOrAppByURL(url);
204 base::UTF8ToUTF16(extension->name()) : base::UTF8ToUTF16(url.host());
207 if (url.SchemeIsHTTPOrHTTPS() || url.SchemeIs(content::kFtpScheme)) {
208 // See ToolbarModelImpl::GetText(). Does not pay attention to any user
209 // edits, and uses GetURL/net::FormatUrl -- We don't really care about
210 // length or the autocomplete parser.
211 // TODO(gbillock): This uses an algorithm very similar to GetText, which
212 // is probably too conservative. Try out just using a simpler mechanism of
213 // StripWWW() and IDNToUnicode().
214 std::string languages;
216 languages = profile->GetPrefs()->GetString(prefs::kAcceptLanguages);
218 base::string16 formatted = net::FormatUrl(url.GetOrigin(), languages,
219 net::kFormatUrlOmitAll, net::UnescapeRule::NORMAL, NULL, NULL, NULL);
220 // Remove scheme, "www.", and trailing "/".
221 if (StartsWith(formatted, base::ASCIIToUTF16("http://"), false))
222 formatted = formatted.substr(7);
223 else if (StartsWith(formatted, base::ASCIIToUTF16("https://"), false))
224 formatted = formatted.substr(8);
225 else if (StartsWith(formatted, base::ASCIIToUTF16("ftp://"), false))
226 formatted = formatted.substr(6);
227 if (StartsWith(formatted, base::ASCIIToUTF16("www."), false))
228 formatted = formatted.substr(4);
229 if (EndsWith(formatted, base::ASCIIToUTF16("/"), false))
230 formatted = formatted.substr(0, formatted.size()-1);
234 // These internal-ish debugging-style schemes we don't expect users
235 // to see. In these cases, the site chip will display the first
236 // part of the full URL.
237 if (url.SchemeIs(chrome::kBlobScheme) ||
238 url.SchemeIs(chrome::kChromeDevToolsScheme) ||
239 url.SchemeIs(chrome::kChromeNativeScheme) ||
240 url.SchemeIs(content::kDataScheme) ||
241 url.SchemeIs(content::kFileScheme) ||
242 url.SchemeIs(content::kFileSystemScheme) ||
243 url.SchemeIs(content::kGuestScheme) ||
244 url.SchemeIs(content::kJavaScriptScheme) ||
245 url.SchemeIs(content::kMailToScheme) ||
246 url.SchemeIs(content::kMetadataScheme) ||
247 url.SchemeIs(content::kSwappedOutScheme)) {
248 std::string truncated_url;
249 base::TruncateUTF8ToByteSize(url.spec(), 1000, &truncated_url);
250 return base::UTF8ToUTF16(truncated_url);
253 #if defined(OS_CHROMEOS)
254 if (url.SchemeIs(chrome::kCrosScheme) ||
255 url.SchemeIs(chrome::kDriveScheme)) {
256 return base::UTF8ToUTF16(url.spec());
260 // If all else fails, return hostname.
261 return base::UTF8ToUTF16(url.host());
264 SiteChipView::SiteChipView(ToolbarView* toolbar_view)
265 : ToolbarButton(this, NULL),
266 toolbar_view_(toolbar_view),
268 showing_16x16_icon_(false) {
269 scoped_refptr<SafeBrowsingService> sb_service =
270 g_browser_process->safe_browsing_service();
271 // May not be set for unit tests.
272 if (sb_service.get() && sb_service->ui_manager())
273 sb_service->ui_manager()->AddObserver(this);
275 set_drag_controller(this);
278 SiteChipView::~SiteChipView() {
279 scoped_refptr<SafeBrowsingService> sb_service =
280 g_browser_process->safe_browsing_service();
281 if (sb_service.get() && sb_service->ui_manager())
282 sb_service->ui_manager()->RemoveObserver(this);
285 void SiteChipView::Init() {
286 ToolbarButton::Init();
287 image()->EnableCanvasFlippingForRTLUI(false);
289 // TODO(gbillock): Would be nice to just use stock LabelButton stuff here.
290 location_icon_view_ = new LocationIconView(toolbar_view_->location_bar());
291 // Make location icon hover events count as hovering the site chip.
292 location_icon_view_->set_interactive(false);
294 host_label_ = new views::Label();
295 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
296 host_label_->SetFontList(rb.GetFontList(ui::ResourceBundle::MediumFont));
298 AddChildView(location_icon_view_);
299 AddChildView(host_label_);
301 location_icon_view_->SetImage(GetThemeProvider()->GetImageSkiaNamed(
302 IDR_LOCATION_BAR_HTTP));
303 location_icon_view_->ShowTooltip(true);
305 const int kEVBackgroundImages[] = IMAGE_GRID(IDR_SITE_CHIP_EV);
306 ev_background_painter_.reset(
307 views::Painter::CreateImageGridPainter(kEVBackgroundImages));
308 const int kBrokenSSLBackgroundImages[] = IMAGE_GRID(IDR_SITE_CHIP_BROKENSSL);
309 broken_ssl_background_painter_.reset(
310 views::Painter::CreateImageGridPainter(kBrokenSSLBackgroundImages));
311 const int kMalwareBackgroundImages[] = IMAGE_GRID(IDR_SITE_CHIP_MALWARE);
312 malware_background_painter_.reset(
313 views::Painter::CreateImageGridPainter(kMalwareBackgroundImages));
316 bool SiteChipView::ShouldShow() {
317 return chrome::ShouldDisplayOriginChip();
320 void SiteChipView::Update(content::WebContents* web_contents) {
324 // Note: security level can change async as the connection is made.
325 GURL url = toolbar_view_->GetToolbarModel()->GetURL();
326 const ToolbarModel::SecurityLevel security_level =
327 toolbar_view_->GetToolbarModel()->GetSecurityLevel(true);
329 bool url_malware = IsMalware(url, web_contents);
331 // TODO(gbillock): We persist a malware setting while a new WebContents
332 // content is loaded, meaning that we end up transiently marking a safe
333 // page as malware. Need to fix that.
335 if ((url == url_displayed_) &&
336 (security_level == security_level_) &&
337 (url_malware == url_malware_))
340 url_displayed_ = url;
341 url_malware_ = url_malware;
342 security_level_ = security_level;
344 SkColor label_background =
345 GetThemeProvider()->GetColor(ThemeProperties::COLOR_TOOLBAR);
347 painter_ = malware_background_painter_.get();
348 label_background = kMalwareBackgroundColor;
349 } else if (security_level_ == ToolbarModel::SECURITY_ERROR) {
350 painter_ = broken_ssl_background_painter_.get();
351 label_background = kBrokenSSLBackgroundColor;
352 } else if (security_level_ == ToolbarModel::EV_SECURE) {
353 painter_ = ev_background_painter_.get();
354 label_background = kEVBackgroundColor;
359 base::string16 host = SiteLabelFromURL(url_displayed_);
360 if (security_level_ == ToolbarModel::EV_SECURE) {
361 host = l10n_util::GetStringFUTF16(IDS_SITE_CHIP_EV_SSL_LABEL,
362 toolbar_view_->GetToolbarModel()->GetEVCertName(),
366 host_label_->SetText(host);
367 host_label_->SetTooltipText(host);
368 host_label_->SetBackgroundColor(label_background);
370 int icon = toolbar_view_->GetToolbarModel()->GetIconForSecurityLevel(
372 showing_16x16_icon_ = false;
374 if (url_displayed_.is_empty() ||
375 url_displayed_.SchemeIs(chrome::kChromeUIScheme)) {
376 icon = IDR_PRODUCT_LOGO_16;
377 showing_16x16_icon_ = true;
380 location_icon_view_->SetImage(GetThemeProvider()->GetImageSkiaNamed(icon));
382 if (url_displayed_.SchemeIs(extensions::kExtensionScheme)) {
383 icon = IDR_EXTENSIONS_FAVICON;
384 showing_16x16_icon_ = true;
385 location_icon_view_->SetImage(GetThemeProvider()->GetImageSkiaNamed(icon));
387 ExtensionService* service =
388 extensions::ExtensionSystem::Get(
389 toolbar_view_->browser()->profile())->extension_service();
390 const extensions::Extension* extension =
391 service->extensions()->GetExtensionOrAppByURL(url_displayed_);
392 extension_icon_.reset(
393 new SiteChipExtensionIcon(location_icon_view_,
394 toolbar_view_->browser()->profile(),
397 extension_icon_.reset();
404 void SiteChipView::OnChanged() {
405 Update(toolbar_view_->GetWebContents());
406 toolbar_view_->Layout();
407 toolbar_view_->SchedulePaint();
408 // TODO(gbillock): Also need to potentially repaint infobars to make sure the
409 // arrows are pointing to the right spot. Only needed for some edge cases.
412 gfx::Size SiteChipView::GetPreferredSize() {
413 gfx::Size label_size = host_label_->GetPreferredSize();
414 gfx::Size icon_size = location_icon_view_->GetPreferredSize();
415 int icon_spacing = showing_16x16_icon_ ?
416 (k16x16IconLeadingSpacing + k16x16IconTrailingSpacing) : 0;
417 return gfx::Size(kEdgeThickness + icon_size.width() + icon_spacing +
418 kIconTextSpacing + label_size.width() +
419 kTrailingLabelMargin + kEdgeThickness,
423 void SiteChipView::Layout() {
424 // TODO(gbillock): Eventually we almost certainly want to use
425 // LocationBarLayout for leading and trailing decorations.
427 location_icon_view_->SetBounds(
428 kEdgeThickness + (showing_16x16_icon_ ? k16x16IconLeadingSpacing : 0),
429 LocationBarView::kNormalEdgeThickness,
430 location_icon_view_->GetPreferredSize().width(),
431 height() - 2 * LocationBarView::kNormalEdgeThickness);
433 int host_label_x = location_icon_view_->x() + location_icon_view_->width() +
435 host_label_x += showing_16x16_icon_ ? k16x16IconTrailingSpacing : 0;
436 int host_label_width =
437 width() - host_label_x - kEdgeThickness - kTrailingLabelMargin;
438 host_label_->SetBounds(host_label_x,
439 LocationBarView::kNormalEdgeThickness,
441 height() - 2 * LocationBarView::kNormalEdgeThickness);
444 void SiteChipView::OnPaint(gfx::Canvas* canvas) {
445 gfx::Rect rect(GetLocalBounds());
447 views::Painter::PaintPainterAt(canvas, painter_, rect);
449 ToolbarButton::OnPaint(canvas);
452 // TODO(gbillock): Make the LocationBarView or OmniboxView the listener for
454 void SiteChipView::ButtonPressed(views::Button* sender,
455 const ui::Event& event) {
456 // See if the event needs to be passed to the LocationIconView.
457 if (event.IsMouseEvent() || (event.type() == ui::ET_GESTURE_TAP)) {
458 location_icon_view_->set_interactive(true);
459 const ui::LocatedEvent& located_event =
460 static_cast<const ui::LocatedEvent&>(event);
461 if (GetEventHandlerForPoint(located_event.location()) ==
462 location_icon_view_) {
463 location_icon_view_->page_info_helper()->ProcessEvent(located_event);
464 location_icon_view_->set_interactive(false);
467 location_icon_view_->set_interactive(false);
470 UMA_HISTOGRAM_COUNTS("SiteChip.Pressed", 1);
471 content::RecordAction(base::UserMetricsAction("SiteChipPress"));
473 toolbar_view_->location_bar()->GetOmniboxView()->SetFocus();
474 toolbar_view_->location_bar()->GetOmniboxView()->model()->
475 SetCaretVisibility(true);
476 toolbar_view_->location_bar()->GetOmniboxView()->ShowURL();
479 void SiteChipView::WriteDragDataForView(View* sender,
480 const gfx::Point& press_pt,
481 OSExchangeData* data) {
482 // TODO(gbillock): Consolidate this with the identical logic in
484 content::WebContents* web_contents = toolbar_view_->GetWebContents();
485 FaviconTabHelper* favicon_tab_helper =
486 FaviconTabHelper::FromWebContents(web_contents);
487 gfx::ImageSkia favicon = favicon_tab_helper->GetFavicon().AsImageSkia();
488 button_drag_utils::SetURLAndDragImage(web_contents->GetURL(),
489 web_contents->GetTitle(),
492 sender->GetWidget());
495 int SiteChipView::GetDragOperationsForView(View* sender,
496 const gfx::Point& p) {
497 return ui::DragDropTypes::DRAG_COPY | ui::DragDropTypes::DRAG_LINK;
500 bool SiteChipView::CanStartDragForView(View* sender,
501 const gfx::Point& press_pt,
502 const gfx::Point& p) {
506 // Note: When OnSafeBrowsingHit would be called, OnSafeBrowsingMatch will
507 // have already been called.
508 void SiteChipView::OnSafeBrowsingHit(
509 const SafeBrowsingUIManager::UnsafeResource& resource) {}
511 void SiteChipView::OnSafeBrowsingMatch(
512 const SafeBrowsingUIManager::UnsafeResource& resource) {