| 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_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" | |
| 15 | |
| 16 typedef void (^Callback)(void); | |
| 17 | |
| 18 @interface ManagePasswordsBubbleCocoaNotificationBridge : NSObject { | |
| 19 base::mac::ScopedBlock<Callback> callback_; | |
| 20 } | |
| 21 - (id)initWithCallback:(Callback)callback; | |
| 22 - (void)onClose; | |
| 23 @end | |
| 24 | |
| 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; | |
| 31 } | |
| 32 - (void)onClose { | |
| 33 callback_.get()(); | |
| 34 } | |
| 35 @end | |
| 36 | |
| 37 // static | |
| 38 ManagePasswordsBubbleCocoa* ManagePasswordsBubbleCocoa::bubble_ = NULL; | |
| 39 | |
| 40 ManagePasswordsBubbleCocoa::ManagePasswordsBubbleCocoa( | |
| 41 content::WebContents* webContents, | |
| 42 ManagePasswordsBubbleModel::DisplayReason displayReason, | |
| 43 ManagePasswordsIcon* icon) | |
| 44 : model_(webContents, displayReason), | |
| 45 icon_(icon), | |
| 46 closing_(false), | |
| 47 controller_(nil), | |
| 48 webContents_(webContents), | |
| 49 bridge_(nil) { | |
| 50 DCHECK(icon_); | |
| 51 icon_->SetActive(true); | |
| 52 } | |
| 53 | |
| 54 ManagePasswordsBubbleCocoa::~ManagePasswordsBubbleCocoa() { | |
| 55 [[NSNotificationCenter defaultCenter] removeObserver:bridge_]; | |
| 56 // Clear the global bubble_ pointer. | |
| 57 bubble_ = NULL; | |
| 58 if (icon_) | |
| 59 icon_->SetActive(false); | |
| 60 } | |
| 61 | |
| 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]; | |
| 73 | |
| 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]]; | |
| 87 } | |
| 88 | |
| 89 void ManagePasswordsBubbleCocoa::Close() { | |
| 90 if (!closing_) { | |
| 91 closing_ = true; | |
| 92 [controller_ close]; | |
| 93 } | |
| 94 } | |
| 95 | |
| 96 void ManagePasswordsBubbleCocoa::OnClose() { | |
| 97 delete this; | |
| 98 } | |
| 99 | |
| 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; | |
| 110 | |
| 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, user_action ? ManagePasswordsBubbleModel::USER_ACTION | |
| 120 : ManagePasswordsBubbleModel::AUTOMATIC, | |
| 121 [bwc locationBarBridge]->manage_passwords_decoration()->icon()); | |
| 122 | |
| 123 bubble_->Show(user_action); | |
| 124 } | |
| OLD | NEW |