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_common.h"
6
7#include "components/autofill/core/browser/autofill_field.h"
8#include "components/autofill/core/browser/autofill_type.h"
9#include "grit/chromium_strings.h"
10#include "grit/component_strings.h"
11#include "grit/generated_resources.h"
12#include "grit/theme_resources.h"
13#include "grit/webkit_resources.h"
14
15namespace autofill {
16namespace common {
17
18// Returns true if |input| should be shown when |field_type| has been requested.
19bool InputTypeMatchesFieldType(const DetailInput& input,
20                               const AutofillType& field_type) {
21  // If any credit card expiration info is asked for, show both month and year
22  // inputs.
23  ServerFieldType server_type = field_type.GetStorableType();
24  if (server_type == CREDIT_CARD_EXP_4_DIGIT_YEAR ||
25      server_type == CREDIT_CARD_EXP_2_DIGIT_YEAR ||
26      server_type == CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR ||
27      server_type == CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR ||
28      server_type == CREDIT_CARD_EXP_MONTH) {
29    return input.type == CREDIT_CARD_EXP_4_DIGIT_YEAR ||
30           input.type == CREDIT_CARD_EXP_MONTH;
31  }
32
33  if (server_type == CREDIT_CARD_TYPE)
34    return input.type == CREDIT_CARD_NUMBER;
35
36  // Check the groups to distinguish billing types from shipping ones.
37  AutofillType input_type = AutofillType(input.type);
38  return input_type.GetStorableType() == server_type &&
39         input_type.group() == field_type.group();
40}
41
42// Returns true if |input| in the given |section| should be used for a
43// site-requested |field|.
44bool DetailInputMatchesField(DialogSection section,
45                             const DetailInput& input,
46                             const AutofillField& field) {
47  AutofillType field_type = field.Type();
48
49  // The credit card name is filled from the billing section's data.
50  if (field_type.GetStorableType() == CREDIT_CARD_NAME &&
51      (section == SECTION_BILLING || section == SECTION_CC_BILLING)) {
52    return input.type == NAME_BILLING_FULL;
53  }
54
55  return InputTypeMatchesFieldType(input, field_type);
56}
57
58bool IsCreditCardType(ServerFieldType type) {
59  return AutofillType(type).group() == CREDIT_CARD;
60}
61
62// Constructs |inputs| from template data.
63void BuildInputs(const DetailInput* input_template,
64                 size_t template_size,
65                 DetailInputs* inputs) {
66  for (size_t i = 0; i < template_size; ++i) {
67    const DetailInput* input = &input_template[i];
68    inputs->push_back(*input);
69  }
70}
71
72// Constructs |inputs| from template data for a given |dialog_section|.
73void BuildInputsForSection(DialogSection dialog_section,
74                           DetailInputs* inputs) {
75  const DetailInput kEmailInputs[] = {
76    { 1, EMAIL_ADDRESS, IDS_AUTOFILL_DIALOG_PLACEHOLDER_EMAIL },
77  };
78
79  const DetailInput kCCInputs[] = {
80    { 2, CREDIT_CARD_NUMBER, IDS_AUTOFILL_DIALOG_PLACEHOLDER_CARD_NUMBER },
81    { 3, CREDIT_CARD_EXP_MONTH, IDS_AUTOFILL_DIALOG_PLACEHOLDER_EXPIRY_MONTH },
82    { 3, CREDIT_CARD_EXP_4_DIGIT_YEAR,
83      IDS_AUTOFILL_DIALOG_PLACEHOLDER_EXPIRY_YEAR },
84    { 3, CREDIT_CARD_VERIFICATION_CODE, IDS_AUTOFILL_DIALOG_PLACEHOLDER_CVC,
85      1.5 },
86  };
87
88  const DetailInput kBillingInputs[] = {
89    { 4, NAME_BILLING_FULL, IDS_AUTOFILL_DIALOG_PLACEHOLDER_CARDHOLDER_NAME },
90    { 5, ADDRESS_BILLING_LINE1,
91      IDS_AUTOFILL_DIALOG_PLACEHOLDER_ADDRESS_LINE_1 },
92    { 6, ADDRESS_BILLING_LINE2,
93      IDS_AUTOFILL_DIALOG_PLACEHOLDER_ADDRESS_LINE_2 },
94    { 7, ADDRESS_BILLING_CITY,
95      IDS_AUTOFILL_DIALOG_PLACEHOLDER_LOCALITY },
96    // TODO(estade): state placeholder should depend on locale.
97    { 8, ADDRESS_BILLING_STATE, IDS_AUTOFILL_FIELD_LABEL_STATE },
98    { 8, ADDRESS_BILLING_ZIP,
99      IDS_AUTOFILL_DIALOG_PLACEHOLDER_POSTAL_CODE },
100    // We don't allow the user to change the country: http://crbug.com/247518
101    { -1, ADDRESS_BILLING_COUNTRY, 0 },
102    { 10, PHONE_BILLING_WHOLE_NUMBER,
103      IDS_AUTOFILL_DIALOG_PLACEHOLDER_PHONE_NUMBER },
104  };
105
106  const DetailInput kShippingInputs[] = {
107    { 11, NAME_FULL, IDS_AUTOFILL_DIALOG_PLACEHOLDER_ADDRESSEE_NAME },
108    { 12, ADDRESS_HOME_LINE1, IDS_AUTOFILL_DIALOG_PLACEHOLDER_ADDRESS_LINE_1 },
109    { 13, ADDRESS_HOME_LINE2, IDS_AUTOFILL_DIALOG_PLACEHOLDER_ADDRESS_LINE_2 },
110    { 14, ADDRESS_HOME_CITY, IDS_AUTOFILL_DIALOG_PLACEHOLDER_LOCALITY },
111    { 15, ADDRESS_HOME_STATE, IDS_AUTOFILL_FIELD_LABEL_STATE },
112    { 15, ADDRESS_HOME_ZIP, IDS_AUTOFILL_DIALOG_PLACEHOLDER_POSTAL_CODE },
113    { -1, ADDRESS_HOME_COUNTRY, 0 },
114    { 17, PHONE_HOME_WHOLE_NUMBER,
115      IDS_AUTOFILL_DIALOG_PLACEHOLDER_PHONE_NUMBER },
116  };
117
118  switch (dialog_section) {
119    case SECTION_EMAIL:
120      BuildInputs(kEmailInputs,
121                  arraysize(kEmailInputs),
122                  inputs);
123      break;
124
125    case SECTION_CC:
126      BuildInputs(kCCInputs,
127                  arraysize(kCCInputs),
128                  inputs);
129      break;
130
131    case SECTION_BILLING:
132      BuildInputs(kBillingInputs,
133                  arraysize(kBillingInputs),
134                  inputs);
135      break;
136
137    case SECTION_CC_BILLING:
138      BuildInputs(kCCInputs,
139                  arraysize(kCCInputs),
140                  inputs);
141      BuildInputs(kBillingInputs,
142                  arraysize(kBillingInputs),
143                  inputs);
144      break;
145
146    case SECTION_SHIPPING:
147      BuildInputs(kShippingInputs,
148                  arraysize(kShippingInputs),
149                  inputs);
150      break;
151  }
152}
153
154AutofillMetrics::DialogUiEvent DialogSectionToUiEditEvent(
155    DialogSection section) {
156  switch (section) {
157    case SECTION_EMAIL:
158      return AutofillMetrics::DIALOG_UI_EMAIL_EDIT_UI_SHOWN;
159
160    case SECTION_BILLING:
161      return AutofillMetrics::DIALOG_UI_BILLING_EDIT_UI_SHOWN;
162
163    case SECTION_CC_BILLING:
164      return AutofillMetrics::DIALOG_UI_CC_BILLING_EDIT_UI_SHOWN;
165
166    case SECTION_SHIPPING:
167      return AutofillMetrics::DIALOG_UI_SHIPPING_EDIT_UI_SHOWN;
168
169    case SECTION_CC:
170      return AutofillMetrics::DIALOG_UI_CC_EDIT_UI_SHOWN;
171  }
172
173  NOTREACHED();
174  return AutofillMetrics::NUM_DIALOG_UI_EVENTS;
175}
176
177AutofillMetrics::DialogUiEvent DialogSectionToUiItemAddedEvent(
178    DialogSection section) {
179  switch (section) {
180    case SECTION_EMAIL:
181      return AutofillMetrics::DIALOG_UI_EMAIL_ITEM_ADDED;
182
183    case SECTION_BILLING:
184      return AutofillMetrics::DIALOG_UI_BILLING_ITEM_ADDED;
185
186    case SECTION_CC_BILLING:
187      return AutofillMetrics::DIALOG_UI_CC_BILLING_ITEM_ADDED;
188
189    case SECTION_SHIPPING:
190      return AutofillMetrics::DIALOG_UI_SHIPPING_ITEM_ADDED;
191
192    case SECTION_CC:
193      return AutofillMetrics::DIALOG_UI_CC_ITEM_ADDED;
194  }
195
196  NOTREACHED();
197  return AutofillMetrics::NUM_DIALOG_UI_EVENTS;
198}
199
200AutofillMetrics::DialogUiEvent DialogSectionToUiSelectionChangedEvent(
201    DialogSection section) {
202  switch (section) {
203    case SECTION_EMAIL:
204      return AutofillMetrics::DIALOG_UI_EMAIL_SELECTED_SUGGESTION_CHANGED;
205
206    case SECTION_BILLING:
207      return AutofillMetrics::DIALOG_UI_BILLING_SELECTED_SUGGESTION_CHANGED;
208
209    case SECTION_CC_BILLING:
210      return AutofillMetrics::DIALOG_UI_CC_BILLING_SELECTED_SUGGESTION_CHANGED;
211
212    case SECTION_SHIPPING:
213      return AutofillMetrics::DIALOG_UI_SHIPPING_SELECTED_SUGGESTION_CHANGED;
214
215    case SECTION_CC:
216      return AutofillMetrics::DIALOG_UI_CC_SELECTED_SUGGESTION_CHANGED;
217  }
218
219  NOTREACHED();
220  return AutofillMetrics::NUM_DIALOG_UI_EVENTS;
221}
222
223}  // namespace common
224}  // namespace autofill
225