Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / android / preferences / autofill / autofill_profile_bridge.cc
blob96c6810ba19fba770069300da356231f4311fca7
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.
5 #include "chrome/browser/android/preferences/autofill/autofill_profile_bridge.h"
7 #include <jni.h>
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 "base/strings/utf_string_conversions.h"
14 #include "chrome/browser/browser_process.h"
15 #include "components/autofill/core/browser/autofill_country.h"
16 #include "jni/AutofillProfileBridge_jni.h"
17 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_ui.h"
18 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_ui_component.h"
19 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/localization.h"
20 #include "ui/base/l10n/l10n_util.h"
22 using base::android::ConvertJavaStringToUTF8;
23 using base::android::ConvertUTF8ToJavaString;
24 using base::android::ToJavaArrayOfStrings;
25 using base::android::ToJavaIntArray;
27 namespace autofill {
29 namespace {
31 // Address field types. This list must be kept in-sync with the corresponding
32 // AddressField class in AutofillProfileBridge.java.
33 enum AddressField {
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.
47 } // namespace
49 static ScopedJavaLocalRef<jstring> GetDefaultCountryCode(
50 JNIEnv* env,
51 const JavaParamRef<jclass>& clazz) {
52 std::string default_country_code =
53 autofill::AutofillCountry::CountryCodeForLocale(
54 g_browser_process->GetApplicationLocale());
55 return ConvertUTF8ToJavaString(env, default_country_code);
58 static void GetSupportedCountries(
59 JNIEnv* env,
60 const JavaParamRef<jclass>& clazz,
61 const JavaParamRef<jobject>& j_country_code_list,
62 const JavaParamRef<jobject>& j_country_name_list) {
63 std::vector<std::string> country_codes =
64 ::i18n::addressinput::GetRegionCodes();
65 std::vector<std::string> known_country_codes;
66 std::vector<base::string16> known_country_names;
67 std::string locale = g_browser_process->GetApplicationLocale();
68 for (auto country_code : country_codes) {
69 const base::string16& country_name =
70 l10n_util::GetDisplayNameForCountry(country_code, locale);
71 // Don't display a country code for which a name is not known yet.
72 if (country_name != base::UTF8ToUTF16(country_code)) {
73 known_country_codes.push_back(country_code);
74 known_country_names.push_back(country_name);
78 Java_AutofillProfileBridge_stringArrayToList(
79 env, ToJavaArrayOfStrings(env, known_country_codes).obj(),
80 j_country_code_list);
81 Java_AutofillProfileBridge_stringArrayToList(
82 env, ToJavaArrayOfStrings(env, known_country_names).obj(),
83 j_country_name_list);
86 static ScopedJavaLocalRef<jstring> GetAddressUiComponents(
87 JNIEnv* env,
88 const JavaParamRef<jclass>& clazz,
89 const JavaParamRef<jstring>& j_country_code,
90 const JavaParamRef<jstring>& j_language_code,
91 const JavaParamRef<jobject>& j_id_list,
92 const JavaParamRef<jobject>& j_name_list) {
93 std::string best_language_tag;
94 std::vector<int> component_ids;
95 std::vector<std::string> component_labels;
96 ::i18n::addressinput::Localization localization;
97 localization.SetGetter(l10n_util::GetStringUTF8);
99 std::string language_code;
100 if (j_language_code != NULL) {
101 language_code = ConvertJavaStringToUTF8(env, j_language_code);
103 if (language_code.empty()) {
104 language_code = g_browser_process->GetApplicationLocale();
107 std::vector<::i18n::addressinput::AddressUiComponent> ui_components =
108 ::i18n::addressinput::BuildComponents(
109 ConvertJavaStringToUTF8(env, j_country_code), localization,
110 language_code, &best_language_tag);
112 for (auto ui_component : ui_components) {
113 component_labels.push_back(ui_component.name);
115 switch (ui_component.field) {
116 case ::i18n::addressinput::COUNTRY:
117 component_ids.push_back(AddressField::COUNTRY);
118 break;
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();
148 Java_AutofillProfileBridge_intArrayToList(env,
149 ToJavaIntArray(
150 env, component_ids).obj(),
151 j_id_list);
152 Java_AutofillProfileBridge_stringArrayToList(env,
153 ToJavaArrayOfStrings(
154 env, component_labels).obj(),
155 j_name_list);
157 return ConvertUTF8ToJavaString(env, best_language_tag);
160 // static
161 bool RegisterAutofillProfileBridge(JNIEnv* env) {
162 return RegisterNativesImpl(env);
165 } // namespace autofill