Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / ui / cocoa / passwords / manage_passwords_bubble_cocoa.mm
blobdc142b1ec699cbb9dd80c35ecb1059ba707fa6a1
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;
22 - (void)onClose;
23 @end
25 @implementation ManagePasswordsBubbleCocoaNotificationBridge
26 - (id)initWithCallback:(Callback)callback {
27   if ((self = [super init])) {
28     callback_.reset(callback, base::scoped_policy::RETAIN);
29   }
30   return self;
32 - (void)onClose {
33   callback_.get()();
35 @end
37 // static
38 ManagePasswordsBubbleCocoa* ManagePasswordsBubbleCocoa::bubble_ = NULL;
40 ManagePasswordsBubbleCocoa::ManagePasswordsBubbleCocoa(
41     content::WebContents* webContents,
42     DisplayReason displayReason,
43     ManagePasswordsIcon* icon)
44     : ManagePasswordsBubble(webContents, displayReason),
45       icon_(icon),
46       closing_(false),
47       controller_(nil),
48       webContents_(webContents),
49       bridge_(nil) {
50   DCHECK(icon_);
51   icon_->SetActive(true);
54 ManagePasswordsBubbleCocoa::~ManagePasswordsBubbleCocoa() {
55   [[NSNotificationCenter defaultCenter] removeObserver:bridge_];
56   // Clear the global bubble_ pointer.
57   bubble_ = NULL;
58   if (icon_)
59     icon_->SetActive(false);
62 void ManagePasswordsBubbleCocoa::Show(bool user_action) {
63   // Create and show the bubble.
64   NSView* browserView = webContents_->GetNativeView();
65   DCHECK(browserView);
66   NSWindow* browserWindow = [browserView window];
67   DCHECK(browserWindow);
68   controller_ = [[ManagePasswordsBubbleController alloc]
69       initWithParentWindow:browserWindow
70                      model:model()];
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]
79       initWithCallback:^{
80           OnClose();
81       }]);
82   [[NSNotificationCenter defaultCenter]
83       addObserver:bridge_
84          selector:@selector(onClose)
85              name:NSWindowWillCloseNotification
86            object:[controller_ window]];
89 void ManagePasswordsBubbleCocoa::Close() {
90   if (!closing_) {
91     closing_ = true;
92     [controller_ close];
93   }
96 void ManagePasswordsBubbleCocoa::OnClose() {
97   delete this;
100 // static
101 void ManagePasswordsBubbleCocoa::Show(content::WebContents* webContents,
102                                       bool user_action) {
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|.
106     bubble_->Close();
107   }
108   if (bubble_)
109     return;
111   NSWindow* window = [webContents->GetNativeView() window];
112   if (!window) {
113     // The tab isn't active right now.
114     return;
115   }
116   BrowserWindowController* bwc =
117       [BrowserWindowController browserWindowControllerForWindow:window];
118   bubble_ = new ManagePasswordsBubbleCocoa(
119       webContents,
120       user_action ? ManagePasswordsBubble::USER_ACTION
121                   : ManagePasswordsBubble::AUTOMATIC,
122       [bwc locationBarBridge]->manage_passwords_decoration()->icon());
124   bubble_->Show(user_action);