| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "components/autofill/core/browser/autofill_field.h" | 5 #include "components/autofill/core/browser/autofill_field.h" |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/sha1.h" | 9 #include "base/sha1.h" |
| 10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
| (...skipping 258 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 269 } | 269 } |
| 270 } | 270 } |
| 271 | 271 |
| 272 // For American Express, also try filling as "AmEx". | 272 // For American Express, also try filling as "AmEx". |
| 273 if (value == l10n_util::GetStringUTF16(IDS_AUTOFILL_CC_AMEX)) | 273 if (value == l10n_util::GetStringUTF16(IDS_AUTOFILL_CC_AMEX)) |
| 274 return FillCreditCardTypeSelectControl(ASCIIToUTF16("AmEx"), field); | 274 return FillCreditCardTypeSelectControl(ASCIIToUTF16("AmEx"), field); |
| 275 | 275 |
| 276 return false; | 276 return false; |
| 277 } | 277 } |
| 278 | 278 |
| 279 // Set |field_data|'s value to |number|, or possibly an appropriate substring of | |
| 280 // |number|. The |field| specifies the type of the phone and whether this is a | |
| 281 // phone prefix or suffix. | |
| 282 void FillPhoneNumberField(const AutofillField& field, | |
| 283 const base::string16& number, | |
| 284 FormFieldData* field_data) { | |
| 285 field_data->value = | |
| 286 AutofillField::GetPhoneNumberValue(field, number, *field_data); | |
| 287 } | |
| 288 | |
| 289 // Set |field_data|'s value to |number|, or possibly an appropriate substring | |
| 290 // of |number| for cases where credit card number splits across multiple HTML | |
| 291 // form input fields. | |
| 292 // The |field| specifies the |credit_card_number_offset_| to the substring | |
| 293 // within credit card number. | |
| 294 void FillCreditCardNumberField(const AutofillField& field, | |
| 295 const base::string16& number, | |
| 296 FormFieldData* field_data) { | |
| 297 base::string16 value = number; | |
| 298 | |
| 299 // |field|'s max_length truncates credit card number to fit within. | |
| 300 if (field.credit_card_number_offset() < value.length()) | |
| 301 value = value.substr(field.credit_card_number_offset()); | |
| 302 | |
| 303 field_data->value = value; | |
| 304 } | |
| 305 | |
| 306 // Fills in the select control |field| with |value|. If an exact match is not | 279 // Fills in the select control |field| with |value|. If an exact match is not |
| 307 // found, falls back to alternate filling strategies based on the |type|. | 280 // found, falls back to alternate filling strategies based on the |type|. |
| 308 bool FillSelectControl(const AutofillType& type, | 281 bool FillSelectControl(const AutofillType& type, |
| 309 const base::string16& value, | 282 const base::string16& value, |
| 310 const std::string& app_locale, | 283 const std::string& app_locale, |
| 311 FormFieldData* field) { | 284 FormFieldData* field) { |
| 312 DCHECK_EQ("select-one", field->form_control_type); | 285 DCHECK_EQ("select-one", field->form_control_type); |
| 313 | 286 |
| 314 // Guard against corrupted values passed over IPC. | 287 // Guard against corrupted values passed over IPC. |
| 315 if (field->option_values.size() != field->option_contents.size()) | 288 if (field->option_values.size() != field->option_contents.size()) |
| (...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 480 | 453 |
| 481 // static | 454 // static |
| 482 bool AutofillField::FillFormField(const AutofillField& field, | 455 bool AutofillField::FillFormField(const AutofillField& field, |
| 483 const base::string16& value, | 456 const base::string16& value, |
| 484 const std::string& address_language_code, | 457 const std::string& address_language_code, |
| 485 const std::string& app_locale, | 458 const std::string& app_locale, |
| 486 FormFieldData* field_data) { | 459 FormFieldData* field_data) { |
| 487 AutofillType type = field.Type(); | 460 AutofillType type = field.Type(); |
| 488 | 461 |
| 489 if (type.GetStorableType() == PHONE_HOME_NUMBER) { | 462 if (type.GetStorableType() == PHONE_HOME_NUMBER) { |
| 490 FillPhoneNumberField(field, value, field_data); | 463 field_data->value = AutofillField::GetPhoneNumberValue(field, value); |
| 491 return true; | 464 return true; |
| 492 } else if (field_data->form_control_type == "select-one") { | 465 } else if (field_data->form_control_type == "select-one") { |
| 493 return FillSelectControl(type, value, app_locale, field_data); | 466 return FillSelectControl(type, value, app_locale, field_data); |
| 494 } else if (field_data->form_control_type == "month") { | 467 } else if (field_data->form_control_type == "month") { |
| 495 return FillMonthControl(value, field_data); | 468 return FillMonthControl(value, field_data); |
| 496 } else if (type.GetStorableType() == ADDRESS_HOME_STREET_ADDRESS) { | 469 } else if (type.GetStorableType() == ADDRESS_HOME_STREET_ADDRESS) { |
| 497 FillStreetAddress(value, address_language_code, field_data); | 470 FillStreetAddress(value, address_language_code, field_data); |
| 498 return true; | 471 return true; |
| 499 } else if (type.GetStorableType() == CREDIT_CARD_NUMBER) { | 472 } else if (type.GetStorableType() == CREDIT_CARD_NUMBER) { |
| 500 FillCreditCardNumberField(field, value, field_data); | 473 field_data->value = AutofillField::GetCreditCardNumberValue(field, value); |
| 501 return true; | 474 return true; |
| 502 } | 475 } |
| 503 | 476 |
| 504 field_data->value = value; | 477 field_data->value = value; |
| 505 return true; | 478 return true; |
| 506 } | 479 } |
| 507 | 480 |
| 508 base::string16 AutofillField::GetPhoneNumberValue( | 481 base::string16 AutofillField::GetPhoneNumberValue( |
| 509 const AutofillField& field, | 482 const AutofillField& field, |
| 510 const base::string16& number, | 483 const base::string16& number) { |
| 511 const FormFieldData& field_data) { | |
| 512 // Check to see if the size field matches the "prefix" or "suffix" size. | 484 // Check to see if the size field matches the "prefix" or "suffix" size. |
| 513 // If so, return the appropriate substring. | 485 // If so, return the appropriate substring. |
| 514 if (number.length() != | 486 if (number.length() != |
| 515 PhoneNumber::kPrefixLength + PhoneNumber::kSuffixLength) { | 487 PhoneNumber::kPrefixLength + PhoneNumber::kSuffixLength) { |
| 516 return number; | 488 return number; |
| 517 } | 489 } |
| 518 | 490 |
| 519 if (field.phone_part() == AutofillField::PHONE_PREFIX || | 491 if (field.phone_part() == AutofillField::PHONE_PREFIX || |
| 520 field_data.max_length == PhoneNumber::kPrefixLength) { | 492 field.max_length == PhoneNumber::kPrefixLength) { |
| 521 return | 493 return |
| 522 number.substr(PhoneNumber::kPrefixOffset, PhoneNumber::kPrefixLength); | 494 number.substr(PhoneNumber::kPrefixOffset, PhoneNumber::kPrefixLength); |
| 523 } | 495 } |
| 524 | 496 |
| 525 if (field.phone_part() == AutofillField::PHONE_SUFFIX || | 497 if (field.phone_part() == AutofillField::PHONE_SUFFIX || |
| 526 field_data.max_length == PhoneNumber::kSuffixLength) { | 498 field.max_length == PhoneNumber::kSuffixLength) { |
| 527 return | 499 return |
| 528 number.substr(PhoneNumber::kSuffixOffset, PhoneNumber::kSuffixLength); | 500 number.substr(PhoneNumber::kSuffixOffset, PhoneNumber::kSuffixLength); |
| 529 } | 501 } |
| 530 | 502 |
| 531 return number; | 503 return number; |
| 532 } | 504 } |
| 533 | 505 |
| 506 // The |field| specifies the |credit_card_number_offset()| to the substring |
| 507 // within credit card number. |
| 508 base::string16 AutofillField::GetCreditCardNumberValue( |
| 509 const AutofillField& field, |
| 510 const base::string16& number) { |
| 511 base::string16 value = number; |
| 512 |
| 513 // |field|'s max_length truncates credit card number to fit within. |
| 514 if (field.credit_card_number_offset() < value.length()) |
| 515 value = value.substr(field.credit_card_number_offset()); |
| 516 |
| 517 return value; |
| 518 } |
| 519 |
| 534 } // namespace autofill | 520 } // namespace autofill |
| OLD | NEW |