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 <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 DetailInput InputForComponent(const AddressUiComponent& component, | |
33 int row_id, | |
34 bool billing, | |
35 bool address_line_2) { | |
36 ServerFieldType type = UNKNOWN_TYPE; | |
37 | |
38 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.
| |
39 case i18n::addressinput::COUNTRY: | |
40 type = billing ? ADDRESS_BILLING_COUNTRY : ADDRESS_HOME_COUNTRY; | |
41 break; | |
42 case i18n::addressinput::ADMIN_AREA: | |
43 type = billing ? ADDRESS_BILLING_STATE : ADDRESS_HOME_STATE; | |
44 break; | |
45 case i18n::addressinput::LOCALITY: | |
46 type = billing ? ADDRESS_BILLING_CITY : ADDRESS_HOME_CITY; | |
47 break; | |
48 case i18n::addressinput::DEPENDENT_LOCALITY: | |
49 type = billing ? ADDRESS_BILLING_DEPENDENT_LOCALITY : | |
50 ADDRESS_HOME_DEPENDENT_LOCALITY; | |
51 break; | |
52 case i18n::addressinput::POSTAL_CODE: | |
53 type = billing ? ADDRESS_BILLING_ZIP : ADDRESS_HOME_ZIP; | |
54 break; | |
55 case i18n::addressinput::SORTING_CODE: | |
56 type = billing ? ADDRESS_BILLING_SORTING_CODE : ADDRESS_HOME_SORTING_CODE; | |
57 break; | |
58 case i18n::addressinput::STREET_ADDRESS: | |
59 type = address_line_2 ? | |
60 billing ? ADDRESS_BILLING_LINE2 : ADDRESS_HOME_LINE2 : | |
61 billing ? ADDRESS_BILLING_LINE1 : ADDRESS_HOME_LINE1; | |
62 break; | |
63 case i18n::addressinput::RECIPIENT: | |
64 type = billing ? NAME_BILLING_FULL : NAME_FULL; | |
65 break; | |
66 case i18n::addressinput::ORGANIZATION: | |
67 type = COMPANY_NAME; | |
68 break; | |
69 default: | |
70 NOTREACHED(); | |
71 } | |
72 | |
73 DetailInput input = { row_id, type, UTF8ToUTF16(component.name) }; | |
74 return input; | |
75 } | |
76 | |
77 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,
| |
78 AddressUiComponent::LengthHint length_hint, | |
79 AddressUiComponent::LengthHint prev_length_hint, | |
80 int* row_id) { | |
81 if (length_hint == AddressUiComponent::HINT_SHORT && | |
82 prev_length_hint == AddressUiComponent::HINT_SHORT) { | |
83 return; | |
84 } | |
85 | |
86 *row_id += 1; | |
87 } | |
88 | |
89 } // namespace | |
90 | |
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.
| |
91 bool IsI18nAddressInputEnabled() { | |
92 return CommandLine::ForCurrentProcess()->HasSwitch( | |
93 ::switches::kEnableAutofillAddressInternationalization); | |
94 } | |
95 | |
96 std::string GuessCountry() { | |
97 if (!IsI18nAddressInputEnabled()) | |
98 return "US"; | |
99 | |
100 // TODO(rouslan): Improve on this rudimentary implementation of guessing the | |
101 // current country code. | |
102 std::string ui_locale = g_browser_process->GetApplicationLocale(); | |
103 return AutofillCountry::CountryCodeForLocale(ui_locale); | |
104 } | |
105 | |
106 void BuildI18nInputs(AddressType address_type, | |
107 const std::string& country_region, | |
108 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.
| |
109 DetailInputs* inputs) { | |
110 const bool is_billing = address_type == ADDRESS_TYPE_BILLING; | |
111 | |
112 i18n::addressinput::Localization localization; | |
113 // TODO(dbeam): figure out how to include libaddressinput's translations into | |
114 // some .pak file so I can call |SetGetter(&l10n_util::GetStringUTF8)| here. | |
115 std::vector<AddressUiComponent> components( | |
116 i18n::addressinput::BuildComponents(country_region, localization)); | |
117 | |
118 AddressUiComponent::LengthHint prev_length_hint = | |
119 AddressUiComponent::HINT_LONG; | |
120 for (size_t i = 0; i < components.size(); ++i) { | |
121 const AddressUiComponent& component = components[i]; | |
122 if (component.field == i18n::addressinput::ORGANIZATION) { | |
123 // TODO(dbeam): figure out when we actually need this. | |
124 continue; | |
125 } | |
126 | |
127 const AddressUiComponent::LengthHint length_hint = component.length_hint; | |
128 | |
129 IncrementRowIdForComponent(length_hint, prev_length_hint, &row_id); | |
130 inputs->push_back(InputForComponent(component, row_id, is_billing, false)); | |
131 | |
132 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.
| |
133 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.
| |
134 inputs->push_back(InputForComponent(component, row_id, is_billing, true)); | |
135 } | |
136 | |
137 prev_length_hint = component.length_hint; | |
138 } | |
139 | |
140 bool has_country = false; | |
141 for (size_t i = 0; i < inputs->size(); ++i) { | |
142 AutofillType type((*inputs)[i].type); | |
143 if (type.GetStorableType() == ADDRESS_HOME_COUNTRY) { | |
144 has_country = true; | |
145 break; | |
146 } | |
147 } | |
148 | |
149 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
| |
150 // Make sure there's always a country input so the user can switch. | |
151 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.
| |
152 AddressUiComponent::HINT_LONG, prev_length_hint, &row_id); | |
153 ServerFieldType server_type = | |
154 is_billing ? ADDRESS_BILLING_COUNTRY : ADDRESS_HOME_COUNTRY; | |
155 base::string16 placeholder_text = | |
156 l10n_util::GetStringUTF16(IDS_AUTOFILL_FIELD_LABEL_COUNTRY); | |
157 DetailInput input = { row_id, server_type, placeholder_text }; | |
158 inputs->push_back(input); | |
159 } | |
160 } | |
161 | |
162 } // namespace i18ninput | |
163 } // namespace autofill | |
OLD | NEW |