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
)));
53 get_info
.Run(AutofillType(ADDRESS_HOME_STREET_ADDRESS
))),
55 &address_data
->address_line
);
56 return address_data
.Pass();
59 scoped_ptr
< ::i18n::addressinput::AddressData
>
60 CreateAddressDataFromAutofillProfile(const AutofillProfile
& profile
,
61 const std::string
& app_locale
) {
62 scoped_ptr
< ::i18n::addressinput::AddressData
> address_data
=
63 i18n::CreateAddressData(base::Bind(&GetInfoHelper
, profile
, app_locale
));
64 address_data
->language_code
= profile
.language_code();
65 return address_data
.Pass();
68 ServerFieldType
TypeForField(AddressField address_field
, bool billing
) {
69 switch (address_field
) {
70 case ::i18n::addressinput::COUNTRY
:
71 return billing
? ADDRESS_BILLING_COUNTRY
: ADDRESS_HOME_COUNTRY
;
72 case ::i18n::addressinput::ADMIN_AREA
:
73 return billing
? ADDRESS_BILLING_STATE
: ADDRESS_HOME_STATE
;
74 case ::i18n::addressinput::LOCALITY
:
75 return billing
? ADDRESS_BILLING_CITY
: ADDRESS_HOME_CITY
;
76 case ::i18n::addressinput::DEPENDENT_LOCALITY
:
77 return billing
? ADDRESS_BILLING_DEPENDENT_LOCALITY
78 : ADDRESS_HOME_DEPENDENT_LOCALITY
;
79 case ::i18n::addressinput::POSTAL_CODE
:
80 return billing
? ADDRESS_BILLING_ZIP
: ADDRESS_HOME_ZIP
;
81 case ::i18n::addressinput::SORTING_CODE
:
82 return billing
? ADDRESS_BILLING_SORTING_CODE
: ADDRESS_HOME_SORTING_CODE
;
83 case ::i18n::addressinput::STREET_ADDRESS
:
84 return billing
? ADDRESS_BILLING_STREET_ADDRESS
85 : ADDRESS_HOME_STREET_ADDRESS
;
86 case ::i18n::addressinput::ORGANIZATION
:
88 case ::i18n::addressinput::RECIPIENT
:
89 return billing
? NAME_BILLING_FULL
: NAME_FULL
;
95 bool FieldForType(ServerFieldType server_type
, AddressField
* field
) {
96 switch (server_type
) {
97 case ADDRESS_BILLING_COUNTRY
:
98 case ADDRESS_HOME_COUNTRY
:
100 *field
= ::i18n::addressinput::COUNTRY
;
102 case ADDRESS_BILLING_STATE
:
103 case ADDRESS_HOME_STATE
:
105 *field
= ::i18n::addressinput::ADMIN_AREA
;
107 case ADDRESS_BILLING_CITY
:
108 case ADDRESS_HOME_CITY
:
110 *field
= ::i18n::addressinput::LOCALITY
;
112 case ADDRESS_BILLING_DEPENDENT_LOCALITY
:
113 case ADDRESS_HOME_DEPENDENT_LOCALITY
:
115 *field
= ::i18n::addressinput::DEPENDENT_LOCALITY
;
117 case ADDRESS_BILLING_SORTING_CODE
:
118 case ADDRESS_HOME_SORTING_CODE
:
120 *field
= ::i18n::addressinput::SORTING_CODE
;
122 case ADDRESS_BILLING_ZIP
:
123 case ADDRESS_HOME_ZIP
:
125 *field
= ::i18n::addressinput::POSTAL_CODE
;
127 case ADDRESS_BILLING_STREET_ADDRESS
:
128 case ADDRESS_BILLING_LINE1
:
129 case ADDRESS_BILLING_LINE2
:
130 case ADDRESS_HOME_STREET_ADDRESS
:
131 case ADDRESS_HOME_LINE1
:
132 case ADDRESS_HOME_LINE2
:
134 *field
= ::i18n::addressinput::STREET_ADDRESS
;
138 *field
= ::i18n::addressinput::ORGANIZATION
;
140 case NAME_BILLING_FULL
:
143 *field
= ::i18n::addressinput::RECIPIENT
;
151 } // namespace autofill