Fire an error if a pref used in the UI is missing once all prefs are fetched.
[chromium-blink-merge.git] / chrome / browser / autofill / android / personal_data_manager_android.cc
blobb6214efa1edeb18edf182f107f104e4e26153584
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;
34 namespace autofill {
35 namespace {
37 Profile* GetProfile() {
38 return ProfileManager::GetActiveUserProfile()->GetOriginalProfile();
41 PrefService* GetPrefs() {
42 return GetProfile()->GetPrefs();
45 ScopedJavaLocalRef<jobject> CreateJavaProfileFromNative(
46 JNIEnv* env,
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))
57 .obj(),
58 ConvertUTF16ToJavaString(env, profile.GetRawInfo(ADDRESS_HOME_CITY))
59 .obj(),
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))
66 .obj(),
67 ConvertUTF16ToJavaString(env, profile.GetRawInfo(PHONE_HOME_WHOLE_NUMBER))
68 .obj(),
69 ConvertUTF16ToJavaString(env, profile.GetRawInfo(EMAIL_ADDRESS)).obj(),
70 ConvertUTF8ToJavaString(env, profile.language_code()).obj());
73 void PopulateNativeProfileFromJava(
74 const jobject& jprofile,
75 JNIEnv* env,
76 AutofillProfile* profile) {
77 profile->set_origin(
78 ConvertJavaStringToUTF8(
79 Java_AutofillProfile_getOrigin(env, jprofile)));
80 profile->SetInfo(
81 AutofillType(NAME_FULL),
82 ConvertJavaStringToUTF16(
83 Java_AutofillProfile_getFullName(env, jprofile)),
84 g_browser_process->GetApplicationLocale());
85 profile->SetInfo(
86 AutofillType(COMPANY_NAME),
87 ConvertJavaStringToUTF16(
88 Java_AutofillProfile_getCompanyName(env, jprofile)),
89 g_browser_process->GetApplicationLocale());
90 profile->SetInfo(
91 AutofillType(ADDRESS_HOME_STREET_ADDRESS),
92 ConvertJavaStringToUTF16(
93 Java_AutofillProfile_getStreetAddress(env, jprofile)),
94 g_browser_process->GetApplicationLocale());
95 profile->SetInfo(
96 AutofillType(ADDRESS_HOME_STATE),
97 ConvertJavaStringToUTF16(
98 Java_AutofillProfile_getRegion(env, jprofile)),
99 g_browser_process->GetApplicationLocale());
100 profile->SetInfo(
101 AutofillType(ADDRESS_HOME_CITY),
102 ConvertJavaStringToUTF16(
103 Java_AutofillProfile_getLocality(env, jprofile)),
104 g_browser_process->GetApplicationLocale());
105 profile->SetInfo(
106 AutofillType(ADDRESS_HOME_DEPENDENT_LOCALITY),
107 ConvertJavaStringToUTF16(
108 Java_AutofillProfile_getDependentLocality(env, jprofile)),
109 g_browser_process->GetApplicationLocale());
110 profile->SetInfo(
111 AutofillType(ADDRESS_HOME_ZIP),
112 ConvertJavaStringToUTF16(
113 Java_AutofillProfile_getPostalCode(env, jprofile)),
114 g_browser_process->GetApplicationLocale());
115 profile->SetInfo(
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());
124 profile->SetInfo(
125 AutofillType(PHONE_HOME_WHOLE_NUMBER),
126 ConvertJavaStringToUTF16(
127 Java_AutofillProfile_getPhoneNumber(env, jprofile)),
128 g_browser_process->GetApplicationLocale());
129 profile->SetInfo(
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(
140 JNIEnv* env,
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))
151 .obj(),
152 ConvertUTF16ToJavaString(
153 env, card.GetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR)).obj());
156 void PopulateNativeCreditCardFromJava(
157 const jobject& jcard,
158 JNIEnv* env,
159 CreditCard* card) {
160 card->set_origin(
161 ConvertJavaStringToUTF8(Java_CreditCard_getOrigin(env, jcard)));
162 card->SetRawInfo(
163 CREDIT_CARD_NAME,
164 ConvertJavaStringToUTF16(Java_CreditCard_getName(env, jcard)));
165 card->SetRawInfo(
166 CREDIT_CARD_NUMBER,
167 ConvertJavaStringToUTF16(Java_CreditCard_getNumber(env, jcard)));
168 card->SetRawInfo(
169 CREDIT_CARD_EXP_MONTH,
170 ConvertJavaStringToUTF16(Java_CreditCard_getMonth(env, jcard)));
171 card->SetRawInfo(
172 CREDIT_CARD_EXP_4_DIGIT_YEAR,
173 ConvertJavaStringToUTF16(Java_CreditCard_getYear(env, jcard)));
176 } // namespace
178 PersonalDataManagerAndroid::PersonalDataManagerAndroid(JNIEnv* env,
179 jobject obj)
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(
196 JNIEnv* env,
197 jobject unused_obj,
198 jint index) {
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(
207 JNIEnv* env,
208 jobject unused_obj,
209 jstring jguid) {
210 AutofillProfile* profile = personal_data_manager_->GetProfileByGUID(
211 ConvertJavaStringToUTF8(env, jguid));
212 if (!profile)
213 return ScopedJavaLocalRef<jobject>();
215 return CreateJavaProfileFromNative(env, *profile);
218 ScopedJavaLocalRef<jstring> PersonalDataManagerAndroid::SetProfile(
219 JNIEnv* env,
220 jobject unused_obj,
221 jobject jprofile) {
222 std::string guid = ConvertJavaStringToUTF8(
223 env,
224 Java_AutofillProfile_getGUID(env, jprofile).obj());
226 AutofillProfile profile;
227 PopulateNativeProfileFromJava(jprofile, env, &profile);
229 if (guid.empty()) {
230 personal_data_manager_->AddProfile(profile);
231 } else {
232 profile.set_guid(guid);
233 personal_data_manager_->UpdateProfile(profile);
236 return ConvertUTF8ToJavaString(env, profile.guid());
239 ScopedJavaLocalRef<jobjectArray> PersonalDataManagerAndroid::GetProfileLabels(
240 JNIEnv* env,
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(
256 JNIEnv* env,
257 jobject unused_obj,
258 jint index) {
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(
267 JNIEnv* env,
268 jobject unused_obj,
269 jstring jguid) {
270 CreditCard* card = personal_data_manager_->GetCreditCardByGUID(
271 ConvertJavaStringToUTF8(env, jguid));
272 if (!card)
273 return ScopedJavaLocalRef<jobject>();
275 return CreateJavaCreditCardFromNative(env, *card);
278 ScopedJavaLocalRef<jstring> PersonalDataManagerAndroid::SetCreditCard(
279 JNIEnv* env,
280 jobject unused_obj,
281 jobject jcard) {
282 std::string guid = ConvertJavaStringToUTF8(
283 env,
284 Java_CreditCard_getGUID(env, jcard).obj());
286 CreditCard card;
287 PopulateNativeCreditCardFromJava(jcard, env, &card);
289 if (guid.empty()) {
290 personal_data_manager_->AddCreditCard(card);
291 } else {
292 card.set_guid(guid);
293 personal_data_manager_->UpdateCreditCard(card);
295 return ConvertUTF8ToJavaString(env, card.guid());
298 void PersonalDataManagerAndroid::RemoveByGUID(JNIEnv* env,
299 jobject unused_obj,
300 jstring jguid) {
301 personal_data_manager_->RemoveByGUID(ConvertJavaStringToUTF8(env, jguid));
304 void PersonalDataManagerAndroid::ClearUnmaskedCache(JNIEnv* env,
305 jobject unused_obj,
306 jstring guid) {
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())
314 return;
316 Java_PersonalDataManager_personalDataChanged(env,
317 weak_java_obj_.get(env).obj());
320 // static
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(
346 GetProfile());
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(
365 env,
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