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(
49 env
, ConvertUTF8ToJavaString(env
, profile
.guid()).obj(),
50 ConvertUTF8ToJavaString(env
, profile
.origin()).obj(),
51 profile
.record_type() == AutofillProfile::LOCAL_PROFILE
,
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(
143 env
, ConvertUTF8ToJavaString(env
, card
.guid()).obj(),
144 ConvertUTF8ToJavaString(env
, card
.origin()).obj(),
145 card
.record_type() == CreditCard::LOCAL_CARD
,
146 card
.record_type() == CreditCard::FULL_SERVER_CARD
,
147 ConvertUTF16ToJavaString(env
, card
.GetRawInfo(CREDIT_CARD_NAME
)).obj(),
148 ConvertUTF16ToJavaString(env
, card
.GetRawInfo(CREDIT_CARD_NUMBER
)).obj(),
149 ConvertUTF16ToJavaString(env
, card
.TypeAndLastFourDigits()).obj(),
150 ConvertUTF16ToJavaString(env
, card
.GetRawInfo(CREDIT_CARD_EXP_MONTH
))
152 ConvertUTF16ToJavaString(
153 env
, card
.GetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR
)).obj());
156 void PopulateNativeCreditCardFromJava(
157 const jobject
& jcard
,
161 ConvertJavaStringToUTF8(Java_CreditCard_getOrigin(env
, jcard
)));
164 ConvertJavaStringToUTF16(Java_CreditCard_getName(env
, jcard
)));
167 ConvertJavaStringToUTF16(Java_CreditCard_getNumber(env
, jcard
)));
169 CREDIT_CARD_EXP_MONTH
,
170 ConvertJavaStringToUTF16(Java_CreditCard_getMonth(env
, jcard
)));
172 CREDIT_CARD_EXP_4_DIGIT_YEAR
,
173 ConvertJavaStringToUTF16(Java_CreditCard_getYear(env
, jcard
)));
178 PersonalDataManagerAndroid::PersonalDataManagerAndroid(JNIEnv
* env
,
180 : weak_java_obj_(env
, obj
),
181 personal_data_manager_(PersonalDataManagerFactory::GetForProfile(
182 ProfileManager::GetActiveUserProfile())) {
183 personal_data_manager_
->AddObserver(this);
186 PersonalDataManagerAndroid::~PersonalDataManagerAndroid() {
187 personal_data_manager_
->RemoveObserver(this);
190 jint
PersonalDataManagerAndroid::GetProfileCount(JNIEnv
* unused_env
,
191 jobject unused_obj
) {
192 return personal_data_manager_
->GetProfiles().size();
195 ScopedJavaLocalRef
<jobject
> PersonalDataManagerAndroid::GetProfileByIndex(
199 const std::vector
<AutofillProfile
*>& profiles
=
200 personal_data_manager_
->GetProfiles();
201 size_t index_size_t
= static_cast<size_t>(index
);
202 DCHECK_LT(index_size_t
, profiles
.size());
203 return CreateJavaProfileFromNative(env
, *profiles
[index_size_t
]);
206 ScopedJavaLocalRef
<jobject
> PersonalDataManagerAndroid::GetProfileByGUID(
210 AutofillProfile
* profile
= personal_data_manager_
->GetProfileByGUID(
211 ConvertJavaStringToUTF8(env
, jguid
));
213 return ScopedJavaLocalRef
<jobject
>();
215 return CreateJavaProfileFromNative(env
, *profile
);
218 ScopedJavaLocalRef
<jstring
> PersonalDataManagerAndroid::SetProfile(
222 std::string guid
= ConvertJavaStringToUTF8(
224 Java_AutofillProfile_getGUID(env
, jprofile
).obj());
226 AutofillProfile profile
;
227 PopulateNativeProfileFromJava(jprofile
, env
, &profile
);
230 personal_data_manager_
->AddProfile(profile
);
232 profile
.set_guid(guid
);
233 personal_data_manager_
->UpdateProfile(profile
);
236 return ConvertUTF8ToJavaString(env
, profile
.guid());
239 ScopedJavaLocalRef
<jobjectArray
> PersonalDataManagerAndroid::GetProfileLabels(
241 jobject unused_obj
) {
242 std::vector
<base::string16
> labels
;
243 AutofillProfile::CreateInferredLabels(
244 personal_data_manager_
->GetProfiles(), NULL
, NAME_FULL
, 2,
245 g_browser_process
->GetApplicationLocale(), &labels
);
247 return base::android::ToJavaArrayOfStrings(env
, labels
);
250 jint
PersonalDataManagerAndroid::GetCreditCardCount(JNIEnv
* unused_env
,
251 jobject unused_obj
) {
252 return personal_data_manager_
->GetCreditCards().size();
255 ScopedJavaLocalRef
<jobject
> PersonalDataManagerAndroid::GetCreditCardByIndex(
259 const std::vector
<CreditCard
*>& credit_cards
=
260 personal_data_manager_
->GetCreditCards();
261 size_t index_size_t
= static_cast<size_t>(index
);
262 DCHECK_LT(index_size_t
, credit_cards
.size());
263 return CreateJavaCreditCardFromNative(env
, *credit_cards
[index_size_t
]);
266 ScopedJavaLocalRef
<jobject
> PersonalDataManagerAndroid::GetCreditCardByGUID(
270 CreditCard
* card
= personal_data_manager_
->GetCreditCardByGUID(
271 ConvertJavaStringToUTF8(env
, jguid
));
273 return ScopedJavaLocalRef
<jobject
>();
275 return CreateJavaCreditCardFromNative(env
, *card
);
278 ScopedJavaLocalRef
<jstring
> PersonalDataManagerAndroid::SetCreditCard(
282 std::string guid
= ConvertJavaStringToUTF8(
284 Java_CreditCard_getGUID(env
, jcard
).obj());
287 PopulateNativeCreditCardFromJava(jcard
, env
, &card
);
290 personal_data_manager_
->AddCreditCard(card
);
293 personal_data_manager_
->UpdateCreditCard(card
);
295 return ConvertUTF8ToJavaString(env
, card
.guid());
298 void PersonalDataManagerAndroid::RemoveByGUID(JNIEnv
* env
,
301 personal_data_manager_
->RemoveByGUID(ConvertJavaStringToUTF8(env
, jguid
));
304 void PersonalDataManagerAndroid::ClearUnmaskedCache(JNIEnv
* env
,
307 personal_data_manager_
->ResetFullServerCard(
308 ConvertJavaStringToUTF8(env
, guid
));
311 void PersonalDataManagerAndroid::OnPersonalDataChanged() {
312 JNIEnv
* env
= base::android::AttachCurrentThread();
313 if (weak_java_obj_
.get(env
).is_null())
316 Java_PersonalDataManager_personalDataChanged(env
,
317 weak_java_obj_
.get(env
).obj());
321 bool PersonalDataManagerAndroid::Register(JNIEnv
* env
) {
322 return RegisterNativesImpl(env
);
325 // Returns whether the Autofill feature is enabled.
326 static jboolean
IsAutofillEnabled(JNIEnv
* env
, jclass clazz
) {
327 return GetPrefs()->GetBoolean(autofill::prefs::kAutofillEnabled
);
330 // Enables or disables the Autofill feature.
331 static void SetAutofillEnabled(JNIEnv
* env
, jclass clazz
, jboolean enable
) {
332 GetPrefs()->SetBoolean(autofill::prefs::kAutofillEnabled
, enable
);
335 // Returns whether the Autofill feature is managed.
336 static jboolean
IsAutofillManaged(JNIEnv
* env
, jclass clazz
) {
337 return GetPrefs()->IsManagedPreference(autofill::prefs::kAutofillEnabled
);
340 // Returns whether the Wallet import feature is available.
341 static jboolean
IsWalletImportFeatureAvailable(JNIEnv
* env
, jclass clazz
) {
342 // TODO(estade): what to do in the IsManaged case?
343 ProfileSyncService
* service
=
344 ProfileSyncServiceFactory::GetInstance()->GetForProfile(GetProfile());
345 PersonalDataManager
* pdm
= PersonalDataManagerFactory::GetForProfile(
347 return service
&& service
->IsSyncEnabledAndLoggedIn() &&
348 pdm
->IsExperimentalWalletIntegrationEnabled();
351 // Returns whether the Wallet import feature is enabled.
352 static jboolean
IsWalletImportEnabled(JNIEnv
* env
, jclass clazz
) {
353 return GetPrefs()->GetBoolean(autofill::prefs::kAutofillWalletImportEnabled
);
356 // Enables or disables the Wallet import feature.
357 static void SetWalletImportEnabled(JNIEnv
* env
, jclass clazz
, jboolean enable
) {
358 GetPrefs()->SetBoolean(autofill::prefs::kAutofillWalletImportEnabled
, enable
);
361 // Returns an ISO 3166-1-alpha-2 country code for a |jcountry_name| using
362 // the application locale, or an empty string.
363 static jstring
ToCountryCode(JNIEnv
* env
, jclass clazz
, jstring jcountry_name
) {
364 return ConvertUTF8ToJavaString(
366 AutofillCountry::GetCountryCode(
367 base::android::ConvertJavaStringToUTF16(env
, jcountry_name
),
368 g_browser_process
->GetApplicationLocale())).Release();
371 static jlong
Init(JNIEnv
* env
, jobject obj
) {
372 PersonalDataManagerAndroid
* personal_data_manager_android
=
373 new PersonalDataManagerAndroid(env
, obj
);
374 return reinterpret_cast<intptr_t>(personal_data_manager_android
);
377 } // namespace autofill