| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2012 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 "content/shell/shell_login_dialog.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/logging.h" |
| 9 #include "base/utf_string_conversions.h" |
| 10 #include "content/public/browser/browser_thread.h" |
| 11 #include "content/public/browser/resource_dispatcher_host.h" |
| 12 #include "net/base/auth.h" |
| 13 #include "net/url_request/url_request.h" |
| 14 #include "ui/base/text/text_elider.h" |
| 15 |
| 16 namespace content { |
| 17 |
| 18 ShellLoginDialog::ShellLoginDialog( |
| 19 net::AuthChallengeInfo* auth_info, |
| 20 net::URLRequest* request) : auth_info_(auth_info), |
| 21 request_(request) { |
| 22 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 23 BrowserThread::PostTask( |
| 24 BrowserThread::UI, FROM_HERE, |
| 25 base::Bind(&ShellLoginDialog::PrepDialog, this, |
| 26 ASCIIToUTF16(auth_info->challenger.ToString()), |
| 27 UTF8ToUTF16(auth_info->realm))); |
| 28 } |
| 29 |
| 30 ShellLoginDialog::~ShellLoginDialog() { |
| 31 // Cannot post any tasks here; this object is going away and cannot be |
| 32 // referenced/dereferenced. |
| 33 } |
| 34 |
| 35 void ShellLoginDialog::OnRequestCancelled() { |
| 36 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 37 BrowserThread::PostTask( |
| 38 BrowserThread::UI, FROM_HERE, |
| 39 base::Bind(&ShellLoginDialog::PlatformRequestCancelled, this)); |
| 40 } |
| 41 |
| 42 void ShellLoginDialog::PrepDialog(const string16& host, |
| 43 const string16& realm) { |
| 44 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 45 // The realm is controlled by the remote server, so there is no reason to |
| 46 // believe it is of a reasonable length. |
| 47 string16 elided_realm; |
| 48 ui::ElideString(realm, 120, &elided_realm); |
| 49 |
| 50 string16 explanation = |
| 51 ASCIIToUTF16("The server ") + host + |
| 52 ASCIIToUTF16(" requires a username and password."); |
| 53 |
| 54 if (!elided_realm.empty()) { |
| 55 explanation += ASCIIToUTF16(" The server says: "); |
| 56 explanation += elided_realm; |
| 57 explanation += ASCIIToUTF16("."); |
| 58 } |
| 59 |
| 60 PlatformCreateDialog(explanation); |
| 61 } |
| 62 |
| 63 void ShellLoginDialog::UserAcceptedAuth(const string16& username, |
| 64 const string16& password) { |
| 65 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 66 BrowserThread::PostTask( |
| 67 BrowserThread::IO, FROM_HERE, |
| 68 base::Bind(&ShellLoginDialog::SendAuthToRequester, this, |
| 69 true, username, password)); |
| 70 } |
| 71 |
| 72 void ShellLoginDialog::UserCancelledAuth() { |
| 73 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 74 BrowserThread::PostTask( |
| 75 BrowserThread::IO, FROM_HERE, |
| 76 base::Bind(&ShellLoginDialog::SendAuthToRequester, this, |
| 77 false, string16(), string16())); |
| 78 BrowserThread::PostTask( |
| 79 BrowserThread::UI, FROM_HERE, |
| 80 base::Bind(&ShellLoginDialog::PlatformCleanUp, this)); |
| 81 } |
| 82 |
| 83 void ShellLoginDialog::SendAuthToRequester(bool success, |
| 84 const string16& username, |
| 85 const string16& password) { |
| 86 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 87 if (success) |
| 88 request_->SetAuth(net::AuthCredentials(username, password)); |
| 89 else |
| 90 request_->CancelAuth(); |
| 91 ResourceDispatcherHost::Get()->ClearLoginDelegateForRequest(request_); |
| 92 |
| 93 BrowserThread::PostTask( |
| 94 BrowserThread::UI, FROM_HERE, |
| 95 base::Bind(&ShellLoginDialog::PlatformCleanUp, this)); |
| 96 } |
| 97 |
| 98 #if !defined(OS_MACOSX) |
| 99 // Bogus implementations for linking. They are never called because |
| 100 // ResourceDispatcherHostDelegate::CreateLoginDelegate returns NULL. |
| 101 // TODO: implement ShellLoginDialog for other platforms, drop this #if |
| 102 void ShellLoginDialog::PlatformCreateDialog(const string16& message) {} |
| 103 void ShellLoginDialog::PlatformCleanUp() {} |
| 104 void ShellLoginDialog::PlatformRequestCancelled() {} |
| 105 #endif |
| 106 |
| 107 } // namespace content |
| OLD | NEW |