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

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: 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/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"
16 #include "chrome/browser/ui/chrome_style.h" 18 #include "chrome/browser/ui/chrome_style.h"
17 #include "chrome/browser/ui/content_settings/content_setting_bubble_model.h" 19 #include "chrome/browser/ui/content_settings/content_setting_bubble_model.h"
20 #include "chrome/browser/ui/content_settings/content_setting_media_menu_model.h"
18 #include "chrome/browser/ui/views/browser_dialogs.h" 21 #include "chrome/browser/ui/views/browser_dialogs.h"
19 #include "content/public/browser/notification_source.h" 22 #include "content/public/browser/notification_source.h"
20 #include "content/public/browser/notification_types.h" 23 #include "content/public/browser/notification_types.h"
21 #include "content/public/browser/plugin_service.h" 24 #include "content/public/browser/plugin_service.h"
25 #include "content/public/browser/web_contents.h"
22 #include "grit/generated_resources.h" 26 #include "grit/generated_resources.h"
27 #include "grit/theme_resources.h"
23 #include "ui/base/l10n/l10n_util.h" 28 #include "ui/base/l10n/l10n_util.h"
29 #include "ui/base/models/simple_menu_model.h"
30 #include "ui/views/controls/button/menu_button.h"
24 #include "ui/views/controls/button/radio_button.h" 31 #include "ui/views/controls/button/radio_button.h"
25 #include "ui/views/controls/button/text_button.h" 32 #include "ui/views/controls/button/text_button.h"
26 #include "ui/views/controls/image_view.h" 33 #include "ui/views/controls/image_view.h"
27 #include "ui/views/controls/label.h" 34 #include "ui/views/controls/label.h"
28 #include "ui/views/controls/link.h" 35 #include "ui/views/controls/link.h"
36 #include "ui/views/controls/menu/menu.h"
37 #include "ui/views/controls/menu/menu_model_adapter.h"
38 #include "ui/views/controls/menu/menu_runner.h"
29 #include "ui/views/controls/separator.h" 39 #include "ui/views/controls/separator.h"
30 #include "ui/views/layout/grid_layout.h" 40 #include "ui/views/layout/grid_layout.h"
31 #include "ui/views/layout/layout_constants.h" 41 #include "ui/views/layout/layout_constants.h"
32 42
33 #if defined(USE_AURA) 43 #if defined(USE_AURA)
34 #include "ui/base/cursor/cursor.h" 44 #include "ui/base/cursor/cursor.h"
35 #endif 45 #endif
36 46
47 namespace {
48
37 // If we don't clamp the maximum width, then very long URLs and titles can make 49 // If we don't clamp the maximum width, then very long URLs and titles can make
38 // the bubble arbitrarily wide. 50 // the bubble arbitrarily wide.
39 const int kMaxContentsWidth = 500; 51 const int kMaxContentsWidth = 500;
40 52
41 // When we have multiline labels, we should set a minimum width lest we get very 53 // When we have multiline labels, we should set a minimum width lest we get very
42 // narrow bubbles with lots of line-wrapping. 54 // narrow bubbles with lots of line-wrapping.
43 const int kMinMultiLineContentsWidth = 250; 55 const int kMinMultiLineContentsWidth = 250;
44 56
57 // The minimum width of the media menu button.
58 const int kMinMediaMenuButtonWidth = 100;
59
60 }
61
45 using content::PluginService; 62 using content::PluginService;
46 using content::WebContents; 63 using content::WebContents;
47 64
65
66 // ContentSettingBubbleContents::Favicon --------------------------------------
67
48 class ContentSettingBubbleContents::Favicon : public views::ImageView { 68 class ContentSettingBubbleContents::Favicon : public views::ImageView {
49 public: 69 public:
50 Favicon(const gfx::Image& image, 70 Favicon(const gfx::Image& image,
51 ContentSettingBubbleContents* parent, 71 ContentSettingBubbleContents* parent,
52 views::Link* link); 72 views::Link* link);
53 virtual ~Favicon(); 73 virtual ~Favicon();
54 74
55 private: 75 private:
56 // views::View overrides: 76 // views::View overrides:
57 virtual bool OnMousePressed(const ui::MouseEvent& event) OVERRIDE; 77 virtual bool OnMousePressed(const ui::MouseEvent& event) OVERRIDE;
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
89 109
90 gfx::NativeCursor ContentSettingBubbleContents::Favicon::GetCursor( 110 gfx::NativeCursor ContentSettingBubbleContents::Favicon::GetCursor(
91 const ui::MouseEvent& event) { 111 const ui::MouseEvent& event) {
92 #if defined(USE_AURA) 112 #if defined(USE_AURA)
93 return ui::kCursorHand; 113 return ui::kCursorHand;
94 #elif defined(OS_WIN) 114 #elif defined(OS_WIN)
95 static HCURSOR g_hand_cursor = LoadCursor(NULL, IDC_HAND); 115 static HCURSOR g_hand_cursor = LoadCursor(NULL, IDC_HAND);
96 return g_hand_cursor; 116 return g_hand_cursor;
97 #endif 117 #endif
98 } 118 }
99 119
Peter Kasting 2013/02/20 05:46:20 Nit: One more blank line
no longer working on chromium 2013/02/20 15:23:40 Done.
120 // ContentSettingBubbleContents::MediaMenuParts -------------------------------
121
122 struct ContentSettingBubbleContents::MediaMenuParts {
123 explicit MediaMenuParts(content::MediaStreamType type);
124 ~MediaMenuParts();
125
126 content::MediaStreamType type;
127 scoped_ptr<ui::SimpleMenuModel> menu_model;
128
129 private:
130 DISALLOW_COPY_AND_ASSIGN(MediaMenuParts);
131 };
132
133 ContentSettingBubbleContents::MediaMenuParts::MediaMenuParts(
134 content::MediaStreamType type)
135 : type(type) {}
136
137 ContentSettingBubbleContents::MediaMenuParts::~MediaMenuParts() {}
138
139
140 // ContentSettingBubbleContents -----------------------------------------------
141
100 ContentSettingBubbleContents::ContentSettingBubbleContents( 142 ContentSettingBubbleContents::ContentSettingBubbleContents(
101 ContentSettingBubbleModel* content_setting_bubble_model, 143 ContentSettingBubbleModel* content_setting_bubble_model,
102 WebContents* web_contents, 144 WebContents* web_contents,
103 views::View* anchor_view, 145 views::View* anchor_view,
104 views::BubbleBorder::ArrowLocation arrow_location) 146 views::BubbleBorder::ArrowLocation arrow_location)
105 : BubbleDelegateView(anchor_view, arrow_location), 147 : BubbleDelegateView(anchor_view, arrow_location),
106 content_setting_bubble_model_(content_setting_bubble_model), 148 content_setting_bubble_model_(content_setting_bubble_model),
107 web_contents_(web_contents), 149 web_contents_(web_contents),
108 custom_link_(NULL), 150 custom_link_(NULL),
109 manage_link_(NULL), 151 manage_link_(NULL),
110 close_button_(NULL) { 152 close_button_(NULL) {
111 // Compensate for built-in vertical padding in the anchor view's image. 153 // Compensate for built-in vertical padding in the anchor view's image.
112 set_anchor_insets(gfx::Insets(5, 0, 5, 0)); 154 set_anchor_insets(gfx::Insets(5, 0, 5, 0));
113 155
114 registrar_.Add(this, content::NOTIFICATION_WEB_CONTENTS_DESTROYED, 156 registrar_.Add(this, content::NOTIFICATION_WEB_CONTENTS_DESTROYED,
115 content::Source<WebContents>(web_contents)); 157 content::Source<WebContents>(web_contents));
116 } 158 }
117 159
118 ContentSettingBubbleContents::~ContentSettingBubbleContents() { 160 ContentSettingBubbleContents::~ContentSettingBubbleContents() {
161 STLDeleteValues(&media_menus_);
119 } 162 }
120 163
121 gfx::Size ContentSettingBubbleContents::GetPreferredSize() { 164 gfx::Size ContentSettingBubbleContents::GetPreferredSize() {
122 gfx::Size preferred_size(views::View::GetPreferredSize()); 165 gfx::Size preferred_size(views::View::GetPreferredSize());
123 int preferred_width = 166 int preferred_width =
124 (!content_setting_bubble_model_->bubble_content().domain_lists.empty() && 167 (!content_setting_bubble_model_->bubble_content().domain_lists.empty() &&
125 (kMinMultiLineContentsWidth > preferred_size.width())) ? 168 (kMinMultiLineContentsWidth > preferred_size.width())) ?
126 kMinMultiLineContentsWidth : preferred_size.width(); 169 kMinMultiLineContentsWidth : preferred_size.width();
127 preferred_size.set_width(std::min(preferred_width, kMaxContentsWidth)); 170 preferred_size.set_width(std::min(preferred_width, kMaxContentsWidth));
128 return preferred_size; 171 return preferred_size;
129 } 172 }
130 173
174 void ContentSettingBubbleContents::UpdateMenuLabel(
175 content::MediaStreamType type,
176 const std::string& label) {
177 for (MediaMenuPartsMap::const_iterator it = media_menus_.begin();
178 it != media_menus_.end(); ++it) {
179 if (it->second->type == type) {
180 it->first->SetText(UTF8ToUTF16(label));
181 return;
182 }
183 }
184 NOTREACHED();
185 }
186
131 void ContentSettingBubbleContents::Init() { 187 void ContentSettingBubbleContents::Init() {
132 using views::GridLayout; 188 using views::GridLayout;
133 189
134 GridLayout* layout = new views::GridLayout(this); 190 GridLayout* layout = new views::GridLayout(this);
135 SetLayoutManager(layout); 191 SetLayoutManager(layout);
136 192
137 const int kSingleColumnSetId = 0; 193 const int kSingleColumnSetId = 0;
138 views::ColumnSet* column_set = layout->AddColumnSet(kSingleColumnSetId); 194 views::ColumnSet* column_set = layout->AddColumnSet(kSingleColumnSetId);
139 column_set->AddColumn(GridLayout::LEADING, GridLayout::FILL, 1, 195 column_set->AddColumn(GridLayout::LEADING, GridLayout::FILL, 1,
140 GridLayout::USE_PREF, 0, 0); 196 GridLayout::USE_PREF, 0, 0);
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
218 layout->StartRow(0, indented_kSingleColumnSetId); 274 layout->StartRow(0, indented_kSingleColumnSetId);
219 layout->AddView(radio); 275 layout->AddView(radio);
220 bubble_content_empty = false; 276 bubble_content_empty = false;
221 } 277 }
222 DCHECK(!radio_group_.empty()); 278 DCHECK(!radio_group_.empty());
223 // Now that the buttons have been added to the view hierarchy, it's safe 279 // Now that the buttons have been added to the view hierarchy, it's safe
224 // to call SetChecked() on them. 280 // to call SetChecked() on them.
225 radio_group_[radio_group.default_item]->SetChecked(true); 281 radio_group_[radio_group.default_item]->SetChecked(true);
226 } 282 }
227 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(
291 0, chrome_style::kCheckboxIndent);
292 menu_column_set->AddColumn(GridLayout::LEADING, GridLayout::FILL, 0,
293 GridLayout::USE_PREF, 0, 0);
294 menu_column_set->AddPaddingColumn(
295 0, views::kRelatedControlHorizontalSpacing);
296 menu_column_set->AddColumn(GridLayout::LEADING, GridLayout::FILL, 1,
297 GridLayout::USE_PREF, 0, 0);
298
299 int menu_width = 0;
300 for (ContentSettingBubbleModel::MediaMenuMap::const_iterator i(
301 bubble_content.media_menus.begin());
302 i != bubble_content.media_menus.end(); ++i) {
303 if (!bubble_content_empty)
304 layout->AddPaddingRow(0, views::kRelatedControlVerticalSpacing);
305 layout->StartRow(0, kMediaMenuColumnSetId);
306
307 views::Label* label = new views::Label(UTF8ToUTF16(i->second.label));
308 label->SetHorizontalAlignment(gfx::ALIGN_LEFT);
309
310 views::MenuButton* menu_button = new views::MenuButton(
311 NULL, UTF8ToUTF16((i->second.selected_device.name)), this, false);
312 menu_button->set_alignment(views::TextButton::ALIGN_LEFT);
313 menu_button->set_border(
314 new views::TextButtonNativeThemeBorder(menu_button));
315 menu_button->set_animate_on_state_change(false);
316
317 MediaMenuParts* menu_view = new MediaMenuParts(i->first);
318 menu_view->menu_model.reset(new ContentSettingMediaMenuModel(
319 i->first,
320 content_setting_bubble_model_.get(),
321 base::Bind(&ContentSettingBubbleContents::UpdateMenuLabel,
322 base::Unretained(this))));
323 media_menus_[menu_button] = menu_view;
324
325 // Use the longest width of the menus as the width of the menu buttons.
326 menu_width = std::max(menu_width,
327 GetPreferredMediaMenuWidth(
328 menu_button, menu_view->menu_model.get()));
Peter Kasting 2013/02/20 05:46:20 Cool, this is much simpler. We may want to put a
no longer working on chromium 2013/02/20 15:23:40 Done. I added a maximum width (600px) for the menu
329
330 layout->AddView(label);
331 layout->AddView(menu_button);
332
333 bubble_content_empty = false;
334 }
335
336 // Set all the menu buttons to the width we calculated above.
337 for (MediaMenuPartsMap::const_iterator i = media_menus_.begin();
338 i != media_menus_.end(); ++i) {
339 i->first->set_min_width(menu_width);
340 i->first->set_max_width(menu_width);
341 }
342 }
343
228 gfx::Font domain_font = 344 gfx::Font domain_font =
229 views::Label().font().DeriveFont(0, gfx::Font::BOLD); 345 views::Label().font().DeriveFont(0, gfx::Font::BOLD);
230 for (std::vector<ContentSettingBubbleModel::DomainList>::const_iterator i( 346 for (std::vector<ContentSettingBubbleModel::DomainList>::const_iterator i(
231 bubble_content.domain_lists.begin()); 347 bubble_content.domain_lists.begin());
232 i != bubble_content.domain_lists.end(); ++i) { 348 i != bubble_content.domain_lists.end(); ++i) {
233 layout->StartRow(0, kSingleColumnSetId); 349 layout->StartRow(0, kSingleColumnSetId);
234 views::Label* section_title = new views::Label(UTF8ToUTF16(i->title)); 350 views::Label* section_title = new views::Label(UTF8ToUTF16(i->title));
235 section_title->SetMultiLine(true); 351 section_title->SetMultiLine(true);
236 section_title->SetHorizontalAlignment(gfx::ALIGN_LEFT); 352 section_title->SetHorizontalAlignment(gfx::ALIGN_LEFT);
237 layout->AddView(section_title, 1, 1, GridLayout::FILL, GridLayout::LEADING); 353 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 429 // CAREFUL: Showing the settings window activates it, which deactivates the
314 // info bubble, which causes it to close, which deletes us. 430 // info bubble, which causes it to close, which deletes us.
315 return; 431 return;
316 } 432 }
317 433
318 PopupLinks::const_iterator i(popup_links_.find(source)); 434 PopupLinks::const_iterator i(popup_links_.find(source));
319 DCHECK(i != popup_links_.end()); 435 DCHECK(i != popup_links_.end());
320 content_setting_bubble_model_->OnPopupClicked(i->second); 436 content_setting_bubble_model_->OnPopupClicked(i->second);
321 } 437 }
322 438
439 void ContentSettingBubbleContents::OnMenuButtonClicked(
440 views::View* source,
441 const gfx::Point& point) {
442 MediaMenuPartsMap::iterator i(media_menus_.find(
443 static_cast<views::MenuButton*>(source)));
444 DCHECK(i != media_menus_.end());
445
446 views::MenuModelAdapter menu_model_adapter(i->second->menu_model.get());
447 menu_runner_.reset(new views::MenuRunner(menu_model_adapter.CreateMenu()));
448
449 gfx::Point screen_location;
450 views::View::ConvertPointToScreen(i->first, &screen_location);
451 ignore_result(menu_runner_->RunMenuAt(
452 source->GetWidget(),
453 i->first,
454 gfx::Rect(screen_location, i->first->size()),
455 views::MenuItemView::TOPLEFT,
456 views::MenuRunner::HAS_MNEMONICS));
457 }
458
323 void ContentSettingBubbleContents::Observe( 459 void ContentSettingBubbleContents::Observe(
324 int type, 460 int type,
325 const content::NotificationSource& source, 461 const content::NotificationSource& source,
326 const content::NotificationDetails& details) { 462 const content::NotificationDetails& details) {
327 DCHECK_EQ(content::NOTIFICATION_WEB_CONTENTS_DESTROYED, type); 463 DCHECK_EQ(content::NOTIFICATION_WEB_CONTENTS_DESTROYED, type);
328 DCHECK(source == content::Source<WebContents>(web_contents_)); 464 DCHECK(source == content::Source<WebContents>(web_contents_));
329 web_contents_ = NULL; 465 web_contents_ = NULL;
330 } 466 }
467
468 int ContentSettingBubbleContents::GetPreferredMediaMenuWidth(
469 views::MenuButton* button,
470 ui::SimpleMenuModel* menu_model) {
471 string16 title = button->text();
472
473 int width = button->GetPreferredSize().width();
474 for (int i = 0; i < menu_model->GetItemCount(); ++i) {
475 button->SetText(menu_model->GetLabelAt(i));
476 width = std::max(width, button->GetPreferredSize().width());
477 }
478
479 // Recover the title for the menu button.
480 button->SetText(title);
481 return width;
482 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698