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 "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 jobject
ChromeHttpAuthHandler::GetJavaObject() {
43 return java_chrome_http_auth_handler_
.obj();
46 void ChromeHttpAuthHandler::OnAutofillDataAvailable(
47 const base::string16
& username
,
48 const base::string16
& password
) {
49 DCHECK(java_chrome_http_auth_handler_
.obj() != NULL
);
50 JNIEnv
* env
= base::android::AttachCurrentThread();
51 ScopedJavaLocalRef
<jstring
> j_username
=
52 ConvertUTF16ToJavaString(env
, username
);
53 ScopedJavaLocalRef
<jstring
> j_password
=
54 ConvertUTF16ToJavaString(env
, password
);
55 Java_ChromeHttpAuthHandler_onAutofillDataAvailable(
56 env
, java_chrome_http_auth_handler_
.obj(),
57 j_username
.obj(), j_password
.obj());
60 void ChromeHttpAuthHandler::SetAuth(JNIEnv
* env
,
65 base::string16 username16
= ConvertJavaStringToUTF16(env
, username
);
66 base::string16 password16
= ConvertJavaStringToUTF16(env
, password
);
67 observer_
->SetAuth(username16
, password16
);
71 void ChromeHttpAuthHandler::CancelAuth(JNIEnv
* env
, jobject
) {
73 observer_
->CancelAuth();
76 ScopedJavaLocalRef
<jstring
> ChromeHttpAuthHandler::GetMessageTitle(
77 JNIEnv
* env
, jobject
) {
78 return ConvertUTF16ToJavaString(env
,
79 l10n_util::GetStringUTF16(IDS_LOGIN_DIALOG_TITLE
));
82 ScopedJavaLocalRef
<jstring
> ChromeHttpAuthHandler::GetMessageBody(
83 JNIEnv
* env
, jobject
) {
84 return ConvertUTF16ToJavaString(env
, explanation_
);
87 ScopedJavaLocalRef
<jstring
> ChromeHttpAuthHandler::GetUsernameLabelText(
88 JNIEnv
* env
, jobject
) {
89 return ConvertUTF16ToJavaString(env
,
90 l10n_util::GetStringUTF16(IDS_LOGIN_DIALOG_USERNAME_FIELD
));
93 ScopedJavaLocalRef
<jstring
> ChromeHttpAuthHandler::GetPasswordLabelText(
94 JNIEnv
* env
, jobject
) {
95 return ConvertUTF16ToJavaString(env
,
96 l10n_util::GetStringUTF16(IDS_LOGIN_DIALOG_PASSWORD_FIELD
));
99 ScopedJavaLocalRef
<jstring
> ChromeHttpAuthHandler::GetOkButtonText(
100 JNIEnv
* env
, jobject
) {
101 return ConvertUTF16ToJavaString(env
,
102 l10n_util::GetStringUTF16(IDS_LOGIN_DIALOG_OK_BUTTON_LABEL
));
105 ScopedJavaLocalRef
<jstring
> ChromeHttpAuthHandler::GetCancelButtonText(
106 JNIEnv
* env
, jobject
) {
107 return ConvertUTF16ToJavaString(env
, l10n_util::GetStringUTF16(IDS_CANCEL
));
111 bool ChromeHttpAuthHandler::RegisterChromeHttpAuthHandler(JNIEnv
* env
) {
112 return RegisterNativesImpl(env
);