Chromium Code Reviews| Index: chrome/browser/ui/autofill/autofill_dialog_i18n_input.cc |
| diff --git a/chrome/browser/ui/autofill/autofill_dialog_i18n_input.cc b/chrome/browser/ui/autofill/autofill_dialog_i18n_input.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..f4a78ab8a997fbc85e24629258653f6fe67ff0b0 |
| --- /dev/null |
| +++ b/chrome/browser/ui/autofill/autofill_dialog_i18n_input.cc |
| @@ -0,0 +1,163 @@ |
| +// Copyright 2013 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/ui/autofill/autofill_dialog_i18n_input.h" |
| + |
| +#include <algorithm> |
| + |
| +#include "base/command_line.h" |
| +#include "base/strings/utf_string_conversions.h" |
| +#include "chrome/browser/browser_process.h" |
| +#include "chrome/common/chrome_switches.h" |
| +#include "components/autofill/core/browser/autofill_country.h" |
| +#include "components/autofill/core/browser/autofill_type.h" |
| +#include "components/autofill/core/browser/field_types.h" |
| +#include "grit/component_strings.h" |
| +#include "grit/generated_resources.h" |
| +#include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_field.h" |
| +#include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_ui.h" |
| +#include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_ui_component.h" |
| +#include "third_party/libaddressinput/src/cpp/include/libaddressinput/localization.h" |
| +#include "ui/base/l10n/l10n_util.h" |
| + |
| +namespace autofill { |
| +namespace i18ninput { |
| + |
| +namespace { |
| + |
| +using i18n::addressinput::AddressField; |
| +using i18n::addressinput::AddressUiComponent; |
| + |
| +DetailInput InputForComponent(const AddressUiComponent& component, |
| + int row_id, |
| + bool billing, |
| + bool address_line_2) { |
| + ServerFieldType type = UNKNOWN_TYPE; |
| + |
| + switch (component.field) { |
|
Evan Stade
2013/12/02 17:57:33
imo, make this switch its own function and remove
Dan Beam
2013/12/02 22:37:54
Done.
|
| + case i18n::addressinput::COUNTRY: |
| + type = billing ? ADDRESS_BILLING_COUNTRY : ADDRESS_HOME_COUNTRY; |
| + break; |
| + case i18n::addressinput::ADMIN_AREA: |
| + type = billing ? ADDRESS_BILLING_STATE : ADDRESS_HOME_STATE; |
| + break; |
| + case i18n::addressinput::LOCALITY: |
| + type = billing ? ADDRESS_BILLING_CITY : ADDRESS_HOME_CITY; |
| + break; |
| + case i18n::addressinput::DEPENDENT_LOCALITY: |
| + type = billing ? ADDRESS_BILLING_DEPENDENT_LOCALITY : |
| + ADDRESS_HOME_DEPENDENT_LOCALITY; |
| + break; |
| + case i18n::addressinput::POSTAL_CODE: |
| + type = billing ? ADDRESS_BILLING_ZIP : ADDRESS_HOME_ZIP; |
| + break; |
| + case i18n::addressinput::SORTING_CODE: |
| + type = billing ? ADDRESS_BILLING_SORTING_CODE : ADDRESS_HOME_SORTING_CODE; |
| + break; |
| + case i18n::addressinput::STREET_ADDRESS: |
| + type = address_line_2 ? |
| + billing ? ADDRESS_BILLING_LINE2 : ADDRESS_HOME_LINE2 : |
| + billing ? ADDRESS_BILLING_LINE1 : ADDRESS_HOME_LINE1; |
| + break; |
| + case i18n::addressinput::RECIPIENT: |
| + type = billing ? NAME_BILLING_FULL : NAME_FULL; |
| + break; |
| + case i18n::addressinput::ORGANIZATION: |
| + type = COMPANY_NAME; |
| + break; |
| + default: |
| + NOTREACHED(); |
| + } |
| + |
| + DetailInput input = { row_id, type, UTF8ToUTF16(component.name) }; |
| + return input; |
| +} |
| + |
| +void IncrementRowIdForComponent( |
|
Evan Stade
2013/12/02 17:57:33
2/3 of the callsites will always increment the row
Dan Beam
2013/12/02 22:37:54
^ yes, they do
Dan Beam
2013/12/02 23:10:32
whoops, published without changing this -- sorry,
|
| + AddressUiComponent::LengthHint length_hint, |
| + AddressUiComponent::LengthHint prev_length_hint, |
| + int* row_id) { |
| + if (length_hint == AddressUiComponent::HINT_SHORT && |
| + prev_length_hint == AddressUiComponent::HINT_SHORT) { |
| + return; |
| + } |
| + |
| + *row_id += 1; |
| +} |
| + |
| +} // namespace |
| + |
|
Evan Stade
2013/12/02 17:57:33
// static
Dan Beam
2013/12/02 22:37:54
it's just in a namespace, no?
Evan Stade
2013/12/02 23:30:34
oh, ok.
|
| +bool IsI18nAddressInputEnabled() { |
| + return CommandLine::ForCurrentProcess()->HasSwitch( |
| + ::switches::kEnableAutofillAddressInternationalization); |
| +} |
| + |
| +std::string GuessCountry() { |
| + if (!IsI18nAddressInputEnabled()) |
| + return "US"; |
| + |
| + // TODO(rouslan): Improve on this rudimentary implementation of guessing the |
| + // current country code. |
| + std::string ui_locale = g_browser_process->GetApplicationLocale(); |
| + return AutofillCountry::CountryCodeForLocale(ui_locale); |
| +} |
| + |
| +void BuildI18nInputs(AddressType address_type, |
| + const std::string& country_region, |
| + int row_id, |
|
Evan Stade
2013/12/02 17:57:33
you don't need row_id to be a parameter. It's ok t
Dan Beam
2013/12/02 22:37:54
Done.
|
| + DetailInputs* inputs) { |
| + const bool is_billing = address_type == ADDRESS_TYPE_BILLING; |
| + |
| + i18n::addressinput::Localization localization; |
| + // TODO(dbeam): figure out how to include libaddressinput's translations into |
| + // some .pak file so I can call |SetGetter(&l10n_util::GetStringUTF8)| here. |
| + std::vector<AddressUiComponent> components( |
| + i18n::addressinput::BuildComponents(country_region, localization)); |
| + |
| + AddressUiComponent::LengthHint prev_length_hint = |
| + AddressUiComponent::HINT_LONG; |
| + for (size_t i = 0; i < components.size(); ++i) { |
| + const AddressUiComponent& component = components[i]; |
| + if (component.field == i18n::addressinput::ORGANIZATION) { |
| + // TODO(dbeam): figure out when we actually need this. |
| + continue; |
| + } |
| + |
| + const AddressUiComponent::LengthHint length_hint = component.length_hint; |
| + |
| + IncrementRowIdForComponent(length_hint, prev_length_hint, &row_id); |
| + inputs->push_back(InputForComponent(component, row_id, is_billing, false)); |
| + |
| + if (component.field == i18n::addressinput::STREET_ADDRESS) { |
|
Evan Stade
2013/12/02 17:57:33
ugh.. This isn't so great because some countries w
Dan Beam
2013/12/02 22:37:54
Done.
|
| + IncrementRowIdForComponent(length_hint, prev_length_hint, &row_id); |
|
Evan Stade
2013/12/02 17:57:33
why bother calling this? just row_id++
Dan Beam
2013/12/02 22:37:54
Done.
|
| + inputs->push_back(InputForComponent(component, row_id, is_billing, true)); |
| + } |
| + |
| + prev_length_hint = component.length_hint; |
| + } |
| + |
| + bool has_country = false; |
| + for (size_t i = 0; i < inputs->size(); ++i) { |
| + AutofillType type((*inputs)[i].type); |
| + if (type.GetStorableType() == ADDRESS_HOME_COUNTRY) { |
| + has_country = true; |
| + break; |
| + } |
| + } |
| + |
| + if (!has_country) { |
|
Evan Stade
2013/12/02 17:57:33
when is there no country?
Dan Beam
2013/12/02 22:37:54
i guess always, removed the country check
|
| + // Make sure there's always a country input so the user can switch. |
| + IncrementRowIdForComponent( |
|
Evan Stade
2013/12/02 17:57:33
why bother calling this? just row_id++
Dan Beam
2013/12/02 22:37:54
Done.
|
| + AddressUiComponent::HINT_LONG, prev_length_hint, &row_id); |
| + ServerFieldType server_type = |
| + is_billing ? ADDRESS_BILLING_COUNTRY : ADDRESS_HOME_COUNTRY; |
| + base::string16 placeholder_text = |
| + l10n_util::GetStringUTF16(IDS_AUTOFILL_FIELD_LABEL_COUNTRY); |
| + DetailInput input = { row_id, server_type, placeholder_text }; |
| + inputs->push_back(input); |
| + } |
| +} |
| + |
| +} // namespace i18ninput |
| +} // namespace autofill |