ozone: evdev: Sync caps lock LED state to evdev
[chromium-blink-merge.git] / chrome / browser / autofill / android / personal_data_manager_android.cc
blob7192239e48be43f129474fcd2c680aebd20a1b7a
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,
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))
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,
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(
150 env,
151 card.GetRawInfo(CREDIT_CARD_EXP_MONTH)).obj(),
152 ConvertUTF16ToJavaString(
153 env,
154 card.GetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR)).obj());
157 void PopulateNativeCreditCardFromJava(
158 const jobject& jcard,
159 JNIEnv* env,
160 CreditCard* card) {
161 card->set_origin(
162 ConvertJavaStringToUTF8(Java_CreditCard_getOrigin(env, jcard)));
163 card->SetRawInfo(
164 CREDIT_CARD_NAME,
165 ConvertJavaStringToUTF16(Java_CreditCard_getName(env, jcard)));
166 card->SetRawInfo(
167 CREDIT_CARD_NUMBER,
168 ConvertJavaStringToUTF16(Java_CreditCard_getNumber(env, jcard)));
169 card->SetRawInfo(
170 CREDIT_CARD_EXP_MONTH,
171 ConvertJavaStringToUTF16(Java_CreditCard_getMonth(env, jcard)));
172 card->SetRawInfo(
173 CREDIT_CARD_EXP_4_DIGIT_YEAR,
174 ConvertJavaStringToUTF16(Java_CreditCard_getYear(env, jcard)));
177 } // namespace
179 PersonalDataManagerAndroid::PersonalDataManagerAndroid(JNIEnv* env,
180 jobject obj)
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(
197 JNIEnv* env,
198 jobject unused_obj,
199 jint index) {
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(
208 JNIEnv* env,
209 jobject unused_obj,
210 jstring jguid) {
211 AutofillProfile* profile = personal_data_manager_->GetProfileByGUID(
212 ConvertJavaStringToUTF8(env, jguid));
213 if (!profile)
214 return ScopedJavaLocalRef<jobject>();
216 return CreateJavaProfileFromNative(env, *profile);
219 ScopedJavaLocalRef<jstring> PersonalDataManagerAndroid::SetProfile(
220 JNIEnv* env,
221 jobject unused_obj,
222 jobject jprofile) {
223 std::string guid = ConvertJavaStringToUTF8(
224 env,
225 Java_AutofillProfile_getGUID(env, jprofile).obj());
227 AutofillProfile profile;
228 PopulateNativeProfileFromJava(jprofile, env, &profile);
230 if (guid.empty()) {
231 personal_data_manager_->AddProfile(profile);
232 } else {
233 profile.set_guid(guid);
234 personal_data_manager_->UpdateProfile(profile);
237 return ConvertUTF8ToJavaString(env, profile.guid());
240 ScopedJavaLocalRef<jobjectArray> PersonalDataManagerAndroid::GetProfileLabels(
241 JNIEnv* env,
242 jobject unused_obj) {
243 std::vector<base::string16> labels;
244 AutofillProfile::CreateInferredLabels(
245 personal_data_manager_->web_profiles(),
246 NULL,
247 NAME_FULL,
249 g_browser_process->GetApplicationLocale(),
250 &labels);
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(
261 JNIEnv* env,
262 jobject unused_obj,
263 jint index) {
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(
272 JNIEnv* env,
273 jobject unused_obj,
274 jstring jguid) {
275 CreditCard* card = personal_data_manager_->GetCreditCardByGUID(
276 ConvertJavaStringToUTF8(env, jguid));
277 if (!card)
278 return ScopedJavaLocalRef<jobject>();
280 return CreateJavaCreditCardFromNative(env, *card);
283 ScopedJavaLocalRef<jstring> PersonalDataManagerAndroid::SetCreditCard(
284 JNIEnv* env,
285 jobject unused_obj,
286 jobject jcard) {
287 std::string guid = ConvertJavaStringToUTF8(
288 env,
289 Java_CreditCard_getGUID(env, jcard).obj());
291 CreditCard card;
292 PopulateNativeCreditCardFromJava(jcard, env, &card);
294 if (guid.empty()) {
295 personal_data_manager_->AddCreditCard(card);
296 } else {
297 card.set_guid(guid);
298 personal_data_manager_->UpdateCreditCard(card);
300 return ConvertUTF8ToJavaString(env, card.guid());
303 void PersonalDataManagerAndroid::RemoveByGUID(JNIEnv* env,
304 jobject unused_obj,
305 jstring jguid) {
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())
317 return;
319 Java_PersonalDataManager_personalDataChanged(env,
320 weak_java_obj_.get(env).obj());
323 // static
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(
349 GetProfile());
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(
368 env,
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