data_model_wrapper.cc revision 2385ea399aae016c0806a4f9ef3c9cfe3d2a39df
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/callback.h"
8#include "base/strings/utf_string_conversions.h"
9#include "chrome/browser/browser_process.h"
10#include "chrome/browser/ui/autofill/autofill_dialog_models.h"
11#include "components/autofill/content/browser/wallet/full_wallet.h"
12#include "components/autofill/content/browser/wallet/wallet_address.h"
13#include "components/autofill/content/browser/wallet/wallet_items.h"
14#include "components/autofill/core/browser/autofill_data_model.h"
15#include "components/autofill/core/browser/autofill_profile.h"
16#include "components/autofill/core/browser/autofill_type.h"
17#include "components/autofill/core/browser/credit_card.h"
18#include "components/autofill/core/browser/form_structure.h"
19#include "components/autofill/core/browser/validation.h"
20#include "ui/base/resource/resource_bundle.h"
21#include "ui/gfx/image/image.h"
22
23namespace autofill {
24
25DataModelWrapper::~DataModelWrapper() {}
26
27string16 DataModelWrapper::GetDisplayText() {
28  string16 comma = ASCIIToUTF16(", ");
29  string16 label = GetInfo(NAME_FULL) + comma + GetInfo(ADDRESS_HOME_LINE1);
30  string16 address2 = GetInfo(ADDRESS_HOME_LINE2);
31  if (!address2.empty())
32    label += comma + address2;
33  label += ASCIIToUTF16("\n") +
34      GetInfo(ADDRESS_HOME_CITY) + comma +
35      GetInfo(ADDRESS_HOME_STATE) + ASCIIToUTF16(" ") +
36      GetInfo(ADDRESS_HOME_ZIP);
37  return label;
38}
39
40bool DataModelWrapper::FillFormStructure(
41    const DetailInputs& inputs,
42    const InputFieldComparator& compare,
43    FormStructure* form_structure) const {
44  bool filled_something = false;
45  for (size_t i = 0; i < form_structure->field_count(); ++i) {
46    AutofillField* field = form_structure->field(i);
47    for (size_t j = 0; j < inputs.size(); ++j) {
48      if (compare.Run(inputs[j], *field)) {
49        FillFormField(field);
50        filled_something = true;
51        break;
52      }
53    }
54  }
55  return filled_something;
56}
57
58void DataModelWrapper::FillInputs(DetailInputs* inputs) {
59  for (size_t i = 0; i < inputs->size(); ++i) {
60    (*inputs)[i].initial_value = GetInfo((*inputs)[i].type);
61  }
62}
63
64void DataModelWrapper::FillFormField(AutofillField* field) const {
65  field->value = GetInfo(field->type());
66}
67
68DataModelWrapper::DataModelWrapper() {}
69
70gfx::Image DataModelWrapper::GetIcon() {
71  return gfx::Image();
72}
73
74// EmptyDataModelWrapper
75
76EmptyDataModelWrapper::EmptyDataModelWrapper() {}
77EmptyDataModelWrapper::~EmptyDataModelWrapper() {}
78
79string16 EmptyDataModelWrapper::GetInfo(AutofillFieldType type) const {
80  return string16();
81}
82
83void EmptyDataModelWrapper::FillFormField(AutofillField* field) const {}
84
85// AutofillDataModelWrapper
86
87AutofillDataModelWrapper::AutofillDataModelWrapper(
88    const AutofillDataModel* data_model,
89    size_t variant)
90    : data_model_(data_model),
91      variant_(variant) {}
92
93AutofillDataModelWrapper::~AutofillDataModelWrapper() {}
94
95string16 AutofillDataModelWrapper::GetInfo(AutofillFieldType type) const {
96  return data_model_->GetInfo(type, g_browser_process->GetApplicationLocale());
97}
98
99void AutofillDataModelWrapper::FillFormField(AutofillField* field) const {
100  data_model_->FillFormField(
101      *field, variant_, g_browser_process->GetApplicationLocale(), field);
102}
103
104// AutofillProfileWrapper
105
106AutofillProfileWrapper::AutofillProfileWrapper(
107    const AutofillProfile* profile, size_t variant)
108    : AutofillDataModelWrapper(profile, variant),
109      profile_(profile) {}
110
111AutofillProfileWrapper::~AutofillProfileWrapper() {}
112
113void AutofillProfileWrapper::FillInputs(DetailInputs* inputs) {
114  const std::string app_locale = g_browser_process->GetApplicationLocale();
115  for (size_t j = 0; j < inputs->size(); ++j) {
116    std::vector<string16> values;
117    profile_->GetMultiInfo((*inputs)[j].type, app_locale, &values);
118    (*inputs)[j].initial_value = values[variant()];
119  }
120}
121
122void AutofillProfileWrapper::FillFormField(AutofillField* field) const {
123  AutofillFieldType field_type = field->type();
124
125  if (field_type == CREDIT_CARD_NAME) {
126    // Requests for the user's credit card are filled from the billing address,
127    // but the AutofillProfile class doesn't know how to fill credit card
128    // fields. So, temporarily set the type to the corresponding profile type.
129    field->set_heuristic_type(NAME_FULL);
130  }
131
132  AutofillDataModelWrapper::FillFormField(field);
133
134  field->set_heuristic_type(field_type);
135}
136
137// AutofillCreditCardWrapper
138
139AutofillCreditCardWrapper::AutofillCreditCardWrapper(const CreditCard* card)
140    : AutofillDataModelWrapper(card, 0),
141      card_(card) {}
142
143AutofillCreditCardWrapper::~AutofillCreditCardWrapper() {}
144
145string16 AutofillCreditCardWrapper::GetInfo(AutofillFieldType type) const {
146  if (type == CREDIT_CARD_EXP_MONTH)
147    return MonthComboboxModel::FormatMonth(card_->expiration_month());
148
149  return AutofillDataModelWrapper::GetInfo(type);
150}
151
152gfx::Image AutofillCreditCardWrapper::GetIcon() {
153  ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
154  return rb.GetImageNamed(CreditCard::IconResourceId(card_->type()));
155}
156
157string16 AutofillCreditCardWrapper::GetDisplayText() {
158  if (!card_->IsValid())
159    return string16();
160
161  return card_->TypeAndLastFourDigits();
162}
163
164// WalletAddressWrapper
165
166WalletAddressWrapper::WalletAddressWrapper(
167    const wallet::Address* address) : address_(address) {}
168
169WalletAddressWrapper::~WalletAddressWrapper() {}
170
171string16 WalletAddressWrapper::GetInfo(AutofillFieldType type) const {
172  return address_->GetInfo(type, g_browser_process->GetApplicationLocale());
173}
174
175string16 WalletAddressWrapper::GetDisplayText() {
176  if (!address_->is_complete_address() ||
177      GetInfo(PHONE_HOME_WHOLE_NUMBER).empty()) {
178    return string16();
179  }
180
181  return DataModelWrapper::GetDisplayText();
182}
183
184// WalletInstrumentWrapper
185
186WalletInstrumentWrapper::WalletInstrumentWrapper(
187    const wallet::WalletItems::MaskedInstrument* instrument)
188    : instrument_(instrument) {}
189
190WalletInstrumentWrapper::~WalletInstrumentWrapper() {}
191
192string16 WalletInstrumentWrapper::GetInfo(AutofillFieldType type) const {
193  if (type == CREDIT_CARD_EXP_MONTH)
194    return MonthComboboxModel::FormatMonth(instrument_->expiration_month());
195
196  return instrument_->GetInfo(type, g_browser_process->GetApplicationLocale());
197}
198
199gfx::Image WalletInstrumentWrapper::GetIcon() {
200  return instrument_->CardIcon();
201}
202
203string16 WalletInstrumentWrapper::GetDisplayText() {
204  // TODO(dbeam): handle other instrument statuses? http://crbug.com/233048
205  if (instrument_->status() == wallet::WalletItems::MaskedInstrument::EXPIRED ||
206      !instrument_->address().is_complete_address() ||
207      GetInfo(PHONE_HOME_WHOLE_NUMBER).empty()) {
208    return string16();
209  }
210
211  // TODO(estade): descriptive_name() is user-provided. Should we use it or
212  // just type + last 4 digits?
213  string16 line1 = instrument_->descriptive_name();
214  return line1 + ASCIIToUTF16("\n") + DataModelWrapper::GetDisplayText();
215}
216
217// FullWalletBillingWrapper
218
219FullWalletBillingWrapper::FullWalletBillingWrapper(
220    wallet::FullWallet* full_wallet)
221    : full_wallet_(full_wallet) {
222  DCHECK(full_wallet_);
223}
224
225FullWalletBillingWrapper::~FullWalletBillingWrapper() {}
226
227string16 FullWalletBillingWrapper::GetInfo(AutofillFieldType type) const {
228  if (type == CREDIT_CARD_EXP_MONTH)
229    return MonthComboboxModel::FormatMonth(full_wallet_->expiration_month());
230
231  if (AutofillType(type).group() == CREDIT_CARD)
232    return full_wallet_->GetInfo(type);
233
234  return full_wallet_->billing_address()->GetInfo(
235      type, g_browser_process->GetApplicationLocale());
236}
237
238string16 FullWalletBillingWrapper::GetDisplayText() {
239  // TODO(dbeam): handle other required actions? http://crbug.com/163508
240  if (full_wallet_->HasRequiredAction(wallet::UPDATE_EXPIRATION_DATE))
241    return string16();
242
243  return DataModelWrapper::GetDisplayText();
244}
245
246// FullWalletShippingWrapper
247
248FullWalletShippingWrapper::FullWalletShippingWrapper(
249    wallet::FullWallet* full_wallet)
250    : full_wallet_(full_wallet) {
251  DCHECK(full_wallet_);
252}
253
254FullWalletShippingWrapper::~FullWalletShippingWrapper() {}
255
256string16 FullWalletShippingWrapper::GetInfo(AutofillFieldType type) const {
257  return full_wallet_->shipping_address()->GetInfo(
258      type, g_browser_process->GetApplicationLocale());
259}
260
261DetailOutputWrapper::DetailOutputWrapper(const DetailOutputMap& outputs)
262    : outputs_(outputs) {}
263
264DetailOutputWrapper::~DetailOutputWrapper() {}
265
266base::string16 DetailOutputWrapper::GetInfo(AutofillFieldType type) const {
267  for (DetailOutputMap::const_iterator it = outputs_.begin();
268       it != outputs_.end(); ++it) {
269    if (type == it->first->type)
270      return it->second;
271  }
272  return base::string16();
273}
274
275}  // namespace autofill
276