[Cronet] Delay StartNetLog and StopNetLog until native request context is initialized
[chromium-blink-merge.git] / chrome / browser / autofill / android / personal_data_manager_android.cc
blobd0ef526670601020f76f3132eeb01047213523f8
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(
82 Java_AutofillProfile_getFullName(env, jprofile)),
83 g_browser_process->GetApplicationLocale());
84 profile->SetInfo(
85 AutofillType(COMPANY_NAME),
86 ConvertJavaStringToUTF16(
87 Java_AutofillProfile_getCompanyName(env, jprofile)),
88 g_browser_process->GetApplicationLocale());
89 profile->SetInfo(
90 AutofillType(ADDRESS_HOME_STREET_ADDRESS),
91 ConvertJavaStringToUTF16(
92 Java_AutofillProfile_getStreetAddress(env, jprofile)),
93 g_browser_process->GetApplicationLocale());
94 profile->SetInfo(
95 AutofillType(ADDRESS_HOME_STATE),
96 ConvertJavaStringToUTF16(
97 Java_AutofillProfile_getRegion(env, jprofile)),
98 g_browser_process->GetApplicationLocale());
99 profile->SetInfo(
100 AutofillType(ADDRESS_HOME_CITY),
101 ConvertJavaStringToUTF16(
102 Java_AutofillProfile_getLocality(env, jprofile)),
103 g_browser_process->GetApplicationLocale());
104 profile->SetInfo(
105 AutofillType(ADDRESS_HOME_DEPENDENT_LOCALITY),
106 ConvertJavaStringToUTF16(
107 Java_AutofillProfile_getDependentLocality(env, jprofile)),
108 g_browser_process->GetApplicationLocale());
109 profile->SetInfo(
110 AutofillType(ADDRESS_HOME_ZIP),
111 ConvertJavaStringToUTF16(
112 Java_AutofillProfile_getPostalCode(env, jprofile)),
113 g_browser_process->GetApplicationLocale());
114 profile->SetInfo(
115 AutofillType(ADDRESS_HOME_SORTING_CODE),
116 ConvertJavaStringToUTF16(
117 Java_AutofillProfile_getSortingCode(env, jprofile)),
118 g_browser_process->GetApplicationLocale());
119 profile->SetInfo(AutofillType(ADDRESS_HOME_COUNTRY),
120 ConvertJavaStringToUTF16(
121 Java_AutofillProfile_getCountryCode(env, jprofile)),
122 g_browser_process->GetApplicationLocale());
123 profile->SetInfo(
124 AutofillType(PHONE_HOME_WHOLE_NUMBER),
125 ConvertJavaStringToUTF16(
126 Java_AutofillProfile_getPhoneNumber(env, jprofile)),
127 g_browser_process->GetApplicationLocale());
128 profile->SetInfo(
129 AutofillType(EMAIL_ADDRESS),
130 ConvertJavaStringToUTF16(
131 Java_AutofillProfile_getEmailAddress(env, jprofile)),
132 g_browser_process->GetApplicationLocale());
133 profile->set_language_code(
134 ConvertJavaStringToUTF8(
135 Java_AutofillProfile_getLanguageCode(env, jprofile)));
138 ScopedJavaLocalRef<jobject> CreateJavaCreditCardFromNative(
139 JNIEnv* env,
140 const CreditCard& card) {
141 return Java_CreditCard_create(
142 env, ConvertUTF8ToJavaString(env, card.guid()).obj(),
143 ConvertUTF8ToJavaString(env, card.origin()).obj(),
144 card.record_type() == CreditCard::LOCAL_CARD,
145 card.record_type() == CreditCard::FULL_SERVER_CARD,
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(env, card.GetRawInfo(CREDIT_CARD_EXP_MONTH))
150 .obj(),
151 ConvertUTF16ToJavaString(
152 env, card.GetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR)).obj());
155 void PopulateNativeCreditCardFromJava(
156 const jobject& jcard,
157 JNIEnv* env,
158 CreditCard* card) {
159 card->set_origin(
160 ConvertJavaStringToUTF8(Java_CreditCard_getOrigin(env, jcard)));
161 card->SetRawInfo(
162 CREDIT_CARD_NAME,
163 ConvertJavaStringToUTF16(Java_CreditCard_getName(env, jcard)));
164 card->SetRawInfo(
165 CREDIT_CARD_NUMBER,
166 ConvertJavaStringToUTF16(Java_CreditCard_getNumber(env, jcard)));
167 card->SetRawInfo(
168 CREDIT_CARD_EXP_MONTH,
169 ConvertJavaStringToUTF16(Java_CreditCard_getMonth(env, jcard)));
170 card->SetRawInfo(
171 CREDIT_CARD_EXP_4_DIGIT_YEAR,
172 ConvertJavaStringToUTF16(Java_CreditCard_getYear(env, jcard)));
175 } // namespace
177 PersonalDataManagerAndroid::PersonalDataManagerAndroid(JNIEnv* env,
178 jobject obj)
179 : weak_java_obj_(env, obj),
180 personal_data_manager_(PersonalDataManagerFactory::GetForProfile(
181 ProfileManager::GetActiveUserProfile())) {
182 personal_data_manager_->AddObserver(this);
185 PersonalDataManagerAndroid::~PersonalDataManagerAndroid() {
186 personal_data_manager_->RemoveObserver(this);
189 jint PersonalDataManagerAndroid::GetProfileCount(JNIEnv* unused_env,
190 jobject unused_obj) {
191 return personal_data_manager_->GetProfiles().size();
194 ScopedJavaLocalRef<jobject> PersonalDataManagerAndroid::GetProfileByIndex(
195 JNIEnv* env,
196 jobject unused_obj,
197 jint index) {
198 const std::vector<AutofillProfile*>& profiles =
199 personal_data_manager_->GetProfiles();
200 size_t index_size_t = static_cast<size_t>(index);
201 DCHECK_LT(index_size_t, profiles.size());
202 return CreateJavaProfileFromNative(env, *profiles[index_size_t]);
205 ScopedJavaLocalRef<jobject> PersonalDataManagerAndroid::GetProfileByGUID(
206 JNIEnv* env,
207 jobject unused_obj,
208 jstring jguid) {
209 AutofillProfile* profile = personal_data_manager_->GetProfileByGUID(
210 ConvertJavaStringToUTF8(env, jguid));
211 if (!profile)
212 return ScopedJavaLocalRef<jobject>();
214 return CreateJavaProfileFromNative(env, *profile);
217 ScopedJavaLocalRef<jstring> PersonalDataManagerAndroid::SetProfile(
218 JNIEnv* env,
219 jobject unused_obj,
220 jobject jprofile) {
221 std::string guid = ConvertJavaStringToUTF8(
222 env,
223 Java_AutofillProfile_getGUID(env, jprofile).obj());
225 AutofillProfile profile;
226 PopulateNativeProfileFromJava(jprofile, env, &profile);
228 if (guid.empty()) {
229 personal_data_manager_->AddProfile(profile);
230 } else {
231 profile.set_guid(guid);
232 personal_data_manager_->UpdateProfile(profile);
235 return ConvertUTF8ToJavaString(env, profile.guid());
238 ScopedJavaLocalRef<jobjectArray> PersonalDataManagerAndroid::GetProfileLabels(
239 JNIEnv* env,
240 jobject unused_obj) {
241 std::vector<base::string16> labels;
242 AutofillProfile::CreateInferredLabels(
243 personal_data_manager_->GetProfiles(), NULL, NAME_FULL, 2,
244 g_browser_process->GetApplicationLocale(), &labels);
246 return base::android::ToJavaArrayOfStrings(env, labels);
249 jint PersonalDataManagerAndroid::GetCreditCardCount(JNIEnv* unused_env,
250 jobject unused_obj) {
251 return personal_data_manager_->GetCreditCards().size();
254 ScopedJavaLocalRef<jobject> PersonalDataManagerAndroid::GetCreditCardByIndex(
255 JNIEnv* env,
256 jobject unused_obj,
257 jint index) {
258 const std::vector<CreditCard*>& credit_cards =
259 personal_data_manager_->GetCreditCards();
260 size_t index_size_t = static_cast<size_t>(index);
261 DCHECK_LT(index_size_t, credit_cards.size());
262 return CreateJavaCreditCardFromNative(env, *credit_cards[index_size_t]);
265 ScopedJavaLocalRef<jobject> PersonalDataManagerAndroid::GetCreditCardByGUID(
266 JNIEnv* env,
267 jobject unused_obj,
268 jstring jguid) {
269 CreditCard* card = personal_data_manager_->GetCreditCardByGUID(
270 ConvertJavaStringToUTF8(env, jguid));
271 if (!card)
272 return ScopedJavaLocalRef<jobject>();
274 return CreateJavaCreditCardFromNative(env, *card);
277 ScopedJavaLocalRef<jstring> PersonalDataManagerAndroid::SetCreditCard(
278 JNIEnv* env,
279 jobject unused_obj,
280 jobject jcard) {
281 std::string guid = ConvertJavaStringToUTF8(
282 env,
283 Java_CreditCard_getGUID(env, jcard).obj());
285 CreditCard card;
286 PopulateNativeCreditCardFromJava(jcard, env, &card);
288 if (guid.empty()) {
289 personal_data_manager_->AddCreditCard(card);
290 } else {
291 card.set_guid(guid);
292 personal_data_manager_->UpdateCreditCard(card);
294 return ConvertUTF8ToJavaString(env, card.guid());
297 void PersonalDataManagerAndroid::RemoveByGUID(JNIEnv* env,
298 jobject unused_obj,
299 jstring jguid) {
300 personal_data_manager_->RemoveByGUID(ConvertJavaStringToUTF8(env, jguid));
303 void PersonalDataManagerAndroid::ClearUnmaskedCache(JNIEnv* env,
304 jobject unused_obj,
305 jstring guid) {
306 personal_data_manager_->ResetFullServerCard(
307 ConvertJavaStringToUTF8(env, guid));
310 void PersonalDataManagerAndroid::OnPersonalDataChanged() {
311 JNIEnv* env = base::android::AttachCurrentThread();
312 if (weak_java_obj_.get(env).is_null())
313 return;
315 Java_PersonalDataManager_personalDataChanged(env,
316 weak_java_obj_.get(env).obj());
319 // static
320 bool PersonalDataManagerAndroid::Register(JNIEnv* env) {
321 return RegisterNativesImpl(env);
324 // Returns whether the Autofill feature is enabled.
325 static jboolean IsAutofillEnabled(JNIEnv* env, jclass clazz) {
326 return GetPrefs()->GetBoolean(autofill::prefs::kAutofillEnabled);
329 // Enables or disables the Autofill feature.
330 static void SetAutofillEnabled(JNIEnv* env, jclass clazz, jboolean enable) {
331 GetPrefs()->SetBoolean(autofill::prefs::kAutofillEnabled, enable);
334 // Returns whether the Autofill feature is managed.
335 static jboolean IsAutofillManaged(JNIEnv* env, jclass clazz) {
336 return GetPrefs()->IsManagedPreference(autofill::prefs::kAutofillEnabled);
339 // Returns whether the Wallet import feature is available.
340 static jboolean IsWalletImportFeatureAvailable(JNIEnv* env, jclass clazz) {
341 return WalletIntegrationAvailableForProfile(GetProfile());
344 // Returns whether the Wallet import feature is enabled.
345 static jboolean IsWalletImportEnabled(JNIEnv* env, jclass clazz) {
346 return GetPrefs()->GetBoolean(autofill::prefs::kAutofillWalletImportEnabled);
349 // Enables or disables the Wallet import feature.
350 static void SetWalletImportEnabled(JNIEnv* env, jclass clazz, jboolean enable) {
351 GetPrefs()->SetBoolean(autofill::prefs::kAutofillWalletImportEnabled, enable);
354 // Returns an ISO 3166-1-alpha-2 country code for a |jcountry_name| using
355 // the application locale, or an empty string.
356 static jstring ToCountryCode(JNIEnv* env, jclass clazz, jstring jcountry_name) {
357 return ConvertUTF8ToJavaString(
358 env,
359 AutofillCountry::GetCountryCode(
360 base::android::ConvertJavaStringToUTF16(env, jcountry_name),
361 g_browser_process->GetApplicationLocale())).Release();
364 static jlong Init(JNIEnv* env, jobject obj) {
365 PersonalDataManagerAndroid* personal_data_manager_android =
366 new PersonalDataManagerAndroid(env, obj);
367 return reinterpret_cast<intptr_t>(personal_data_manager_android);
370 } // namespace autofill