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