OLD | NEW |
| (Empty) |
1 // Copyright (c) 2009 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 #include "chrome/browser/login_prompt.h" | |
6 #import "chrome/browser/login_prompt_mac.h" | |
7 | |
8 #include "app/l10n_util.h" | |
9 #include "base/mac_util.h" | |
10 #include "base/string_util.h" | |
11 #include "base/sys_string_conversions.h" | |
12 #include "base/utf_string_conversions.h" | |
13 #include "chrome/browser/browser_thread.h" | |
14 #include "chrome/browser/login_model.h" | |
15 #include "chrome/browser/password_manager/password_manager.h" | |
16 #include "chrome/browser/renderer_host/resource_dispatcher_host.h" | |
17 #include "chrome/browser/tab_contents/navigation_controller.h" | |
18 #include "chrome/browser/tab_contents/tab_contents.h" | |
19 #include "chrome/browser/tab_contents/tab_util.h" | |
20 #include "chrome/browser/ui/cocoa/constrained_window_mac.h" | |
21 #include "chrome/common/notification_service.h" | |
22 #include "grit/generated_resources.h" | |
23 #include "net/url_request/url_request.h" | |
24 #include "third_party/GTM/AppKit/GTMUILocalizerAndLayoutTweaker.h" | |
25 | |
26 using webkit_glue::PasswordForm; | |
27 | |
28 // ---------------------------------------------------------------------------- | |
29 // LoginHandlerMac | |
30 | |
31 // This class simply forwards the authentication from the LoginView (on | |
32 // the UI thread) to the net::URLRequest (on the I/O thread). | |
33 // This class uses ref counting to ensure that it lives until all InvokeLaters | |
34 // have been called. | |
35 class LoginHandlerMac : public LoginHandler, | |
36 public ConstrainedWindowMacDelegateCustomSheet { | |
37 public: | |
38 LoginHandlerMac(net::AuthChallengeInfo* auth_info, net::URLRequest* request) | |
39 : LoginHandler(auth_info, request), | |
40 sheet_controller_(nil) { | |
41 } | |
42 | |
43 virtual ~LoginHandlerMac() { | |
44 } | |
45 | |
46 // LoginModelObserver implementation. | |
47 virtual void OnAutofillDataAvailable(const std::wstring& username, | |
48 const std::wstring& password) { | |
49 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
50 | |
51 [sheet_controller_ autofillLogin:base::SysWideToNSString(username) | |
52 password:base::SysWideToNSString(password)]; | |
53 } | |
54 | |
55 // LoginHandler: | |
56 virtual void BuildViewForPasswordManager(PasswordManager* manager, | |
57 std::wstring explanation) { | |
58 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
59 | |
60 // Load nib here instead of in constructor. | |
61 sheet_controller_ = [[[LoginHandlerSheet alloc] | |
62 initWithLoginHandler:this] autorelease]; | |
63 init([sheet_controller_ window], sheet_controller_, | |
64 @selector(sheetDidEnd:returnCode:contextInfo:)); | |
65 | |
66 SetModel(manager); | |
67 | |
68 [sheet_controller_ setExplanation:base::SysWideToNSString(explanation)]; | |
69 | |
70 // Scary thread safety note: This can potentially be called *after* SetAuth | |
71 // or CancelAuth (say, if the request was cancelled before the UI thread got | |
72 // control). However, that's OK since any UI interaction in those functions | |
73 // will occur via an InvokeLater on the UI thread, which is guaranteed | |
74 // to happen after this is called (since this was InvokeLater'd first). | |
75 SetDialog(GetTabContentsForLogin()->CreateConstrainedDialog(this)); | |
76 | |
77 NotifyAuthNeeded(); | |
78 } | |
79 | |
80 // Overridden from ConstrainedWindowMacDelegate: | |
81 virtual void DeleteDelegate() { | |
82 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
83 | |
84 // The constrained window is going to delete itself; clear our pointer. | |
85 SetDialog(NULL); | |
86 SetModel(NULL); | |
87 | |
88 // Close sheet if it's still open, as required by | |
89 // ConstrainedWindowMacDelegate. | |
90 if (is_sheet_open()) | |
91 [NSApp endSheet:sheet()]; | |
92 | |
93 ReleaseSoon(); | |
94 } | |
95 | |
96 void OnLoginPressed(const std::wstring& username, | |
97 const std::wstring& password) { | |
98 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
99 | |
100 SetAuth(username, password); | |
101 } | |
102 | |
103 void OnCancelPressed() { | |
104 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
105 | |
106 CancelAuth(); | |
107 } | |
108 | |
109 private: | |
110 friend class LoginPrompt; | |
111 | |
112 // The Cocoa controller of the GUI. | |
113 LoginHandlerSheet* sheet_controller_; | |
114 | |
115 DISALLOW_COPY_AND_ASSIGN(LoginHandlerMac); | |
116 }; | |
117 | |
118 // static | |
119 LoginHandler* LoginHandler::Create(net::AuthChallengeInfo* auth_info, | |
120 net::URLRequest* request) { | |
121 return new LoginHandlerMac(auth_info, request); | |
122 } | |
123 | |
124 // ---------------------------------------------------------------------------- | |
125 // LoginHandlerSheet | |
126 | |
127 @implementation LoginHandlerSheet | |
128 | |
129 - (id)initWithLoginHandler:(LoginHandlerMac*)handler { | |
130 NSString* nibPath = | |
131 [mac_util::MainAppBundle() pathForResource:@"HttpAuthLoginSheet" | |
132 ofType:@"nib"]; | |
133 if ((self = [super initWithWindowNibPath:nibPath | |
134 owner:self])) { | |
135 handler_ = handler; | |
136 } | |
137 return self; | |
138 } | |
139 | |
140 - (void)dealloc { | |
141 // The buttons could be in a modal loop, so disconnect them so they cannot | |
142 // call back to us after we're dead. | |
143 [loginButton_ setTarget:nil]; | |
144 [cancelButton_ setTarget:nil]; | |
145 [super dealloc]; | |
146 } | |
147 | |
148 - (IBAction)loginPressed:(id)sender { | |
149 using base::SysNSStringToWide; | |
150 [NSApp endSheet:[self window]]; | |
151 handler_->OnLoginPressed(SysNSStringToWide([nameField_ stringValue]), | |
152 SysNSStringToWide([passwordField_ stringValue])); | |
153 } | |
154 | |
155 - (IBAction)cancelPressed:(id)sender { | |
156 [NSApp endSheet:[self window]]; | |
157 handler_->OnCancelPressed(); | |
158 } | |
159 | |
160 - (void)sheetDidEnd:(NSWindow*)sheet | |
161 returnCode:(int)returnCode | |
162 contextInfo:(void *)contextInfo { | |
163 [sheet orderOut:self]; | |
164 // Also called when user navigates to another page while the sheet is open. | |
165 } | |
166 | |
167 - (void)autofillLogin:(NSString*)login password:(NSString*)password { | |
168 if ([[nameField_ stringValue] length] == 0) { | |
169 [nameField_ setStringValue:login]; | |
170 [passwordField_ setStringValue:password]; | |
171 [nameField_ selectText:self]; | |
172 } | |
173 } | |
174 | |
175 - (void)setExplanation:(NSString*)explanation { | |
176 // Put in the text. | |
177 [explanationField_ setStringValue:explanation]; | |
178 | |
179 // Resize the TextField. | |
180 CGFloat explanationShift = | |
181 [GTMUILocalizerAndLayoutTweaker | |
182 sizeToFitFixedWidthTextField:explanationField_]; | |
183 | |
184 // Resize the window (no shifting needed due to window layout). | |
185 NSSize windowDelta = NSMakeSize(0, explanationShift); | |
186 [GTMUILocalizerAndLayoutTweaker | |
187 resizeWindowWithoutAutoResizingSubViews:[self window] | |
188 delta:windowDelta]; | |
189 } | |
190 | |
191 @end | |
OLD | NEW |