OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 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 "chrome/browser/ui/autofill/data_model_wrapper.h" | |
6 | |
7 #include <stddef.h> | |
8 | |
9 #include "base/bind.h" | |
10 #include "base/callback.h" | |
11 #include "base/strings/string_util.h" | |
12 #include "base/strings/utf_string_conversions.h" | |
13 #include "chrome/browser/browser_process.h" | |
14 #include "chrome/browser/ui/autofill/autofill_dialog_i18n_input.h" | |
15 #include "chrome/browser/ui/autofill/autofill_dialog_models.h" | |
16 #include "components/autofill/core/browser/address_i18n.h" | |
17 #include "components/autofill/core/browser/autofill_country.h" | |
18 #include "components/autofill/core/browser/autofill_data_model.h" | |
19 #include "components/autofill/core/browser/autofill_field.h" | |
20 #include "components/autofill/core/browser/autofill_profile.h" | |
21 #include "components/autofill/core/browser/autofill_type.h" | |
22 #include "components/autofill/core/browser/credit_card.h" | |
23 #include "components/autofill/core/browser/form_structure.h" | |
24 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_da
ta.h" | |
25 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_fi
eld.h" | |
26 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_fo
rmatter.h" | |
27 #include "ui/base/resource/resource_bundle.h" | |
28 #include "ui/gfx/image/image.h" | |
29 | |
30 namespace autofill { | |
31 | |
32 DataModelWrapper::~DataModelWrapper() {} | |
33 | |
34 void DataModelWrapper::FillInputs(DetailInputs* inputs) { | |
35 for (size_t i = 0; i < inputs->size(); ++i) { | |
36 (*inputs)[i].initial_value = | |
37 GetInfoForDisplay(AutofillType((*inputs)[i].type)); | |
38 } | |
39 } | |
40 | |
41 base::string16 DataModelWrapper::GetInfoForDisplay(const AutofillType& type) | |
42 const { | |
43 return GetInfo(type); | |
44 } | |
45 | |
46 gfx::Image DataModelWrapper::GetIcon() { | |
47 return gfx::Image(); | |
48 } | |
49 | |
50 bool DataModelWrapper::GetDisplayText( | |
51 base::string16* vertically_compact, | |
52 base::string16* horizontally_compact) { | |
53 base::string16 phone = | |
54 GetInfoForDisplay(AutofillType(PHONE_HOME_WHOLE_NUMBER)); | |
55 if (phone.empty()) | |
56 return false; | |
57 | |
58 // Format the address. | |
59 std::unique_ptr<::i18n::addressinput::AddressData> address_data = | |
60 i18n::CreateAddressData( | |
61 base::Bind(&DataModelWrapper::GetInfo, base::Unretained(this))); | |
62 address_data->language_code = GetLanguageCode(); | |
63 // Interactive autofill dialog does not display organization field. | |
64 address_data->organization.clear(); | |
65 std::vector<std::string> lines; | |
66 ::i18n::addressinput::GetFormattedNationalAddress(*address_data, &lines); | |
67 | |
68 std::string single_line; | |
69 ::i18n::addressinput::GetFormattedNationalAddressLine( | |
70 *address_data, &single_line); | |
71 | |
72 // Email and phone number aren't part of address formatting. | |
73 base::string16 non_address_info; | |
74 base::string16 email = GetInfoForDisplay(AutofillType(EMAIL_ADDRESS)); | |
75 if (!email.empty()) | |
76 non_address_info += base::ASCIIToUTF16("\n") + email; | |
77 | |
78 non_address_info += base::ASCIIToUTF16("\n") + phone; | |
79 | |
80 *vertically_compact = base::UTF8ToUTF16(single_line) + non_address_info; | |
81 *horizontally_compact = base::UTF8ToUTF16(base::JoinString(lines, "\n")) + | |
82 non_address_info; | |
83 | |
84 return true; | |
85 } | |
86 | |
87 bool DataModelWrapper::FillFormStructure( | |
88 const std::vector<ServerFieldType>& types, | |
89 const FormStructure::InputFieldComparator& compare, | |
90 FormStructure* form_structure) const { | |
91 return form_structure->FillFields( | |
92 types, | |
93 compare, | |
94 base::Bind(&DataModelWrapper::GetInfo, base::Unretained(this)), | |
95 GetLanguageCode(), | |
96 g_browser_process->GetApplicationLocale()); | |
97 } | |
98 | |
99 DataModelWrapper::DataModelWrapper() {} | |
100 | |
101 // AutofillProfileWrapper | |
102 | |
103 AutofillProfileWrapper::AutofillProfileWrapper(const AutofillProfile* profile) | |
104 : profile_(profile) {} | |
105 | |
106 AutofillProfileWrapper::~AutofillProfileWrapper() {} | |
107 | |
108 base::string16 AutofillProfileWrapper::GetInfo(const AutofillType& type) const { | |
109 // Requests for the user's credit card are filled from the billing address, | |
110 // but the AutofillProfile class doesn't know how to fill credit card | |
111 // fields. So, request for the corresponding profile type instead. | |
112 AutofillType effective_type = type; | |
113 if (type.GetStorableType() == CREDIT_CARD_NAME_FULL) | |
114 effective_type = AutofillType(NAME_BILLING_FULL); | |
115 | |
116 const std::string& app_locale = g_browser_process->GetApplicationLocale(); | |
117 return profile_->GetInfo(effective_type, app_locale); | |
118 } | |
119 | |
120 base::string16 AutofillProfileWrapper::GetInfoForDisplay( | |
121 const AutofillType& type) const { | |
122 // We display the "raw" phone number which contains user-defined formatting. | |
123 if (type.GetStorableType() == PHONE_HOME_WHOLE_NUMBER) { | |
124 base::string16 phone_number = profile_->GetRawInfo(type.GetStorableType()); | |
125 | |
126 // If there is no user-defined formatting at all, add some standard | |
127 // formatting. | |
128 if (base::ContainsOnlyChars(phone_number, | |
129 base::ASCIIToUTF16("+0123456789"))) { | |
130 std::string region = base::UTF16ToASCII( | |
131 GetInfo(AutofillType(HTML_TYPE_COUNTRY_CODE, HTML_MODE_NONE))); | |
132 i18n::PhoneObject phone(phone_number, region); | |
133 base::string16 formatted_number = phone.GetFormattedNumber(); | |
134 // Formatting may fail. | |
135 if (!formatted_number.empty()) | |
136 return formatted_number; | |
137 } | |
138 | |
139 return phone_number; | |
140 } | |
141 | |
142 return DataModelWrapper::GetInfoForDisplay(type); | |
143 } | |
144 | |
145 const std::string& AutofillProfileWrapper::GetLanguageCode() const { | |
146 return profile_->language_code(); | |
147 } | |
148 | |
149 // AutofillShippingAddressWrapper | |
150 | |
151 AutofillShippingAddressWrapper::AutofillShippingAddressWrapper( | |
152 const AutofillProfile* profile) | |
153 : AutofillProfileWrapper(profile) {} | |
154 | |
155 AutofillShippingAddressWrapper::~AutofillShippingAddressWrapper() {} | |
156 | |
157 base::string16 AutofillShippingAddressWrapper::GetInfo( | |
158 const AutofillType& type) const { | |
159 // Shipping addresses don't have email addresses associated with them. | |
160 if (type.GetStorableType() == EMAIL_ADDRESS) | |
161 return base::string16(); | |
162 | |
163 return AutofillProfileWrapper::GetInfo(type); | |
164 } | |
165 | |
166 // AutofillCreditCardWrapper | |
167 | |
168 AutofillCreditCardWrapper::AutofillCreditCardWrapper(const CreditCard* card) | |
169 : card_(card) {} | |
170 | |
171 AutofillCreditCardWrapper::~AutofillCreditCardWrapper() {} | |
172 | |
173 base::string16 AutofillCreditCardWrapper::GetInfo(const AutofillType& type) | |
174 const { | |
175 if (type.group() != CREDIT_CARD) | |
176 return base::string16(); | |
177 | |
178 if (type.GetStorableType() == CREDIT_CARD_EXP_MONTH) | |
179 return MonthComboboxModel::FormatMonth(card_->expiration_month()); | |
180 | |
181 return card_->GetInfo(type, g_browser_process->GetApplicationLocale()); | |
182 } | |
183 | |
184 gfx::Image AutofillCreditCardWrapper::GetIcon() { | |
185 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | |
186 return rb.GetImageNamed(CreditCard::IconResourceId(card_->type())); | |
187 } | |
188 | |
189 bool AutofillCreditCardWrapper::GetDisplayText( | |
190 base::string16* vertically_compact, | |
191 base::string16* horizontally_compact) { | |
192 if (!card_->IsValid()) | |
193 return false; | |
194 | |
195 *vertically_compact = *horizontally_compact = card_->TypeAndLastFourDigits(); | |
196 return true; | |
197 } | |
198 | |
199 const std::string& AutofillCreditCardWrapper::GetLanguageCode() const { | |
200 // Formatting a credit card for display does not depend on language code. | |
201 return base::EmptyString(); | |
202 } | |
203 | |
204 // I18nAddressDataWrapper | |
205 | |
206 I18nAddressDataWrapper::I18nAddressDataWrapper( | |
207 const ::i18n::addressinput::AddressData* address) | |
208 : address_(address) {} | |
209 | |
210 I18nAddressDataWrapper::~I18nAddressDataWrapper() {} | |
211 | |
212 base::string16 I18nAddressDataWrapper::GetInfo(const AutofillType& type) const { | |
213 ::i18n::addressinput::AddressField field; | |
214 if (!i18n::FieldForType(type.GetStorableType(), &field)) | |
215 return base::string16(); | |
216 | |
217 if (field == ::i18n::addressinput::STREET_ADDRESS) | |
218 return base::string16(); | |
219 | |
220 if (field == ::i18n::addressinput::COUNTRY) { | |
221 return AutofillCountry(address_->region_code, | |
222 g_browser_process->GetApplicationLocale()).name(); | |
223 } | |
224 | |
225 return base::UTF8ToUTF16(address_->GetFieldValue(field)); | |
226 } | |
227 | |
228 const std::string& I18nAddressDataWrapper::GetLanguageCode() const { | |
229 return address_->language_code; | |
230 } | |
231 | |
232 } // namespace autofill | |
OLD | NEW |