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