data_model_wrapper.cc revision 90dce4d38c5ff5333bea97d859d4e484e27edf0c
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/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/browser/wallet/full_wallet.h" 18#include "components/autofill/browser/wallet/wallet_address.h" 19#include "components/autofill/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 40void DataModelWrapper::FillFormStructure( 41 const DetailInputs& inputs, 42 const InputFieldComparator& compare, 43 FormStructure* form_structure) { 44 for (size_t i = 0; i < form_structure->field_count(); ++i) { 45 AutofillField* field = form_structure->field(i); 46 for (size_t j = 0; j < inputs.size(); ++j) { 47 if (compare.Run(inputs[j], *field)) { 48 FillFormField(field); 49 break; 50 } 51 } 52 } 53} 54 55void DataModelWrapper::FillInputs(DetailInputs* inputs) { 56 for (size_t i = 0; i < inputs->size(); ++i) { 57 (*inputs)[i].initial_value = GetInfo((*inputs)[i].type); 58 } 59} 60 61void DataModelWrapper::FillFormField(AutofillField* field) { 62 field->value = GetInfo(field->type()); 63} 64 65DataModelWrapper::DataModelWrapper() {} 66 67gfx::Image DataModelWrapper::GetIcon() { 68 return gfx::Image(); 69} 70 71// AutofillDataModelWrapper 72 73AutofillDataModelWrapper::AutofillDataModelWrapper( 74 const AutofillDataModel* data_model, 75 size_t variant) 76 : data_model_(data_model), 77 variant_(variant) {} 78 79AutofillDataModelWrapper::~AutofillDataModelWrapper() {} 80 81string16 AutofillDataModelWrapper::GetInfo(AutofillFieldType type) { 82 return data_model_->GetInfo(type, g_browser_process->GetApplicationLocale()); 83} 84 85void AutofillDataModelWrapper::FillFormField(AutofillField* field) { 86 data_model_->FillFormField( 87 *field, variant_, g_browser_process->GetApplicationLocale(), field); 88} 89 90// AutofillProfileWrapper 91 92AutofillProfileWrapper::AutofillProfileWrapper( 93 const AutofillProfile* profile, size_t variant) 94 : AutofillDataModelWrapper(profile, variant), 95 profile_(profile) {} 96 97AutofillProfileWrapper::~AutofillProfileWrapper() {} 98 99void AutofillProfileWrapper::FillInputs(DetailInputs* inputs) { 100 const std::string app_locale = g_browser_process->GetApplicationLocale(); 101 for (size_t j = 0; j < inputs->size(); ++j) { 102 std::vector<string16> values; 103 profile_->GetMultiInfo((*inputs)[j].type, app_locale, &values); 104 (*inputs)[j].initial_value = values[variant()]; 105 } 106} 107 108// AutofillCreditCardWrapper 109 110AutofillCreditCardWrapper::AutofillCreditCardWrapper(const CreditCard* card) 111 : AutofillDataModelWrapper(card, 0), 112 card_(card) {} 113 114AutofillCreditCardWrapper::~AutofillCreditCardWrapper() {} 115 116string16 AutofillCreditCardWrapper::GetInfo(AutofillFieldType type) { 117 if (type == CREDIT_CARD_EXP_MONTH) 118 return MonthComboboxModel::FormatMonth(card_->expiration_month()); 119 120 return AutofillDataModelWrapper::GetInfo(type); 121} 122 123gfx::Image AutofillCreditCardWrapper::GetIcon() { 124 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); 125 return rb.GetImageNamed(card_->IconResourceId()); 126} 127 128string16 AutofillCreditCardWrapper::GetDisplayText() { 129 if (!autofill::IsValidCreditCardExpirationDate( 130 card_->GetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR), 131 card_->GetRawInfo(CREDIT_CARD_EXP_MONTH), 132 base::Time::Now())) { 133 return string16(); 134 } 135 136 return card_->TypeAndLastFourDigits(); 137} 138 139void AutofillCreditCardWrapper::FillFormField(AutofillField* field) { 140 AutofillFieldType field_type = field->type(); 141 142 if (field_type == NAME_FULL) { 143 // Requests for the user's full name are filled from the credit card data, 144 // but the CreditCard class only knows how to fill credit card fields. So, 145 // temporarily set the type to the corresponding credit card type. 146 field->set_heuristic_type(CREDIT_CARD_NAME); 147 } 148 149 AutofillDataModelWrapper::FillFormField(field); 150 151 field->set_heuristic_type(field_type); 152} 153 154// WalletAddressWrapper 155 156WalletAddressWrapper::WalletAddressWrapper( 157 const wallet::Address* address) : address_(address) {} 158 159WalletAddressWrapper::~WalletAddressWrapper() {} 160 161string16 WalletAddressWrapper::GetInfo(AutofillFieldType type) { 162 return address_->GetInfo(type, g_browser_process->GetApplicationLocale()); 163} 164 165string16 WalletAddressWrapper::GetDisplayText() { 166 if (!address_->is_complete_address()) 167 return string16(); 168 169 return DataModelWrapper::GetDisplayText(); 170} 171 172// WalletInstrumentWrapper 173 174WalletInstrumentWrapper::WalletInstrumentWrapper( 175 const wallet::WalletItems::MaskedInstrument* instrument) 176 : instrument_(instrument) {} 177 178WalletInstrumentWrapper::~WalletInstrumentWrapper() {} 179 180string16 WalletInstrumentWrapper::GetInfo(AutofillFieldType type) { 181 if (type == CREDIT_CARD_EXP_MONTH) 182 return MonthComboboxModel::FormatMonth(instrument_->expiration_month()); 183 184 return instrument_->GetInfo(type, g_browser_process->GetApplicationLocale()); 185} 186 187gfx::Image WalletInstrumentWrapper::GetIcon() { 188 return instrument_->CardIcon(); 189} 190 191string16 WalletInstrumentWrapper::GetDisplayText() { 192 // TODO(dbeam): handle other instrument statuses? http://crbug.com/233048 193 if (instrument_->status() == wallet::WalletItems::MaskedInstrument::EXPIRED || 194 !instrument_->address().is_complete_address()) { 195 return string16(); 196 } 197 198 // TODO(estade): descriptive_name() is user-provided. Should we use it or 199 // just type + last 4 digits? 200 string16 line1 = instrument_->descriptive_name(); 201 return line1 + ASCIIToUTF16("\n") + DataModelWrapper::GetDisplayText(); 202} 203 204// FullWalletBillingWrapper 205 206FullWalletBillingWrapper::FullWalletBillingWrapper( 207 wallet::FullWallet* full_wallet) 208 : full_wallet_(full_wallet) { 209 DCHECK(full_wallet_); 210} 211 212FullWalletBillingWrapper::~FullWalletBillingWrapper() {} 213 214string16 FullWalletBillingWrapper::GetInfo(AutofillFieldType type) { 215 if (AutofillType(type).group() == AutofillType::CREDIT_CARD) 216 return full_wallet_->GetInfo(type); 217 218 return full_wallet_->billing_address()->GetInfo( 219 type, g_browser_process->GetApplicationLocale()); 220} 221 222string16 FullWalletBillingWrapper::GetDisplayText() { 223 // TODO(dbeam): handle other required actions? http://crbug.com/163508 224 if (full_wallet_->HasRequiredAction(wallet::UPDATE_EXPIRATION_DATE)) 225 return string16(); 226 227 return DataModelWrapper::GetDisplayText(); 228} 229 230// FullWalletShippingWrapper 231 232FullWalletShippingWrapper::FullWalletShippingWrapper( 233 wallet::FullWallet* full_wallet) 234 : full_wallet_(full_wallet) { 235 DCHECK(full_wallet_); 236} 237 238FullWalletShippingWrapper::~FullWalletShippingWrapper() {} 239 240string16 FullWalletShippingWrapper::GetInfo(AutofillFieldType type) { 241 return full_wallet_->shipping_address()->GetInfo( 242 type, g_browser_process->GetApplicationLocale()); 243} 244 245} // namespace autofill 246