OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #import "chrome/browser/ui/cocoa/passwords/manage_passwords_bubble_cocoa.h" |
| 6 |
| 7 #include "base/mac/scoped_block.h" |
| 8 #include "chrome/browser/ui/browser_dialogs.h" |
| 9 #include "chrome/browser/ui/browser_finder.h" |
| 10 #import "chrome/browser/ui/cocoa/passwords/manage_passwords_bubble_controller.h" |
| 11 #include "chrome/browser/ui/passwords/manage_passwords_ui_controller.h" |
| 12 #include "content/public/browser/web_contents.h" |
| 13 |
| 14 typedef void (^Callback)(void); |
| 15 |
| 16 @interface ManagePasswordsBubbleCocoaNotificationBridge : NSObject { |
| 17 base::mac::ScopedBlock<Callback> callback_; |
| 18 } |
| 19 - (id)initWithCallback:(Callback)callback; |
| 20 - (void)onClose; |
| 21 @end |
| 22 |
| 23 @implementation ManagePasswordsBubbleCocoaNotificationBridge |
| 24 - (id)initWithCallback:(Callback)callback { |
| 25 if ((self = [super init])) { |
| 26 callback_.reset(callback, base::scoped_policy::RETAIN); |
| 27 } |
| 28 return self; |
| 29 } |
| 30 - (void)onClose { |
| 31 callback_.get()(); |
| 32 } |
| 33 @end |
| 34 |
| 35 // static |
| 36 ManagePasswordsBubbleCocoa* ManagePasswordsBubbleCocoa::bubble_ = NULL; |
| 37 |
| 38 ManagePasswordsBubbleCocoa::ManagePasswordsBubbleCocoa( |
| 39 content::WebContents* webContents, |
| 40 DisplayReason displayReason) |
| 41 : ManagePasswordsBubble(webContents, displayReason), |
| 42 closing_(false), |
| 43 controller_(nil), |
| 44 webContents_(webContents), |
| 45 bridge_(nil) { |
| 46 } |
| 47 |
| 48 ManagePasswordsBubbleCocoa::~ManagePasswordsBubbleCocoa() { |
| 49 [[NSNotificationCenter defaultCenter] removeObserver:bridge_]; |
| 50 // Clear the global instance pointer. |
| 51 bubble_ = NULL; |
| 52 } |
| 53 |
| 54 void ManagePasswordsBubbleCocoa::Show() { |
| 55 // Create and show the bubble. |
| 56 NSView* browserView = webContents_->GetNativeView(); |
| 57 DCHECK(browserView); |
| 58 NSWindow* browserWindow = [browserView window]; |
| 59 DCHECK(browserWindow); |
| 60 controller_ = [[ManagePasswordsBubbleController alloc] |
| 61 initWithParentWindow:browserWindow |
| 62 model:model()]; |
| 63 [controller_ showWindow:nil]; |
| 64 |
| 65 // Listen for close notification to perform cleanup: the window isn't |
| 66 // necessarily closed by calling Close(). Use a block instead of directly |
| 67 // calling OnClose from the bridge so that we don't need to expose OnClose |
| 68 // in the public API of ManagePasswordsBubbleCocoa. |
| 69 bridge_.reset([[ManagePasswordsBubbleCocoaNotificationBridge alloc] |
| 70 initWithCallback:^{ |
| 71 OnClose(); |
| 72 }]); |
| 73 [[NSNotificationCenter defaultCenter] |
| 74 addObserver:bridge_ |
| 75 selector:@selector(onClose) |
| 76 name:NSWindowWillCloseNotification |
| 77 object:[controller_ window]]; |
| 78 } |
| 79 |
| 80 void ManagePasswordsBubbleCocoa::Close() { |
| 81 if (!closing_) { |
| 82 closing_ = true; |
| 83 [controller_ close]; |
| 84 } |
| 85 } |
| 86 |
| 87 void ManagePasswordsBubbleCocoa::OnClose() { |
| 88 delete this; |
| 89 } |
| 90 |
| 91 // static |
| 92 void ManagePasswordsBubbleCocoa::ShowBubble(content::WebContents* webContents, |
| 93 DisplayReason displayReason) { |
| 94 if (bubble_) |
| 95 return; |
| 96 bubble_ = new ManagePasswordsBubbleCocoa(webContents, displayReason); |
| 97 bubble_->Show(); |
| 98 } |
OLD | NEW |