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.
5 #include "chrome/browser/ui/autofill/data_model_wrapper.h"
8 #include "base/callback.h"
9 #include "base/strings/string_util.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "chrome/browser/browser_process.h"
12 #include "chrome/browser/ui/autofill/autofill_dialog_common.h"
13 #include "chrome/browser/ui/autofill/autofill_dialog_i18n_input.h"
14 #include "chrome/browser/ui/autofill/autofill_dialog_models.h"
15 #include "components/autofill/content/browser/wallet/full_wallet.h"
16 #include "components/autofill/content/browser/wallet/wallet_address.h"
17 #include "components/autofill/content/browser/wallet/wallet_items.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/chromium/cpp/include/libaddressinput/address_data.h"
25 #include "third_party/libaddressinput/chromium/cpp/include/libaddressinput/address_ui.h"
26 #include "ui/base/resource/resource_bundle.h"
27 #include "ui/gfx/image/image.h"
31 using base::ASCIIToUTF16;
32 using base::UTF16ToUTF8;
34 DataModelWrapper::~DataModelWrapper() {}
36 void DataModelWrapper::FillInputs(DetailInputs* inputs) {
37 for (size_t i = 0; i < inputs->size(); ++i) {
38 DetailInput* input = &(*inputs)[i];
39 input->initial_value = common::GetHardcodedValueForType(input->type);
40 if (input->initial_value.empty())
41 input->initial_value = GetInfo(AutofillType(input->type));
45 base::string16 DataModelWrapper::GetInfoForDisplay(const AutofillType& type)
50 gfx::Image DataModelWrapper::GetIcon() {
54 bool DataModelWrapper::GetDisplayText(
55 base::string16* vertically_compact,
56 base::string16* horizontally_compact) {
57 base::string16 phone =
58 GetInfoForDisplay(AutofillType(PHONE_HOME_WHOLE_NUMBER));
62 // Format the address.
63 ::i18n::addressinput::AddressData address_data;
64 i18ninput::CreateAddressData(
65 base::Bind(&DataModelWrapper::GetInfo, base::Unretained(this)),
67 std::vector<std::string> lines;
68 address_data.FormatForDisplay(&lines);
70 // Email and phone number aren't part of address formatting.
71 base::string16 non_address_info;
72 base::string16 email = GetInfoForDisplay(AutofillType(EMAIL_ADDRESS));
74 non_address_info += ASCIIToUTF16("\n") + email;
76 non_address_info += ASCIIToUTF16("\n") + phone;
78 // The separator is locale-specific.
79 std::string compact_separator =
80 ::i18n::addressinput::GetCompactAddressLinesSeparator(
81 g_browser_process->GetApplicationLocale());
83 base::UTF8ToUTF16(JoinString(lines, compact_separator)) +
85 *horizontally_compact = base::UTF8ToUTF16(JoinString(lines, "\n")) +
91 bool DataModelWrapper::FillFormStructure(
92 const std::vector<ServerFieldType>& types,
93 const FormStructure::InputFieldComparator& compare,
94 FormStructure* form_structure) const {
95 return form_structure->FillFields(
98 base::Bind(&DataModelWrapper::GetInfo, base::Unretained(this)),
99 g_browser_process->GetApplicationLocale());
102 DataModelWrapper::DataModelWrapper() {}
104 // AutofillProfileWrapper
106 AutofillProfileWrapper::AutofillProfileWrapper(const AutofillProfile* profile)
108 variant_group_(NO_GROUP),
111 AutofillProfileWrapper::AutofillProfileWrapper(
112 const AutofillProfile* profile,
113 const AutofillType& type,
116 variant_group_(type.group()),
119 AutofillProfileWrapper::~AutofillProfileWrapper() {}
121 base::string16 AutofillProfileWrapper::GetInfo(const AutofillType& type) const {
122 // Requests for the user's credit card are filled from the billing address,
123 // but the AutofillProfile class doesn't know how to fill credit card
124 // fields. So, request for the corresponding profile type instead.
125 AutofillType effective_type = type;
126 if (type.GetStorableType() == CREDIT_CARD_NAME)
127 effective_type = AutofillType(NAME_BILLING_FULL);
129 size_t variant = GetVariantForType(effective_type);
130 const std::string& app_locale = g_browser_process->GetApplicationLocale();
131 return profile_->GetInfoForVariant(effective_type, variant, app_locale);
134 base::string16 AutofillProfileWrapper::GetInfoForDisplay(
135 const AutofillType& type) const {
136 // We display the "raw" phone number which contains user-defined formatting.
137 if (type.GetStorableType() == PHONE_HOME_WHOLE_NUMBER) {
138 std::vector<base::string16> values;
139 profile_->GetRawMultiInfo(type.GetStorableType(), &values);
140 const base::string16& phone_number = values[GetVariantForType(type)];
142 // If there is no user-defined formatting at all, add some standard
144 if (ContainsOnlyChars(phone_number, ASCIIToUTF16("0123456789"))) {
145 std::string region = UTF16ToASCII(
146 GetInfo(AutofillType(HTML_TYPE_COUNTRY_CODE, HTML_MODE_NONE)));
147 i18n::PhoneObject phone(phone_number, region);
148 return phone.GetFormattedNumber();
154 return DataModelWrapper::GetInfoForDisplay(type);
157 size_t AutofillProfileWrapper::GetVariantForType(const AutofillType& type)
159 if (type.group() == variant_group_)
165 // AutofillShippingAddressWrapper
167 AutofillShippingAddressWrapper::AutofillShippingAddressWrapper(
168 const AutofillProfile* profile)
169 : AutofillProfileWrapper(profile) {}
171 AutofillShippingAddressWrapper::~AutofillShippingAddressWrapper() {}
173 base::string16 AutofillShippingAddressWrapper::GetInfo(
174 const AutofillType& type) const {
175 // Shipping addresses don't have email addresses associated with them.
176 if (type.GetStorableType() == EMAIL_ADDRESS)
177 return base::string16();
179 return AutofillProfileWrapper::GetInfo(type);
182 // AutofillCreditCardWrapper
184 AutofillCreditCardWrapper::AutofillCreditCardWrapper(const CreditCard* card)
187 AutofillCreditCardWrapper::~AutofillCreditCardWrapper() {}
189 base::string16 AutofillCreditCardWrapper::GetInfo(const AutofillType& type)
191 if (type.group() != CREDIT_CARD)
192 return base::string16();
194 if (type.GetStorableType() == CREDIT_CARD_EXP_MONTH)
195 return MonthComboboxModel::FormatMonth(card_->expiration_month());
197 return card_->GetInfo(type, g_browser_process->GetApplicationLocale());
200 gfx::Image AutofillCreditCardWrapper::GetIcon() {
201 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
202 return rb.GetImageNamed(CreditCard::IconResourceId(card_->type()));
205 bool AutofillCreditCardWrapper::GetDisplayText(
206 base::string16* vertically_compact,
207 base::string16* horizontally_compact) {
208 if (!card_->IsValid())
211 *vertically_compact = *horizontally_compact = card_->TypeAndLastFourDigits();
215 // WalletAddressWrapper
217 WalletAddressWrapper::WalletAddressWrapper(
218 const wallet::Address* address) : address_(address) {}
220 WalletAddressWrapper::~WalletAddressWrapper() {}
222 base::string16 WalletAddressWrapper::GetInfo(const AutofillType& type) const {
223 // Reachable from DataModelWrapper::GetDisplayText().
224 if (type.GetStorableType() == EMAIL_ADDRESS)
225 return base::string16();
227 return address_->GetInfo(type, g_browser_process->GetApplicationLocale());
230 base::string16 WalletAddressWrapper::GetInfoForDisplay(const AutofillType& type)
232 if (type.GetStorableType() == PHONE_HOME_WHOLE_NUMBER)
233 return address_->DisplayPhoneNumber();
235 return DataModelWrapper::GetInfoForDisplay(type);
238 bool WalletAddressWrapper::GetDisplayText(
239 base::string16* vertically_compact,
240 base::string16* horizontally_compact) {
241 if (!address_->is_complete_address())
244 return DataModelWrapper::GetDisplayText(vertically_compact,
245 horizontally_compact);
248 // WalletInstrumentWrapper
250 WalletInstrumentWrapper::WalletInstrumentWrapper(
251 const wallet::WalletItems::MaskedInstrument* instrument)
252 : instrument_(instrument) {}
254 WalletInstrumentWrapper::~WalletInstrumentWrapper() {}
256 base::string16 WalletInstrumentWrapper::GetInfo(const AutofillType& type)
258 // Reachable from DataModelWrapper::GetDisplayText().
259 if (type.GetStorableType() == EMAIL_ADDRESS)
260 return base::string16();
262 if (type.GetStorableType() == CREDIT_CARD_EXP_MONTH)
263 return MonthComboboxModel::FormatMonth(instrument_->expiration_month());
265 return instrument_->GetInfo(type, g_browser_process->GetApplicationLocale());
268 base::string16 WalletInstrumentWrapper::GetInfoForDisplay(
269 const AutofillType& type) const {
270 if (type.GetStorableType() == PHONE_HOME_WHOLE_NUMBER)
271 return instrument_->address().DisplayPhoneNumber();
273 return DataModelWrapper::GetInfoForDisplay(type);
276 gfx::Image WalletInstrumentWrapper::GetIcon() {
277 return instrument_->CardIcon();
280 bool WalletInstrumentWrapper::GetDisplayText(
281 base::string16* vertically_compact,
282 base::string16* horizontally_compact) {
283 // TODO(dbeam): handle other instrument statuses? http://crbug.com/233048
284 if (instrument_->status() == wallet::WalletItems::MaskedInstrument::EXPIRED ||
285 !instrument_->address().is_complete_address()) {
289 if (!DataModelWrapper::GetDisplayText(vertically_compact,
290 horizontally_compact)) {
294 // TODO(estade): descriptive_name() is user-provided. Should we use it or
295 // just type + last 4 digits?
296 base::string16 line1 = instrument_->descriptive_name() + ASCIIToUTF16("\n");
297 *vertically_compact = line1 + *vertically_compact;
298 *horizontally_compact = line1 + *horizontally_compact;
302 // FullWalletBillingWrapper
304 FullWalletBillingWrapper::FullWalletBillingWrapper(
305 wallet::FullWallet* full_wallet)
306 : full_wallet_(full_wallet) {
307 DCHECK(full_wallet_);
310 FullWalletBillingWrapper::~FullWalletBillingWrapper() {}
312 base::string16 FullWalletBillingWrapper::GetInfo(const AutofillType& type)
314 return full_wallet_->GetInfo(
315 g_browser_process->GetApplicationLocale(),
316 AutofillType(AutofillType::GetEquivalentBillingFieldType(
317 type.GetStorableType())));
320 bool FullWalletBillingWrapper::GetDisplayText(
321 base::string16* vertically_compact,
322 base::string16* horizontally_compact) {
323 // TODO(dbeam): handle other required actions? http://crbug.com/163508
324 if (full_wallet_->HasRequiredAction(wallet::UPDATE_EXPIRATION_DATE))
327 return DataModelWrapper::GetDisplayText(vertically_compact,
328 horizontally_compact);
331 // FullWalletShippingWrapper
333 FullWalletShippingWrapper::FullWalletShippingWrapper(
334 wallet::FullWallet* full_wallet)
335 : full_wallet_(full_wallet) {
336 DCHECK(full_wallet_);
339 FullWalletShippingWrapper::~FullWalletShippingWrapper() {}
341 base::string16 FullWalletShippingWrapper::GetInfo(
342 const AutofillType& type) const {
343 return full_wallet_->shipping_address()->GetInfo(
344 type, g_browser_process->GetApplicationLocale());
347 } // namespace autofill