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/autofill/android/personal_data_manager_android.h"
7 #include "base/android/jni_string.h"
8 #include "base/format_macros.h"
9 #include "base/prefs/pref_service.h"
10 #include "base/strings/stringprintf.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "chrome/browser/autofill/personal_data_manager_factory.h"
13 #include "chrome/browser/browser_process.h"
14 #include "chrome/browser/profiles/profile_manager.h"
15 #include "chrome/common/pref_names.h"
16 #include "components/autofill/core/browser/autofill_country.h"
17 #include "components/autofill/core/browser/autofill_type.h"
18 #include "components/autofill/core/browser/field_types.h"
19 #include "components/autofill/core/browser/personal_data_manager.h"
20 #include "jni/PersonalDataManager_jni.h"
22 using base::android::ConvertJavaStringToUTF8
;
23 using base::android::ConvertUTF16ToJavaString
;
24 using base::android::ConvertUTF8ToJavaString
;
25 using base::android::ScopedJavaLocalRef
;
30 PrefService
* GetPrefs() {
32 ProfileManager::GetActiveUserProfile()->GetOriginalProfile()->GetPrefs();
35 ScopedJavaLocalRef
<jobject
> CreateJavaProfileFromNative(
37 const AutofillProfile
& profile
) {
38 return Java_AutofillProfile_create(
40 ConvertUTF8ToJavaString(env
, profile
.guid()).obj(),
41 ConvertUTF8ToJavaString(env
, profile
.origin()).obj(),
42 ConvertUTF16ToJavaString(env
, profile
.GetRawInfo(NAME_FULL
)).obj(),
43 ConvertUTF16ToJavaString(env
, profile
.GetRawInfo(COMPANY_NAME
)).obj(),
44 ConvertUTF16ToJavaString(
46 profile
.GetRawInfo(ADDRESS_HOME_LINE1
)).obj(),
47 ConvertUTF16ToJavaString(
49 profile
.GetRawInfo(ADDRESS_HOME_LINE2
)).obj(),
50 ConvertUTF16ToJavaString(
52 profile
.GetRawInfo(ADDRESS_HOME_CITY
)).obj(),
53 ConvertUTF16ToJavaString(
55 profile
.GetRawInfo(ADDRESS_HOME_STATE
)).obj(),
56 ConvertUTF16ToJavaString(
58 profile
.GetRawInfo(ADDRESS_HOME_ZIP
)).obj(),
59 ConvertUTF16ToJavaString(
61 profile
.GetInfo(AutofillType(ADDRESS_HOME_COUNTRY
),
62 g_browser_process
->GetApplicationLocale())).obj(),
63 ConvertUTF16ToJavaString(
65 profile
.GetRawInfo(PHONE_HOME_WHOLE_NUMBER
)).obj(),
66 ConvertUTF16ToJavaString(env
, profile
.GetRawInfo(EMAIL_ADDRESS
)).obj());
69 void PopulateNativeProfileFromJava(
70 const jobject
& jprofile
,
72 AutofillProfile
* profile
) {
74 ConvertJavaStringToUTF8(
75 Java_AutofillProfile_getOrigin(env
, jprofile
)));
78 ConvertJavaStringToUTF16(
79 Java_AutofillProfile_getFullName(env
, jprofile
)));
82 ConvertJavaStringToUTF16(
83 Java_AutofillProfile_getCompanyName(env
, jprofile
)));
86 ConvertJavaStringToUTF16(
87 Java_AutofillProfile_getAddressLine1(env
, jprofile
)));
90 ConvertJavaStringToUTF16(
91 Java_AutofillProfile_getAddressLine2(env
, jprofile
)));
94 ConvertJavaStringToUTF16(
95 Java_AutofillProfile_getCity(env
, jprofile
)));
98 ConvertJavaStringToUTF16(
99 Java_AutofillProfile_getState(env
, jprofile
)));
102 ConvertJavaStringToUTF16(
103 Java_AutofillProfile_getZip(env
, jprofile
)));
105 AutofillType(ADDRESS_HOME_COUNTRY
),
106 ConvertJavaStringToUTF16(
107 Java_AutofillProfile_getCountry(env
, jprofile
)),
108 g_browser_process
->GetApplicationLocale());
110 PHONE_HOME_WHOLE_NUMBER
,
111 ConvertJavaStringToUTF16(
112 Java_AutofillProfile_getPhoneNumber(env
, jprofile
)));
115 ConvertJavaStringToUTF16(
116 Java_AutofillProfile_getEmailAddress(env
, jprofile
)));
119 ScopedJavaLocalRef
<jobject
> CreateJavaCreditCardFromNative(
121 const CreditCard
& card
) {
122 return Java_CreditCard_create(
124 ConvertUTF8ToJavaString(env
, card
.guid()).obj(),
125 ConvertUTF8ToJavaString(env
, card
.origin()).obj(),
126 ConvertUTF16ToJavaString(env
, card
.GetRawInfo(CREDIT_CARD_NAME
)).obj(),
127 ConvertUTF16ToJavaString(env
, card
.GetRawInfo(CREDIT_CARD_NUMBER
)).obj(),
128 ConvertUTF16ToJavaString(env
, card
.ObfuscatedNumber()).obj(),
129 ConvertUTF16ToJavaString(
131 card
.GetRawInfo(CREDIT_CARD_EXP_MONTH
)).obj(),
132 ConvertUTF16ToJavaString(
134 card
.GetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR
)).obj());
137 void PopulateNativeCreditCardFromJava(
138 const jobject
& jcard
,
142 ConvertJavaStringToUTF8(Java_CreditCard_getOrigin(env
, jcard
)));
145 ConvertJavaStringToUTF16(Java_CreditCard_getName(env
, jcard
)));
148 ConvertJavaStringToUTF16(Java_CreditCard_getNumber(env
, jcard
)));
150 CREDIT_CARD_EXP_MONTH
,
151 ConvertJavaStringToUTF16(Java_CreditCard_getMonth(env
, jcard
)));
153 CREDIT_CARD_EXP_4_DIGIT_YEAR
,
154 ConvertJavaStringToUTF16(Java_CreditCard_getYear(env
, jcard
)));
159 PersonalDataManagerAndroid::PersonalDataManagerAndroid(JNIEnv
* env
,
161 : weak_java_obj_(env
, obj
),
162 personal_data_manager_(PersonalDataManagerFactory::GetForProfile(
163 ProfileManager::GetActiveUserProfile())) {
164 personal_data_manager_
->AddObserver(this);
167 PersonalDataManagerAndroid::~PersonalDataManagerAndroid() {
168 personal_data_manager_
->RemoveObserver(this);
171 jint
PersonalDataManagerAndroid::GetProfileCount(JNIEnv
* unused_env
,
172 jobject unused_obj
) {
173 return personal_data_manager_
->GetProfiles().size();
176 ScopedJavaLocalRef
<jobject
> PersonalDataManagerAndroid::GetProfileByIndex(
180 const std::vector
<AutofillProfile
*>& profiles
=
181 personal_data_manager_
->GetProfiles();
182 size_t index_size_t
= static_cast<size_t>(index
);
183 DCHECK_LT(index_size_t
, profiles
.size());
184 return CreateJavaProfileFromNative(env
, *profiles
[index_size_t
]);
187 ScopedJavaLocalRef
<jobject
> PersonalDataManagerAndroid::GetProfileByGUID(
191 AutofillProfile
* profile
= personal_data_manager_
->GetProfileByGUID(
192 ConvertJavaStringToUTF8(env
, jguid
));
194 return ScopedJavaLocalRef
<jobject
>();
196 return CreateJavaProfileFromNative(env
, *profile
);
199 ScopedJavaLocalRef
<jstring
> PersonalDataManagerAndroid::SetProfile(
203 std::string guid
= ConvertJavaStringToUTF8(
205 Java_AutofillProfile_getGUID(env
, jprofile
).obj());
207 AutofillProfile profile
;
208 PopulateNativeProfileFromJava(jprofile
, env
, &profile
);
211 personal_data_manager_
->AddProfile(profile
);
213 profile
.set_guid(guid
);
214 personal_data_manager_
->UpdateProfile(profile
);
217 return ConvertUTF8ToJavaString(env
, profile
.guid());
220 jint
PersonalDataManagerAndroid::GetCreditCardCount(JNIEnv
* unused_env
,
221 jobject unused_obj
) {
222 return personal_data_manager_
->GetCreditCards().size();
225 ScopedJavaLocalRef
<jobject
> PersonalDataManagerAndroid::GetCreditCardByIndex(
229 const std::vector
<CreditCard
*>& credit_cards
=
230 personal_data_manager_
->GetCreditCards();
231 size_t index_size_t
= static_cast<size_t>(index
);
232 DCHECK_LT(index_size_t
, credit_cards
.size());
233 return CreateJavaCreditCardFromNative(env
, *credit_cards
[index_size_t
]);
236 ScopedJavaLocalRef
<jobject
> PersonalDataManagerAndroid::GetCreditCardByGUID(
240 CreditCard
* card
= personal_data_manager_
->GetCreditCardByGUID(
241 ConvertJavaStringToUTF8(env
, jguid
));
243 return ScopedJavaLocalRef
<jobject
>();
245 return CreateJavaCreditCardFromNative(env
, *card
);
248 ScopedJavaLocalRef
<jstring
> PersonalDataManagerAndroid::SetCreditCard(
252 std::string guid
= ConvertJavaStringToUTF8(
254 Java_CreditCard_getGUID(env
, jcard
).obj());
257 PopulateNativeCreditCardFromJava(jcard
, env
, &card
);
260 personal_data_manager_
->AddCreditCard(card
);
263 personal_data_manager_
->UpdateCreditCard(card
);
265 return ConvertUTF8ToJavaString(env
, card
.guid());
268 void PersonalDataManagerAndroid::RemoveByGUID(JNIEnv
* env
,
271 personal_data_manager_
->RemoveByGUID(ConvertJavaStringToUTF8(env
, jguid
));
274 void PersonalDataManagerAndroid::OnPersonalDataChanged() {
275 JNIEnv
* env
= base::android::AttachCurrentThread();
276 if (weak_java_obj_
.get(env
).is_null())
279 Java_PersonalDataManager_personalDataChanged(env
,
280 weak_java_obj_
.get(env
).obj());
284 bool PersonalDataManagerAndroid::Register(JNIEnv
* env
) {
285 return RegisterNativesImpl(env
);
288 // Returns whether the Autofill feature is enabled.
289 static jboolean
IsAutofillEnabled(JNIEnv
* env
, jclass clazz
) {
290 return GetPrefs()->GetBoolean(autofill::prefs::kAutofillEnabled
);
293 // Enables or disables the Autofill feature.
294 static void SetAutofillEnabled(JNIEnv
* env
, jclass clazz
, jboolean enable
) {
295 GetPrefs()->SetBoolean(autofill::prefs::kAutofillEnabled
, enable
);
298 // Returns whether Autofill feature is managed.
299 static jboolean
IsAutofillManaged(JNIEnv
* env
, jclass clazz
) {
300 return GetPrefs()->IsManagedPreference(autofill::prefs::kAutofillEnabled
);
303 // Returns an ISO 3166-1-alpha-2 country code for a |jcountry_name| using
304 // the application locale, or an empty string.
305 static jstring
ToCountryCode(JNIEnv
* env
, jclass clazz
, jstring jcountry_name
) {
306 return ConvertUTF8ToJavaString(
308 AutofillCountry::GetCountryCode(
309 base::android::ConvertJavaStringToUTF16(env
, jcountry_name
),
310 g_browser_process
->GetApplicationLocale())).Release();
313 static jlong
Init(JNIEnv
* env
, jobject obj
) {
314 PersonalDataManagerAndroid
* personal_data_manager_android
=
315 new PersonalDataManagerAndroid(env
, obj
);
316 return reinterpret_cast<intptr_t>(personal_data_manager_android
);
319 } // namespace autofill