data_model_wrapper.cc revision eb525c5499e34cc9c4b825d6d9e75bb07cc06ace
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 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 (!card_->IsValid()) 144 return string16(); 145 146 return card_->TypeAndLastFourDigits(); 147} 148 149void AutofillCreditCardWrapper::FillFormField(AutofillField* field) const { 150 AutofillFieldType field_type = field->type(); 151 152 if (field_type == NAME_FULL) { 153 // Requests for the user's full name are filled from the credit card data, 154 // but the CreditCard class only knows how to fill credit card fields. So, 155 // temporarily set the type to the corresponding credit card type. 156 field->set_heuristic_type(CREDIT_CARD_NAME); 157 } 158 159 AutofillDataModelWrapper::FillFormField(field); 160 161 field->set_heuristic_type(field_type); 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 (AutofillType(type).group() == AutofillType::CREDIT_CARD) 229 return full_wallet_->GetInfo(type); 230 231 return full_wallet_->billing_address()->GetInfo( 232 type, g_browser_process->GetApplicationLocale()); 233} 234 235string16 FullWalletBillingWrapper::GetDisplayText() { 236 // TODO(dbeam): handle other required actions? http://crbug.com/163508 237 if (full_wallet_->HasRequiredAction(wallet::UPDATE_EXPIRATION_DATE)) 238 return string16(); 239 240 return DataModelWrapper::GetDisplayText(); 241} 242 243// FullWalletShippingWrapper 244 245FullWalletShippingWrapper::FullWalletShippingWrapper( 246 wallet::FullWallet* full_wallet) 247 : full_wallet_(full_wallet) { 248 DCHECK(full_wallet_); 249} 250 251FullWalletShippingWrapper::~FullWalletShippingWrapper() {} 252 253string16 FullWalletShippingWrapper::GetInfo(AutofillFieldType type) const { 254 return full_wallet_->shipping_address()->GetInfo( 255 type, g_browser_process->GetApplicationLocale()); 256} 257 258DetailOutputWrapper::DetailOutputWrapper(const DetailOutputMap& outputs) 259 : outputs_(outputs) {} 260 261DetailOutputWrapper::~DetailOutputWrapper() {} 262 263base::string16 DetailOutputWrapper::GetInfo(AutofillFieldType type) const { 264 for (DetailOutputMap::const_iterator it = outputs_.begin(); 265 it != outputs_.end(); ++it) { 266 if (type == it->first->type) 267 return it->second; 268 } 269 return base::string16(); 270} 271 272} // namespace autofill 273