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.
4
5 #include "components/autofill/core/browser/address_i18n.h"
6
7 #include "base/bind.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"
15
16 namespace autofill {
17 namespace i18n {
18
19 namespace {
20
GetInfoHelper(const AutofillProfile & profile,const std::string & app_locale,const AutofillType & type)21 base::string16 GetInfoHelper(const AutofillProfile& profile,
22 const std::string& app_locale,
23 const AutofillType& type) {
24 return profile.GetInfo(type, app_locale);
25 }
26
27 } // namespace
28
29 using ::i18n::addressinput::AddressData;
30 using ::i18n::addressinput::AddressField;
31
CreateAddressData(const base::Callback<base::string16 (const AutofillType &)> & get_info)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 base::SplitString(
52 base::UTF16ToUTF8(
53 get_info.Run(AutofillType(ADDRESS_HOME_STREET_ADDRESS))),
54 '\n',
55 &address_data->address_line);
56 return address_data.Pass();
57 }
58
59 scoped_ptr< ::i18n::addressinput::AddressData>
CreateAddressDataFromAutofillProfile(const AutofillProfile & profile,const std::string & app_locale)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();
66 }
67
TypeForField(AddressField address_field,bool billing)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:
87 return COMPANY_NAME;
88 case ::i18n::addressinput::RECIPIENT:
89 return billing ? NAME_BILLING_FULL : NAME_FULL;
90 }
91 NOTREACHED();
92 return UNKNOWN_TYPE;
93 }
94
FieldForType(ServerFieldType server_type,AddressField * field)95 bool FieldForType(ServerFieldType server_type, AddressField* field) {
96 switch (server_type) {
97 case ADDRESS_BILLING_COUNTRY:
98 case ADDRESS_HOME_COUNTRY:
99 if (field)
100 *field = ::i18n::addressinput::COUNTRY;
101 return true;
102 case ADDRESS_BILLING_STATE:
103 case ADDRESS_HOME_STATE:
104 if (field)
105 *field = ::i18n::addressinput::ADMIN_AREA;
106 return true;
107 case ADDRESS_BILLING_CITY:
108 case ADDRESS_HOME_CITY:
109 if (field)
110 *field = ::i18n::addressinput::LOCALITY;
111 return true;
112 case ADDRESS_BILLING_DEPENDENT_LOCALITY:
113 case ADDRESS_HOME_DEPENDENT_LOCALITY:
114 if (field)
115 *field = ::i18n::addressinput::DEPENDENT_LOCALITY;
116 return true;
117 case ADDRESS_BILLING_SORTING_CODE:
118 case ADDRESS_HOME_SORTING_CODE:
119 if (field)
120 *field = ::i18n::addressinput::SORTING_CODE;
121 return true;
122 case ADDRESS_BILLING_ZIP:
123 case ADDRESS_HOME_ZIP:
124 if (field)
125 *field = ::i18n::addressinput::POSTAL_CODE;
126 return true;
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:
133 if (field)
134 *field = ::i18n::addressinput::STREET_ADDRESS;
135 return true;
136 case COMPANY_NAME:
137 if (field)
138 *field = ::i18n::addressinput::ORGANIZATION;
139 return true;
140 case NAME_BILLING_FULL:
141 case NAME_FULL:
142 if (field)
143 *field = ::i18n::addressinput::RECIPIENT;
144 return true;
145 default:
146 return false;
147 }
148 }
149
150 } // namespace i18n
151 } // namespace autofill
152