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() {
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_ showWindow:nil];
73 // Listen for close notification to perform cleanup: the window isn't
74 // necessarily closed by calling Close(). Use a block instead of directly
75 // calling OnClose from the bridge so that we don't need to expose OnClose
76 // in the public API of ManagePasswordsBubbleCocoa.
77 bridge_.reset([[ManagePasswordsBubbleCocoaNotificationBridge alloc]
81 [[NSNotificationCenter defaultCenter]
83 selector:@selector(onClose)
84 name:NSWindowWillCloseNotification
85 object:[controller_ window]];
88 void ManagePasswordsBubbleCocoa::Close() {
95 void ManagePasswordsBubbleCocoa::OnClose() {
100 void ManagePasswordsBubbleCocoa::Show(content::WebContents* webContents,
102 if (bubble_ && (bubble_->webContents_ != webContents)) {
103 // The bubble is currently shown for some other tab. We should close it now
104 // and open for |webContents|.
110 NSWindow* window = [webContents->GetNativeView() window];
112 // The tab isn't active right now.
115 BrowserWindowController* bwc =
116 [BrowserWindowController browserWindowControllerForWindow:window];
117 bubble_ = new ManagePasswordsBubbleCocoa(
119 user_action ? ManagePasswordsBubble::USER_ACTION
120 : ManagePasswordsBubble::AUTOMATIC,
121 [bwc locationBarBridge]->manage_passwords_decoration()->icon());