Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(58)

Side by Side Diff: chrome/browser/ui/views/content_setting_bubble_contents.cc

Issue 12208010: Adding device selection menus to the content setting bubble (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebased Created 7 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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/prefs/pref_service.h"
14 #include "base/stl_util.h"
12 #include "base/utf_string_conversions.h" 15 #include "base/utf_string_conversions.h"
13 #include "chrome/browser/content_settings/host_content_settings_map.h" 16 #include "chrome/browser/content_settings/host_content_settings_map.h"
14 #include "chrome/browser/plugins/plugin_finder.h" 17 #include "chrome/browser/plugins/plugin_finder.h"
15 #include "chrome/browser/plugins/plugin_metadata.h" 18 #include "chrome/browser/plugins/plugin_metadata.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 // The width of the media menu button.
60 const int kMediaMenuButtonWidth = 150;
61
62 }
63
45 using content::PluginService; 64 using content::PluginService;
46 using content::WebContents; 65 using content::WebContents;
47 66
48 class ContentSettingBubbleContents::Favicon : public views::ImageView { 67 class ContentSettingBubbleContents::Favicon : public views::ImageView {
49 public: 68 public:
50 Favicon(const gfx::Image& image, 69 Favicon(const gfx::Image& image,
51 ContentSettingBubbleContents* parent, 70 ContentSettingBubbleContents* parent,
52 views::Link* link); 71 views::Link* link);
53 virtual ~Favicon(); 72 virtual ~Favicon();
54 73
(...skipping 12 matching lines...) Expand all
67 ContentSettingBubbleContents* parent, 86 ContentSettingBubbleContents* parent,
68 views::Link* link) 87 views::Link* link)
69 : parent_(parent), 88 : parent_(parent),
70 link_(link) { 89 link_(link) {
71 SetImage(image.AsImageSkia()); 90 SetImage(image.AsImageSkia());
72 } 91 }
73 92
74 ContentSettingBubbleContents::Favicon::~Favicon() { 93 ContentSettingBubbleContents::Favicon::~Favicon() {
75 } 94 }
76 95
96 struct ContentSettingBubbleContents::MediaMenuParts {
97 explicit MediaMenuParts(content::MediaStreamType type);
98 ~MediaMenuParts();
99
100 content::MediaStreamType type;
101 scoped_ptr<ui::SimpleMenuModel> menu_model;
102
103 private:
104 DISALLOW_COPY_AND_ASSIGN(MediaMenuParts);
105 };
106
107 ContentSettingBubbleContents::MediaMenuParts::MediaMenuParts(
108 content::MediaStreamType type)
109 : type(type) {}
110
111 ContentSettingBubbleContents::MediaMenuParts::~MediaMenuParts() {}
112
77 bool ContentSettingBubbleContents::Favicon::OnMousePressed( 113 bool ContentSettingBubbleContents::Favicon::OnMousePressed(
78 const ui::MouseEvent& event) { 114 const ui::MouseEvent& event) {
79 return event.IsLeftMouseButton() || event.IsMiddleMouseButton(); 115 return event.IsLeftMouseButton() || event.IsMiddleMouseButton();
80 } 116 }
81 117
82 void ContentSettingBubbleContents::Favicon::OnMouseReleased( 118 void ContentSettingBubbleContents::Favicon::OnMouseReleased(
83 const ui::MouseEvent& event) { 119 const ui::MouseEvent& event) {
84 if ((event.IsLeftMouseButton() || event.IsMiddleMouseButton()) && 120 if ((event.IsLeftMouseButton() || event.IsMiddleMouseButton()) &&
85 HitTestPoint(event.location())) { 121 HitTestPoint(event.location())) {
86 parent_->LinkClicked(link_, event.flags()); 122 parent_->LinkClicked(link_, event.flags());
(...skipping 22 matching lines...) Expand all
109 manage_link_(NULL), 145 manage_link_(NULL),
110 close_button_(NULL) { 146 close_button_(NULL) {
111 // Compensate for built-in vertical padding in the anchor view's image. 147 // Compensate for built-in vertical padding in the anchor view's image.
112 set_anchor_insets(gfx::Insets(5, 0, 5, 0)); 148 set_anchor_insets(gfx::Insets(5, 0, 5, 0));
113 149
114 registrar_.Add(this, content::NOTIFICATION_WEB_CONTENTS_DESTROYED, 150 registrar_.Add(this, content::NOTIFICATION_WEB_CONTENTS_DESTROYED,
115 content::Source<WebContents>(web_contents)); 151 content::Source<WebContents>(web_contents));
116 } 152 }
117 153
118 ContentSettingBubbleContents::~ContentSettingBubbleContents() { 154 ContentSettingBubbleContents::~ContentSettingBubbleContents() {
155 STLDeleteValues(&media_menus_);
119 } 156 }
120 157
121 gfx::Size ContentSettingBubbleContents::GetPreferredSize() { 158 gfx::Size ContentSettingBubbleContents::GetPreferredSize() {
122 gfx::Size preferred_size(views::View::GetPreferredSize()); 159 gfx::Size preferred_size(views::View::GetPreferredSize());
123 int preferred_width = 160 int preferred_width =
124 (!content_setting_bubble_model_->bubble_content().domain_lists.empty() && 161 (!content_setting_bubble_model_->bubble_content().domain_lists.empty() &&
125 (kMinMultiLineContentsWidth > preferred_size.width())) ? 162 (kMinMultiLineContentsWidth > preferred_size.width())) ?
126 kMinMultiLineContentsWidth : preferred_size.width(); 163 kMinMultiLineContentsWidth : preferred_size.width();
127 preferred_size.set_width(std::min(preferred_width, kMaxContentsWidth)); 164 preferred_size.set_width(std::min(preferred_width, kMaxContentsWidth));
128 return preferred_size; 165 return preferred_size;
129 } 166 }
130 167
168 void ContentSettingBubbleContents::UpdateMenuLabel(
169 content::MediaStreamType type,
170 const std::string& label) {
171 for (MediaMenuPartsMap::const_iterator it = media_menus_.begin();
172 it != media_menus_.end(); ++it) {
173 if (it->second->type == type) {
174 it->first->SetText(UTF8ToUTF16(label));
175 return;
176 }
177 }
178 NOTREACHED();
179 }
180
131 void ContentSettingBubbleContents::Init() { 181 void ContentSettingBubbleContents::Init() {
132 using views::GridLayout; 182 using views::GridLayout;
133 183
134 GridLayout* layout = new views::GridLayout(this); 184 GridLayout* layout = new views::GridLayout(this);
135 SetLayoutManager(layout); 185 SetLayoutManager(layout);
136 186
137 const int kSingleColumnSetId = 0; 187 const int kSingleColumnSetId = 0;
138 views::ColumnSet* column_set = layout->AddColumnSet(kSingleColumnSetId); 188 views::ColumnSet* column_set = layout->AddColumnSet(kSingleColumnSetId);
139 column_set->AddColumn(GridLayout::LEADING, GridLayout::FILL, 1, 189 column_set->AddColumn(GridLayout::LEADING, GridLayout::FILL, 1,
140 GridLayout::USE_PREF, 0, 0); 190 GridLayout::USE_PREF, 0, 0);
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
218 layout->StartRow(0, indented_kSingleColumnSetId); 268 layout->StartRow(0, indented_kSingleColumnSetId);
219 layout->AddView(radio); 269 layout->AddView(radio);
220 bubble_content_empty = false; 270 bubble_content_empty = false;
221 } 271 }
222 DCHECK(!radio_group_.empty()); 272 DCHECK(!radio_group_.empty());
223 // Now that the buttons have been added to the view hierarchy, it's safe 273 // Now that the buttons have been added to the view hierarchy, it's safe
224 // to call SetChecked() on them. 274 // to call SetChecked() on them.
225 radio_group_[radio_group.default_item]->SetChecked(true); 275 radio_group_[radio_group.default_item]->SetChecked(true);
226 } 276 }
227 277
278 // Layout code for the media device menus.
279 if (content_setting_bubble_model_->content_type() ==
280 CONTENT_SETTINGS_TYPE_MEDIASTREAM) {
281 const int kMediaMenuColumnSetId = 2;
282 views::ColumnSet* menu_column_set =
283 layout->AddColumnSet(kMediaMenuColumnSetId);
284 menu_column_set->AddPaddingColumn(
285 0, chrome_style::kCheckboxIndent);
286 menu_column_set->AddColumn(GridLayout::LEADING, GridLayout::FILL, 0,
287 GridLayout::USE_PREF, 0, 0);
288 menu_column_set->AddPaddingColumn(
289 0, views::kRelatedControlHorizontalSpacing);
290 menu_column_set->AddColumn(GridLayout::LEADING, GridLayout::FILL, 1,
291 GridLayout::USE_PREF, 0, 0);
292
293 for (ContentSettingBubbleModel::MediaMenuMap::const_iterator i(
294 bubble_content.media_menus.begin());
295 i != bubble_content.media_menus.end(); ++i) {
296 if (!bubble_content_empty)
297 layout->AddPaddingRow(0, views::kRelatedControlVerticalSpacing);
298 layout->StartRow(0, kMediaMenuColumnSetId);
299
300 views::Label* menu_label =
301 new views::Label(UTF8ToUTF16(i->second.label));
302 menu_label->SetHorizontalAlignment(gfx::ALIGN_LEFT);
303 views::MenuButton* menu_button = new views::MenuButton(
304 NULL, UTF8ToUTF16((i->second.selected_device.name)), this, false);
305 menu_button->set_min_width(kMediaMenuButtonWidth);
306 menu_button->set_max_width(kMediaMenuButtonWidth);
307 menu_button->set_alignment(views::TextButton::ALIGN_LEFT);
308 menu_button->set_border(
309 new views::TextButtonNativeThemeBorder(menu_button));
310 menu_button->set_animate_on_state_change(false);
311 menu_button->SetEnabledColor(SK_ColorBLACK);
312 menu_button->SetHighlightColor(SK_ColorBLACK);
313 menu_button->SetHoverColor(SK_ColorBLACK);
314
315 layout->AddView(menu_label);
316 layout->AddView(menu_button);
317
318 MediaMenuParts* menu_view = new MediaMenuParts(i->first);
319 menu_view->menu_model.reset(new ContentSettingMediaMenuModel(
320 Profile::FromBrowserContext(
321 web_contents_->GetBrowserContext())->GetPrefs(),
322 i->second.default_device.type,
323 content_setting_bubble_model_.get(),
324 base::Bind(&ContentSettingBubbleContents::UpdateMenuLabel,
325 base::Unretained(this))));
326 media_menus_[menu_button] = menu_view;
327 bubble_content_empty = false;
328 }
329 }
330
228 gfx::Font domain_font = 331 gfx::Font domain_font =
229 views::Label().font().DeriveFont(0, gfx::Font::BOLD); 332 views::Label().font().DeriveFont(0, gfx::Font::BOLD);
230 for (std::vector<ContentSettingBubbleModel::DomainList>::const_iterator i( 333 for (std::vector<ContentSettingBubbleModel::DomainList>::const_iterator i(
231 bubble_content.domain_lists.begin()); 334 bubble_content.domain_lists.begin());
232 i != bubble_content.domain_lists.end(); ++i) { 335 i != bubble_content.domain_lists.end(); ++i) {
233 layout->StartRow(0, kSingleColumnSetId); 336 layout->StartRow(0, kSingleColumnSetId);
234 views::Label* section_title = new views::Label(UTF8ToUTF16(i->title)); 337 views::Label* section_title = new views::Label(UTF8ToUTF16(i->title));
235 section_title->SetMultiLine(true); 338 section_title->SetMultiLine(true);
236 section_title->SetHorizontalAlignment(gfx::ALIGN_LEFT); 339 section_title->SetHorizontalAlignment(gfx::ALIGN_LEFT);
237 layout->AddView(section_title, 1, 1, GridLayout::FILL, GridLayout::LEADING); 340 layout->AddView(section_title, 1, 1, GridLayout::FILL, GridLayout::LEADING);
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
313 // CAREFUL: Showing the settings window activates it, which deactivates the 416 // CAREFUL: Showing the settings window activates it, which deactivates the
314 // info bubble, which causes it to close, which deletes us. 417 // info bubble, which causes it to close, which deletes us.
315 return; 418 return;
316 } 419 }
317 420
318 PopupLinks::const_iterator i(popup_links_.find(source)); 421 PopupLinks::const_iterator i(popup_links_.find(source));
319 DCHECK(i != popup_links_.end()); 422 DCHECK(i != popup_links_.end());
320 content_setting_bubble_model_->OnPopupClicked(i->second); 423 content_setting_bubble_model_->OnPopupClicked(i->second);
321 } 424 }
322 425
426 void ContentSettingBubbleContents::OnMenuButtonClicked(
427 views::View* source,
428 const gfx::Point& point) {
429 MediaMenuPartsMap::iterator i(media_menus_.find(
430 static_cast<views::MenuButton*>(source)));
431 DCHECK(i != media_menus_.end());
432
433 views::MenuModelAdapter menu_model_adapter(i->second->menu_model.get());
434 menu_runner_.reset(new views::MenuRunner(menu_model_adapter.CreateMenu()));
435
436 gfx::Point screen_location;
437 views::View::ConvertPointToScreen(i->first, &screen_location);
438 ignore_result(menu_runner_->RunMenuAt(
439 source->GetWidget(),
440 i->first,
441 gfx::Rect(screen_location, i->first->size()),
442 views::MenuItemView::TOPRIGHT,
443 views::MenuRunner::HAS_MNEMONICS));
444 }
445
323 void ContentSettingBubbleContents::Observe( 446 void ContentSettingBubbleContents::Observe(
324 int type, 447 int type,
325 const content::NotificationSource& source, 448 const content::NotificationSource& source,
326 const content::NotificationDetails& details) { 449 const content::NotificationDetails& details) {
327 DCHECK_EQ(content::NOTIFICATION_WEB_CONTENTS_DESTROYED, type); 450 DCHECK_EQ(content::NOTIFICATION_WEB_CONTENTS_DESTROYED, type);
328 DCHECK(source == content::Source<WebContents>(web_contents_)); 451 DCHECK(source == content::Source<WebContents>(web_contents_));
329 web_contents_ = NULL; 452 web_contents_ = NULL;
330 } 453 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698