OLD | NEW |
---|---|
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/login_prompt.h" | 5 #include "chrome/browser/login_prompt.h" |
6 #import "chrome/browser/login_prompt_mac.h" | 6 #import "chrome/browser/login_prompt_mac.h" |
7 | 7 |
8 #include "app/l10n_util.h" | 8 #include "app/l10n_util.h" |
9 #include "base/mac_util.h" | 9 #include "base/mac_util.h" |
10 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
11 #include "base/sys_string_conversions.h" | 11 #include "base/sys_string_conversions.h" |
12 #include "chrome/browser/cocoa/constrained_window_mac.h" | 12 #include "chrome/browser/cocoa/constrained_window_mac.h" |
13 #include "chrome/browser/login_model.h" | |
13 #include "chrome/browser/password_manager/password_manager.h" | 14 #include "chrome/browser/password_manager/password_manager.h" |
14 #include "chrome/browser/renderer_host/resource_dispatcher_host.h" | 15 #include "chrome/browser/renderer_host/resource_dispatcher_host.h" |
15 #include "chrome/browser/tab_contents/navigation_controller.h" | 16 #include "chrome/browser/tab_contents/navigation_controller.h" |
16 #include "chrome/browser/tab_contents/tab_contents.h" | 17 #include "chrome/browser/tab_contents/tab_contents.h" |
17 #include "chrome/browser/tab_contents/tab_util.h" | 18 #include "chrome/browser/tab_contents/tab_util.h" |
18 #include "chrome/common/notification_service.h" | 19 #include "chrome/common/notification_service.h" |
19 #include "grit/generated_resources.h" | 20 #include "grit/generated_resources.h" |
20 #include "net/url_request/url_request.h" | 21 #include "net/url_request/url_request.h" |
21 | 22 |
22 using webkit_glue::PasswordForm; | 23 using webkit_glue::PasswordForm; |
23 | 24 |
24 // ---------------------------------------------------------------------------- | 25 // ---------------------------------------------------------------------------- |
25 // LoginHandlerMac | 26 // LoginHandlerMac |
26 | 27 |
27 // This class simply forwards the authentication from the LoginView (on | 28 // This class simply forwards the authentication from the LoginView (on |
28 // the UI thread) to the URLRequest (on the I/O thread). | 29 // the UI thread) to the URLRequest (on the I/O thread). |
29 // This class uses ref counting to ensure that it lives until all InvokeLaters | 30 // This class uses ref counting to ensure that it lives until all InvokeLaters |
30 // have been called. | 31 // have been called. |
31 class LoginHandlerMac : public LoginHandler, | 32 class LoginHandlerMac : public LoginHandler, |
32 public base::RefCountedThreadSafe<LoginHandlerMac>, | 33 public base::RefCountedThreadSafe<LoginHandlerMac>, |
33 public ConstrainedWindowMacDelegateCustomSheet { | 34 public ConstrainedWindowMacDelegateCustomSheet, |
35 public LoginModelObserver { | |
34 public: | 36 public: |
35 LoginHandlerMac(URLRequest* request, MessageLoop* ui_loop) | 37 LoginHandlerMac(URLRequest* request, MessageLoop* ui_loop) |
36 : handled_auth_(false), | 38 : handled_auth_(false), |
37 dialog_(NULL), | 39 dialog_(NULL), |
38 ui_loop_(ui_loop), | 40 ui_loop_(ui_loop), |
39 request_(request), | 41 request_(request), |
40 request_loop_(MessageLoop::current()), | 42 request_loop_(MessageLoop::current()), |
41 password_manager_(NULL) { | 43 password_manager_(NULL), |
44 sheet_controller_(nil), | |
45 login_model_(NULL) { | |
42 // This constructor is called on the I/O thread, so we cannot load the nib | 46 // This constructor is called on the I/O thread, so we cannot load the nib |
43 // here. BuildViewForPasswordManager() will be invoked on the UI thread | 47 // here. BuildViewForPasswordManager() will be invoked on the UI thread |
44 // later, so wait with loading the nib until then. | 48 // later, so wait with loading the nib until then. |
45 DCHECK(request_) << "LoginHandlerMac constructed with NULL request"; | 49 DCHECK(request_) << "LoginHandlerMac constructed with NULL request"; |
46 | 50 |
47 AddRef(); // matched by ReleaseLater. | 51 AddRef(); // matched by ReleaseLater. |
48 if (!ResourceDispatcherHost::RenderViewForRequest(request_, | 52 if (!ResourceDispatcherHost::RenderViewForRequest(request_, |
49 &render_process_host_id_, | 53 &render_process_host_id_, |
50 &tab_contents_id_)) { | 54 &tab_contents_id_)) { |
51 NOTREACHED(); | 55 NOTREACHED(); |
52 } | 56 } |
53 } | 57 } |
54 | 58 |
55 virtual ~LoginHandlerMac() { | 59 virtual ~LoginHandlerMac() { |
60 if (login_model_) | |
61 login_model_->SetObserver(NULL); | |
62 } | |
63 | |
64 void SetModel(LoginModel* model) { | |
65 login_model_ = model; | |
66 if (login_model_) | |
67 login_model_->SetObserver(this); | |
68 } | |
69 | |
70 // LoginModelObserver implementation. | |
71 virtual void OnAutofillDataAvailable(const std::wstring& username, | |
72 const std::wstring& password) { | |
73 [sheet_controller_ autofillLogin:base::SysWideToNSString(username) | |
74 password:base::SysWideToNSString(password)]; | |
56 } | 75 } |
57 | 76 |
58 // LoginHandler: | 77 // LoginHandler: |
59 virtual void BuildViewForPasswordManager(PasswordManager* manager, | 78 virtual void BuildViewForPasswordManager(PasswordManager* manager, |
60 std::wstring explanation) { | 79 std::wstring explanation) { |
61 DCHECK(MessageLoop::current() == ui_loop_); | 80 DCHECK(MessageLoop::current() == ui_loop_); |
62 | 81 |
63 // Load nib here instead of in constructor. | 82 // Load nib here instead of in constructor. |
64 LoginHandlerSheet* sheetController = [[[LoginHandlerSheet alloc] | 83 sheet_controller_ = [[[LoginHandlerSheet alloc] |
65 initWithLoginHandler:this] autorelease]; | 84 initWithLoginHandler:this] autorelease]; |
66 init([sheetController window], sheetController, | 85 init([sheet_controller_ window], sheet_controller_, |
67 @selector(sheetDidEnd:returnCode:contextInfo:)); | 86 @selector(sheetDidEnd:returnCode:contextInfo:)); |
68 | 87 |
88 SetModel(manager); | |
89 | |
69 // Scary thread safety note: This can potentially be called *after* SetAuth | 90 // Scary thread safety note: This can potentially be called *after* SetAuth |
70 // or CancelAuth (say, if the request was cancelled before the UI thread got | 91 // or CancelAuth (say, if the request was cancelled before the UI thread got |
71 // control). However, that's OK since any UI interaction in those functions | 92 // control). However, that's OK since any UI interaction in those functions |
72 // will occur via an InvokeLater on the UI thread, which is guaranteed | 93 // will occur via an InvokeLater on the UI thread, which is guaranteed |
73 // to happen after this is called (since this was InvokeLater'd first). | 94 // to happen after this is called (since this was InvokeLater'd first). |
74 dialog_ = GetTabContentsForLogin()->CreateConstrainedDialog(this); | 95 dialog_ = GetTabContentsForLogin()->CreateConstrainedDialog(this); |
75 | 96 |
76 SendNotifications(); | 97 SendNotifications(); |
77 } | 98 } |
78 | 99 |
(...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
258 | 279 |
259 // Points to the password manager owned by the TabContents requesting auth. | 280 // Points to the password manager owned by the TabContents requesting auth. |
260 // Can be null if the TabContents is not a TabContents. | 281 // Can be null if the TabContents is not a TabContents. |
261 // This should only be accessed on the ui_loop_. | 282 // This should only be accessed on the ui_loop_. |
262 PasswordManager* password_manager_; | 283 PasswordManager* password_manager_; |
263 | 284 |
264 // Cached from the URLRequest, in case it goes NULL on us. | 285 // Cached from the URLRequest, in case it goes NULL on us. |
265 int render_process_host_id_; | 286 int render_process_host_id_; |
266 int tab_contents_id_; | 287 int tab_contents_id_; |
267 | 288 |
289 // The Cocoa controller of the GUI. | |
290 LoginHandlerSheet* sheet_controller_; | |
291 | |
292 // If not null, points to a model we need to notify of our own destruction | |
293 // so it doesn't try and access this when its too late. | |
294 LoginModel* login_model_; | |
295 | |
268 DISALLOW_COPY_AND_ASSIGN(LoginHandlerMac); | 296 DISALLOW_COPY_AND_ASSIGN(LoginHandlerMac); |
269 }; | 297 }; |
270 | 298 |
271 // static | 299 // static |
272 LoginHandler* LoginHandler::Create(URLRequest* request, MessageLoop* ui_loop) { | 300 LoginHandler* LoginHandler::Create(URLRequest* request, MessageLoop* ui_loop) { |
273 return new LoginHandlerMac(request, ui_loop); | 301 return new LoginHandlerMac(request, ui_loop); |
274 } | 302 } |
275 | 303 |
276 // ---------------------------------------------------------------------------- | 304 // ---------------------------------------------------------------------------- |
277 // LoginHandlerSheet | 305 // LoginHandlerSheet |
(...skipping 20 matching lines...) Expand all Loading... | |
298 handler_->OnCancelPressed(); | 326 handler_->OnCancelPressed(); |
299 } | 327 } |
300 | 328 |
301 - (void)sheetDidEnd:(NSWindow*)sheet | 329 - (void)sheetDidEnd:(NSWindow*)sheet |
302 returnCode:(int)returnCode | 330 returnCode:(int)returnCode |
303 contextInfo:(void *)contextInfo { | 331 contextInfo:(void *)contextInfo { |
304 [sheet orderOut:self]; | 332 [sheet orderOut:self]; |
305 // Also called when user navigates to another page while the sheet is open. | 333 // Also called when user navigates to another page while the sheet is open. |
306 } | 334 } |
307 | 335 |
336 - (void)autofillLogin:(NSString*)login password:(NSString*)password { | |
337 if ([[nameField_ stringValue] length] == 0) { | |
stuartmorgan
2009/08/25 16:54:39
Per IIRC comments, I believe that because of the d
| |
338 [nameField_ setStringValue:login]; | |
339 [passwordField_ setStringValue:password]; | |
340 [nameField_ selectText:self]; | |
341 } | |
342 } | |
343 | |
308 @end | 344 @end |
OLD | NEW |