| OLD | NEW |
| 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 #import "chrome/browser/ui/cocoa/website_settings/permission_bubble_controller.h
" | 5 #import "chrome/browser/ui/cocoa/website_settings/permission_bubble_controller.h
" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/mac/bind_objc_block.h" | 9 #include "base/mac/bind_objc_block.h" |
| 10 #include "base/strings/sys_string_conversions.h" | 10 #include "base/strings/sys_string_conversions.h" |
| 11 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
| 12 #include "chrome/browser/ui/browser.h" | 12 #include "chrome/browser/ui/browser.h" |
| 13 #include "chrome/browser/ui/browser_finder.h" | 13 #include "chrome/browser/ui/browser_finder.h" |
| 14 #include "chrome/browser/ui/browser_window.h" | 14 #include "chrome/browser/ui/browser_window.h" |
| 15 #import "chrome/browser/ui/chrome_style.h" | 15 #import "chrome/browser/ui/chrome_style.h" |
| 16 #import "chrome/browser/ui/cocoa/browser_window_controller.h" | 16 #import "chrome/browser/ui/cocoa/browser_window_controller.h" |
| 17 #import "chrome/browser/ui/cocoa/browser_window_utils.h" | 17 #import "chrome/browser/ui/cocoa/browser_window_utils.h" |
| 18 #import "chrome/browser/ui/cocoa/constrained_window/constrained_window_button.h" | 18 #import "chrome/browser/ui/cocoa/constrained_window/constrained_window_button.h" |
| 19 #import "chrome/browser/ui/cocoa/hover_close_button.h" | 19 #import "chrome/browser/ui/cocoa/hover_close_button.h" |
| 20 #import "chrome/browser/ui/cocoa/info_bubble_view.h" | 20 #import "chrome/browser/ui/cocoa/info_bubble_view.h" |
| 21 #import "chrome/browser/ui/cocoa/info_bubble_window.h" | 21 #import "chrome/browser/ui/cocoa/info_bubble_window.h" |
| 22 #import "chrome/browser/ui/cocoa/location_bar/location_bar_view_mac.h" | 22 #import "chrome/browser/ui/cocoa/location_bar/location_bar_view_mac.h" |
| 23 #include "chrome/browser/ui/cocoa/website_settings/permission_bubble_cocoa.h" | 23 #include "chrome/browser/ui/cocoa/website_settings/permission_bubble_cocoa.h" |
| 24 #include "chrome/browser/ui/cocoa/website_settings/permission_selector_button.h" | 24 #include "chrome/browser/ui/cocoa/website_settings/permission_selector_button.h" |
| 25 #include "chrome/browser/ui/cocoa/website_settings/split_block_button.h" | 25 #include "chrome/browser/ui/cocoa/website_settings/split_block_button.h" |
| 26 #include "chrome/browser/ui/cocoa/website_settings/website_settings_utils_cocoa.
h" | 26 #include "chrome/browser/ui/cocoa/website_settings/website_settings_utils_cocoa.
h" |
| 27 #include "chrome/browser/ui/website_settings/permission_bubble_manager.h" |
| 27 #include "chrome/browser/ui/website_settings/permission_bubble_request.h" | 28 #include "chrome/browser/ui/website_settings/permission_bubble_request.h" |
| 28 #include "chrome/browser/ui/website_settings/permission_bubble_view.h" | |
| 29 #include "chrome/browser/ui/website_settings/permission_menu_model.h" | 29 #include "chrome/browser/ui/website_settings/permission_menu_model.h" |
| 30 #include "chrome/grit/generated_resources.h" | 30 #include "chrome/grit/generated_resources.h" |
| 31 #include "content/public/browser/native_web_keyboard_event.h" | 31 #include "content/public/browser/native_web_keyboard_event.h" |
| 32 #include "content/public/browser/user_metrics.h" | 32 #include "content/public/browser/user_metrics.h" |
| 33 #include "skia/ext/skia_utils_mac.h" | 33 #include "skia/ext/skia_utils_mac.h" |
| 34 #import "ui/base/cocoa/controls/hyperlink_text_view.h" | 34 #import "ui/base/cocoa/controls/hyperlink_text_view.h" |
| 35 #import "ui/base/cocoa/menu_controller.h" | 35 #import "ui/base/cocoa/menu_controller.h" |
| 36 #include "ui/base/cocoa/window_size_constants.h" | 36 #include "ui/base/cocoa/window_size_constants.h" |
| 37 #include "ui/base/l10n/l10n_util_mac.h" | 37 #include "ui/base/l10n/l10n_util_mac.h" |
| 38 #include "ui/base/models/simple_menu_model.h" | 38 #include "ui/base/models/simple_menu_model.h" |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 77 // multiple permissions in the bubble. | 77 // multiple permissions in the bubble. |
| 78 @interface AllowBlockMenuButton : NSPopUpButton { | 78 @interface AllowBlockMenuButton : NSPopUpButton { |
| 79 @private | 79 @private |
| 80 scoped_ptr<PermissionMenuModel> menuModel_; | 80 scoped_ptr<PermissionMenuModel> menuModel_; |
| 81 base::scoped_nsobject<MenuController> menuController_; | 81 base::scoped_nsobject<MenuController> menuController_; |
| 82 } | 82 } |
| 83 | 83 |
| 84 - (id)initForURL:(const GURL&)url | 84 - (id)initForURL:(const GURL&)url |
| 85 allowed:(BOOL)allow | 85 allowed:(BOOL)allow |
| 86 index:(int)index | 86 index:(int)index |
| 87 delegate:(PermissionBubbleView::Delegate*)delegate; | 87 manager:(PermissionBubbleManager*)manager; |
| 88 | 88 |
| 89 // Returns the maximum width of its possible titles. | 89 // Returns the maximum width of its possible titles. |
| 90 - (CGFloat)maximumTitleWidth; | 90 - (CGFloat)maximumTitleWidth; |
| 91 @end | 91 @end |
| 92 | 92 |
| 93 @implementation AllowBlockMenuButton | 93 @implementation AllowBlockMenuButton |
| 94 | 94 |
| 95 - (id)initForURL:(const GURL&)url | 95 - (id)initForURL:(const GURL&)url |
| 96 allowed:(BOOL)allow | 96 allowed:(BOOL)allow |
| 97 index:(int)index | 97 index:(int)index |
| 98 delegate:(PermissionBubbleView::Delegate*)delegate { | 98 manager:(PermissionBubbleManager*)manager { |
| 99 if (self = [super initWithFrame:NSZeroRect pullsDown:NO]) { | 99 if (self = [super initWithFrame:NSZeroRect pullsDown:NO]) { |
| 100 ContentSetting setting = | 100 ContentSetting setting = |
| 101 allow ? CONTENT_SETTING_ALLOW : CONTENT_SETTING_BLOCK; | 101 allow ? CONTENT_SETTING_ALLOW : CONTENT_SETTING_BLOCK; |
| 102 [self setFont:[NSFont systemFontOfSize:[NSFont smallSystemFontSize]]]; | 102 [self setFont:[NSFont systemFontOfSize:[NSFont smallSystemFontSize]]]; |
| 103 [self setBordered:NO]; | 103 [self setBordered:NO]; |
| 104 | 104 |
| 105 __block PermissionBubbleView::Delegate* blockDelegate = delegate; | 105 __block PermissionBubbleManager* blockManager = manager; |
| 106 __block AllowBlockMenuButton* blockSelf = self; | 106 __block AllowBlockMenuButton* blockSelf = self; |
| 107 PermissionMenuModel::ChangeCallback changeCallback = | 107 PermissionMenuModel::ChangeCallback changeCallback = |
| 108 base::BindBlock(^(const WebsiteSettingsUI::PermissionInfo& permission) { | 108 base::BindBlock(^(const WebsiteSettingsUI::PermissionInfo& permission) { |
| 109 blockDelegate->ToggleAccept( | 109 blockManager->ToggleAccept( |
| 110 index, permission.setting == CONTENT_SETTING_ALLOW); | 110 index, permission.setting == CONTENT_SETTING_ALLOW); |
| 111 [blockSelf setFrameSize: | 111 [blockSelf setFrameSize:SizeForWebsiteSettingsButtonTitle( |
| 112 SizeForWebsiteSettingsButtonTitle(blockSelf, | 112 blockSelf, [blockSelf title])]; |
| 113 [blockSelf title])]; | |
| 114 }); | 113 }); |
| 115 | 114 |
| 116 menuModel_.reset(new PermissionMenuModel(url, setting, changeCallback)); | 115 menuModel_.reset(new PermissionMenuModel(url, setting, changeCallback)); |
| 117 menuController_.reset([[MenuController alloc] initWithModel:menuModel_.get() | 116 menuController_.reset([[MenuController alloc] initWithModel:menuModel_.get() |
| 118 useWithPopUpButtonCell:NO]); | 117 useWithPopUpButtonCell:NO]); |
| 119 [self setMenu:[menuController_ menu]]; | 118 [self setMenu:[menuController_ menu]]; |
| 120 [self selectItemAtIndex:menuModel_->GetIndexOfCommandId(setting)]; | 119 [self selectItemAtIndex:menuModel_->GetIndexOfCommandId(setting)]; |
| 121 // Although the frame is reset, below, this sizes the cell properly. | 120 // Although the frame is reset, below, this sizes the cell properly. |
| 122 [self sizeToFit]; | 121 [self sizeToFit]; |
| 123 // Adjust the size to fit the current title. Using only -sizeToFit leaves | 122 // Adjust the size to fit the current title. Using only -sizeToFit leaves |
| (...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 265 - (void)parentWindowDidResize:(NSNotification*)notification { | 264 - (void)parentWindowDidResize:(NSNotification*)notification { |
| 266 DCHECK(bridge_); | 265 DCHECK(bridge_); |
| 267 [self setAnchorPoint:[self getExpectedAnchorPoint]]; | 266 [self setAnchorPoint:[self getExpectedAnchorPoint]]; |
| 268 } | 267 } |
| 269 | 268 |
| 270 - (void)parentWindowDidMove:(NSNotification*)notification { | 269 - (void)parentWindowDidMove:(NSNotification*)notification { |
| 271 DCHECK(bridge_); | 270 DCHECK(bridge_); |
| 272 [self setAnchorPoint:[self getExpectedAnchorPoint]]; | 271 [self setAnchorPoint:[self getExpectedAnchorPoint]]; |
| 273 } | 272 } |
| 274 | 273 |
| 275 - (void)showWithDelegate:(PermissionBubbleView::Delegate*)delegate | 274 - (void)showWithManager:(PermissionBubbleManager*)manager |
| 276 forRequests:(const std::vector<PermissionBubbleRequest*>&)requests | 275 forRequests:(const std::vector<PermissionBubbleRequest*>&)requests |
| 277 acceptStates:(const std::vector<bool>&)acceptStates { | 276 acceptStates:(const std::vector<bool>&)acceptStates { |
| 278 DCHECK(!requests.empty()); | 277 DCHECK(!requests.empty()); |
| 279 DCHECK(delegate); | 278 DCHECK(manager); |
| 280 delegate_ = delegate; | 279 manager_ = manager; |
| 281 | 280 |
| 282 NSView* contentView = [[self window] contentView]; | 281 NSView* contentView = [[self window] contentView]; |
| 283 [contentView setSubviews:@[]]; | 282 [contentView setSubviews:@[]]; |
| 284 | 283 |
| 285 BOOL singlePermission = requests.size() == 1; | 284 BOOL singlePermission = requests.size() == 1; |
| 286 | 285 |
| 287 // Create one button to use as a guide for the permissions' y-offsets. | 286 // Create one button to use as a guide for the permissions' y-offsets. |
| 288 base::scoped_nsobject<NSView> allowOrOkButton; | 287 base::scoped_nsobject<NSView> allowOrOkButton; |
| 289 if (singlePermission) { | 288 if (singlePermission) { |
| 290 NSString* allowTitle = l10n_util::GetNSString(IDS_PERMISSION_ALLOW); | 289 NSString* allowTitle = l10n_util::GetNSString(IDS_PERMISSION_ALLOW); |
| (...skipping 235 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 526 NSRect titleFrame = [titleView frame]; | 525 NSRect titleFrame = [titleView frame]; |
| 527 [titleView setFrameSize:NSMakeSize(NSWidth(titleFrame) + kTitlePaddingX, | 526 [titleView setFrameSize:NSMakeSize(NSWidth(titleFrame) + kTitlePaddingX, |
| 528 NSHeight(titleFrame))]; | 527 NSHeight(titleFrame))]; |
| 529 return titleView.autorelease(); | 528 return titleView.autorelease(); |
| 530 } | 529 } |
| 531 | 530 |
| 532 - (NSView*)menuForRequest:(PermissionBubbleRequest*)request | 531 - (NSView*)menuForRequest:(PermissionBubbleRequest*)request |
| 533 atIndex:(int)index | 532 atIndex:(int)index |
| 534 allow:(BOOL)allow { | 533 allow:(BOOL)allow { |
| 535 DCHECK(request); | 534 DCHECK(request); |
| 536 DCHECK(delegate_); | 535 DCHECK(manager_); |
| 537 base::scoped_nsobject<AllowBlockMenuButton> button( | 536 base::scoped_nsobject<AllowBlockMenuButton> button( |
| 538 [[AllowBlockMenuButton alloc] initForURL:request->GetRequestingHostname() | 537 [[AllowBlockMenuButton alloc] initForURL:request->GetRequestingHostname() |
| 539 allowed:allow | 538 allowed:allow |
| 540 index:index | 539 index:index |
| 541 delegate:delegate_]); | 540 manager:manager_]); |
| 542 return button.autorelease(); | 541 return button.autorelease(); |
| 543 } | 542 } |
| 544 | 543 |
| 545 - (NSView*)buttonWithTitle:(NSString*)title | 544 - (NSView*)buttonWithTitle:(NSString*)title |
| 546 action:(SEL)action { | 545 action:(SEL)action { |
| 547 base::scoped_nsobject<NSButton> button( | 546 base::scoped_nsobject<NSButton> button( |
| 548 [[ConstrainedWindowButton alloc] initWithFrame:NSZeroRect]); | 547 [[ConstrainedWindowButton alloc] initWithFrame:NSZeroRect]); |
| 549 [button setButtonType:NSMomentaryPushInButton]; | 548 [button setButtonType:NSMomentaryPushInButton]; |
| 550 [button setTitle:title]; | 549 [button setTitle:title]; |
| 551 [button setTarget:self]; | 550 [button setTarget:self]; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 564 int dimension = chrome_style::GetCloseButtonSize(); | 563 int dimension = chrome_style::GetCloseButtonSize(); |
| 565 NSRect frame = NSMakeRect(0, 0, dimension, dimension); | 564 NSRect frame = NSMakeRect(0, 0, dimension, dimension); |
| 566 base::scoped_nsobject<NSButton> button( | 565 base::scoped_nsobject<NSButton> button( |
| 567 [[WebUIHoverCloseButton alloc] initWithFrame:frame]); | 566 [[WebUIHoverCloseButton alloc] initWithFrame:frame]); |
| 568 [button setAction:@selector(onClose:)]; | 567 [button setAction:@selector(onClose:)]; |
| 569 [button setTarget:self]; | 568 [button setTarget:self]; |
| 570 return button.autorelease(); | 569 return button.autorelease(); |
| 571 } | 570 } |
| 572 | 571 |
| 573 - (void)ok:(id)sender { | 572 - (void)ok:(id)sender { |
| 574 DCHECK(delegate_); | 573 DCHECK(manager_); |
| 575 delegate_->Accept(); | 574 manager_->Accept(); |
| 576 } | 575 } |
| 577 | 576 |
| 578 - (void)onAllow:(id)sender { | 577 - (void)onAllow:(id)sender { |
| 579 DCHECK(delegate_); | 578 DCHECK(manager_); |
| 580 delegate_->Accept(); | 579 manager_->Accept(); |
| 581 } | 580 } |
| 582 | 581 |
| 583 - (void)onBlock:(id)sender { | 582 - (void)onBlock:(id)sender { |
| 584 DCHECK(delegate_); | 583 DCHECK(manager_); |
| 585 delegate_->Deny(); | 584 manager_->Deny(); |
| 586 } | 585 } |
| 587 | 586 |
| 588 - (void)onClose:(id)sender { | 587 - (void)onClose:(id)sender { |
| 589 DCHECK(delegate_); | 588 DCHECK(manager_); |
| 590 delegate_->Closing(); | 589 manager_->Closing(); |
| 591 } | 590 } |
| 592 | 591 |
| 593 - (void)activateTabWithContents:(content::WebContents*)newContents | 592 - (void)activateTabWithContents:(content::WebContents*)newContents |
| 594 previousContents:(content::WebContents*)oldContents | 593 previousContents:(content::WebContents*)oldContents |
| 595 atIndex:(NSInteger)index | 594 atIndex:(NSInteger)index |
| 596 reason:(int)reason { | 595 reason:(int)reason { |
| 597 // The show/hide of this bubble is handled by the PermissionBubbleManager. | 596 // The show/hide of this bubble is handled by the PermissionBubbleManager. |
| 598 // So bypass the base class, which would close the bubble here. | 597 // So bypass the base class, which would close the bubble here. |
| 599 } | 598 } |
| 600 | 599 |
| 601 + (CGFloat)matchWidthsOf:(NSView*)viewA andOf:(NSView*)viewB { | 600 + (CGFloat)matchWidthsOf:(NSView*)viewA andOf:(NSView*)viewB { |
| 602 NSRect frameA = [viewA frame]; | 601 NSRect frameA = [viewA frame]; |
| 603 NSRect frameB = [viewB frame]; | 602 NSRect frameB = [viewB frame]; |
| 604 CGFloat width = std::max(NSWidth(frameA), NSWidth(frameB)); | 603 CGFloat width = std::max(NSWidth(frameA), NSWidth(frameB)); |
| 605 [viewA setFrameSize:NSMakeSize(width, NSHeight(frameA))]; | 604 [viewA setFrameSize:NSMakeSize(width, NSHeight(frameA))]; |
| 606 [viewB setFrameSize:NSMakeSize(width, NSHeight(frameB))]; | 605 [viewB setFrameSize:NSMakeSize(width, NSHeight(frameB))]; |
| 607 return width; | 606 return width; |
| 608 } | 607 } |
| 609 | 608 |
| 610 + (void)alignCenterOf:(NSView*)viewA verticallyToCenterOf:(NSView*)viewB { | 609 + (void)alignCenterOf:(NSView*)viewA verticallyToCenterOf:(NSView*)viewB { |
| 611 NSRect frameA = [viewA frame]; | 610 NSRect frameA = [viewA frame]; |
| 612 NSRect frameB = [viewB frame]; | 611 NSRect frameB = [viewB frame]; |
| 613 frameA.origin.y = | 612 frameA.origin.y = |
| 614 NSMinY(frameB) + std::floor((NSHeight(frameB) - NSHeight(frameA)) / 2); | 613 NSMinY(frameB) + std::floor((NSHeight(frameB) - NSHeight(frameA)) / 2); |
| 615 [viewA setFrameOrigin:frameA.origin]; | 614 [viewA setFrameOrigin:frameA.origin]; |
| 616 } | 615 } |
| 617 | 616 |
| 618 @end // implementation PermissionBubbleController | 617 @end // implementation PermissionBubbleController |
| OLD | NEW |