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 "android_webview/browser/aw_login_delegate.h"
7 #include "base/android/jni_android.h"
8 #include "base/logging.h"
9 #include "base/supports_user_data.h"
10 #include "content/public/browser/browser_thread.h"
11 #include "content/public/browser/render_frame_host.h"
12 #include "content/public/browser/resource_dispatcher_host.h"
13 #include "content/public/browser/resource_request_info.h"
14 #include "content/public/browser/web_contents.h"
15 #include "net/base/auth.h"
16 #include "net/url_request/url_request.h"
18 using namespace base::android
;
20 using content::BrowserThread
;
21 using content::RenderFrameHost
;
22 using content::ResourceDispatcherHost
;
23 using content::ResourceRequestInfo
;
24 using content::WebContents
;
27 const char* kAuthAttemptsKey
= "android_webview_auth_attempts";
29 class UrlRequestAuthAttemptsData
: public base::SupportsUserData::Data
{
31 UrlRequestAuthAttemptsData() : auth_attempts_(0) { }
37 namespace android_webview
{
39 AwLoginDelegate::AwLoginDelegate(net::AuthChallengeInfo
* auth_info
,
40 net::URLRequest
* request
)
41 : auth_info_(auth_info
),
43 render_process_id_(0),
45 ResourceRequestInfo::GetRenderFrameForRequest(
46 request
, &render_process_id_
, &render_frame_id_
);
48 UrlRequestAuthAttemptsData
* count
=
49 static_cast<UrlRequestAuthAttemptsData
*>(
50 request
->GetUserData(kAuthAttemptsKey
));
53 count
= new UrlRequestAuthAttemptsData();
54 request
->SetUserData(kAuthAttemptsKey
, count
);
57 BrowserThread::PostTask(BrowserThread::UI
, FROM_HERE
,
58 base::Bind(&AwLoginDelegate::HandleHttpAuthRequestOnUIThread
,
59 this, (count
->auth_attempts_
== 0)));
60 count
->auth_attempts_
++;
63 AwLoginDelegate::~AwLoginDelegate() {
64 // The Auth handler holds a ref count back on |this| object, so it should be
65 // impossible to reach here while this object still owns an auth handler.
66 DCHECK(aw_http_auth_handler_
== NULL
);
69 void AwLoginDelegate::Proceed(const base::string16
& user
,
70 const base::string16
& password
) {
71 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
72 BrowserThread::PostTask(BrowserThread::IO
, FROM_HERE
,
73 base::Bind(&AwLoginDelegate::ProceedOnIOThread
,
74 this, user
, password
));
77 void AwLoginDelegate::Cancel() {
78 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
79 BrowserThread::PostTask(BrowserThread::IO
, FROM_HERE
,
80 base::Bind(&AwLoginDelegate::CancelOnIOThread
, this));
83 void AwLoginDelegate::HandleHttpAuthRequestOnUIThread(
84 bool first_auth_attempt
) {
85 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
87 aw_http_auth_handler_
.reset(AwHttpAuthHandlerBase::Create(
88 this, auth_info_
.get(), first_auth_attempt
));
90 RenderFrameHost
* render_frame_host
= RenderFrameHost::FromID(
91 render_process_id_
, render_frame_id_
);
92 WebContents
* web_contents
= WebContents::FromRenderFrameHost(
94 if (!aw_http_auth_handler_
->HandleOnUIThread(web_contents
)) {
100 void AwLoginDelegate::CancelOnIOThread() {
101 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
103 request_
->CancelAuth();
104 ResourceDispatcherHost::Get()->ClearLoginDelegateForRequest(request_
);
107 DeleteAuthHandlerSoon();
110 void AwLoginDelegate::ProceedOnIOThread(const base::string16
& user
,
111 const base::string16
& password
) {
112 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
114 request_
->SetAuth(net::AuthCredentials(user
, password
));
115 ResourceDispatcherHost::Get()->ClearLoginDelegateForRequest(request_
);
118 DeleteAuthHandlerSoon();
121 void AwLoginDelegate::OnRequestCancelled() {
122 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
124 DeleteAuthHandlerSoon();
127 void AwLoginDelegate::DeleteAuthHandlerSoon() {
128 if (!BrowserThread::CurrentlyOn(BrowserThread::UI
)) {
129 BrowserThread::PostTask(BrowserThread::UI
, FROM_HERE
,
130 base::Bind(&AwLoginDelegate::DeleteAuthHandlerSoon
, this));
133 aw_http_auth_handler_
.reset();
136 } // namespace android_webview