Add new certificateProvider extension API.
[chromium-blink-merge.git] / chrome / browser / autofill / android / personal_data_manager_android.cc
blob707565ba56e5083b3d52817da3230d6139c01d05
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/options_util.h"
15 #include "chrome/browser/autofill/personal_data_manager_factory.h"
16 #include "chrome/browser/browser_process.h"
17 #include "chrome/browser/profiles/profile.h"
18 #include "chrome/browser/profiles/profile_manager.h"
19 #include "chrome/common/pref_names.h"
20 #include "components/autofill/core/browser/autofill_country.h"
21 #include "components/autofill/core/browser/autofill_type.h"
22 #include "components/autofill/core/browser/field_types.h"
23 #include "components/autofill/core/browser/personal_data_manager.h"
24 #include "components/autofill/core/common/autofill_pref_names.h"
25 #include "components/autofill/core/common/autofill_switches.h"
26 #include "jni/PersonalDataManager_jni.h"
28 using base::android::ConvertJavaStringToUTF8;
29 using base::android::ConvertUTF16ToJavaString;
30 using base::android::ConvertUTF8ToJavaString;
31 using base::android::ScopedJavaLocalRef;
33 namespace autofill {
34 namespace {
36 Profile* GetProfile() {
37 return ProfileManager::GetActiveUserProfile()->GetOriginalProfile();
40 PrefService* GetPrefs() {
41 return GetProfile()->GetPrefs();
44 ScopedJavaLocalRef<jobject> CreateJavaProfileFromNative(
45 JNIEnv* env,
46 const AutofillProfile& profile) {
47 return Java_AutofillProfile_create(
48 env, ConvertUTF8ToJavaString(env, profile.guid()).obj(),
49 ConvertUTF8ToJavaString(env, profile.origin()).obj(),
50 profile.record_type() == AutofillProfile::LOCAL_PROFILE,
51 ConvertUTF16ToJavaString(env, profile.GetRawInfo(NAME_FULL)).obj(),
52 ConvertUTF16ToJavaString(env, profile.GetRawInfo(COMPANY_NAME)).obj(),
53 ConvertUTF16ToJavaString(
54 env, profile.GetRawInfo(ADDRESS_HOME_STREET_ADDRESS)).obj(),
55 ConvertUTF16ToJavaString(env, profile.GetRawInfo(ADDRESS_HOME_STATE))
56 .obj(),
57 ConvertUTF16ToJavaString(env, profile.GetRawInfo(ADDRESS_HOME_CITY))
58 .obj(),
59 ConvertUTF16ToJavaString(
60 env, profile.GetRawInfo(ADDRESS_HOME_DEPENDENT_LOCALITY)).obj(),
61 ConvertUTF16ToJavaString(env, profile.GetRawInfo(ADDRESS_HOME_ZIP)).obj(),
62 ConvertUTF16ToJavaString(
63 env, profile.GetRawInfo(ADDRESS_HOME_SORTING_CODE)).obj(),
64 ConvertUTF16ToJavaString(env, profile.GetRawInfo(ADDRESS_HOME_COUNTRY))
65 .obj(),
66 ConvertUTF16ToJavaString(env, profile.GetRawInfo(PHONE_HOME_WHOLE_NUMBER))
67 .obj(),
68 ConvertUTF16ToJavaString(env, profile.GetRawInfo(EMAIL_ADDRESS)).obj(),
69 ConvertUTF8ToJavaString(env, profile.language_code()).obj());
72 void PopulateNativeProfileFromJava(
73 const jobject& jprofile,
74 JNIEnv* env,
75 AutofillProfile* profile) {
76 profile->set_origin(
77 ConvertJavaStringToUTF8(
78 Java_AutofillProfile_getOrigin(env, jprofile)));
79 profile->SetInfo(
80 AutofillType(NAME_FULL),
81 ConvertJavaStringToUTF16(Java_AutofillProfile_getFullName(env, jprofile)),
82 g_browser_process->GetApplicationLocale());
83 profile->SetRawInfo(autofill::COMPANY_NAME,
84 ConvertJavaStringToUTF16(
85 Java_AutofillProfile_getCompanyName(env, jprofile)));
86 profile->SetRawInfo(
87 autofill::ADDRESS_HOME_STREET_ADDRESS,
88 ConvertJavaStringToUTF16(
89 Java_AutofillProfile_getStreetAddress(env, jprofile)));
90 profile->SetRawInfo(
91 autofill::ADDRESS_HOME_STATE,
92 ConvertJavaStringToUTF16(Java_AutofillProfile_getRegion(env, jprofile)));
93 profile->SetRawInfo(autofill::ADDRESS_HOME_CITY,
94 ConvertJavaStringToUTF16(
95 Java_AutofillProfile_getLocality(env, jprofile)));
96 profile->SetRawInfo(
97 autofill::ADDRESS_HOME_DEPENDENT_LOCALITY,
98 ConvertJavaStringToUTF16(
99 Java_AutofillProfile_getDependentLocality(env, jprofile)));
100 profile->SetRawInfo(autofill::ADDRESS_HOME_ZIP,
101 ConvertJavaStringToUTF16(
102 Java_AutofillProfile_getPostalCode(env, jprofile)));
103 profile->SetRawInfo(autofill::ADDRESS_HOME_SORTING_CODE,
104 ConvertJavaStringToUTF16(
105 Java_AutofillProfile_getSortingCode(env, jprofile)));
106 profile->SetInfo(AutofillType(ADDRESS_HOME_COUNTRY),
107 ConvertJavaStringToUTF16(
108 Java_AutofillProfile_getCountryCode(env, jprofile)),
109 g_browser_process->GetApplicationLocale());
110 profile->SetRawInfo(autofill::PHONE_HOME_WHOLE_NUMBER,
111 ConvertJavaStringToUTF16(
112 Java_AutofillProfile_getPhoneNumber(env, jprofile)));
113 profile->SetRawInfo(autofill::EMAIL_ADDRESS,
114 ConvertJavaStringToUTF16(
115 Java_AutofillProfile_getEmailAddress(env, jprofile)));
116 profile->set_language_code(
117 ConvertJavaStringToUTF8(
118 Java_AutofillProfile_getLanguageCode(env, jprofile)));
121 ScopedJavaLocalRef<jobject> CreateJavaCreditCardFromNative(
122 JNIEnv* env,
123 const CreditCard& card) {
124 return Java_CreditCard_create(
125 env, ConvertUTF8ToJavaString(env, card.guid()).obj(),
126 ConvertUTF8ToJavaString(env, card.origin()).obj(),
127 card.record_type() == CreditCard::LOCAL_CARD,
128 card.record_type() == CreditCard::FULL_SERVER_CARD,
129 ConvertUTF16ToJavaString(env, card.GetRawInfo(CREDIT_CARD_NAME)).obj(),
130 ConvertUTF16ToJavaString(env, card.GetRawInfo(CREDIT_CARD_NUMBER)).obj(),
131 ConvertUTF16ToJavaString(env, card.TypeAndLastFourDigits()).obj(),
132 ConvertUTF16ToJavaString(env, card.GetRawInfo(CREDIT_CARD_EXP_MONTH))
133 .obj(),
134 ConvertUTF16ToJavaString(
135 env, card.GetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR)).obj());
138 void PopulateNativeCreditCardFromJava(
139 const jobject& jcard,
140 JNIEnv* env,
141 CreditCard* card) {
142 card->set_origin(
143 ConvertJavaStringToUTF8(Java_CreditCard_getOrigin(env, jcard)));
144 card->SetRawInfo(
145 CREDIT_CARD_NAME,
146 ConvertJavaStringToUTF16(Java_CreditCard_getName(env, jcard)));
147 card->SetRawInfo(
148 CREDIT_CARD_NUMBER,
149 ConvertJavaStringToUTF16(Java_CreditCard_getNumber(env, jcard)));
150 card->SetRawInfo(
151 CREDIT_CARD_EXP_MONTH,
152 ConvertJavaStringToUTF16(Java_CreditCard_getMonth(env, jcard)));
153 card->SetRawInfo(
154 CREDIT_CARD_EXP_4_DIGIT_YEAR,
155 ConvertJavaStringToUTF16(Java_CreditCard_getYear(env, jcard)));
158 } // namespace
160 PersonalDataManagerAndroid::PersonalDataManagerAndroid(JNIEnv* env,
161 jobject obj)
162 : weak_java_obj_(env, obj),
163 personal_data_manager_(PersonalDataManagerFactory::GetForProfile(
164 ProfileManager::GetActiveUserProfile())) {
165 personal_data_manager_->AddObserver(this);
168 PersonalDataManagerAndroid::~PersonalDataManagerAndroid() {
169 personal_data_manager_->RemoveObserver(this);
172 jint PersonalDataManagerAndroid::GetProfileCount(JNIEnv* unused_env,
173 jobject unused_obj) {
174 return personal_data_manager_->GetProfiles().size();
177 ScopedJavaLocalRef<jobject> PersonalDataManagerAndroid::GetProfileByIndex(
178 JNIEnv* env,
179 jobject unused_obj,
180 jint index) {
181 const std::vector<AutofillProfile*>& profiles =
182 personal_data_manager_->GetProfiles();
183 size_t index_size_t = static_cast<size_t>(index);
184 DCHECK_LT(index_size_t, profiles.size());
185 return CreateJavaProfileFromNative(env, *profiles[index_size_t]);
188 ScopedJavaLocalRef<jobject> PersonalDataManagerAndroid::GetProfileByGUID(
189 JNIEnv* env,
190 jobject unused_obj,
191 jstring jguid) {
192 AutofillProfile* profile = personal_data_manager_->GetProfileByGUID(
193 ConvertJavaStringToUTF8(env, jguid));
194 if (!profile)
195 return ScopedJavaLocalRef<jobject>();
197 return CreateJavaProfileFromNative(env, *profile);
200 ScopedJavaLocalRef<jstring> PersonalDataManagerAndroid::SetProfile(
201 JNIEnv* env,
202 jobject unused_obj,
203 jobject jprofile) {
204 std::string guid = ConvertJavaStringToUTF8(
205 env,
206 Java_AutofillProfile_getGUID(env, jprofile).obj());
208 AutofillProfile profile;
209 PopulateNativeProfileFromJava(jprofile, env, &profile);
211 if (guid.empty()) {
212 personal_data_manager_->AddProfile(profile);
213 } else {
214 profile.set_guid(guid);
215 personal_data_manager_->UpdateProfile(profile);
218 return ConvertUTF8ToJavaString(env, profile.guid());
221 ScopedJavaLocalRef<jobjectArray> PersonalDataManagerAndroid::GetProfileLabels(
222 JNIEnv* env,
223 jobject unused_obj) {
224 std::vector<base::string16> labels;
225 AutofillProfile::CreateInferredLabels(
226 personal_data_manager_->GetProfiles(), NULL, NAME_FULL, 2,
227 g_browser_process->GetApplicationLocale(), &labels);
229 return base::android::ToJavaArrayOfStrings(env, labels);
232 jint PersonalDataManagerAndroid::GetCreditCardCount(JNIEnv* unused_env,
233 jobject unused_obj) {
234 return personal_data_manager_->GetCreditCards().size();
237 ScopedJavaLocalRef<jobject> PersonalDataManagerAndroid::GetCreditCardByIndex(
238 JNIEnv* env,
239 jobject unused_obj,
240 jint index) {
241 const std::vector<CreditCard*>& credit_cards =
242 personal_data_manager_->GetCreditCards();
243 size_t index_size_t = static_cast<size_t>(index);
244 DCHECK_LT(index_size_t, credit_cards.size());
245 return CreateJavaCreditCardFromNative(env, *credit_cards[index_size_t]);
248 ScopedJavaLocalRef<jobject> PersonalDataManagerAndroid::GetCreditCardByGUID(
249 JNIEnv* env,
250 jobject unused_obj,
251 jstring jguid) {
252 CreditCard* card = personal_data_manager_->GetCreditCardByGUID(
253 ConvertJavaStringToUTF8(env, jguid));
254 if (!card)
255 return ScopedJavaLocalRef<jobject>();
257 return CreateJavaCreditCardFromNative(env, *card);
260 ScopedJavaLocalRef<jstring> PersonalDataManagerAndroid::SetCreditCard(
261 JNIEnv* env,
262 jobject unused_obj,
263 jobject jcard) {
264 std::string guid = ConvertJavaStringToUTF8(
265 env,
266 Java_CreditCard_getGUID(env, jcard).obj());
268 CreditCard card;
269 PopulateNativeCreditCardFromJava(jcard, env, &card);
271 if (guid.empty()) {
272 personal_data_manager_->AddCreditCard(card);
273 } else {
274 card.set_guid(guid);
275 personal_data_manager_->UpdateCreditCard(card);
277 return ConvertUTF8ToJavaString(env, card.guid());
280 void PersonalDataManagerAndroid::RemoveByGUID(JNIEnv* env,
281 jobject unused_obj,
282 jstring jguid) {
283 personal_data_manager_->RemoveByGUID(ConvertJavaStringToUTF8(env, jguid));
286 void PersonalDataManagerAndroid::ClearUnmaskedCache(JNIEnv* env,
287 jobject unused_obj,
288 jstring guid) {
289 personal_data_manager_->ResetFullServerCard(
290 ConvertJavaStringToUTF8(env, guid));
293 void PersonalDataManagerAndroid::OnPersonalDataChanged() {
294 JNIEnv* env = base::android::AttachCurrentThread();
295 if (weak_java_obj_.get(env).is_null())
296 return;
298 Java_PersonalDataManager_personalDataChanged(env,
299 weak_java_obj_.get(env).obj());
302 // static
303 bool PersonalDataManagerAndroid::Register(JNIEnv* env) {
304 return RegisterNativesImpl(env);
307 // Returns whether the Autofill feature is enabled.
308 static jboolean IsAutofillEnabled(JNIEnv* env,
309 const JavaParamRef<jclass>& clazz) {
310 return GetPrefs()->GetBoolean(autofill::prefs::kAutofillEnabled);
313 // Enables or disables the Autofill feature.
314 static void SetAutofillEnabled(JNIEnv* env,
315 const JavaParamRef<jclass>& clazz,
316 jboolean enable) {
317 GetPrefs()->SetBoolean(autofill::prefs::kAutofillEnabled, enable);
320 // Returns whether the Autofill feature is managed.
321 static jboolean IsAutofillManaged(JNIEnv* env,
322 const JavaParamRef<jclass>& clazz) {
323 return GetPrefs()->IsManagedPreference(autofill::prefs::kAutofillEnabled);
326 // Returns whether the Wallet import feature is available.
327 static jboolean IsWalletImportFeatureAvailable(
328 JNIEnv* env,
329 const JavaParamRef<jclass>& clazz) {
330 return WalletIntegrationAvailableForProfile(GetProfile());
333 // Returns whether the Wallet import feature is enabled.
334 static jboolean IsWalletImportEnabled(JNIEnv* env,
335 const JavaParamRef<jclass>& clazz) {
336 return GetPrefs()->GetBoolean(autofill::prefs::kAutofillWalletImportEnabled);
339 // Enables or disables the Wallet import feature.
340 static void SetWalletImportEnabled(JNIEnv* env,
341 const JavaParamRef<jclass>& clazz,
342 jboolean enable) {
343 GetPrefs()->SetBoolean(autofill::prefs::kAutofillWalletImportEnabled, enable);
346 // Returns an ISO 3166-1-alpha-2 country code for a |jcountry_name| using
347 // the application locale, or an empty string.
348 static ScopedJavaLocalRef<jstring> ToCountryCode(
349 JNIEnv* env,
350 const JavaParamRef<jclass>& clazz,
351 const JavaParamRef<jstring>& jcountry_name) {
352 return ConvertUTF8ToJavaString(
353 env, AutofillCountry::GetCountryCode(
354 base::android::ConvertJavaStringToUTF16(env, jcountry_name),
355 g_browser_process->GetApplicationLocale()));
358 static jlong Init(JNIEnv* env, const JavaParamRef<jobject>& obj) {
359 PersonalDataManagerAndroid* personal_data_manager_android =
360 new PersonalDataManagerAndroid(env, obj);
361 return reinterpret_cast<intptr_t>(personal_data_manager_android);
364 } // namespace autofill