| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/android/preferences/autofill/autofill_profile_bridge.h" | 5 #include "chrome/browser/android/preferences/autofill/autofill_profile_bridge.h" |
| 6 | 6 |
| 7 #include <jni.h> | 7 #include <jni.h> |
| 8 | 8 |
| 9 #include "base/android/jni_android.h" | 9 #include "base/android/jni_android.h" |
| 10 #include "base/android/jni_array.h" | 10 #include "base/android/jni_array.h" |
| 11 #include "base/android/jni_string.h" | 11 #include "base/android/jni_string.h" |
| 12 #include "base/bind.h" | 12 #include "base/bind.h" |
| 13 #include "base/strings/utf_string_conversions.h" | 13 #include "base/strings/utf_string_conversions.h" |
| 14 #include "chrome/browser/browser_process.h" | 14 #include "chrome/browser/browser_process.h" |
| 15 #include "components/autofill/core/browser/autofill_country.h" | 15 #include "components/autofill/core/browser/autofill_country.h" |
| 16 #include "jni/AutofillProfileBridge_jni.h" | 16 #include "jni/AutofillProfileBridge_jni.h" |
| 17 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_fi
eld.h" |
| 18 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_me
tadata.h" |
| 17 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_ui
.h" | 19 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_ui
.h" |
| 18 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_ui
_component.h" | 20 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_ui
_component.h" |
| 19 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/localizati
on.h" | 21 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/localizati
on.h" |
| 20 #include "ui/base/l10n/l10n_util.h" | 22 #include "ui/base/l10n/l10n_util.h" |
| 21 | 23 |
| 24 namespace autofill { |
| 25 |
| 22 using base::android::ConvertJavaStringToUTF8; | 26 using base::android::ConvertJavaStringToUTF8; |
| 23 using base::android::ConvertUTF8ToJavaString; | 27 using base::android::ConvertUTF8ToJavaString; |
| 24 using base::android::ToJavaArrayOfStrings; | 28 using base::android::ToJavaArrayOfStrings; |
| 25 using base::android::ToJavaIntArray; | 29 using base::android::ToJavaIntArray; |
| 26 | 30 using ::i18n::addressinput::AddressField; |
| 27 namespace autofill { | 31 using ::i18n::addressinput::AddressUiComponent; |
| 28 | 32 using ::i18n::addressinput::BuildComponents; |
| 29 namespace { | 33 using ::i18n::addressinput::COUNTRY; |
| 30 | 34 using ::i18n::addressinput::GetRegionCodes; |
| 31 // Address field types. This list must be kept in-sync with the corresponding | 35 using ::i18n::addressinput::IsFieldRequired; |
| 32 // AddressField class in AutofillProfileBridge.java. | 36 using ::i18n::addressinput::Localization; |
| 33 enum AddressField { | 37 using ::i18n::addressinput::RECIPIENT; |
| 34 COUNTRY, // Country code. | |
| 35 ADMIN_AREA, // Administrative area such as a state, province, | |
| 36 // island, etc. | |
| 37 LOCALITY, // City or locality. | |
| 38 DEPENDENT_LOCALITY, // Dependent locality (may be an inner-city district or | |
| 39 // a suburb). | |
| 40 SORTING_CODE, // Sorting code. | |
| 41 POSTAL_CODE, // Zip or postal code. | |
| 42 STREET_ADDRESS, // Street address lines. | |
| 43 ORGANIZATION, // Organization, company, firm, institution, etc. | |
| 44 RECIPIENT // Name. | |
| 45 }; | |
| 46 | |
| 47 } // namespace | |
| 48 | 38 |
| 49 static ScopedJavaLocalRef<jstring> GetDefaultCountryCode( | 39 static ScopedJavaLocalRef<jstring> GetDefaultCountryCode( |
| 50 JNIEnv* env, | 40 JNIEnv* env, |
| 51 const JavaParamRef<jclass>& clazz) { | 41 const JavaParamRef<jclass>& clazz) { |
| 52 std::string default_country_code = | 42 std::string default_country_code = |
| 53 autofill::AutofillCountry::CountryCodeForLocale( | 43 autofill::AutofillCountry::CountryCodeForLocale( |
| 54 g_browser_process->GetApplicationLocale()); | 44 g_browser_process->GetApplicationLocale()); |
| 55 return ConvertUTF8ToJavaString(env, default_country_code); | 45 return ConvertUTF8ToJavaString(env, default_country_code); |
| 56 } | 46 } |
| 57 | 47 |
| 58 static void GetSupportedCountries( | 48 static void GetSupportedCountries( |
| 59 JNIEnv* env, | 49 JNIEnv* env, |
| 60 const JavaParamRef<jclass>& clazz, | 50 const JavaParamRef<jclass>& clazz, |
| 61 const JavaParamRef<jobject>& j_country_code_list, | 51 const JavaParamRef<jobject>& j_country_code_list, |
| 62 const JavaParamRef<jobject>& j_country_name_list) { | 52 const JavaParamRef<jobject>& j_country_name_list) { |
| 63 std::vector<std::string> country_codes = | 53 std::vector<std::string> country_codes = GetRegionCodes(); |
| 64 ::i18n::addressinput::GetRegionCodes(); | |
| 65 std::vector<std::string> known_country_codes; | 54 std::vector<std::string> known_country_codes; |
| 66 std::vector<base::string16> known_country_names; | 55 std::vector<base::string16> known_country_names; |
| 67 std::string locale = g_browser_process->GetApplicationLocale(); | 56 std::string locale = g_browser_process->GetApplicationLocale(); |
| 68 for (auto country_code : country_codes) { | 57 for (auto country_code : country_codes) { |
| 69 const base::string16& country_name = | 58 const base::string16& country_name = |
| 70 l10n_util::GetDisplayNameForCountry(country_code, locale); | 59 l10n_util::GetDisplayNameForCountry(country_code, locale); |
| 71 // Don't display a country code for which a name is not known yet. | 60 // Don't display a country code for which a name is not known yet. |
| 72 if (country_name != base::UTF8ToUTF16(country_code)) { | 61 if (country_name != base::UTF8ToUTF16(country_code)) { |
| 73 known_country_codes.push_back(country_code); | 62 known_country_codes.push_back(country_code); |
| 74 known_country_names.push_back(country_name); | 63 known_country_names.push_back(country_name); |
| 75 } | 64 } |
| 76 } | 65 } |
| 77 | 66 |
| 78 Java_AutofillProfileBridge_stringArrayToList( | 67 Java_AutofillProfileBridge_stringArrayToList( |
| 79 env, ToJavaArrayOfStrings(env, known_country_codes).obj(), | 68 env, ToJavaArrayOfStrings(env, known_country_codes).obj(), |
| 80 j_country_code_list); | 69 j_country_code_list); |
| 81 Java_AutofillProfileBridge_stringArrayToList( | 70 Java_AutofillProfileBridge_stringArrayToList( |
| 82 env, ToJavaArrayOfStrings(env, known_country_names).obj(), | 71 env, ToJavaArrayOfStrings(env, known_country_names).obj(), |
| 83 j_country_name_list); | 72 j_country_name_list); |
| 84 } | 73 } |
| 85 | 74 |
| 75 static void GetRequiredFields( |
| 76 JNIEnv* env, |
| 77 const JavaParamRef<jclass>& clazz, |
| 78 const JavaParamRef<jstring>& j_country_code, |
| 79 const JavaParamRef<jobject>& j_required_fields_list) { |
| 80 std::string country_code = ConvertJavaStringToUTF8(env, j_country_code); |
| 81 std::vector<int> required; |
| 82 |
| 83 // Should iterate over all fields in: |
| 84 // third_party/libaddressinput/src/cpp/include/libaddressinput/address_field.h |
| 85 for (int i = COUNTRY; i <= RECIPIENT; ++i) { |
| 86 AddressField field = static_cast<AddressField>(i); |
| 87 if (IsFieldRequired(field, country_code)) { |
| 88 required.push_back(field); |
| 89 } |
| 90 } |
| 91 |
| 92 Java_AutofillProfileBridge_intArrayToList( |
| 93 env, ToJavaIntArray(env, required).obj(), j_required_fields_list); |
| 94 } |
| 95 |
| 86 static ScopedJavaLocalRef<jstring> GetAddressUiComponents( | 96 static ScopedJavaLocalRef<jstring> GetAddressUiComponents( |
| 87 JNIEnv* env, | 97 JNIEnv* env, |
| 88 const JavaParamRef<jclass>& clazz, | 98 const JavaParamRef<jclass>& clazz, |
| 89 const JavaParamRef<jstring>& j_country_code, | 99 const JavaParamRef<jstring>& j_country_code, |
| 90 const JavaParamRef<jstring>& j_language_code, | 100 const JavaParamRef<jstring>& j_language_code, |
| 91 const JavaParamRef<jobject>& j_id_list, | 101 const JavaParamRef<jobject>& j_id_list, |
| 92 const JavaParamRef<jobject>& j_name_list) { | 102 const JavaParamRef<jobject>& j_name_list, |
| 103 const JavaParamRef<jobject>& j_required_list, |
| 104 const JavaParamRef<jobject>& j_length_list) { |
| 93 std::string best_language_tag; | 105 std::string best_language_tag; |
| 94 std::vector<int> component_ids; | 106 std::vector<int> component_ids; |
| 95 std::vector<std::string> component_labels; | 107 std::vector<std::string> component_labels; |
| 96 ::i18n::addressinput::Localization localization; | 108 std::vector<int> component_required; |
| 109 std::vector<int> component_length; |
| 110 Localization localization; |
| 97 localization.SetGetter(l10n_util::GetStringUTF8); | 111 localization.SetGetter(l10n_util::GetStringUTF8); |
| 98 | 112 |
| 99 std::string language_code; | 113 std::string language_code; |
| 100 if (j_language_code != NULL) { | 114 if (j_language_code != NULL) { |
| 101 language_code = ConvertJavaStringToUTF8(env, j_language_code); | 115 language_code = ConvertJavaStringToUTF8(env, j_language_code); |
| 102 } | 116 } |
| 103 if (language_code.empty()) { | 117 if (language_code.empty()) { |
| 104 language_code = g_browser_process->GetApplicationLocale(); | 118 language_code = g_browser_process->GetApplicationLocale(); |
| 105 } | 119 } |
| 106 | 120 |
| 107 std::vector<::i18n::addressinput::AddressUiComponent> ui_components = | 121 std::string country_code = ConvertJavaStringToUTF8(env, j_country_code); |
| 108 ::i18n::addressinput::BuildComponents( | 122 std::vector<AddressUiComponent> ui_components = BuildComponents( |
| 109 ConvertJavaStringToUTF8(env, j_country_code), localization, | 123 country_code, localization, language_code, &best_language_tag); |
| 110 language_code, &best_language_tag); | |
| 111 | 124 |
| 112 for (auto ui_component : ui_components) { | 125 for (const auto& ui_component : ui_components) { |
| 113 component_labels.push_back(ui_component.name); | 126 component_labels.push_back(ui_component.name); |
| 114 | 127 component_required.push_back( |
| 115 switch (ui_component.field) { | 128 IsFieldRequired(ui_component.field, country_code)); |
| 116 case ::i18n::addressinput::COUNTRY: | 129 component_length.push_back(ui_component.length_hint == |
| 117 component_ids.push_back(AddressField::COUNTRY); | 130 AddressUiComponent::HINT_LONG); |
| 118 break; | 131 component_ids.push_back(ui_component.field); |
| 119 case ::i18n::addressinput::ADMIN_AREA: | |
| 120 component_ids.push_back(AddressField::ADMIN_AREA); | |
| 121 break; | |
| 122 case ::i18n::addressinput::LOCALITY: | |
| 123 component_ids.push_back(AddressField::LOCALITY); | |
| 124 break; | |
| 125 case ::i18n::addressinput::DEPENDENT_LOCALITY: | |
| 126 component_ids.push_back(AddressField::DEPENDENT_LOCALITY); | |
| 127 break; | |
| 128 case ::i18n::addressinput::SORTING_CODE: | |
| 129 component_ids.push_back(AddressField::SORTING_CODE); | |
| 130 break; | |
| 131 case ::i18n::addressinput::POSTAL_CODE: | |
| 132 component_ids.push_back(AddressField::POSTAL_CODE); | |
| 133 break; | |
| 134 case ::i18n::addressinput::STREET_ADDRESS: | |
| 135 component_ids.push_back(AddressField::STREET_ADDRESS); | |
| 136 break; | |
| 137 case ::i18n::addressinput::ORGANIZATION: | |
| 138 component_ids.push_back(AddressField::ORGANIZATION); | |
| 139 break; | |
| 140 case ::i18n::addressinput::RECIPIENT: | |
| 141 component_ids.push_back(AddressField::RECIPIENT); | |
| 142 break; | |
| 143 default: | |
| 144 NOTREACHED(); | |
| 145 } | |
| 146 } | 132 } |
| 147 | 133 |
| 148 Java_AutofillProfileBridge_intArrayToList(env, | 134 Java_AutofillProfileBridge_intArrayToList( |
| 149 ToJavaIntArray( | 135 env, ToJavaIntArray(env, component_ids).obj(), j_id_list); |
| 150 env, component_ids).obj(), | 136 Java_AutofillProfileBridge_stringArrayToList( |
| 151 j_id_list); | 137 env, ToJavaArrayOfStrings(env, component_labels).obj(), j_name_list); |
| 152 Java_AutofillProfileBridge_stringArrayToList(env, | 138 Java_AutofillProfileBridge_intArrayToList( |
| 153 ToJavaArrayOfStrings( | 139 env, ToJavaIntArray(env, component_required).obj(), j_required_list); |
| 154 env, component_labels).obj(), | 140 Java_AutofillProfileBridge_intArrayToList( |
| 155 j_name_list); | 141 env, ToJavaIntArray(env, component_length).obj(), j_length_list); |
| 156 | 142 |
| 157 return ConvertUTF8ToJavaString(env, best_language_tag); | 143 return ConvertUTF8ToJavaString(env, best_language_tag); |
| 158 } | 144 } |
| 159 | 145 |
| 160 // static | 146 // static |
| 161 bool RegisterAutofillProfileBridge(JNIEnv* env) { | 147 bool RegisterAutofillProfileBridge(JNIEnv* env) { |
| 162 return RegisterNativesImpl(env); | 148 return RegisterNativesImpl(env); |
| 163 } | 149 } |
| 164 | 150 |
| 165 } // namespace autofill | 151 } // namespace autofill |
| OLD | NEW |