Revert of Add button to add new FSP services to Files app. (patchset #8 id:140001...
[chromium-blink-merge.git] / chrome / browser / ui / cocoa / passwords / manage_passwords_bubble_cocoa.mm
blob4f1974313bbb2659159065286ce31629de56f148
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() {
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_ 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]
78       initWithCallback:^{
79           OnClose();
80       }]);
81   [[NSNotificationCenter defaultCenter]
82       addObserver:bridge_
83          selector:@selector(onClose)
84              name:NSWindowWillCloseNotification
85            object:[controller_ window]];
88 void ManagePasswordsBubbleCocoa::Close() {
89   if (!closing_) {
90     closing_ = true;
91     [controller_ close];
92   }
95 void ManagePasswordsBubbleCocoa::OnClose() {
96   delete this;
99 // static
100 void ManagePasswordsBubbleCocoa::Show(content::WebContents* webContents,
101                                       bool user_action) {
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|.
105     bubble_->Close();
106   }
107   if (bubble_)
108     return;
110   NSWindow* window = [webContents->GetNativeView() window];
111   if (!window) {
112     // The tab isn't active right now.
113     return;
114   }
115   BrowserWindowController* bwc =
116       [BrowserWindowController browserWindowControllerForWindow:window];
117   bubble_ = new ManagePasswordsBubbleCocoa(
118       webContents,
119       user_action ? ManagePasswordsBubble::USER_ACTION
120                   : ManagePasswordsBubble::AUTOMATIC,
121       [bwc locationBarBridge]->manage_passwords_decoration()->icon());
123   bubble_->Show();