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