1 // Copyright 2013 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/android/password_ui_view_android.h"
7 #include "base/android/jni_helper.h"
8 #include "base/android/jni_string.h"
9 #include "chrome/browser/profiles/profile_manager.h"
10 #include "components/autofill/core/common/password_form.h"
11 #include "jni/PasswordUIView_jni.h"
13 using base::android::ConvertUTF16ToJavaString
;
14 using base::android::ConvertUTF8ToJavaString
;
15 using base::android::ScopedJavaLocalRef
;
17 PasswordUIViewAndroid::PasswordUIViewAndroid(JNIEnv
* env
, jobject obj
)
18 : password_manager_presenter_(this), weak_java_ui_controller_(env
, obj
) {}
20 PasswordUIViewAndroid::~PasswordUIViewAndroid() {}
22 void PasswordUIViewAndroid::Destroy(JNIEnv
*, jobject
) { delete this; }
24 Profile
* PasswordUIViewAndroid::GetProfile() {
25 return ProfileManager::GetLastUsedProfile();
28 void PasswordUIViewAndroid::ShowPassword(
29 size_t index
, const base::string16
& password_value
) {
33 void PasswordUIViewAndroid::SetPasswordList(
34 const ScopedVector
<autofill::PasswordForm
>& password_list
,
35 bool show_passwords
) {
36 // Android just ignores the |show_passwords| argument.
37 JNIEnv
* env
= base::android::AttachCurrentThread();
38 ScopedJavaLocalRef
<jobject
> ui_controller
= weak_java_ui_controller_
.get(env
);
39 if (!ui_controller
.is_null()) {
40 Java_PasswordUIView_passwordListAvailable(
41 env
, ui_controller
.obj(), static_cast<int>(password_list
.size()));
45 void PasswordUIViewAndroid::SetPasswordExceptionList(
46 const ScopedVector
<autofill::PasswordForm
>& password_exception_list
) {
47 JNIEnv
* env
= base::android::AttachCurrentThread();
48 ScopedJavaLocalRef
<jobject
> ui_controller
= weak_java_ui_controller_
.get(env
);
49 if (!ui_controller
.is_null()) {
50 Java_PasswordUIView_passwordExceptionListAvailable(
53 static_cast<int>(password_exception_list
.size()));
57 void PasswordUIViewAndroid::UpdatePasswordLists(JNIEnv
* env
, jobject
) {
58 password_manager_presenter_
.UpdatePasswordLists();
61 ScopedJavaLocalRef
<jobject
>
62 PasswordUIViewAndroid::GetSavedPasswordEntry(JNIEnv
* env
, jobject
, int index
) {
63 const autofill::PasswordForm
& form
=
64 password_manager_presenter_
.GetPassword(index
);
65 return Java_PasswordUIView_createSavedPasswordEntry(
67 ConvertUTF8ToJavaString(env
, form
.origin
.spec()).obj(),
68 ConvertUTF16ToJavaString(env
, form
.username_value
).obj());
71 ScopedJavaLocalRef
<jstring
> PasswordUIViewAndroid::GetSavedPasswordException(
72 JNIEnv
* env
, jobject
, int index
) {
73 const autofill::PasswordForm
& form
=
74 password_manager_presenter_
.GetPasswordException(index
);
75 return ConvertUTF8ToJavaString(env
, form
.origin
.spec());
78 void PasswordUIViewAndroid::HandleRemoveSavedPasswordEntry(
79 JNIEnv
* env
, jobject
, int index
) {
80 password_manager_presenter_
.RemoveSavedPassword(index
);
83 void PasswordUIViewAndroid::HandleRemoveSavedPasswordException(
84 JNIEnv
* env
, jobject
, int index
) {
85 password_manager_presenter_
.RemovePasswordException(index
);
89 static jlong
Init(JNIEnv
* env
, jobject obj
) {
90 PasswordUIViewAndroid
* controller
= new PasswordUIViewAndroid(env
, obj
);
91 return reinterpret_cast<intptr_t>(controller
);
94 bool PasswordUIViewAndroid::RegisterPasswordUIViewAndroid(JNIEnv
* env
) {
95 return RegisterNativesImpl(env
);