Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(146)

Side by Side Diff: chrome/browser/ui/cocoa/passwords/manage_passwords_bubble_cocoa.mm

Issue 441683002: Add ManagePasswordsBubbleCocoa and unit tests. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 ManagePasswordsBubbleCocoa::ManagePasswordsBubbleCocoa(
36 content::WebContents* webContents,
37 DisplayReason displayReason)
38 : ManagePasswordsBubble(webContents, displayReason),
39 closing_(false),
40 controller_(nil),
41 webContents_(webContents),
42 bridge_(nil) {
43 }
44
45 ManagePasswordsBubbleCocoa::~ManagePasswordsBubbleCocoa() {
46 [[NSNotificationCenter defaultCenter] removeObserver:bridge_];
47 // Clear the global instance pointer.
48 bubble_ = NULL;
49 }
50
51 ManagePasswordsBubbleCocoa* ManagePasswordsBubbleCocoa::bubble_ = NULL;
Scott Hess - ex-Googler 2014/08/22 21:55:29 I'd put this above the constructor. Also, convent
dconnelly 2014/08/25 09:31:34 Done.
52
53 void ManagePasswordsBubbleCocoa::Show() {
54 // Create and show the bubble.
55 NSView* browserView = webContents_->GetNativeView();
56 DCHECK(browserView);
57 NSWindow* browserWindow = [browserView window];
58 DCHECK(browserWindow);
59 controller_ = [[ManagePasswordsBubbleController alloc]
60 initWithParentWindow:browserWindow
61 model:model()];
62 [controller_ showWindow:nil];
63
64 // Listen for close notification to perform cleanup: the window isn't
65 // necessarily closed by calling Close(). Use a block instead of directly
66 // calling OnClose from the bridge so that we don't need to expose OnClose
67 // in the public API of ManagePasswordsBubbleCocoa.
68 bridge_.reset([[ManagePasswordsBubbleCocoaNotificationBridge alloc]
69 initWithCallback:^{
70 OnClose();
71 }]);
72 [[NSNotificationCenter defaultCenter]
73 addObserver:bridge_
74 selector:@selector(onClose)
75 name:NSWindowWillCloseNotification
76 object:[controller_ window]];
77 }
78
79 void ManagePasswordsBubbleCocoa::Close() {
80 if (!closing_) {
81 closing_ = true;
82 [controller_ close];
83 }
84 }
85
86 void ManagePasswordsBubbleCocoa::OnClose() {
87 delete this;
88 }
89
90 // static
91 void ManagePasswordsBubbleCocoa::ShowBubble(content::WebContents* webContents,
92 DisplayReason displayReason) {
93 if (bubble_)
94 return;
95 bubble_ = new ManagePasswordsBubbleCocoa(webContents, displayReason);
96 bubble_->Show();
97 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698