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

Side by Side Diff: chrome/browser/ui/views/website_settings/permissions_bubble_view.cc

Issue 885373004: Use customization mode by default in permission bubbles (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Test fixes etc. Created 5 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
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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/website_settings/permissions_bubble_view.h" 5 #include "chrome/browser/ui/views/website_settings/permissions_bubble_view.h"
6 6
7 #include "base/strings/string16.h" 7 #include "base/strings/string16.h"
8 #include "chrome/browser/ui/views/website_settings/permission_selector_view.h" 8 #include "chrome/browser/ui/views/website_settings/permission_selector_view.h"
9 #include "chrome/browser/ui/views/website_settings/permission_selector_view_obse rver.h" 9 #include "chrome/browser/ui/views/website_settings/permission_selector_view_obse rver.h"
10 #include "chrome/browser/ui/website_settings/permission_bubble_request.h" 10 #include "chrome/browser/ui/website_settings/permission_bubble_request.h"
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after
126 126
127 void PermissionCombobox::PermissionChanged( 127 void PermissionCombobox::PermissionChanged(
128 const WebsiteSettingsUI::PermissionInfo& permission) { 128 const WebsiteSettingsUI::PermissionInfo& permission) {
129 SetText(model_->GetLabelAt(model_->GetIndexOfCommandId(permission.setting))); 129 SetText(model_->GetLabelAt(model_->GetIndexOfCommandId(permission.setting)));
130 SizeToPreferredSize(); 130 SizeToPreferredSize();
131 131
132 listener_->PermissionSelectionChanged( 132 listener_->PermissionSelectionChanged(
133 index_, permission.setting == CONTENT_SETTING_ALLOW); 133 index_, permission.setting == CONTENT_SETTING_ALLOW);
134 } 134 }
135 135
136 // A combobox originating on the Allow button allowing for customization
137 // of permissions.
138 class CustomizeAllowComboboxModel : public ui::ComboboxModel {
139 public:
140 enum Item {
141 INDEX_ALLOW = 0,
142 INDEX_CUSTOMIZE = 1
143 };
144
145 CustomizeAllowComboboxModel() {}
146 ~CustomizeAllowComboboxModel() override {}
147
148 int GetItemCount() const override;
149 base::string16 GetItemAt(int index) override;
150 int GetDefaultIndex() const override;
151 };
152
153 int CustomizeAllowComboboxModel::GetItemCount() const {
154 return 2;
155 }
156
157 base::string16 CustomizeAllowComboboxModel::GetItemAt(int index) {
158 if (index == INDEX_ALLOW)
159 return l10n_util::GetStringUTF16(IDS_PERMISSION_ALLOW);
160 else
161 return l10n_util::GetStringUTF16(IDS_PERMISSION_CUSTOMIZE);
162 }
163
164 int CustomizeAllowComboboxModel::GetDefaultIndex() const {
165 return INDEX_ALLOW;
166 }
167
168 /////////////////////////////////////////////////////////////////////////////// 136 ///////////////////////////////////////////////////////////////////////////////
169 // View implementation for the permissions bubble. 137 // View implementation for the permissions bubble.
170 class PermissionsBubbleDelegateView : public views::BubbleDelegateView, 138 class PermissionsBubbleDelegateView : public views::BubbleDelegateView,
171 public views::ButtonListener, 139 public views::ButtonListener,
172 public views::ComboboxListener,
173 public PermissionCombobox::Listener { 140 public PermissionCombobox::Listener {
174 public: 141 public:
175 PermissionsBubbleDelegateView( 142 PermissionsBubbleDelegateView(
176 views::View* anchor, 143 views::View* anchor,
177 PermissionBubbleViewViews* owner, 144 PermissionBubbleViewViews* owner,
178 const std::string& languages, 145 const std::string& languages,
179 const std::vector<PermissionBubbleRequest*>& requests, 146 const std::vector<PermissionBubbleRequest*>& requests,
180 const std::vector<bool>& accept_state, 147 const std::vector<bool>& accept_state);
181 bool customization_mode);
182 ~PermissionsBubbleDelegateView() override; 148 ~PermissionsBubbleDelegateView() override;
183 149
184 void Close(); 150 void Close();
185 void SizeToContents(); 151 void SizeToContents();
186 152
187 // BubbleDelegateView: 153 // BubbleDelegateView:
188 bool ShouldShowCloseButton() const override; 154 bool ShouldShowCloseButton() const override;
189 bool ShouldShowWindowTitle() const override; 155 bool ShouldShowWindowTitle() const override;
190 const gfx::FontList& GetTitleFontList() const override; 156 const gfx::FontList& GetTitleFontList() const override;
191 base::string16 GetWindowTitle() const override; 157 base::string16 GetWindowTitle() const override;
192 void OnWidgetDestroying(views::Widget* widget) override; 158 void OnWidgetDestroying(views::Widget* widget) override;
193 159
194 // ButtonListener: 160 // ButtonListener:
195 void ButtonPressed(views::Button* button, const ui::Event& event) override; 161 void ButtonPressed(views::Button* button, const ui::Event& event) override;
196 162
197 // ComboboxListener:
198 void OnPerformAction(views::Combobox* combobox) override;
199
200 // PermissionCombobox::Listener: 163 // PermissionCombobox::Listener:
201 void PermissionSelectionChanged(int index, bool allowed) override; 164 void PermissionSelectionChanged(int index, bool allowed) override;
202 165
203 private: 166 private:
204 PermissionBubbleViewViews* owner_; 167 PermissionBubbleViewViews* owner_;
205 views::Button* allow_; 168 views::Button* allow_;
206 views::Button* deny_; 169 views::Button* deny_;
207 views::Combobox* allow_combobox_; 170 views::Combobox* allow_combobox_;
208 base::string16 hostname_; 171 base::string16 hostname_;
209 scoped_ptr<PermissionMenuModel> menu_button_model_; 172 scoped_ptr<PermissionMenuModel> menu_button_model_;
210 std::vector<PermissionCombobox*> customize_comboboxes_; 173 std::vector<PermissionCombobox*> customize_comboboxes_;
211 174
212 DISALLOW_COPY_AND_ASSIGN(PermissionsBubbleDelegateView); 175 DISALLOW_COPY_AND_ASSIGN(PermissionsBubbleDelegateView);
213 }; 176 };
214 177
215 PermissionsBubbleDelegateView::PermissionsBubbleDelegateView( 178 PermissionsBubbleDelegateView::PermissionsBubbleDelegateView(
216 views::View* anchor, 179 views::View* anchor,
217 PermissionBubbleViewViews* owner, 180 PermissionBubbleViewViews* owner,
218 const std::string& languages, 181 const std::string& languages,
219 const std::vector<PermissionBubbleRequest*>& requests, 182 const std::vector<PermissionBubbleRequest*>& requests,
220 const std::vector<bool>& accept_state, 183 const std::vector<bool>& accept_state)
221 bool customization_mode)
222 : views::BubbleDelegateView(anchor, views::BubbleBorder::TOP_LEFT), 184 : views::BubbleDelegateView(anchor, views::BubbleBorder::TOP_LEFT),
223 owner_(owner), 185 owner_(owner),
224 allow_(NULL), 186 allow_(NULL),
225 deny_(NULL), 187 deny_(NULL),
226 allow_combobox_(NULL) { 188 allow_combobox_(NULL) {
227 DCHECK(!requests.empty()); 189 DCHECK(!requests.empty());
228 190
229 RemoveAllChildViews(true); 191 RemoveAllChildViews(true);
230 customize_comboboxes_.clear(); 192 customize_comboboxes_.clear();
231 set_close_on_esc(true); 193 set_close_on_esc(true);
232 set_close_on_deactivate(false); 194 set_close_on_deactivate(false);
233 195
234 SetLayoutManager(new views::BoxLayout( 196 SetLayoutManager(new views::BoxLayout(
235 views::BoxLayout::kVertical, kBubbleOuterMargin, 0, kItemMajorSpacing)); 197 views::BoxLayout::kVertical, kBubbleOuterMargin, 0, kItemMajorSpacing));
236 198
237 hostname_ = net::FormatUrl(requests[0]->GetRequestingHostname(), 199 hostname_ = net::FormatUrl(requests[0]->GetRequestingHostname(),
238 languages, 200 languages,
239 net::kFormatUrlOmitUsernamePassword | 201 net::kFormatUrlOmitUsernamePassword |
240 net::kFormatUrlOmitTrailingSlashOnBareHostname, 202 net::kFormatUrlOmitTrailingSlashOnBareHostname,
241 net::UnescapeRule::SPACES, NULL, NULL, NULL); 203 net::UnescapeRule::SPACES, NULL, NULL, NULL);
242 204
243 ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance(); 205 ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance();
244 for (size_t index = 0; index < requests.size(); index++) { 206 for (size_t index = 0; index < requests.size(); index++) {
245 DCHECK(index < accept_state.size()); 207 DCHECK(index < accept_state.size());
246 // The row is laid out containing a leading-aligned label area and a 208 // The row is laid out containing a leading-aligned label area and a
247 // trailing column which will be filled during customization with a 209 // trailing column which will be filled if there are multiple permission
248 // combobox. 210 // requests.
249 views::View* row = new views::View(); 211 views::View* row = new views::View();
250 views::GridLayout* row_layout = new views::GridLayout(row); 212 views::GridLayout* row_layout = new views::GridLayout(row);
251 row->SetLayoutManager(row_layout); 213 row->SetLayoutManager(row_layout);
252 views::ColumnSet* columns = row_layout->AddColumnSet(0); 214 views::ColumnSet* columns = row_layout->AddColumnSet(0);
253 columns->AddColumn(views::GridLayout::LEADING, views::GridLayout::FILL, 215 columns->AddColumn(views::GridLayout::LEADING, views::GridLayout::FILL,
254 0, views::GridLayout::USE_PREF, 0, 0); 216 0, views::GridLayout::USE_PREF, 0, 0);
255 columns->AddColumn(views::GridLayout::TRAILING, views::GridLayout::FILL, 217 columns->AddColumn(views::GridLayout::TRAILING, views::GridLayout::FILL,
256 100, views::GridLayout::USE_PREF, 0, 0); 218 100, views::GridLayout::USE_PREF, 0, 0);
257 row_layout->StartRow(0, 0); 219 row_layout->StartRow(0, 0);
258 220
259 views::View* label_container = new views::View(); 221 views::View* label_container = new views::View();
260 label_container->SetLayoutManager( 222 label_container->SetLayoutManager(
261 new views::BoxLayout(views::BoxLayout::kHorizontal, 223 new views::BoxLayout(views::BoxLayout::kHorizontal,
262 kPermissionIndentSpacing, 224 kPermissionIndentSpacing,
263 0, kBubbleOuterMargin)); 225 0, kBubbleOuterMargin));
264 views::ImageView* icon = new views::ImageView(); 226 views::ImageView* icon = new views::ImageView();
265 icon->SetImage(bundle.GetImageSkiaNamed(requests.at(index)->GetIconID())); 227 icon->SetImage(bundle.GetImageSkiaNamed(requests.at(index)->GetIconID()));
266 icon->SetImageSize(gfx::Size(kIconSize, kIconSize)); 228 icon->SetImageSize(gfx::Size(kIconSize, kIconSize));
267 icon->SetTooltipText(base::string16()); // Redundant with the text fragment 229 icon->SetTooltipText(base::string16()); // Redundant with the text fragment
268 label_container->AddChildView(icon); 230 label_container->AddChildView(icon);
269 views::Label* label = 231 views::Label* label =
270 new views::Label(requests.at(index)->GetMessageTextFragment()); 232 new views::Label(requests.at(index)->GetMessageTextFragment());
271 label->SetHorizontalAlignment(gfx::ALIGN_LEFT); 233 label->SetHorizontalAlignment(gfx::ALIGN_LEFT);
272 label_container->AddChildView(label); 234 label_container->AddChildView(label);
273 row_layout->AddView(label_container); 235 row_layout->AddView(label_container);
274 236
275 if (customization_mode) { 237 if (requests.size() > 1) {
276 PermissionCombobox* combobox = new PermissionCombobox( 238 PermissionCombobox* combobox = new PermissionCombobox(
277 this, 239 this,
278 index, 240 index,
279 requests[index]->GetRequestingHostname(), 241 requests[index]->GetRequestingHostname(),
280 accept_state[index] ? CONTENT_SETTING_ALLOW : CONTENT_SETTING_BLOCK); 242 accept_state[index] ? CONTENT_SETTING_ALLOW : CONTENT_SETTING_BLOCK);
281 row_layout->AddView(combobox); 243 row_layout->AddView(combobox);
282 customize_comboboxes_.push_back(combobox); 244 customize_comboboxes_.push_back(combobox);
283 } else { 245 } else {
284 row_layout->AddView(new views::View()); 246 row_layout->AddView(new views::View());
285 } 247 }
286 248
287 AddChildView(row); 249 AddChildView(row);
288 } 250 }
289 251
290 views::View* button_row = new views::View(); 252 views::View* button_row = new views::View();
291 views::GridLayout* button_layout = new views::GridLayout(button_row); 253 views::GridLayout* button_layout = new views::GridLayout(button_row);
292 views::ColumnSet* columns = button_layout->AddColumnSet(0); 254 views::ColumnSet* columns = button_layout->AddColumnSet(0);
293 button_row->SetLayoutManager(button_layout); 255 button_row->SetLayoutManager(button_layout);
294 AddChildView(button_row); 256 AddChildView(button_row);
295 257
296 // Customization case: just an "OK" button 258 // For multiple permissions: just an "OK" button.
297 if (customization_mode) { 259 if (requests.size() > 1) {
298 columns->AddColumn(views::GridLayout::TRAILING, views::GridLayout::FILL, 260 columns->AddColumn(views::GridLayout::TRAILING, views::GridLayout::FILL,
299 100, views::GridLayout::USE_PREF, 0, 0); 261 100, views::GridLayout::USE_PREF, 0, 0);
300 button_layout->StartRow(0, 0); 262 button_layout->StartRow(0, 0);
301 views::LabelButton* ok_button = 263 views::LabelButton* ok_button =
302 new views::LabelButton(this, l10n_util::GetStringUTF16(IDS_OK)); 264 new views::LabelButton(this, l10n_util::GetStringUTF16(IDS_OK));
303 ok_button->SetStyle(views::Button::STYLE_BUTTON); 265 ok_button->SetStyle(views::Button::STYLE_BUTTON);
304 button_layout->AddView(ok_button); 266 button_layout->AddView(ok_button);
305 allow_ = ok_button; 267 allow_ = ok_button;
306 268
307 button_layout->AddPaddingRow(0, kBubbleOuterMargin); 269 button_layout->AddPaddingRow(0, kBubbleOuterMargin);
308 return; 270 return;
309 } 271 }
310 272
311 // No customization: lay out the Deny/Allow buttons. 273 // For a single permission: lay out the Deny/Allow buttons.
312
313 columns->AddColumn(views::GridLayout::TRAILING, views::GridLayout::FILL, 274 columns->AddColumn(views::GridLayout::TRAILING, views::GridLayout::FILL,
314 100, views::GridLayout::USE_PREF, 0, 0); 275 100, views::GridLayout::USE_PREF, 0, 0);
315 columns->AddPaddingColumn(0, kItemMajorSpacing - (2*kButtonBorderSize)); 276 columns->AddPaddingColumn(0, kItemMajorSpacing - (2*kButtonBorderSize));
316 columns->AddColumn(views::GridLayout::TRAILING, views::GridLayout::FILL, 277 columns->AddColumn(views::GridLayout::TRAILING, views::GridLayout::FILL,
317 0, views::GridLayout::USE_PREF, 0, 0); 278 0, views::GridLayout::USE_PREF, 0, 0);
318 button_layout->StartRow(0, 0); 279 button_layout->StartRow(0, 0);
319 280
320 // Allow button is a regular button when there's only one option, and a
321 // STYLE_ACTION Combobox when there are more than one option and
322 // customization is an option.
323
324 base::string16 allow_text = l10n_util::GetStringUTF16(IDS_PERMISSION_ALLOW); 281 base::string16 allow_text = l10n_util::GetStringUTF16(IDS_PERMISSION_ALLOW);
325 if (requests.size() == 1) { 282 views::LabelButton* allow_button = new views::LabelButton(this, allow_text);
326 views::LabelButton* allow_button = new views::LabelButton(this, allow_text); 283 allow_button->SetStyle(views::Button::STYLE_BUTTON);
327 allow_button->SetStyle(views::Button::STYLE_BUTTON); 284 button_layout->AddView(allow_button);
328 button_layout->AddView(allow_button); 285 allow_ = allow_button;
329 allow_ = allow_button;
330 } else {
331 views::Combobox* allow_combobox = new views::Combobox(
332 new CustomizeAllowComboboxModel());
333 allow_combobox->set_listener(this);
334 allow_combobox->SetStyle(views::Combobox::STYLE_ACTION);
335 allow_combobox->SetAccessibleName(
336 l10n_util::GetStringUTF16(IDS_PERMISSION_ALLOW_COMBOBOX));
337 button_layout->AddView(allow_combobox);
338 allow_combobox_ = allow_combobox;
339 }
340 286
341 base::string16 deny_text = l10n_util::GetStringUTF16(IDS_PERMISSION_DENY); 287 base::string16 deny_text = l10n_util::GetStringUTF16(IDS_PERMISSION_DENY);
342 views::LabelButton* deny_button = new views::LabelButton(this, deny_text); 288 views::LabelButton* deny_button = new views::LabelButton(this, deny_text);
343 deny_button->SetStyle(views::Button::STYLE_BUTTON); 289 deny_button->SetStyle(views::Button::STYLE_BUTTON);
344 button_layout->AddView(deny_button); 290 button_layout->AddView(deny_button);
345 deny_ = deny_button; 291 deny_ = deny_button;
346 292
347 button_layout->AddPaddingRow(0, kBubbleOuterMargin); 293 button_layout->AddPaddingRow(0, kBubbleOuterMargin);
348 } 294 }
349 295
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
396 owner_->Accept(); 342 owner_->Accept();
397 else if (button == deny_) 343 else if (button == deny_)
398 owner_->Deny(); 344 owner_->Deny();
399 } 345 }
400 346
401 void PermissionsBubbleDelegateView::PermissionSelectionChanged( 347 void PermissionsBubbleDelegateView::PermissionSelectionChanged(
402 int index, bool allowed) { 348 int index, bool allowed) {
403 owner_->Toggle(index, allowed); 349 owner_->Toggle(index, allowed);
404 } 350 }
405 351
406 void PermissionsBubbleDelegateView::OnPerformAction(
407 views::Combobox* combobox) {
408 if (combobox == allow_combobox_) {
409 if (combobox->selected_index() ==
410 CustomizeAllowComboboxModel::INDEX_CUSTOMIZE)
411 owner_->SetCustomizationMode();
412 else if (combobox->selected_index() ==
413 CustomizeAllowComboboxModel::INDEX_ALLOW)
414 owner_->Accept();
415 }
416 }
417
418 ////////////////////////////////////////////////////////////////////////////// 352 //////////////////////////////////////////////////////////////////////////////
419 // PermissionBubbleViewViews 353 // PermissionBubbleViewViews
420 354
421 PermissionBubbleViewViews::PermissionBubbleViewViews( 355 PermissionBubbleViewViews::PermissionBubbleViewViews(
422 views::View* anchor_view, 356 views::View* anchor_view,
423 const std::string& languages) 357 const std::string& languages)
424 : anchor_view_(anchor_view), 358 : anchor_view_(anchor_view),
425 delegate_(NULL), 359 delegate_(NULL),
426 bubble_delegate_(NULL), 360 bubble_delegate_(NULL),
427 languages_(languages) {} 361 languages_(languages) {}
428 362
429 PermissionBubbleViewViews::~PermissionBubbleViewViews() { 363 PermissionBubbleViewViews::~PermissionBubbleViewViews() {
430 if (delegate_) 364 if (delegate_)
431 delegate_->SetView(NULL); 365 delegate_->SetView(NULL);
432 } 366 }
433 367
434 void PermissionBubbleViewViews::SetDelegate(Delegate* delegate) { 368 void PermissionBubbleViewViews::SetDelegate(Delegate* delegate) {
435 delegate_ = delegate; 369 delegate_ = delegate;
436 } 370 }
437 371
438 void PermissionBubbleViewViews::Show( 372 void PermissionBubbleViewViews::Show(
439 const std::vector<PermissionBubbleRequest*>& requests, 373 const std::vector<PermissionBubbleRequest*>& requests,
440 const std::vector<bool>& values, 374 const std::vector<bool>& values) {
441 bool customization_mode) {
442 if (bubble_delegate_ != NULL) 375 if (bubble_delegate_ != NULL)
443 bubble_delegate_->Close(); 376 bubble_delegate_->Close();
444 377
445 bubble_delegate_ = 378 bubble_delegate_ =
446 new PermissionsBubbleDelegateView(anchor_view_, this, languages_, 379 new PermissionsBubbleDelegateView(anchor_view_, this, languages_,
447 requests, values, customization_mode); 380 requests, values);
448 views::BubbleDelegateView::CreateBubble(bubble_delegate_)->Show(); 381 views::BubbleDelegateView::CreateBubble(bubble_delegate_)->Show();
449 bubble_delegate_->SizeToContents(); 382 bubble_delegate_->SizeToContents();
450 } 383 }
451 384
452 bool PermissionBubbleViewViews::CanAcceptRequestUpdate() { 385 bool PermissionBubbleViewViews::CanAcceptRequestUpdate() {
453 return !(bubble_delegate_ && bubble_delegate_->IsMouseHovered()); 386 return !(bubble_delegate_ && bubble_delegate_->IsMouseHovered());
454 } 387 }
455 388
456 void PermissionBubbleViewViews::Hide() { 389 void PermissionBubbleViewViews::Hide() {
457 if (bubble_delegate_) { 390 if (bubble_delegate_) {
(...skipping 20 matching lines...) Expand all
478 411
479 void PermissionBubbleViewViews::Accept() { 412 void PermissionBubbleViewViews::Accept() {
480 if (delegate_) 413 if (delegate_)
481 delegate_->Accept(); 414 delegate_->Accept();
482 } 415 }
483 416
484 void PermissionBubbleViewViews::Deny() { 417 void PermissionBubbleViewViews::Deny() {
485 if (delegate_) 418 if (delegate_)
486 delegate_->Deny(); 419 delegate_->Deny();
487 } 420 }
488
489 void PermissionBubbleViewViews::SetCustomizationMode() {
490 if (delegate_)
491 delegate_->SetCustomizationMode();
492 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698