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/command_line.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "chrome/common/chrome_switches.h"
10 #include "components/autofill/core/browser/field_types.h"
11 #include "grit/component_strings.h"
12 #include "third_party/libaddressinput/chromium/cpp/include/libaddressinput/address_field.h"
13 #include "third_party/libaddressinput/chromium/cpp/include/libaddressinput/address_ui.h"
14 #include "third_party/libaddressinput/chromium/cpp/include/libaddressinput/address_ui_component.h"
15 #include "ui/base/l10n/l10n_util.h"
22 using i18n::addressinput::AddressField
;
23 using i18n::addressinput::AddressUiComponent
;
25 ServerFieldType
GetServerType(AddressField address_field
, bool billing
) {
26 switch (address_field
) {
27 case i18n::addressinput::COUNTRY
:
28 return billing
? ADDRESS_BILLING_COUNTRY
: ADDRESS_HOME_COUNTRY
;
29 case i18n::addressinput::ADMIN_AREA
:
30 return billing
? ADDRESS_BILLING_STATE
: ADDRESS_HOME_STATE
;
31 case i18n::addressinput::LOCALITY
:
32 return billing
? ADDRESS_BILLING_CITY
: ADDRESS_HOME_CITY
;
33 case i18n::addressinput::DEPENDENT_LOCALITY
:
34 return billing
? ADDRESS_BILLING_DEPENDENT_LOCALITY
:
35 ADDRESS_HOME_DEPENDENT_LOCALITY
;
36 case i18n::addressinput::POSTAL_CODE
:
37 return billing
? ADDRESS_BILLING_ZIP
: ADDRESS_HOME_ZIP
;
38 case i18n::addressinput::SORTING_CODE
:
39 return billing
? ADDRESS_BILLING_SORTING_CODE
: ADDRESS_HOME_SORTING_CODE
;
40 case i18n::addressinput::STREET_ADDRESS
:
41 return billing
? ADDRESS_BILLING_LINE1
: ADDRESS_HOME_LINE1
;
42 case i18n::addressinput::RECIPIENT
:
43 return billing
? NAME_BILLING_FULL
: NAME_FULL
;
44 case i18n::addressinput::ORGANIZATION
:
51 DetailInput::Length
LengthFromHint(AddressUiComponent::LengthHint hint
) {
52 if (hint
== AddressUiComponent::HINT_SHORT
)
53 return DetailInput::SHORT
;
54 DCHECK_EQ(hint
, AddressUiComponent::HINT_LONG
);
55 return DetailInput::LONG
;
61 CommandLine
* command_line
= CommandLine::ForCurrentProcess();
62 return command_line
->HasSwitch(::switches::kEnableAutofillAddressI18n
);
65 void BuildAddressInputs(common::AddressType address_type
,
66 const std::string
& country_code
,
67 DetailInputs
* inputs
) {
68 std::vector
<AddressUiComponent
> components(
69 i18n::addressinput::BuildComponents(country_code
));
71 const bool billing
= address_type
== common::ADDRESS_TYPE_BILLING
;
73 for (size_t i
= 0; i
< components
.size(); ++i
) {
74 const AddressUiComponent
& component
= components
[i
];
75 if (component
.field
== i18n::addressinput::ORGANIZATION
) {
76 // TODO(dbeam): figure out when we actually need this.
80 ServerFieldType server_type
= GetServerType(component
.field
, billing
);
81 DetailInput::Length length
= LengthFromHint(component
.length_hint
);
82 base::string16 placeholder
= l10n_util::GetStringUTF16(component
.name_id
);
83 DetailInput input
= { length
, server_type
, placeholder
};
84 inputs
->push_back(input
);
86 if (component
.field
== i18n::addressinput::STREET_ADDRESS
&&
87 component
.length_hint
== AddressUiComponent::HINT_LONG
) {
88 // TODO(dbeam): support more than 2 address lines. http://crbug.com/324889
89 ServerFieldType server_type
=
90 billing
? ADDRESS_BILLING_LINE2
: ADDRESS_HOME_LINE2
;
91 base::string16 placeholder
= l10n_util::GetStringUTF16(component
.name_id
);
92 DetailInput input
= { length
, server_type
, placeholder
};
93 inputs
->push_back(input
);
97 ServerFieldType server_type
=
98 billing
? ADDRESS_BILLING_COUNTRY
: ADDRESS_HOME_COUNTRY
;
99 base::string16 placeholder_text
=
100 l10n_util::GetStringUTF16(IDS_AUTOFILL_FIELD_LABEL_COUNTRY
);
101 DetailInput input
= { DetailInput::LONG
, server_type
, placeholder_text
};
102 inputs
->push_back(input
);
105 } // namespace i18ninput
106 } // namespace autofill