Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(840)

Side by Side Diff: chrome/browser/ui/autofill/autofill_dialog_i18n_input.cc

Issue 63053003: Ask libaddressinput for address components to use in requestAutocomplete(). (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 } // namespace
59
60 bool IsI18nAddressInputEnabled() {
61 #if defined(OS_ANDROID)
62 return false;
63 #else
64 return CommandLine::ForCurrentProcess()->HasSwitch(
65 ::switches::kEnableAutofillAddressInternationalization);
66 #endif
67 }
68
69 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.
70 if (!IsI18nAddressInputEnabled())
71 return "US";
72
73 // TODO(rouslan): Improve on this rudimentary implementation of guessing the
74 // current country code.
75 std::string ui_locale = g_browser_process->GetApplicationLocale();
76 return AutofillCountry::CountryCodeForLocale(ui_locale);
77 }
78
79 void BuildI18nInputs(AddressType address_type,
80 const std::string& country_region,
81 DetailInputs* inputs) {
82 i18n::addressinput::Localization localization;
83 // TODO(dbeam): figure out how to include libaddressinput's translations into
84 // some .pak file so I can call |SetGetter(&l10n_util::GetStringUTF8)| here.
85 std::vector<AddressUiComponent> components(
86 i18n::addressinput::BuildComponents(country_region, localization));
87
88 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.
89 const bool billing = address_type == ADDRESS_TYPE_BILLING;
90
91 for (size_t i = 0; i < components.size(); ++i) {
92 const AddressUiComponent& component = components[i];
93 if (component.field == i18n::addressinput::ORGANIZATION) {
94 // TODO(dbeam): figure out when we actually need this.
95 continue;
96 }
97
98 if (component.length_hint == AddressUiComponent::HINT_LONG)
99 ++row_id;
100
101 ServerFieldType server_type = GetServerType(component.field, billing);
102 DetailInput input = { row_id, server_type, UTF8ToUTF16(component.name) };
103 inputs->push_back(input);
104
105 if (component.field == i18n::addressinput::STREET_ADDRESS) {
106 if (component.length_hint == AddressUiComponent::HINT_LONG)
107 ++row_id;
Evan Stade 2013/12/02 23:30:34 \n
Dan Beam 2013/12/03 02:11:19 Done.
108 // TODO(dbeam): support more than 2 address lines. http://crbug.com/324889
109 ServerFieldType server_type =
110 billing ? ADDRESS_BILLING_LINE2 : ADDRESS_HOME_LINE2;
111 DetailInput input = { row_id, server_type, UTF8ToUTF16(component.name) };
112 inputs->push_back(input);
113 }
114
115 if (component.length_hint == AddressUiComponent::HINT_LONG)
116 ++row_id;
117 }
118
119 // Also add a country input so the user can switch countries.
120 ServerFieldType server_type =
121 billing ? ADDRESS_BILLING_COUNTRY : ADDRESS_HOME_COUNTRY;
122 base::string16 placeholder_text =
123 l10n_util::GetStringUTF16(IDS_AUTOFILL_FIELD_LABEL_COUNTRY);
124 DetailInput input = { row_id + 1, server_type, placeholder_text };
125 inputs->push_back(input);
126 }
127
128 } // namespace i18ninput
129 } // namespace autofill
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698