content_setting_bubble_contents.cc revision 7d4cd473f85ac64c3747c96c277f9e506a0d2246
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 3// found in the LICENSE file. 4 5#include "chrome/browser/ui/views/content_setting_bubble_contents.h" 6 7#include <algorithm> 8#include <set> 9#include <string> 10#include <vector> 11 12#include "base/bind.h" 13#include "base/stl_util.h" 14#include "base/strings/utf_string_conversions.h" 15#include "chrome/browser/content_settings/host_content_settings_map.h" 16#include "chrome/browser/plugins/plugin_finder.h" 17#include "chrome/browser/plugins/plugin_metadata.h" 18#include "chrome/browser/ui/content_settings/content_setting_bubble_model.h" 19#include "chrome/browser/ui/content_settings/content_setting_media_menu_model.h" 20#include "chrome/browser/ui/views/browser_dialogs.h" 21#include "content/public/browser/notification_source.h" 22#include "content/public/browser/notification_types.h" 23#include "content/public/browser/plugin_service.h" 24#include "content/public/browser/web_contents.h" 25#include "grit/generated_resources.h" 26#include "grit/theme_resources.h" 27#include "ui/base/l10n/l10n_util.h" 28#include "ui/base/models/simple_menu_model.h" 29#include "ui/views/controls/button/label_button.h" 30#include "ui/views/controls/button/menu_button.h" 31#include "ui/views/controls/button/radio_button.h" 32#include "ui/views/controls/image_view.h" 33#include "ui/views/controls/label.h" 34#include "ui/views/controls/link.h" 35#include "ui/views/controls/menu/menu.h" 36#include "ui/views/controls/menu/menu_runner.h" 37#include "ui/views/controls/separator.h" 38#include "ui/views/layout/grid_layout.h" 39#include "ui/views/layout/layout_constants.h" 40 41#if defined(USE_AURA) 42#include "ui/base/cursor/cursor.h" 43#endif 44 45namespace { 46 47// If we don't clamp the maximum width, then very long URLs and titles can make 48// the bubble arbitrarily wide. 49const int kMaxContentsWidth = 500; 50 51// When we have multiline labels, we should set a minimum width lest we get very 52// narrow bubbles with lots of line-wrapping. 53const int kMinMultiLineContentsWidth = 250; 54 55// The minimum width of the media menu buttons. 56const int kMinMediaMenuButtonWidth = 100; 57 58} // namespace 59 60using content::PluginService; 61using content::WebContents; 62 63 64// ContentSettingBubbleContents::Favicon -------------------------------------- 65 66class ContentSettingBubbleContents::Favicon : public views::ImageView { 67 public: 68 Favicon(const gfx::Image& image, 69 ContentSettingBubbleContents* parent, 70 views::Link* link); 71 virtual ~Favicon(); 72 73 private: 74 // views::View overrides: 75 virtual bool OnMousePressed(const ui::MouseEvent& event) OVERRIDE; 76 virtual void OnMouseReleased(const ui::MouseEvent& event) OVERRIDE; 77 virtual gfx::NativeCursor GetCursor(const ui::MouseEvent& event) OVERRIDE; 78 79 ContentSettingBubbleContents* parent_; 80 views::Link* link_; 81}; 82 83ContentSettingBubbleContents::Favicon::Favicon( 84 const gfx::Image& image, 85 ContentSettingBubbleContents* parent, 86 views::Link* link) 87 : parent_(parent), 88 link_(link) { 89 SetImage(image.AsImageSkia()); 90} 91 92ContentSettingBubbleContents::Favicon::~Favicon() { 93} 94 95bool ContentSettingBubbleContents::Favicon::OnMousePressed( 96 const ui::MouseEvent& event) { 97 return event.IsLeftMouseButton() || event.IsMiddleMouseButton(); 98} 99 100void ContentSettingBubbleContents::Favicon::OnMouseReleased( 101 const ui::MouseEvent& event) { 102 if ((event.IsLeftMouseButton() || event.IsMiddleMouseButton()) && 103 HitTestPoint(event.location())) { 104 parent_->LinkClicked(link_, event.flags()); 105 } 106} 107 108gfx::NativeCursor ContentSettingBubbleContents::Favicon::GetCursor( 109 const ui::MouseEvent& event) { 110#if defined(USE_AURA) 111 return ui::kCursorHand; 112#elif defined(OS_WIN) 113 static HCURSOR g_hand_cursor = LoadCursor(NULL, IDC_HAND); 114 return g_hand_cursor; 115#endif 116} 117 118 119// ContentSettingBubbleContents::MediaMenuParts ------------------------------- 120 121struct ContentSettingBubbleContents::MediaMenuParts { 122 explicit MediaMenuParts(content::MediaStreamType type); 123 ~MediaMenuParts(); 124 125 content::MediaStreamType type; 126 scoped_ptr<ui::SimpleMenuModel> menu_model; 127 128 private: 129 DISALLOW_COPY_AND_ASSIGN(MediaMenuParts); 130}; 131 132ContentSettingBubbleContents::MediaMenuParts::MediaMenuParts( 133 content::MediaStreamType type) 134 : type(type) {} 135 136ContentSettingBubbleContents::MediaMenuParts::~MediaMenuParts() {} 137 138 139// ContentSettingBubbleContents ----------------------------------------------- 140 141ContentSettingBubbleContents::ContentSettingBubbleContents( 142 ContentSettingBubbleModel* content_setting_bubble_model, 143 WebContents* web_contents, 144 views::View* anchor_view, 145 views::BubbleBorder::Arrow arrow) 146 : BubbleDelegateView(anchor_view, arrow), 147 content_setting_bubble_model_(content_setting_bubble_model), 148 web_contents_(web_contents), 149 custom_link_(NULL), 150 manage_link_(NULL), 151 close_button_(NULL) { 152 // Compensate for built-in vertical padding in the anchor view's image. 153 set_anchor_view_insets(gfx::Insets(5, 0, 5, 0)); 154 155 registrar_.Add(this, content::NOTIFICATION_WEB_CONTENTS_DESTROYED, 156 content::Source<WebContents>(web_contents)); 157} 158 159ContentSettingBubbleContents::~ContentSettingBubbleContents() { 160 STLDeleteValues(&media_menus_); 161} 162 163gfx::Size ContentSettingBubbleContents::GetPreferredSize() { 164 gfx::Size preferred_size(views::View::GetPreferredSize()); 165 int preferred_width = 166 (!content_setting_bubble_model_->bubble_content().domain_lists.empty() && 167 (kMinMultiLineContentsWidth > preferred_size.width())) ? 168 kMinMultiLineContentsWidth : preferred_size.width(); 169 preferred_size.set_width(std::min(preferred_width, kMaxContentsWidth)); 170 return preferred_size; 171} 172 173void ContentSettingBubbleContents::UpdateMenuLabel( 174 content::MediaStreamType type, 175 const std::string& label) { 176 for (MediaMenuPartsMap::const_iterator it = media_menus_.begin(); 177 it != media_menus_.end(); ++it) { 178 if (it->second->type == type) { 179 it->first->SetText(UTF8ToUTF16(label)); 180 return; 181 } 182 } 183 NOTREACHED(); 184} 185 186void ContentSettingBubbleContents::Init() { 187 using views::GridLayout; 188 189 GridLayout* layout = new views::GridLayout(this); 190 SetLayoutManager(layout); 191 192 const int kSingleColumnSetId = 0; 193 views::ColumnSet* column_set = layout->AddColumnSet(kSingleColumnSetId); 194 column_set->AddColumn(GridLayout::LEADING, GridLayout::FILL, 1, 195 GridLayout::USE_PREF, 0, 0); 196 197 const ContentSettingBubbleModel::BubbleContent& bubble_content = 198 content_setting_bubble_model_->bubble_content(); 199 bool bubble_content_empty = true; 200 201 if (!bubble_content.title.empty()) { 202 views::Label* title_label = new views::Label(UTF8ToUTF16( 203 bubble_content.title)); 204 title_label->SetMultiLine(true); 205 title_label->SetHorizontalAlignment(gfx::ALIGN_LEFT); 206 layout->StartRow(0, kSingleColumnSetId); 207 layout->AddView(title_label); 208 bubble_content_empty = false; 209 } 210 211 const std::set<std::string>& plugins = bubble_content.resource_identifiers; 212 if (!plugins.empty()) { 213 if (!bubble_content_empty) 214 layout->AddPaddingRow(0, views::kRelatedControlVerticalSpacing); 215 PluginFinder* finder = PluginFinder::GetInstance(); 216 for (std::set<std::string>::const_iterator i(plugins.begin()); 217 i != plugins.end(); ++i) { 218 string16 name = finder->FindPluginNameWithIdentifier(*i); 219 layout->StartRow(0, kSingleColumnSetId); 220 layout->AddView(new views::Label(name)); 221 bubble_content_empty = false; 222 } 223 } 224 225 if (content_setting_bubble_model_->content_type() == 226 CONTENT_SETTINGS_TYPE_POPUPS) { 227 const int kPopupColumnSetId = 2; 228 views::ColumnSet* popup_column_set = 229 layout->AddColumnSet(kPopupColumnSetId); 230 popup_column_set->AddColumn(GridLayout::LEADING, GridLayout::FILL, 0, 231 GridLayout::USE_PREF, 0, 0); 232 popup_column_set->AddPaddingColumn( 233 0, views::kRelatedControlHorizontalSpacing); 234 popup_column_set->AddColumn(GridLayout::LEADING, GridLayout::FILL, 1, 235 GridLayout::USE_PREF, 0, 0); 236 237 for (std::vector<ContentSettingBubbleModel::PopupItem>::const_iterator 238 i(bubble_content.popup_items.begin()); 239 i != bubble_content.popup_items.end(); ++i) { 240 if (!bubble_content_empty) 241 layout->AddPaddingRow(0, views::kRelatedControlVerticalSpacing); 242 layout->StartRow(0, kPopupColumnSetId); 243 244 views::Link* link = new views::Link(UTF8ToUTF16(i->title)); 245 link->set_listener(this); 246 link->SetElideBehavior(views::Label::ELIDE_IN_MIDDLE); 247 popup_links_[link] = i - bubble_content.popup_items.begin(); 248 layout->AddView(new Favicon(i->image, this, link)); 249 layout->AddView(link); 250 bubble_content_empty = false; 251 } 252 } 253 254 const int indented_kSingleColumnSetId = 3; 255 // Insert a column set with greater indent. 256 views::ColumnSet* indented_single_column_set = 257 layout->AddColumnSet(indented_kSingleColumnSetId); 258 indented_single_column_set->AddPaddingColumn(0, views::kCheckboxIndent); 259 indented_single_column_set->AddColumn(GridLayout::LEADING, GridLayout::FILL, 260 1, GridLayout::USE_PREF, 0, 0); 261 262 const ContentSettingBubbleModel::RadioGroup& radio_group = 263 bubble_content.radio_group; 264 if (!radio_group.radio_items.empty()) { 265 if (!bubble_content_empty) 266 layout->AddPaddingRow(0, views::kRelatedControlVerticalSpacing); 267 for (ContentSettingBubbleModel::RadioItems::const_iterator i( 268 radio_group.radio_items.begin()); 269 i != radio_group.radio_items.end(); ++i) { 270 views::RadioButton* radio = new views::RadioButton(UTF8ToUTF16(*i), 0); 271 radio->SetEnabled(bubble_content.radio_group_enabled); 272 radio->set_listener(this); 273 radio_group_.push_back(radio); 274 layout->StartRow(0, indented_kSingleColumnSetId); 275 layout->AddView(radio); 276 bubble_content_empty = false; 277 } 278 DCHECK(!radio_group_.empty()); 279 // Now that the buttons have been added to the view hierarchy, it's safe 280 // to call SetChecked() on them. 281 radio_group_[radio_group.default_item]->SetChecked(true); 282 } 283 284 // Layout code for the media device menus. 285 if (content_setting_bubble_model_->content_type() == 286 CONTENT_SETTINGS_TYPE_MEDIASTREAM) { 287 const int kMediaMenuColumnSetId = 2; 288 views::ColumnSet* menu_column_set = 289 layout->AddColumnSet(kMediaMenuColumnSetId); 290 menu_column_set->AddPaddingColumn(0, views::kCheckboxIndent); 291 menu_column_set->AddColumn(GridLayout::LEADING, GridLayout::FILL, 0, 292 GridLayout::USE_PREF, 0, 0); 293 menu_column_set->AddPaddingColumn( 294 0, views::kRelatedControlHorizontalSpacing); 295 menu_column_set->AddColumn(GridLayout::LEADING, GridLayout::FILL, 1, 296 GridLayout::USE_PREF, 0, 0); 297 298 int menu_width = 0; 299 for (ContentSettingBubbleModel::MediaMenuMap::const_iterator i( 300 bubble_content.media_menus.begin()); 301 i != bubble_content.media_menus.end(); ++i) { 302 if (!bubble_content_empty) 303 layout->AddPaddingRow(0, views::kRelatedControlVerticalSpacing); 304 layout->StartRow(0, kMediaMenuColumnSetId); 305 306 views::Label* label = new views::Label(UTF8ToUTF16(i->second.label)); 307 label->SetHorizontalAlignment(gfx::ALIGN_LEFT); 308 309 views::MenuButton* menu_button = new views::MenuButton( 310 NULL, UTF8ToUTF16((i->second.selected_device.name)), this, true); 311 menu_button->set_alignment(views::TextButton::ALIGN_LEFT); 312 menu_button->set_border( 313 new views::TextButtonNativeThemeBorder(menu_button)); 314 menu_button->set_animate_on_state_change(false); 315 316 MediaMenuParts* menu_view = new MediaMenuParts(i->first); 317 menu_view->menu_model.reset(new ContentSettingMediaMenuModel( 318 i->first, 319 content_setting_bubble_model_.get(), 320 base::Bind(&ContentSettingBubbleContents::UpdateMenuLabel, 321 base::Unretained(this)))); 322 media_menus_[menu_button] = menu_view; 323 324 if (!menu_view->menu_model->GetItemCount()) { 325 // Show a "None available" title and grey out the menu when there is no 326 // available device. 327 menu_button->SetText( 328 l10n_util::GetStringUTF16(IDS_MEDIA_MENU_NO_DEVICE_TITLE)); 329 menu_button->SetEnabled(false); 330 } 331 332 // Use the longest width of the menus as the width of the menu buttons. 333 menu_width = std::max(menu_width, 334 GetPreferredMediaMenuWidth( 335 menu_button, menu_view->menu_model.get())); 336 337 layout->AddView(label); 338 layout->AddView(menu_button); 339 340 bubble_content_empty = false; 341 } 342 343 // Make sure the width is at least kMinMediaMenuButtonWidth. The 344 // maximum width will be clamped by kMaxContentsWidth of the view. 345 menu_width = std::max(kMinMediaMenuButtonWidth, menu_width); 346 347 // Set all the menu buttons to the width we calculated above. 348 for (MediaMenuPartsMap::const_iterator i = media_menus_.begin(); 349 i != media_menus_.end(); ++i) { 350 i->first->set_min_width(menu_width); 351 i->first->set_max_width(menu_width); 352 } 353 } 354 355 gfx::Font domain_font = 356 views::Label().font().DeriveFont(0, gfx::Font::BOLD); 357 for (std::vector<ContentSettingBubbleModel::DomainList>::const_iterator i( 358 bubble_content.domain_lists.begin()); 359 i != bubble_content.domain_lists.end(); ++i) { 360 layout->StartRow(0, kSingleColumnSetId); 361 views::Label* section_title = new views::Label(UTF8ToUTF16(i->title)); 362 section_title->SetMultiLine(true); 363 section_title->SetHorizontalAlignment(gfx::ALIGN_LEFT); 364 layout->AddView(section_title, 1, 1, GridLayout::FILL, GridLayout::LEADING); 365 for (std::set<std::string>::const_iterator j = i->hosts.begin(); 366 j != i->hosts.end(); ++j) { 367 layout->StartRow(0, indented_kSingleColumnSetId); 368 layout->AddView(new views::Label(UTF8ToUTF16(*j), domain_font)); 369 } 370 bubble_content_empty = false; 371 } 372 373 if (!bubble_content.custom_link.empty()) { 374 custom_link_ = new views::Link(UTF8ToUTF16(bubble_content.custom_link)); 375 custom_link_->SetEnabled(bubble_content.custom_link_enabled); 376 custom_link_->set_listener(this); 377 if (!bubble_content_empty) 378 layout->AddPaddingRow(0, views::kRelatedControlVerticalSpacing); 379 layout->StartRow(0, kSingleColumnSetId); 380 layout->AddView(custom_link_); 381 bubble_content_empty = false; 382 } 383 384 if (!bubble_content_empty) { 385 layout->AddPaddingRow(0, views::kRelatedControlVerticalSpacing); 386 layout->StartRow(0, kSingleColumnSetId); 387 layout->AddView(new views::Separator(views::Separator::HORIZONTAL), 1, 1, 388 GridLayout::FILL, GridLayout::FILL); 389 layout->AddPaddingRow(0, views::kRelatedControlVerticalSpacing); 390 } 391 392 const int kDoubleColumnSetId = 1; 393 views::ColumnSet* double_column_set = 394 layout->AddColumnSet(kDoubleColumnSetId); 395 double_column_set->AddColumn(GridLayout::LEADING, GridLayout::CENTER, 1, 396 GridLayout::USE_PREF, 0, 0); 397 double_column_set->AddPaddingColumn( 398 0, views::kUnrelatedControlHorizontalSpacing); 399 double_column_set->AddColumn(GridLayout::TRAILING, GridLayout::CENTER, 0, 400 GridLayout::USE_PREF, 0, 0); 401 402 layout->StartRow(0, kDoubleColumnSetId); 403 manage_link_ = new views::Link(UTF8ToUTF16(bubble_content.manage_link)); 404 manage_link_->set_listener(this); 405 layout->AddView(manage_link_); 406 407 close_button_ = new views::LabelButton( 408 this, l10n_util::GetStringUTF16(IDS_DONE)); 409 close_button_->SetStyle(views::Button::STYLE_NATIVE_TEXTBUTTON); 410 layout->AddView(close_button_); 411} 412 413void ContentSettingBubbleContents::ButtonPressed(views::Button* sender, 414 const ui::Event& event) { 415 if (sender == close_button_) { 416 content_setting_bubble_model_->OnDoneClicked(); 417 StartFade(false); 418 return; 419 } 420 421 for (RadioGroup::const_iterator i(radio_group_.begin()); 422 i != radio_group_.end(); ++i) { 423 if (sender == *i) { 424 content_setting_bubble_model_->OnRadioClicked(i - radio_group_.begin()); 425 return; 426 } 427 } 428 NOTREACHED() << "unknown radio"; 429} 430 431void ContentSettingBubbleContents::LinkClicked(views::Link* source, 432 int event_flags) { 433 if (source == custom_link_) { 434 content_setting_bubble_model_->OnCustomLinkClicked(); 435 StartFade(false); 436 return; 437 } 438 if (source == manage_link_) { 439 StartFade(false); 440 content_setting_bubble_model_->OnManageLinkClicked(); 441 // CAREFUL: Showing the settings window activates it, which deactivates the 442 // info bubble, which causes it to close, which deletes us. 443 return; 444 } 445 446 PopupLinks::const_iterator i(popup_links_.find(source)); 447 DCHECK(i != popup_links_.end()); 448 content_setting_bubble_model_->OnPopupClicked(i->second); 449} 450 451void ContentSettingBubbleContents::OnMenuButtonClicked( 452 views::View* source, 453 const gfx::Point& point) { 454 MediaMenuPartsMap::iterator i(media_menus_.find( 455 static_cast<views::MenuButton*>(source))); 456 DCHECK(i != media_menus_.end()); 457 458 menu_runner_.reset(new views::MenuRunner(i->second->menu_model.get())); 459 460 gfx::Point screen_location; 461 views::View::ConvertPointToScreen(i->first, &screen_location); 462 ignore_result(menu_runner_->RunMenuAt( 463 source->GetWidget(), 464 i->first, 465 gfx::Rect(screen_location, i->first->size()), 466 views::MenuItemView::TOPLEFT, 467 ui::MENU_SOURCE_NONE, 468 views::MenuRunner::HAS_MNEMONICS)); 469} 470 471void ContentSettingBubbleContents::Observe( 472 int type, 473 const content::NotificationSource& source, 474 const content::NotificationDetails& details) { 475 DCHECK_EQ(content::NOTIFICATION_WEB_CONTENTS_DESTROYED, type); 476 DCHECK(source == content::Source<WebContents>(web_contents_)); 477 web_contents_ = NULL; 478} 479 480int ContentSettingBubbleContents::GetPreferredMediaMenuWidth( 481 views::MenuButton* button, 482 ui::SimpleMenuModel* menu_model) { 483 string16 title = button->text(); 484 485 int width = button->GetPreferredSize().width(); 486 for (int i = 0; i < menu_model->GetItemCount(); ++i) { 487 button->SetText(menu_model->GetLabelAt(i)); 488 width = std::max(width, button->GetPreferredSize().width()); 489 } 490 491 // Recover the title for the menu button. 492 button->SetText(title); 493 return width; 494} 495