Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / autofill / android / personal_data_manager_android.cc
bloba094a2c7ae3dd5cc87a5ad434ec0702ce6e88728
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_string.h"
8 #include "base/format_macros.h"
9 #include "base/prefs/pref_service.h"
10 #include "base/strings/stringprintf.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "chrome/browser/autofill/personal_data_manager_factory.h"
13 #include "chrome/browser/browser_process.h"
14 #include "chrome/browser/profiles/profile_manager.h"
15 #include "chrome/common/pref_names.h"
16 #include "components/autofill/core/browser/autofill_country.h"
17 #include "components/autofill/core/browser/autofill_type.h"
18 #include "components/autofill/core/browser/field_types.h"
19 #include "components/autofill/core/browser/personal_data_manager.h"
20 #include "jni/PersonalDataManager_jni.h"
22 using base::android::ConvertJavaStringToUTF8;
23 using base::android::ConvertUTF16ToJavaString;
24 using base::android::ConvertUTF8ToJavaString;
25 using base::android::ScopedJavaLocalRef;
27 namespace autofill {
28 namespace {
30 PrefService* GetPrefs() {
31 return
32 ProfileManager::GetActiveUserProfile()->GetOriginalProfile()->GetPrefs();
35 ScopedJavaLocalRef<jobject> CreateJavaProfileFromNative(
36 JNIEnv* env,
37 const AutofillProfile& profile) {
38 return Java_AutofillProfile_create(
39 env,
40 ConvertUTF8ToJavaString(env, profile.guid()).obj(),
41 ConvertUTF8ToJavaString(env, profile.origin()).obj(),
42 ConvertUTF16ToJavaString(env, profile.GetRawInfo(NAME_FULL)).obj(),
43 ConvertUTF16ToJavaString(env, profile.GetRawInfo(COMPANY_NAME)).obj(),
44 ConvertUTF16ToJavaString(
45 env,
46 profile.GetRawInfo(ADDRESS_HOME_LINE1)).obj(),
47 ConvertUTF16ToJavaString(
48 env,
49 profile.GetRawInfo(ADDRESS_HOME_LINE2)).obj(),
50 ConvertUTF16ToJavaString(
51 env,
52 profile.GetRawInfo(ADDRESS_HOME_CITY)).obj(),
53 ConvertUTF16ToJavaString(
54 env,
55 profile.GetRawInfo(ADDRESS_HOME_STATE)).obj(),
56 ConvertUTF16ToJavaString(
57 env,
58 profile.GetRawInfo(ADDRESS_HOME_ZIP)).obj(),
59 ConvertUTF16ToJavaString(
60 env,
61 profile.GetInfo(AutofillType(ADDRESS_HOME_COUNTRY),
62 g_browser_process->GetApplicationLocale())).obj(),
63 ConvertUTF16ToJavaString(
64 env,
65 profile.GetRawInfo(PHONE_HOME_WHOLE_NUMBER)).obj(),
66 ConvertUTF16ToJavaString(env, profile.GetRawInfo(EMAIL_ADDRESS)).obj());
69 void PopulateNativeProfileFromJava(
70 const jobject& jprofile,
71 JNIEnv* env,
72 AutofillProfile* profile) {
73 profile->set_origin(
74 ConvertJavaStringToUTF8(
75 Java_AutofillProfile_getOrigin(env, jprofile)));
76 profile->SetRawInfo(
77 NAME_FULL,
78 ConvertJavaStringToUTF16(
79 Java_AutofillProfile_getFullName(env, jprofile)));
80 profile->SetRawInfo(
81 COMPANY_NAME,
82 ConvertJavaStringToUTF16(
83 Java_AutofillProfile_getCompanyName(env, jprofile)));
84 profile->SetRawInfo(
85 ADDRESS_HOME_LINE1,
86 ConvertJavaStringToUTF16(
87 Java_AutofillProfile_getAddressLine1(env, jprofile)));
88 profile->SetRawInfo(
89 ADDRESS_HOME_LINE2,
90 ConvertJavaStringToUTF16(
91 Java_AutofillProfile_getAddressLine2(env, jprofile)));
92 profile->SetRawInfo(
93 ADDRESS_HOME_CITY,
94 ConvertJavaStringToUTF16(
95 Java_AutofillProfile_getCity(env, jprofile)));
96 profile->SetRawInfo(
97 ADDRESS_HOME_STATE,
98 ConvertJavaStringToUTF16(
99 Java_AutofillProfile_getState(env, jprofile)));
100 profile->SetRawInfo(
101 ADDRESS_HOME_ZIP,
102 ConvertJavaStringToUTF16(
103 Java_AutofillProfile_getZip(env, jprofile)));
104 profile->SetInfo(
105 AutofillType(ADDRESS_HOME_COUNTRY),
106 ConvertJavaStringToUTF16(
107 Java_AutofillProfile_getCountry(env, jprofile)),
108 g_browser_process->GetApplicationLocale());
109 profile->SetRawInfo(
110 PHONE_HOME_WHOLE_NUMBER,
111 ConvertJavaStringToUTF16(
112 Java_AutofillProfile_getPhoneNumber(env, jprofile)));
113 profile->SetRawInfo(
114 EMAIL_ADDRESS,
115 ConvertJavaStringToUTF16(
116 Java_AutofillProfile_getEmailAddress(env, jprofile)));
119 ScopedJavaLocalRef<jobject> CreateJavaCreditCardFromNative(
120 JNIEnv* env,
121 const CreditCard& card) {
122 return Java_CreditCard_create(
123 env,
124 ConvertUTF8ToJavaString(env, card.guid()).obj(),
125 ConvertUTF8ToJavaString(env, card.origin()).obj(),
126 ConvertUTF16ToJavaString(env, card.GetRawInfo(CREDIT_CARD_NAME)).obj(),
127 ConvertUTF16ToJavaString(env, card.GetRawInfo(CREDIT_CARD_NUMBER)).obj(),
128 ConvertUTF16ToJavaString(env, card.ObfuscatedNumber()).obj(),
129 ConvertUTF16ToJavaString(
130 env,
131 card.GetRawInfo(CREDIT_CARD_EXP_MONTH)).obj(),
132 ConvertUTF16ToJavaString(
133 env,
134 card.GetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR)).obj());
137 void PopulateNativeCreditCardFromJava(
138 const jobject& jcard,
139 JNIEnv* env,
140 CreditCard* card) {
141 card->set_origin(
142 ConvertJavaStringToUTF8(Java_CreditCard_getOrigin(env, jcard)));
143 card->SetRawInfo(
144 CREDIT_CARD_NAME,
145 ConvertJavaStringToUTF16(Java_CreditCard_getName(env, jcard)));
146 card->SetRawInfo(
147 CREDIT_CARD_NUMBER,
148 ConvertJavaStringToUTF16(Java_CreditCard_getNumber(env, jcard)));
149 card->SetRawInfo(
150 CREDIT_CARD_EXP_MONTH,
151 ConvertJavaStringToUTF16(Java_CreditCard_getMonth(env, jcard)));
152 card->SetRawInfo(
153 CREDIT_CARD_EXP_4_DIGIT_YEAR,
154 ConvertJavaStringToUTF16(Java_CreditCard_getYear(env, jcard)));
157 } // namespace
159 PersonalDataManagerAndroid::PersonalDataManagerAndroid(JNIEnv* env,
160 jobject obj)
161 : weak_java_obj_(env, obj),
162 personal_data_manager_(PersonalDataManagerFactory::GetForProfile(
163 ProfileManager::GetActiveUserProfile())) {
164 personal_data_manager_->AddObserver(this);
167 PersonalDataManagerAndroid::~PersonalDataManagerAndroid() {
168 personal_data_manager_->RemoveObserver(this);
171 jint PersonalDataManagerAndroid::GetProfileCount(JNIEnv* unused_env,
172 jobject unused_obj) {
173 return personal_data_manager_->GetProfiles().size();
176 ScopedJavaLocalRef<jobject> PersonalDataManagerAndroid::GetProfileByIndex(
177 JNIEnv* env,
178 jobject unused_obj,
179 jint index) {
180 const std::vector<AutofillProfile*>& profiles =
181 personal_data_manager_->GetProfiles();
182 size_t index_size_t = static_cast<size_t>(index);
183 DCHECK_LT(index_size_t, profiles.size());
184 return CreateJavaProfileFromNative(env, *profiles[index_size_t]);
187 ScopedJavaLocalRef<jobject> PersonalDataManagerAndroid::GetProfileByGUID(
188 JNIEnv* env,
189 jobject unused_obj,
190 jstring jguid) {
191 AutofillProfile* profile = personal_data_manager_->GetProfileByGUID(
192 ConvertJavaStringToUTF8(env, jguid));
193 if (!profile)
194 return ScopedJavaLocalRef<jobject>();
196 return CreateJavaProfileFromNative(env, *profile);
199 ScopedJavaLocalRef<jstring> PersonalDataManagerAndroid::SetProfile(
200 JNIEnv* env,
201 jobject unused_obj,
202 jobject jprofile) {
203 std::string guid = ConvertJavaStringToUTF8(
204 env,
205 Java_AutofillProfile_getGUID(env, jprofile).obj());
207 AutofillProfile profile;
208 PopulateNativeProfileFromJava(jprofile, env, &profile);
210 if (guid.empty()) {
211 personal_data_manager_->AddProfile(profile);
212 } else {
213 profile.set_guid(guid);
214 personal_data_manager_->UpdateProfile(profile);
217 return ConvertUTF8ToJavaString(env, profile.guid());
220 jint PersonalDataManagerAndroid::GetCreditCardCount(JNIEnv* unused_env,
221 jobject unused_obj) {
222 return personal_data_manager_->GetCreditCards().size();
225 ScopedJavaLocalRef<jobject> PersonalDataManagerAndroid::GetCreditCardByIndex(
226 JNIEnv* env,
227 jobject unused_obj,
228 jint index) {
229 const std::vector<CreditCard*>& credit_cards =
230 personal_data_manager_->GetCreditCards();
231 size_t index_size_t = static_cast<size_t>(index);
232 DCHECK_LT(index_size_t, credit_cards.size());
233 return CreateJavaCreditCardFromNative(env, *credit_cards[index_size_t]);
236 ScopedJavaLocalRef<jobject> PersonalDataManagerAndroid::GetCreditCardByGUID(
237 JNIEnv* env,
238 jobject unused_obj,
239 jstring jguid) {
240 CreditCard* card = personal_data_manager_->GetCreditCardByGUID(
241 ConvertJavaStringToUTF8(env, jguid));
242 if (!card)
243 return ScopedJavaLocalRef<jobject>();
245 return CreateJavaCreditCardFromNative(env, *card);
248 ScopedJavaLocalRef<jstring> PersonalDataManagerAndroid::SetCreditCard(
249 JNIEnv* env,
250 jobject unused_obj,
251 jobject jcard) {
252 std::string guid = ConvertJavaStringToUTF8(
253 env,
254 Java_CreditCard_getGUID(env, jcard).obj());
256 CreditCard card;
257 PopulateNativeCreditCardFromJava(jcard, env, &card);
259 if (guid.empty()) {
260 personal_data_manager_->AddCreditCard(card);
261 } else {
262 card.set_guid(guid);
263 personal_data_manager_->UpdateCreditCard(card);
265 return ConvertUTF8ToJavaString(env, card.guid());
268 void PersonalDataManagerAndroid::RemoveByGUID(JNIEnv* env,
269 jobject unused_obj,
270 jstring jguid) {
271 personal_data_manager_->RemoveByGUID(ConvertJavaStringToUTF8(env, jguid));
274 void PersonalDataManagerAndroid::OnPersonalDataChanged() {
275 JNIEnv* env = base::android::AttachCurrentThread();
276 if (weak_java_obj_.get(env).is_null())
277 return;
279 Java_PersonalDataManager_personalDataChanged(env,
280 weak_java_obj_.get(env).obj());
283 // static
284 bool PersonalDataManagerAndroid::Register(JNIEnv* env) {
285 return RegisterNativesImpl(env);
288 // Returns whether the Autofill feature is enabled.
289 static jboolean IsAutofillEnabled(JNIEnv* env, jclass clazz) {
290 return GetPrefs()->GetBoolean(autofill::prefs::kAutofillEnabled);
293 // Enables or disables the Autofill feature.
294 static void SetAutofillEnabled(JNIEnv* env, jclass clazz, jboolean enable) {
295 GetPrefs()->SetBoolean(autofill::prefs::kAutofillEnabled, enable);
298 // Returns whether Autofill feature is managed.
299 static jboolean IsAutofillManaged(JNIEnv* env, jclass clazz) {
300 return GetPrefs()->IsManagedPreference(autofill::prefs::kAutofillEnabled);
303 // Returns an ISO 3166-1-alpha-2 country code for a |jcountry_name| using
304 // the application locale, or an empty string.
305 static jstring ToCountryCode(JNIEnv* env, jclass clazz, jstring jcountry_name) {
306 return ConvertUTF8ToJavaString(
307 env,
308 AutofillCountry::GetCountryCode(
309 base::android::ConvertJavaStringToUTF16(env, jcountry_name),
310 g_browser_process->GetApplicationLocale())).Release();
313 static jlong Init(JNIEnv* env, jobject obj) {
314 PersonalDataManagerAndroid* personal_data_manager_android =
315 new PersonalDataManagerAndroid(env, obj);
316 return reinterpret_cast<intptr_t>(personal_data_manager_android);
319 } // namespace autofill