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 // Address field types, ordered by size, from largest to smallest. | |
29 // This list must be kept in-sync with the corresponding AddressField class in | |
30 // AutofillProfileBridge.java. | |
31 enum AddressField { | |
newt (away)
2015/01/29 20:00:37
put this enum in an anonymous namespace so it can'
Evan Stade
2015/01/29 22:29:57
sorry if you already explained it, but why does th
Theresa
2015/01/29 22:31:12
Done.
Theresa
2015/01/29 22:49:42
It is duplicated. It exists because we need the ex
Evan Stade
2015/01/30 00:02:17
I think you should use ::i18n::addressinput::Addre
Theresa
2015/01/30 00:37:06
Whoever rolls libaddress could also see the build
| |
32 COUNTRY, // Country code. | |
33 ADMIN_AREA, // Administrative area such as a state, province, | |
34 // island, etc. | |
35 LOCALITY, // City or locality. | |
36 DEPENDENT_LOCALITY, // Dependent locality (may be an inner-city district or | |
37 // a suburb). | |
38 SORTING_CODE, // Sorting code. | |
39 POSTAL_CODE, // Zip or postal code. | |
40 STREET_ADDRESS, // Street address lines. | |
41 ORGANIZATION, // Organization, company, firm, institution, etc. | |
42 RECIPIENT // Name. | |
43 }; | |
44 | |
45 static jstring GetDefaultCountryCode(JNIEnv* env, | |
46 jclass clazz) { | |
47 std::string defaultCountryCode = | |
48 autofill::AutofillCountry::CountryCodeForLocale( | |
49 g_browser_process->GetApplicationLocale()); | |
50 return ConvertUTF8ToJavaString(env, defaultCountryCode).Release(); | |
51 } | |
52 | |
53 static void GetSupportedCountries(JNIEnv* env, | |
54 jclass clazz, | |
55 jobject j_country_code_list, | |
56 jobject j_country_name_list) { | |
57 std::vector<std::string> country_codes = | |
58 ::i18n::addressinput::GetRegionCodes(); | |
59 std::vector<base::string16> country_names; | |
60 std::string locale = g_browser_process->GetApplicationLocale(); | |
61 for (auto country_code : country_codes) { | |
62 country_names.push_back( | |
63 l10n_util::GetDisplayNameForCountry(country_code, | |
newt (away)
2015/01/29 20:00:37
one line?
Theresa
2015/01/29 22:31:12
Done.
| |
64 locale)); | |
65 } | |
66 | |
67 Java_AutofillProfileBridge_stringArrayToList(env, | |
68 ToJavaArrayOfStrings( | |
69 env, country_codes).obj(), | |
70 j_country_code_list); | |
71 Java_AutofillProfileBridge_stringArrayToList(env, | |
72 ToJavaArrayOfStrings( | |
73 env, country_names).obj(), | |
74 j_country_name_list); | |
75 } | |
76 | |
77 static void GetAddressUiComponents(JNIEnv* env, | |
78 jclass clazz, | |
79 jstring j_country_code, | |
80 jobject j_id_list, | |
81 jobject j_name_list) { | |
82 std::string best_language_tag; | |
83 std::string language_code; | |
newt (away)
2015/01/29 20:00:37
unused
Theresa
2015/01/29 22:31:12
Done.
| |
84 std::vector<int> component_ids; | |
85 std::vector<std::string> component_labels; | |
86 ::i18n::addressinput::Localization localization; | |
87 localization.SetGetter(l10n_util::GetStringUTF8); | |
88 | |
89 std::vector<::i18n::addressinput::AddressUiComponent> ui_components = | |
90 ::i18n::addressinput::BuildComponents( | |
91 ConvertJavaStringToUTF8(env, j_country_code), localization, | |
92 g_browser_process->GetApplicationLocale(), &best_language_tag); | |
93 | |
94 for (auto ui_component : ui_components) { | |
95 component_labels.push_back(ui_component.name); | |
96 | |
97 switch (ui_component.field) { | |
98 case ::i18n::addressinput::COUNTRY: | |
99 component_ids.push_back(AddressField::COUNTRY); | |
100 break; | |
101 case ::i18n::addressinput::ADMIN_AREA: | |
102 component_ids.push_back(AddressField::ADMIN_AREA); | |
103 break; | |
104 case ::i18n::addressinput::LOCALITY: | |
105 component_ids.push_back(AddressField::LOCALITY); | |
106 break; | |
107 case ::i18n::addressinput::DEPENDENT_LOCALITY: | |
108 component_ids.push_back(AddressField::DEPENDENT_LOCALITY); | |
109 break; | |
110 case ::i18n::addressinput::SORTING_CODE: | |
111 component_ids.push_back(AddressField::SORTING_CODE); | |
112 break; | |
113 case ::i18n::addressinput::POSTAL_CODE: | |
114 component_ids.push_back(AddressField::POSTAL_CODE); | |
115 break; | |
116 case ::i18n::addressinput::STREET_ADDRESS: | |
117 component_ids.push_back(AddressField::STREET_ADDRESS); | |
118 break; | |
119 case ::i18n::addressinput::ORGANIZATION: | |
120 component_ids.push_back(AddressField::ORGANIZATION); | |
121 break; | |
122 case ::i18n::addressinput::RECIPIENT: | |
123 component_ids.push_back(AddressField::RECIPIENT); | |
124 break; | |
125 default: | |
126 NOTREACHED(); | |
127 } | |
128 } | |
129 | |
130 Java_AutofillProfileBridge_intArrayToList(env, | |
131 ToJavaIntArray( | |
132 env, component_ids).obj(), | |
133 j_id_list); | |
134 Java_AutofillProfileBridge_stringArrayToList(env, | |
135 ToJavaArrayOfStrings( | |
136 env, component_labels).obj(), | |
137 j_name_list); | |
138 } | |
139 | |
140 // static | |
141 bool RegisterAutofillProfileBridge(JNIEnv* env) { | |
142 return RegisterNativesImpl(env); | |
143 } | |
144 | |
145 } // namespace autofill | |
OLD | NEW |