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 "xwalk/runtime/browser/android/xwalk_http_auth_handler.h"
7 #include "xwalk/runtime/browser/android/xwalk_content.h"
8 #include "xwalk/runtime/browser/android/xwalk_contents_client_bridge.h"
9 #include "xwalk/runtime/browser/android/xwalk_login_delegate.h"
10 #include "base/android/jni_android.h"
11 #include "base/android/jni_string.h"
12 #include "content/public/browser/browser_thread.h"
13 #include "jni/XWalkHttpAuthHandler_jni.h"
14 #include "net/base/auth.h"
15 #include "content/public/browser/web_contents.h"
17 using base::android::ConvertJavaStringToUTF16;
21 XWalkHttpAuthHandler::XWalkHttpAuthHandler(XWalkLoginDelegate* 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(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
28 JNIEnv* env = base::android::AttachCurrentThread();
29 http_auth_handler_.Reset(
30 Java_XWalkHttpAuthHandler_create(
31 env, reinterpret_cast<intptr_t>(this), first_auth_attempt));
34 XWalkHttpAuthHandler:: ~XWalkHttpAuthHandler() {
35 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
36 Java_XWalkHttpAuthHandler_handlerDestroyed(
37 base::android::AttachCurrentThread(),
38 http_auth_handler_.obj());
41 void XWalkHttpAuthHandler::Proceed(JNIEnv* env,
45 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
46 if (login_delegate_.get()) {
47 login_delegate_->Proceed(ConvertJavaStringToUTF16(env, user),
48 ConvertJavaStringToUTF16(env, password));
49 login_delegate_ = NULL;
53 void XWalkHttpAuthHandler::Cancel(JNIEnv* env, jobject obj) {
54 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
55 if (login_delegate_.get()) {
56 login_delegate_->Cancel();
57 login_delegate_ = NULL;
61 bool XWalkHttpAuthHandler::HandleOnUIThread(
62 content::WebContents* web_contents) {
64 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
65 XWalkContent* xwalk_content = XWalkContent::FromWebContents(web_contents);
66 return xwalk_content->GetContentsClientBridge()->OnReceivedHttpAuthRequest(
67 http_auth_handler_, host_, realm_);
71 XWalkHttpAuthHandlerBase* XWalkHttpAuthHandlerBase::Create(
72 XWalkLoginDelegate* login_delegate,
73 net::AuthChallengeInfo* auth_info,
74 bool first_auth_attempt) {
75 return new XWalkHttpAuthHandler(login_delegate, auth_info,
79 bool RegisterXWalkHttpAuthHandler(JNIEnv* env) {
80 return RegisterNativesImpl(env);