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_array.h"
8 #include "base/android/jni_string.h"
9 #include "base/command_line.h"
10 #include "base/format_macros.h"
11 #include "base/prefs/pref_service.h"
12 #include "base/strings/stringprintf.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "chrome/browser/autofill/personal_data_manager_factory.h"
15 #include "chrome/browser/browser_process.h"
16 #include "chrome/browser/profiles/profile.h"
17 #include "chrome/browser/profiles/profile_manager.h"
18 #include "chrome/browser/sync/profile_sync_service.h"
19 #include "chrome/browser/sync/profile_sync_service_factory.h"
20 #include "chrome/common/pref_names.h"
21 #include "components/autofill/core/browser/autofill_country.h"
22 #include "components/autofill/core/browser/autofill_type.h"
23 #include "components/autofill/core/browser/field_types.h"
24 #include "components/autofill/core/browser/personal_data_manager.h"
25 #include "components/autofill/core/common/autofill_pref_names.h"
26 #include "components/autofill/core/common/autofill_switches.h"
27 #include "jni/PersonalDataManager_jni.h"
29 using base::android::ConvertJavaStringToUTF8
;
30 using base::android::ConvertUTF16ToJavaString
;
31 using base::android::ConvertUTF8ToJavaString
;
32 using base::android::ScopedJavaLocalRef
;
37 Profile
* GetProfile() {
38 return ProfileManager::GetActiveUserProfile()->GetOriginalProfile();
41 PrefService
* GetPrefs() {
42 return GetProfile()->GetPrefs();
45 ScopedJavaLocalRef
<jobject
> CreateJavaProfileFromNative(
47 const AutofillProfile
& profile
) {
48 return Java_AutofillProfile_create(
50 ConvertUTF8ToJavaString(env
, profile
.guid()).obj(),
51 ConvertUTF8ToJavaString(env
, profile
.origin()).obj(),
52 ConvertUTF16ToJavaString(env
, profile
.GetRawInfo(NAME_FULL
)).obj(),
53 ConvertUTF16ToJavaString(env
, profile
.GetRawInfo(COMPANY_NAME
)).obj(),
54 ConvertUTF16ToJavaString(
55 env
, profile
.GetRawInfo(ADDRESS_HOME_STREET_ADDRESS
)).obj(),
56 ConvertUTF16ToJavaString(env
, profile
.GetRawInfo(ADDRESS_HOME_STATE
))
58 ConvertUTF16ToJavaString(env
, profile
.GetRawInfo(ADDRESS_HOME_CITY
))
60 ConvertUTF16ToJavaString(
61 env
, profile
.GetRawInfo(ADDRESS_HOME_DEPENDENT_LOCALITY
)).obj(),
62 ConvertUTF16ToJavaString(env
, profile
.GetRawInfo(ADDRESS_HOME_ZIP
)).obj(),
63 ConvertUTF16ToJavaString(
64 env
, profile
.GetRawInfo(ADDRESS_HOME_SORTING_CODE
)).obj(),
65 ConvertUTF16ToJavaString(env
, profile
.GetRawInfo(ADDRESS_HOME_COUNTRY
))
67 ConvertUTF16ToJavaString(env
, profile
.GetRawInfo(PHONE_HOME_WHOLE_NUMBER
))
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
)));
81 AutofillType(NAME_FULL
),
82 ConvertJavaStringToUTF16(
83 Java_AutofillProfile_getFullName(env
, jprofile
)),
84 g_browser_process
->GetApplicationLocale());
86 AutofillType(COMPANY_NAME
),
87 ConvertJavaStringToUTF16(
88 Java_AutofillProfile_getCompanyName(env
, jprofile
)),
89 g_browser_process
->GetApplicationLocale());
91 AutofillType(ADDRESS_HOME_STREET_ADDRESS
),
92 ConvertJavaStringToUTF16(
93 Java_AutofillProfile_getStreetAddress(env
, jprofile
)),
94 g_browser_process
->GetApplicationLocale());
96 AutofillType(ADDRESS_HOME_STATE
),
97 ConvertJavaStringToUTF16(
98 Java_AutofillProfile_getRegion(env
, jprofile
)),
99 g_browser_process
->GetApplicationLocale());
101 AutofillType(ADDRESS_HOME_CITY
),
102 ConvertJavaStringToUTF16(
103 Java_AutofillProfile_getLocality(env
, jprofile
)),
104 g_browser_process
->GetApplicationLocale());
106 AutofillType(ADDRESS_HOME_DEPENDENT_LOCALITY
),
107 ConvertJavaStringToUTF16(
108 Java_AutofillProfile_getDependentLocality(env
, jprofile
)),
109 g_browser_process
->GetApplicationLocale());
111 AutofillType(ADDRESS_HOME_ZIP
),
112 ConvertJavaStringToUTF16(
113 Java_AutofillProfile_getPostalCode(env
, jprofile
)),
114 g_browser_process
->GetApplicationLocale());
116 AutofillType(ADDRESS_HOME_SORTING_CODE
),
117 ConvertJavaStringToUTF16(
118 Java_AutofillProfile_getSortingCode(env
, jprofile
)),
119 g_browser_process
->GetApplicationLocale());
120 profile
->SetInfo(AutofillType(ADDRESS_HOME_COUNTRY
),
121 ConvertJavaStringToUTF16(
122 Java_AutofillProfile_getCountryCode(env
, jprofile
)),
123 g_browser_process
->GetApplicationLocale());
125 AutofillType(PHONE_HOME_WHOLE_NUMBER
),
126 ConvertJavaStringToUTF16(
127 Java_AutofillProfile_getPhoneNumber(env
, jprofile
)),
128 g_browser_process
->GetApplicationLocale());
130 AutofillType(EMAIL_ADDRESS
),
131 ConvertJavaStringToUTF16(
132 Java_AutofillProfile_getEmailAddress(env
, jprofile
)),
133 g_browser_process
->GetApplicationLocale());
134 profile
->set_language_code(
135 ConvertJavaStringToUTF8(
136 Java_AutofillProfile_getLanguageCode(env
, jprofile
)));
139 ScopedJavaLocalRef
<jobject
> CreateJavaCreditCardFromNative(
141 const CreditCard
& card
) {
142 return Java_CreditCard_create(
144 ConvertUTF8ToJavaString(env
, card
.guid()).obj(),
145 ConvertUTF8ToJavaString(env
, card
.origin()).obj(),
146 ConvertUTF16ToJavaString(env
, card
.GetRawInfo(CREDIT_CARD_NAME
)).obj(),
147 ConvertUTF16ToJavaString(env
, card
.GetRawInfo(CREDIT_CARD_NUMBER
)).obj(),
148 ConvertUTF16ToJavaString(env
, card
.TypeAndLastFourDigits()).obj(),
149 ConvertUTF16ToJavaString(
151 card
.GetRawInfo(CREDIT_CARD_EXP_MONTH
)).obj(),
152 ConvertUTF16ToJavaString(
154 card
.GetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR
)).obj());
157 void PopulateNativeCreditCardFromJava(
158 const jobject
& jcard
,
162 ConvertJavaStringToUTF8(Java_CreditCard_getOrigin(env
, jcard
)));
165 ConvertJavaStringToUTF16(Java_CreditCard_getName(env
, jcard
)));
168 ConvertJavaStringToUTF16(Java_CreditCard_getNumber(env
, jcard
)));
170 CREDIT_CARD_EXP_MONTH
,
171 ConvertJavaStringToUTF16(Java_CreditCard_getMonth(env
, jcard
)));
173 CREDIT_CARD_EXP_4_DIGIT_YEAR
,
174 ConvertJavaStringToUTF16(Java_CreditCard_getYear(env
, jcard
)));
179 PersonalDataManagerAndroid::PersonalDataManagerAndroid(JNIEnv
* env
,
181 : weak_java_obj_(env
, obj
),
182 personal_data_manager_(PersonalDataManagerFactory::GetForProfile(
183 ProfileManager::GetActiveUserProfile())) {
184 personal_data_manager_
->AddObserver(this);
187 PersonalDataManagerAndroid::~PersonalDataManagerAndroid() {
188 personal_data_manager_
->RemoveObserver(this);
191 jint
PersonalDataManagerAndroid::GetProfileCount(JNIEnv
* unused_env
,
192 jobject unused_obj
) {
193 return personal_data_manager_
->web_profiles().size();
196 ScopedJavaLocalRef
<jobject
> PersonalDataManagerAndroid::GetProfileByIndex(
200 const std::vector
<AutofillProfile
*>& profiles
=
201 personal_data_manager_
->web_profiles();
202 size_t index_size_t
= static_cast<size_t>(index
);
203 DCHECK_LT(index_size_t
, profiles
.size());
204 return CreateJavaProfileFromNative(env
, *profiles
[index_size_t
]);
207 ScopedJavaLocalRef
<jobject
> PersonalDataManagerAndroid::GetProfileByGUID(
211 AutofillProfile
* profile
= personal_data_manager_
->GetProfileByGUID(
212 ConvertJavaStringToUTF8(env
, jguid
));
214 return ScopedJavaLocalRef
<jobject
>();
216 return CreateJavaProfileFromNative(env
, *profile
);
219 ScopedJavaLocalRef
<jstring
> PersonalDataManagerAndroid::SetProfile(
223 std::string guid
= ConvertJavaStringToUTF8(
225 Java_AutofillProfile_getGUID(env
, jprofile
).obj());
227 AutofillProfile profile
;
228 PopulateNativeProfileFromJava(jprofile
, env
, &profile
);
231 personal_data_manager_
->AddProfile(profile
);
233 profile
.set_guid(guid
);
234 personal_data_manager_
->UpdateProfile(profile
);
237 return ConvertUTF8ToJavaString(env
, profile
.guid());
240 ScopedJavaLocalRef
<jobjectArray
> PersonalDataManagerAndroid::GetProfileLabels(
242 jobject unused_obj
) {
243 std::vector
<base::string16
> labels
;
244 AutofillProfile::CreateInferredLabels(
245 personal_data_manager_
->web_profiles(),
249 g_browser_process
->GetApplicationLocale(),
252 return base::android::ToJavaArrayOfStrings(env
, labels
);
255 jint
PersonalDataManagerAndroid::GetCreditCardCount(JNIEnv
* unused_env
,
256 jobject unused_obj
) {
257 return personal_data_manager_
->GetLocalCreditCards().size();
260 ScopedJavaLocalRef
<jobject
> PersonalDataManagerAndroid::GetCreditCardByIndex(
264 const std::vector
<CreditCard
*>& credit_cards
=
265 personal_data_manager_
->GetLocalCreditCards();
266 size_t index_size_t
= static_cast<size_t>(index
);
267 DCHECK_LT(index_size_t
, credit_cards
.size());
268 return CreateJavaCreditCardFromNative(env
, *credit_cards
[index_size_t
]);
271 ScopedJavaLocalRef
<jobject
> PersonalDataManagerAndroid::GetCreditCardByGUID(
275 CreditCard
* card
= personal_data_manager_
->GetCreditCardByGUID(
276 ConvertJavaStringToUTF8(env
, jguid
));
278 return ScopedJavaLocalRef
<jobject
>();
280 return CreateJavaCreditCardFromNative(env
, *card
);
283 ScopedJavaLocalRef
<jstring
> PersonalDataManagerAndroid::SetCreditCard(
287 std::string guid
= ConvertJavaStringToUTF8(
289 Java_CreditCard_getGUID(env
, jcard
).obj());
292 PopulateNativeCreditCardFromJava(jcard
, env
, &card
);
295 personal_data_manager_
->AddCreditCard(card
);
298 personal_data_manager_
->UpdateCreditCard(card
);
300 return ConvertUTF8ToJavaString(env
, card
.guid());
303 void PersonalDataManagerAndroid::RemoveByGUID(JNIEnv
* env
,
306 personal_data_manager_
->RemoveByGUID(ConvertJavaStringToUTF8(env
, jguid
));
309 void PersonalDataManagerAndroid::ClearUnmaskedCache(JNIEnv
* env
,
310 jobject unused_obj
) {
311 personal_data_manager_
->ResetFullServerCards();
314 void PersonalDataManagerAndroid::OnPersonalDataChanged() {
315 JNIEnv
* env
= base::android::AttachCurrentThread();
316 if (weak_java_obj_
.get(env
).is_null())
319 Java_PersonalDataManager_personalDataChanged(env
,
320 weak_java_obj_
.get(env
).obj());
324 bool PersonalDataManagerAndroid::Register(JNIEnv
* env
) {
325 return RegisterNativesImpl(env
);
328 // Returns whether the Autofill feature is enabled.
329 static jboolean
IsAutofillEnabled(JNIEnv
* env
, jclass clazz
) {
330 return GetPrefs()->GetBoolean(autofill::prefs::kAutofillEnabled
);
333 // Enables or disables the Autofill feature.
334 static void SetAutofillEnabled(JNIEnv
* env
, jclass clazz
, jboolean enable
) {
335 GetPrefs()->SetBoolean(autofill::prefs::kAutofillEnabled
, enable
);
338 // Returns whether the Autofill feature is managed.
339 static jboolean
IsAutofillManaged(JNIEnv
* env
, jclass clazz
) {
340 return GetPrefs()->IsManagedPreference(autofill::prefs::kAutofillEnabled
);
343 // Returns whether the Wallet import feature is available.
344 static jboolean
IsWalletImportFeatureAvailable(JNIEnv
* env
, jclass clazz
) {
345 // TODO(estade): what to do in the IsManaged case?
346 ProfileSyncService
* service
=
347 ProfileSyncServiceFactory::GetInstance()->GetForProfile(GetProfile());
348 PersonalDataManager
* pdm
= PersonalDataManagerFactory::GetForProfile(
350 return service
&& service
->IsSyncEnabledAndLoggedIn() &&
351 pdm
->IsExperimentalWalletIntegrationEnabled();
354 // Returns whether the Wallet import feature is enabled.
355 static jboolean
IsWalletImportEnabled(JNIEnv
* env
, jclass clazz
) {
356 return GetPrefs()->GetBoolean(autofill::prefs::kAutofillWalletImportEnabled
);
359 // Enables or disables the Wallet import feature.
360 static void SetWalletImportEnabled(JNIEnv
* env
, jclass clazz
, jboolean enable
) {
361 GetPrefs()->SetBoolean(autofill::prefs::kAutofillWalletImportEnabled
, enable
);
364 // Returns an ISO 3166-1-alpha-2 country code for a |jcountry_name| using
365 // the application locale, or an empty string.
366 static jstring
ToCountryCode(JNIEnv
* env
, jclass clazz
, jstring jcountry_name
) {
367 return ConvertUTF8ToJavaString(
369 AutofillCountry::GetCountryCode(
370 base::android::ConvertJavaStringToUTF16(env
, jcountry_name
),
371 g_browser_process
->GetApplicationLocale())).Release();
374 static jlong
Init(JNIEnv
* env
, jobject obj
) {
375 PersonalDataManagerAndroid
* personal_data_manager_android
=
376 new PersonalDataManagerAndroid(env
, obj
);
377 return reinterpret_cast<intptr_t>(personal_data_manager_android
);
380 } // namespace autofill