Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / ui / autofill / autofill_dialog_common.cc
blob5c3fdac846ce9e8b9d099b5a5dbb5d6eeddffd8d
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.
5 #include "chrome/browser/ui/autofill/autofill_dialog_common.h"
7 #include "chrome/browser/browser_process.h"
8 #include "components/autofill/core/browser/autofill_country.h"
9 #include "components/autofill/core/browser/autofill_field.h"
10 #include "components/autofill/core/browser/autofill_type.h"
11 #include "grit/chromium_strings.h"
12 #include "grit/component_strings.h"
13 #include "grit/generated_resources.h"
14 #include "grit/theme_resources.h"
15 #include "grit/webkit_resources.h"
16 #include "ui/base/l10n/l10n_util.h"
18 #if !defined(OS_ANDROID)
19 #include "chrome/browser/ui/autofill/autofill_dialog_i18n_input.h"
20 #endif // !defined(OS_ANDROID)
22 namespace autofill {
23 namespace common {
25 // Returns true if |input| should be shown when |field_type| has been requested.
26 bool InputTypeMatchesFieldType(const DetailInput& input,
27 const AutofillType& field_type) {
28 // If any credit card expiration info is asked for, show both month and year
29 // inputs.
30 ServerFieldType server_type = field_type.GetStorableType();
31 if (server_type == CREDIT_CARD_EXP_4_DIGIT_YEAR ||
32 server_type == CREDIT_CARD_EXP_2_DIGIT_YEAR ||
33 server_type == CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR ||
34 server_type == CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR ||
35 server_type == CREDIT_CARD_EXP_MONTH) {
36 return input.type == CREDIT_CARD_EXP_4_DIGIT_YEAR ||
37 input.type == CREDIT_CARD_EXP_MONTH;
40 if (server_type == CREDIT_CARD_TYPE)
41 return input.type == CREDIT_CARD_NUMBER;
43 // Check the groups to distinguish billing types from shipping ones.
44 AutofillType input_type = AutofillType(input.type);
45 if (input_type.group() != field_type.group())
46 return false;
48 // Street address (all lines) is matched to the first input address line.
49 if (server_type == ADDRESS_HOME_STREET_ADDRESS)
50 return input_type.GetStorableType() == ADDRESS_HOME_LINE1;
52 return input_type.GetStorableType() == server_type;
55 // Returns true if |input| in the given |section| should be used for a
56 // site-requested |field|.
57 bool DetailInputMatchesField(DialogSection section,
58 const DetailInput& input,
59 const AutofillField& field) {
60 AutofillType field_type = field.Type();
62 // The credit card name is filled from the billing section's data.
63 if (field_type.GetStorableType() == CREDIT_CARD_NAME &&
64 (section == SECTION_BILLING || section == SECTION_CC_BILLING)) {
65 return input.type == NAME_BILLING_FULL;
68 return InputTypeMatchesFieldType(input, field_type);
71 bool IsCreditCardType(ServerFieldType type) {
72 return AutofillType(type).group() == CREDIT_CARD;
75 // Constructs |inputs| from template data.
76 void BuildInputs(const DetailInput* input_template,
77 size_t template_size,
78 DetailInputs* inputs) {
79 for (size_t i = 0; i < template_size; ++i) {
80 const DetailInput* input = &input_template[i];
81 inputs->push_back(*input);
85 bool IsI18nInputEnabled() {
86 #if defined(OS_ANDROID)
87 return false;
88 #else
89 return i18ninput::Enabled();
90 #endif
93 void BuildI18nAddressInputs(AddressType address_type,
94 const std::string& country_code,
95 DetailInputs* inputs) {
96 #if defined(OS_ANDROID)
97 NOTREACHED();
98 #else
99 i18ninput::BuildAddressInputs(address_type, country_code, inputs);
100 #endif
103 // Constructs |inputs| from template data for a given |dialog_section|.
104 void BuildInputsForSection(DialogSection dialog_section,
105 const std::string& country_code,
106 DetailInputs* inputs) {
107 using l10n_util::GetStringUTF16;
109 const DetailInput kCCInputs[] = {
110 { DetailInput::LONG,
111 CREDIT_CARD_NUMBER,
112 GetStringUTF16(IDS_AUTOFILL_DIALOG_PLACEHOLDER_CARD_NUMBER) },
113 { DetailInput::SHORT,
114 CREDIT_CARD_EXP_MONTH,
115 GetStringUTF16(IDS_AUTOFILL_DIALOG_PLACEHOLDER_EXPIRY_MONTH) },
116 { DetailInput::SHORT,
117 CREDIT_CARD_EXP_4_DIGIT_YEAR,
118 GetStringUTF16(IDS_AUTOFILL_DIALOG_PLACEHOLDER_EXPIRY_YEAR) },
119 { DetailInput::SHORT_EOL,
120 CREDIT_CARD_VERIFICATION_CODE,
121 GetStringUTF16(IDS_AUTOFILL_DIALOG_PLACEHOLDER_CVC),
122 1.5 },
125 const DetailInput kBillingInputs[] = {
126 { DetailInput::LONG,
127 NAME_BILLING_FULL,
128 GetStringUTF16(IDS_AUTOFILL_DIALOG_PLACEHOLDER_CARDHOLDER_NAME) },
129 { DetailInput::LONG,
130 ADDRESS_BILLING_LINE1,
131 GetStringUTF16(IDS_AUTOFILL_DIALOG_PLACEHOLDER_ADDRESS_LINE_1) },
132 { DetailInput::LONG,
133 ADDRESS_BILLING_LINE2,
134 GetStringUTF16(IDS_AUTOFILL_DIALOG_PLACEHOLDER_ADDRESS_LINE_2) },
135 { DetailInput::LONG,
136 ADDRESS_BILLING_CITY,
137 GetStringUTF16(IDS_AUTOFILL_DIALOG_PLACEHOLDER_LOCALITY) },
138 { DetailInput::SHORT,
139 ADDRESS_BILLING_STATE,
140 GetStringUTF16(IDS_AUTOFILL_FIELD_LABEL_STATE) },
141 { DetailInput::SHORT_EOL,
142 ADDRESS_BILLING_ZIP,
143 GetStringUTF16(IDS_AUTOFILL_DIALOG_PLACEHOLDER_POSTAL_CODE) },
144 // We don't allow the user to change the country: http://crbug.com/247518
145 { DetailInput::NONE, ADDRESS_BILLING_COUNTRY },
148 const DetailInput kBillingPhoneInputs[] = {
149 { DetailInput::LONG,
150 PHONE_BILLING_WHOLE_NUMBER,
151 GetStringUTF16(IDS_AUTOFILL_DIALOG_PLACEHOLDER_PHONE_NUMBER) },
154 const DetailInput kEmailInputs[] = {
155 { DetailInput::LONG,
156 EMAIL_ADDRESS,
157 GetStringUTF16(IDS_AUTOFILL_DIALOG_PLACEHOLDER_EMAIL) },
160 const DetailInput kShippingInputs[] = {
161 { DetailInput::LONG,
162 NAME_FULL,
163 GetStringUTF16(IDS_AUTOFILL_DIALOG_PLACEHOLDER_ADDRESSEE_NAME) },
164 { DetailInput::LONG,
165 ADDRESS_HOME_LINE1,
166 GetStringUTF16(IDS_AUTOFILL_DIALOG_PLACEHOLDER_ADDRESS_LINE_1) },
167 { DetailInput::LONG,
168 ADDRESS_HOME_LINE2,
169 GetStringUTF16(IDS_AUTOFILL_DIALOG_PLACEHOLDER_ADDRESS_LINE_2) },
170 { DetailInput::LONG,
171 ADDRESS_HOME_CITY,
172 GetStringUTF16(IDS_AUTOFILL_DIALOG_PLACEHOLDER_LOCALITY) },
173 { DetailInput::SHORT,
174 ADDRESS_HOME_STATE,
175 GetStringUTF16(IDS_AUTOFILL_FIELD_LABEL_STATE) },
176 { DetailInput::SHORT_EOL,
177 ADDRESS_HOME_ZIP,
178 GetStringUTF16(IDS_AUTOFILL_DIALOG_PLACEHOLDER_POSTAL_CODE) },
179 { DetailInput::NONE, ADDRESS_HOME_COUNTRY },
182 const DetailInput kShippingPhoneInputs[] = {
183 { DetailInput::LONG,
184 PHONE_HOME_WHOLE_NUMBER,
185 GetStringUTF16(IDS_AUTOFILL_DIALOG_PLACEHOLDER_PHONE_NUMBER) },
188 switch (dialog_section) {
189 case SECTION_CC:
190 BuildInputs(kCCInputs, arraysize(kCCInputs), inputs);
191 break;
193 case SECTION_BILLING:
194 if (IsI18nInputEnabled())
195 BuildI18nAddressInputs(ADDRESS_TYPE_BILLING, country_code, inputs);
196 else
197 BuildInputs(kBillingInputs, arraysize(kBillingInputs), inputs);
199 BuildInputs(kBillingPhoneInputs, arraysize(kBillingPhoneInputs), inputs);
200 BuildInputs(kEmailInputs, arraysize(kEmailInputs), inputs);
201 break;
203 case SECTION_CC_BILLING:
204 BuildInputs(kCCInputs, arraysize(kCCInputs), inputs);
206 if (IsI18nInputEnabled())
207 BuildI18nAddressInputs(ADDRESS_TYPE_BILLING, country_code, inputs);
208 else
209 BuildInputs(kBillingInputs, arraysize(kBillingInputs), inputs);
211 BuildInputs(kBillingPhoneInputs, arraysize(kBillingPhoneInputs), inputs);
212 break;
214 case SECTION_SHIPPING:
215 if (IsI18nInputEnabled())
216 BuildI18nAddressInputs(ADDRESS_TYPE_SHIPPING, country_code, inputs);
217 else
218 BuildInputs(kShippingInputs, arraysize(kShippingInputs), inputs);
220 BuildInputs(
221 kShippingPhoneInputs, arraysize(kShippingPhoneInputs), inputs);
222 break;
226 AutofillMetrics::DialogUiEvent DialogSectionToUiItemAddedEvent(
227 DialogSection section) {
228 switch (section) {
229 case SECTION_BILLING:
230 return AutofillMetrics::DIALOG_UI_BILLING_ITEM_ADDED;
232 case SECTION_CC_BILLING:
233 return AutofillMetrics::DIALOG_UI_CC_BILLING_ITEM_ADDED;
235 case SECTION_SHIPPING:
236 return AutofillMetrics::DIALOG_UI_SHIPPING_ITEM_ADDED;
238 case SECTION_CC:
239 return AutofillMetrics::DIALOG_UI_CC_ITEM_ADDED;
242 NOTREACHED();
243 return AutofillMetrics::NUM_DIALOG_UI_EVENTS;
246 AutofillMetrics::DialogUiEvent DialogSectionToUiSelectionChangedEvent(
247 DialogSection section) {
248 switch (section) {
249 case SECTION_BILLING:
250 return AutofillMetrics::DIALOG_UI_BILLING_SELECTED_SUGGESTION_CHANGED;
252 case SECTION_CC_BILLING:
253 return AutofillMetrics::DIALOG_UI_CC_BILLING_SELECTED_SUGGESTION_CHANGED;
255 case SECTION_SHIPPING:
256 return AutofillMetrics::DIALOG_UI_SHIPPING_SELECTED_SUGGESTION_CHANGED;
258 case SECTION_CC:
259 return AutofillMetrics::DIALOG_UI_CC_SELECTED_SUGGESTION_CHANGED;
262 NOTREACHED();
263 return AutofillMetrics::NUM_DIALOG_UI_EVENTS;
266 base::string16 GetHardcodedValueForType(ServerFieldType type) {
267 // TODO(dbeam): remove this entire function when i18n inputs are the default.
268 if (IsI18nInputEnabled())
269 return base::string16();
271 if (AutofillType(type).GetStorableType() == ADDRESS_HOME_COUNTRY) {
272 AutofillCountry country("US", g_browser_process->GetApplicationLocale());
273 return country.name();
276 return base::string16();
279 } // namespace common
280 } // namespace autofill