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" |
11 #include "base/strings/string_util.h" | 11 #include "base/strings/string_util.h" |
12 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
13 #include "components/autofill/core/browser/autofill_country.h" | 13 #include "components/autofill/core/browser/autofill_country.h" |
14 #include "components/autofill/core/browser/autofill_type.h" | 14 #include "components/autofill/core/browser/autofill_type.h" |
15 #include "components/autofill/core/browser/phone_number.h" | 15 #include "components/autofill/core/browser/phone_number.h" |
16 #include "components/autofill/core/browser/state_names.h" | 16 #include "components/autofill/core/browser/state_names.h" |
17 #include "grit/components_strings.h" | 17 #include "grit/components_strings.h" |
18 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_da
ta.h" | |
19 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_fo
rmatter.h" | |
20 #include "ui/base/l10n/l10n_util.h" | 18 #include "ui/base/l10n/l10n_util.h" |
21 | 19 |
22 using ::i18n::addressinput::AddressData; | |
23 using ::i18n::addressinput::GetStreetAddressLinesAsSingleLine; | |
24 using base::ASCIIToUTF16; | 20 using base::ASCIIToUTF16; |
25 using base::StringToInt; | 21 using base::StringToInt; |
26 | 22 |
27 namespace autofill { | 23 namespace autofill { |
28 namespace { | 24 namespace { |
29 | 25 |
30 const char* const kMonthsAbbreviated[] = { | 26 const char* const kMonthsAbbreviated[] = { |
31 NULL, // Padding so index 1 = month 1 = January. | 27 NULL, // Padding so index 1 = month 1 = January. |
32 "Jan", "Feb", "Mar", "Apr", "May", "Jun", | 28 "Jan", "Feb", "Mar", "Apr", "May", "Jun", |
33 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", | 29 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", |
(...skipping 317 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
351 return false; | 347 return false; |
352 | 348 |
353 // HTML5 input="month" expects zero-padded months. | 349 // HTML5 input="month" expects zero-padded months. |
354 if (month.size() == 1) | 350 if (month.size() == 1) |
355 month = ASCIIToUTF16("0") + month; | 351 month = ASCIIToUTF16("0") + month; |
356 | 352 |
357 field->value = year + ASCIIToUTF16("-") + month; | 353 field->value = year + ASCIIToUTF16("-") + month; |
358 return true; | 354 return true; |
359 } | 355 } |
360 | 356 |
361 // Fills |field| with the street address in |value|. Translates newlines into | 357 // Fills |field| with the street address in |value|. Translates newlines into |
362 // equivalent separators when necessary, i.e. when filling a single-line field. | 358 // equivalent separators when necessary, i.e. when filling a single-line field. |
363 // The separators depend on |address_language_code|. | |
364 void FillStreetAddress(const base::string16& value, | 359 void FillStreetAddress(const base::string16& value, |
365 const std::string& address_language_code, | |
366 FormFieldData* field) { | 360 FormFieldData* field) { |
367 if (field->form_control_type == "textarea") { | 361 if (field->form_control_type == "textarea") { |
368 field->value = value; | 362 field->value = value; |
369 return; | 363 return; |
370 } | 364 } |
371 | 365 |
372 AddressData address_data; | 366 const base::string16& separator = |
373 address_data.language_code = address_language_code; | 367 l10n_util::GetStringUTF16(IDS_AUTOFILL_ADDRESS_LINE_SEPARATOR); |
374 base::SplitString(base::UTF16ToUTF8(value), '\n', &address_data.address_line); | 368 base::ReplaceChars(value, base::ASCIIToUTF16("\n"), separator, &field->value); |
375 std::string line; | |
376 GetStreetAddressLinesAsSingleLine(address_data, &line); | |
377 field->value = base::UTF8ToUTF16(line); | |
378 } | 369 } |
379 | 370 |
380 std::string Hash32Bit(const std::string& str) { | 371 std::string Hash32Bit(const std::string& str) { |
381 std::string hash_bin = base::SHA1HashString(str); | 372 std::string hash_bin = base::SHA1HashString(str); |
382 DCHECK_EQ(20U, hash_bin.length()); | 373 DCHECK_EQ(20U, hash_bin.length()); |
383 | 374 |
384 uint32 hash32 = ((hash_bin[0] & 0xFF) << 24) | | 375 uint32 hash32 = ((hash_bin[0] & 0xFF) << 24) | |
385 ((hash_bin[1] & 0xFF) << 16) | | 376 ((hash_bin[1] & 0xFF) << 16) | |
386 ((hash_bin[2] & 0xFF) << 8) | | 377 ((hash_bin[2] & 0xFF) << 8) | |
387 (hash_bin[3] & 0xFF); | 378 (hash_bin[3] & 0xFF); |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
464 return Hash32Bit(field_string); | 455 return Hash32Bit(field_string); |
465 } | 456 } |
466 | 457 |
467 bool AutofillField::IsFieldFillable() const { | 458 bool AutofillField::IsFieldFillable() const { |
468 return should_autocomplete && !Type().IsUnknown(); | 459 return should_autocomplete && !Type().IsUnknown(); |
469 } | 460 } |
470 | 461 |
471 // static | 462 // static |
472 bool AutofillField::FillFormField(const AutofillField& field, | 463 bool AutofillField::FillFormField(const AutofillField& field, |
473 const base::string16& value, | 464 const base::string16& value, |
474 const std::string& address_language_code, | |
475 const std::string& app_locale, | 465 const std::string& app_locale, |
476 FormFieldData* field_data) { | 466 FormFieldData* field_data) { |
477 AutofillType type = field.Type(); | 467 AutofillType type = field.Type(); |
478 | 468 |
479 if (type.GetStorableType() == PHONE_HOME_NUMBER) { | 469 if (type.GetStorableType() == PHONE_HOME_NUMBER) { |
480 FillPhoneNumberField(field, value, field_data); | 470 FillPhoneNumberField(field, value, field_data); |
481 return true; | 471 return true; |
482 } else if (field_data->form_control_type == "select-one") { | 472 } else if (field_data->form_control_type == "select-one") { |
483 return FillSelectControl(type, value, app_locale, field_data); | 473 return FillSelectControl(type, value, app_locale, field_data); |
484 } else if (field_data->form_control_type == "month") { | 474 } else if (field_data->form_control_type == "month") { |
485 return FillMonthControl(value, field_data); | 475 return FillMonthControl(value, field_data); |
486 } else if (type.GetStorableType() == ADDRESS_HOME_STREET_ADDRESS) { | 476 } else if (type.GetStorableType() == ADDRESS_HOME_STREET_ADDRESS) { |
487 FillStreetAddress(value, address_language_code, field_data); | 477 FillStreetAddress(value, field_data); |
488 return true; | 478 return true; |
489 } | 479 } |
490 | 480 |
491 field_data->value = value; | 481 field_data->value = value; |
492 return true; | 482 return true; |
493 } | 483 } |
494 | 484 |
495 } // namespace autofill | 485 } // namespace autofill |
OLD | NEW |