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 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "app/l10n_util.h" | 9 #include "app/l10n_util.h" |
10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
11 #include "base/lock.h" | 11 #include "base/lock.h" |
12 #include "base/utf_string_conversions.h" | 12 #include "base/utf_string_conversions.h" |
13 #include "chrome/browser/chrome_thread.h" | 13 #include "chrome/browser/chrome_thread.h" |
14 #include "chrome/browser/password_manager/password_manager.h" | 14 #include "chrome/browser/password_manager/password_manager.h" |
15 #include "chrome/browser/renderer_host/render_process_host.h" | 15 #include "chrome/browser/renderer_host/render_process_host.h" |
16 #include "chrome/browser/renderer_host/resource_dispatcher_host.h" | 16 #include "chrome/browser/renderer_host/resource_dispatcher_host.h" |
17 #include "chrome/browser/renderer_host/resource_dispatcher_host_request_info.h" | 17 #include "chrome/browser/renderer_host/resource_dispatcher_host_request_info.h" |
18 #include "chrome/browser/tab_contents/constrained_window.h" | 18 #include "chrome/browser/tab_contents/constrained_window.h" |
19 #include "chrome/browser/tab_contents/tab_contents.h" | 19 #include "chrome/browser/tab_contents/tab_contents.h" |
| 20 #include "chrome/browser/tab_contents/tab_util.h" |
20 #include "chrome/common/chrome_switches.h" | 21 #include "chrome/common/chrome_switches.h" |
| 22 #include "chrome/common/notification_service.h" |
21 #include "grit/generated_resources.h" | 23 #include "grit/generated_resources.h" |
22 #include "net/base/auth.h" | 24 #include "net/base/auth.h" |
23 #include "net/base/net_util.h" | 25 #include "net/base/net_util.h" |
24 #include "net/url_request/url_request.h" | 26 #include "net/url_request/url_request.h" |
25 | 27 |
26 using webkit_glue::PasswordForm; | 28 using webkit_glue::PasswordForm; |
27 | 29 |
28 class LoginHandlerImpl; | 30 class LoginHandlerImpl; |
29 | 31 |
30 // Helper to remove the ref from an URLRequest to the LoginHandler. | 32 // Helper to remove the ref from an URLRequest to the LoginHandler. |
(...skipping 25 matching lines...) Expand all Loading... |
56 } else { | 58 } else { |
57 // Take scheme, host, and port from the url. | 59 // Take scheme, host, and port from the url. |
58 signon_realm = url.GetOrigin().spec(); | 60 signon_realm = url.GetOrigin().spec(); |
59 // This ends with a "/". | 61 // This ends with a "/". |
60 } | 62 } |
61 signon_realm.append(WideToUTF8(auth_info.realm)); | 63 signon_realm.append(WideToUTF8(auth_info.realm)); |
62 return signon_realm; | 64 return signon_realm; |
63 } | 65 } |
64 | 66 |
65 // ---------------------------------------------------------------------------- | 67 // ---------------------------------------------------------------------------- |
| 68 // LoginHandler |
| 69 |
| 70 LoginHandler::LoginHandler(URLRequest* request) |
| 71 : handled_auth_(false), |
| 72 dialog_(NULL), |
| 73 request_(request), |
| 74 password_manager_(NULL), |
| 75 login_model_(NULL) { |
| 76 // This constructor is called on the I/O thread, so we cannot load the nib |
| 77 // here. BuildViewForPasswordManager() will be invoked on the UI thread |
| 78 // later, so wait with loading the nib until then. |
| 79 DCHECK(request_) << "LoginHandlerMac constructed with NULL request"; |
| 80 |
| 81 AddRef(); // matched by ReleaseSoon::ReleaseSoon(). |
| 82 if (!ResourceDispatcherHost::RenderViewForRequest( |
| 83 request_, &render_process_host_id_, &tab_contents_id_)) { |
| 84 NOTREACHED(); |
| 85 } |
| 86 } |
| 87 |
| 88 LoginHandler::~LoginHandler() { |
| 89 SetModel(NULL); |
| 90 } |
| 91 |
| 92 void LoginHandler::SetPasswordForm(const webkit_glue::PasswordForm& form) { |
| 93 password_form_ = form; |
| 94 } |
| 95 |
| 96 void LoginHandler::SetPasswordManager(PasswordManager* password_manager) { |
| 97 password_manager_ = password_manager; |
| 98 } |
| 99 |
| 100 TabContents* LoginHandler::GetTabContentsForLogin() const { |
| 101 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); |
| 102 |
| 103 return tab_util::GetTabContentsByID(render_process_host_id_, |
| 104 tab_contents_id_); |
| 105 } |
| 106 |
| 107 void LoginHandler::SetAuth(const std::wstring& username, |
| 108 const std::wstring& password) { |
| 109 if (WasAuthHandled(true)) |
| 110 return; |
| 111 |
| 112 // Tell the password manager the credentials were submitted / accepted. |
| 113 if (password_manager_) { |
| 114 password_form_.username_value = WideToUTF16Hack(username); |
| 115 password_form_.password_value = WideToUTF16Hack(password); |
| 116 password_manager_->ProvisionallySavePassword(password_form_); |
| 117 } |
| 118 |
| 119 ChromeThread::PostTask( |
| 120 ChromeThread::UI, FROM_HERE, |
| 121 NewRunnableMethod(this, &LoginHandler::CloseContentsDeferred)); |
| 122 ChromeThread::PostTask( |
| 123 ChromeThread::UI, FROM_HERE, |
| 124 NewRunnableMethod(this, &LoginHandler::SendNotifications)); |
| 125 ChromeThread::PostTask( |
| 126 ChromeThread::IO, FROM_HERE, |
| 127 NewRunnableMethod(this, &LoginHandler::SetAuthDeferred, |
| 128 username, password)); |
| 129 } |
| 130 |
| 131 void LoginHandler::CancelAuth() { |
| 132 if (WasAuthHandled(true)) |
| 133 return; |
| 134 |
| 135 ChromeThread::PostTask( |
| 136 ChromeThread::UI, FROM_HERE, |
| 137 NewRunnableMethod(this, &LoginHandler::CloseContentsDeferred)); |
| 138 ChromeThread::PostTask( |
| 139 ChromeThread::UI, FROM_HERE, |
| 140 NewRunnableMethod(this, &LoginHandler::SendNotifications)); |
| 141 ChromeThread::PostTask( |
| 142 ChromeThread::IO, FROM_HERE, |
| 143 NewRunnableMethod(this, &LoginHandler::CancelAuthDeferred)); |
| 144 } |
| 145 |
| 146 void LoginHandler::OnRequestCancelled() { |
| 147 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::IO)) << |
| 148 "Why is OnRequestCancelled called from the UI thread?"; |
| 149 |
| 150 // Reference is no longer valid. |
| 151 request_ = NULL; |
| 152 |
| 153 // Give up on auth if the request was cancelled. |
| 154 CancelAuth(); |
| 155 } |
| 156 |
| 157 void LoginHandler::SetModel(LoginModel* model) { |
| 158 if (login_model_) |
| 159 login_model_->SetObserver(NULL); |
| 160 login_model_ = model; |
| 161 if (login_model_) |
| 162 login_model_->SetObserver(this); |
| 163 } |
| 164 |
| 165 void LoginHandler::SetDialog(ConstrainedWindow* dialog) { |
| 166 dialog_ = dialog; |
| 167 } |
| 168 |
| 169 // Notify observers that authentication is needed or received. The automation |
| 170 // proxy uses this for testing. |
| 171 void LoginHandler::SendNotifications() { |
| 172 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); |
| 173 |
| 174 NotificationService* service = NotificationService::current(); |
| 175 TabContents* requesting_contents = GetTabContentsForLogin(); |
| 176 if (!requesting_contents) |
| 177 return; |
| 178 |
| 179 NavigationController* controller = &requesting_contents->controller(); |
| 180 |
| 181 if (!WasAuthHandled(false)) { |
| 182 LoginNotificationDetails details(this); |
| 183 service->Notify(NotificationType::AUTH_NEEDED, |
| 184 Source<NavigationController>(controller), |
| 185 Details<LoginNotificationDetails>(&details)); |
| 186 } else { |
| 187 service->Notify(NotificationType::AUTH_SUPPLIED, |
| 188 Source<NavigationController>(controller), |
| 189 NotificationService::NoDetails()); |
| 190 } |
| 191 } |
| 192 |
| 193 void LoginHandler::ReleaseSoon() { |
| 194 if (!WasAuthHandled(true)) { |
| 195 ChromeThread::PostTask( |
| 196 ChromeThread::IO, FROM_HERE, |
| 197 NewRunnableMethod(this, &LoginHandler::CancelAuthDeferred)); |
| 198 ChromeThread::PostTask( |
| 199 ChromeThread::UI, FROM_HERE, |
| 200 NewRunnableMethod(this, &LoginHandler::SendNotifications)); |
| 201 } |
| 202 |
| 203 // Delete this object once all InvokeLaters have been called. |
| 204 ChromeThread::ReleaseSoon(ChromeThread::IO, FROM_HERE, this); |
| 205 } |
| 206 |
| 207 // Returns whether authentication had been handled (SetAuth or CancelAuth). |
| 208 // If |set_handled| is true, it will mark authentication as handled. |
| 209 bool LoginHandler::WasAuthHandled(bool set_handled) { |
| 210 AutoLock lock(handled_auth_lock_); |
| 211 bool was_handled = handled_auth_; |
| 212 if (set_handled) |
| 213 handled_auth_ = true; |
| 214 return was_handled; |
| 215 } |
| 216 |
| 217 // Calls SetAuth from the IO loop. |
| 218 void LoginHandler::SetAuthDeferred(const std::wstring& username, |
| 219 const std::wstring& password) { |
| 220 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::IO)); |
| 221 |
| 222 if (request_) { |
| 223 request_->SetAuth(username, password); |
| 224 ResetLoginHandlerForRequest(request_); |
| 225 } |
| 226 } |
| 227 |
| 228 // Calls CancelAuth from the IO loop. |
| 229 void LoginHandler::CancelAuthDeferred() { |
| 230 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::IO)); |
| 231 |
| 232 if (request_) { |
| 233 request_->CancelAuth(); |
| 234 // Verify that CancelAuth does destroy the request via our delegate. |
| 235 DCHECK(request_ != NULL); |
| 236 ResetLoginHandlerForRequest(request_); |
| 237 } |
| 238 } |
| 239 |
| 240 // Closes the view_contents from the UI loop. |
| 241 void LoginHandler::CloseContentsDeferred() { |
| 242 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); |
| 243 |
| 244 // The hosting ConstrainedWindow may have been freed. |
| 245 if (dialog_) |
| 246 dialog_->CloseConstrainedWindow(); |
| 247 } |
| 248 |
| 249 // ---------------------------------------------------------------------------- |
66 // LoginDialogTask | 250 // LoginDialogTask |
67 | 251 |
68 // This task is run on the UI thread and creates a constrained window with | 252 // This task is run on the UI thread and creates a constrained window with |
69 // a LoginView to prompt the user. The response will be sent to LoginHandler, | 253 // a LoginView to prompt the user. The response will be sent to LoginHandler, |
70 // which then routes it to the URLRequest on the I/O thread. | 254 // which then routes it to the URLRequest on the I/O thread. |
71 class LoginDialogTask : public Task { | 255 class LoginDialogTask : public Task { |
72 public: | 256 public: |
73 LoginDialogTask(const GURL& request_url, | 257 LoginDialogTask(const GURL& request_url, |
74 net::AuthChallengeInfo* auth_info, | 258 net::AuthChallengeInfo* auth_info, |
75 LoginHandler* handler) | 259 LoginHandler* handler) |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
152 // Public API | 336 // Public API |
153 | 337 |
154 LoginHandler* CreateLoginPrompt(net::AuthChallengeInfo* auth_info, | 338 LoginHandler* CreateLoginPrompt(net::AuthChallengeInfo* auth_info, |
155 URLRequest* request) { | 339 URLRequest* request) { |
156 LoginHandler* handler = LoginHandler::Create(request); | 340 LoginHandler* handler = LoginHandler::Create(request); |
157 ChromeThread::PostTask( | 341 ChromeThread::PostTask( |
158 ChromeThread::UI, FROM_HERE, new LoginDialogTask( | 342 ChromeThread::UI, FROM_HERE, new LoginDialogTask( |
159 request->url(), auth_info, handler)); | 343 request->url(), auth_info, handler)); |
160 return handler; | 344 return handler; |
161 } | 345 } |
OLD | NEW |