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/chromium/cpp/include/libaddressinput/address_data.h" 15 16 namespace autofill { 17 namespace i18n { 18 19 namespace { 20 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 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->country_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_lines); 53 return address_data.Pass(); 54 } 55 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(); 63 } 64 65 } // namespace i18n 66 } // namespace autofill 67