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 "components/autofill/core/browser/address_i18n.h"
8 #include "base/callback.h"
9 #include "base/logging.h"
10 #include "base/strings/string_split.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "components/autofill/core/browser/autofill_profile.h"
13 #include "components/autofill/core/browser/autofill_type.h"
14 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_data.h"
21 base::string16
GetInfoHelper(const AutofillProfile
& profile
,
22 const std::string
& app_locale
,
23 const AutofillType
& type
) {
24 return profile
.GetInfo(type
, app_locale
);
29 using ::i18n::addressinput::AddressData
;
30 using ::i18n::addressinput::AddressField
;
32 scoped_ptr
<AddressData
> CreateAddressData(
33 const base::Callback
<base::string16(const AutofillType
&)>& get_info
) {
34 scoped_ptr
<AddressData
> address_data(new AddressData());
35 address_data
->recipient
= base::UTF16ToUTF8(
36 get_info
.Run(AutofillType(NAME_FULL
)));
37 address_data
->organization
= base::UTF16ToUTF8(
38 get_info
.Run(AutofillType(COMPANY_NAME
)));
39 address_data
->region_code
= base::UTF16ToUTF8(
40 get_info
.Run(AutofillType(HTML_TYPE_COUNTRY_CODE
, HTML_MODE_NONE
)));
41 address_data
->administrative_area
= base::UTF16ToUTF8(
42 get_info
.Run(AutofillType(ADDRESS_HOME_STATE
)));
43 address_data
->locality
= base::UTF16ToUTF8(
44 get_info
.Run(AutofillType(ADDRESS_HOME_CITY
)));
45 address_data
->dependent_locality
= base::UTF16ToUTF8(
46 get_info
.Run(AutofillType(ADDRESS_HOME_DEPENDENT_LOCALITY
)));
47 address_data
->sorting_code
= base::UTF16ToUTF8(
48 get_info
.Run(AutofillType(ADDRESS_HOME_SORTING_CODE
)));
49 address_data
->postal_code
= base::UTF16ToUTF8(
50 get_info
.Run(AutofillType(ADDRESS_HOME_ZIP
)));
51 address_data
->address_line
= base::SplitString(
53 get_info
.Run(AutofillType(ADDRESS_HOME_STREET_ADDRESS
))),
54 "\n", base::TRIM_WHITESPACE
, base::SPLIT_WANT_ALL
);
55 return address_data
.Pass();
58 scoped_ptr
< ::i18n::addressinput::AddressData
>
59 CreateAddressDataFromAutofillProfile(const AutofillProfile
& profile
,
60 const std::string
& app_locale
) {
61 scoped_ptr
< ::i18n::addressinput::AddressData
> address_data
=
62 i18n::CreateAddressData(base::Bind(&GetInfoHelper
, profile
, app_locale
));
63 address_data
->language_code
= profile
.language_code();
64 return address_data
.Pass();
67 ServerFieldType
TypeForField(AddressField address_field
, bool billing
) {
68 switch (address_field
) {
69 case ::i18n::addressinput::COUNTRY
:
70 return billing
? ADDRESS_BILLING_COUNTRY
: ADDRESS_HOME_COUNTRY
;
71 case ::i18n::addressinput::ADMIN_AREA
:
72 return billing
? ADDRESS_BILLING_STATE
: ADDRESS_HOME_STATE
;
73 case ::i18n::addressinput::LOCALITY
:
74 return billing
? ADDRESS_BILLING_CITY
: ADDRESS_HOME_CITY
;
75 case ::i18n::addressinput::DEPENDENT_LOCALITY
:
76 return billing
? ADDRESS_BILLING_DEPENDENT_LOCALITY
77 : ADDRESS_HOME_DEPENDENT_LOCALITY
;
78 case ::i18n::addressinput::POSTAL_CODE
:
79 return billing
? ADDRESS_BILLING_ZIP
: ADDRESS_HOME_ZIP
;
80 case ::i18n::addressinput::SORTING_CODE
:
81 return billing
? ADDRESS_BILLING_SORTING_CODE
: ADDRESS_HOME_SORTING_CODE
;
82 case ::i18n::addressinput::STREET_ADDRESS
:
83 return billing
? ADDRESS_BILLING_STREET_ADDRESS
84 : ADDRESS_HOME_STREET_ADDRESS
;
85 case ::i18n::addressinput::ORGANIZATION
:
87 case ::i18n::addressinput::RECIPIENT
:
88 return billing
? NAME_BILLING_FULL
: NAME_FULL
;
94 bool FieldForType(ServerFieldType server_type
, AddressField
* field
) {
95 switch (server_type
) {
96 case ADDRESS_BILLING_COUNTRY
:
97 case ADDRESS_HOME_COUNTRY
:
99 *field
= ::i18n::addressinput::COUNTRY
;
101 case ADDRESS_BILLING_STATE
:
102 case ADDRESS_HOME_STATE
:
104 *field
= ::i18n::addressinput::ADMIN_AREA
;
106 case ADDRESS_BILLING_CITY
:
107 case ADDRESS_HOME_CITY
:
109 *field
= ::i18n::addressinput::LOCALITY
;
111 case ADDRESS_BILLING_DEPENDENT_LOCALITY
:
112 case ADDRESS_HOME_DEPENDENT_LOCALITY
:
114 *field
= ::i18n::addressinput::DEPENDENT_LOCALITY
;
116 case ADDRESS_BILLING_SORTING_CODE
:
117 case ADDRESS_HOME_SORTING_CODE
:
119 *field
= ::i18n::addressinput::SORTING_CODE
;
121 case ADDRESS_BILLING_ZIP
:
122 case ADDRESS_HOME_ZIP
:
124 *field
= ::i18n::addressinput::POSTAL_CODE
;
126 case ADDRESS_BILLING_STREET_ADDRESS
:
127 case ADDRESS_BILLING_LINE1
:
128 case ADDRESS_BILLING_LINE2
:
129 case ADDRESS_HOME_STREET_ADDRESS
:
130 case ADDRESS_HOME_LINE1
:
131 case ADDRESS_HOME_LINE2
:
133 *field
= ::i18n::addressinput::STREET_ADDRESS
;
137 *field
= ::i18n::addressinput::ORGANIZATION
;
139 case NAME_BILLING_FULL
:
142 *field
= ::i18n::addressinput::RECIPIENT
;
150 } // namespace autofill