OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/android/preferences/autofill/autofill_profile_bridge.h" |
| 6 |
| 7 #include <jni.h> |
| 8 |
| 9 #include "base/android/jni_android.h" |
| 10 #include "base/android/jni_array.h" |
| 11 #include "base/android/jni_string.h" |
| 12 #include "base/bind.h" |
| 13 #include "chrome/browser/browser_process.h" |
| 14 #include "components/autofill/core/browser/autofill_country.h" |
| 15 #include "jni/AutofillProfileBridge_jni.h" |
| 16 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_ui
.h" |
| 17 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_ui
_component.h" |
| 18 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/localizati
on.h" |
| 19 #include "ui/base/l10n/l10n_util.h" |
| 20 |
| 21 using base::android::ConvertJavaStringToUTF8; |
| 22 using base::android::ConvertUTF8ToJavaString; |
| 23 using base::android::ToJavaArrayOfStrings; |
| 24 using base::android::ToJavaIntArray; |
| 25 |
| 26 namespace autofill { |
| 27 |
| 28 namespace { |
| 29 |
| 30 // Address field types. This list must be kept in-sync with the corresponding |
| 31 // AddressField class in AutofillProfileBridge.java. |
| 32 enum AddressField { |
| 33 COUNTRY, // Country code. |
| 34 ADMIN_AREA, // Administrative area such as a state, province, |
| 35 // island, etc. |
| 36 LOCALITY, // City or locality. |
| 37 DEPENDENT_LOCALITY, // Dependent locality (may be an inner-city district or |
| 38 // a suburb). |
| 39 SORTING_CODE, // Sorting code. |
| 40 POSTAL_CODE, // Zip or postal code. |
| 41 STREET_ADDRESS, // Street address lines. |
| 42 ORGANIZATION, // Organization, company, firm, institution, etc. |
| 43 RECIPIENT // Name. |
| 44 }; |
| 45 |
| 46 } // namespace |
| 47 |
| 48 static jstring GetDefaultCountryCode(JNIEnv* env, |
| 49 jclass clazz) { |
| 50 std::string defaultCountryCode = |
| 51 autofill::AutofillCountry::CountryCodeForLocale( |
| 52 g_browser_process->GetApplicationLocale()); |
| 53 return ConvertUTF8ToJavaString(env, defaultCountryCode).Release(); |
| 54 } |
| 55 |
| 56 static void GetSupportedCountries(JNIEnv* env, |
| 57 jclass clazz, |
| 58 jobject j_country_code_list, |
| 59 jobject j_country_name_list) { |
| 60 std::vector<std::string> country_codes = |
| 61 ::i18n::addressinput::GetRegionCodes(); |
| 62 std::vector<base::string16> country_names; |
| 63 std::string locale = g_browser_process->GetApplicationLocale(); |
| 64 for (auto country_code : country_codes) { |
| 65 country_names.push_back(l10n_util::GetDisplayNameForCountry(country_code, |
| 66 locale)); |
| 67 } |
| 68 |
| 69 Java_AutofillProfileBridge_stringArrayToList(env, |
| 70 ToJavaArrayOfStrings( |
| 71 env, country_codes).obj(), |
| 72 j_country_code_list); |
| 73 Java_AutofillProfileBridge_stringArrayToList(env, |
| 74 ToJavaArrayOfStrings( |
| 75 env, country_names).obj(), |
| 76 j_country_name_list); |
| 77 } |
| 78 |
| 79 static void GetAddressUiComponents(JNIEnv* env, |
| 80 jclass clazz, |
| 81 jstring j_country_code, |
| 82 jobject j_id_list, |
| 83 jobject j_name_list) { |
| 84 std::string best_language_tag; |
| 85 std::vector<int> component_ids; |
| 86 std::vector<std::string> component_labels; |
| 87 ::i18n::addressinput::Localization localization; |
| 88 localization.SetGetter(l10n_util::GetStringUTF8); |
| 89 |
| 90 std::vector<::i18n::addressinput::AddressUiComponent> ui_components = |
| 91 ::i18n::addressinput::BuildComponents( |
| 92 ConvertJavaStringToUTF8(env, j_country_code), localization, |
| 93 g_browser_process->GetApplicationLocale(), &best_language_tag); |
| 94 |
| 95 for (auto ui_component : ui_components) { |
| 96 component_labels.push_back(ui_component.name); |
| 97 |
| 98 switch (ui_component.field) { |
| 99 case ::i18n::addressinput::COUNTRY: |
| 100 component_ids.push_back(AddressField::COUNTRY); |
| 101 break; |
| 102 case ::i18n::addressinput::ADMIN_AREA: |
| 103 component_ids.push_back(AddressField::ADMIN_AREA); |
| 104 break; |
| 105 case ::i18n::addressinput::LOCALITY: |
| 106 component_ids.push_back(AddressField::LOCALITY); |
| 107 break; |
| 108 case ::i18n::addressinput::DEPENDENT_LOCALITY: |
| 109 component_ids.push_back(AddressField::DEPENDENT_LOCALITY); |
| 110 break; |
| 111 case ::i18n::addressinput::SORTING_CODE: |
| 112 component_ids.push_back(AddressField::SORTING_CODE); |
| 113 break; |
| 114 case ::i18n::addressinput::POSTAL_CODE: |
| 115 component_ids.push_back(AddressField::POSTAL_CODE); |
| 116 break; |
| 117 case ::i18n::addressinput::STREET_ADDRESS: |
| 118 component_ids.push_back(AddressField::STREET_ADDRESS); |
| 119 break; |
| 120 case ::i18n::addressinput::ORGANIZATION: |
| 121 component_ids.push_back(AddressField::ORGANIZATION); |
| 122 break; |
| 123 case ::i18n::addressinput::RECIPIENT: |
| 124 component_ids.push_back(AddressField::RECIPIENT); |
| 125 break; |
| 126 default: |
| 127 NOTREACHED(); |
| 128 } |
| 129 } |
| 130 |
| 131 Java_AutofillProfileBridge_intArrayToList(env, |
| 132 ToJavaIntArray( |
| 133 env, component_ids).obj(), |
| 134 j_id_list); |
| 135 Java_AutofillProfileBridge_stringArrayToList(env, |
| 136 ToJavaArrayOfStrings( |
| 137 env, component_labels).obj(), |
| 138 j_name_list); |
| 139 } |
| 140 |
| 141 // static |
| 142 bool RegisterAutofillProfileBridge(JNIEnv* env) { |
| 143 return RegisterNativesImpl(env); |
| 144 } |
| 145 |
| 146 } // namespace autofill |
OLD | NEW |