OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/ui/views/toolbar/site_chip_view.h" | 5 #include "chrome/browser/ui/views/toolbar/site_chip_view.h" |
6 | 6 |
7 #include "base/files/file_path.h" | |
7 #include "base/prefs/pref_service.h" | 8 #include "base/prefs/pref_service.h" |
8 #include "base/strings/string_util.h" | 9 #include "base/strings/string_util.h" |
9 #include "base/strings/utf_string_conversions.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_system.h" | |
15 #include "chrome/browser/favicon/favicon_tab_helper.h" | |
10 #include "chrome/browser/profiles/profile.h" | 16 #include "chrome/browser/profiles/profile.h" |
11 #include "chrome/browser/search/search.h" | 17 #include "chrome/browser/search/search.h" |
12 #include "chrome/browser/themes/theme_properties.h" | 18 #include "chrome/browser/themes/theme_properties.h" |
13 #include "chrome/browser/ui/browser.h" | 19 #include "chrome/browser/ui/browser.h" |
14 #include "chrome/browser/ui/omnibox/omnibox_view.h" | 20 #include "chrome/browser/ui/omnibox/omnibox_view.h" |
15 #include "chrome/browser/ui/toolbar/toolbar_model.h" | 21 #include "chrome/browser/ui/toolbar/toolbar_model.h" |
16 #include "chrome/browser/ui/views/location_bar/location_bar_view.h" | 22 #include "chrome/browser/ui/views/location_bar/location_bar_view.h" |
17 #include "chrome/browser/ui/views/toolbar/toolbar_view.h" | 23 #include "chrome/browser/ui/views/toolbar/toolbar_view.h" |
24 #include "chrome/common/extensions/extension_constants.h" | |
25 #include "chrome/common/extensions/manifest_handlers/icons_handler.h" | |
18 #include "chrome/common/pref_names.h" | 26 #include "chrome/common/pref_names.h" |
19 #include "content/public/browser/web_contents.h" | 27 #include "content/public/browser/web_contents.h" |
28 #include "extensions/common/constants.h" | |
20 #include "grit/theme_resources.h" | 29 #include "grit/theme_resources.h" |
21 #include "net/base/net_util.h" | 30 #include "net/base/net_util.h" |
22 #include "ui/base/resource/resource_bundle.h" | 31 #include "ui/base/resource/resource_bundle.h" |
23 #include "ui/base/theme_provider.h" | 32 #include "ui/base/theme_provider.h" |
24 #include "ui/views/background.h" | 33 #include "ui/views/background.h" |
34 #include "ui/views/button_drag_utils.h" | |
25 #include "ui/views/controls/button/label_button.h" | 35 #include "ui/views/controls/button/label_button.h" |
26 #include "ui/views/controls/button/label_button_border.h" | 36 #include "ui/views/controls/button/label_button_border.h" |
27 #include "ui/views/controls/label.h" | 37 #include "ui/views/controls/label.h" |
28 #include "ui/views/painter.h" | 38 #include "ui/views/painter.h" |
29 | 39 |
30 const int kEdgeThickness = 4; | 40 |
31 const int kIconTextSpacing = 4; | 41 // SiteChipExtensionIcon ------------------------------------------------------ |
32 const int kTrailingLabelMargin = 2; | 42 |
43 class SiteChipExtensionIcon : public extensions::IconImage::Observer { | |
44 public: | |
45 SiteChipExtensionIcon(LocationIconView* icon_view, | |
46 Profile* profile, | |
47 const extensions::Extension* extension); | |
48 ~SiteChipExtensionIcon(); | |
49 | |
50 // IconImage::Observer: | |
51 virtual void OnExtensionIconImageChanged( | |
52 extensions::IconImage* image) OVERRIDE; | |
53 | |
54 private: | |
55 LocationIconView* icon_view_; | |
56 scoped_ptr<extensions::IconImage> icon_image_; | |
57 | |
58 DISALLOW_COPY_AND_ASSIGN(SiteChipExtensionIcon); | |
59 }; | |
60 | |
61 SiteChipExtensionIcon::SiteChipExtensionIcon( | |
62 LocationIconView* icon_view, | |
63 Profile* profile, | |
64 const extensions::Extension* extension) | |
65 : icon_view_(icon_view), | |
66 icon_image_(new extensions::IconImage( | |
67 profile, | |
68 extension, | |
69 extensions::IconsInfo::GetIcons(extension), | |
70 extension_misc::EXTENSION_ICON_BITTY, | |
71 extensions::IconsInfo::GetDefaultAppIcon(), | |
72 this)) { | |
73 // Forces load of the image. | |
74 icon_image_->image_skia().GetRepresentation(1.0f); | |
75 | |
76 if (!icon_image_->image_skia().image_reps().empty()) | |
77 OnExtensionIconImageChanged(icon_image_.get()); | |
78 } | |
79 | |
80 SiteChipExtensionIcon::~SiteChipExtensionIcon() { | |
81 } | |
82 | |
83 void SiteChipExtensionIcon::OnExtensionIconImageChanged( | |
84 extensions::IconImage* image) { | |
85 if (icon_view_) | |
86 icon_view_->SetImage(&icon_image_->image_skia()); | |
87 } | |
88 | |
89 | |
90 // SiteChipView --------------------------------------------------------------- | |
91 | |
92 namespace { | |
93 | |
94 const int kEdgeThickness = 5; | |
95 const int k16x16IconExtraSpacing = 3; | |
Peter Kasting
2013/12/10 03:28:39
Nit: Change this to two constants for now (a befor
Greg Billock
2013/12/10 17:37:59
Done.
| |
96 const int kIconTextSpacing = 3; | |
97 const int kTrailingLabelMargin = 0; | |
98 | |
99 } // namespace | |
100 | |
101 string16 SiteChipView::SiteLabelFromURL(const GURL& url) { | |
102 // The NTP. | |
103 if (!url.is_valid()) | |
104 return string16(UTF8ToUTF16("Chrome")); | |
105 | |
106 // TODO(gbillock): for kChromeUIScheme and kAboutScheme, return the title of | |
107 // the page. | |
108 // See url_constants.cc for hosts. ?? Or just show "Chrome"? | |
109 if (url.SchemeIs(chrome::kChromeUIScheme) || | |
110 url.SchemeIs(chrome::kAboutScheme)) { | |
111 return string16(UTF8ToUTF16("Chrome")); | |
112 } | |
113 | |
114 // For file: urls, return the BaseName of the file . | |
Peter Kasting
2013/12/10 03:28:39
Nit: Lots of spaces before period?
Greg Billock
2013/12/10 17:37:59
Done.
| |
115 if (url.SchemeIsFile()) { | |
116 base::FilePath path = base::FilePath::FromUTF8Unsafe(url.path()); | |
117 // TODO(gbillock): Need nuance here. | |
118 return string16(base::UTF8ToUTF16(path.BaseName().value())); | |
119 } | |
120 | |
121 // TODO(gbillock): Handle filesystem urls the same way? | |
122 // Also: should handle interstitials differently? | |
123 | |
124 // TODO(gbillock): think about view-source? | |
125 | |
126 Profile* profile = toolbar_view_->browser()->profile(); | |
127 | |
128 // For chrome-extension urls, return the extension name. | |
129 if (url.SchemeIs(extensions::kExtensionScheme)) { | |
130 ExtensionService* service = | |
131 extensions::ExtensionSystem::Get(profile)->extension_service(); | |
132 const extensions::Extension* extension = | |
133 service->extensions()->GetExtensionOrAppByURL(url); | |
134 return extension ? base::UTF8ToUTF16(extension->name()) : | |
Peter Kasting
2013/12/10 03:28:39
Nit: break after '?' instead of ':'
Greg Billock
2013/12/10 17:37:59
Done.
| |
135 UTF8ToUTF16(url.host()); | |
136 } | |
137 | |
138 if (url.SchemeIsHTTPOrHTTPS()) { | |
139 // See ToolbarModelImpl::GetText(). Does not pay attention to any user | |
140 // edits, and uses GetURL/net::FormatUrl -- We don't really care about | |
141 // length or the autocomplete parser. | |
142 std::string languages; | |
143 if (profile) | |
144 languages = profile->GetPrefs()->GetString(prefs::kAcceptLanguages); | |
145 | |
146 base::string16 formatted = net::FormatUrl(url.GetOrigin(), languages, | |
Peter Kasting
2013/12/10 03:28:39
I still think this should just be the host, run th
Greg Billock
2013/12/10 17:37:59
I agree this is conservative. Let's keep it for th
Peter Kasting
2013/12/10 19:44:31
OK I guess, but consider at least adding a TODO ab
| |
147 net::kFormatUrlOmitAll, net::UnescapeRule::NORMAL, NULL, NULL, NULL); | |
148 // Remove scheme, "www.", and trailing "/". | |
149 if (StartsWith(formatted, ASCIIToUTF16("http://"), false)) | |
150 formatted = formatted.substr(7); | |
151 if (StartsWith(formatted, ASCIIToUTF16("https://"), false)) | |
152 formatted = formatted.substr(8); | |
153 if (StartsWith(formatted, ASCIIToUTF16("www."), false)) | |
154 formatted = formatted.substr(4); | |
155 if (EndsWith(formatted, ASCIIToUTF16("/"), false)) | |
156 formatted = formatted.substr(0, formatted.size()-1); | |
157 return formatted; | |
158 } | |
159 | |
160 // For FTP, prepend "ftp:" to hostname. | |
161 if (url.SchemeIs(content::kFtpScheme)) { | |
162 return string16(UTF8ToUTF16(std::string("ftp:") + url.host())); | |
163 } | |
164 | |
165 // If all else fails, return hostname. | |
166 return string16(UTF8ToUTF16(url.host())); | |
167 } | |
33 | 168 |
34 SiteChipView::SiteChipView(ToolbarView* toolbar_view) | 169 SiteChipView::SiteChipView(ToolbarView* toolbar_view) |
35 : ToolbarButton(this, NULL), | 170 : ToolbarButton(this, NULL), |
36 toolbar_view_(toolbar_view) { | 171 toolbar_view_(toolbar_view), |
172 painter_(NULL), | |
173 showing_16x16_icon_(false) { | |
174 set_drag_controller(this); | |
37 } | 175 } |
38 | 176 |
39 SiteChipView::~SiteChipView() { | 177 SiteChipView::~SiteChipView() { |
40 } | 178 } |
41 | 179 |
42 void SiteChipView::Init() { | 180 void SiteChipView::Init() { |
43 ToolbarButton::Init(); | 181 ToolbarButton::Init(); |
44 | 182 |
45 // TODO(gbillock): Would be nice to just use stock LabelButton stuff here. | 183 // TODO(gbillock): Would be nice to just use stock LabelButton stuff here. |
46 location_icon_view_ = new LocationIconView(toolbar_view_->location_bar()); | 184 location_icon_view_ = new LocationIconView(toolbar_view_->location_bar()); |
47 host_label_ = new views::Label(); | 185 host_label_ = new views::Label(); |
48 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | 186 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
49 host_label_->SetFont(rb.GetFont(ui::ResourceBundle::MediumFont)); | 187 host_label_->SetFont(rb.GetFont(ui::ResourceBundle::MediumFont)); |
50 | 188 |
51 AddChildView(location_icon_view_); | 189 AddChildView(location_icon_view_); |
52 AddChildView(host_label_); | 190 AddChildView(host_label_); |
53 | 191 |
54 // temporary background painter | |
55 const int kBackgroundImages[] = IMAGE_GRID(IDR_SITE_CHIP_EV); | |
56 background_painter_.reset( | |
57 views::Painter::CreateImageGridPainter(kBackgroundImages)); | |
58 | |
59 // temporary icon filler | |
60 location_icon_view_->SetImage(GetThemeProvider()->GetImageSkiaNamed( | 192 location_icon_view_->SetImage(GetThemeProvider()->GetImageSkiaNamed( |
61 IDR_OMNIBOX_HTTPS_VALID)); | 193 IDR_LOCATION_BAR_HTTP)); |
62 location_icon_view_->ShowTooltip(true); | 194 location_icon_view_->ShowTooltip(true); |
63 | 195 |
64 // temporary filler text. | 196 const int kEVBackgroundImages[] = IMAGE_GRID(IDR_SITE_CHIP_EV); |
65 host_label_->SetText(ASCIIToUTF16("site.chip")); | 197 ev_background_painter_.reset( |
198 views::Painter::CreateImageGridPainter(kEVBackgroundImages)); | |
199 const int kBadSSLBackgroundImages[] = IMAGE_GRID(IDR_SITE_CHIP_BROKENSSL); | |
200 badssl_background_painter_.reset( | |
201 views::Painter::CreateImageGridPainter(kBadSSLBackgroundImages)); | |
66 } | 202 } |
67 | 203 |
68 bool SiteChipView::ShouldShow() { | 204 bool SiteChipView::ShouldShow() { |
69 return chrome::ShouldDisplayOriginChip(); | 205 return chrome::ShouldDisplayOriginChip(); |
70 } | 206 } |
71 | 207 |
72 void SiteChipView::Update(content::WebContents* tab) { | 208 void SiteChipView::Update(content::WebContents* web_contents) { |
209 if (!web_contents) | |
210 return; | |
211 | |
212 // Note: security level can change async as the connection is made. | |
213 GURL url = toolbar_view_->GetToolbarModel()->GetURL(); | |
214 const ToolbarModel::SecurityLevel security_level = | |
215 toolbar_view_->GetToolbarModel()->GetSecurityLevel(true); | |
216 if (url == url_displayed_ && (security_level == security_level_)) | |
Peter Kasting
2013/12/10 03:28:39
Nit: Parens on both subexprs
Greg Billock
2013/12/10 17:37:59
Done.
| |
217 return; | |
218 | |
219 url_displayed_ = url; | |
220 | |
221 string16 host = SiteLabelFromURL(url); | |
222 | |
223 // TODO(gbillock): Deal with RTL here better? Use a separate | |
224 // label for cert name? | |
225 if ((security_level != security_level_) && | |
226 (security_level == ToolbarModel::EV_SECURE)) { | |
227 host = toolbar_view_->GetToolbarModel()->GetEVCertName() + | |
228 ASCIIToUTF16(" ") + host; | |
Peter Kasting
2013/12/10 03:28:39
This isn't good l10n, even ignoring RTL.
Use some
Greg Billock
2013/12/10 17:37:59
I hadn't considered using L10N to deal with this -
| |
229 } | |
230 | |
231 host_ = host; | |
232 host_label_->SetText(host_); | |
233 host_label_->SetTooltipText(host_); | |
234 SkColor toolbar_background = | |
235 GetThemeProvider()->GetColor(ThemeProperties::COLOR_TOOLBAR); | |
236 SkColor host_color = | |
Peter Kasting
2013/12/10 03:28:39
Nit: I would at least inline this into the next st
Greg Billock
2013/12/10 17:37:59
Done.
| |
237 color_utils::GetReadableColor(SK_ColorBLACK, toolbar_background); | |
238 host_label_->SetEnabledColor(host_color); | |
239 | |
240 security_level_ = security_level; | |
241 int icon = toolbar_view_->GetToolbarModel()->GetIconForSecurityLevel( | |
242 security_level_); | |
243 showing_16x16_icon_ = false; | |
244 | |
245 if (!url.is_valid() || | |
246 url.SchemeIs(chrome::kChromeUIScheme)) { | |
247 icon = IDR_PRODUCT_LOGO_16; | |
248 showing_16x16_icon_ = true; | |
249 } | |
250 | |
251 if (url.SchemeIs(extensions::kExtensionScheme)) { | |
252 icon = IDR_EXTENSIONS_FAVICON; | |
253 showing_16x16_icon_ = true; | |
254 | |
255 ExtensionService* service = | |
256 extensions::ExtensionSystem::Get( | |
257 toolbar_view_->browser()->profile())->extension_service(); | |
258 const extensions::Extension* extension = | |
259 service->extensions()->GetExtensionOrAppByURL(url); | |
260 extension_icon_loader_.reset( | |
261 new SiteChipExtensionIcon(location_icon_view_, | |
262 toolbar_view_->browser()->profile(), | |
263 extension)); | |
264 } else { | |
265 extension_icon_loader_.reset(); | |
266 } | |
267 | |
268 location_icon_view_->SetImage(GetThemeProvider()->GetImageSkiaNamed(icon)); | |
269 location_icon_view_->ShowTooltip(true); | |
270 | |
271 // TODO(gbillock): Add malware accounting. | |
272 if (security_level_ == ToolbarModel::SECURITY_ERROR) | |
273 painter_ = badssl_background_painter_.get(); | |
274 else if (security_level_ == ToolbarModel::EV_SECURE) | |
275 painter_ = ev_background_painter_.get(); | |
276 else | |
277 painter_ = NULL; | |
278 | |
73 Layout(); | 279 Layout(); |
74 SchedulePaint(); | 280 SchedulePaint(); |
281 // TODO(gbillock): Need to schedule paint on parent to erase any background? | |
282 } | |
283 | |
284 void SiteChipView::OnChanged() { | |
285 Update(toolbar_view_->GetWebContents()); | |
286 toolbar_view_->Layout(); | |
287 toolbar_view_->SchedulePaint(); | |
288 // TODO(gbillock): Also need to potentially redo infobars. | |
Peter Kasting
2013/12/10 03:28:39
Nit: Might be nice to clarify that this is about w
Greg Billock
2013/12/10 17:37:59
Done. The update signals come in lower, though, so
| |
75 } | 289 } |
76 | 290 |
77 gfx::Size SiteChipView::GetPreferredSize() { | 291 gfx::Size SiteChipView::GetPreferredSize() { |
78 gfx::Size label_size = host_label_->GetPreferredSize(); | 292 gfx::Size label_size = host_label_->GetPreferredSize(); |
79 gfx::Size icon_size = location_icon_view_->GetPreferredSize(); | 293 gfx::Size icon_size = location_icon_view_->GetPreferredSize(); |
80 return gfx::Size(icon_size.width() + label_size.width() + | 294 return gfx::Size(icon_size.width() + label_size.width() + |
81 kIconTextSpacing + kTrailingLabelMargin + | 295 kIconTextSpacing + kTrailingLabelMargin + |
82 2 * kEdgeThickness, | 296 2 * kEdgeThickness + |
297 (showing_16x16_icon_ ? 2 * k16x16IconExtraSpacing : 0), | |
83 icon_size.height()); | 298 icon_size.height()); |
84 } | 299 } |
85 | 300 |
86 void SiteChipView::Layout() { | 301 void SiteChipView::Layout() { |
302 // TODO(gbillock): Eventually we almost certainly want to use | |
303 // LocationBarLayout for leading and trailing decorations. | |
304 | |
87 location_icon_view_->SetBounds( | 305 location_icon_view_->SetBounds( |
88 kEdgeThickness, | 306 kEdgeThickness + (showing_16x16_icon_ ? k16x16IconExtraSpacing : 0), |
89 LocationBarView::kNormalEdgeThickness, | 307 LocationBarView::kNormalEdgeThickness, |
90 location_icon_view_->GetPreferredSize().width(), | 308 location_icon_view_->GetPreferredSize().width(), |
91 height() - 2 * LocationBarView::kNormalEdgeThickness); | 309 height() - 2 * LocationBarView::kNormalEdgeThickness); |
92 | 310 |
93 int host_label_x = location_icon_view_->x() + location_icon_view_->width() + | 311 int host_label_x = location_icon_view_->x() + location_icon_view_->width() + |
94 kIconTextSpacing; | 312 (showing_16x16_icon_ ? k16x16IconExtraSpacing : 0) + kIconTextSpacing; |
95 int host_label_width = | 313 int host_label_width = |
96 width() - host_label_x - kEdgeThickness - kTrailingLabelMargin; | 314 width() - host_label_x - kEdgeThickness - kTrailingLabelMargin; |
97 host_label_->SetBounds(host_label_x, | 315 host_label_->SetBounds(host_label_x, |
98 LocationBarView::kNormalEdgeThickness, | 316 LocationBarView::kNormalEdgeThickness, |
99 host_label_width, | 317 host_label_width, |
100 height() - 2 * LocationBarView::kNormalEdgeThickness); | 318 height() - 2 * LocationBarView::kNormalEdgeThickness); |
101 } | 319 } |
102 | 320 |
103 void SiteChipView::OnPaint(gfx::Canvas* canvas) { | 321 void SiteChipView::OnPaint(gfx::Canvas* canvas) { |
104 gfx::Rect rect(GetLocalBounds()); | 322 gfx::Rect rect(GetLocalBounds()); |
105 rect.Inset(LocationBarView::kNormalEdgeThickness, | 323 if (painter_) |
106 LocationBarView::kNormalEdgeThickness); | 324 views::Painter::PaintPainterAt(canvas, painter_, rect); |
107 if (background_painter_.get()) | |
108 views::Painter::PaintPainterAt(canvas, background_painter_.get(), rect); | |
109 | 325 |
110 ToolbarButton::OnPaint(canvas); | 326 ToolbarButton::OnPaint(canvas); |
111 } | 327 } |
112 | 328 |
113 // TODO(gbillock): Make the LocationBarView or OmniboxView the listener for | 329 // TODO(gbillock): Make the LocationBarView or OmniboxView the listener for |
114 // this button. | 330 // this button. |
115 void SiteChipView::ButtonPressed(views::Button* sender, | 331 void SiteChipView::ButtonPressed(views::Button* sender, |
116 const ui::Event& event) { | 332 const ui::Event& event) { |
117 toolbar_view_->location_bar()->GetOmniboxView()->SetFocus(); | 333 toolbar_view_->location_bar()->GetOmniboxView()->SetFocus(); |
118 toolbar_view_->location_bar()->GetOmniboxView()->SelectAll(true); | 334 toolbar_view_->location_bar()->GetOmniboxView()->SelectAll(true); |
119 toolbar_view_->location_bar()->GetOmniboxView()->model()-> | 335 toolbar_view_->location_bar()->GetOmniboxView()->model()-> |
120 SetCaretVisibility(true); | 336 SetCaretVisibility(true); |
121 } | 337 } |
338 | |
339 void SiteChipView::WriteDragDataForView(View* sender, | |
340 const gfx::Point& press_pt, | |
341 OSExchangeData* data) { | |
342 content::WebContents* web_contents = toolbar_view_->GetWebContents(); | |
Peter Kasting
2013/12/10 03:28:39
Nit: At least add a TODO here about consolidating
Greg Billock
2013/12/10 17:37:59
Done.
| |
343 FaviconTabHelper* favicon_tab_helper = | |
344 FaviconTabHelper::FromWebContents(web_contents); | |
345 gfx::ImageSkia favicon = favicon_tab_helper->GetFavicon().AsImageSkia(); | |
346 button_drag_utils::SetURLAndDragImage(web_contents->GetURL(), | |
347 web_contents->GetTitle(), | |
348 favicon, | |
349 data, | |
350 sender->GetWidget()); | |
351 } | |
352 | |
353 int SiteChipView::GetDragOperationsForView(View* sender, | |
354 const gfx::Point& p) { | |
355 return ui::DragDropTypes::DRAG_COPY | ui::DragDropTypes::DRAG_LINK; | |
356 } | |
357 | |
358 bool SiteChipView::CanStartDragForView(View* sender, | |
359 const gfx::Point& press_pt, | |
360 const gfx::Point& p) { | |
361 return true; | |
362 } | |
363 | |
OLD | NEW |