Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / autofill / data_model_wrapper.cc
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 "base/bind.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"
28
29 namespace autofill {
30
31 using base::ASCIIToUTF16;
32 using base::UTF16ToUTF8;
33
34 DataModelWrapper::~DataModelWrapper() {}
35
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));
42   }
43 }
44
45 base::string16 DataModelWrapper::GetInfoForDisplay(const AutofillType& type)
46     const {
47   return GetInfo(type);
48 }
49
50 gfx::Image DataModelWrapper::GetIcon() {
51   return gfx::Image();
52 }
53
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));
59   if (phone.empty())
60     return false;
61
62   // Format the address.
63   ::i18n::addressinput::AddressData address_data;
64   i18ninput::CreateAddressData(
65       base::Bind(&DataModelWrapper::GetInfo, base::Unretained(this)),
66       &address_data);
67   std::vector<std::string> lines;
68   address_data.FormatForDisplay(&lines);
69
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));
73   if (!email.empty())
74     non_address_info += ASCIIToUTF16("\n") + email;
75
76   non_address_info += ASCIIToUTF16("\n") + phone;
77
78   // The separator is locale-specific.
79   std::string compact_separator =
80       ::i18n::addressinput::GetCompactAddressLinesSeparator(
81           g_browser_process->GetApplicationLocale());
82   *vertically_compact =
83       base::UTF8ToUTF16(JoinString(lines, compact_separator)) +
84           non_address_info;
85   *horizontally_compact = base::UTF8ToUTF16(JoinString(lines, "\n")) +
86       non_address_info;
87
88   return true;
89 }
90
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(
96       types,
97       compare,
98       base::Bind(&DataModelWrapper::GetInfo, base::Unretained(this)),
99       g_browser_process->GetApplicationLocale());
100 }
101
102 DataModelWrapper::DataModelWrapper() {}
103
104 // AutofillProfileWrapper
105
106 AutofillProfileWrapper::AutofillProfileWrapper(const AutofillProfile* profile)
107     : profile_(profile),
108       variant_group_(NO_GROUP),
109       variant_(0) {}
110
111 AutofillProfileWrapper::AutofillProfileWrapper(
112     const AutofillProfile* profile,
113     const AutofillType& type,
114     size_t variant)
115     : profile_(profile),
116       variant_group_(type.group()),
117       variant_(variant) {}
118
119 AutofillProfileWrapper::~AutofillProfileWrapper() {}
120
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);
128
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);
132 }
133
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)];
141
142     // If there is no user-defined formatting at all, add some standard
143     // formatting.
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();
149     }
150
151     return phone_number;
152   }
153
154   return DataModelWrapper::GetInfoForDisplay(type);
155 }
156
157 size_t AutofillProfileWrapper::GetVariantForType(const AutofillType& type)
158     const {
159   if (type.group() == variant_group_)
160     return variant_;
161
162   return 0;
163 }
164
165 // AutofillShippingAddressWrapper
166
167 AutofillShippingAddressWrapper::AutofillShippingAddressWrapper(
168     const AutofillProfile* profile)
169     : AutofillProfileWrapper(profile) {}
170
171 AutofillShippingAddressWrapper::~AutofillShippingAddressWrapper() {}
172
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();
178
179   return AutofillProfileWrapper::GetInfo(type);
180 }
181
182 // AutofillCreditCardWrapper
183
184 AutofillCreditCardWrapper::AutofillCreditCardWrapper(const CreditCard* card)
185     : card_(card) {}
186
187 AutofillCreditCardWrapper::~AutofillCreditCardWrapper() {}
188
189 base::string16 AutofillCreditCardWrapper::GetInfo(const AutofillType& type)
190     const {
191   if (type.group() != CREDIT_CARD)
192     return base::string16();
193
194   if (type.GetStorableType() == CREDIT_CARD_EXP_MONTH)
195     return MonthComboboxModel::FormatMonth(card_->expiration_month());
196
197   return card_->GetInfo(type, g_browser_process->GetApplicationLocale());
198 }
199
200 gfx::Image AutofillCreditCardWrapper::GetIcon() {
201   ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
202   return rb.GetImageNamed(CreditCard::IconResourceId(card_->type()));
203 }
204
205 bool AutofillCreditCardWrapper::GetDisplayText(
206     base::string16* vertically_compact,
207     base::string16* horizontally_compact) {
208   if (!card_->IsValid())
209     return false;
210
211   *vertically_compact = *horizontally_compact = card_->TypeAndLastFourDigits();
212   return true;
213 }
214
215 // WalletAddressWrapper
216
217 WalletAddressWrapper::WalletAddressWrapper(
218     const wallet::Address* address) : address_(address) {}
219
220 WalletAddressWrapper::~WalletAddressWrapper() {}
221
222 base::string16 WalletAddressWrapper::GetInfo(const AutofillType& type) const {
223   // Reachable from DataModelWrapper::GetDisplayText().
224   if (type.GetStorableType() == EMAIL_ADDRESS)
225     return base::string16();
226
227   return address_->GetInfo(type, g_browser_process->GetApplicationLocale());
228 }
229
230 base::string16 WalletAddressWrapper::GetInfoForDisplay(const AutofillType& type)
231     const {
232   if (type.GetStorableType() == PHONE_HOME_WHOLE_NUMBER)
233     return address_->DisplayPhoneNumber();
234
235   return DataModelWrapper::GetInfoForDisplay(type);
236 }
237
238 bool WalletAddressWrapper::GetDisplayText(
239     base::string16* vertically_compact,
240     base::string16* horizontally_compact) {
241   if (!address_->is_complete_address())
242     return false;
243
244   return DataModelWrapper::GetDisplayText(vertically_compact,
245                                           horizontally_compact);
246 }
247
248 // WalletInstrumentWrapper
249
250 WalletInstrumentWrapper::WalletInstrumentWrapper(
251     const wallet::WalletItems::MaskedInstrument* instrument)
252     : instrument_(instrument) {}
253
254 WalletInstrumentWrapper::~WalletInstrumentWrapper() {}
255
256 base::string16 WalletInstrumentWrapper::GetInfo(const AutofillType& type)
257     const {
258   // Reachable from DataModelWrapper::GetDisplayText().
259   if (type.GetStorableType() == EMAIL_ADDRESS)
260     return base::string16();
261
262   if (type.GetStorableType() == CREDIT_CARD_EXP_MONTH)
263     return MonthComboboxModel::FormatMonth(instrument_->expiration_month());
264
265   return instrument_->GetInfo(type, g_browser_process->GetApplicationLocale());
266 }
267
268 base::string16 WalletInstrumentWrapper::GetInfoForDisplay(
269     const AutofillType& type) const {
270   if (type.GetStorableType() == PHONE_HOME_WHOLE_NUMBER)
271     return instrument_->address().DisplayPhoneNumber();
272
273   return DataModelWrapper::GetInfoForDisplay(type);
274 }
275
276 gfx::Image WalletInstrumentWrapper::GetIcon() {
277   return instrument_->CardIcon();
278 }
279
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()) {
286     return false;
287   }
288
289   if (!DataModelWrapper::GetDisplayText(vertically_compact,
290                                         horizontally_compact)) {
291     return false;
292   }
293
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;
299   return true;
300 }
301
302 // FullWalletBillingWrapper
303
304 FullWalletBillingWrapper::FullWalletBillingWrapper(
305     wallet::FullWallet* full_wallet)
306     : full_wallet_(full_wallet) {
307   DCHECK(full_wallet_);
308 }
309
310 FullWalletBillingWrapper::~FullWalletBillingWrapper() {}
311
312 base::string16 FullWalletBillingWrapper::GetInfo(const AutofillType& type)
313     const {
314   return full_wallet_->GetInfo(
315       g_browser_process->GetApplicationLocale(),
316       AutofillType(AutofillType::GetEquivalentBillingFieldType(
317           type.GetStorableType())));
318 }
319
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))
325     return false;
326
327   return DataModelWrapper::GetDisplayText(vertically_compact,
328                                           horizontally_compact);
329 }
330
331 // FullWalletShippingWrapper
332
333 FullWalletShippingWrapper::FullWalletShippingWrapper(
334     wallet::FullWallet* full_wallet)
335     : full_wallet_(full_wallet) {
336   DCHECK(full_wallet_);
337 }
338
339 FullWalletShippingWrapper::~FullWalletShippingWrapper() {}
340
341 base::string16 FullWalletShippingWrapper::GetInfo(
342     const AutofillType& type) const {
343   return full_wallet_->shipping_address()->GetInfo(
344       type, g_browser_process->GetApplicationLocale());
345 }
346
347 }  // namespace autofill