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
3 // found in the LICENSE file.
5 #include "chrome/browser/ui/autofill/autofill_dialog_i18n_input.h"
7 #include "base/strings/string_split.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "chrome/browser/browser_process.h"
10 #include "components/autofill/core/browser/autofill_profile.h"
11 #include "components/autofill/core/browser/credit_card.h"
12 #include "components/autofill/core/browser/field_types.h"
13 #include "grit/component_strings.h"
14 #include "third_party/libaddressinput/chromium/cpp/include/libaddressinput/address_data.h"
15 #include "third_party/libaddressinput/chromium/cpp/include/libaddressinput/address_field.h"
16 #include "third_party/libaddressinput/chromium/cpp/include/libaddressinput/address_ui.h"
17 #include "third_party/libaddressinput/chromium/cpp/include/libaddressinput/address_ui_component.h"
18 #include "ui/base/l10n/l10n_util.h"
25 using base::UTF16ToUTF8;
26 using ::i18n::addressinput::AddressData;
27 using ::i18n::addressinput::AddressField;
28 using ::i18n::addressinput::AddressUiComponent;
30 DetailInput::Length LengthFromHint(AddressUiComponent::LengthHint hint) {
31 if (hint == AddressUiComponent::HINT_SHORT)
32 return DetailInput::SHORT;
33 DCHECK_EQ(hint, AddressUiComponent::HINT_LONG);
34 return DetailInput::LONG;
39 void BuildAddressInputs(common::AddressType address_type,
40 const std::string& country_code,
42 std::string* language_code) {
43 std::vector<AddressUiComponent> components(
44 ::i18n::addressinput::BuildComponents(
45 country_code, g_browser_process->GetApplicationLocale(),
48 const bool billing = address_type == common::ADDRESS_TYPE_BILLING;
50 for (size_t i = 0; i < components.size(); ++i) {
51 const AddressUiComponent& component = components[i];
52 if (component.field == ::i18n::addressinput::ORGANIZATION) {
53 // TODO(dbeam): figure out when we actually need this.
57 ServerFieldType server_type = TypeForField(component.field, address_type);
58 DetailInput::Length length = LengthFromHint(component.length_hint);
59 base::string16 placeholder = l10n_util::GetStringUTF16(component.name_id);
60 DetailInput input = { length, server_type, placeholder };
61 inputs->push_back(input);
64 ServerFieldType server_type =
65 billing ? ADDRESS_BILLING_COUNTRY : ADDRESS_HOME_COUNTRY;
66 base::string16 placeholder_text =
67 l10n_util::GetStringUTF16(IDS_AUTOFILL_FIELD_LABEL_COUNTRY);
68 DetailInput input = { DetailInput::LONG, server_type, placeholder_text };
69 inputs->push_back(input);
72 bool CardHasCompleteAndVerifiedData(const CreditCard& card) {
73 if (!card.IsVerified())
76 const ServerFieldType required_fields[] = {
78 CREDIT_CARD_EXP_MONTH,
79 CREDIT_CARD_EXP_4_DIGIT_YEAR,
82 for (size_t i = 0; i < arraysize(required_fields); ++i) {
83 if (card.GetRawInfo(required_fields[i]).empty())
90 bool AddressHasCompleteAndVerifiedData(const AutofillProfile& profile) {
91 if (!profile.IsVerified())
94 base::string16 country_code = profile.GetRawInfo(ADDRESS_HOME_COUNTRY);
95 if (country_code.empty())
98 std::vector<AddressField> required_fields =
99 ::i18n::addressinput::GetRequiredFields(base::UTF16ToUTF8(country_code));
101 for (size_t i = 0; i < required_fields.size(); ++i) {
102 ServerFieldType type =
103 TypeForField(required_fields[i], common::ADDRESS_TYPE_SHIPPING);
104 if (profile.GetRawInfo(type).empty())
108 const ServerFieldType more_required_fields[] = {
110 PHONE_HOME_WHOLE_NUMBER
113 for (size_t i = 0; i < arraysize(more_required_fields); ++i) {
114 if (profile.GetRawInfo(more_required_fields[i]).empty())
121 ServerFieldType TypeForField(AddressField address_field,
122 common::AddressType address_type) {
123 bool billing = address_type == common::ADDRESS_TYPE_BILLING;
124 switch (address_field) {
125 case ::i18n::addressinput::COUNTRY:
126 return billing ? ADDRESS_BILLING_COUNTRY : ADDRESS_HOME_COUNTRY;
127 case ::i18n::addressinput::ADMIN_AREA:
128 return billing ? ADDRESS_BILLING_STATE : ADDRESS_HOME_STATE;
129 case ::i18n::addressinput::LOCALITY:
130 return billing ? ADDRESS_BILLING_CITY : ADDRESS_HOME_CITY;
131 case ::i18n::addressinput::DEPENDENT_LOCALITY:
132 return billing ? ADDRESS_BILLING_DEPENDENT_LOCALITY :
133 ADDRESS_HOME_DEPENDENT_LOCALITY;
134 case ::i18n::addressinput::POSTAL_CODE:
135 return billing ? ADDRESS_BILLING_ZIP : ADDRESS_HOME_ZIP;
136 case ::i18n::addressinput::SORTING_CODE:
137 return billing ? ADDRESS_BILLING_SORTING_CODE : ADDRESS_HOME_SORTING_CODE;
138 case ::i18n::addressinput::STREET_ADDRESS:
139 return billing ? ADDRESS_BILLING_STREET_ADDRESS :
140 ADDRESS_HOME_STREET_ADDRESS;
141 case ::i18n::addressinput::RECIPIENT:
142 return billing ? NAME_BILLING_FULL : NAME_FULL;
143 case ::i18n::addressinput::ORGANIZATION:
150 bool FieldForType(ServerFieldType server_type,
151 ::i18n::addressinput::AddressField* field) {
152 switch (server_type) {
153 case ADDRESS_BILLING_COUNTRY:
154 case ADDRESS_HOME_COUNTRY:
156 *field = ::i18n::addressinput::COUNTRY;
158 case ADDRESS_BILLING_STATE:
159 case ADDRESS_HOME_STATE:
161 *field = ::i18n::addressinput::ADMIN_AREA;
163 case ADDRESS_BILLING_CITY:
164 case ADDRESS_HOME_CITY:
166 *field = ::i18n::addressinput::LOCALITY;
168 case ADDRESS_BILLING_DEPENDENT_LOCALITY:
169 case ADDRESS_HOME_DEPENDENT_LOCALITY:
171 *field = ::i18n::addressinput::DEPENDENT_LOCALITY;
173 case ADDRESS_BILLING_SORTING_CODE:
174 case ADDRESS_HOME_SORTING_CODE:
176 *field = ::i18n::addressinput::SORTING_CODE;
178 case ADDRESS_BILLING_ZIP:
179 case ADDRESS_HOME_ZIP:
181 *field = ::i18n::addressinput::POSTAL_CODE;
183 case ADDRESS_BILLING_STREET_ADDRESS:
184 case ADDRESS_BILLING_LINE1:
185 case ADDRESS_BILLING_LINE2:
186 case ADDRESS_HOME_STREET_ADDRESS:
187 case ADDRESS_HOME_LINE1:
188 case ADDRESS_HOME_LINE2:
190 *field = ::i18n::addressinput::STREET_ADDRESS;
194 *field = ::i18n::addressinput::ORGANIZATION;
196 case NAME_BILLING_FULL:
199 *field = ::i18n::addressinput::RECIPIENT;
206 void CreateAddressData(
207 const base::Callback<base::string16(const AutofillType&)>& get_info,
208 AddressData* address_data) {
209 address_data->recipient = UTF16ToUTF8(get_info.Run(AutofillType(NAME_FULL)));
210 address_data->country_code = UTF16ToUTF8(
211 get_info.Run(AutofillType(HTML_TYPE_COUNTRY_CODE, HTML_MODE_SHIPPING)));
212 DCHECK_EQ(2U, address_data->country_code.size());
213 address_data->administrative_area = UTF16ToUTF8(
214 get_info.Run(AutofillType(ADDRESS_HOME_STATE)));
215 address_data->locality = UTF16ToUTF8(
216 get_info.Run(AutofillType(ADDRESS_HOME_CITY)));
217 address_data->dependent_locality = UTF16ToUTF8(
218 get_info.Run(AutofillType(ADDRESS_HOME_DEPENDENT_LOCALITY)));
219 address_data->sorting_code = UTF16ToUTF8(
220 get_info.Run(AutofillType(ADDRESS_HOME_SORTING_CODE)));
221 address_data->postal_code = UTF16ToUTF8(
222 get_info.Run(AutofillType(ADDRESS_HOME_ZIP)));
224 UTF16ToUTF8(get_info.Run(AutofillType(ADDRESS_HOME_STREET_ADDRESS))),
226 &address_data->address_lines);
229 bool CountryIsFullySupported(const std::string& country_code) {
230 DCHECK_EQ(2U, country_code.size());
231 std::vector< ::i18n::addressinput::AddressUiComponent> components =
232 ::i18n::addressinput::BuildComponents(
233 country_code, g_browser_process->GetApplicationLocale(), NULL);
234 for (size_t i = 0; i < components.size(); ++i) {
235 if (components[i].field == ::i18n::addressinput::DEPENDENT_LOCALITY)
241 } // namespace i18ninput
242 } // namespace autofill