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..f4a9c93e81ab62620293ae4d41a46ddbf0050181 |
--- /dev/null |
+++ b/chrome/browser/ui/autofill/autofill_dialog_i18n_input.cc |
@@ -0,0 +1,129 @@ |
+// 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; |
+ |
+ServerFieldType GetServerType(AddressField address_field, bool billing) { |
+ switch (address_field) { |
+ case i18n::addressinput::COUNTRY: |
+ return billing ? ADDRESS_BILLING_COUNTRY : ADDRESS_HOME_COUNTRY; |
+ case i18n::addressinput::ADMIN_AREA: |
+ return billing ? ADDRESS_BILLING_STATE : ADDRESS_HOME_STATE; |
+ case i18n::addressinput::LOCALITY: |
+ return billing ? ADDRESS_BILLING_CITY : ADDRESS_HOME_CITY; |
+ case i18n::addressinput::DEPENDENT_LOCALITY: |
+ return billing ? ADDRESS_BILLING_DEPENDENT_LOCALITY : |
+ ADDRESS_HOME_DEPENDENT_LOCALITY; |
+ case i18n::addressinput::POSTAL_CODE: |
+ return billing ? ADDRESS_BILLING_ZIP : ADDRESS_HOME_ZIP; |
+ case i18n::addressinput::SORTING_CODE: |
+ return billing ? ADDRESS_BILLING_SORTING_CODE : ADDRESS_HOME_SORTING_CODE; |
+ case i18n::addressinput::STREET_ADDRESS: |
+ return billing ? ADDRESS_BILLING_LINE1 : ADDRESS_HOME_LINE1; |
+ case i18n::addressinput::RECIPIENT: |
+ return billing ? NAME_BILLING_FULL : NAME_FULL; |
+ case i18n::addressinput::ORGANIZATION: |
+ return COMPANY_NAME; |
+ } |
+ NOTREACHED(); |
+ return UNKNOWN_TYPE; |
+} |
+ |
+} // namespace |
+ |
+bool IsI18nAddressInputEnabled() { |
+#if defined(OS_ANDROID) |
+ return false; |
+#else |
+ return CommandLine::ForCurrentProcess()->HasSwitch( |
+ ::switches::kEnableAutofillAddressInternationalization); |
+#endif |
+} |
+ |
+std::string GuessCountry() { |
Evan Stade
2013/12/02 23:30:34
don't add this function; use PersonalDataManager::
Dan Beam
2013/12/03 02:11:19
Done.
|
+ 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, |
+ DetailInputs* inputs) { |
+ 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)); |
+ |
+ int row_id = 100; |
Evan Stade
2013/12/02 23:30:34
I don't really have an objection to the extra knee
Dan Beam
2013/12/03 02:11:19
Added comments.
Evan Stade
2013/12/03 02:32:48
discussed offline. Best to remove the whole row id
Dan Beam
2013/12/03 07:38:07
Done.
|
+ const bool billing = address_type == ADDRESS_TYPE_BILLING; |
+ |
+ 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; |
+ } |
+ |
+ if (component.length_hint == AddressUiComponent::HINT_LONG) |
+ ++row_id; |
+ |
+ ServerFieldType server_type = GetServerType(component.field, billing); |
+ DetailInput input = { row_id, server_type, UTF8ToUTF16(component.name) }; |
+ inputs->push_back(input); |
+ |
+ if (component.field == i18n::addressinput::STREET_ADDRESS) { |
+ if (component.length_hint == AddressUiComponent::HINT_LONG) |
+ ++row_id; |
Evan Stade
2013/12/02 23:30:34
\n
Dan Beam
2013/12/03 02:11:19
Done.
|
+ // TODO(dbeam): support more than 2 address lines. http://crbug.com/324889 |
+ ServerFieldType server_type = |
+ billing ? ADDRESS_BILLING_LINE2 : ADDRESS_HOME_LINE2; |
+ DetailInput input = { row_id, server_type, UTF8ToUTF16(component.name) }; |
+ inputs->push_back(input); |
+ } |
+ |
+ if (component.length_hint == AddressUiComponent::HINT_LONG) |
+ ++row_id; |
+ } |
+ |
+ // Also add a country input so the user can switch countries. |
+ ServerFieldType server_type = |
+ billing ? ADDRESS_BILLING_COUNTRY : ADDRESS_HOME_COUNTRY; |
+ base::string16 placeholder_text = |
+ l10n_util::GetStringUTF16(IDS_AUTOFILL_FIELD_LABEL_COUNTRY); |
+ DetailInput input = { row_id + 1, server_type, placeholder_text }; |
+ inputs->push_back(input); |
+} |
+ |
+} // namespace i18ninput |
+} // namespace autofill |