ozone: evdev: Sync caps lock LED state to evdev
[chromium-blink-merge.git] / chrome / browser / android / preferences / autofill / autofill_profile_bridge.cc
blob42903337cb004536fe7b811b3d9c51380e1eeeb0
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 jstring GetDefaultCountryCode(JNIEnv* env,
50 jclass clazz) {
51 std::string default_country_code =
52 autofill::AutofillCountry::CountryCodeForLocale(
53 g_browser_process->GetApplicationLocale());
54 return ConvertUTF8ToJavaString(env, default_country_code).Release();
57 static void GetSupportedCountries(JNIEnv* env,
58 jclass clazz,
59 jobject j_country_code_list,
60 jobject j_country_name_list) {
61 std::vector<std::string> country_codes =
62 ::i18n::addressinput::GetRegionCodes();
63 std::vector<std::string> known_country_codes;
64 std::vector<base::string16> known_country_names;
65 std::string locale = g_browser_process->GetApplicationLocale();
66 for (auto country_code : country_codes) {
67 const base::string16& country_name =
68 l10n_util::GetDisplayNameForCountry(country_code, locale);
69 // Don't display a country code for which a name is not known yet.
70 if (country_name != base::UTF8ToUTF16(country_code)) {
71 known_country_codes.push_back(country_code);
72 known_country_names.push_back(country_name);
76 Java_AutofillProfileBridge_stringArrayToList(
77 env, ToJavaArrayOfStrings(env, known_country_codes).obj(),
78 j_country_code_list);
79 Java_AutofillProfileBridge_stringArrayToList(
80 env, ToJavaArrayOfStrings(env, known_country_names).obj(),
81 j_country_name_list);
84 static jstring GetAddressUiComponents(JNIEnv* env,
85 jclass clazz,
86 jstring j_country_code,
87 jstring j_language_code,
88 jobject j_id_list,
89 jobject j_name_list) {
90 std::string best_language_tag;
91 std::vector<int> component_ids;
92 std::vector<std::string> component_labels;
93 ::i18n::addressinput::Localization localization;
94 localization.SetGetter(l10n_util::GetStringUTF8);
96 std::string language_code;
97 if (j_language_code != NULL) {
98 language_code = ConvertJavaStringToUTF8(env, j_language_code);
100 if (language_code.empty()) {
101 language_code = g_browser_process->GetApplicationLocale();
104 std::vector<::i18n::addressinput::AddressUiComponent> ui_components =
105 ::i18n::addressinput::BuildComponents(
106 ConvertJavaStringToUTF8(env, j_country_code), localization,
107 language_code, &best_language_tag);
109 for (auto ui_component : ui_components) {
110 component_labels.push_back(ui_component.name);
112 switch (ui_component.field) {
113 case ::i18n::addressinput::COUNTRY:
114 component_ids.push_back(AddressField::COUNTRY);
115 break;
116 case ::i18n::addressinput::ADMIN_AREA:
117 component_ids.push_back(AddressField::ADMIN_AREA);
118 break;
119 case ::i18n::addressinput::LOCALITY:
120 component_ids.push_back(AddressField::LOCALITY);
121 break;
122 case ::i18n::addressinput::DEPENDENT_LOCALITY:
123 component_ids.push_back(AddressField::DEPENDENT_LOCALITY);
124 break;
125 case ::i18n::addressinput::SORTING_CODE:
126 component_ids.push_back(AddressField::SORTING_CODE);
127 break;
128 case ::i18n::addressinput::POSTAL_CODE:
129 component_ids.push_back(AddressField::POSTAL_CODE);
130 break;
131 case ::i18n::addressinput::STREET_ADDRESS:
132 component_ids.push_back(AddressField::STREET_ADDRESS);
133 break;
134 case ::i18n::addressinput::ORGANIZATION:
135 component_ids.push_back(AddressField::ORGANIZATION);
136 break;
137 case ::i18n::addressinput::RECIPIENT:
138 component_ids.push_back(AddressField::RECIPIENT);
139 break;
140 default:
141 NOTREACHED();
145 Java_AutofillProfileBridge_intArrayToList(env,
146 ToJavaIntArray(
147 env, component_ids).obj(),
148 j_id_list);
149 Java_AutofillProfileBridge_stringArrayToList(env,
150 ToJavaArrayOfStrings(
151 env, component_labels).obj(),
152 j_name_list);
154 return ConvertUTF8ToJavaString(env, best_language_tag).Release();
157 // static
158 bool RegisterAutofillProfileBridge(JNIEnv* env) {
159 return RegisterNativesImpl(env);
162 } // namespace autofill