Check USB device path access when prompting users to select a device.
[chromium-blink-merge.git] / chrome / browser / ui / cocoa / passwords / manage_passwords_bubble_controller.mm
blob6ac063aa0d3785e52e52cec1460ce6c18e7c851f
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 #import "chrome/browser/ui/cocoa/passwords/manage_passwords_bubble_manage_view_controller.h"
12 #include "chrome/browser/ui/cocoa/location_bar/location_bar_view_mac.h"
13 #import "chrome/browser/ui/cocoa/passwords/manage_passwords_bubble_account_chooser_view_controller.h"
14 #import "chrome/browser/ui/cocoa/passwords/manage_passwords_bubble_blacklist_view_controller.h"
15 #import "chrome/browser/ui/cocoa/passwords/manage_passwords_bubble_confirmation_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)showWindow:(id)sender {
42   [self performLayout];
43   [super showWindow:sender];
46 - (void)close {
47   [currentController_ bubbleWillDisappear];
48   [super close];
51 - (void)updateState {
52   // Find the next view controller.
53   currentController_.reset();
54   if (model_->state() == password_manager::ui::PENDING_PASSWORD_STATE) {
55     currentController_.reset(
56         [[ManagePasswordsBubblePendingViewController alloc]
57             initWithModel:model_
58                  delegate:self]);
59   } else if (model_->state() == password_manager::ui::CONFIRMATION_STATE) {
60     currentController_.reset(
61         [[ManagePasswordsBubbleConfirmationViewController alloc]
62             initWithModel:model_
63                  delegate:self]);
64   } else if (model_->state() == password_manager::ui::MANAGE_STATE) {
65     currentController_.reset([[ManagePasswordsBubbleManageViewController alloc]
66         initWithModel:model_
67              delegate:self]);
68   } else if (model_->state() == password_manager::ui::BLACKLIST_STATE) {
69     currentController_.reset(
70         [[ManagePasswordsBubbleBlacklistViewController alloc]
71             initWithModel:model_
72                  delegate:self]);
73   } else if (model_->state() ==
74              password_manager::ui::CREDENTIAL_REQUEST_STATE) {
75     currentController_.reset(
76         [[ManagePasswordsBubbleAccountChooserViewController alloc]
77             initWithModel:model_
78                  delegate:self]);
79   } else {
80     NOTREACHED();
81   }
82   [self performLayout];
85 - (void)performLayout {
86   if (!currentController_)
87     return;
89   // Update the window.
90   NSWindow* window = [self window];
91   [[window contentView] setSubviews:@[ [currentController_ view] ]];
93   // Update the anchor.
94   BrowserWindowController* controller = [BrowserWindowController
95       browserWindowControllerForWindow:[self parentWindow]];
96   NSPoint anchorPoint =
97       [controller locationBarBridge]->GetManagePasswordsBubblePoint();
98   anchorPoint = [[self parentWindow] convertBaseToScreen:anchorPoint];
99   [self setAnchorPoint:anchorPoint];
101   // Update the frame.
102   CGFloat height = NSHeight([[currentController_ view] frame]) +
103                    info_bubble::kBubbleArrowHeight;
104   CGFloat width = NSWidth([[currentController_ view] frame]);
105   CGFloat x = anchorPoint.x - width +
106               info_bubble::kBubbleArrowXOffset +
107               (0.5 * info_bubble::kBubbleArrowWidth);
108   CGFloat y = anchorPoint.y - height;
109   [window setFrame:NSMakeRect(x, y, width, height)
110            display:YES
111            animate:[window isVisible]];
114 #pragma mark ManagePasswordsBubbleContentViewDelegate
116 - (void)viewShouldDismiss {
117   [self close];
120 #pragma mark ManagePasswordsBubblePendingViewDelegate
122 - (void)passwordShouldNeverBeSavedOnSiteWithExistingPasswords {
123   currentController_.reset([[ManagePasswordsBubbleNeverSaveViewController alloc]
124       initWithModel:model_
125            delegate:self]);
126   [self performLayout];
129 #pragma mark ManagePasswordsBubbleNeverSaveViewDelegate
131 - (void)neverSavePasswordCancelled {
132   [self updateState];
135 @end
137 @implementation ManagePasswordsBubbleController (Testing)
139 - (NSViewController*)currentController {
140   return currentController_.get();
143 @end