Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / ui / cocoa / passwords / manage_passwords_bubble_controller.mm
blobf9bbc55dd8d4f276a084d9b63d92bb4d485118e9
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.
20 - (void)updateState;
21 @end
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
31                                               defer:NO]);
32   if ((self = [super initWithWindow:window
33                        parentWindow:parentWindow
34                          anchoredAt:NSZeroPoint])) {
35     model_ = model;
36     [self updateState];
37   }
38   return self;
41 - (void)dealloc {
42   [currentController_ setDelegate:nil];
43   [super dealloc];
46 - (void)showWindow:(id)sender {
47   [self performLayout];
48   [super showWindow:sender];
51 - (void)close {
52   [currentController_ bubbleWillDisappear];
53   [super close];
56 - (void)updateState {
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]
62             initWithModel:model_
63                  delegate:self]);
64   } else if (model_->state() == password_manager::ui::CONFIRMATION_STATE) {
65     currentController_.reset(
66         [[ManagePasswordsBubbleConfirmationViewController alloc]
67             initWithModel:model_
68                  delegate:self]);
69   } else if (model_->state() == password_manager::ui::MANAGE_STATE) {
70     currentController_.reset(
71         [[ManagePasswordsBubbleManageViewController alloc]
72             initWithModel:model_
73                  delegate:self]);
74   } else if (model_->state() == password_manager::ui::AUTO_SIGNIN_STATE) {
75     currentController_.reset(
76         [[ManagePasswordsBubbleAutoSigninViewController alloc]
77             initWithModel:model_
78                  delegate:self]);
79   } else if (model_->state() ==
80              password_manager::ui::CREDENTIAL_REQUEST_STATE) {
81     currentController_.reset(
82         [[ManagePasswordsBubbleAccountChooserViewController alloc]
83             initWithModel:model_
84                  delegate:self]);
85   } else {
86     NOTREACHED();
87   }
88   [self performLayout];
91 - (void)performLayout {
92   if (!currentController_)
93     return;
95   // Update the window.
96   NSWindow* window = [self window];
97   [[window contentView] setSubviews:@[ [currentController_ view] ]];
98   NSButton* button = [currentController_ defaultButton];
99   if (button)
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];
110   // Update the frame.
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)
119            display:YES
120            animate:[window isVisible]];
123 #pragma mark ManagePasswordsBubbleContentViewDelegate
125 - (void)viewShouldDismiss {
126   [self close];
129 @end
131 @implementation ManagePasswordsBubbleController (Testing)
133 - (NSViewController*)currentController {
134   return currentController_.get();
137 @end