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/android/chrome_http_auth_handler.h"
9 #include "base/android/jni_android.h"
10 #include "base/android/jni_string.h"
11 #include "base/android/scoped_java_ref.h"
12 #include "base/logging.h"
13 #include "base/strings/string16.h"
14 #include "chrome/grit/generated_resources.h"
15 #include "jni/ChromeHttpAuthHandler_jni.h"
16 #include "ui/base/l10n/l10n_util.h"
18 using base::android::AttachCurrentThread
;
19 using base::android::CheckException
;
20 using base::android::ConvertJavaStringToUTF16
;
21 using base::android::ConvertUTF16ToJavaString
;
22 using base::android::ScopedJavaLocalRef
;
24 ChromeHttpAuthHandler::ChromeHttpAuthHandler(const base::string16
& explanation
)
26 explanation_(explanation
) {
29 ChromeHttpAuthHandler::~ChromeHttpAuthHandler() {}
31 void ChromeHttpAuthHandler::Init() {
32 DCHECK(java_chrome_http_auth_handler_
.is_null());
33 JNIEnv
* env
= AttachCurrentThread();
34 java_chrome_http_auth_handler_
.Reset(
35 Java_ChromeHttpAuthHandler_create(env
, reinterpret_cast<intptr_t>(this)));
38 void ChromeHttpAuthHandler::SetObserver(LoginHandler
* observer
) {
42 void ChromeHttpAuthHandler::ShowDialog(jobject window_android
) {
43 JNIEnv
* env
= AttachCurrentThread();
44 Java_ChromeHttpAuthHandler_showDialog(
46 java_chrome_http_auth_handler_
.obj(),
50 void ChromeHttpAuthHandler::OnAutofillDataAvailable(
51 const base::string16
& username
,
52 const base::string16
& password
) {
53 DCHECK(java_chrome_http_auth_handler_
.obj() != NULL
);
54 JNIEnv
* env
= base::android::AttachCurrentThread();
55 ScopedJavaLocalRef
<jstring
> j_username
=
56 ConvertUTF16ToJavaString(env
, username
);
57 ScopedJavaLocalRef
<jstring
> j_password
=
58 ConvertUTF16ToJavaString(env
, password
);
59 Java_ChromeHttpAuthHandler_onAutofillDataAvailable(
60 env
, java_chrome_http_auth_handler_
.obj(),
61 j_username
.obj(), j_password
.obj());
64 void ChromeHttpAuthHandler::SetAuth(JNIEnv
* env
,
69 base::string16 username16
= ConvertJavaStringToUTF16(env
, username
);
70 base::string16 password16
= ConvertJavaStringToUTF16(env
, password
);
71 observer_
->SetAuth(username16
, password16
);
75 void ChromeHttpAuthHandler::CancelAuth(JNIEnv
* env
, jobject
) {
77 observer_
->CancelAuth();
80 ScopedJavaLocalRef
<jstring
> ChromeHttpAuthHandler::GetMessageTitle(
81 JNIEnv
* env
, jobject
) {
82 return ConvertUTF16ToJavaString(env
,
83 l10n_util::GetStringUTF16(IDS_LOGIN_DIALOG_TITLE
));
86 ScopedJavaLocalRef
<jstring
> ChromeHttpAuthHandler::GetMessageBody(
87 JNIEnv
* env
, jobject
) {
88 return ConvertUTF16ToJavaString(env
, explanation_
);
91 ScopedJavaLocalRef
<jstring
> ChromeHttpAuthHandler::GetUsernameLabelText(
92 JNIEnv
* env
, jobject
) {
93 return ConvertUTF16ToJavaString(env
,
94 l10n_util::GetStringUTF16(IDS_LOGIN_DIALOG_USERNAME_FIELD
));
97 ScopedJavaLocalRef
<jstring
> ChromeHttpAuthHandler::GetPasswordLabelText(
98 JNIEnv
* env
, jobject
) {
99 return ConvertUTF16ToJavaString(env
,
100 l10n_util::GetStringUTF16(IDS_LOGIN_DIALOG_PASSWORD_FIELD
));
103 ScopedJavaLocalRef
<jstring
> ChromeHttpAuthHandler::GetOkButtonText(
104 JNIEnv
* env
, jobject
) {
105 return ConvertUTF16ToJavaString(env
,
106 l10n_util::GetStringUTF16(IDS_LOGIN_DIALOG_OK_BUTTON_LABEL
));
109 ScopedJavaLocalRef
<jstring
> ChromeHttpAuthHandler::GetCancelButtonText(
110 JNIEnv
* env
, jobject
) {
111 return ConvertUTF16ToJavaString(env
, l10n_util::GetStringUTF16(IDS_CANCEL
));
115 bool ChromeHttpAuthHandler::RegisterChromeHttpAuthHandler(JNIEnv
* env
) {
116 return RegisterNativesImpl(env
);