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.
5 #include "chrome/browser/ui/login/login_prompt.h"
7 #include "base/logging.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/strings/string16.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "chrome/browser/android/tab_android.h"
12 #include "chrome/browser/ui/android/chrome_http_auth_handler.h"
13 #include "chrome/browser/ui/login/login_prompt.h"
14 #include "content/public/browser/browser_thread.h"
15 #include "content/public/browser/web_contents.h"
16 #include "net/base/auth.h"
18 using content::BrowserThread
;
19 using net::URLRequest
;
20 using net::AuthChallengeInfo
;
22 class LoginHandlerAndroid
: public LoginHandler
{
24 LoginHandlerAndroid(AuthChallengeInfo
* auth_info
, URLRequest
* request
)
25 : LoginHandler(auth_info
, request
) {
28 // LoginHandler methods:
30 virtual void OnAutofillDataAvailable(
31 const base::string16
& username
,
32 const base::string16
& password
) OVERRIDE
{
33 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
34 DCHECK(chrome_http_auth_handler_
.get() != NULL
);
35 chrome_http_auth_handler_
->OnAutofillDataAvailable(
38 virtual void OnLoginModelDestroying() OVERRIDE
{}
40 virtual void BuildViewForPasswordManager(
41 PasswordManager
* manager
,
42 const base::string16
& explanation
) OVERRIDE
{
43 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
45 // Get pointer to TabAndroid
46 content::WebContents
* web_contents
= GetWebContentsForLogin();
48 TabAndroid
* tab_android
= TabAndroid::FromWebContents(web_contents
);
50 // Notify TabAndroid that HTTP authentication is required for current page.
52 chrome_http_auth_handler_
.reset(new ChromeHttpAuthHandler(explanation
));
53 chrome_http_auth_handler_
->Init();
54 chrome_http_auth_handler_
->SetObserver(this);
56 tab_android
->OnReceivedHttpAuthRequest(
57 chrome_http_auth_handler_
.get()->GetJavaObject(),
58 base::ASCIIToUTF16(auth_info()->challenger
.ToString()),
59 base::UTF8ToUTF16(auth_info()->realm
));
61 // Register to receive a callback to OnAutofillDataAvailable().
66 LOG(WARNING
) << "HTTP Authentication failed because TabAndroid is "
72 virtual ~LoginHandlerAndroid() {}
74 virtual void CloseDialog() OVERRIDE
{}
77 scoped_ptr
<ChromeHttpAuthHandler
> chrome_http_auth_handler_
;
81 LoginHandler
* LoginHandler::Create(net::AuthChallengeInfo
* auth_info
,
82 net::URLRequest
* request
) {
83 return new LoginHandlerAndroid(auth_info
, request
);