Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/ui/autofill/data_model_wrapper.h" | 5 #include "chrome/browser/ui/autofill/data_model_wrapper.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | |
| 7 #include "base/callback.h" | 8 #include "base/callback.h" |
| 8 #include "base/strings/string_util.h" | 9 #include "base/strings/string_util.h" |
| 9 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
| 10 #include "chrome/browser/browser_process.h" | 11 #include "chrome/browser/browser_process.h" |
| 11 #include "chrome/browser/ui/autofill/autofill_dialog_common.h" | 12 #include "chrome/browser/ui/autofill/autofill_dialog_common.h" |
| 12 #include "chrome/browser/ui/autofill/autofill_dialog_models.h" | 13 #include "chrome/browser/ui/autofill/autofill_dialog_models.h" |
| 13 #include "components/autofill/content/browser/wallet/full_wallet.h" | 14 #include "components/autofill/content/browser/wallet/full_wallet.h" |
| 14 #include "components/autofill/content/browser/wallet/wallet_address.h" | 15 #include "components/autofill/content/browser/wallet/wallet_address.h" |
| 15 #include "components/autofill/content/browser/wallet/wallet_items.h" | 16 #include "components/autofill/content/browser/wallet/wallet_items.h" |
| 16 #include "components/autofill/core/browser/autofill_data_model.h" | 17 #include "components/autofill/core/browser/autofill_data_model.h" |
| 17 #include "components/autofill/core/browser/autofill_field.h" | 18 #include "components/autofill/core/browser/autofill_field.h" |
| 18 #include "components/autofill/core/browser/autofill_profile.h" | 19 #include "components/autofill/core/browser/autofill_profile.h" |
| 19 #include "components/autofill/core/browser/autofill_type.h" | 20 #include "components/autofill/core/browser/autofill_type.h" |
| 20 #include "components/autofill/core/browser/credit_card.h" | 21 #include "components/autofill/core/browser/credit_card.h" |
| 21 #include "components/autofill/core/browser/form_structure.h" | 22 #include "components/autofill/core/browser/form_structure.h" |
| 23 #include "third_party/libaddressinput/chromium/cpp/include/libaddressinput/addre ss_data.h" | |
| 24 #include "third_party/libaddressinput/chromium/cpp/include/libaddressinput/addre ss_ui.h" | |
| 22 #include "ui/base/resource/resource_bundle.h" | 25 #include "ui/base/resource/resource_bundle.h" |
| 23 #include "ui/gfx/image/image.h" | 26 #include "ui/gfx/image/image.h" |
| 24 | 27 |
| 25 #if !defined(OS_ANDROID) | |
| 26 #include "third_party/libaddressinput/chromium/cpp/include/libaddressinput/addre ss_data.h" | |
| 27 #include "third_party/libaddressinput/chromium/cpp/include/libaddressinput/addre ss_ui.h" | |
| 28 #endif | |
| 29 | |
| 30 namespace autofill { | 28 namespace autofill { |
| 31 | 29 |
| 32 using base::ASCIIToUTF16; | 30 using base::ASCIIToUTF16; |
| 33 using base::UTF16ToUTF8; | 31 using base::UTF16ToUTF8; |
| 34 | 32 |
| 35 DataModelWrapper::~DataModelWrapper() {} | 33 DataModelWrapper::~DataModelWrapper() {} |
| 36 | 34 |
| 37 void DataModelWrapper::FillInputs(DetailInputs* inputs) { | 35 void DataModelWrapper::FillInputs(DetailInputs* inputs) { |
| 38 for (size_t i = 0; i < inputs->size(); ++i) { | 36 for (size_t i = 0; i < inputs->size(); ++i) { |
| 39 DetailInput* input = &(*inputs)[i]; | 37 DetailInput* input = &(*inputs)[i]; |
| 40 input->initial_value = common::GetHardcodedValueForType(input->type); | 38 input->initial_value = common::GetHardcodedValueForType(input->type); |
| 41 if (input->initial_value.empty()) | 39 if (input->initial_value.empty()) |
| 42 input->initial_value = GetInfo(AutofillType(input->type)); | 40 input->initial_value = GetInfo(AutofillType(input->type)); |
| 43 } | 41 } |
| 44 } | 42 } |
| 45 | 43 |
| 46 base::string16 DataModelWrapper::GetInfoForDisplay(const AutofillType& type) | 44 base::string16 DataModelWrapper::GetInfoForDisplay(const AutofillType& type) |
| 47 const { | 45 const { |
| 48 return GetInfo(type); | 46 return GetInfo(type); |
| 49 } | 47 } |
| 50 | 48 |
| 51 gfx::Image DataModelWrapper::GetIcon() { | 49 gfx::Image DataModelWrapper::GetIcon() { |
| 52 return gfx::Image(); | 50 return gfx::Image(); |
| 53 } | 51 } |
| 54 | 52 |
| 55 #if !defined(OS_ANDROID) | |
| 56 bool DataModelWrapper::GetDisplayText( | 53 bool DataModelWrapper::GetDisplayText( |
| 57 base::string16* vertically_compact, | 54 base::string16* vertically_compact, |
| 58 base::string16* horizontally_compact) { | 55 base::string16* horizontally_compact) { |
| 59 // Format the address. | 56 // Format the address. |
| 60 ::i18n::addressinput::AddressData address_data; | 57 ::i18n::addressinput::AddressData address_data; |
| 61 address_data.recipient = UTF16ToUTF8( | 58 address_data.recipient = UTF16ToUTF8( |
| 62 GetInfoForDisplay(AutofillType(NAME_FULL))); | 59 GetInfoForDisplay(AutofillType(NAME_FULL))); |
| 63 address_data.country_code = UTF16ToUTF8( | 60 address_data.country_code = UTF16ToUTF8( |
| 64 GetInfoForDisplay(AutofillType(ADDRESS_HOME_COUNTRY))); | 61 GetInfoForDisplay(AutofillType(ADDRESS_HOME_COUNTRY))); |
| 65 address_data.administrative_area = UTF16ToUTF8( | 62 address_data.administrative_area = UTF16ToUTF8( |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 96 ::i18n::addressinput::GetCompactAddressLinesSeparator( | 93 ::i18n::addressinput::GetCompactAddressLinesSeparator( |
| 97 g_browser_process->GetApplicationLocale()); | 94 g_browser_process->GetApplicationLocale()); |
| 98 *vertically_compact = | 95 *vertically_compact = |
| 99 base::UTF8ToUTF16(JoinString(lines, compact_separator)) + | 96 base::UTF8ToUTF16(JoinString(lines, compact_separator)) + |
| 100 non_address_info; | 97 non_address_info; |
| 101 *horizontally_compact = base::UTF8ToUTF16(JoinString(lines, "\n")) + | 98 *horizontally_compact = base::UTF8ToUTF16(JoinString(lines, "\n")) + |
| 102 non_address_info; | 99 non_address_info; |
| 103 | 100 |
| 104 return true; | 101 return true; |
| 105 } | 102 } |
| 106 #endif | |
| 107 | 103 |
| 108 bool DataModelWrapper::FillFormStructure( | 104 bool DataModelWrapper::FillFormStructure( |
| 109 const DetailInputs& inputs, | 105 const std::vector<ServerFieldType>& types, |
| 110 const InputFieldComparator& compare, | 106 const FormStructure::InputFieldComparator& compare, |
| 111 FormStructure* form_structure) const { | 107 FormStructure* form_structure) const { |
| 112 bool filled_something = false; | 108 return form_structure->FillFields( |
| 113 for (size_t i = 0; i < form_structure->field_count(); ++i) { | 109 types, |
| 114 AutofillField* field = form_structure->field(i); | 110 compare, |
| 115 for (size_t j = 0; j < inputs.size(); ++j) { | 111 base::Bind(&DataModelWrapper::GetInfo, base::Unretained(this)), |
| 116 if (compare.Run(inputs[j].type, *field)) { | 112 g_browser_process->GetApplicationLocale()); |
| 117 AutofillField::FillFormField(*field, GetInfo(field->Type()), | |
| 118 g_browser_process->GetApplicationLocale(), | |
| 119 field); | |
| 120 filled_something = true; | |
| 121 break; | |
| 122 } | |
| 123 } | |
| 124 } | |
| 125 return filled_something; | |
| 126 } | 113 } |
| 127 | 114 |
| 128 DataModelWrapper::DataModelWrapper() {} | 115 DataModelWrapper::DataModelWrapper() {} |
| 129 | 116 |
| 130 // EmptyDataModelWrapper | |
| 131 | |
| 132 EmptyDataModelWrapper::EmptyDataModelWrapper() {} | |
| 133 EmptyDataModelWrapper::~EmptyDataModelWrapper() {} | |
| 134 | |
| 135 base::string16 EmptyDataModelWrapper::GetInfo(const AutofillType& type) const { | |
| 136 return base::string16(); | |
| 137 } | |
| 138 | |
| 139 // AutofillProfileWrapper | 117 // AutofillProfileWrapper |
| 140 | 118 |
| 141 AutofillProfileWrapper::AutofillProfileWrapper(const AutofillProfile* profile) | 119 AutofillProfileWrapper::AutofillProfileWrapper(const AutofillProfile* profile) |
| 142 : profile_(profile), | 120 : profile_(profile), |
| 143 variant_group_(NO_GROUP), | 121 variant_group_(NO_GROUP), |
| 144 variant_(0) {} | 122 variant_(0) {} |
| 145 | 123 |
| 146 AutofillProfileWrapper::AutofillProfileWrapper( | 124 AutofillProfileWrapper::AutofillProfileWrapper( |
| 147 const AutofillProfile* profile, | 125 const AutofillProfile* profile, |
| 148 const AutofillType& type, | 126 const AutofillType& type, |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 230 return MonthComboboxModel::FormatMonth(card_->expiration_month()); | 208 return MonthComboboxModel::FormatMonth(card_->expiration_month()); |
| 231 | 209 |
| 232 return card_->GetInfo(type, g_browser_process->GetApplicationLocale()); | 210 return card_->GetInfo(type, g_browser_process->GetApplicationLocale()); |
| 233 } | 211 } |
| 234 | 212 |
| 235 gfx::Image AutofillCreditCardWrapper::GetIcon() { | 213 gfx::Image AutofillCreditCardWrapper::GetIcon() { |
| 236 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | 214 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
| 237 return rb.GetImageNamed(CreditCard::IconResourceId(card_->type())); | 215 return rb.GetImageNamed(CreditCard::IconResourceId(card_->type())); |
| 238 } | 216 } |
| 239 | 217 |
| 240 #if !defined(OS_ANDROID) | |
| 241 bool AutofillCreditCardWrapper::GetDisplayText( | 218 bool AutofillCreditCardWrapper::GetDisplayText( |
| 242 base::string16* vertically_compact, | 219 base::string16* vertically_compact, |
| 243 base::string16* horizontally_compact) { | 220 base::string16* horizontally_compact) { |
| 244 if (!card_->IsValid()) | 221 if (!card_->IsValid()) |
| 245 return false; | 222 return false; |
| 246 | 223 |
| 247 *vertically_compact = *horizontally_compact = card_->TypeAndLastFourDigits(); | 224 *vertically_compact = *horizontally_compact = card_->TypeAndLastFourDigits(); |
| 248 return true; | 225 return true; |
| 249 } | 226 } |
| 250 #endif | |
| 251 | 227 |
| 252 // WalletAddressWrapper | 228 // WalletAddressWrapper |
| 253 | 229 |
| 254 WalletAddressWrapper::WalletAddressWrapper( | 230 WalletAddressWrapper::WalletAddressWrapper( |
| 255 const wallet::Address* address) : address_(address) {} | 231 const wallet::Address* address) : address_(address) {} |
| 256 | 232 |
| 257 WalletAddressWrapper::~WalletAddressWrapper() {} | 233 WalletAddressWrapper::~WalletAddressWrapper() {} |
| 258 | 234 |
| 259 base::string16 WalletAddressWrapper::GetInfo(const AutofillType& type) const { | 235 base::string16 WalletAddressWrapper::GetInfo(const AutofillType& type) const { |
| 260 // Reachable from DataModelWrapper::GetDisplayText(). | 236 // Reachable from DataModelWrapper::GetDisplayText(). |
| 261 if (type.GetStorableType() == EMAIL_ADDRESS) | 237 if (type.GetStorableType() == EMAIL_ADDRESS) |
| 262 return base::string16(); | 238 return base::string16(); |
| 263 | 239 |
| 264 return address_->GetInfo(type, g_browser_process->GetApplicationLocale()); | 240 return address_->GetInfo(type, g_browser_process->GetApplicationLocale()); |
| 265 } | 241 } |
| 266 | 242 |
| 267 base::string16 WalletAddressWrapper::GetInfoForDisplay(const AutofillType& type) | 243 base::string16 WalletAddressWrapper::GetInfoForDisplay(const AutofillType& type) |
| 268 const { | 244 const { |
| 269 if (type.GetStorableType() == PHONE_HOME_WHOLE_NUMBER) | 245 if (type.GetStorableType() == PHONE_HOME_WHOLE_NUMBER) |
| 270 return address_->DisplayPhoneNumber(); | 246 return address_->DisplayPhoneNumber(); |
| 271 | 247 |
| 272 return DataModelWrapper::GetInfoForDisplay(type); | 248 return DataModelWrapper::GetInfoForDisplay(type); |
| 273 } | 249 } |
| 274 | 250 |
| 275 #if !defined(OS_ANDROID) | |
| 276 bool WalletAddressWrapper::GetDisplayText( | 251 bool WalletAddressWrapper::GetDisplayText( |
| 277 base::string16* vertically_compact, | 252 base::string16* vertically_compact, |
| 278 base::string16* horizontally_compact) { | 253 base::string16* horizontally_compact) { |
| 279 if (!address_->is_complete_address() || | 254 if (!address_->is_complete_address() || |
| 280 GetInfo(AutofillType(PHONE_HOME_WHOLE_NUMBER)).empty()) { | 255 GetInfo(AutofillType(PHONE_HOME_WHOLE_NUMBER)).empty()) { |
| 281 return false; | 256 return false; |
| 282 } | 257 } |
| 283 | 258 |
| 284 return DataModelWrapper::GetDisplayText(vertically_compact, | 259 return DataModelWrapper::GetDisplayText(vertically_compact, |
| 285 horizontally_compact); | 260 horizontally_compact); |
| 286 } | 261 } |
| 287 #endif | |
| 288 | 262 |
| 289 // WalletInstrumentWrapper | 263 // WalletInstrumentWrapper |
| 290 | 264 |
| 291 WalletInstrumentWrapper::WalletInstrumentWrapper( | 265 WalletInstrumentWrapper::WalletInstrumentWrapper( |
| 292 const wallet::WalletItems::MaskedInstrument* instrument) | 266 const wallet::WalletItems::MaskedInstrument* instrument) |
| 293 : instrument_(instrument) {} | 267 : instrument_(instrument) {} |
| 294 | 268 |
| 295 WalletInstrumentWrapper::~WalletInstrumentWrapper() {} | 269 WalletInstrumentWrapper::~WalletInstrumentWrapper() {} |
| 296 | 270 |
| 297 base::string16 WalletInstrumentWrapper::GetInfo(const AutofillType& type) | 271 base::string16 WalletInstrumentWrapper::GetInfo(const AutofillType& type) |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 311 if (type.GetStorableType() == PHONE_HOME_WHOLE_NUMBER) | 285 if (type.GetStorableType() == PHONE_HOME_WHOLE_NUMBER) |
| 312 return instrument_->address().DisplayPhoneNumber(); | 286 return instrument_->address().DisplayPhoneNumber(); |
| 313 | 287 |
| 314 return DataModelWrapper::GetInfoForDisplay(type); | 288 return DataModelWrapper::GetInfoForDisplay(type); |
| 315 } | 289 } |
| 316 | 290 |
| 317 gfx::Image WalletInstrumentWrapper::GetIcon() { | 291 gfx::Image WalletInstrumentWrapper::GetIcon() { |
| 318 return instrument_->CardIcon(); | 292 return instrument_->CardIcon(); |
| 319 } | 293 } |
| 320 | 294 |
| 321 #if !defined(OS_ANDROID) | |
| 322 bool WalletInstrumentWrapper::GetDisplayText( | 295 bool WalletInstrumentWrapper::GetDisplayText( |
| 323 base::string16* vertically_compact, | 296 base::string16* vertically_compact, |
| 324 base::string16* horizontally_compact) { | 297 base::string16* horizontally_compact) { |
| 325 // TODO(dbeam): handle other instrument statuses? http://crbug.com/233048 | 298 // TODO(dbeam): handle other instrument statuses? http://crbug.com/233048 |
| 326 if (instrument_->status() == wallet::WalletItems::MaskedInstrument::EXPIRED || | 299 if (instrument_->status() == wallet::WalletItems::MaskedInstrument::EXPIRED || |
| 327 !instrument_->address().is_complete_address() || | 300 !instrument_->address().is_complete_address() || |
| 328 GetInfo(AutofillType(PHONE_HOME_WHOLE_NUMBER)).empty()) { | 301 GetInfo(AutofillType(PHONE_HOME_WHOLE_NUMBER)).empty()) { |
| 329 return false; | 302 return false; |
| 330 } | 303 } |
| 331 | 304 |
| 332 DataModelWrapper::GetDisplayText(vertically_compact, horizontally_compact); | 305 DataModelWrapper::GetDisplayText(vertically_compact, horizontally_compact); |
| 333 // TODO(estade): descriptive_name() is user-provided. Should we use it or | 306 // TODO(estade): descriptive_name() is user-provided. Should we use it or |
| 334 // just type + last 4 digits? | 307 // just type + last 4 digits? |
| 335 base::string16 line1 = instrument_->descriptive_name() + ASCIIToUTF16("\n"); | 308 base::string16 line1 = instrument_->descriptive_name() + ASCIIToUTF16("\n"); |
| 336 *vertically_compact = line1 + *vertically_compact; | 309 *vertically_compact = line1 + *vertically_compact; |
| 337 *horizontally_compact = line1 + *horizontally_compact; | 310 *horizontally_compact = line1 + *horizontally_compact; |
| 338 return true; | 311 return true; |
| 339 } | 312 } |
| 340 #endif | |
| 341 | 313 |
| 342 // FullWalletBillingWrapper | 314 // FullWalletBillingWrapper |
| 343 | 315 |
| 344 FullWalletBillingWrapper::FullWalletBillingWrapper( | 316 FullWalletBillingWrapper::FullWalletBillingWrapper( |
| 345 wallet::FullWallet* full_wallet) | 317 wallet::FullWallet* full_wallet) |
| 346 : full_wallet_(full_wallet) { | 318 : full_wallet_(full_wallet) { |
| 347 DCHECK(full_wallet_); | 319 DCHECK(full_wallet_); |
| 348 } | 320 } |
| 349 | 321 |
| 350 FullWalletBillingWrapper::~FullWalletBillingWrapper() {} | 322 FullWalletBillingWrapper::~FullWalletBillingWrapper() {} |
| 351 | 323 |
| 352 base::string16 FullWalletBillingWrapper::GetInfo(const AutofillType& type) | 324 base::string16 FullWalletBillingWrapper::GetInfo(const AutofillType& type) |
| 353 const { | 325 const { |
| 354 if (type.GetStorableType() == CREDIT_CARD_EXP_MONTH) | 326 return full_wallet_->GetInfo( |
| 355 return MonthComboboxModel::FormatMonth(full_wallet_->expiration_month()); | 327 g_browser_process->GetApplicationLocale(), |
| 356 | 328 AutofillType(AutofillType::GetEquivalentBillingFieldType( |
| 357 if (type.group() == CREDIT_CARD) | 329 type.GetStorableType()))); |
| 358 return full_wallet_->GetInfo(type); | |
| 359 | |
| 360 return full_wallet_->billing_address()->GetInfo( | |
| 361 type, g_browser_process->GetApplicationLocale()); | |
| 362 } | 330 } |
| 363 | 331 |
| 364 #if !defined(OS_ANDROID) | |
| 365 bool FullWalletBillingWrapper::GetDisplayText( | 332 bool FullWalletBillingWrapper::GetDisplayText( |
| 366 base::string16* vertically_compact, | 333 base::string16* vertically_compact, |
| 367 base::string16* horizontally_compact) { | 334 base::string16* horizontally_compact) { |
| 368 // TODO(dbeam): handle other required actions? http://crbug.com/163508 | 335 // TODO(dbeam): handle other required actions? http://crbug.com/163508 |
| 369 if (full_wallet_->HasRequiredAction(wallet::UPDATE_EXPIRATION_DATE)) | 336 if (full_wallet_->HasRequiredAction(wallet::UPDATE_EXPIRATION_DATE)) |
| 370 return false; | 337 return false; |
| 371 | 338 |
| 372 return DataModelWrapper::GetDisplayText(vertically_compact, | 339 return DataModelWrapper::GetDisplayText(vertically_compact, |
| 373 horizontally_compact); | 340 horizontally_compact); |
| 374 } | 341 } |
| 375 #endif | |
| 376 | 342 |
| 377 // FullWalletShippingWrapper | 343 // FullWalletShippingWrapper |
| 378 | 344 |
| 379 FullWalletShippingWrapper::FullWalletShippingWrapper( | 345 FullWalletShippingWrapper::FullWalletShippingWrapper( |
| 380 wallet::FullWallet* full_wallet) | 346 wallet::FullWallet* full_wallet) |
| 381 : full_wallet_(full_wallet) { | 347 : full_wallet_(full_wallet) { |
| 382 DCHECK(full_wallet_); | 348 DCHECK(full_wallet_); |
| 383 } | 349 } |
| 384 | 350 |
| 385 FullWalletShippingWrapper::~FullWalletShippingWrapper() {} | 351 FullWalletShippingWrapper::~FullWalletShippingWrapper() {} |
| 386 | 352 |
| 387 base::string16 FullWalletShippingWrapper::GetInfo( | 353 base::string16 FullWalletShippingWrapper::GetInfo( |
| 388 const AutofillType& type) const { | 354 const AutofillType& type) const { |
| 389 return full_wallet_->shipping_address()->GetInfo( | 355 return full_wallet_->shipping_address()->GetInfo( |
| 390 type, g_browser_process->GetApplicationLocale()); | 356 type, g_browser_process->GetApplicationLocale()); |
|
Dan Beam
2014/01/27 23:11:27
why can't this be
full_wallet_->GetInfo(g_brows
Evan Stade
2014/01/28 00:07:59
I happen to like the code better as is. I am not c
Dan Beam
2014/01/28 00:18:47
that's fine
| |
| 391 } | 357 } |
| 392 | 358 |
| 393 FieldMapWrapper::FieldMapWrapper(const FieldValueMap& field_map) | 359 FieldMapWrapper::FieldMapWrapper(const FieldValueMap& field_map) |
| 394 : field_map_(field_map) {} | 360 : field_map_(field_map) {} |
| 395 | 361 |
| 396 FieldMapWrapper::~FieldMapWrapper() {} | 362 FieldMapWrapper::~FieldMapWrapper() {} |
| 397 | 363 |
| 398 base::string16 FieldMapWrapper::GetInfo(const AutofillType& type) const { | 364 base::string16 FieldMapWrapper::GetInfo(const AutofillType& type) const { |
| 399 FieldValueMap::const_iterator it = field_map_.find(type.server_type()); | 365 FieldValueMap::const_iterator it = field_map_.find(type.server_type()); |
| 400 return it != field_map_.end() ? it->second : base::string16(); | 366 return it != field_map_.end() ? it->second : base::string16(); |
| 401 } | 367 } |
| 402 | 368 |
| 403 } // namespace autofill | 369 } // namespace autofill |
| OLD | NEW |