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 "android_webview/native/aw_http_auth_handler.h"
6
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"
15
16using base::android::ConvertJavaStringToUTF16;
17
18namespace android_webview {
19
20AwHttpAuthHandler::AwHttpAuthHandler(AwLoginDelegate* login_delegate,
21                                     net::AuthChallengeInfo* auth_info,
22                                     bool first_auth_attempt)
23    : login_delegate_(login_delegate),
24      host_(auth_info->challenger.host()),
25      realm_(auth_info->realm) {
26  DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
27  JNIEnv* env = base::android::AttachCurrentThread();
28  http_auth_handler_.Reset(
29      Java_AwHttpAuthHandler_create(
30          env, reinterpret_cast<intptr_t>(this), first_auth_attempt));
31}
32
33AwHttpAuthHandler:: ~AwHttpAuthHandler() {
34  DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
35  Java_AwHttpAuthHandler_handlerDestroyed(base::android::AttachCurrentThread(),
36                                          http_auth_handler_.obj());
37}
38
39void AwHttpAuthHandler::Proceed(JNIEnv* env,
40                                jobject obj,
41                                jstring user,
42                                jstring password) {
43  DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
44  if (login_delegate_.get()) {
45    login_delegate_->Proceed(ConvertJavaStringToUTF16(env, user),
46                             ConvertJavaStringToUTF16(env, password));
47    login_delegate_ = NULL;
48  }
49}
50
51void AwHttpAuthHandler::Cancel(JNIEnv* env, jobject obj) {
52  DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
53  if (login_delegate_.get()) {
54    login_delegate_->Cancel();
55    login_delegate_ = NULL;
56  }
57}
58
59bool AwHttpAuthHandler::HandleOnUIThread(content::WebContents* web_contents) {
60  DCHECK(web_contents);
61  DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
62  AwContents* aw_contents = AwContents::FromWebContents(web_contents);
63
64  return aw_contents->OnReceivedHttpAuthRequest(http_auth_handler_, host_,
65                                                realm_);
66}
67
68// static
69AwHttpAuthHandlerBase* AwHttpAuthHandlerBase::Create(
70    AwLoginDelegate* login_delegate,
71    net::AuthChallengeInfo* auth_info,
72    bool first_auth_attempt) {
73  return new AwHttpAuthHandler(login_delegate, auth_info, first_auth_attempt);
74}
75
76bool RegisterAwHttpAuthHandler(JNIEnv* env) {
77  return RegisterNativesImpl(env);
78}
79
80}  // namespace android_webview
81