Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/content_setting_bubble_contents.h" | 5 #include "chrome/browser/ui/views/content_setting_bubble_contents.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <set> | 8 #include <set> |
| 9 #include <string> | 9 #include <string> |
| 10 #include <vector> | 10 #include <vector> |
| 11 | 11 |
| 12 #include "base/bind.h" | |
| 13 #include "base/stl_util.h" | |
| 12 #include "base/utf_string_conversions.h" | 14 #include "base/utf_string_conversions.h" |
| 13 #include "chrome/browser/content_settings/host_content_settings_map.h" | 15 #include "chrome/browser/content_settings/host_content_settings_map.h" |
| 14 #include "chrome/browser/plugins/plugin_finder.h" | 16 #include "chrome/browser/plugins/plugin_finder.h" |
| 15 #include "chrome/browser/plugins/plugin_metadata.h" | 17 #include "chrome/browser/plugins/plugin_metadata.h" |
| 18 #include "chrome/browser/prefs/pref_service.h" | |
| 19 #include "chrome/browser/profiles/profile.h" | |
| 16 #include "chrome/browser/ui/chrome_style.h" | 20 #include "chrome/browser/ui/chrome_style.h" |
| 17 #include "chrome/browser/ui/content_settings/content_setting_bubble_model.h" | 21 #include "chrome/browser/ui/content_settings/content_setting_bubble_model.h" |
| 22 #include "chrome/browser/ui/content_settings/content_setting_media_menu_model.h" | |
| 18 #include "chrome/browser/ui/views/browser_dialogs.h" | 23 #include "chrome/browser/ui/views/browser_dialogs.h" |
| 19 #include "content/public/browser/notification_source.h" | 24 #include "content/public/browser/notification_source.h" |
| 20 #include "content/public/browser/notification_types.h" | 25 #include "content/public/browser/notification_types.h" |
| 21 #include "content/public/browser/plugin_service.h" | 26 #include "content/public/browser/plugin_service.h" |
| 27 #include "content/public/browser/web_contents.h" | |
| 22 #include "grit/generated_resources.h" | 28 #include "grit/generated_resources.h" |
| 29 #include "grit/theme_resources.h" | |
| 23 #include "ui/base/l10n/l10n_util.h" | 30 #include "ui/base/l10n/l10n_util.h" |
| 31 #include "ui/base/models/simple_menu_model.h" | |
| 32 #include "ui/views/controls/button/menu_button.h" | |
| 24 #include "ui/views/controls/button/radio_button.h" | 33 #include "ui/views/controls/button/radio_button.h" |
| 25 #include "ui/views/controls/button/text_button.h" | 34 #include "ui/views/controls/button/text_button.h" |
| 26 #include "ui/views/controls/image_view.h" | 35 #include "ui/views/controls/image_view.h" |
| 27 #include "ui/views/controls/label.h" | 36 #include "ui/views/controls/label.h" |
| 28 #include "ui/views/controls/link.h" | 37 #include "ui/views/controls/link.h" |
| 38 #include "ui/views/controls/menu/menu.h" | |
| 39 #include "ui/views/controls/menu/menu_model_adapter.h" | |
| 40 #include "ui/views/controls/menu/menu_runner.h" | |
| 29 #include "ui/views/controls/separator.h" | 41 #include "ui/views/controls/separator.h" |
| 30 #include "ui/views/layout/grid_layout.h" | 42 #include "ui/views/layout/grid_layout.h" |
| 31 #include "ui/views/layout/layout_constants.h" | 43 #include "ui/views/layout/layout_constants.h" |
| 32 | 44 |
| 33 #if defined(USE_AURA) | 45 #if defined(USE_AURA) |
| 34 #include "ui/base/cursor/cursor.h" | 46 #include "ui/base/cursor/cursor.h" |
| 35 #endif | 47 #endif |
| 36 | 48 |
| 49 namespace { | |
| 50 | |
| 37 // If we don't clamp the maximum width, then very long URLs and titles can make | 51 // If we don't clamp the maximum width, then very long URLs and titles can make |
| 38 // the bubble arbitrarily wide. | 52 // the bubble arbitrarily wide. |
| 39 const int kMaxContentsWidth = 500; | 53 const int kMaxContentsWidth = 500; |
| 40 | 54 |
| 41 // When we have multiline labels, we should set a minimum width lest we get very | 55 // When we have multiline labels, we should set a minimum width lest we get very |
| 42 // narrow bubbles with lots of line-wrapping. | 56 // narrow bubbles with lots of line-wrapping. |
| 43 const int kMinMultiLineContentsWidth = 250; | 57 const int kMinMultiLineContentsWidth = 250; |
| 44 | 58 |
| 59 // Color of the border of media menu button. | |
| 60 const SkColor kBorderDarkColor = SkColorSetRGB(0xaa, 0xaa, 0xaa); | |
|
Peter Kasting
2013/02/07 23:55:04
You can't hardcode colors. All colors need to com
no longer working on chromium
2013/02/08 12:32:39
Oh, uncleaned code from debugging. I removed it.
| |
| 61 | |
| 62 // The width of the media menu button. | |
| 63 const int kMediaMenuButtonWidth = 150; | |
| 64 | |
| 65 } | |
| 66 | |
| 45 using content::PluginService; | 67 using content::PluginService; |
| 46 using content::WebContents; | 68 using content::WebContents; |
| 47 | 69 |
| 48 class ContentSettingBubbleContents::Favicon : public views::ImageView { | 70 class ContentSettingBubbleContents::Favicon : public views::ImageView { |
| 49 public: | 71 public: |
| 50 Favicon(const gfx::Image& image, | 72 Favicon(const gfx::Image& image, |
| 51 ContentSettingBubbleContents* parent, | 73 ContentSettingBubbleContents* parent, |
| 52 views::Link* link); | 74 views::Link* link); |
| 53 virtual ~Favicon(); | 75 virtual ~Favicon(); |
| 54 | 76 |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 67 ContentSettingBubbleContents* parent, | 89 ContentSettingBubbleContents* parent, |
| 68 views::Link* link) | 90 views::Link* link) |
| 69 : parent_(parent), | 91 : parent_(parent), |
| 70 link_(link) { | 92 link_(link) { |
| 71 SetImage(image.AsImageSkia()); | 93 SetImage(image.AsImageSkia()); |
| 72 } | 94 } |
| 73 | 95 |
| 74 ContentSettingBubbleContents::Favicon::~Favicon() { | 96 ContentSettingBubbleContents::Favicon::~Favicon() { |
| 75 } | 97 } |
| 76 | 98 |
| 99 struct ContentSettingBubbleContents::MediaMenuParts { | |
| 100 explicit MediaMenuParts(content::MediaStreamType type); | |
| 101 ~MediaMenuParts(); | |
| 102 | |
| 103 content::MediaStreamType type; | |
| 104 scoped_ptr<ui::SimpleMenuModel> menu_model; | |
| 105 | |
| 106 private: | |
| 107 DISALLOW_COPY_AND_ASSIGN(MediaMenuParts); | |
| 108 }; | |
| 109 | |
| 110 ContentSettingBubbleContents::MediaMenuParts::MediaMenuParts( | |
| 111 content::MediaStreamType type) | |
| 112 : type(type) {} | |
| 113 | |
| 114 ContentSettingBubbleContents::MediaMenuParts::~MediaMenuParts() {} | |
| 115 | |
| 77 bool ContentSettingBubbleContents::Favicon::OnMousePressed( | 116 bool ContentSettingBubbleContents::Favicon::OnMousePressed( |
| 78 const ui::MouseEvent& event) { | 117 const ui::MouseEvent& event) { |
| 79 return event.IsLeftMouseButton() || event.IsMiddleMouseButton(); | 118 return event.IsLeftMouseButton() || event.IsMiddleMouseButton(); |
| 80 } | 119 } |
| 81 | 120 |
| 82 void ContentSettingBubbleContents::Favicon::OnMouseReleased( | 121 void ContentSettingBubbleContents::Favicon::OnMouseReleased( |
| 83 const ui::MouseEvent& event) { | 122 const ui::MouseEvent& event) { |
| 84 if ((event.IsLeftMouseButton() || event.IsMiddleMouseButton()) && | 123 if ((event.IsLeftMouseButton() || event.IsMiddleMouseButton()) && |
| 85 HitTestPoint(event.location())) { | 124 HitTestPoint(event.location())) { |
| 86 parent_->LinkClicked(link_, event.flags()); | 125 parent_->LinkClicked(link_, event.flags()); |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 101 ContentSettingBubbleModel* content_setting_bubble_model, | 140 ContentSettingBubbleModel* content_setting_bubble_model, |
| 102 WebContents* web_contents, | 141 WebContents* web_contents, |
| 103 views::View* anchor_view, | 142 views::View* anchor_view, |
| 104 views::BubbleBorder::ArrowLocation arrow_location) | 143 views::BubbleBorder::ArrowLocation arrow_location) |
| 105 : BubbleDelegateView(anchor_view, arrow_location), | 144 : BubbleDelegateView(anchor_view, arrow_location), |
| 106 content_setting_bubble_model_(content_setting_bubble_model), | 145 content_setting_bubble_model_(content_setting_bubble_model), |
| 107 web_contents_(web_contents), | 146 web_contents_(web_contents), |
| 108 custom_link_(NULL), | 147 custom_link_(NULL), |
| 109 manage_link_(NULL), | 148 manage_link_(NULL), |
| 110 close_button_(NULL) { | 149 close_button_(NULL) { |
| 111 // Compensate for built-in vertical padding in the anchor view's image. | 150 // Compensate for built-in vertical padding in the anchor view's image. |
| 112 set_anchor_insets(gfx::Insets(5, 0, 5, 0)); | 151 set_anchor_insets(gfx::Insets(5, 0, 5, 0)); |
| 113 | 152 |
| 114 registrar_.Add(this, content::NOTIFICATION_WEB_CONTENTS_DESTROYED, | 153 registrar_.Add(this, content::NOTIFICATION_WEB_CONTENTS_DESTROYED, |
| 115 content::Source<WebContents>(web_contents)); | 154 content::Source<WebContents>(web_contents)); |
| 116 } | 155 } |
| 117 | 156 |
| 118 ContentSettingBubbleContents::~ContentSettingBubbleContents() { | 157 ContentSettingBubbleContents::~ContentSettingBubbleContents() { |
| 158 STLDeleteValues(&media_menus_); | |
| 119 } | 159 } |
| 120 | 160 |
| 121 gfx::Size ContentSettingBubbleContents::GetPreferredSize() { | 161 gfx::Size ContentSettingBubbleContents::GetPreferredSize() { |
| 122 gfx::Size preferred_size(views::View::GetPreferredSize()); | 162 gfx::Size preferred_size(views::View::GetPreferredSize()); |
| 123 int preferred_width = | 163 int preferred_width = |
| 124 (!content_setting_bubble_model_->bubble_content().domain_lists.empty() && | 164 (!content_setting_bubble_model_->bubble_content().domain_lists.empty() && |
| 125 (kMinMultiLineContentsWidth > preferred_size.width())) ? | 165 (kMinMultiLineContentsWidth > preferred_size.width())) ? |
| 126 kMinMultiLineContentsWidth : preferred_size.width(); | 166 kMinMultiLineContentsWidth : preferred_size.width(); |
| 127 preferred_size.set_width(std::min(preferred_width, kMaxContentsWidth)); | 167 preferred_size.set_width(std::min(preferred_width, kMaxContentsWidth)); |
| 128 return preferred_size; | 168 return preferred_size; |
| 129 } | 169 } |
| 130 | 170 |
| 171 void ContentSettingBubbleContents::UpdateMenuLabel( | |
| 172 content::MediaStreamType type, | |
| 173 const std::string& label) { | |
| 174 for (MediaMenuPartsMap::const_iterator it = media_menus_.begin(); | |
| 175 it != media_menus_.end(); ++it) { | |
| 176 if (it->second->type == type) { | |
| 177 it->first->SetText(UTF8ToUTF16(label)); | |
| 178 return; | |
| 179 } | |
| 180 } | |
| 181 NOTREACHED(); | |
| 182 } | |
| 183 | |
| 131 void ContentSettingBubbleContents::Init() { | 184 void ContentSettingBubbleContents::Init() { |
| 132 using views::GridLayout; | 185 using views::GridLayout; |
| 133 | 186 |
| 134 GridLayout* layout = new views::GridLayout(this); | 187 GridLayout* layout = new views::GridLayout(this); |
| 135 SetLayoutManager(layout); | 188 SetLayoutManager(layout); |
| 136 | 189 |
| 137 const int kSingleColumnSetId = 0; | 190 const int kSingleColumnSetId = 0; |
| 138 views::ColumnSet* column_set = layout->AddColumnSet(kSingleColumnSetId); | 191 views::ColumnSet* column_set = layout->AddColumnSet(kSingleColumnSetId); |
| 139 column_set->AddColumn(GridLayout::LEADING, GridLayout::FILL, 1, | 192 column_set->AddColumn(GridLayout::LEADING, GridLayout::FILL, 1, |
| 140 GridLayout::USE_PREF, 0, 0); | 193 GridLayout::USE_PREF, 0, 0); |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 218 layout->StartRow(0, indented_kSingleColumnSetId); | 271 layout->StartRow(0, indented_kSingleColumnSetId); |
| 219 layout->AddView(radio); | 272 layout->AddView(radio); |
| 220 bubble_content_empty = false; | 273 bubble_content_empty = false; |
| 221 } | 274 } |
| 222 DCHECK(!radio_group_.empty()); | 275 DCHECK(!radio_group_.empty()); |
| 223 // Now that the buttons have been added to the view hierarchy, it's safe | 276 // Now that the buttons have been added to the view hierarchy, it's safe |
| 224 // to call SetChecked() on them. | 277 // to call SetChecked() on them. |
| 225 radio_group_[radio_group.default_item]->SetChecked(true); | 278 radio_group_[radio_group.default_item]->SetChecked(true); |
| 226 } | 279 } |
| 227 | 280 |
| 281 // Layout code for the media device menus. | |
| 282 if (content_setting_bubble_model_->content_type() == | |
| 283 CONTENT_SETTINGS_TYPE_MEDIASTREAM) { | |
| 284 const int kMediaMenuColumnSetId = 2; | |
| 285 views::ColumnSet* menu_column_set = | |
| 286 layout->AddColumnSet(kMediaMenuColumnSetId); | |
| 287 menu_column_set->AddColumn(GridLayout::LEADING, GridLayout::FILL, 0, | |
| 288 GridLayout::USE_PREF, 0, 0); | |
| 289 menu_column_set->AddPaddingColumn( | |
| 290 0, views::kRelatedControlHorizontalSpacing); | |
| 291 menu_column_set->AddColumn(GridLayout::LEADING, GridLayout::FILL, 1, | |
| 292 GridLayout::USE_PREF, 0, 0); | |
| 293 | |
| 294 for (ContentSettingBubbleModel::MediaMenuMap::const_iterator i( | |
| 295 bubble_content.media_menus.begin()); | |
| 296 i != bubble_content.media_menus.end(); ++i) { | |
| 297 if (!bubble_content_empty) | |
| 298 layout->AddPaddingRow(0, views::kRelatedControlVerticalSpacing); | |
| 299 layout->StartRow(0, kMediaMenuColumnSetId); | |
| 300 | |
| 301 views::Label* menu_label = new views::Label( | |
|
Peter Kasting
2013/02/07 23:55:04
Nit: I suggest breaking after '=' instead
no longer working on chromium
2013/02/08 12:32:39
Done.
| |
| 302 UTF8ToUTF16(i->second.label)); | |
| 303 menu_label->SetHorizontalAlignment(gfx::ALIGN_LEFT); | |
| 304 views::MenuButton* menu_button = | |
| 305 new views::MenuButton( | |
|
Peter Kasting
2013/02/07 23:55:04
Nit: If you want to indent the subsequent line the
no longer working on chromium
2013/02/08 12:32:39
Done.
| |
| 306 NULL, UTF8ToUTF16((i->second.selected_device.name)), this, false); | |
| 307 menu_button->set_min_width(kMediaMenuButtonWidth); | |
| 308 menu_button->set_max_width(kMediaMenuButtonWidth); | |
| 309 menu_button->set_alignment(views::TextButton::ALIGN_LEFT); | |
| 310 menu_button->set_border( | |
| 311 new views::TextButtonNativeThemeBorder(menu_button)); | |
| 312 menu_button->set_animate_on_state_change(false); | |
| 313 menu_button->SetEnabledColor(SK_ColorBLACK); | |
| 314 menu_button->SetHighlightColor(SK_ColorBLACK); | |
| 315 menu_button->SetHoverColor(SK_ColorBLACK); | |
| 316 | |
| 317 layout->AddView(menu_label); | |
| 318 layout->AddView(menu_button); | |
| 319 | |
| 320 MediaMenuParts* menu_view = new MediaMenuParts(i->first); | |
| 321 Profile* profile = | |
| 322 Profile::FromBrowserContext(web_contents_->GetBrowserContext()); | |
|
Peter Kasting
2013/02/07 23:55:04
Nit: I'd just inline this into the subsequent stat
no longer working on chromium
2013/02/08 12:32:39
Done.
| |
| 323 menu_view->menu_model.reset(new ContentSettingMediaMenuModel( | |
| 324 profile->GetPrefs(), | |
| 325 i->second.default_device.type, | |
| 326 content_setting_bubble_model_.get(), | |
| 327 base::Bind(&ContentSettingBubbleContents::UpdateMenuLabel, | |
| 328 base::Unretained(this)))); | |
| 329 media_menus_[menu_button] = menu_view; | |
| 330 bubble_content_empty = false; | |
| 331 } | |
| 332 } | |
| 333 | |
| 228 gfx::Font domain_font = | 334 gfx::Font domain_font = |
| 229 views::Label().font().DeriveFont(0, gfx::Font::BOLD); | 335 views::Label().font().DeriveFont(0, gfx::Font::BOLD); |
| 230 for (std::vector<ContentSettingBubbleModel::DomainList>::const_iterator i( | 336 for (std::vector<ContentSettingBubbleModel::DomainList>::const_iterator i( |
| 231 bubble_content.domain_lists.begin()); | 337 bubble_content.domain_lists.begin()); |
| 232 i != bubble_content.domain_lists.end(); ++i) { | 338 i != bubble_content.domain_lists.end(); ++i) { |
| 233 layout->StartRow(0, kSingleColumnSetId); | 339 layout->StartRow(0, kSingleColumnSetId); |
| 234 views::Label* section_title = new views::Label(UTF8ToUTF16(i->title)); | 340 views::Label* section_title = new views::Label(UTF8ToUTF16(i->title)); |
| 235 section_title->SetMultiLine(true); | 341 section_title->SetMultiLine(true); |
| 236 section_title->SetHorizontalAlignment(gfx::ALIGN_LEFT); | 342 section_title->SetHorizontalAlignment(gfx::ALIGN_LEFT); |
| 237 layout->AddView(section_title, 1, 1, GridLayout::FILL, GridLayout::LEADING); | 343 layout->AddView(section_title, 1, 1, GridLayout::FILL, GridLayout::LEADING); |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 313 // CAREFUL: Showing the settings window activates it, which deactivates the | 419 // CAREFUL: Showing the settings window activates it, which deactivates the |
| 314 // info bubble, which causes it to close, which deletes us. | 420 // info bubble, which causes it to close, which deletes us. |
| 315 return; | 421 return; |
| 316 } | 422 } |
| 317 | 423 |
| 318 PopupLinks::const_iterator i(popup_links_.find(source)); | 424 PopupLinks::const_iterator i(popup_links_.find(source)); |
| 319 DCHECK(i != popup_links_.end()); | 425 DCHECK(i != popup_links_.end()); |
| 320 content_setting_bubble_model_->OnPopupClicked(i->second); | 426 content_setting_bubble_model_->OnPopupClicked(i->second); |
| 321 } | 427 } |
| 322 | 428 |
| 429 void ContentSettingBubbleContents::OnMenuButtonClicked( | |
| 430 views::View* source, | |
| 431 const gfx::Point& point) { | |
| 432 MediaMenuPartsMap::iterator i(media_menus_.find( | |
| 433 static_cast<views::MenuButton*>(source))); | |
| 434 DCHECK(i != media_menus_.end()); | |
| 435 | |
| 436 views::MenuModelAdapter menu_model_adapter(i->second->menu_model.get()); | |
| 437 menu_runner_.reset(new views::MenuRunner(menu_model_adapter.CreateMenu())); | |
| 438 | |
| 439 gfx::Point screen_location; | |
| 440 views::View::ConvertPointToScreen(i->first, &screen_location); | |
| 441 if (menu_runner_->RunMenuAt(source->GetWidget(), | |
| 442 i->first, | |
| 443 gfx::Rect(screen_location, i->first->size()), | |
| 444 views::MenuItemView::TOPRIGHT, | |
| 445 views::MenuRunner::HAS_MNEMONICS) == | |
| 446 views::MenuRunner::MENU_DELETED) | |
| 447 return; | |
| 448 } | |
| 449 | |
| 323 void ContentSettingBubbleContents::Observe( | 450 void ContentSettingBubbleContents::Observe( |
| 324 int type, | 451 int type, |
| 325 const content::NotificationSource& source, | 452 const content::NotificationSource& source, |
| 326 const content::NotificationDetails& details) { | 453 const content::NotificationDetails& details) { |
| 327 DCHECK_EQ(content::NOTIFICATION_WEB_CONTENTS_DESTROYED, type); | 454 DCHECK_EQ(content::NOTIFICATION_WEB_CONTENTS_DESTROYED, type); |
| 328 DCHECK(source == content::Source<WebContents>(web_contents_)); | 455 DCHECK(source == content::Source<WebContents>(web_contents_)); |
| 329 web_contents_ = NULL; | 456 web_contents_ = NULL; |
| 330 } | 457 } |
| OLD | NEW |