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.
5 #include "chrome/browser/ui/views/content_setting_bubble_contents.h"
12 #include "base/bind.h"
13 #include "base/stl_util.h"
14 #include "base/strings/utf_string_conversions.h"
15 #include "chrome/browser/plugins/plugin_finder.h"
16 #include "chrome/browser/plugins/plugin_metadata.h"
17 #include "chrome/browser/ui/content_settings/content_setting_bubble_model.h"
18 #include "chrome/browser/ui/content_settings/content_setting_media_menu_model.h"
19 #include "chrome/browser/ui/views/browser_dialogs.h"
20 #include "chrome/grit/generated_resources.h"
21 #include "components/content_settings/core/browser/host_content_settings_map.h"
22 #include "content/public/browser/plugin_service.h"
23 #include "content/public/browser/web_contents.h"
24 #include "ui/base/cursor/cursor.h"
25 #include "ui/base/l10n/l10n_util.h"
26 #include "ui/base/models/simple_menu_model.h"
27 #include "ui/base/resource/resource_bundle.h"
28 #include "ui/gfx/font_list.h"
29 #include "ui/gfx/text_utils.h"
30 #include "ui/views/controls/button/label_button.h"
31 #include "ui/views/controls/button/menu_button.h"
32 #include "ui/views/controls/button/radio_button.h"
33 #include "ui/views/controls/image_view.h"
34 #include "ui/views/controls/label.h"
35 #include "ui/views/controls/link.h"
36 #include "ui/views/controls/menu/menu.h"
37 #include "ui/views/controls/menu/menu_config.h"
38 #include "ui/views/controls/menu/menu_runner.h"
39 #include "ui/views/controls/separator.h"
40 #include "ui/views/layout/grid_layout.h"
41 #include "ui/views/layout/layout_constants.h"
42 #include "ui/views/native_cursor.h"
46 // If we don't clamp the maximum width, then very long URLs and titles can make
47 // the bubble arbitrarily wide.
48 const int kMaxContentsWidth
= 500;
50 // When we have multiline labels, we should set a minimum width lest we get very
51 // narrow bubbles with lots of line-wrapping.
52 const int kMinMultiLineContentsWidth
= 250;
54 // The minimum width of the media menu buttons.
55 const int kMinMediaMenuButtonWidth
= 150;
59 using content::PluginService
;
60 using content::WebContents
;
63 // ContentSettingBubbleContents::Favicon --------------------------------------
65 class ContentSettingBubbleContents::Favicon
: public views::ImageView
{
67 Favicon(const gfx::Image
& image
,
68 ContentSettingBubbleContents
* parent
,
73 // views::View overrides:
74 bool OnMousePressed(const ui::MouseEvent
& event
) override
;
75 void OnMouseReleased(const ui::MouseEvent
& event
) override
;
76 gfx::NativeCursor
GetCursor(const ui::MouseEvent
& event
) override
;
78 ContentSettingBubbleContents
* parent_
;
82 ContentSettingBubbleContents::Favicon::Favicon(
83 const gfx::Image
& image
,
84 ContentSettingBubbleContents
* parent
,
88 SetImage(image
.AsImageSkia());
91 ContentSettingBubbleContents::Favicon::~Favicon() {
94 bool ContentSettingBubbleContents::Favicon::OnMousePressed(
95 const ui::MouseEvent
& event
) {
96 return event
.IsLeftMouseButton() || event
.IsMiddleMouseButton();
99 void ContentSettingBubbleContents::Favicon::OnMouseReleased(
100 const ui::MouseEvent
& event
) {
101 if ((event
.IsLeftMouseButton() || event
.IsMiddleMouseButton()) &&
102 HitTestPoint(event
.location())) {
103 parent_
->LinkClicked(link_
, event
.flags());
107 gfx::NativeCursor
ContentSettingBubbleContents::Favicon::GetCursor(
108 const ui::MouseEvent
& event
) {
109 return views::GetNativeHandCursor();
113 // ContentSettingBubbleContents::MediaMenuParts -------------------------------
115 struct ContentSettingBubbleContents::MediaMenuParts
{
116 explicit MediaMenuParts(content::MediaStreamType type
);
119 content::MediaStreamType type
;
120 scoped_ptr
<ui::SimpleMenuModel
> menu_model
;
123 DISALLOW_COPY_AND_ASSIGN(MediaMenuParts
);
126 ContentSettingBubbleContents::MediaMenuParts::MediaMenuParts(
127 content::MediaStreamType type
)
130 ContentSettingBubbleContents::MediaMenuParts::~MediaMenuParts() {}
132 // ContentSettingBubbleContents -----------------------------------------------
134 ContentSettingBubbleContents::ContentSettingBubbleContents(
135 ContentSettingBubbleModel
* content_setting_bubble_model
,
136 content::WebContents
* web_contents
,
137 views::View
* anchor_view
,
138 views::BubbleBorder::Arrow arrow
)
139 : content::WebContentsObserver(web_contents
),
140 BubbleDelegateView(anchor_view
, arrow
),
141 content_setting_bubble_model_(content_setting_bubble_model
),
144 learn_more_link_(NULL
),
145 close_button_(NULL
) {
146 // Compensate for built-in vertical padding in the anchor view's image.
147 set_anchor_view_insets(gfx::Insets(5, 0, 5, 0));
150 ContentSettingBubbleContents::~ContentSettingBubbleContents() {
151 STLDeleteValues(&media_menus_
);
154 gfx::Size
ContentSettingBubbleContents::GetPreferredSize() const {
155 gfx::Size
preferred_size(views::View::GetPreferredSize());
156 int preferred_width
=
157 (!content_setting_bubble_model_
->bubble_content().domain_lists
.empty() &&
158 (kMinMultiLineContentsWidth
> preferred_size
.width())) ?
159 kMinMultiLineContentsWidth
: preferred_size
.width();
160 preferred_size
.set_width(std::min(preferred_width
, kMaxContentsWidth
));
161 return preferred_size
;
164 void ContentSettingBubbleContents::UpdateMenuLabel(
165 content::MediaStreamType type
,
166 const std::string
& label
) {
167 for (MediaMenuPartsMap::const_iterator it
= media_menus_
.begin();
168 it
!= media_menus_
.end(); ++it
) {
169 if (it
->second
->type
== type
) {
170 it
->first
->SetText(base::UTF8ToUTF16(label
));
177 void ContentSettingBubbleContents::Init() {
178 using views::GridLayout
;
180 GridLayout
* layout
= new views::GridLayout(this);
181 SetLayoutManager(layout
);
183 const int kSingleColumnSetId
= 0;
184 views::ColumnSet
* column_set
= layout
->AddColumnSet(kSingleColumnSetId
);
185 column_set
->AddColumn(GridLayout::LEADING
, GridLayout::FILL
, 1,
186 GridLayout::USE_PREF
, 0, 0);
187 column_set
->AddPaddingColumn(0, views::kRelatedControlHorizontalSpacing
);
188 column_set
->AddColumn(GridLayout::LEADING
, GridLayout::FILL
, 1,
189 GridLayout::USE_PREF
, 0, 0);
191 const ContentSettingBubbleModel::BubbleContent
& bubble_content
=
192 content_setting_bubble_model_
->bubble_content();
193 bool bubble_content_empty
= true;
195 if (!bubble_content
.title
.empty()) {
196 views::Label
* title_label
= new views::Label(base::UTF8ToUTF16(
197 bubble_content
.title
));
198 title_label
->SetMultiLine(true);
199 title_label
->SetHorizontalAlignment(gfx::ALIGN_LEFT
);
200 layout
->StartRow(0, kSingleColumnSetId
);
201 layout
->AddView(title_label
);
202 bubble_content_empty
= false;
205 if (!bubble_content
.learn_more_link
.empty()) {
207 new views::Link(base::UTF8ToUTF16(bubble_content
.learn_more_link
));
208 learn_more_link_
->set_listener(this);
209 learn_more_link_
->SetHorizontalAlignment(gfx::ALIGN_LEFT
);
210 layout
->AddView(learn_more_link_
);
211 bubble_content_empty
= false;
214 // Layout for the item list (blocked plugins and popups).
215 if (!bubble_content
.list_items
.empty()) {
216 const int kItemListColumnSetId
= 2;
217 views::ColumnSet
* item_list_column_set
=
218 layout
->AddColumnSet(kItemListColumnSetId
);
219 item_list_column_set
->AddColumn(GridLayout::LEADING
, GridLayout::FILL
, 0,
220 GridLayout::USE_PREF
, 0, 0);
221 item_list_column_set
->AddPaddingColumn(
222 0, views::kRelatedControlHorizontalSpacing
);
223 item_list_column_set
->AddColumn(GridLayout::LEADING
, GridLayout::FILL
, 1,
224 GridLayout::USE_PREF
, 0, 0);
227 for (const ContentSettingBubbleModel::ListItem
& list_item
:
228 bubble_content
.list_items
) {
229 if (!bubble_content_empty
)
230 layout
->AddPaddingRow(0, views::kRelatedControlVerticalSpacing
);
231 layout
->StartRow(0, kItemListColumnSetId
);
232 if (list_item
.has_link
) {
233 views::Link
* link
= new views::Link(base::UTF8ToUTF16(list_item
.title
));
234 link
->set_listener(this);
235 link
->SetElideBehavior(gfx::ELIDE_MIDDLE
);
236 list_item_links_
[link
] = row
;
237 layout
->AddView(new Favicon(list_item
.image
, this, link
));
238 layout
->AddView(link
);
240 views::Label
* label
=
241 new views::Label(base::UTF8ToUTF16(list_item
.title
));
242 layout
->AddView(new Favicon(list_item
.image
, this, NULL
));
243 layout
->AddView(label
);
246 bubble_content_empty
= false;
250 const int indented_kSingleColumnSetId
= 3;
251 // Insert a column set with greater indent.
252 views::ColumnSet
* indented_single_column_set
=
253 layout
->AddColumnSet(indented_kSingleColumnSetId
);
254 indented_single_column_set
->AddPaddingColumn(0, views::kCheckboxIndent
);
255 indented_single_column_set
->AddColumn(GridLayout::LEADING
, GridLayout::FILL
,
256 1, GridLayout::USE_PREF
, 0, 0);
258 const ContentSettingBubbleModel::RadioGroup
& radio_group
=
259 bubble_content
.radio_group
;
260 if (!radio_group
.radio_items
.empty()) {
261 if (!bubble_content_empty
)
262 layout
->AddPaddingRow(0, views::kRelatedControlVerticalSpacing
);
263 for (ContentSettingBubbleModel::RadioItems::const_iterator
i(
264 radio_group
.radio_items
.begin());
265 i
!= radio_group
.radio_items
.end(); ++i
) {
266 views::RadioButton
* radio
=
267 new views::RadioButton(base::UTF8ToUTF16(*i
), 0);
268 radio
->SetEnabled(bubble_content
.radio_group_enabled
);
269 radio
->set_listener(this);
270 radio_group_
.push_back(radio
);
271 layout
->StartRow(0, indented_kSingleColumnSetId
);
272 layout
->AddView(radio
);
273 bubble_content_empty
= false;
275 DCHECK(!radio_group_
.empty());
276 // Now that the buttons have been added to the view hierarchy, it's safe
277 // to call SetChecked() on them.
278 radio_group_
[radio_group
.default_item
]->SetChecked(true);
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
= 4;
285 views::ColumnSet
* menu_column_set
=
286 layout
->AddColumnSet(kMediaMenuColumnSetId
);
287 menu_column_set
->AddPaddingColumn(0, views::kCheckboxIndent
);
288 menu_column_set
->AddColumn(GridLayout::LEADING
, GridLayout::FILL
, 0,
289 GridLayout::USE_PREF
, 0, 0);
290 menu_column_set
->AddPaddingColumn(
291 0, views::kRelatedControlHorizontalSpacing
);
292 menu_column_set
->AddColumn(GridLayout::LEADING
, GridLayout::FILL
, 1,
293 GridLayout::USE_PREF
, 0, 0);
295 for (ContentSettingBubbleModel::MediaMenuMap::const_iterator
i(
296 bubble_content
.media_menus
.begin());
297 i
!= bubble_content
.media_menus
.end(); ++i
) {
298 if (!bubble_content_empty
)
299 layout
->AddPaddingRow(0, views::kRelatedControlVerticalSpacing
);
300 layout
->StartRow(0, kMediaMenuColumnSetId
);
302 views::Label
* label
=
303 new views::Label(base::UTF8ToUTF16(i
->second
.label
));
304 label
->SetHorizontalAlignment(gfx::ALIGN_LEFT
);
306 views::MenuButton
* menu_button
= new views::MenuButton(
307 NULL
, base::UTF8ToUTF16((i
->second
.selected_device
.name
)),
309 menu_button
->SetStyle(views::Button::STYLE_BUTTON
);
310 menu_button
->SetHorizontalAlignment(gfx::ALIGN_LEFT
);
311 menu_button
->set_animate_on_state_change(false);
313 MediaMenuParts
* menu_view
= new MediaMenuParts(i
->first
);
314 menu_view
->menu_model
.reset(new ContentSettingMediaMenuModel(
316 content_setting_bubble_model_
.get(),
317 base::Bind(&ContentSettingBubbleContents::UpdateMenuLabel
,
318 base::Unretained(this))));
319 media_menus_
[menu_button
] = menu_view
;
321 if (!menu_view
->menu_model
->GetItemCount()) {
322 // Show a "None available" title and grey out the menu when there are
323 // no available devices.
324 menu_button
->SetText(
325 l10n_util::GetStringUTF16(IDS_MEDIA_MENU_NO_DEVICE_TITLE
));
326 menu_button
->SetEnabled(false);
329 // Disable the device selection when the website is managing the devices
331 if (i
->second
.disabled
)
332 menu_button
->SetEnabled(false);
334 layout
->AddView(label
);
335 layout
->AddView(menu_button
);
337 bubble_content_empty
= false;
341 UpdateMenuButtonSizes(GetNativeTheme());
343 const gfx::FontList
& domain_font
=
344 ui::ResourceBundle::GetSharedInstance().GetFontList(
345 ui::ResourceBundle::BoldFont
);
346 for (std::vector
<ContentSettingBubbleModel::DomainList
>::const_iterator
i(
347 bubble_content
.domain_lists
.begin());
348 i
!= bubble_content
.domain_lists
.end(); ++i
) {
349 layout
->StartRow(0, kSingleColumnSetId
);
350 views::Label
* section_title
= new views::Label(base::UTF8ToUTF16(i
->title
));
351 section_title
->SetMultiLine(true);
352 section_title
->SetHorizontalAlignment(gfx::ALIGN_LEFT
);
353 layout
->AddView(section_title
, 1, 1, GridLayout::FILL
, GridLayout::LEADING
);
354 for (std::set
<std::string
>::const_iterator j
= i
->hosts
.begin();
355 j
!= i
->hosts
.end(); ++j
) {
356 layout
->StartRow(0, indented_kSingleColumnSetId
);
357 layout
->AddView(new views::Label(base::UTF8ToUTF16(*j
), domain_font
));
359 bubble_content_empty
= false;
362 if (!bubble_content
.custom_link
.empty()) {
364 new views::Link(base::UTF8ToUTF16(bubble_content
.custom_link
));
365 custom_link_
->SetEnabled(bubble_content
.custom_link_enabled
);
366 custom_link_
->set_listener(this);
367 if (!bubble_content_empty
)
368 layout
->AddPaddingRow(0, views::kRelatedControlVerticalSpacing
);
369 layout
->StartRow(0, kSingleColumnSetId
);
370 layout
->AddView(custom_link_
);
371 bubble_content_empty
= false;
374 const int kDoubleColumnSetId
= 1;
375 views::ColumnSet
* double_column_set
=
376 layout
->AddColumnSet(kDoubleColumnSetId
);
377 if (!bubble_content_empty
) {
378 layout
->AddPaddingRow(0, views::kRelatedControlVerticalSpacing
);
379 layout
->StartRow(0, kSingleColumnSetId
);
380 layout
->AddView(new views::Separator(views::Separator::HORIZONTAL
), 1, 1,
381 GridLayout::FILL
, GridLayout::FILL
);
382 layout
->AddPaddingRow(0, views::kRelatedControlVerticalSpacing
);
385 double_column_set
->AddColumn(GridLayout::LEADING
, GridLayout::CENTER
, 1,
386 GridLayout::USE_PREF
, 0, 0);
387 double_column_set
->AddPaddingColumn(
388 0, views::kUnrelatedControlHorizontalSpacing
);
389 double_column_set
->AddColumn(GridLayout::TRAILING
, GridLayout::CENTER
, 0,
390 GridLayout::USE_PREF
, 0, 0);
392 layout
->StartRow(0, kDoubleColumnSetId
);
394 new views::Link(base::UTF8ToUTF16(bubble_content
.manage_link
));
395 manage_link_
->set_listener(this);
396 layout
->AddView(manage_link_
);
399 new views::LabelButton(this, l10n_util::GetStringUTF16(IDS_DONE
));
400 close_button_
->SetStyle(views::Button::STYLE_BUTTON
);
401 layout
->AddView(close_button_
);
404 void ContentSettingBubbleContents::DidNavigateMainFrame(
405 const content::LoadCommittedDetails
& details
,
406 const content::FrameNavigateParams
& params
) {
407 // Content settings are based on the main frame, so if it switches then
409 content_setting_bubble_model_
->OnDoneClicked();
410 GetWidget()->Close();
413 void ContentSettingBubbleContents::OnNativeThemeChanged(
414 const ui::NativeTheme
* theme
) {
415 views::BubbleDelegateView::OnNativeThemeChanged(theme
);
416 UpdateMenuButtonSizes(theme
);
419 void ContentSettingBubbleContents::ButtonPressed(views::Button
* sender
,
420 const ui::Event
& event
) {
421 RadioGroup::const_iterator
i(
422 std::find(radio_group_
.begin(), radio_group_
.end(), sender
));
423 if (i
!= radio_group_
.end()) {
424 content_setting_bubble_model_
->OnRadioClicked(i
- radio_group_
.begin());
427 DCHECK_EQ(sender
, close_button_
);
428 content_setting_bubble_model_
->OnDoneClicked();
429 GetWidget()->Close();
432 void ContentSettingBubbleContents::LinkClicked(views::Link
* source
,
434 if (source
== learn_more_link_
) {
435 content_setting_bubble_model_
->OnLearnMoreLinkClicked();
436 GetWidget()->Close();
439 if (source
== custom_link_
) {
440 content_setting_bubble_model_
->OnCustomLinkClicked();
441 GetWidget()->Close();
444 if (source
== manage_link_
) {
445 GetWidget()->Close();
446 content_setting_bubble_model_
->OnManageLinkClicked();
447 // CAREFUL: Showing the settings window activates it, which deactivates the
448 // info bubble, which causes it to close, which deletes us.
452 ListItemLinks::const_iterator
i(list_item_links_
.find(source
));
453 DCHECK(i
!= list_item_links_
.end());
454 content_setting_bubble_model_
->OnListItemClicked(i
->second
);
457 void ContentSettingBubbleContents::OnMenuButtonClicked(
459 const gfx::Point
& point
) {
460 MediaMenuPartsMap::iterator
j(media_menus_
.find(
461 static_cast<views::MenuButton
*>(source
)));
462 DCHECK(j
!= media_menus_
.end());
463 menu_runner_
.reset(new views::MenuRunner(j
->second
->menu_model
.get(),
464 views::MenuRunner::HAS_MNEMONICS
));
466 gfx::Point screen_location
;
467 views::View::ConvertPointToScreen(j
->first
, &screen_location
);
469 menu_runner_
->RunMenuAt(source
->GetWidget(),
471 gfx::Rect(screen_location
, j
->first
->size()),
472 views::MENU_ANCHOR_TOPLEFT
,
473 ui::MENU_SOURCE_NONE
));
476 void ContentSettingBubbleContents::UpdateMenuButtonSizes(
477 const ui::NativeTheme
* theme
) {
478 const views::MenuConfig config
= views::MenuConfig(theme
);
479 const int margins
= config
.item_left_margin
+ config
.check_width
+
480 config
.label_to_arrow_padding
+ config
.arrow_width
+
481 config
.arrow_to_edge_padding
;
483 // The preferred media menu size sort of copies the logic in
484 // MenuItemView::CalculateDimensions(). When this was using TextButton, it
485 // completely coincidentally matched the logic in MenuItemView. We now need
486 // to redo this manually.
488 for (MediaMenuPartsMap::const_iterator i
= media_menus_
.begin();
489 i
!= media_menus_
.end(); ++i
) {
490 for (int j
= 0; j
< i
->second
->menu_model
->GetItemCount(); ++j
) {
491 int string_width
= gfx::GetStringWidth(
492 i
->second
->menu_model
->GetLabelAt(j
),
495 menu_width
= std::max(menu_width
, string_width
);
499 // Make sure the width is at least kMinMediaMenuButtonWidth. The
500 // maximum width will be clamped by kMaxContentsWidth of the view.
501 menu_width
= std::max(kMinMediaMenuButtonWidth
, menu_width
+ margins
);
503 for (MediaMenuPartsMap::const_iterator i
= media_menus_
.begin();
504 i
!= media_menus_
.end(); ++i
) {
505 i
->first
->SetMinSize(gfx::Size(menu_width
, 0));
506 i
->first
->SetMaxSize(gfx::Size(menu_width
, 0));