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/native/aw_http_auth_handler.h"
7 #include "android_webview/browser/aw_login_delegate.h"
8 #include "android_webview/native/aw_contents.h"
9 #include "base/android/jni_android.h"
10 #include "base/android/jni_string.h"
11 #include "content/public/browser/browser_thread.h"
12 #include "jni/AwHttpAuthHandler_jni.h"
13 #include "net/base/auth.h"
14 #include "content/public/browser/web_contents.h"
16 using base::android::ConvertJavaStringToUTF16
;
17 using content::BrowserThread
;
19 namespace android_webview
{
21 AwHttpAuthHandler::AwHttpAuthHandler(AwLoginDelegate
* login_delegate
,
22 net::AuthChallengeInfo
* auth_info
,
23 bool first_auth_attempt
)
24 : login_delegate_(login_delegate
),
25 host_(auth_info
->challenger
.host()),
26 realm_(auth_info
->realm
) {
27 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
28 JNIEnv
* env
= base::android::AttachCurrentThread();
29 http_auth_handler_
.Reset(
30 Java_AwHttpAuthHandler_create(
31 env
, reinterpret_cast<intptr_t>(this), first_auth_attempt
));
34 AwHttpAuthHandler:: ~AwHttpAuthHandler() {
35 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
36 Java_AwHttpAuthHandler_handlerDestroyed(base::android::AttachCurrentThread(),
37 http_auth_handler_
.obj());
40 void AwHttpAuthHandler::Proceed(JNIEnv
* env
,
44 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
45 if (login_delegate_
.get()) {
46 login_delegate_
->Proceed(ConvertJavaStringToUTF16(env
, user
),
47 ConvertJavaStringToUTF16(env
, password
));
48 login_delegate_
= NULL
;
52 void AwHttpAuthHandler::Cancel(JNIEnv
* env
, jobject obj
) {
53 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
54 if (login_delegate_
.get()) {
55 login_delegate_
->Cancel();
56 login_delegate_
= NULL
;
60 bool AwHttpAuthHandler::HandleOnUIThread(content::WebContents
* web_contents
) {
62 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
63 AwContents
* aw_contents
= AwContents::FromWebContents(web_contents
);
65 return aw_contents
->OnReceivedHttpAuthRequest(http_auth_handler_
, host_
,
70 AwHttpAuthHandlerBase
* AwHttpAuthHandlerBase::Create(
71 AwLoginDelegate
* login_delegate
,
72 net::AuthChallengeInfo
* auth_info
,
73 bool first_auth_attempt
) {
74 return new AwHttpAuthHandler(login_delegate
, auth_info
, first_auth_attempt
);
77 bool RegisterAwHttpAuthHandler(JNIEnv
* env
) {
78 return RegisterNativesImpl(env
);
81 } // namespace android_webview