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_cocoa.h"
7 #include "base/mac/scoped_block.h"
8 #include "chrome/browser/ui/browser_finder.h"
9 #import "chrome/browser/ui/cocoa/browser_window_controller.h"
10 #include "chrome/browser/ui/cocoa/location_bar/location_bar_view_mac.h"
11 #include "chrome/browser/ui/cocoa/location_bar/manage_passwords_decoration.h"
12 #import "chrome/browser/ui/cocoa/passwords/manage_passwords_bubble_controller.h"
13 #include "chrome/browser/ui/passwords/manage_passwords_icon.h"
14 #include "content/public/browser/web_contents.h"
16 typedef void (^Callback)(void);
18 @interface ManagePasswordsBubbleCocoaNotificationBridge : NSObject {
19 base::mac::ScopedBlock<Callback> callback_;
21 - (id)initWithCallback:(Callback)callback;
25 @implementation ManagePasswordsBubbleCocoaNotificationBridge
26 - (id)initWithCallback:(Callback)callback {
27 if ((self = [super init])) {
28 callback_.reset(callback, base::scoped_policy::RETAIN);
38 ManagePasswordsBubbleCocoa* ManagePasswordsBubbleCocoa::bubble_ = NULL;
40 ManagePasswordsBubbleCocoa::ManagePasswordsBubbleCocoa(
41 content::WebContents* webContents,
42 DisplayReason displayReason,
43 ManagePasswordsIcon* icon)
44 : ManagePasswordsBubble(webContents, displayReason),
48 webContents_(webContents),
51 icon_->SetActive(true);
54 ManagePasswordsBubbleCocoa::~ManagePasswordsBubbleCocoa() {
55 [[NSNotificationCenter defaultCenter] removeObserver:bridge_];
56 // Clear the global bubble_ pointer.
59 icon_->SetActive(false);
62 void ManagePasswordsBubbleCocoa::Show(bool user_action) {
63 // Create and show the bubble.
64 NSView* browserView = webContents_->GetNativeView();
66 NSWindow* browserWindow = [browserView window];
67 DCHECK(browserWindow);
68 controller_ = [[ManagePasswordsBubbleController alloc]
69 initWithParentWindow:browserWindow
71 [controller_ setShouldOpenAsKeyWindow:(user_action ? YES : NO)];
72 [controller_ showWindow:nil];
74 // Listen for close notification to perform cleanup: the window isn't
75 // necessarily closed by calling Close(). Use a block instead of directly
76 // calling OnClose from the bridge so that we don't need to expose OnClose
77 // in the public API of ManagePasswordsBubbleCocoa.
78 bridge_.reset([[ManagePasswordsBubbleCocoaNotificationBridge alloc]
82 [[NSNotificationCenter defaultCenter]
84 selector:@selector(onClose)
85 name:NSWindowWillCloseNotification
86 object:[controller_ window]];
89 void ManagePasswordsBubbleCocoa::Close() {
96 void ManagePasswordsBubbleCocoa::OnClose() {
101 void ManagePasswordsBubbleCocoa::Show(content::WebContents* webContents,
103 if (bubble_ && (bubble_->webContents_ != webContents)) {
104 // The bubble is currently shown for some other tab. We should close it now
105 // and open for |webContents|.
111 NSWindow* window = [webContents->GetNativeView() window];
113 // The tab isn't active right now.
116 BrowserWindowController* bwc =
117 [BrowserWindowController browserWindowControllerForWindow:window];
118 bubble_ = new ManagePasswordsBubbleCocoa(
120 user_action ? ManagePasswordsBubble::USER_ACTION
121 : ManagePasswordsBubble::AUTOMATIC,
122 [bwc locationBarBridge]->manage_passwords_decoration()->icon());
124 bubble_->Show(user_action);