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_STREET_ADDRESS
)).obj(),
47 ConvertUTF16ToJavaString(
49 profile
.GetRawInfo(ADDRESS_HOME_STATE
)).obj(),
50 ConvertUTF16ToJavaString(
52 profile
.GetRawInfo(ADDRESS_HOME_CITY
)).obj(),
53 ConvertUTF16ToJavaString(
55 profile
.GetRawInfo(ADDRESS_HOME_DEPENDENT_LOCALITY
)).obj(),
56 ConvertUTF16ToJavaString(
58 profile
.GetRawInfo(ADDRESS_HOME_ZIP
)).obj(),
59 ConvertUTF16ToJavaString(
61 profile
.GetRawInfo(ADDRESS_HOME_SORTING_CODE
)).obj(),
62 ConvertUTF16ToJavaString(
64 profile
.GetInfo(AutofillType(ADDRESS_HOME_COUNTRY
),
65 g_browser_process
->GetApplicationLocale())).obj(),
66 ConvertUTF16ToJavaString(
68 profile
.GetRawInfo(PHONE_HOME_WHOLE_NUMBER
)).obj(),
69 ConvertUTF16ToJavaString(env
, profile
.GetRawInfo(EMAIL_ADDRESS
)).obj(),
70 ConvertUTF8ToJavaString(env
, profile
.language_code()).obj());
73 void PopulateNativeProfileFromJava(
74 const jobject
& jprofile
,
76 AutofillProfile
* profile
) {
78 ConvertJavaStringToUTF8(
79 Java_AutofillProfile_getOrigin(env
, jprofile
)));
82 ConvertJavaStringToUTF16(
83 Java_AutofillProfile_getFullName(env
, jprofile
)));
86 ConvertJavaStringToUTF16(
87 Java_AutofillProfile_getCompanyName(env
, jprofile
)));
89 ADDRESS_HOME_STREET_ADDRESS
,
90 ConvertJavaStringToUTF16(
91 Java_AutofillProfile_getStreetAddress(env
, jprofile
)));
94 ConvertJavaStringToUTF16(
95 Java_AutofillProfile_getRegion(env
, jprofile
)));
98 ConvertJavaStringToUTF16(
99 Java_AutofillProfile_getLocality(env
, jprofile
)));
101 ADDRESS_HOME_DEPENDENT_LOCALITY
,
102 ConvertJavaStringToUTF16(
103 Java_AutofillProfile_getDependentLocality(env
, jprofile
)));
106 ConvertJavaStringToUTF16(
107 Java_AutofillProfile_getPostalCode(env
, jprofile
)));
109 ADDRESS_HOME_SORTING_CODE
,
110 ConvertJavaStringToUTF16(
111 Java_AutofillProfile_getSortingCode(env
, jprofile
)));
113 AutofillType(ADDRESS_HOME_COUNTRY
),
114 ConvertJavaStringToUTF16(
115 Java_AutofillProfile_getCountry(env
, jprofile
)),
116 g_browser_process
->GetApplicationLocale());
118 PHONE_HOME_WHOLE_NUMBER
,
119 ConvertJavaStringToUTF16(
120 Java_AutofillProfile_getPhoneNumber(env
, jprofile
)));
123 ConvertJavaStringToUTF16(
124 Java_AutofillProfile_getEmailAddress(env
, jprofile
)));
125 profile
->set_language_code(
126 ConvertJavaStringToUTF8(
127 Java_AutofillProfile_getLanguageCode(env
, jprofile
)));
130 ScopedJavaLocalRef
<jobject
> CreateJavaCreditCardFromNative(
132 const CreditCard
& card
) {
133 return Java_CreditCard_create(
135 ConvertUTF8ToJavaString(env
, card
.guid()).obj(),
136 ConvertUTF8ToJavaString(env
, card
.origin()).obj(),
137 ConvertUTF16ToJavaString(env
, card
.GetRawInfo(CREDIT_CARD_NAME
)).obj(),
138 ConvertUTF16ToJavaString(env
, card
.GetRawInfo(CREDIT_CARD_NUMBER
)).obj(),
139 ConvertUTF16ToJavaString(env
, card
.ObfuscatedNumber()).obj(),
140 ConvertUTF16ToJavaString(
142 card
.GetRawInfo(CREDIT_CARD_EXP_MONTH
)).obj(),
143 ConvertUTF16ToJavaString(
145 card
.GetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR
)).obj());
148 void PopulateNativeCreditCardFromJava(
149 const jobject
& jcard
,
153 ConvertJavaStringToUTF8(Java_CreditCard_getOrigin(env
, jcard
)));
156 ConvertJavaStringToUTF16(Java_CreditCard_getName(env
, jcard
)));
159 ConvertJavaStringToUTF16(Java_CreditCard_getNumber(env
, jcard
)));
161 CREDIT_CARD_EXP_MONTH
,
162 ConvertJavaStringToUTF16(Java_CreditCard_getMonth(env
, jcard
)));
164 CREDIT_CARD_EXP_4_DIGIT_YEAR
,
165 ConvertJavaStringToUTF16(Java_CreditCard_getYear(env
, jcard
)));
170 PersonalDataManagerAndroid::PersonalDataManagerAndroid(JNIEnv
* env
,
172 : weak_java_obj_(env
, obj
),
173 personal_data_manager_(PersonalDataManagerFactory::GetForProfile(
174 ProfileManager::GetActiveUserProfile())) {
175 personal_data_manager_
->AddObserver(this);
178 PersonalDataManagerAndroid::~PersonalDataManagerAndroid() {
179 personal_data_manager_
->RemoveObserver(this);
182 jint
PersonalDataManagerAndroid::GetProfileCount(JNIEnv
* unused_env
,
183 jobject unused_obj
) {
184 return personal_data_manager_
->GetProfiles().size();
187 ScopedJavaLocalRef
<jobject
> PersonalDataManagerAndroid::GetProfileByIndex(
191 const std::vector
<AutofillProfile
*>& profiles
=
192 personal_data_manager_
->GetProfiles();
193 size_t index_size_t
= static_cast<size_t>(index
);
194 DCHECK_LT(index_size_t
, profiles
.size());
195 return CreateJavaProfileFromNative(env
, *profiles
[index_size_t
]);
198 ScopedJavaLocalRef
<jobject
> PersonalDataManagerAndroid::GetProfileByGUID(
202 AutofillProfile
* profile
= personal_data_manager_
->GetProfileByGUID(
203 ConvertJavaStringToUTF8(env
, jguid
));
205 return ScopedJavaLocalRef
<jobject
>();
207 return CreateJavaProfileFromNative(env
, *profile
);
210 ScopedJavaLocalRef
<jstring
> PersonalDataManagerAndroid::SetProfile(
214 std::string guid
= ConvertJavaStringToUTF8(
216 Java_AutofillProfile_getGUID(env
, jprofile
).obj());
218 AutofillProfile profile
;
219 PopulateNativeProfileFromJava(jprofile
, env
, &profile
);
222 personal_data_manager_
->AddProfile(profile
);
224 profile
.set_guid(guid
);
225 personal_data_manager_
->UpdateProfile(profile
);
228 return ConvertUTF8ToJavaString(env
, profile
.guid());
231 jint
PersonalDataManagerAndroid::GetCreditCardCount(JNIEnv
* unused_env
,
232 jobject unused_obj
) {
233 return personal_data_manager_
->GetCreditCards().size();
236 ScopedJavaLocalRef
<jobject
> PersonalDataManagerAndroid::GetCreditCardByIndex(
240 const std::vector
<CreditCard
*>& credit_cards
=
241 personal_data_manager_
->GetCreditCards();
242 size_t index_size_t
= static_cast<size_t>(index
);
243 DCHECK_LT(index_size_t
, credit_cards
.size());
244 return CreateJavaCreditCardFromNative(env
, *credit_cards
[index_size_t
]);
247 ScopedJavaLocalRef
<jobject
> PersonalDataManagerAndroid::GetCreditCardByGUID(
251 CreditCard
* card
= personal_data_manager_
->GetCreditCardByGUID(
252 ConvertJavaStringToUTF8(env
, jguid
));
254 return ScopedJavaLocalRef
<jobject
>();
256 return CreateJavaCreditCardFromNative(env
, *card
);
259 ScopedJavaLocalRef
<jstring
> PersonalDataManagerAndroid::SetCreditCard(
263 std::string guid
= ConvertJavaStringToUTF8(
265 Java_CreditCard_getGUID(env
, jcard
).obj());
268 PopulateNativeCreditCardFromJava(jcard
, env
, &card
);
271 personal_data_manager_
->AddCreditCard(card
);
274 personal_data_manager_
->UpdateCreditCard(card
);
276 return ConvertUTF8ToJavaString(env
, card
.guid());
279 void PersonalDataManagerAndroid::RemoveByGUID(JNIEnv
* env
,
282 personal_data_manager_
->RemoveByGUID(ConvertJavaStringToUTF8(env
, jguid
));
285 void PersonalDataManagerAndroid::OnPersonalDataChanged() {
286 JNIEnv
* env
= base::android::AttachCurrentThread();
287 if (weak_java_obj_
.get(env
).is_null())
290 Java_PersonalDataManager_personalDataChanged(env
,
291 weak_java_obj_
.get(env
).obj());
295 bool PersonalDataManagerAndroid::Register(JNIEnv
* env
) {
296 return RegisterNativesImpl(env
);
299 // Returns whether the Autofill feature is enabled.
300 static jboolean
IsAutofillEnabled(JNIEnv
* env
, jclass clazz
) {
301 return GetPrefs()->GetBoolean(autofill::prefs::kAutofillEnabled
);
304 // Enables or disables the Autofill feature.
305 static void SetAutofillEnabled(JNIEnv
* env
, jclass clazz
, jboolean enable
) {
306 GetPrefs()->SetBoolean(autofill::prefs::kAutofillEnabled
, enable
);
309 // Returns whether Autofill feature is managed.
310 static jboolean
IsAutofillManaged(JNIEnv
* env
, jclass clazz
) {
311 return GetPrefs()->IsManagedPreference(autofill::prefs::kAutofillEnabled
);
314 // Returns an ISO 3166-1-alpha-2 country code for a |jcountry_name| using
315 // the application locale, or an empty string.
316 static jstring
ToCountryCode(JNIEnv
* env
, jclass clazz
, jstring jcountry_name
) {
317 return ConvertUTF8ToJavaString(
319 AutofillCountry::GetCountryCode(
320 base::android::ConvertJavaStringToUTF16(env
, jcountry_name
),
321 g_browser_process
->GetApplicationLocale())).Release();
324 static jlong
Init(JNIEnv
* env
, jobject obj
) {
325 PersonalDataManagerAndroid
* personal_data_manager_android
=
326 new PersonalDataManagerAndroid(env
, obj
);
327 return reinterpret_cast<intptr_t>(personal_data_manager_android
);
330 } // namespace autofill