Reland "Use upstream libaddressinput in Chrome."
[chromium-blink-merge.git] / components / autofill / core / browser / address_i18n.cc
blob29227005c805f0ec8d0c25572e74a96d1e75a234
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"
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"
16 namespace autofill {
17 namespace i18n {
19 namespace {
21 base::string16 GetInfoHelper(const AutofillProfile& profile,
22 const std::string& app_locale,
23 const AutofillType& type) {
24 return profile.GetInfo(type, app_locale);
27 } // namespace
29 using ::i18n::addressinput::AddressData;
31 scoped_ptr<AddressData> CreateAddressData(
32 const base::Callback<base::string16(const AutofillType&)>& get_info) {
33 scoped_ptr<AddressData> address_data(new AddressData());
34 address_data->recipient = base::UTF16ToUTF8(
35 get_info.Run(AutofillType(NAME_FULL)));
36 address_data->region_code = base::UTF16ToUTF8(
37 get_info.Run(AutofillType(HTML_TYPE_COUNTRY_CODE, HTML_MODE_NONE)));
38 address_data->administrative_area = base::UTF16ToUTF8(
39 get_info.Run(AutofillType(ADDRESS_HOME_STATE)));
40 address_data->locality = base::UTF16ToUTF8(
41 get_info.Run(AutofillType(ADDRESS_HOME_CITY)));
42 address_data->dependent_locality = base::UTF16ToUTF8(
43 get_info.Run(AutofillType(ADDRESS_HOME_DEPENDENT_LOCALITY)));
44 address_data->sorting_code = base::UTF16ToUTF8(
45 get_info.Run(AutofillType(ADDRESS_HOME_SORTING_CODE)));
46 address_data->postal_code = base::UTF16ToUTF8(
47 get_info.Run(AutofillType(ADDRESS_HOME_ZIP)));
48 base::SplitString(
49 base::UTF16ToUTF8(
50 get_info.Run(AutofillType(ADDRESS_HOME_STREET_ADDRESS))),
51 '\n',
52 &address_data->address_line);
53 return address_data.Pass();
56 scoped_ptr< ::i18n::addressinput::AddressData>
57 CreateAddressDataFromAutofillProfile(const AutofillProfile& profile,
58 const std::string& app_locale) {
59 scoped_ptr< ::i18n::addressinput::AddressData> address_data =
60 i18n::CreateAddressData(base::Bind(&GetInfoHelper, profile, app_locale));
61 address_data->language_code = profile.language_code();
62 return address_data.Pass();
65 } // namespace i18n
66 } // namespace autofill