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_string.h"
8 #include "base/android/jni_weak_ref.h"
9 #include "base/command_line.h"
10 #include "base/metrics/field_trial.h"
11 #include "base/prefs/pref_service.h"
12 #include "chrome/browser/profiles/profile_manager.h"
13 #include "chrome/browser/sync/profile_sync_service.h"
14 #include "chrome/browser/sync/profile_sync_service_factory.h"
15 #include "chrome/common/pref_names.h"
16 #include "chrome/common/url_constants.h"
17 #include "components/autofill/core/common/password_form.h"
18 #include "components/password_manager/core/browser/affiliation_utils.h"
19 #include "components/password_manager/core/browser/password_bubble_experiment.h"
20 #include "components/password_manager/core/common/experiments.h"
21 #include "components/password_manager/core/common/password_manager_switches.h"
22 #include "jni/PasswordUIView_jni.h"
24 using base::android::ConvertUTF16ToJavaString
;
25 using base::android::ConvertUTF8ToJavaString
;
26 using base::android::ScopedJavaLocalRef
;
28 PasswordUIViewAndroid::PasswordUIViewAndroid(JNIEnv
* env
, jobject obj
)
29 : password_manager_presenter_(this), weak_java_ui_controller_(env
, obj
) {}
31 PasswordUIViewAndroid::~PasswordUIViewAndroid() {}
33 void PasswordUIViewAndroid::Destroy(JNIEnv
*, jobject
) { delete this; }
35 Profile
* PasswordUIViewAndroid::GetProfile() {
36 return ProfileManager::GetLastUsedProfile();
39 void PasswordUIViewAndroid::ShowPassword(
41 const std::string
& origin_url
,
42 const std::string
& username
,
43 const base::string16
& password_value
) {
47 void PasswordUIViewAndroid::SetPasswordList(
48 const ScopedVector
<autofill::PasswordForm
>& password_list
,
49 bool show_passwords
) {
50 // Android just ignores the |show_passwords| argument.
51 JNIEnv
* env
= base::android::AttachCurrentThread();
52 ScopedJavaLocalRef
<jobject
> ui_controller
= weak_java_ui_controller_
.get(env
);
53 if (!ui_controller
.is_null()) {
54 Java_PasswordUIView_passwordListAvailable(
55 env
, ui_controller
.obj(), static_cast<int>(password_list
.size()));
59 void PasswordUIViewAndroid::SetPasswordExceptionList(
60 const ScopedVector
<autofill::PasswordForm
>& password_exception_list
) {
61 JNIEnv
* env
= base::android::AttachCurrentThread();
62 ScopedJavaLocalRef
<jobject
> ui_controller
= weak_java_ui_controller_
.get(env
);
63 if (!ui_controller
.is_null()) {
64 Java_PasswordUIView_passwordExceptionListAvailable(
67 static_cast<int>(password_exception_list
.size()));
71 void PasswordUIViewAndroid::UpdatePasswordLists(JNIEnv
* env
, jobject
) {
72 password_manager_presenter_
.UpdatePasswordLists();
75 ScopedJavaLocalRef
<jobject
>
76 PasswordUIViewAndroid::GetSavedPasswordEntry(JNIEnv
* env
, jobject
, int index
) {
77 const autofill::PasswordForm
* form
=
78 password_manager_presenter_
.GetPassword(index
);
80 return Java_PasswordUIView_createSavedPasswordEntry(
82 ConvertUTF8ToJavaString(env
, std::string()).obj(),
83 ConvertUTF16ToJavaString(env
, base::string16()).obj());
85 std::string human_readable_origin
= password_manager::GetHumanReadableOrigin(
86 *form
, GetProfile()->GetPrefs()->GetString(prefs::kAcceptLanguages
));
87 return Java_PasswordUIView_createSavedPasswordEntry(
88 env
, ConvertUTF8ToJavaString(env
, human_readable_origin
).obj(),
89 ConvertUTF16ToJavaString(env
, form
->username_value
).obj());
92 ScopedJavaLocalRef
<jstring
> PasswordUIViewAndroid::GetSavedPasswordException(
93 JNIEnv
* env
, jobject
, int index
) {
94 const autofill::PasswordForm
* form
=
95 password_manager_presenter_
.GetPasswordException(index
);
97 return ConvertUTF8ToJavaString(env
, std::string());
98 std::string human_readable_origin
= password_manager::GetHumanReadableOrigin(
99 *form
, GetProfile()->GetPrefs()->GetString(prefs::kAcceptLanguages
));
100 return ConvertUTF8ToJavaString(env
, human_readable_origin
);
103 void PasswordUIViewAndroid::HandleRemoveSavedPasswordEntry(
104 JNIEnv
* env
, jobject
, int index
) {
105 password_manager_presenter_
.RemoveSavedPassword(index
);
108 void PasswordUIViewAndroid::HandleRemoveSavedPasswordException(
109 JNIEnv
* env
, jobject
, int index
) {
110 password_manager_presenter_
.RemovePasswordException(index
);
113 jstring
GetAccountDashboardURL(JNIEnv
* env
, jclass
) {
114 return ConvertUTF8ToJavaString(
115 env
, chrome::kPasswordManagerAccountDashboardURL
).Release();
118 static jboolean
ShouldDisplayManageAccountLink(
119 JNIEnv
* env
, jclass
) {
120 return password_manager::ManageAccountLinkExperimentEnabled();
123 static jboolean
ShouldUseSmartLockBranding(
124 JNIEnv
* env
, jclass
) {
125 const ProfileSyncService
* sync_service
=
126 ProfileSyncServiceFactory::GetForProfile(
127 ProfileManager::GetLastUsedProfile());
128 return password_bubble_experiment::IsSmartLockBrandingEnabled(sync_service
);
132 static jlong
Init(JNIEnv
* env
, jobject obj
) {
133 PasswordUIViewAndroid
* controller
= new PasswordUIViewAndroid(env
, obj
);
134 return reinterpret_cast<intptr_t>(controller
);
137 bool PasswordUIViewAndroid::RegisterPasswordUIViewAndroid(JNIEnv
* env
) {
138 return RegisterNativesImpl(env
);