data_model_wrapper.cc revision a1401311d1ab56c4ed0a474bd38c108f75cb0cd9
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_i18n_input.h" 13#include "chrome/browser/ui/autofill/autofill_dialog_models.h" 14#include "components/autofill/content/browser/wallet/full_wallet.h" 15#include "components/autofill/content/browser/wallet/wallet_address.h" 16#include "components/autofill/content/browser/wallet/wallet_items.h" 17#include "components/autofill/core/browser/autofill_data_model.h" 18#include "components/autofill/core/browser/autofill_field.h" 19#include "components/autofill/core/browser/autofill_profile.h" 20#include "components/autofill/core/browser/autofill_type.h" 21#include "components/autofill/core/browser/credit_card.h" 22#include "components/autofill/core/browser/form_structure.h" 23#include "third_party/libaddressinput/chromium/cpp/include/libaddressinput/address_data.h" 24#include "third_party/libaddressinput/chromium/cpp/include/libaddressinput/address_ui.h" 25#include "ui/base/resource/resource_bundle.h" 26#include "ui/gfx/image/image.h" 27 28namespace autofill { 29 30DataModelWrapper::~DataModelWrapper() {} 31 32void DataModelWrapper::FillInputs(DetailInputs* inputs) { 33 for (size_t i = 0; i < inputs->size(); ++i) { 34 (*inputs)[i].initial_value = GetInfo(AutofillType((*inputs)[i].type)); 35 } 36} 37 38base::string16 DataModelWrapper::GetInfoForDisplay(const AutofillType& type) 39 const { 40 return GetInfo(type); 41} 42 43gfx::Image DataModelWrapper::GetIcon() { 44 return gfx::Image(); 45} 46 47bool DataModelWrapper::GetDisplayText( 48 base::string16* vertically_compact, 49 base::string16* horizontally_compact) { 50 base::string16 phone = 51 GetInfoForDisplay(AutofillType(PHONE_HOME_WHOLE_NUMBER)); 52 if (phone.empty()) 53 return false; 54 55 // Format the address. 56 ::i18n::addressinput::AddressData address_data; 57 i18ninput::CreateAddressData( 58 base::Bind(&DataModelWrapper::GetInfo, base::Unretained(this)), 59 &address_data); 60 std::vector<std::string> lines; 61 address_data.FormatForDisplay(&lines); 62 63 // Email and phone number aren't part of address formatting. 64 base::string16 non_address_info; 65 base::string16 email = GetInfoForDisplay(AutofillType(EMAIL_ADDRESS)); 66 if (!email.empty()) 67 non_address_info += base::ASCIIToUTF16("\n") + email; 68 69 non_address_info += base::ASCIIToUTF16("\n") + phone; 70 71 // The separator is locale-specific. 72 std::string compact_separator = 73 ::i18n::addressinput::GetCompactAddressLinesSeparator( 74 g_browser_process->GetApplicationLocale()); 75 *vertically_compact = 76 base::UTF8ToUTF16(JoinString(lines, compact_separator)) + 77 non_address_info; 78 *horizontally_compact = base::UTF8ToUTF16(JoinString(lines, "\n")) + 79 non_address_info; 80 81 return true; 82} 83 84bool DataModelWrapper::FillFormStructure( 85 const std::vector<ServerFieldType>& types, 86 const FormStructure::InputFieldComparator& compare, 87 FormStructure* form_structure) const { 88 return form_structure->FillFields( 89 types, 90 compare, 91 base::Bind(&DataModelWrapper::GetInfo, base::Unretained(this)), 92 g_browser_process->GetApplicationLocale()); 93} 94 95DataModelWrapper::DataModelWrapper() {} 96 97// AutofillProfileWrapper 98 99AutofillProfileWrapper::AutofillProfileWrapper(const AutofillProfile* profile) 100 : profile_(profile), 101 variant_group_(NO_GROUP), 102 variant_(0) {} 103 104AutofillProfileWrapper::AutofillProfileWrapper( 105 const AutofillProfile* profile, 106 const AutofillType& type, 107 size_t variant) 108 : profile_(profile), 109 variant_group_(type.group()), 110 variant_(variant) {} 111 112AutofillProfileWrapper::~AutofillProfileWrapper() {} 113 114base::string16 AutofillProfileWrapper::GetInfo(const AutofillType& type) const { 115 // Requests for the user's credit card are filled from the billing address, 116 // but the AutofillProfile class doesn't know how to fill credit card 117 // fields. So, request for the corresponding profile type instead. 118 AutofillType effective_type = type; 119 if (type.GetStorableType() == CREDIT_CARD_NAME) 120 effective_type = AutofillType(NAME_BILLING_FULL); 121 122 size_t variant = GetVariantForType(effective_type); 123 const std::string& app_locale = g_browser_process->GetApplicationLocale(); 124 return profile_->GetInfoForVariant(effective_type, variant, app_locale); 125} 126 127base::string16 AutofillProfileWrapper::GetInfoForDisplay( 128 const AutofillType& type) const { 129 // We display the "raw" phone number which contains user-defined formatting. 130 if (type.GetStorableType() == PHONE_HOME_WHOLE_NUMBER) { 131 std::vector<base::string16> values; 132 profile_->GetRawMultiInfo(type.GetStorableType(), &values); 133 const base::string16& phone_number = values[GetVariantForType(type)]; 134 135 // If there is no user-defined formatting at all, add some standard 136 // formatting. 137 if (base::ContainsOnlyChars(phone_number, 138 base::ASCIIToUTF16("0123456789"))) { 139 std::string region = base::UTF16ToASCII( 140 GetInfo(AutofillType(HTML_TYPE_COUNTRY_CODE, HTML_MODE_NONE))); 141 i18n::PhoneObject phone(phone_number, region); 142 return phone.GetFormattedNumber(); 143 } 144 145 return phone_number; 146 } 147 148 return DataModelWrapper::GetInfoForDisplay(type); 149} 150 151size_t AutofillProfileWrapper::GetVariantForType(const AutofillType& type) 152 const { 153 if (type.group() == variant_group_) 154 return variant_; 155 156 return 0; 157} 158 159// AutofillShippingAddressWrapper 160 161AutofillShippingAddressWrapper::AutofillShippingAddressWrapper( 162 const AutofillProfile* profile) 163 : AutofillProfileWrapper(profile) {} 164 165AutofillShippingAddressWrapper::~AutofillShippingAddressWrapper() {} 166 167base::string16 AutofillShippingAddressWrapper::GetInfo( 168 const AutofillType& type) const { 169 // Shipping addresses don't have email addresses associated with them. 170 if (type.GetStorableType() == EMAIL_ADDRESS) 171 return base::string16(); 172 173 return AutofillProfileWrapper::GetInfo(type); 174} 175 176// AutofillCreditCardWrapper 177 178AutofillCreditCardWrapper::AutofillCreditCardWrapper(const CreditCard* card) 179 : card_(card) {} 180 181AutofillCreditCardWrapper::~AutofillCreditCardWrapper() {} 182 183base::string16 AutofillCreditCardWrapper::GetInfo(const AutofillType& type) 184 const { 185 if (type.group() != CREDIT_CARD) 186 return base::string16(); 187 188 if (type.GetStorableType() == CREDIT_CARD_EXP_MONTH) 189 return MonthComboboxModel::FormatMonth(card_->expiration_month()); 190 191 return card_->GetInfo(type, g_browser_process->GetApplicationLocale()); 192} 193 194gfx::Image AutofillCreditCardWrapper::GetIcon() { 195 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); 196 return rb.GetImageNamed(CreditCard::IconResourceId(card_->type())); 197} 198 199bool AutofillCreditCardWrapper::GetDisplayText( 200 base::string16* vertically_compact, 201 base::string16* horizontally_compact) { 202 if (!card_->IsValid()) 203 return false; 204 205 *vertically_compact = *horizontally_compact = card_->TypeAndLastFourDigits(); 206 return true; 207} 208 209// WalletAddressWrapper 210 211WalletAddressWrapper::WalletAddressWrapper( 212 const wallet::Address* address) : address_(address) {} 213 214WalletAddressWrapper::~WalletAddressWrapper() {} 215 216base::string16 WalletAddressWrapper::GetInfo(const AutofillType& type) const { 217 // Reachable from DataModelWrapper::GetDisplayText(). 218 if (type.GetStorableType() == EMAIL_ADDRESS) 219 return base::string16(); 220 221 return address_->GetInfo(type, g_browser_process->GetApplicationLocale()); 222} 223 224base::string16 WalletAddressWrapper::GetInfoForDisplay(const AutofillType& type) 225 const { 226 if (type.GetStorableType() == PHONE_HOME_WHOLE_NUMBER) 227 return address_->DisplayPhoneNumber(); 228 229 return DataModelWrapper::GetInfoForDisplay(type); 230} 231 232bool WalletAddressWrapper::GetDisplayText( 233 base::string16* vertically_compact, 234 base::string16* horizontally_compact) { 235 if (!address_->is_complete_address()) 236 return false; 237 238 return DataModelWrapper::GetDisplayText(vertically_compact, 239 horizontally_compact); 240} 241 242// WalletInstrumentWrapper 243 244WalletInstrumentWrapper::WalletInstrumentWrapper( 245 const wallet::WalletItems::MaskedInstrument* instrument) 246 : instrument_(instrument) {} 247 248WalletInstrumentWrapper::~WalletInstrumentWrapper() {} 249 250base::string16 WalletInstrumentWrapper::GetInfo(const AutofillType& type) 251 const { 252 // Reachable from DataModelWrapper::GetDisplayText(). 253 if (type.GetStorableType() == EMAIL_ADDRESS) 254 return base::string16(); 255 256 if (type.GetStorableType() == CREDIT_CARD_EXP_MONTH) 257 return MonthComboboxModel::FormatMonth(instrument_->expiration_month()); 258 259 return instrument_->GetInfo(type, g_browser_process->GetApplicationLocale()); 260} 261 262base::string16 WalletInstrumentWrapper::GetInfoForDisplay( 263 const AutofillType& type) const { 264 if (type.GetStorableType() == PHONE_HOME_WHOLE_NUMBER) 265 return instrument_->address().DisplayPhoneNumber(); 266 267 return DataModelWrapper::GetInfoForDisplay(type); 268} 269 270gfx::Image WalletInstrumentWrapper::GetIcon() { 271 return instrument_->CardIcon(); 272} 273 274bool WalletInstrumentWrapper::GetDisplayText( 275 base::string16* vertically_compact, 276 base::string16* horizontally_compact) { 277 // TODO(dbeam): handle other instrument statuses? http://crbug.com/233048 278 if (instrument_->status() == wallet::WalletItems::MaskedInstrument::EXPIRED || 279 !instrument_->address().is_complete_address()) { 280 return false; 281 } 282 283 if (!DataModelWrapper::GetDisplayText(vertically_compact, 284 horizontally_compact)) { 285 return false; 286 } 287 288 // TODO(estade): descriptive_name() is user-provided. Should we use it or 289 // just type + last 4 digits? 290 base::string16 line1 = 291 instrument_->descriptive_name() + base::ASCIIToUTF16("\n"); 292 *vertically_compact = line1 + *vertically_compact; 293 *horizontally_compact = line1 + *horizontally_compact; 294 return true; 295} 296 297// FullWalletBillingWrapper 298 299FullWalletBillingWrapper::FullWalletBillingWrapper( 300 wallet::FullWallet* full_wallet) 301 : full_wallet_(full_wallet) { 302 DCHECK(full_wallet_); 303} 304 305FullWalletBillingWrapper::~FullWalletBillingWrapper() {} 306 307base::string16 FullWalletBillingWrapper::GetInfo(const AutofillType& type) 308 const { 309 return full_wallet_->GetInfo( 310 g_browser_process->GetApplicationLocale(), 311 AutofillType(AutofillType::GetEquivalentBillingFieldType( 312 type.GetStorableType()))); 313} 314 315bool FullWalletBillingWrapper::GetDisplayText( 316 base::string16* vertically_compact, 317 base::string16* horizontally_compact) { 318 // TODO(dbeam): handle other required actions? http://crbug.com/163508 319 if (full_wallet_->HasRequiredAction(wallet::UPDATE_EXPIRATION_DATE)) 320 return false; 321 322 return DataModelWrapper::GetDisplayText(vertically_compact, 323 horizontally_compact); 324} 325 326// FullWalletShippingWrapper 327 328FullWalletShippingWrapper::FullWalletShippingWrapper( 329 wallet::FullWallet* full_wallet) 330 : full_wallet_(full_wallet) { 331 DCHECK(full_wallet_); 332} 333 334FullWalletShippingWrapper::~FullWalletShippingWrapper() {} 335 336base::string16 FullWalletShippingWrapper::GetInfo( 337 const AutofillType& type) const { 338 return full_wallet_->shipping_address()->GetInfo( 339 type, g_browser_process->GetApplicationLocale()); 340} 341 342} // namespace autofill 343