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 "base/strings/utf_string_conversions.h"
8 #include "chrome/browser/browser_process.h"
9 #include "components/autofill/core/browser/autofill_country.h"
10 #include "components/autofill/core/browser/autofill_field.h"
11 #include "components/autofill/core/browser/autofill_type.h"
12
13 namespace autofill {
14 namespace common {
15
ServerTypeEncompassesFieldType(ServerFieldType type,const AutofillType & field_type)16 bool ServerTypeEncompassesFieldType(ServerFieldType type,
17 const AutofillType& field_type) {
18 // If any credit card expiration info is asked for, show both month and year
19 // inputs.
20 ServerFieldType server_type = field_type.GetStorableType();
21 if (server_type == CREDIT_CARD_EXP_4_DIGIT_YEAR ||
22 server_type == CREDIT_CARD_EXP_2_DIGIT_YEAR ||
23 server_type == CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR ||
24 server_type == CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR ||
25 server_type == CREDIT_CARD_EXP_MONTH) {
26 return type == CREDIT_CARD_EXP_4_DIGIT_YEAR ||
27 type == CREDIT_CARD_EXP_MONTH;
28 }
29
30 if (server_type == CREDIT_CARD_TYPE)
31 return type == CREDIT_CARD_NUMBER;
32
33 // Check the groups to distinguish billing types from shipping ones.
34 AutofillType autofill_type = AutofillType(type);
35 if (autofill_type.group() != field_type.group())
36 return false;
37
38 // The page may ask for individual address lines; this roughly matches the
39 // street address blob.
40 if (server_type == ADDRESS_HOME_LINE1 ||
41 server_type == ADDRESS_HOME_LINE2 ||
42 server_type == ADDRESS_HOME_LINE3) {
43 return autofill_type.GetStorableType() == ADDRESS_HOME_STREET_ADDRESS;
44 }
45
46 // First, middle and last name are parsed from full name.
47 if (field_type.group() == NAME || field_type.group() == NAME_BILLING)
48 return autofill_type.GetStorableType() == NAME_FULL;
49
50 return autofill_type.GetStorableType() == server_type;
51 }
52
ServerTypeMatchesField(DialogSection section,ServerFieldType type,const AutofillField & field)53 bool ServerTypeMatchesField(DialogSection section,
54 ServerFieldType type,
55 const AutofillField& field) {
56 AutofillType field_type = field.Type();
57
58 // The credit card name is filled from the billing section's data.
59 if (field_type.GetStorableType() == CREDIT_CARD_NAME &&
60 (section == SECTION_BILLING || section == SECTION_CC_BILLING)) {
61 return type == NAME_BILLING_FULL;
62 }
63
64 return ServerTypeEncompassesFieldType(type, field_type);
65 }
66
IsCreditCardType(ServerFieldType type)67 bool IsCreditCardType(ServerFieldType type) {
68 return AutofillType(type).group() == CREDIT_CARD;
69 }
70
BuildInputs(const DetailInput * input_template,size_t template_size,DetailInputs * inputs)71 void BuildInputs(const DetailInput* input_template,
72 size_t template_size,
73 DetailInputs* inputs) {
74 for (size_t i = 0; i < template_size; ++i) {
75 const DetailInput* input = &input_template[i];
76 inputs->push_back(*input);
77 }
78 }
79
DialogSectionToUiItemAddedEvent(DialogSection section)80 AutofillMetrics::DialogUiEvent DialogSectionToUiItemAddedEvent(
81 DialogSection section) {
82 switch (section) {
83 case SECTION_BILLING:
84 return AutofillMetrics::DIALOG_UI_BILLING_ITEM_ADDED;
85
86 case SECTION_CC_BILLING:
87 return AutofillMetrics::DIALOG_UI_CC_BILLING_ITEM_ADDED;
88
89 case SECTION_SHIPPING:
90 return AutofillMetrics::DIALOG_UI_SHIPPING_ITEM_ADDED;
91
92 case SECTION_CC:
93 return AutofillMetrics::DIALOG_UI_CC_ITEM_ADDED;
94 }
95
96 NOTREACHED();
97 return AutofillMetrics::NUM_DIALOG_UI_EVENTS;
98 }
99
DialogSectionToUiSelectionChangedEvent(DialogSection section)100 AutofillMetrics::DialogUiEvent DialogSectionToUiSelectionChangedEvent(
101 DialogSection section) {
102 switch (section) {
103 case SECTION_BILLING:
104 return AutofillMetrics::DIALOG_UI_BILLING_SELECTED_SUGGESTION_CHANGED;
105
106 case SECTION_CC_BILLING:
107 return AutofillMetrics::DIALOG_UI_CC_BILLING_SELECTED_SUGGESTION_CHANGED;
108
109 case SECTION_SHIPPING:
110 return AutofillMetrics::DIALOG_UI_SHIPPING_SELECTED_SUGGESTION_CHANGED;
111
112 case SECTION_CC:
113 return AutofillMetrics::DIALOG_UI_CC_SELECTED_SUGGESTION_CHANGED;
114 }
115
116 NOTREACHED();
117 return AutofillMetrics::NUM_DIALOG_UI_EVENTS;
118 }
119
TypesFromInputs(const DetailInputs & inputs)120 std::vector<ServerFieldType> TypesFromInputs(const DetailInputs& inputs) {
121 std::vector<ServerFieldType> types;
122 for (size_t i = 0; i < inputs.size(); ++i) {
123 types.push_back(inputs[i].type);
124 }
125 return types;
126 }
127
128 } // namespace common
129 } // namespace autofill
130