data_model_wrapper.cc revision 7d4cd473f85ac64c3747c96c277f9e506a0d2246
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/browser/autofill_data_model.h" 12#include "components/autofill/browser/autofill_profile.h" 13#include "components/autofill/browser/autofill_type.h" 14#include "components/autofill/browser/credit_card.h" 15#include "components/autofill/browser/form_structure.h" 16#include "components/autofill/browser/validation.h" 17#include "components/autofill/content/browser/wallet/full_wallet.h" 18#include "components/autofill/content/browser/wallet/wallet_address.h" 19#include "components/autofill/content/browser/wallet/wallet_items.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 122// AutofillCreditCardWrapper 123 124AutofillCreditCardWrapper::AutofillCreditCardWrapper(const CreditCard* card) 125 : AutofillDataModelWrapper(card, 0), 126 card_(card) {} 127 128AutofillCreditCardWrapper::~AutofillCreditCardWrapper() {} 129 130string16 AutofillCreditCardWrapper::GetInfo(AutofillFieldType type) const { 131 if (type == CREDIT_CARD_EXP_MONTH) 132 return MonthComboboxModel::FormatMonth(card_->expiration_month()); 133 134 return AutofillDataModelWrapper::GetInfo(type); 135} 136 137gfx::Image AutofillCreditCardWrapper::GetIcon() { 138 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); 139 return rb.GetImageNamed(CreditCard::IconResourceId(card_->type())); 140} 141 142string16 AutofillCreditCardWrapper::GetDisplayText() { 143 if (!autofill::IsValidCreditCardExpirationDate( 144 card_->GetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR), 145 card_->GetRawInfo(CREDIT_CARD_EXP_MONTH), 146 base::Time::Now())) { 147 return string16(); 148 } 149 150 return card_->TypeAndLastFourDigits(); 151} 152 153void AutofillCreditCardWrapper::FillFormField(AutofillField* field) const { 154 AutofillFieldType field_type = field->type(); 155 156 if (field_type == NAME_FULL) { 157 // Requests for the user's full name are filled from the credit card data, 158 // but the CreditCard class only knows how to fill credit card fields. So, 159 // temporarily set the type to the corresponding credit card type. 160 field->set_heuristic_type(CREDIT_CARD_NAME); 161 } 162 163 AutofillDataModelWrapper::FillFormField(field); 164 165 field->set_heuristic_type(field_type); 166} 167 168// WalletAddressWrapper 169 170WalletAddressWrapper::WalletAddressWrapper( 171 const wallet::Address* address) : address_(address) {} 172 173WalletAddressWrapper::~WalletAddressWrapper() {} 174 175string16 WalletAddressWrapper::GetInfo(AutofillFieldType type) const { 176 return address_->GetInfo(type, g_browser_process->GetApplicationLocale()); 177} 178 179string16 WalletAddressWrapper::GetDisplayText() { 180 if (!address_->is_complete_address() || 181 GetInfo(PHONE_HOME_WHOLE_NUMBER).empty()) { 182 return string16(); 183 } 184 185 return DataModelWrapper::GetDisplayText(); 186} 187 188// WalletInstrumentWrapper 189 190WalletInstrumentWrapper::WalletInstrumentWrapper( 191 const wallet::WalletItems::MaskedInstrument* instrument) 192 : instrument_(instrument) {} 193 194WalletInstrumentWrapper::~WalletInstrumentWrapper() {} 195 196string16 WalletInstrumentWrapper::GetInfo(AutofillFieldType type) const { 197 if (type == CREDIT_CARD_EXP_MONTH) 198 return MonthComboboxModel::FormatMonth(instrument_->expiration_month()); 199 200 return instrument_->GetInfo(type, g_browser_process->GetApplicationLocale()); 201} 202 203gfx::Image WalletInstrumentWrapper::GetIcon() { 204 return instrument_->CardIcon(); 205} 206 207string16 WalletInstrumentWrapper::GetDisplayText() { 208 // TODO(dbeam): handle other instrument statuses? http://crbug.com/233048 209 if (instrument_->status() == wallet::WalletItems::MaskedInstrument::EXPIRED || 210 !instrument_->address().is_complete_address() || 211 GetInfo(PHONE_HOME_WHOLE_NUMBER).empty()) { 212 return string16(); 213 } 214 215 // TODO(estade): descriptive_name() is user-provided. Should we use it or 216 // just type + last 4 digits? 217 string16 line1 = instrument_->descriptive_name(); 218 return line1 + ASCIIToUTF16("\n") + DataModelWrapper::GetDisplayText(); 219} 220 221// FullWalletBillingWrapper 222 223FullWalletBillingWrapper::FullWalletBillingWrapper( 224 wallet::FullWallet* full_wallet) 225 : full_wallet_(full_wallet) { 226 DCHECK(full_wallet_); 227} 228 229FullWalletBillingWrapper::~FullWalletBillingWrapper() {} 230 231string16 FullWalletBillingWrapper::GetInfo(AutofillFieldType type) const { 232 if (AutofillType(type).group() == AutofillType::CREDIT_CARD) 233 return full_wallet_->GetInfo(type); 234 235 return full_wallet_->billing_address()->GetInfo( 236 type, g_browser_process->GetApplicationLocale()); 237} 238 239string16 FullWalletBillingWrapper::GetDisplayText() { 240 // TODO(dbeam): handle other required actions? http://crbug.com/163508 241 if (full_wallet_->HasRequiredAction(wallet::UPDATE_EXPIRATION_DATE)) 242 return string16(); 243 244 return DataModelWrapper::GetDisplayText(); 245} 246 247// FullWalletShippingWrapper 248 249FullWalletShippingWrapper::FullWalletShippingWrapper( 250 wallet::FullWallet* full_wallet) 251 : full_wallet_(full_wallet) { 252 DCHECK(full_wallet_); 253} 254 255FullWalletShippingWrapper::~FullWalletShippingWrapper() {} 256 257string16 FullWalletShippingWrapper::GetInfo(AutofillFieldType type) const { 258 return full_wallet_->shipping_address()->GetInfo( 259 type, g_browser_process->GetApplicationLocale()); 260} 261 262} // namespace autofill 263