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" |
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" |
22 #include "grit/generated_resources.h" | 25 #include "grit/generated_resources.h" |
23 #include "ui/base/l10n/l10n_util.h" | 26 #include "ui/base/l10n/l10n_util.h" |
27 #include "ui/base/models/simple_menu_model.h" | |
28 #include "ui/views/controls/button/menu_button.h" | |
24 #include "ui/views/controls/button/radio_button.h" | 29 #include "ui/views/controls/button/radio_button.h" |
25 #include "ui/views/controls/button/text_button.h" | 30 #include "ui/views/controls/button/text_button.h" |
26 #include "ui/views/controls/image_view.h" | 31 #include "ui/views/controls/image_view.h" |
27 #include "ui/views/controls/label.h" | 32 #include "ui/views/controls/label.h" |
28 #include "ui/views/controls/link.h" | 33 #include "ui/views/controls/link.h" |
34 #include "ui/views/controls/menu/menu.h" | |
35 #include "ui/views/controls/menu/menu_model_adapter.h" | |
36 #include "ui/views/controls/menu/menu_runner.h" | |
29 #include "ui/views/controls/separator.h" | 37 #include "ui/views/controls/separator.h" |
30 #include "ui/views/layout/grid_layout.h" | 38 #include "ui/views/layout/grid_layout.h" |
31 #include "ui/views/layout/layout_constants.h" | 39 #include "ui/views/layout/layout_constants.h" |
32 | 40 |
33 #if defined(USE_AURA) | 41 #if defined(USE_AURA) |
34 #include "ui/base/cursor/cursor.h" | 42 #include "ui/base/cursor/cursor.h" |
35 #endif | 43 #endif |
36 | 44 |
45 namespace { | |
Peter Kasting
2013/02/06 22:11:18
Nit: Add blank line below
no longer working on chromium
2013/02/07 16:25:19
Done.
| |
37 // If we don't clamp the maximum width, then very long URLs and titles can make | 46 // If we don't clamp the maximum width, then very long URLs and titles can make |
38 // the bubble arbitrarily wide. | 47 // the bubble arbitrarily wide. |
39 const int kMaxContentsWidth = 500; | 48 const int kMaxContentsWidth = 500; |
40 | 49 |
41 // When we have multiline labels, we should set a minimum width lest we get very | 50 // When we have multiline labels, we should set a minimum width lest we get very |
42 // narrow bubbles with lots of line-wrapping. | 51 // narrow bubbles with lots of line-wrapping. |
43 const int kMinMultiLineContentsWidth = 250; | 52 const int kMinMultiLineContentsWidth = 250; |
44 | 53 |
54 // Color of the border of media menu button. | |
55 const SkColor kBorderLightColor = SkColorSetRGB(0xeb, 0xeb, 0xeb); | |
56 | |
57 // The width of the media menu button. | |
58 const int kMediaMenuButtonWidth = 300; | |
Peter Kasting
2013/02/06 22:11:18
This seems arbitrary. How was this decided? Shou
no longer working on chromium
2013/02/07 16:25:19
The width of the button is decided based on how th
Peter Kasting
2013/02/07 23:55:04
But that's the point: "how the UI looks" is almost
no longer working on chromium
2013/02/08 12:32:39
We actually don't allow the users to resize the bu
Peter Kasting
2013/02/09 01:46:34
I'm actually worried about not adapting to the len
no longer working on chromium
2013/02/09 14:53:51
Thanks for ideas. After thinking a bit, I still th
Peter Kasting
2013/02/09 17:42:46
What if the names of the devices are all identical
| |
59 | |
60 } | |
61 | |
45 using content::PluginService; | 62 using content::PluginService; |
46 using content::WebContents; | 63 using content::WebContents; |
47 | 64 |
48 class ContentSettingBubbleContents::Favicon : public views::ImageView { | 65 class ContentSettingBubbleContents::Favicon : public views::ImageView { |
49 public: | 66 public: |
50 Favicon(const gfx::Image& image, | 67 Favicon(const gfx::Image& image, |
51 ContentSettingBubbleContents* parent, | 68 ContentSettingBubbleContents* parent, |
52 views::Link* link); | 69 views::Link* link); |
53 virtual ~Favicon(); | 70 virtual ~Favicon(); |
54 | 71 |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
92 #if defined(USE_AURA) | 109 #if defined(USE_AURA) |
93 return ui::kCursorHand; | 110 return ui::kCursorHand; |
94 #elif defined(OS_WIN) | 111 #elif defined(OS_WIN) |
95 static HCURSOR g_hand_cursor = LoadCursor(NULL, IDC_HAND); | 112 static HCURSOR g_hand_cursor = LoadCursor(NULL, IDC_HAND); |
96 return g_hand_cursor; | 113 return g_hand_cursor; |
97 #endif | 114 #endif |
98 } | 115 } |
99 | 116 |
100 ContentSettingBubbleContents::ContentSettingBubbleContents( | 117 ContentSettingBubbleContents::ContentSettingBubbleContents( |
101 ContentSettingBubbleModel* content_setting_bubble_model, | 118 ContentSettingBubbleModel* content_setting_bubble_model, |
119 Profile* profile, | |
102 WebContents* web_contents, | 120 WebContents* web_contents, |
103 views::View* anchor_view, | 121 views::View* anchor_view, |
104 views::BubbleBorder::ArrowLocation arrow_location) | 122 views::BubbleBorder::ArrowLocation arrow_location) |
105 : BubbleDelegateView(anchor_view, arrow_location), | 123 : BubbleDelegateView(anchor_view, arrow_location), |
106 content_setting_bubble_model_(content_setting_bubble_model), | 124 content_setting_bubble_model_(content_setting_bubble_model), |
125 profile_(profile), | |
107 web_contents_(web_contents), | 126 web_contents_(web_contents), |
108 custom_link_(NULL), | 127 custom_link_(NULL), |
109 manage_link_(NULL), | 128 manage_link_(NULL), |
110 close_button_(NULL) { | 129 close_button_(NULL) { |
130 DCHECK(profile); | |
111 // Compensate for built-in vertical padding in the anchor view's image. | 131 // Compensate for built-in vertical padding in the anchor view's image. |
112 set_anchor_insets(gfx::Insets(5, 0, 5, 0)); | 132 set_anchor_insets(gfx::Insets(5, 0, 5, 0)); |
113 | 133 |
114 registrar_.Add(this, content::NOTIFICATION_WEB_CONTENTS_DESTROYED, | 134 registrar_.Add(this, content::NOTIFICATION_WEB_CONTENTS_DESTROYED, |
115 content::Source<WebContents>(web_contents)); | 135 content::Source<WebContents>(web_contents)); |
116 } | 136 } |
117 | 137 |
118 ContentSettingBubbleContents::~ContentSettingBubbleContents() { | 138 ContentSettingBubbleContents::~ContentSettingBubbleContents() { |
139 // Free any MediaMenuParts objects left over. | |
Peter Kasting
2013/02/06 22:11:18
Nit: Comment unnecessary.
no longer working on chromium
2013/02/07 16:25:19
Done.
| |
140 for (MediaMenuPartsMap::const_iterator it = media_menus_.begin(); | |
141 it != media_menus_.end(); ++it) { | |
142 delete it->second; | |
Peter Kasting
2013/02/06 22:11:18
Nit: I believe STLDeleteValues() does what you wan
no longer working on chromium
2013/02/07 16:25:19
Done.
| |
143 } | |
119 } | 144 } |
120 | 145 |
121 gfx::Size ContentSettingBubbleContents::GetPreferredSize() { | 146 gfx::Size ContentSettingBubbleContents::GetPreferredSize() { |
122 gfx::Size preferred_size(views::View::GetPreferredSize()); | 147 gfx::Size preferred_size(views::View::GetPreferredSize()); |
123 int preferred_width = | 148 int preferred_width = |
124 (!content_setting_bubble_model_->bubble_content().domain_lists.empty() && | 149 (!content_setting_bubble_model_->bubble_content().domain_lists.empty() && |
125 (kMinMultiLineContentsWidth > preferred_size.width())) ? | 150 (kMinMultiLineContentsWidth > preferred_size.width())) ? |
126 kMinMultiLineContentsWidth : preferred_size.width(); | 151 kMinMultiLineContentsWidth : preferred_size.width(); |
127 preferred_size.set_width(std::min(preferred_width, kMaxContentsWidth)); | 152 preferred_size.set_width(std::min(preferred_width, kMaxContentsWidth)); |
128 return preferred_size; | 153 return preferred_size; |
129 } | 154 } |
130 | 155 |
156 void ContentSettingBubbleContents::UpdateMenuLabel( | |
157 content::MediaStreamType type, | |
158 const std::string& label) { | |
159 MediaMenuPartsMap::const_iterator it = media_menus_.begin(); | |
Peter Kasting
2013/02/06 22:11:18
Nit: Declare this in the loop declaration.
You ca
no longer working on chromium
2013/02/07 16:25:19
Done.
| |
160 for (; it != media_menus_.end(); ++it) { | |
161 if (it->second->type == type) { | |
162 it->first->SetText(UTF8ToUTF16(label)); | |
163 break; | |
164 } | |
165 } | |
166 DCHECK(it != media_menus_.end()); | |
167 } | |
168 | |
131 void ContentSettingBubbleContents::Init() { | 169 void ContentSettingBubbleContents::Init() { |
132 using views::GridLayout; | 170 using views::GridLayout; |
133 | 171 |
134 GridLayout* layout = new views::GridLayout(this); | 172 GridLayout* layout = new views::GridLayout(this); |
135 SetLayoutManager(layout); | 173 SetLayoutManager(layout); |
136 | 174 |
137 const int kSingleColumnSetId = 0; | 175 const int kSingleColumnSetId = 0; |
138 views::ColumnSet* column_set = layout->AddColumnSet(kSingleColumnSetId); | 176 views::ColumnSet* column_set = layout->AddColumnSet(kSingleColumnSetId); |
139 column_set->AddColumn(GridLayout::LEADING, GridLayout::FILL, 1, | 177 column_set->AddColumn(GridLayout::LEADING, GridLayout::FILL, 1, |
140 GridLayout::USE_PREF, 0, 0); | 178 GridLayout::USE_PREF, 0, 0); |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
218 layout->StartRow(0, indented_kSingleColumnSetId); | 256 layout->StartRow(0, indented_kSingleColumnSetId); |
219 layout->AddView(radio); | 257 layout->AddView(radio); |
220 bubble_content_empty = false; | 258 bubble_content_empty = false; |
221 } | 259 } |
222 DCHECK(!radio_group_.empty()); | 260 DCHECK(!radio_group_.empty()); |
223 // Now that the buttons have been added to the view hierarchy, it's safe | 261 // Now that the buttons have been added to the view hierarchy, it's safe |
224 // to call SetChecked() on them. | 262 // to call SetChecked() on them. |
225 radio_group_[radio_group.default_item]->SetChecked(true); | 263 radio_group_[radio_group.default_item]->SetChecked(true); |
226 } | 264 } |
227 | 265 |
266 // Layout code for the media device menus. | |
267 if (content_setting_bubble_model_->content_type() == | |
268 CONTENT_SETTINGS_TYPE_MEDIASTREAM) { | |
269 const int kMediaMenuColumnSetId = 2; | |
270 views::ColumnSet* menu_column_set = | |
271 layout->AddColumnSet(kMediaMenuColumnSetId); | |
272 menu_column_set->AddColumn(GridLayout::LEADING, GridLayout::FILL, 0, | |
273 GridLayout::USE_PREF, 0, 0); | |
274 menu_column_set->AddPaddingColumn( | |
275 0, views::kRelatedControlHorizontalSpacing); | |
276 menu_column_set->AddColumn(GridLayout::LEADING, GridLayout::FILL, 1, | |
277 GridLayout::USE_PREF, 0, 0); | |
278 | |
279 for (ContentSettingBubbleModel::MediaMenuMap::const_iterator i( | |
280 bubble_content.media_menus.begin()); | |
281 i != bubble_content.media_menus.end(); ++i) { | |
282 if (!bubble_content_empty) | |
283 layout->AddPaddingRow(0, views::kRelatedControlVerticalSpacing); | |
284 layout->StartRow(0, kMediaMenuColumnSetId); | |
285 | |
286 views::Label* menu_label = new views::Label( | |
287 UTF8ToUTF16(i->second.label)); | |
Peter Kasting
2013/02/06 22:11:18
Nit: Bizarre indenting (all over this section; did
no longer working on chromium
2013/02/07 16:25:19
likely, I just got my windows working, so the VS e
| |
288 menu_label->SetHorizontalAlignment(gfx::ALIGN_LEFT); | |
289 gfx::Size menu_label_size = menu_label->GetPreferredSize(); | |
290 views::MenuButton* menu_button = | |
291 new views::MenuButton( | |
292 NULL, UTF8ToUTF16((i->second.selected_device.name)), this, fal se); | |
Peter Kasting
2013/02/06 22:11:18
Nit: 80 columns (due to indenting)
no longer working on chromium
2013/02/07 16:25:19
Done.
| |
293 menu_button->set_alignment(views::TextButton::ALIGN_LEFT); | |
294 menu_button->set_border(views::Border::CreateSolidSidedBorder( | |
295 1, 0, 1, 0, kBorderLightColor)); | |
296 menu_button->SetSize(gfx::Size(kMediaMenuButtonWidth, | |
297 menu_label_size.height())); | |
298 menu_button->SetVisible(true); | |
299 | |
300 layout->AddView(menu_label); | |
301 layout->AddView(menu_button); | |
302 // Store the menu view to the map. | |
Peter Kasting
2013/02/06 22:11:18
Nit: This comment adds nothing.
no longer working on chromium
2013/02/07 16:25:19
Done.
| |
303 MediaMenuParts* menu_view = new MediaMenuParts(i->first); | |
304 menu_view->menu_model.reset(new ContentSettingMediaMenuModel( | |
305 profile_, | |
306 i->second.default_device.type, | |
307 content_setting_bubble_model_.get(), | |
308 base::Bind(&ContentSettingBubbleContents::UpdateMenuLabel, | |
309 base::Unretained(this)))); | |
310 media_menus_[menu_button] = menu_view; | |
311 bubble_content_empty = false; | |
312 } | |
313 } | |
314 | |
228 gfx::Font domain_font = | 315 gfx::Font domain_font = |
229 views::Label().font().DeriveFont(0, gfx::Font::BOLD); | 316 views::Label().font().DeriveFont(0, gfx::Font::BOLD); |
230 for (std::vector<ContentSettingBubbleModel::DomainList>::const_iterator i( | 317 for (std::vector<ContentSettingBubbleModel::DomainList>::const_iterator i( |
231 bubble_content.domain_lists.begin()); | 318 bubble_content.domain_lists.begin()); |
232 i != bubble_content.domain_lists.end(); ++i) { | 319 i != bubble_content.domain_lists.end(); ++i) { |
233 layout->StartRow(0, kSingleColumnSetId); | 320 layout->StartRow(0, kSingleColumnSetId); |
234 views::Label* section_title = new views::Label(UTF8ToUTF16(i->title)); | 321 views::Label* section_title = new views::Label(UTF8ToUTF16(i->title)); |
235 section_title->SetMultiLine(true); | 322 section_title->SetMultiLine(true); |
236 section_title->SetHorizontalAlignment(gfx::ALIGN_LEFT); | 323 section_title->SetHorizontalAlignment(gfx::ALIGN_LEFT); |
237 layout->AddView(section_title, 1, 1, GridLayout::FILL, GridLayout::LEADING); | 324 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 | 400 // CAREFUL: Showing the settings window activates it, which deactivates the |
314 // info bubble, which causes it to close, which deletes us. | 401 // info bubble, which causes it to close, which deletes us. |
315 return; | 402 return; |
316 } | 403 } |
317 | 404 |
318 PopupLinks::const_iterator i(popup_links_.find(source)); | 405 PopupLinks::const_iterator i(popup_links_.find(source)); |
319 DCHECK(i != popup_links_.end()); | 406 DCHECK(i != popup_links_.end()); |
320 content_setting_bubble_model_->OnPopupClicked(i->second); | 407 content_setting_bubble_model_->OnPopupClicked(i->second); |
321 } | 408 } |
322 | 409 |
410 void ContentSettingBubbleContents::OnMenuButtonClicked( | |
411 views::View* source, | |
412 const gfx::Point& point) { | |
413 MediaMenuPartsMap::iterator i(media_menus_.find( | |
414 static_cast<views::MenuButton*>(source))); | |
415 DCHECK(i != media_menus_.end()); | |
416 | |
417 views::MenuModelAdapter menu_model_adapter(i->second->menu_model.get()); | |
418 menu_runner_.reset(new views::MenuRunner(menu_model_adapter.CreateMenu())); | |
419 | |
420 gfx::Point screen_location; | |
421 views::View::ConvertPointToScreen(i->first, &screen_location); | |
422 if (menu_runner_->RunMenuAt(source->GetWidget(), | |
423 i->first, | |
424 gfx::Rect(screen_location, i->first->size()), | |
425 views::MenuItemView::TOPRIGHT, | |
426 views::MenuRunner::HAS_MNEMONICS) == | |
427 views::MenuRunner::MENU_DELETED) | |
428 return; | |
Peter Kasting
2013/02/06 22:11:18
We return here anyway. There's no need to explici
no longer working on chromium
2013/02/07 16:25:19
We need it to get rid of a warning which complains
Peter Kasting
2013/02/07 23:55:04
There's an explicit way to note that you don't car
no longer working on chromium
2013/02/08 12:32:39
Done.
| |
429 } | |
430 | |
323 void ContentSettingBubbleContents::Observe( | 431 void ContentSettingBubbleContents::Observe( |
324 int type, | 432 int type, |
325 const content::NotificationSource& source, | 433 const content::NotificationSource& source, |
326 const content::NotificationDetails& details) { | 434 const content::NotificationDetails& details) { |
327 DCHECK_EQ(content::NOTIFICATION_WEB_CONTENTS_DESTROYED, type); | 435 DCHECK_EQ(content::NOTIFICATION_WEB_CONTENTS_DESTROYED, type); |
328 DCHECK(source == content::Source<WebContents>(web_contents_)); | 436 DCHECK(source == content::Source<WebContents>(web_contents_)); |
329 web_contents_ = NULL; | 437 web_contents_ = NULL; |
330 } | 438 } |
439 | |
440 ContentSettingBubbleContents::MediaMenuParts::MediaMenuParts( | |
441 content::MediaStreamType type) | |
442 : type(type) {} | |
443 | |
444 ContentSettingBubbleContents::MediaMenuParts::~MediaMenuParts() {} | |
OLD | NEW |