| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/ui/autofill/autofill_dialog_i18n_input.h" |
| 6 |
| 7 #include <algorithm> |
| 8 |
| 9 #include "base/command_line.h" |
| 10 #include "base/strings/utf_string_conversions.h" |
| 11 #include "chrome/browser/browser_process.h" |
| 12 #include "chrome/common/chrome_switches.h" |
| 13 #include "components/autofill/core/browser/autofill_country.h" |
| 14 #include "components/autofill/core/browser/autofill_type.h" |
| 15 #include "components/autofill/core/browser/field_types.h" |
| 16 #include "grit/component_strings.h" |
| 17 #include "grit/generated_resources.h" |
| 18 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_fi
eld.h" |
| 19 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_ui
.h" |
| 20 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_ui
_component.h" |
| 21 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/localizati
on.h" |
| 22 #include "ui/base/l10n/l10n_util.h" |
| 23 |
| 24 namespace autofill { |
| 25 namespace i18ninput { |
| 26 |
| 27 namespace { |
| 28 |
| 29 using i18n::addressinput::AddressField; |
| 30 using i18n::addressinput::AddressUiComponent; |
| 31 |
| 32 ServerFieldType GetServerType(AddressField address_field, bool billing) { |
| 33 switch (address_field) { |
| 34 case i18n::addressinput::COUNTRY: |
| 35 return billing ? ADDRESS_BILLING_COUNTRY : ADDRESS_HOME_COUNTRY; |
| 36 case i18n::addressinput::ADMIN_AREA: |
| 37 return billing ? ADDRESS_BILLING_STATE : ADDRESS_HOME_STATE; |
| 38 case i18n::addressinput::LOCALITY: |
| 39 return billing ? ADDRESS_BILLING_CITY : ADDRESS_HOME_CITY; |
| 40 case i18n::addressinput::DEPENDENT_LOCALITY: |
| 41 return billing ? ADDRESS_BILLING_DEPENDENT_LOCALITY : |
| 42 ADDRESS_HOME_DEPENDENT_LOCALITY; |
| 43 case i18n::addressinput::POSTAL_CODE: |
| 44 return billing ? ADDRESS_BILLING_ZIP : ADDRESS_HOME_ZIP; |
| 45 case i18n::addressinput::SORTING_CODE: |
| 46 return billing ? ADDRESS_BILLING_SORTING_CODE : ADDRESS_HOME_SORTING_CODE; |
| 47 case i18n::addressinput::STREET_ADDRESS: |
| 48 return billing ? ADDRESS_BILLING_LINE1 : ADDRESS_HOME_LINE1; |
| 49 case i18n::addressinput::RECIPIENT: |
| 50 return billing ? NAME_BILLING_FULL : NAME_FULL; |
| 51 case i18n::addressinput::ORGANIZATION: |
| 52 return COMPANY_NAME; |
| 53 } |
| 54 NOTREACHED(); |
| 55 return UNKNOWN_TYPE; |
| 56 } |
| 57 |
| 58 DetailInput::Length LengthFromHint(AddressUiComponent::LengthHint hint) { |
| 59 if (hint == AddressUiComponent::HINT_SHORT) |
| 60 return DetailInput::SHORT; |
| 61 DCHECK_EQ(hint, AddressUiComponent::HINT_LONG); |
| 62 return DetailInput::LONG; |
| 63 } |
| 64 |
| 65 } // namespace |
| 66 |
| 67 bool Enabled() { |
| 68 return CommandLine::ForCurrentProcess()->HasSwitch( |
| 69 ::switches::kEnableAutofillAddressInternationalization); |
| 70 } |
| 71 |
| 72 void BuildAddressInputs(common::AddressType address_type, |
| 73 const std::string& country_code, |
| 74 DetailInputs* inputs) { |
| 75 i18n::addressinput::Localization localization; |
| 76 // TODO(dbeam): figure out how to include libaddressinput's translations into |
| 77 // some .pak file so I can call |SetGetter(&l10n_util::GetStringUTF8)| here. |
| 78 std::vector<AddressUiComponent> components( |
| 79 i18n::addressinput::BuildComponents(country_code, localization)); |
| 80 |
| 81 const bool billing = address_type == common::ADDRESS_TYPE_BILLING; |
| 82 |
| 83 for (size_t i = 0; i < components.size(); ++i) { |
| 84 const AddressUiComponent& component = components[i]; |
| 85 if (component.field == i18n::addressinput::ORGANIZATION) { |
| 86 // TODO(dbeam): figure out when we actually need this. |
| 87 continue; |
| 88 } |
| 89 |
| 90 ServerFieldType server_type = GetServerType(component.field, billing); |
| 91 DetailInput::Length length = LengthFromHint(component.length_hint); |
| 92 DetailInput input = { length, server_type, UTF8ToUTF16(component.name) }; |
| 93 inputs->push_back(input); |
| 94 |
| 95 if (component.field == i18n::addressinput::STREET_ADDRESS) { |
| 96 // TODO(dbeam): support more than 2 address lines. http://crbug.com/324889 |
| 97 ServerFieldType server_type = |
| 98 billing ? ADDRESS_BILLING_LINE2 : ADDRESS_HOME_LINE2; |
| 99 DetailInput input = { length, server_type, UTF8ToUTF16(component.name) }; |
| 100 inputs->push_back(input); |
| 101 } |
| 102 } |
| 103 |
| 104 // Also add a country input so the user can switch countries. |
| 105 ServerFieldType server_type = |
| 106 billing ? ADDRESS_BILLING_COUNTRY : ADDRESS_HOME_COUNTRY; |
| 107 base::string16 placeholder_text = |
| 108 l10n_util::GetStringUTF16(IDS_AUTOFILL_FIELD_LABEL_COUNTRY); |
| 109 DetailInput input = { DetailInput::LONG, server_type, placeholder_text }; |
| 110 inputs->push_back(input); |
| 111 } |
| 112 |
| 113 } // namespace i18ninput |
| 114 } // namespace autofill |
| OLD | NEW |