Only grant permissions to new extensions from sync if they have the expected version
[chromium-blink-merge.git] / components / autofill / core / browser / address_i18n.cc
blobca74fc519df2e918c7a788b6c78dc27b44fe8234
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;
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(
52 base::UTF16ToUTF8(
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:
86 return COMPANY_NAME;
87 case ::i18n::addressinput::RECIPIENT:
88 return billing ? NAME_BILLING_FULL : NAME_FULL;
90 NOTREACHED();
91 return UNKNOWN_TYPE;
94 bool FieldForType(ServerFieldType server_type, AddressField* field) {
95 switch (server_type) {
96 case ADDRESS_BILLING_COUNTRY:
97 case ADDRESS_HOME_COUNTRY:
98 if (field)
99 *field = ::i18n::addressinput::COUNTRY;
100 return true;
101 case ADDRESS_BILLING_STATE:
102 case ADDRESS_HOME_STATE:
103 if (field)
104 *field = ::i18n::addressinput::ADMIN_AREA;
105 return true;
106 case ADDRESS_BILLING_CITY:
107 case ADDRESS_HOME_CITY:
108 if (field)
109 *field = ::i18n::addressinput::LOCALITY;
110 return true;
111 case ADDRESS_BILLING_DEPENDENT_LOCALITY:
112 case ADDRESS_HOME_DEPENDENT_LOCALITY:
113 if (field)
114 *field = ::i18n::addressinput::DEPENDENT_LOCALITY;
115 return true;
116 case ADDRESS_BILLING_SORTING_CODE:
117 case ADDRESS_HOME_SORTING_CODE:
118 if (field)
119 *field = ::i18n::addressinput::SORTING_CODE;
120 return true;
121 case ADDRESS_BILLING_ZIP:
122 case ADDRESS_HOME_ZIP:
123 if (field)
124 *field = ::i18n::addressinput::POSTAL_CODE;
125 return true;
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:
132 if (field)
133 *field = ::i18n::addressinput::STREET_ADDRESS;
134 return true;
135 case COMPANY_NAME:
136 if (field)
137 *field = ::i18n::addressinput::ORGANIZATION;
138 return true;
139 case NAME_BILLING_FULL:
140 case NAME_FULL:
141 if (field)
142 *field = ::i18n::addressinput::RECIPIENT;
143 return true;
144 default:
145 return false;
149 } // namespace i18n
150 } // namespace autofill