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