OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/ui/autofill/autofill_dialog_i18n_input.h" | 5 #include "chrome/browser/ui/autofill/autofill_dialog_i18n_input.h" |
6 | 6 |
7 #include "base/macros.h" | |
7 #include "base/strings/string_split.h" | 8 #include "base/strings/string_split.h" |
8 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
9 #include "chrome/browser/browser_process.h" | 10 #include "chrome/browser/browser_process.h" |
10 #include "components/autofill/core/browser/address_i18n.h" | 11 #include "components/autofill/core/browser/address_i18n.h" |
11 #include "components/autofill/core/browser/autofill_profile.h" | 12 #include "components/autofill/core/browser/autofill_profile.h" |
12 #include "components/autofill/core/browser/credit_card.h" | 13 #include "components/autofill/core/browser/credit_card.h" |
13 #include "components/autofill/core/browser/field_types.h" | 14 #include "components/autofill/core/browser/field_types.h" |
14 #include "grit/components_strings.h" | 15 #include "grit/components_strings.h" |
15 #include "third_party/libaddressinput/chromium/cpp/include/libaddressinput/addre ss_data.h" | 16 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_da ta.h" |
16 #include "third_party/libaddressinput/chromium/cpp/include/libaddressinput/addre ss_field.h" | 17 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_fi eld.h" |
17 #include "third_party/libaddressinput/chromium/cpp/include/libaddressinput/addre ss_ui.h" | 18 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_me tadata.h" |
18 #include "third_party/libaddressinput/chromium/cpp/include/libaddressinput/addre ss_ui_component.h" | 19 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_ui .h" |
20 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_ui _component.h" | |
21 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/localizati on.h" | |
19 #include "ui/base/l10n/l10n_util.h" | 22 #include "ui/base/l10n/l10n_util.h" |
20 | 23 |
21 namespace autofill { | 24 namespace autofill { |
22 namespace i18ninput { | 25 namespace i18ninput { |
23 | 26 |
24 namespace { | 27 namespace { |
25 | 28 |
26 using base::UTF16ToUTF8; | 29 using base::UTF16ToUTF8; |
27 using ::i18n::addressinput::AddressData; | 30 using ::i18n::addressinput::AddressData; |
28 using ::i18n::addressinput::AddressField; | 31 using ::i18n::addressinput::AddressField; |
29 using ::i18n::addressinput::AddressUiComponent; | 32 using ::i18n::addressinput::AddressUiComponent; |
30 | 33 |
31 DetailInput::Length LengthFromHint(AddressUiComponent::LengthHint hint) { | 34 DetailInput::Length LengthFromHint(AddressUiComponent::LengthHint hint) { |
32 if (hint == AddressUiComponent::HINT_SHORT) | 35 if (hint == AddressUiComponent::HINT_SHORT) |
33 return DetailInput::SHORT; | 36 return DetailInput::SHORT; |
34 DCHECK_EQ(hint, AddressUiComponent::HINT_LONG); | 37 DCHECK_EQ(hint, AddressUiComponent::HINT_LONG); |
35 return DetailInput::LONG; | 38 return DetailInput::LONG; |
36 } | 39 } |
37 | 40 |
38 } // namespace | 41 } // namespace |
39 | 42 |
40 void BuildAddressInputs(common::AddressType address_type, | 43 void BuildAddressInputs(common::AddressType address_type, |
41 const std::string& country_code, | 44 const std::string& country_code, |
42 DetailInputs* inputs, | 45 DetailInputs* inputs, |
43 std::string* language_code) { | 46 std::string* language_code) { |
47 DCHECK(inputs); | |
48 DCHECK(language_code); | |
49 | |
50 ::i18n::addressinput::Localization localization; | |
51 localization.SetGetter( | |
52 l10n_util::GetStringUTF8, g_browser_process->GetApplicationLocale()); | |
44 std::vector<AddressUiComponent> components( | 53 std::vector<AddressUiComponent> components( |
45 ::i18n::addressinput::BuildComponents( | 54 ::i18n::addressinput::BuildComponents( |
46 country_code, g_browser_process->GetApplicationLocale(), | 55 country_code, localization, language_code)); |
47 language_code)); | |
48 | 56 |
49 const bool billing = address_type == common::ADDRESS_TYPE_BILLING; | 57 const bool billing = address_type == common::ADDRESS_TYPE_BILLING; |
50 | 58 |
51 for (size_t i = 0; i < components.size(); ++i) { | 59 for (size_t i = 0; i < components.size(); ++i) { |
52 const AddressUiComponent& component = components[i]; | 60 const AddressUiComponent& component = components[i]; |
53 if (component.field == ::i18n::addressinput::ORGANIZATION) { | |
54 // TODO(dbeam): figure out when we actually need this. | |
55 continue; | |
56 } | |
57 | |
58 ServerFieldType server_type = TypeForField(component.field, address_type); | 61 ServerFieldType server_type = TypeForField(component.field, address_type); |
59 DetailInput::Length length = LengthFromHint(component.length_hint); | 62 DetailInput::Length length = LengthFromHint(component.length_hint); |
60 base::string16 placeholder = l10n_util::GetStringUTF16(component.name_id); | 63 base::string16 placeholder = base::UTF8ToUTF16(component.name); |
61 DetailInput input = { length, server_type, placeholder }; | 64 DetailInput input = { length, server_type, placeholder }; |
62 inputs->push_back(input); | 65 inputs->push_back(input); |
63 } | 66 } |
64 | 67 |
65 ServerFieldType server_type = | 68 ServerFieldType server_type = |
66 billing ? ADDRESS_BILLING_COUNTRY : ADDRESS_HOME_COUNTRY; | 69 billing ? ADDRESS_BILLING_COUNTRY : ADDRESS_HOME_COUNTRY; |
67 base::string16 placeholder_text = | 70 base::string16 placeholder_text = |
68 l10n_util::GetStringUTF16(IDS_AUTOFILL_FIELD_LABEL_COUNTRY); | 71 l10n_util::GetStringUTF16(IDS_AUTOFILL_FIELD_LABEL_COUNTRY); |
69 DetailInput input = { DetailInput::LONG, server_type, placeholder_text }; | 72 DetailInput input = { DetailInput::LONG, server_type, placeholder_text }; |
70 inputs->push_back(input); | 73 inputs->push_back(input); |
(...skipping 15 matching lines...) Expand all Loading... | |
86 } | 89 } |
87 | 90 |
88 return true; | 91 return true; |
89 } | 92 } |
90 | 93 |
91 bool AddressHasCompleteAndVerifiedData(const AutofillProfile& profile, | 94 bool AddressHasCompleteAndVerifiedData(const AutofillProfile& profile, |
92 const std::string& app_locale) { | 95 const std::string& app_locale) { |
93 if (!profile.IsVerified()) | 96 if (!profile.IsVerified()) |
94 return false; | 97 return false; |
95 | 98 |
96 if (!i18n::CreateAddressDataFromAutofillProfile(profile, app_locale)-> | 99 scoped_ptr<AddressData> address( |
97 HasAllRequiredFields()) { | 100 i18n::CreateAddressDataFromAutofillProfile(profile, app_locale)); |
please use gerrit instead
2014/06/05 22:22:48
To reduce reviewer burden: make a HasAllRequiredFi
please use gerrit instead
2014/06/09 23:28:16
Done.
| |
98 return false; | 101 |
102 // TODO: Think about whether it makes sense to do this special check here in | |
103 // Chromium, instead of just using the regular address validation feature of | |
104 // libaddressinput. | |
105 static const AddressField kFields[] = { | |
106 ::i18n::addressinput::COUNTRY, | |
107 ::i18n::addressinput::ADMIN_AREA, | |
108 ::i18n::addressinput::LOCALITY, | |
109 ::i18n::addressinput::DEPENDENT_LOCALITY, | |
110 ::i18n::addressinput::SORTING_CODE, | |
111 ::i18n::addressinput::POSTAL_CODE, | |
112 ::i18n::addressinput::STREET_ADDRESS, | |
113 ::i18n::addressinput::RECIPIENT | |
114 }; | |
115 | |
116 for (size_t i = 0; i < arraysize(kFields); ++i) { | |
117 AddressField field = kFields[i]; | |
118 if (address->IsFieldEmpty(field) && | |
119 ::i18n::addressinput::IsFieldRequired(field, address->region_code)) { | |
120 return false; | |
121 } | |
99 } | 122 } |
100 | 123 |
101 const ServerFieldType more_required_fields[] = { | 124 static const ServerFieldType more_required_fields[] = { |
102 NAME_FULL, | 125 NAME_FULL, |
103 PHONE_HOME_WHOLE_NUMBER | 126 PHONE_HOME_WHOLE_NUMBER |
104 }; | 127 }; |
105 | 128 |
106 for (size_t i = 0; i < arraysize(more_required_fields); ++i) { | 129 for (size_t i = 0; i < arraysize(more_required_fields); ++i) { |
107 if (profile.GetRawInfo(more_required_fields[i]).empty()) | 130 if (profile.GetRawInfo(more_required_fields[i]).empty()) { |
108 return false; | 131 return false; |
132 } | |
109 } | 133 } |
110 | 134 |
111 return true; | 135 return true; |
112 } | 136 } |
113 | 137 |
114 ServerFieldType TypeForField(AddressField address_field, | 138 ServerFieldType TypeForField(AddressField address_field, |
115 common::AddressType address_type) { | 139 common::AddressType address_type) { |
116 bool billing = address_type == common::ADDRESS_TYPE_BILLING; | 140 bool billing = address_type == common::ADDRESS_TYPE_BILLING; |
117 switch (address_field) { | 141 switch (address_field) { |
118 case ::i18n::addressinput::COUNTRY: | 142 case ::i18n::addressinput::COUNTRY: |
119 return billing ? ADDRESS_BILLING_COUNTRY : ADDRESS_HOME_COUNTRY; | 143 return billing ? ADDRESS_BILLING_COUNTRY : ADDRESS_HOME_COUNTRY; |
120 case ::i18n::addressinput::ADMIN_AREA: | 144 case ::i18n::addressinput::ADMIN_AREA: |
121 return billing ? ADDRESS_BILLING_STATE : ADDRESS_HOME_STATE; | 145 return billing ? ADDRESS_BILLING_STATE : ADDRESS_HOME_STATE; |
122 case ::i18n::addressinput::LOCALITY: | 146 case ::i18n::addressinput::LOCALITY: |
123 return billing ? ADDRESS_BILLING_CITY : ADDRESS_HOME_CITY; | 147 return billing ? ADDRESS_BILLING_CITY : ADDRESS_HOME_CITY; |
124 case ::i18n::addressinput::DEPENDENT_LOCALITY: | 148 case ::i18n::addressinput::DEPENDENT_LOCALITY: |
125 return billing ? ADDRESS_BILLING_DEPENDENT_LOCALITY : | 149 return billing ? ADDRESS_BILLING_DEPENDENT_LOCALITY : |
126 ADDRESS_HOME_DEPENDENT_LOCALITY; | 150 ADDRESS_HOME_DEPENDENT_LOCALITY; |
127 case ::i18n::addressinput::POSTAL_CODE: | 151 case ::i18n::addressinput::POSTAL_CODE: |
128 return billing ? ADDRESS_BILLING_ZIP : ADDRESS_HOME_ZIP; | 152 return billing ? ADDRESS_BILLING_ZIP : ADDRESS_HOME_ZIP; |
129 case ::i18n::addressinput::SORTING_CODE: | 153 case ::i18n::addressinput::SORTING_CODE: |
130 return billing ? ADDRESS_BILLING_SORTING_CODE : ADDRESS_HOME_SORTING_CODE; | 154 return billing ? ADDRESS_BILLING_SORTING_CODE : ADDRESS_HOME_SORTING_CODE; |
131 case ::i18n::addressinput::STREET_ADDRESS: | 155 case ::i18n::addressinput::STREET_ADDRESS: |
132 return billing ? ADDRESS_BILLING_STREET_ADDRESS : | 156 return billing ? ADDRESS_BILLING_STREET_ADDRESS : |
133 ADDRESS_HOME_STREET_ADDRESS; | 157 ADDRESS_HOME_STREET_ADDRESS; |
134 case ::i18n::addressinput::RECIPIENT: | 158 case ::i18n::addressinput::RECIPIENT: |
135 return billing ? NAME_BILLING_FULL : NAME_FULL; | 159 return billing ? NAME_BILLING_FULL : NAME_FULL; |
136 case ::i18n::addressinput::ORGANIZATION: | |
137 return COMPANY_NAME; | |
138 } | 160 } |
139 NOTREACHED(); | 161 NOTREACHED(); |
140 return UNKNOWN_TYPE; | 162 return UNKNOWN_TYPE; |
141 } | 163 } |
142 | 164 |
143 bool FieldForType(ServerFieldType server_type, | 165 bool FieldForType(ServerFieldType server_type, |
144 ::i18n::addressinput::AddressField* field) { | 166 ::i18n::addressinput::AddressField* field) { |
145 switch (server_type) { | 167 switch (server_type) { |
146 case ADDRESS_BILLING_COUNTRY: | 168 case ADDRESS_BILLING_COUNTRY: |
147 case ADDRESS_HOME_COUNTRY: | 169 case ADDRESS_HOME_COUNTRY: |
(...skipping 28 matching lines...) Expand all Loading... | |
176 case ADDRESS_BILLING_STREET_ADDRESS: | 198 case ADDRESS_BILLING_STREET_ADDRESS: |
177 case ADDRESS_BILLING_LINE1: | 199 case ADDRESS_BILLING_LINE1: |
178 case ADDRESS_BILLING_LINE2: | 200 case ADDRESS_BILLING_LINE2: |
179 case ADDRESS_HOME_STREET_ADDRESS: | 201 case ADDRESS_HOME_STREET_ADDRESS: |
180 case ADDRESS_HOME_LINE1: | 202 case ADDRESS_HOME_LINE1: |
181 case ADDRESS_HOME_LINE2: | 203 case ADDRESS_HOME_LINE2: |
182 if (field) | 204 if (field) |
183 *field = ::i18n::addressinput::STREET_ADDRESS; | 205 *field = ::i18n::addressinput::STREET_ADDRESS; |
184 return true; | 206 return true; |
185 case COMPANY_NAME: | 207 case COMPANY_NAME: |
186 if (field) | 208 // Ignored. |
187 *field = ::i18n::addressinput::ORGANIZATION; | |
188 return true; | 209 return true; |
please use gerrit instead
2014/06/05 22:22:48
Investigate: this might have to return false due t
please use gerrit instead
2014/06/09 23:28:16
Done.
| |
189 case NAME_BILLING_FULL: | 210 case NAME_BILLING_FULL: |
190 case NAME_FULL: | 211 case NAME_FULL: |
191 if (field) | 212 if (field) |
192 *field = ::i18n::addressinput::RECIPIENT; | 213 *field = ::i18n::addressinput::RECIPIENT; |
193 return true; | 214 return true; |
194 default: | 215 default: |
195 return false; | 216 return false; |
196 } | 217 } |
197 } | 218 } |
198 | 219 |
199 bool CountryIsFullySupported(const std::string& country_code) { | 220 bool CountryIsFullySupported(const std::string& country_code) { |
200 DCHECK_EQ(2U, country_code.size()); | 221 DCHECK_EQ(2U, country_code.size()); |
222 ::i18n::addressinput::Localization localization; | |
223 localization.SetGetter( | |
224 l10n_util::GetStringUTF8, g_browser_process->GetApplicationLocale()); | |
225 std::string best_address_language; | |
please use gerrit instead
2014/06/05 22:22:47
To reduce reviewer burden: put this into a functio
please use gerrit instead
2014/06/09 23:28:16
Done.
| |
201 std::vector< ::i18n::addressinput::AddressUiComponent> components = | 226 std::vector< ::i18n::addressinput::AddressUiComponent> components = |
202 ::i18n::addressinput::BuildComponents( | 227 ::i18n::addressinput::BuildComponents( |
203 country_code, g_browser_process->GetApplicationLocale(), NULL); | 228 country_code, localization, &best_address_language); |
204 for (size_t i = 0; i < components.size(); ++i) { | 229 for (size_t i = 0; i < components.size(); ++i) { |
205 if (components[i].field == ::i18n::addressinput::DEPENDENT_LOCALITY) | 230 if (components[i].field == ::i18n::addressinput::DEPENDENT_LOCALITY) |
206 return false; | 231 return false; |
207 } | 232 } |
208 return true; | 233 return true; |
209 } | 234 } |
210 | 235 |
211 } // namespace i18ninput | 236 } // namespace i18ninput |
212 } // namespace autofill | 237 } // namespace autofill |
OLD | NEW |