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 // A stub implementation of internationalized address input fields for |
| 6 // interactive autofill dialog. The implementation always returns the same |
| 7 // address fields until libaddressinput library is integrated. |
| 8 // |
| 9 // After libaddressinput library is integrated, these enums will removed below: |
| 10 // i18n::addressinput::AddressField |
| 11 // i18n::addressinput::LengthHint |
| 12 // Also, GetI18nFields() will call libaddressinput library to determine the |
| 13 // address fields instead of always returning US address fields. |
| 14 // |
| 15 // GuessCountry() uses only the application locale and should be improved to use |
| 16 // the timezone and possibly geolocation. |
| 17 |
| 18 #include "chrome/browser/ui/autofill/autofill_dialog_i18n_input.h" |
| 19 |
| 20 #include "base/command_line.h" |
| 21 #include "chrome/browser/browser_process.h" |
| 22 #include "chrome/common/chrome_switches.h" |
| 23 #include "components/autofill/core/browser/autofill_country.h" |
| 24 #include "grit/component_strings.h" |
| 25 #include "grit/generated_resources.h" |
| 26 |
| 27 namespace autofill { |
| 28 namespace i18ninput { |
| 29 |
| 30 namespace { |
| 31 |
| 32 // TODO(rouslan): Use the enums from libaddressinput instead after the library |
| 33 // has been integrated. |
| 34 namespace i18n { |
| 35 namespace addressinput { |
| 36 |
| 37 enum AddressField { |
| 38 COUNTRY, |
| 39 ADMIN_AREA, |
| 40 LOCALITY, |
| 41 DEPENDENT_LOCALITY, |
| 42 POSTAL_CODE, |
| 43 SORTING_CODE, |
| 44 STREET_ADDRESS, |
| 45 ORGANIZATION, |
| 46 RECIPIENT, |
| 47 }; |
| 48 |
| 49 enum LengthHint { |
| 50 LENGTH_HINT_SHORT, |
| 51 LENGTH_HINT_LONG, |
| 52 }; |
| 53 |
| 54 } // namespace addressinput |
| 55 } // namespace i18n |
| 56 |
| 57 // Indexes into arrays of address field data. The values in |
| 58 // i18n::addressinput::AddressField enum cannot be used directly because they |
| 59 // are not sequential. |
| 60 enum AddressFieldIndex { |
| 61 COUNTRY_INDEX, |
| 62 ADMIN_AREA_INDEX, |
| 63 LOCALITY_INDEX, |
| 64 POSTAL_CODE_INDEX, |
| 65 STREET_ADDRESS_1_INDEX, |
| 66 STREET_ADDRESS_2_INDEX, |
| 67 RECIPIENT_INDEX, |
| 68 }; |
| 69 |
| 70 // Indexes into arrays of street address line data. |
| 71 enum StreetAddressLine { |
| 72 STREET_ADDRESS_LINE_1, |
| 73 STREET_ADDRESS_LINE_2, |
| 74 }; |
| 75 |
| 76 // The number of address types. |
| 77 const int kNumberOfAddressTypes = ADDRESS_TYPE_BILLING + 1; |
| 78 |
| 79 // The maximum number of input fields. |
| 80 const int kMaxNumberOfInputFields = RECIPIENT_INDEX + 1; |
| 81 |
| 82 // The maximum number of street address lines. |
| 83 const int kMaxNumberOfStreetAddressLines = STREET_ADDRESS_LINE_2 + 1; |
| 84 |
| 85 // A mapping of StreetAddressLine to corresponding values in AddressFieldIndex. |
| 86 // Used to lookup address field data for street address lines. |
| 87 const AddressFieldIndex kStreetAddressLineIndex |
| 88 [kMaxNumberOfStreetAddressLines] = { |
| 89 STREET_ADDRESS_1_INDEX, |
| 90 STREET_ADDRESS_2_INDEX, |
| 91 }; |
| 92 |
| 93 // A mapping of AddressFieldIndex and AddressType to autofill field types. |
| 94 const ServerFieldType kServerFields[kMaxNumberOfInputFields] |
| 95 [kNumberOfAddressTypes] = { |
| 96 {ADDRESS_HOME_COUNTRY, ADDRESS_BILLING_COUNTRY}, |
| 97 {ADDRESS_HOME_STATE, ADDRESS_BILLING_STATE}, |
| 98 {ADDRESS_HOME_CITY, ADDRESS_BILLING_CITY}, |
| 99 {ADDRESS_HOME_ZIP, ADDRESS_BILLING_ZIP}, |
| 100 {ADDRESS_HOME_LINE1, ADDRESS_BILLING_LINE1}, |
| 101 {ADDRESS_HOME_LINE2, ADDRESS_BILLING_LINE2}, |
| 102 {NAME_FULL, NAME_BILLING_FULL}, |
| 103 }; |
| 104 |
| 105 // A mapping of AddressFieldIndex and AddressType to string identifiers for |
| 106 // placeholder text. |
| 107 const int kPlaceHolderStringIds[kMaxNumberOfInputFields] |
| 108 [kNumberOfAddressTypes] = { |
| 109 {IDS_AUTOFILL_FIELD_LABEL_COUNTRY, IDS_AUTOFILL_FIELD_LABEL_COUNTRY}, |
| 110 {IDS_AUTOFILL_FIELD_LABEL_STATE, IDS_AUTOFILL_FIELD_LABEL_STATE}, |
| 111 {IDS_AUTOFILL_DIALOG_PLACEHOLDER_LOCALITY, |
| 112 IDS_AUTOFILL_DIALOG_PLACEHOLDER_LOCALITY}, |
| 113 {IDS_AUTOFILL_DIALOG_PLACEHOLDER_POSTAL_CODE, |
| 114 IDS_AUTOFILL_DIALOG_PLACEHOLDER_POSTAL_CODE}, |
| 115 {IDS_AUTOFILL_DIALOG_PLACEHOLDER_ADDRESS_LINE_1, |
| 116 IDS_AUTOFILL_DIALOG_PLACEHOLDER_ADDRESS_LINE_1}, |
| 117 {IDS_AUTOFILL_DIALOG_PLACEHOLDER_ADDRESS_LINE_2, |
| 118 IDS_AUTOFILL_DIALOG_PLACEHOLDER_ADDRESS_LINE_2}, |
| 119 {IDS_AUTOFILL_DIALOG_PLACEHOLDER_ADDRESSEE_NAME, |
| 120 IDS_AUTOFILL_DIALOG_PLACEHOLDER_CARDHOLDER_NAME}, |
| 121 }; |
| 122 |
| 123 // Returns the AddressFieldIndex for the given i18n::addressinput::AddressField. |
| 124 // If the address field is STREET_ADDRESS, then uses the given StreetAddressLine |
| 125 // to determine whether to return STREET_ADDRESS_1_INDEX or |
| 126 // STREET_ADDRESS_2_INDEX. |
| 127 AddressFieldIndex AddressFieldToIndex( |
| 128 i18n::addressinput::AddressField field_type, |
| 129 StreetAddressLine street_address_line) { |
| 130 switch (field_type) { |
| 131 case i18n::addressinput::COUNTRY: |
| 132 return COUNTRY_INDEX; |
| 133 case i18n::addressinput::ADMIN_AREA: |
| 134 return ADMIN_AREA_INDEX; |
| 135 case i18n::addressinput::LOCALITY: |
| 136 return LOCALITY_INDEX; |
| 137 case i18n::addressinput::POSTAL_CODE: |
| 138 return POSTAL_CODE_INDEX; |
| 139 case i18n::addressinput::STREET_ADDRESS: |
| 140 return kStreetAddressLineIndex[street_address_line]; |
| 141 return POSTAL_CODE_INDEX; |
| 142 case i18n::addressinput::RECIPIENT: |
| 143 return RECIPIENT_INDEX; |
| 144 default: |
| 145 NOTREACHED(); |
| 146 return COUNTRY_INDEX; |
| 147 } |
| 148 } |
| 149 |
| 150 // Sets the address field types and length hints for the given |country_region| |
| 151 // and |language|. |
| 152 void GetI18nFields(const std::string country_region, |
| 153 const std::string& language, |
| 154 std::vector<i18n::addressinput::AddressField>* field_types, |
| 155 std::vector<i18n::addressinput::LengthHint>* length_hints) { |
| 156 // TODO(rouslan): Use the country_region and language specific |
| 157 // address input field types and names after libaddressinput is integrated. |
| 158 field_types->push_back(i18n::addressinput::RECIPIENT); |
| 159 length_hints->push_back(i18n::addressinput::LENGTH_HINT_LONG); |
| 160 |
| 161 field_types->push_back(i18n::addressinput::STREET_ADDRESS); |
| 162 length_hints->push_back(i18n::addressinput::LENGTH_HINT_LONG); |
| 163 |
| 164 field_types->push_back(i18n::addressinput::LOCALITY); |
| 165 length_hints->push_back(i18n::addressinput::LENGTH_HINT_LONG); |
| 166 |
| 167 field_types->push_back(i18n::addressinput::ADMIN_AREA); |
| 168 length_hints->push_back(i18n::addressinput::LENGTH_HINT_SHORT); |
| 169 |
| 170 field_types->push_back(i18n::addressinput::POSTAL_CODE); |
| 171 length_hints->push_back(i18n::addressinput::LENGTH_HINT_SHORT); |
| 172 |
| 173 field_types->push_back(i18n::addressinput::COUNTRY); |
| 174 length_hints->push_back(i18n::addressinput::LENGTH_HINT_LONG); |
| 175 |
| 176 } |
| 177 |
| 178 // Returns an incremented |row_index| if |length_hint| and |prev_length_hint| |
| 179 // indicate that the current input should start on a new line. |
| 180 int IncrementRowIndexBasedOnLengthHint( |
| 181 i18n::addressinput::LengthHint prev_length_hint, |
| 182 i18n::addressinput::LengthHint length_hint, |
| 183 int row_index) { |
| 184 if (length_hint == i18n::addressinput::LENGTH_HINT_LONG || |
| 185 prev_length_hint != length_hint) { |
| 186 return row_index + 1; |
| 187 } |
| 188 return row_index; |
| 189 } |
| 190 |
| 191 // Appends the |field_type| input to |inputs|. Appends |
| 192 // kMaxNumberOfStreetAddressLines inputs if |field_type| is STREET_ADDRESS. |
| 193 int BuildI18nInput(int row_index, |
| 194 i18n::addressinput::AddressField field_type, |
| 195 i18n::addressinput::LengthHint prev_length_hint, |
| 196 i18n::addressinput::LengthHint length_hint, |
| 197 AddressType address_type, |
| 198 DetailInputs* inputs) { |
| 199 int field_index = AddressFieldToIndex(field_type, STREET_ADDRESS_LINE_1); |
| 200 row_index = IncrementRowIndexBasedOnLengthHint( |
| 201 prev_length_hint, length_hint, row_index); |
| 202 DetailInput input = {row_index, kServerFields[field_index][address_type], |
| 203 kPlaceHolderStringIds[field_index][address_type]}; |
| 204 inputs->push_back(input); |
| 205 |
| 206 if (field_type == i18n::addressinput::STREET_ADDRESS) { |
| 207 field_index = AddressFieldToIndex(field_type, STREET_ADDRESS_LINE_2); |
| 208 row_index = IncrementRowIndexBasedOnLengthHint( |
| 209 prev_length_hint, length_hint, row_index); |
| 210 DetailInput input = {row_index, kServerFields[field_index][address_type], |
| 211 kPlaceHolderStringIds[field_index][address_type]}; |
| 212 inputs->push_back(input); |
| 213 } |
| 214 |
| 215 return row_index; |
| 216 } |
| 217 |
| 218 // Returns the language of the current application locale. |
| 219 std::string GetLocaleLanguage() { |
| 220 const std::string& locale = g_browser_process->GetApplicationLocale(); |
| 221 return locale.substr(0, locale.find('-')); |
| 222 } |
| 223 |
| 224 } // namespace |
| 225 |
| 226 bool IsI18nAddressInputEnabled() { |
| 227 return CommandLine::ForCurrentProcess()->HasSwitch( |
| 228 ::switches::kEnableAutofillAddressInternationalization); |
| 229 } |
| 230 |
| 231 std::string GuessCountry() { |
| 232 if (!IsI18nAddressInputEnabled()) |
| 233 return "US"; |
| 234 |
| 235 // TODO(rouslan): Improve on this rudimentary implementation of guessing the |
| 236 // current country code. |
| 237 return AutofillCountry::CountryCodeForLocale( |
| 238 g_browser_process->GetApplicationLocale()); |
| 239 } |
| 240 |
| 241 void BuildI18nInputs(AddressType address_type, |
| 242 const std::string& country_region, |
| 243 int row_index, |
| 244 DetailInputs* inputs) { |
| 245 std::vector<i18n::addressinput::AddressField> field_types; |
| 246 std::vector<i18n::addressinput::LengthHint> length_hints; |
| 247 GetI18nFields( |
| 248 country_region, GetLocaleLanguage(), &field_types, &length_hints); |
| 249 i18n::addressinput::LengthHint prev_length_hint = |
| 250 i18n::addressinput::LENGTH_HINT_LONG; |
| 251 for (size_t i = 0; i < field_types.size() && i < length_hints.size(); ++i) { |
| 252 row_index = BuildI18nInput(row_index, |
| 253 field_types[i], |
| 254 prev_length_hint, |
| 255 length_hints[i], |
| 256 address_type, |
| 257 inputs); |
| 258 prev_length_hint = length_hints[i]; |
| 259 } |
| 260 } |
| 261 |
| 262 } // namespace i18ninput |
| 263 } // namespace autofill |
OLD | NEW |