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