Cast: Stop logging kVideoFrameSentToEncoder and rename a couple events.
[chromium-blink-merge.git] / chrome / browser / autofill / android / personal_data_manager_android.cc
blobc020e102852afe0dd970fb51a3c44dbfd3bfa1d6
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_STREET_ADDRESS)).obj(),
47 ConvertUTF16ToJavaString(
48 env,
49 profile.GetRawInfo(ADDRESS_HOME_STATE)).obj(),
50 ConvertUTF16ToJavaString(
51 env,
52 profile.GetRawInfo(ADDRESS_HOME_CITY)).obj(),
53 ConvertUTF16ToJavaString(
54 env,
55 profile.GetRawInfo(ADDRESS_HOME_DEPENDENT_LOCALITY)).obj(),
56 ConvertUTF16ToJavaString(
57 env,
58 profile.GetRawInfo(ADDRESS_HOME_ZIP)).obj(),
59 ConvertUTF16ToJavaString(
60 env,
61 profile.GetRawInfo(ADDRESS_HOME_SORTING_CODE)).obj(),
62 ConvertUTF16ToJavaString(
63 env,
64 profile.GetInfo(AutofillType(ADDRESS_HOME_COUNTRY),
65 g_browser_process->GetApplicationLocale())).obj(),
66 ConvertUTF16ToJavaString(
67 env,
68 profile.GetRawInfo(PHONE_HOME_WHOLE_NUMBER)).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->SetRawInfo(
81 NAME_FULL,
82 ConvertJavaStringToUTF16(
83 Java_AutofillProfile_getFullName(env, jprofile)));
84 profile->SetRawInfo(
85 COMPANY_NAME,
86 ConvertJavaStringToUTF16(
87 Java_AutofillProfile_getCompanyName(env, jprofile)));
88 profile->SetRawInfo(
89 ADDRESS_HOME_STREET_ADDRESS,
90 ConvertJavaStringToUTF16(
91 Java_AutofillProfile_getStreetAddress(env, jprofile)));
92 profile->SetRawInfo(
93 ADDRESS_HOME_STATE,
94 ConvertJavaStringToUTF16(
95 Java_AutofillProfile_getRegion(env, jprofile)));
96 profile->SetRawInfo(
97 ADDRESS_HOME_CITY,
98 ConvertJavaStringToUTF16(
99 Java_AutofillProfile_getLocality(env, jprofile)));
100 profile->SetRawInfo(
101 ADDRESS_HOME_DEPENDENT_LOCALITY,
102 ConvertJavaStringToUTF16(
103 Java_AutofillProfile_getDependentLocality(env, jprofile)));
104 profile->SetRawInfo(
105 ADDRESS_HOME_ZIP,
106 ConvertJavaStringToUTF16(
107 Java_AutofillProfile_getPostalCode(env, jprofile)));
108 profile->SetRawInfo(
109 ADDRESS_HOME_SORTING_CODE,
110 ConvertJavaStringToUTF16(
111 Java_AutofillProfile_getSortingCode(env, jprofile)));
112 profile->SetInfo(
113 AutofillType(ADDRESS_HOME_COUNTRY),
114 ConvertJavaStringToUTF16(
115 Java_AutofillProfile_getCountry(env, jprofile)),
116 g_browser_process->GetApplicationLocale());
117 profile->SetRawInfo(
118 PHONE_HOME_WHOLE_NUMBER,
119 ConvertJavaStringToUTF16(
120 Java_AutofillProfile_getPhoneNumber(env, jprofile)));
121 profile->SetRawInfo(
122 EMAIL_ADDRESS,
123 ConvertJavaStringToUTF16(
124 Java_AutofillProfile_getEmailAddress(env, jprofile)));
125 profile->set_language_code(
126 ConvertJavaStringToUTF8(
127 Java_AutofillProfile_getLanguageCode(env, jprofile)));
130 ScopedJavaLocalRef<jobject> CreateJavaCreditCardFromNative(
131 JNIEnv* env,
132 const CreditCard& card) {
133 return Java_CreditCard_create(
134 env,
135 ConvertUTF8ToJavaString(env, card.guid()).obj(),
136 ConvertUTF8ToJavaString(env, card.origin()).obj(),
137 ConvertUTF16ToJavaString(env, card.GetRawInfo(CREDIT_CARD_NAME)).obj(),
138 ConvertUTF16ToJavaString(env, card.GetRawInfo(CREDIT_CARD_NUMBER)).obj(),
139 ConvertUTF16ToJavaString(env, card.ObfuscatedNumber()).obj(),
140 ConvertUTF16ToJavaString(
141 env,
142 card.GetRawInfo(CREDIT_CARD_EXP_MONTH)).obj(),
143 ConvertUTF16ToJavaString(
144 env,
145 card.GetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR)).obj());
148 void PopulateNativeCreditCardFromJava(
149 const jobject& jcard,
150 JNIEnv* env,
151 CreditCard* card) {
152 card->set_origin(
153 ConvertJavaStringToUTF8(Java_CreditCard_getOrigin(env, jcard)));
154 card->SetRawInfo(
155 CREDIT_CARD_NAME,
156 ConvertJavaStringToUTF16(Java_CreditCard_getName(env, jcard)));
157 card->SetRawInfo(
158 CREDIT_CARD_NUMBER,
159 ConvertJavaStringToUTF16(Java_CreditCard_getNumber(env, jcard)));
160 card->SetRawInfo(
161 CREDIT_CARD_EXP_MONTH,
162 ConvertJavaStringToUTF16(Java_CreditCard_getMonth(env, jcard)));
163 card->SetRawInfo(
164 CREDIT_CARD_EXP_4_DIGIT_YEAR,
165 ConvertJavaStringToUTF16(Java_CreditCard_getYear(env, jcard)));
168 } // namespace
170 PersonalDataManagerAndroid::PersonalDataManagerAndroid(JNIEnv* env,
171 jobject obj)
172 : weak_java_obj_(env, obj),
173 personal_data_manager_(PersonalDataManagerFactory::GetForProfile(
174 ProfileManager::GetActiveUserProfile())) {
175 personal_data_manager_->AddObserver(this);
178 PersonalDataManagerAndroid::~PersonalDataManagerAndroid() {
179 personal_data_manager_->RemoveObserver(this);
182 jint PersonalDataManagerAndroid::GetProfileCount(JNIEnv* unused_env,
183 jobject unused_obj) {
184 return personal_data_manager_->GetProfiles().size();
187 ScopedJavaLocalRef<jobject> PersonalDataManagerAndroid::GetProfileByIndex(
188 JNIEnv* env,
189 jobject unused_obj,
190 jint index) {
191 const std::vector<AutofillProfile*>& profiles =
192 personal_data_manager_->GetProfiles();
193 size_t index_size_t = static_cast<size_t>(index);
194 DCHECK_LT(index_size_t, profiles.size());
195 return CreateJavaProfileFromNative(env, *profiles[index_size_t]);
198 ScopedJavaLocalRef<jobject> PersonalDataManagerAndroid::GetProfileByGUID(
199 JNIEnv* env,
200 jobject unused_obj,
201 jstring jguid) {
202 AutofillProfile* profile = personal_data_manager_->GetProfileByGUID(
203 ConvertJavaStringToUTF8(env, jguid));
204 if (!profile)
205 return ScopedJavaLocalRef<jobject>();
207 return CreateJavaProfileFromNative(env, *profile);
210 ScopedJavaLocalRef<jstring> PersonalDataManagerAndroid::SetProfile(
211 JNIEnv* env,
212 jobject unused_obj,
213 jobject jprofile) {
214 std::string guid = ConvertJavaStringToUTF8(
215 env,
216 Java_AutofillProfile_getGUID(env, jprofile).obj());
218 AutofillProfile profile;
219 PopulateNativeProfileFromJava(jprofile, env, &profile);
221 if (guid.empty()) {
222 personal_data_manager_->AddProfile(profile);
223 } else {
224 profile.set_guid(guid);
225 personal_data_manager_->UpdateProfile(profile);
228 return ConvertUTF8ToJavaString(env, profile.guid());
231 jint PersonalDataManagerAndroid::GetCreditCardCount(JNIEnv* unused_env,
232 jobject unused_obj) {
233 return personal_data_manager_->GetCreditCards().size();
236 ScopedJavaLocalRef<jobject> PersonalDataManagerAndroid::GetCreditCardByIndex(
237 JNIEnv* env,
238 jobject unused_obj,
239 jint index) {
240 const std::vector<CreditCard*>& credit_cards =
241 personal_data_manager_->GetCreditCards();
242 size_t index_size_t = static_cast<size_t>(index);
243 DCHECK_LT(index_size_t, credit_cards.size());
244 return CreateJavaCreditCardFromNative(env, *credit_cards[index_size_t]);
247 ScopedJavaLocalRef<jobject> PersonalDataManagerAndroid::GetCreditCardByGUID(
248 JNIEnv* env,
249 jobject unused_obj,
250 jstring jguid) {
251 CreditCard* card = personal_data_manager_->GetCreditCardByGUID(
252 ConvertJavaStringToUTF8(env, jguid));
253 if (!card)
254 return ScopedJavaLocalRef<jobject>();
256 return CreateJavaCreditCardFromNative(env, *card);
259 ScopedJavaLocalRef<jstring> PersonalDataManagerAndroid::SetCreditCard(
260 JNIEnv* env,
261 jobject unused_obj,
262 jobject jcard) {
263 std::string guid = ConvertJavaStringToUTF8(
264 env,
265 Java_CreditCard_getGUID(env, jcard).obj());
267 CreditCard card;
268 PopulateNativeCreditCardFromJava(jcard, env, &card);
270 if (guid.empty()) {
271 personal_data_manager_->AddCreditCard(card);
272 } else {
273 card.set_guid(guid);
274 personal_data_manager_->UpdateCreditCard(card);
276 return ConvertUTF8ToJavaString(env, card.guid());
279 void PersonalDataManagerAndroid::RemoveByGUID(JNIEnv* env,
280 jobject unused_obj,
281 jstring jguid) {
282 personal_data_manager_->RemoveByGUID(ConvertJavaStringToUTF8(env, jguid));
285 void PersonalDataManagerAndroid::OnPersonalDataChanged() {
286 JNIEnv* env = base::android::AttachCurrentThread();
287 if (weak_java_obj_.get(env).is_null())
288 return;
290 Java_PersonalDataManager_personalDataChanged(env,
291 weak_java_obj_.get(env).obj());
294 // static
295 bool PersonalDataManagerAndroid::Register(JNIEnv* env) {
296 return RegisterNativesImpl(env);
299 // Returns whether the Autofill feature is enabled.
300 static jboolean IsAutofillEnabled(JNIEnv* env, jclass clazz) {
301 return GetPrefs()->GetBoolean(autofill::prefs::kAutofillEnabled);
304 // Enables or disables the Autofill feature.
305 static void SetAutofillEnabled(JNIEnv* env, jclass clazz, jboolean enable) {
306 GetPrefs()->SetBoolean(autofill::prefs::kAutofillEnabled, enable);
309 // Returns whether Autofill feature is managed.
310 static jboolean IsAutofillManaged(JNIEnv* env, jclass clazz) {
311 return GetPrefs()->IsManagedPreference(autofill::prefs::kAutofillEnabled);
314 // Returns an ISO 3166-1-alpha-2 country code for a |jcountry_name| using
315 // the application locale, or an empty string.
316 static jstring ToCountryCode(JNIEnv* env, jclass clazz, jstring jcountry_name) {
317 return ConvertUTF8ToJavaString(
318 env,
319 AutofillCountry::GetCountryCode(
320 base::android::ConvertJavaStringToUTF16(env, jcountry_name),
321 g_browser_process->GetApplicationLocale())).Release();
324 static jlong Init(JNIEnv* env, jobject obj) {
325 PersonalDataManagerAndroid* personal_data_manager_android =
326 new PersonalDataManagerAndroid(env, obj);
327 return reinterpret_cast<intptr_t>(personal_data_manager_android);
330 } // namespace autofill