OLD | NEW |
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" |
12 #include "base/utf_string_conversions.h" | 13 #include "base/utf_string_conversions.h" |
13 #include "chrome/browser/content_settings/host_content_settings_map.h" | 14 #include "chrome/browser/content_settings/host_content_settings_map.h" |
14 #include "chrome/browser/plugins/plugin_finder.h" | 15 #include "chrome/browser/plugins/plugin_finder.h" |
15 #include "chrome/browser/plugins/plugin_metadata.h" | 16 #include "chrome/browser/plugins/plugin_metadata.h" |
| 17 #include "chrome/browser/profiles/profile.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" |
18 #include "chrome/browser/ui/views/browser_dialogs.h" | 20 #include "chrome/browser/ui/views/browser_dialogs.h" |
19 #include "content/public/browser/notification_source.h" | 21 #include "content/public/browser/notification_source.h" |
20 #include "content/public/browser/notification_types.h" | 22 #include "content/public/browser/notification_types.h" |
21 #include "content/public/browser/plugin_service.h" | 23 #include "content/public/browser/plugin_service.h" |
22 #include "grit/generated_resources.h" | 24 #include "grit/generated_resources.h" |
23 #include "ui/base/l10n/l10n_util.h" | 25 #include "ui/base/l10n/l10n_util.h" |
| 26 #include "ui/views/controls/button/menu_button.h" |
24 #include "ui/views/controls/button/radio_button.h" | 27 #include "ui/views/controls/button/radio_button.h" |
25 #include "ui/views/controls/button/text_button.h" | 28 #include "ui/views/controls/button/text_button.h" |
26 #include "ui/views/controls/image_view.h" | 29 #include "ui/views/controls/image_view.h" |
27 #include "ui/views/controls/label.h" | 30 #include "ui/views/controls/label.h" |
28 #include "ui/views/controls/link.h" | 31 #include "ui/views/controls/link.h" |
| 32 #include "ui/views/controls/menu/menu.h" |
| 33 #include "ui/views/controls/menu/menu_model_adapter.h" |
| 34 #include "ui/views/controls/menu/menu_runner.h" |
29 #include "ui/views/controls/separator.h" | 35 #include "ui/views/controls/separator.h" |
30 #include "ui/views/layout/grid_layout.h" | 36 #include "ui/views/layout/grid_layout.h" |
31 #include "ui/views/layout/layout_constants.h" | 37 #include "ui/views/layout/layout_constants.h" |
32 | 38 |
33 #if defined(USE_AURA) | 39 #if defined(USE_AURA) |
34 #include "ui/base/cursor/cursor.h" | 40 #include "ui/base/cursor/cursor.h" |
35 #endif | 41 #endif |
36 | 42 |
37 // If we don't clamp the maximum width, then very long URLs and titles can make | 43 // If we don't clamp the maximum width, then very long URLs and titles can make |
38 // the bubble arbitrarily wide. | 44 // the bubble arbitrarily wide. |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
92 #if defined(USE_AURA) | 98 #if defined(USE_AURA) |
93 return ui::kCursorHand; | 99 return ui::kCursorHand; |
94 #elif defined(OS_WIN) | 100 #elif defined(OS_WIN) |
95 static HCURSOR g_hand_cursor = LoadCursor(NULL, IDC_HAND); | 101 static HCURSOR g_hand_cursor = LoadCursor(NULL, IDC_HAND); |
96 return g_hand_cursor; | 102 return g_hand_cursor; |
97 #endif | 103 #endif |
98 } | 104 } |
99 | 105 |
100 ContentSettingBubbleContents::ContentSettingBubbleContents( | 106 ContentSettingBubbleContents::ContentSettingBubbleContents( |
101 ContentSettingBubbleModel* content_setting_bubble_model, | 107 ContentSettingBubbleModel* content_setting_bubble_model, |
| 108 Profile* profile, |
102 WebContents* web_contents, | 109 WebContents* web_contents, |
103 views::View* anchor_view, | 110 views::View* anchor_view, |
104 views::BubbleBorder::ArrowLocation arrow_location) | 111 views::BubbleBorder::ArrowLocation arrow_location) |
105 : BubbleDelegateView(anchor_view, arrow_location), | 112 : BubbleDelegateView(anchor_view, arrow_location), |
106 content_setting_bubble_model_(content_setting_bubble_model), | 113 content_setting_bubble_model_(content_setting_bubble_model), |
| 114 profile_(profile), |
107 web_contents_(web_contents), | 115 web_contents_(web_contents), |
108 custom_link_(NULL), | 116 custom_link_(NULL), |
109 manage_link_(NULL), | 117 manage_link_(NULL), |
110 close_button_(NULL) { | 118 close_button_(NULL) { |
| 119 DCHECK(profile); |
111 // Compensate for built-in vertical padding in the anchor view's image. | 120 // Compensate for built-in vertical padding in the anchor view's image. |
112 set_anchor_insets(gfx::Insets(5, 0, 5, 0)); | 121 set_anchor_insets(gfx::Insets(5, 0, 5, 0)); |
113 | 122 |
114 registrar_.Add(this, content::NOTIFICATION_WEB_CONTENTS_DESTROYED, | 123 registrar_.Add(this, content::NOTIFICATION_WEB_CONTENTS_DESTROYED, |
115 content::Source<WebContents>(web_contents)); | 124 content::Source<WebContents>(web_contents)); |
116 } | 125 } |
117 | 126 |
118 ContentSettingBubbleContents::~ContentSettingBubbleContents() { | 127 ContentSettingBubbleContents::~ContentSettingBubbleContents() { |
| 128 // Free any MediaMenuParts objects left over. |
| 129 for (MediaMenuPartsMap::const_iterator it = media_menus_.begin(); |
| 130 it != media_menus_.end(); ++it) { |
| 131 delete it->second; |
| 132 } |
119 } | 133 } |
120 | 134 |
121 gfx::Size ContentSettingBubbleContents::GetPreferredSize() { | 135 gfx::Size ContentSettingBubbleContents::GetPreferredSize() { |
122 gfx::Size preferred_size(views::View::GetPreferredSize()); | 136 gfx::Size preferred_size(views::View::GetPreferredSize()); |
123 int preferred_width = | 137 int preferred_width = |
124 (!content_setting_bubble_model_->bubble_content().domain_lists.empty() && | 138 (!content_setting_bubble_model_->bubble_content().domain_lists.empty() && |
125 (kMinMultiLineContentsWidth > preferred_size.width())) ? | 139 (kMinMultiLineContentsWidth > preferred_size.width())) ? |
126 kMinMultiLineContentsWidth : preferred_size.width(); | 140 kMinMultiLineContentsWidth : preferred_size.width(); |
127 preferred_size.set_width(std::min(preferred_width, kMaxContentsWidth)); | 141 preferred_size.set_width(std::min(preferred_width, kMaxContentsWidth)); |
128 return preferred_size; | 142 return preferred_size; |
129 } | 143 } |
130 | 144 |
| 145 void ContentSettingBubbleContents::UpdateMenuLabel( |
| 146 content::MediaStreamType type, |
| 147 const std::string& label) { |
| 148 MediaMenuPartsMap::const_iterator it = media_menus_.begin(); |
| 149 for (; it != media_menus_.end(); ++it) { |
| 150 if (it->second->type == type) { |
| 151 it->first->SetText(UTF8ToUTF16(label)); |
| 152 break; |
| 153 } |
| 154 } |
| 155 DCHECK(it != media_menus_.end()); |
| 156 } |
| 157 |
131 void ContentSettingBubbleContents::Init() { | 158 void ContentSettingBubbleContents::Init() { |
132 using views::GridLayout; | 159 using views::GridLayout; |
133 | 160 |
134 GridLayout* layout = new views::GridLayout(this); | 161 GridLayout* layout = new views::GridLayout(this); |
135 SetLayoutManager(layout); | 162 SetLayoutManager(layout); |
136 | 163 |
137 const int kSingleColumnSetId = 0; | 164 const int kSingleColumnSetId = 0; |
138 views::ColumnSet* column_set = layout->AddColumnSet(kSingleColumnSetId); | 165 views::ColumnSet* column_set = layout->AddColumnSet(kSingleColumnSetId); |
139 column_set->AddColumn(GridLayout::LEADING, GridLayout::FILL, 1, | 166 column_set->AddColumn(GridLayout::LEADING, GridLayout::FILL, 1, |
140 GridLayout::USE_PREF, 0, 0); | 167 GridLayout::USE_PREF, 0, 0); |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
218 layout->StartRow(0, indented_kSingleColumnSetId); | 245 layout->StartRow(0, indented_kSingleColumnSetId); |
219 layout->AddView(radio); | 246 layout->AddView(radio); |
220 bubble_content_empty = false; | 247 bubble_content_empty = false; |
221 } | 248 } |
222 DCHECK(!radio_group_.empty()); | 249 DCHECK(!radio_group_.empty()); |
223 // Now that the buttons have been added to the view hierarchy, it's safe | 250 // Now that the buttons have been added to the view hierarchy, it's safe |
224 // to call SetChecked() on them. | 251 // to call SetChecked() on them. |
225 radio_group_[radio_group.default_item]->SetChecked(true); | 252 radio_group_[radio_group.default_item]->SetChecked(true); |
226 } | 253 } |
227 | 254 |
| 255 // Layout code for the media device menus. |
| 256 if (content_setting_bubble_model_->content_type() == |
| 257 CONTENT_SETTINGS_TYPE_MEDIASTREAM) { |
| 258 const int kMediaMenuColumnSetId = 2; |
| 259 views::ColumnSet* menu_column_set = |
| 260 layout->AddColumnSet(kMediaMenuColumnSetId); |
| 261 menu_column_set->AddColumn(GridLayout::LEADING, GridLayout::FILL, 0, |
| 262 GridLayout::USE_PREF, 0, 0); |
| 263 menu_column_set->AddPaddingColumn( |
| 264 0, views::kRelatedControlHorizontalSpacing); |
| 265 menu_column_set->AddColumn(GridLayout::LEADING, GridLayout::FILL, 1, |
| 266 GridLayout::USE_PREF, 0, 0); |
| 267 |
| 268 for (ContentSettingBubbleModel::MediaMenuMap::const_iterator i( |
| 269 bubble_content.media_menus.begin()); |
| 270 i != bubble_content.media_menus.end(); ++i) { |
| 271 if (!bubble_content_empty) |
| 272 layout->AddPaddingRow(0, views::kRelatedControlVerticalSpacing); |
| 273 layout->StartRow(0, kMediaMenuColumnSetId); |
| 274 |
| 275 views::Label* menu_label = new views::Label(UTF8ToUTF16(i->second.label)); |
| 276 menu_label->SetHorizontalAlignment(gfx::ALIGN_LEFT); |
| 277 layout->AddView(menu_label, 1, 1, GridLayout::FILL, GridLayout::LEADING); |
| 278 views::MenuButton* menu_button = |
| 279 new views::MenuButton(NULL, string16(), this, false); |
| 280 menu_button->set_alignment(views::TextButton::ALIGN_CENTER); |
| 281 menu_button->set_border(NULL); |
| 282 //menu_button->SetBoundsRect(GetOptionsButtonBounds()); |
| 283 |
| 284 layout->AddView(menu_label); |
| 285 layout->AddView(menu_button); |
| 286 // Store the menu view to the map. |
| 287 MediaMenuParts* menu_view = new MediaMenuParts(i->first); |
| 288 menu_view->menu_model.reset(new ContentSettingMediaMenuModel( |
| 289 profile_, |
| 290 i->second.default_device.type, |
| 291 content_setting_bubble_model_.get(), |
| 292 base::Bind(&ContentSettingBubbleContents::UpdateMenuLabel, |
| 293 base::Unretained(this)))); |
| 294 media_menus_[menu_button] = menu_view; |
| 295 bubble_content_empty = false; |
| 296 } |
| 297 } |
| 298 |
228 gfx::Font domain_font = | 299 gfx::Font domain_font = |
229 views::Label().font().DeriveFont(0, gfx::Font::BOLD); | 300 views::Label().font().DeriveFont(0, gfx::Font::BOLD); |
230 for (std::vector<ContentSettingBubbleModel::DomainList>::const_iterator i( | 301 for (std::vector<ContentSettingBubbleModel::DomainList>::const_iterator i( |
231 bubble_content.domain_lists.begin()); | 302 bubble_content.domain_lists.begin()); |
232 i != bubble_content.domain_lists.end(); ++i) { | 303 i != bubble_content.domain_lists.end(); ++i) { |
233 layout->StartRow(0, kSingleColumnSetId); | 304 layout->StartRow(0, kSingleColumnSetId); |
234 views::Label* section_title = new views::Label(UTF8ToUTF16(i->title)); | 305 views::Label* section_title = new views::Label(UTF8ToUTF16(i->title)); |
235 section_title->SetMultiLine(true); | 306 section_title->SetMultiLine(true); |
236 section_title->SetHorizontalAlignment(gfx::ALIGN_LEFT); | 307 section_title->SetHorizontalAlignment(gfx::ALIGN_LEFT); |
237 layout->AddView(section_title, 1, 1, GridLayout::FILL, GridLayout::LEADING); | 308 layout->AddView(section_title, 1, 1, GridLayout::FILL, GridLayout::LEADING); |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
313 // CAREFUL: Showing the settings window activates it, which deactivates the | 384 // CAREFUL: Showing the settings window activates it, which deactivates the |
314 // info bubble, which causes it to close, which deletes us. | 385 // info bubble, which causes it to close, which deletes us. |
315 return; | 386 return; |
316 } | 387 } |
317 | 388 |
318 PopupLinks::const_iterator i(popup_links_.find(source)); | 389 PopupLinks::const_iterator i(popup_links_.find(source)); |
319 DCHECK(i != popup_links_.end()); | 390 DCHECK(i != popup_links_.end()); |
320 content_setting_bubble_model_->OnPopupClicked(i->second); | 391 content_setting_bubble_model_->OnPopupClicked(i->second); |
321 } | 392 } |
322 | 393 |
| 394 void ContentSettingBubbleContents::OnMenuButtonClicked( |
| 395 views::View* source, |
| 396 const gfx::Point& point) { |
| 397 MediaMenuPartsMap::iterator i(media_menus_.find( |
| 398 static_cast<views::MenuButton*>(source))); |
| 399 DCHECK(i != media_menus_.end()); |
| 400 |
| 401 views::MenuModelAdapter menu_model_adapter(i->second->menu_model.get()); |
| 402 menu_runner_.reset(new views::MenuRunner(menu_model_adapter.CreateMenu())); |
| 403 |
| 404 gfx::Point screen_location; |
| 405 views::View::ConvertPointToScreen(i->first, &screen_location); |
| 406 if (menu_runner_->RunMenuAt(source->GetWidget(), |
| 407 i->first, |
| 408 gfx::Rect(screen_location, i->first->size()), |
| 409 views::MenuItemView::TOPRIGHT, |
| 410 views::MenuRunner::HAS_MNEMONICS) == |
| 411 views::MenuRunner::MENU_DELETED) |
| 412 return; |
| 413 } |
| 414 |
323 void ContentSettingBubbleContents::Observe( | 415 void ContentSettingBubbleContents::Observe( |
324 int type, | 416 int type, |
325 const content::NotificationSource& source, | 417 const content::NotificationSource& source, |
326 const content::NotificationDetails& details) { | 418 const content::NotificationDetails& details) { |
327 DCHECK_EQ(content::NOTIFICATION_WEB_CONTENTS_DESTROYED, type); | 419 DCHECK_EQ(content::NOTIFICATION_WEB_CONTENTS_DESTROYED, type); |
328 DCHECK(source == content::Source<WebContents>(web_contents_)); | 420 DCHECK(source == content::Source<WebContents>(web_contents_)); |
329 web_contents_ = NULL; | 421 web_contents_ = NULL; |
330 } | 422 } |
| 423 |
| 424 ContentSettingBubbleContents::MediaMenuParts::MediaMenuParts( |
| 425 content::MediaStreamType type) |
| 426 : type(type) {} |
| 427 |
| 428 ContentSettingBubbleContents::MediaMenuParts::~MediaMenuParts() {} |
OLD | NEW |