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

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/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 minimum width of the media menu button.
60 const int kMinMediaMenuButtonWidth = 100;
61
62 }
63
45 using content::PluginService; 64 using content::PluginService;
46 using content::WebContents; 65 using content::WebContents;
47 66
67
68 // Favicon --------------------------------------------------------------------
Peter Kasting 2013/02/18 18:23:18 Nit: I would write the class name as "ContentSetti
no longer working on chromium 2013/02/19 14:37:36 Done.
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
55 private: 77 private:
56 // views::View overrides: 78 // views::View overrides:
57 virtual bool OnMousePressed(const ui::MouseEvent& event) OVERRIDE; 79 virtual bool OnMousePressed(const ui::MouseEvent& event) OVERRIDE;
58 virtual void OnMouseReleased(const ui::MouseEvent& event) OVERRIDE; 80 virtual void OnMouseReleased(const ui::MouseEvent& event) OVERRIDE;
59 virtual gfx::NativeCursor GetCursor(const ui::MouseEvent& event) OVERRIDE; 81 virtual gfx::NativeCursor GetCursor(const ui::MouseEvent& event) OVERRIDE;
60 82
61 ContentSettingBubbleContents* parent_; 83 ContentSettingBubbleContents* parent_;
62 views::Link* link_; 84 views::Link* link_;
63 }; 85 };
64 86
65 ContentSettingBubbleContents::Favicon::Favicon( 87 ContentSettingBubbleContents::Favicon::Favicon(
66 const gfx::Image& image, 88 const gfx::Image& image,
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
100 // MediaMenuParts -------------------------------------------------------------
101
102 struct ContentSettingBubbleContents::MediaMenuParts {
103 explicit MediaMenuParts(content::MediaStreamType type);
104 ~MediaMenuParts();
105
106 content::MediaStreamType type;
107 scoped_ptr<ui::SimpleMenuModel> menu_model;
108
109 private:
110 DISALLOW_COPY_AND_ASSIGN(MediaMenuParts);
111 };
112
113 ContentSettingBubbleContents::MediaMenuParts::MediaMenuParts(
114 content::MediaStreamType type)
115 : type(type) {}
116
117 ContentSettingBubbleContents::MediaMenuParts::~MediaMenuParts() {}
118
77 bool ContentSettingBubbleContents::Favicon::OnMousePressed( 119 bool ContentSettingBubbleContents::Favicon::OnMousePressed(
78 const ui::MouseEvent& event) { 120 const ui::MouseEvent& event) {
79 return event.IsLeftMouseButton() || event.IsMiddleMouseButton(); 121 return event.IsLeftMouseButton() || event.IsMiddleMouseButton();
80 } 122 }
81 123
82 void ContentSettingBubbleContents::Favicon::OnMouseReleased( 124 void ContentSettingBubbleContents::Favicon::OnMouseReleased(
83 const ui::MouseEvent& event) { 125 const ui::MouseEvent& event) {
84 if ((event.IsLeftMouseButton() || event.IsMiddleMouseButton()) && 126 if ((event.IsLeftMouseButton() || event.IsMiddleMouseButton()) &&
85 HitTestPoint(event.location())) { 127 HitTestPoint(event.location())) {
86 parent_->LinkClicked(link_, event.flags()); 128 parent_->LinkClicked(link_, event.flags());
87 } 129 }
88 } 130 }
89 131
90 gfx::NativeCursor ContentSettingBubbleContents::Favicon::GetCursor( 132 gfx::NativeCursor ContentSettingBubbleContents::Favicon::GetCursor(
Peter Kasting 2013/02/18 18:23:18 This function needs to go with the rest of the cla
no longer working on chromium 2013/02/19 14:37:36 Done.
91 const ui::MouseEvent& event) { 133 const ui::MouseEvent& event) {
92 #if defined(USE_AURA) 134 #if defined(USE_AURA)
93 return ui::kCursorHand; 135 return ui::kCursorHand;
94 #elif defined(OS_WIN) 136 #elif defined(OS_WIN)
95 static HCURSOR g_hand_cursor = LoadCursor(NULL, IDC_HAND); 137 static HCURSOR g_hand_cursor = LoadCursor(NULL, IDC_HAND);
96 return g_hand_cursor; 138 return g_hand_cursor;
97 #endif 139 #endif
98 } 140 }
99 141
100 ContentSettingBubbleContents::ContentSettingBubbleContents( 142 ContentSettingBubbleContents::ContentSettingBubbleContents(
Peter Kasting 2013/02/18 18:23:18 You should have a divider above this.
no longer working on chromium 2013/02/19 14:37:36 Done.
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 max_menu_label_width = 0;
300 int menu_width = 0;
301 for (ContentSettingBubbleModel::MediaMenuMap::const_iterator i(
302 bubble_content.media_menus.begin());
303 i != bubble_content.media_menus.end(); ++i) {
304 if (!bubble_content_empty)
305 layout->AddPaddingRow(0, views::kRelatedControlVerticalSpacing);
306 layout->StartRow(0, kMediaMenuColumnSetId);
307
308 views::Label* menu_label =
309 new views::Label(UTF8ToUTF16(i->second.label));
310 menu_label->SetHorizontalAlignment(gfx::ALIGN_LEFT);
311 views::MenuButton* menu_button = new views::MenuButton(
312 NULL, UTF8ToUTF16((i->second.selected_device.name)), this, false);
313 menu_button->set_alignment(views::TextButton::ALIGN_LEFT);
314 menu_button->set_border(
315 new views::TextButtonNativeThemeBorder(menu_button));
316 menu_button->set_animate_on_state_change(false);
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->first,
323 content_setting_bubble_model_.get(),
324 base::Bind(&ContentSettingBubbleContents::UpdateMenuLabel,
325 base::Unretained(this))));
326 media_menus_[menu_button] = menu_view;
327
328 // Calculate the width for the menus based on the longest item in the
329 // menus and the available with of the bubble.
330 int menu_preferred_width = GetPreferredMediaMenuWidth(
331 menu_button, menu_view->menu_model.get());
332 menu_width = std::max(menu_width, menu_preferred_width);
333 max_menu_label_width = std::max(max_menu_label_width,
334 menu_label->GetPreferredSize().width());
335 int available_width =
336 layout->GetPreferredSize(this).width() -
337 chrome_style::kCheckboxIndent -
338 max_menu_label_width -
339 2 * views::kRelatedControlHorizontalSpacing;
340 menu_width = std::min(std::max(menu_width, kMinMediaMenuButtonWidth),
341 available_width);
Peter Kasting 2013/02/18 18:23:18 This code isn't right. The available_width you ca
no longer working on chromium 2013/02/19 14:37:36 The UI looks like that Microphone: menu_button Ca
342
343 layout->AddView(menu_label);
344 layout->AddView(menu_button);
345
346 bubble_content_empty = false;
347 }
348
349 // Set all the menus to the width we calculated above.
350 for (MediaMenuPartsMap::const_iterator i = media_menus_.begin();
351 i != media_menus_.end(); ++i) {
352 i->first->set_min_width(menu_width);
353 i->first->set_max_width(menu_width);
354 }
355 }
356
228 gfx::Font domain_font = 357 gfx::Font domain_font =
229 views::Label().font().DeriveFont(0, gfx::Font::BOLD); 358 views::Label().font().DeriveFont(0, gfx::Font::BOLD);
230 for (std::vector<ContentSettingBubbleModel::DomainList>::const_iterator i( 359 for (std::vector<ContentSettingBubbleModel::DomainList>::const_iterator i(
231 bubble_content.domain_lists.begin()); 360 bubble_content.domain_lists.begin());
232 i != bubble_content.domain_lists.end(); ++i) { 361 i != bubble_content.domain_lists.end(); ++i) {
233 layout->StartRow(0, kSingleColumnSetId); 362 layout->StartRow(0, kSingleColumnSetId);
234 views::Label* section_title = new views::Label(UTF8ToUTF16(i->title)); 363 views::Label* section_title = new views::Label(UTF8ToUTF16(i->title));
235 section_title->SetMultiLine(true); 364 section_title->SetMultiLine(true);
236 section_title->SetHorizontalAlignment(gfx::ALIGN_LEFT); 365 section_title->SetHorizontalAlignment(gfx::ALIGN_LEFT);
237 layout->AddView(section_title, 1, 1, GridLayout::FILL, GridLayout::LEADING); 366 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 442 // CAREFUL: Showing the settings window activates it, which deactivates the
314 // info bubble, which causes it to close, which deletes us. 443 // info bubble, which causes it to close, which deletes us.
315 return; 444 return;
316 } 445 }
317 446
318 PopupLinks::const_iterator i(popup_links_.find(source)); 447 PopupLinks::const_iterator i(popup_links_.find(source));
319 DCHECK(i != popup_links_.end()); 448 DCHECK(i != popup_links_.end());
320 content_setting_bubble_model_->OnPopupClicked(i->second); 449 content_setting_bubble_model_->OnPopupClicked(i->second);
321 } 450 }
322 451
452 void ContentSettingBubbleContents::OnMenuButtonClicked(
453 views::View* source,
454 const gfx::Point& point) {
455 MediaMenuPartsMap::iterator i(media_menus_.find(
456 static_cast<views::MenuButton*>(source)));
457 DCHECK(i != media_menus_.end());
458
459 views::MenuModelAdapter menu_model_adapter(i->second->menu_model.get());
460 menu_runner_.reset(new views::MenuRunner(menu_model_adapter.CreateMenu()));
461
462 gfx::Point screen_location;
463 views::View::ConvertPointToScreen(i->first, &screen_location);
464 ignore_result(menu_runner_->RunMenuAt(
465 source->GetWidget(),
466 i->first,
467 gfx::Rect(screen_location, i->first->size()),
468 views::MenuItemView::TOPLEFT,
469 views::MenuRunner::HAS_MNEMONICS));
470 }
471
323 void ContentSettingBubbleContents::Observe( 472 void ContentSettingBubbleContents::Observe(
324 int type, 473 int type,
325 const content::NotificationSource& source, 474 const content::NotificationSource& source,
326 const content::NotificationDetails& details) { 475 const content::NotificationDetails& details) {
327 DCHECK_EQ(content::NOTIFICATION_WEB_CONTENTS_DESTROYED, type); 476 DCHECK_EQ(content::NOTIFICATION_WEB_CONTENTS_DESTROYED, type);
328 DCHECK(source == content::Source<WebContents>(web_contents_)); 477 DCHECK(source == content::Source<WebContents>(web_contents_));
329 web_contents_ = NULL; 478 web_contents_ = NULL;
330 } 479 }
480
481 int ContentSettingBubbleContents::GetPreferredMediaMenuWidth(
482 views::MenuButton* button,
483 ui::SimpleMenuModel* menu_model) {
484 string16 title = button->text();
485
486 int width = button->GetPreferredSize().width();
487 for (int i = 0; i < menu_model->GetItemCount(); ++i) {
488 button->SetText(menu_model->GetLabelAt(i));
489 width = std::max(width, button->GetPreferredSize().width());
490 }
491
492 // Recover the title for the menu button.
493 button->SetText(title);
494 return width;
495 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698