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)
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
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())
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
,
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)
89 return i18ninput::Enabled();
93 void BuildI18nAddressInputs(AddressType address_type
,
94 const std::string
& country_code
,
95 DetailInputs
* inputs
) {
96 #if defined(OS_ANDROID)
99 i18ninput::BuildAddressInputs(address_type
, country_code
, inputs
);
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
[] = {
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
),
125 const DetailInput kBillingInputs
[] = {
128 GetStringUTF16(IDS_AUTOFILL_DIALOG_PLACEHOLDER_CARDHOLDER_NAME
) },
130 ADDRESS_BILLING_LINE1
,
131 GetStringUTF16(IDS_AUTOFILL_DIALOG_PLACEHOLDER_ADDRESS_LINE_1
) },
133 ADDRESS_BILLING_LINE2
,
134 GetStringUTF16(IDS_AUTOFILL_DIALOG_PLACEHOLDER_ADDRESS_LINE_2
) },
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
,
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
[] = {
150 PHONE_BILLING_WHOLE_NUMBER
,
151 GetStringUTF16(IDS_AUTOFILL_DIALOG_PLACEHOLDER_PHONE_NUMBER
) },
154 const DetailInput kEmailInputs
[] = {
157 GetStringUTF16(IDS_AUTOFILL_DIALOG_PLACEHOLDER_EMAIL
) },
160 const DetailInput kShippingInputs
[] = {
163 GetStringUTF16(IDS_AUTOFILL_DIALOG_PLACEHOLDER_ADDRESSEE_NAME
) },
166 GetStringUTF16(IDS_AUTOFILL_DIALOG_PLACEHOLDER_ADDRESS_LINE_1
) },
169 GetStringUTF16(IDS_AUTOFILL_DIALOG_PLACEHOLDER_ADDRESS_LINE_2
) },
172 GetStringUTF16(IDS_AUTOFILL_DIALOG_PLACEHOLDER_LOCALITY
) },
173 { DetailInput::SHORT
,
175 GetStringUTF16(IDS_AUTOFILL_FIELD_LABEL_STATE
) },
176 { DetailInput::SHORT_EOL
,
178 GetStringUTF16(IDS_AUTOFILL_DIALOG_PLACEHOLDER_POSTAL_CODE
) },
179 { DetailInput::NONE
, ADDRESS_HOME_COUNTRY
},
182 const DetailInput kShippingPhoneInputs
[] = {
184 PHONE_HOME_WHOLE_NUMBER
,
185 GetStringUTF16(IDS_AUTOFILL_DIALOG_PLACEHOLDER_PHONE_NUMBER
) },
188 switch (dialog_section
) {
190 BuildInputs(kCCInputs
, arraysize(kCCInputs
), inputs
);
193 case SECTION_BILLING
:
194 if (IsI18nInputEnabled())
195 BuildI18nAddressInputs(ADDRESS_TYPE_BILLING
, country_code
, inputs
);
197 BuildInputs(kBillingInputs
, arraysize(kBillingInputs
), inputs
);
199 BuildInputs(kBillingPhoneInputs
, arraysize(kBillingPhoneInputs
), inputs
);
200 BuildInputs(kEmailInputs
, arraysize(kEmailInputs
), inputs
);
203 case SECTION_CC_BILLING
:
204 BuildInputs(kCCInputs
, arraysize(kCCInputs
), inputs
);
206 if (IsI18nInputEnabled())
207 BuildI18nAddressInputs(ADDRESS_TYPE_BILLING
, country_code
, inputs
);
209 BuildInputs(kBillingInputs
, arraysize(kBillingInputs
), inputs
);
211 BuildInputs(kBillingPhoneInputs
, arraysize(kBillingPhoneInputs
), inputs
);
214 case SECTION_SHIPPING
:
215 if (IsI18nInputEnabled())
216 BuildI18nAddressInputs(ADDRESS_TYPE_SHIPPING
, country_code
, inputs
);
218 BuildInputs(kShippingInputs
, arraysize(kShippingInputs
), inputs
);
221 kShippingPhoneInputs
, arraysize(kShippingPhoneInputs
), inputs
);
226 AutofillMetrics::DialogUiEvent
DialogSectionToUiItemAddedEvent(
227 DialogSection 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
;
239 return AutofillMetrics::DIALOG_UI_CC_ITEM_ADDED
;
243 return AutofillMetrics::NUM_DIALOG_UI_EVENTS
;
246 AutofillMetrics::DialogUiEvent
DialogSectionToUiSelectionChangedEvent(
247 DialogSection 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
;
259 return AutofillMetrics::DIALOG_UI_CC_SELECTED_SUGGESTION_CHANGED
;
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