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
3 // found in the LICENSE file.
5 #import "chrome/browser/ui/cocoa/passwords/manage_passwords_bubble_controller.h"
7 #include "base/mac/scoped_nsobject.h"
8 #import "chrome/browser/ui/cocoa/browser_window_controller.h"
9 #import "chrome/browser/ui/cocoa/info_bubble_view.h"
10 #import "chrome/browser/ui/cocoa/info_bubble_window.h"
11 #include "chrome/browser/ui/cocoa/location_bar/location_bar_view_mac.h"
12 #import "chrome/browser/ui/cocoa/passwords/manage_passwords_bubble_account_chooser_view_controller.h"
13 #import "chrome/browser/ui/cocoa/passwords/manage_passwords_bubble_auto_signin_view_controller.h"
14 #import "chrome/browser/ui/cocoa/passwords/manage_passwords_bubble_confirmation_view_controller.h"
15 #import "chrome/browser/ui/cocoa/passwords/manage_passwords_bubble_manage_view_controller.h"
16 #include "ui/base/cocoa/window_size_constants.h"
18 @interface ManagePasswordsBubbleController ()
19 // Updates the content view controller according to the current state.
23 @implementation ManagePasswordsBubbleController
24 - (id)initWithParentWindow:(NSWindow*)parentWindow
25 model:(ManagePasswordsBubbleModel*)model {
26 NSRect contentRect = ui::kWindowSizeDeterminedLater;
27 base::scoped_nsobject<InfoBubbleWindow> window(
28 [[InfoBubbleWindow alloc] initWithContentRect:contentRect
29 styleMask:NSBorderlessWindowMask
30 backing:NSBackingStoreBuffered
32 if ((self = [super initWithWindow:window
33 parentWindow:parentWindow
34 anchoredAt:NSZeroPoint])) {
42 [currentController_ setDelegate:nil];
46 - (void)showWindow:(id)sender {
48 [super showWindow:sender];
52 [currentController_ bubbleWillDisappear];
57 // Find the next view controller.
58 currentController_.reset();
59 if (model_->state() == password_manager::ui::PENDING_PASSWORD_STATE) {
60 currentController_.reset(
61 [[ManagePasswordsBubblePendingViewController alloc]
64 } else if (model_->state() == password_manager::ui::CONFIRMATION_STATE) {
65 currentController_.reset(
66 [[ManagePasswordsBubbleConfirmationViewController alloc]
69 } else if (model_->state() == password_manager::ui::MANAGE_STATE) {
70 currentController_.reset(
71 [[ManagePasswordsBubbleManageViewController alloc]
74 } else if (model_->state() == password_manager::ui::AUTO_SIGNIN_STATE) {
75 currentController_.reset(
76 [[ManagePasswordsBubbleAutoSigninViewController alloc]
79 } else if (model_->state() ==
80 password_manager::ui::CREDENTIAL_REQUEST_STATE) {
81 currentController_.reset(
82 [[ManagePasswordsBubbleAccountChooserViewController alloc]
91 - (void)performLayout {
92 if (!currentController_)
96 NSWindow* window = [self window];
97 [[window contentView] setSubviews:@[ [currentController_ view] ]];
98 NSButton* button = [currentController_ defaultButton];
100 [window setDefaultButtonCell:[button cell]];
102 // Update the anchor.
103 BrowserWindowController* controller = [BrowserWindowController
104 browserWindowControllerForWindow:[self parentWindow]];
105 NSPoint anchorPoint =
106 [controller locationBarBridge]->GetManagePasswordsBubblePoint();
107 anchorPoint = [[self parentWindow] convertBaseToScreen:anchorPoint];
108 [self setAnchorPoint:anchorPoint];
111 CGFloat height = NSHeight([[currentController_ view] frame]) +
112 info_bubble::kBubbleArrowHeight;
113 CGFloat width = NSWidth([[currentController_ view] frame]);
114 CGFloat x = anchorPoint.x - width +
115 info_bubble::kBubbleArrowXOffset +
116 (0.5 * info_bubble::kBubbleArrowWidth);
117 CGFloat y = anchorPoint.y - height;
118 [window setFrame:NSMakeRect(x, y, width, height)
120 animate:[window isVisible]];
123 #pragma mark ManagePasswordsBubbleContentViewDelegate
125 - (void)viewShouldDismiss {
131 @implementation ManagePasswordsBubbleController (Testing)
133 - (NSViewController*)currentController {
134 return currentController_.get();