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

Side by Side Diff: chrome/browser/ui/cocoa/content_settings/content_setting_bubble_cocoa.mm

Issue 1025503002: Use same base class for popup and plugin blocking UI (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Use actual types in for loops instead of auto Created 5 years, 9 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 (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 #import "chrome/browser/ui/cocoa/content_settings/content_setting_bubble_cocoa.h " 5 #import "chrome/browser/ui/cocoa/content_settings/content_setting_bubble_cocoa.h "
6 6
7 #include "base/command_line.h" 7 #include "base/command_line.h"
8 #include "base/logging.h" 8 #include "base/logging.h"
9 #include "base/stl_util.h" 9 #include "base/stl_util.h"
10 #include "base/strings/sys_string_conversions.h" 10 #include "base/strings/sys_string_conversions.h"
(...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after
203 webContents:(content::WebContents*)webContents 203 webContents:(content::WebContents*)webContents
204 parentWindow:(NSWindow*)parentWindow 204 parentWindow:(NSWindow*)parentWindow
205 anchoredAt:(NSPoint)anchoredAt; 205 anchoredAt:(NSPoint)anchoredAt;
206 - (NSButton*)hyperlinkButtonWithFrame:(NSRect)frame 206 - (NSButton*)hyperlinkButtonWithFrame:(NSRect)frame
207 title:(NSString*)title 207 title:(NSString*)title
208 icon:(NSImage*)icon 208 icon:(NSImage*)icon
209 referenceFrame:(NSRect)referenceFrame; 209 referenceFrame:(NSRect)referenceFrame;
210 - (void)initializeBlockedPluginsList; 210 - (void)initializeBlockedPluginsList;
211 - (void)initializeTitle; 211 - (void)initializeTitle;
212 - (void)initializeRadioGroup; 212 - (void)initializeRadioGroup;
213 - (void)initializePopupList; 213 - (void)initializeItemList;
214 - (void)initializeGeoLists; 214 - (void)initializeGeoLists;
215 - (void)initializeMediaMenus; 215 - (void)initializeMediaMenus;
216 - (void)initializeMIDISysExLists; 216 - (void)initializeMIDISysExLists;
217 - (void)sizeToFitLoadButton; 217 - (void)sizeToFitLoadButton;
218 - (void)initManageDoneButtons; 218 - (void)initManageDoneButtons;
219 - (void)removeInfoButton; 219 - (void)removeInfoButton;
220 - (void)popupLinkClicked:(id)sender; 220 - (void)popupLinkClicked:(id)sender;
221 - (void)clearGeolocationForCurrentHost:(id)sender; 221 - (void)clearGeolocationForCurrentHost:(id)sender;
222 - (void)clearMIDISysExForCurrentHost:(id)sender; 222 - (void)clearMIDISysExForCurrentHost:(id)sender;
223 @end 223 @end
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after
377 if (NSWidth(buttonFrame) > maxWidth) 377 if (NSWidth(buttonFrame) > maxWidth)
378 buttonFrame.size.width = maxWidth; 378 buttonFrame.size.width = maxWidth;
379 379
380 [button setFrame:buttonFrame]; 380 [button setFrame:buttonFrame];
381 [button setTarget:self]; 381 [button setTarget:self];
382 [button setAction:@selector(popupLinkClicked:)]; 382 [button setAction:@selector(popupLinkClicked:)];
383 return button; 383 return button;
384 } 384 }
385 385
386 - (void)initializeBlockedPluginsList { 386 - (void)initializeBlockedPluginsList {
387 NSString* label = base::SysUTF16ToNSString( 387 // Hide the empty label at the top of the dialog.
388 contentSettingBubbleModel_->bubble_content().plugin_names); 388 int delta =
389 [blockedResourcesField_ setStringValue:label]; 389 NSMinY([titleLabel_ frame]) - NSMinY([blockedResourcesField_ frame]);
390 [blockedResourcesField_ removeFromSuperview];
391 NSRect frame = [[self window] frame];
392 frame.size.height -= delta;
393 [[self window] setFrame:frame display:NO];
390 } 394 }
391 395
392 - (void)initializePopupList { 396 - (void)initializeItemList {
393 // I didn't put the buttons into a NSMatrix because then they are only one 397 // I didn't put the buttons into a NSMatrix because then they are only one
394 // entity in the key view loop. This way, one can tab through all of them. 398 // entity in the key view loop. This way, one can tab through all of them.
395 const ContentSettingBubbleModel::PopupItems& popupItems = 399 const ContentSettingBubbleModel::ListItems& listItems =
396 contentSettingBubbleModel_->bubble_content().popup_items; 400 contentSettingBubbleModel_->bubble_content().list_items;
397 401
398 // Get the pre-resize frame of the radio group. Its origin is where the 402 // Get the pre-resize frame of the radio group. Its origin is where the
399 // popup list should go. 403 // popup list should go.
400 NSRect radioFrame = [allowBlockRadioGroup_ frame]; 404 NSRect radioFrame = [allowBlockRadioGroup_ frame];
401 405
402 // Make room for the popup list. The bubble view and its subviews autosize 406 // Make room for the popup list. The bubble view and its subviews autosize
403 // themselves when the window is enlarged. 407 // themselves when the window is enlarged.
404 // Heading and radio box are already 1 * kLinkOuterPadding apart in the nib, 408 // Heading and radio box are already 1 * kLinkOuterPadding apart in the nib,
405 // so only 1 * kLinkOuterPadding more is needed. 409 // so only 1 * kLinkOuterPadding more is needed.
406 int delta = popupItems.size() * kLinkLineHeight - kLinkPadding + 410 int delta =
407 kLinkOuterPadding; 411 listItems.size() * kLinkLineHeight - kLinkPadding + kLinkOuterPadding;
408 NSSize deltaSize = NSMakeSize(0, delta); 412 NSSize deltaSize = NSMakeSize(0, delta);
409 deltaSize = [[[self window] contentView] convertSize:deltaSize toView:nil]; 413 deltaSize = [[[self window] contentView] convertSize:deltaSize toView:nil];
410 NSRect windowFrame = [[self window] frame]; 414 NSRect windowFrame = [[self window] frame];
411 windowFrame.size.height += deltaSize.height; 415 windowFrame.size.height += deltaSize.height;
412 [[self window] setFrame:windowFrame display:NO]; 416 [[self window] setFrame:windowFrame display:NO];
413 417
414 // Create popup list. 418 // Create item list.
415 int topLinkY = NSMaxY(radioFrame) + delta - kLinkHeight; 419 int topLinkY = NSMaxY(radioFrame) + delta - kLinkHeight;
416 int row = 0; 420 int row = 0;
417 for (std::vector<ContentSettingBubbleModel::PopupItem>::const_iterator 421 for (const ContentSettingBubbleModel::ListItem& listItem : listItems) {
418 it(popupItems.begin()); it != popupItems.end(); ++it, ++row) { 422 NSImage* image = listItem.image.AsNSImage();
419 NSImage* image = it->image.AsNSImage(); 423 NSRect frame = NSMakeRect(
420 424 NSMinX(radioFrame), topLinkY - kLinkLineHeight * row, 200, kLinkHeight);
421 std::string title(it->title); 425 if (listItem.has_link) {
422 // The popup may not have committed a load yet, in which case it won't 426 NSButton* button =
423 // have a URL or title. 427 [self hyperlinkButtonWithFrame:frame
424 if (title.empty()) 428 title:base::SysUTF8ToNSString(listItem.title)
425 title = l10n_util::GetStringUTF8(IDS_TAB_LOADING_TITLE); 429 icon:image
426 430 referenceFrame:radioFrame];
427 NSRect linkFrame = 431 [[self bubble] addSubview:button];
428 NSMakeRect(NSMinX(radioFrame), topLinkY - kLinkLineHeight * row, 432 popupLinks_[button] = row++;
429 200, kLinkHeight); 433 } else {
430 NSButton* button = [self 434 NSTextField* label =
431 hyperlinkButtonWithFrame:linkFrame 435 LabelWithFrame(base::SysUTF8ToNSString(listItem.title), frame);
432 title:base::SysUTF8ToNSString(title) 436 SetControlSize(label, NSSmallControlSize);
433 icon:image 437 [[self bubble] addSubview:label];
434 referenceFrame:radioFrame]; 438 row++;
435 [[self bubble] addSubview:button]; 439 }
436 popupLinks_[button] = row;
437 } 440 }
438 } 441 }
439 442
440 - (void)initializeGeoLists { 443 - (void)initializeGeoLists {
441 // Cocoa has its origin in the lower left corner. This means elements are 444 // Cocoa has its origin in the lower left corner. This means elements are
442 // added from bottom to top, which explains why loops run backwards and the 445 // added from bottom to top, which explains why loops run backwards and the
443 // order of operations is the other way than on Linux/Windows. 446 // order of operations is the other way than on Linux/Windows.
444 const ContentSettingBubbleModel::BubbleContent& content = 447 const ContentSettingBubbleModel::BubbleContent& content =
445 contentSettingBubbleModel_->bubble_content(); 448 contentSettingBubbleModel_->bubble_content();
446 NSRect containerFrame = [contentsContainer_ frame]; 449 NSRect containerFrame = [contentsContainer_ frame];
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
524 - (void)initializeMediaMenus { 527 - (void)initializeMediaMenus {
525 const ContentSettingBubbleModel::MediaMenuMap& media_menus = 528 const ContentSettingBubbleModel::MediaMenuMap& media_menus =
526 contentSettingBubbleModel_->bubble_content().media_menus; 529 contentSettingBubbleModel_->bubble_content().media_menus;
527 530
528 // Calculate the longest width of the labels and menus menus to avoid 531 // Calculate the longest width of the labels and menus menus to avoid
529 // truncation by the window's edge. 532 // truncation by the window's edge.
530 CGFloat maxLabelWidth = 0; 533 CGFloat maxLabelWidth = 0;
531 CGFloat maxMenuWidth = 0; 534 CGFloat maxMenuWidth = 0;
532 CGFloat maxMenuHeight = 0; 535 CGFloat maxMenuHeight = 0;
533 NSRect radioFrame = [allowBlockRadioGroup_ frame]; 536 NSRect radioFrame = [allowBlockRadioGroup_ frame];
534 for (ContentSettingBubbleModel::MediaMenuMap::const_iterator it( 537 for (const std::pair<content::MediaStreamType,
535 media_menus.begin()); it != media_menus.end(); ++it) { 538 ContentSettingBubbleModel::MediaMenu>& map_entry :
539 media_menus) {
536 // |labelFrame| will be resized later on in this function. 540 // |labelFrame| will be resized later on in this function.
537 NSRect labelFrame = NSMakeRect(NSMinX(radioFrame), 0, 0, 0); 541 NSRect labelFrame = NSMakeRect(NSMinX(radioFrame), 0, 0, 0);
538 NSTextField* label = 542 NSTextField* label = LabelWithFrame(
539 LabelWithFrame(base::SysUTF8ToNSString(it->second.label), labelFrame); 543 base::SysUTF8ToNSString(map_entry.second.label), labelFrame);
540 SetControlSize(label, NSSmallControlSize); 544 SetControlSize(label, NSSmallControlSize);
541 NSCell* cell = [label cell]; 545 NSCell* cell = [label cell];
542 [cell setAlignment:NSRightTextAlignment]; 546 [cell setAlignment:NSRightTextAlignment];
543 [GTMUILocalizerAndLayoutTweaker sizeToFitView:label]; 547 [GTMUILocalizerAndLayoutTweaker sizeToFitView:label];
544 maxLabelWidth = std::max(maxLabelWidth, [label frame].size.width); 548 maxLabelWidth = std::max(maxLabelWidth, [label frame].size.width);
545 [[self bubble] addSubview:label]; 549 [[self bubble] addSubview:label];
546 550
547 // |buttonFrame| will be resized and repositioned later on. 551 // |buttonFrame| will be resized and repositioned later on.
548 NSRect buttonFrame = NSMakeRect(NSMinX(radioFrame), 0, 0, 0); 552 NSRect buttonFrame = NSMakeRect(NSMinX(radioFrame), 0, 0, 0);
549 base::scoped_nsobject<NSPopUpButton> button( 553 base::scoped_nsobject<NSPopUpButton> button(
550 [[NSPopUpButton alloc] initWithFrame:buttonFrame]); 554 [[NSPopUpButton alloc] initWithFrame:buttonFrame]);
551 [button setTarget:self]; 555 [button setTarget:self];
552 556
553 // Store the |label| and |button| into MediaMenuParts struct and build 557 // Store the |label| and |button| into MediaMenuParts struct and build
554 // the popup menu from the menu model. 558 // the popup menu from the menu model.
555 content_setting_bubble::MediaMenuParts* menuParts = 559 content_setting_bubble::MediaMenuParts* menuParts =
556 new content_setting_bubble::MediaMenuParts(it->first, label); 560 new content_setting_bubble::MediaMenuParts(map_entry.first, label);
557 menuParts->model.reset(new ContentSettingMediaMenuModel( 561 menuParts->model.reset(new ContentSettingMediaMenuModel(
558 it->first, contentSettingBubbleModel_.get(), 562 map_entry.first, contentSettingBubbleModel_.get(),
559 ContentSettingMediaMenuModel::MenuLabelChangedCallback())); 563 ContentSettingMediaMenuModel::MenuLabelChangedCallback()));
560 mediaMenus_[button] = menuParts; 564 mediaMenus_[button] = menuParts;
561 CGFloat width = BuildPopUpMenuFromModel(button, 565 CGFloat width = BuildPopUpMenuFromModel(
562 menuParts->model.get(), 566 button, menuParts->model.get(), map_entry.second.selected_device.name,
563 it->second.selected_device.name, 567 map_entry.second.disabled);
564 it->second.disabled);
565 maxMenuWidth = std::max(maxMenuWidth, width); 568 maxMenuWidth = std::max(maxMenuWidth, width);
566 569
567 [[self bubble] addSubview:button 570 [[self bubble] addSubview:button
568 positioned:NSWindowBelow 571 positioned:NSWindowBelow
569 relativeTo:nil]; 572 relativeTo:nil];
570 573
571 maxMenuHeight = std::max(maxMenuHeight, [button frame].size.height); 574 maxMenuHeight = std::max(maxMenuHeight, [button frame].size.height);
572 } 575 }
573 576
574 // Make room for the media menu(s) and enlarge the windows to fit the views. 577 // Make room for the media menu(s) and enlarge the windows to fit the views.
(...skipping 11 matching lines...) Expand all
586 windowFrame.size.width = widthNeeded; 589 windowFrame.size.width = widthNeeded;
587 [[self window] setFrame:windowFrame display:NO]; 590 [[self window] setFrame:windowFrame display:NO];
588 591
589 // The radio group lies above the media menus, move the radio group up. 592 // The radio group lies above the media menus, move the radio group up.
590 radioFrame.origin.y += delta; 593 radioFrame.origin.y += delta;
591 [allowBlockRadioGroup_ setFrame:radioFrame]; 594 [allowBlockRadioGroup_ setFrame:radioFrame];
592 595
593 // Resize and reposition the media menus layout. 596 // Resize and reposition the media menus layout.
594 CGFloat topMenuY = NSMinY(radioFrame) - kMediaMenuVerticalPadding; 597 CGFloat topMenuY = NSMinY(radioFrame) - kMediaMenuVerticalPadding;
595 maxMenuWidth = std::max(maxMenuWidth, kMinMediaMenuButtonWidth); 598 maxMenuWidth = std::max(maxMenuWidth, kMinMediaMenuButtonWidth);
596 for (content_setting_bubble::MediaMenuPartsMap::const_iterator i = 599 for (const std::pair<NSPopUpButton*, content_setting_bubble::MediaMenuParts*>&
597 mediaMenus_.begin(); i != mediaMenus_.end(); ++i) { 600 map_entry : mediaMenus_) {
598 NSRect labelFrame = [i->second->label frame]; 601 NSRect labelFrame = [map_entry.second->label frame];
599 // Align the label text with the button text. 602 // Align the label text with the button text.
600 labelFrame.origin.y = 603 labelFrame.origin.y =
601 topMenuY + (maxMenuHeight - labelFrame.size.height) / 2 + 1; 604 topMenuY + (maxMenuHeight - labelFrame.size.height) / 2 + 1;
602 labelFrame.size.width = maxLabelWidth; 605 labelFrame.size.width = maxLabelWidth;
603 [i->second->label setFrame:labelFrame]; 606 [map_entry.second->label setFrame:labelFrame];
604 NSRect menuFrame = [i->first frame]; 607 NSRect menuFrame = [map_entry.first frame];
605 menuFrame.origin.y = topMenuY; 608 menuFrame.origin.y = topMenuY;
606 menuFrame.origin.x = NSMinX(radioFrame) + maxLabelWidth; 609 menuFrame.origin.x = NSMinX(radioFrame) + maxLabelWidth;
607 menuFrame.size.width = maxMenuWidth; 610 menuFrame.size.width = maxMenuWidth;
608 menuFrame.size.height = maxMenuHeight; 611 menuFrame.size.height = maxMenuHeight;
609 [i->first setFrame:menuFrame]; 612 [map_entry.first setFrame:menuFrame];
610 topMenuY -= (maxMenuHeight + kMediaMenuElementVerticalPadding); 613 topMenuY -= (maxMenuHeight + kMediaMenuElementVerticalPadding);
611 } 614 }
612 } 615 }
613 616
614 - (void)initializeMIDISysExLists { 617 - (void)initializeMIDISysExLists {
615 const ContentSettingBubbleModel::BubbleContent& content = 618 const ContentSettingBubbleModel::BubbleContent& content =
616 contentSettingBubbleModel_->bubble_content(); 619 contentSettingBubbleModel_->bubble_content();
617 NSRect containerFrame = [contentsContainer_ frame]; 620 NSRect containerFrame = [contentsContainer_ frame];
618 NSRect frame = 621 NSRect frame =
619 NSMakeRect(0, 0, NSWidth(containerFrame), kMIDISysExLabelHeight); 622 NSMakeRect(0, 0, NSWidth(containerFrame), kMIDISysExLabelHeight);
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after
741 744
742 ContentSettingsType type = contentSettingBubbleModel_->content_type(); 745 ContentSettingsType type = contentSettingBubbleModel_->content_type();
743 if (type == CONTENT_SETTINGS_TYPE_PLUGINS) { 746 if (type == CONTENT_SETTINGS_TYPE_PLUGINS) {
744 [self sizeToFitLoadButton]; 747 [self sizeToFitLoadButton];
745 [self initializeBlockedPluginsList]; 748 [self initializeBlockedPluginsList];
746 } 749 }
747 750
748 if (allowBlockRadioGroup_) // not bound in cookie bubble xib 751 if (allowBlockRadioGroup_) // not bound in cookie bubble xib
749 [self initializeRadioGroup]; 752 [self initializeRadioGroup];
750 753
751 if (type == CONTENT_SETTINGS_TYPE_POPUPS) 754 if (type == CONTENT_SETTINGS_TYPE_POPUPS ||
752 [self initializePopupList]; 755 type == CONTENT_SETTINGS_TYPE_PLUGINS)
756 [self initializeItemList];
753 if (type == CONTENT_SETTINGS_TYPE_GEOLOCATION) 757 if (type == CONTENT_SETTINGS_TYPE_GEOLOCATION)
754 [self initializeGeoLists]; 758 [self initializeGeoLists];
755 if (type == CONTENT_SETTINGS_TYPE_MEDIASTREAM) 759 if (type == CONTENT_SETTINGS_TYPE_MEDIASTREAM)
756 [self initializeMediaMenus]; 760 [self initializeMediaMenus];
757 if (type == CONTENT_SETTINGS_TYPE_MIDI_SYSEX) 761 if (type == CONTENT_SETTINGS_TYPE_MIDI_SYSEX)
758 [self initializeMIDISysExLists]; 762 [self initializeMIDISysExLists];
759 } 763 }
760 764
761 /////////////////////////////////////////////////////////////////////////////// 765 ///////////////////////////////////////////////////////////////////////////////
762 // Actual application logic 766 // Actual application logic
763 767
764 - (IBAction)allowBlockToggled:(id)sender { 768 - (IBAction)allowBlockToggled:(id)sender {
765 NSButtonCell *selectedCell = [sender selectedCell]; 769 NSButtonCell *selectedCell = [sender selectedCell];
766 contentSettingBubbleModel_->OnRadioClicked([selectedCell tag] - 1); 770 contentSettingBubbleModel_->OnRadioClicked([selectedCell tag] - 1);
767 } 771 }
768 772
769 - (void)popupLinkClicked:(id)sender { 773 - (void)popupLinkClicked:(id)sender {
770 content_setting_bubble::PopupLinks::iterator i(popupLinks_.find(sender)); 774 content_setting_bubble::PopupLinks::iterator i(popupLinks_.find(sender));
771 DCHECK(i != popupLinks_.end()); 775 DCHECK(i != popupLinks_.end());
772 contentSettingBubbleModel_->OnPopupClicked(i->second); 776 contentSettingBubbleModel_->OnListItemClicked(i->second);
773 } 777 }
774 778
775 - (void)clearGeolocationForCurrentHost:(id)sender { 779 - (void)clearGeolocationForCurrentHost:(id)sender {
776 contentSettingBubbleModel_->OnCustomLinkClicked(); 780 contentSettingBubbleModel_->OnCustomLinkClicked();
777 [self close]; 781 [self close];
778 } 782 }
779 783
780 - (void)clearMIDISysExForCurrentHost:(id)sender { 784 - (void)clearMIDISysExForCurrentHost:(id)sender {
781 contentSettingBubbleModel_->OnCustomLinkClicked(); 785 contentSettingBubbleModel_->OnCustomLinkClicked();
782 [self close]; 786 [self close];
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
816 button, base::SysUTF16ToNSString(it->second->model->GetLabelAt(index))); 820 button, base::SysUTF16ToNSString(it->second->model->GetLabelAt(index)));
817 821
818 it->second->model->ExecuteCommand(index, 0); 822 it->second->model->ExecuteCommand(index, 0);
819 } 823 }
820 824
821 - (content_setting_bubble::MediaMenuPartsMap*)mediaMenus { 825 - (content_setting_bubble::MediaMenuPartsMap*)mediaMenus {
822 return &mediaMenus_; 826 return &mediaMenus_;
823 } 827 }
824 828
825 @end // ContentSettingBubbleController 829 @end // ContentSettingBubbleController
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698