autofill_dialog_common.cc revision 58537e28ecd584eab876aee8be7156509866d23a
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 kCCInputs[] = { 76 { 1, CREDIT_CARD_NUMBER, IDS_AUTOFILL_DIALOG_PLACEHOLDER_CARD_NUMBER }, 77 { 2, CREDIT_CARD_EXP_MONTH, IDS_AUTOFILL_DIALOG_PLACEHOLDER_EXPIRY_MONTH }, 78 { 2, CREDIT_CARD_EXP_4_DIGIT_YEAR, 79 IDS_AUTOFILL_DIALOG_PLACEHOLDER_EXPIRY_YEAR }, 80 { 2, CREDIT_CARD_VERIFICATION_CODE, IDS_AUTOFILL_DIALOG_PLACEHOLDER_CVC, 81 1.5 }, 82 }; 83 84 const DetailInput kBillingInputs[] = { 85 { 3, NAME_BILLING_FULL, IDS_AUTOFILL_DIALOG_PLACEHOLDER_CARDHOLDER_NAME }, 86 { 4, ADDRESS_BILLING_LINE1, 87 IDS_AUTOFILL_DIALOG_PLACEHOLDER_ADDRESS_LINE_1 }, 88 { 5, ADDRESS_BILLING_LINE2, 89 IDS_AUTOFILL_DIALOG_PLACEHOLDER_ADDRESS_LINE_2 }, 90 { 6, ADDRESS_BILLING_CITY, 91 IDS_AUTOFILL_DIALOG_PLACEHOLDER_LOCALITY }, 92 // TODO(estade): state placeholder should depend on locale. 93 { 7, ADDRESS_BILLING_STATE, IDS_AUTOFILL_FIELD_LABEL_STATE }, 94 { 7, ADDRESS_BILLING_ZIP, 95 IDS_AUTOFILL_DIALOG_PLACEHOLDER_POSTAL_CODE }, 96 // We don't allow the user to change the country: http://crbug.com/247518 97 { -1, ADDRESS_BILLING_COUNTRY, 0 }, 98 { 9, PHONE_BILLING_WHOLE_NUMBER, 99 IDS_AUTOFILL_DIALOG_PLACEHOLDER_PHONE_NUMBER }, 100 }; 101 102 const DetailInput kEmailInputs[] = { 103 { 10, EMAIL_ADDRESS, IDS_AUTOFILL_DIALOG_PLACEHOLDER_EMAIL }, 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_CC: 120 BuildInputs(kCCInputs, arraysize(kCCInputs), inputs); 121 break; 122 123 case SECTION_BILLING: 124 BuildInputs(kBillingInputs, arraysize(kBillingInputs), inputs); 125 BuildInputs(kEmailInputs, arraysize(kEmailInputs), inputs); 126 break; 127 128 case SECTION_CC_BILLING: 129 BuildInputs(kCCInputs, arraysize(kCCInputs), inputs); 130 BuildInputs(kBillingInputs, arraysize(kBillingInputs), inputs); 131 break; 132 133 case SECTION_SHIPPING: 134 BuildInputs(kShippingInputs, arraysize(kShippingInputs), inputs); 135 break; 136 } 137} 138 139AutofillMetrics::DialogUiEvent DialogSectionToUiItemAddedEvent( 140 DialogSection section) { 141 switch (section) { 142 case SECTION_BILLING: 143 return AutofillMetrics::DIALOG_UI_BILLING_ITEM_ADDED; 144 145 case SECTION_CC_BILLING: 146 return AutofillMetrics::DIALOG_UI_CC_BILLING_ITEM_ADDED; 147 148 case SECTION_SHIPPING: 149 return AutofillMetrics::DIALOG_UI_SHIPPING_ITEM_ADDED; 150 151 case SECTION_CC: 152 return AutofillMetrics::DIALOG_UI_CC_ITEM_ADDED; 153 } 154 155 NOTREACHED(); 156 return AutofillMetrics::NUM_DIALOG_UI_EVENTS; 157} 158 159AutofillMetrics::DialogUiEvent DialogSectionToUiSelectionChangedEvent( 160 DialogSection section) { 161 switch (section) { 162 case SECTION_BILLING: 163 return AutofillMetrics::DIALOG_UI_BILLING_SELECTED_SUGGESTION_CHANGED; 164 165 case SECTION_CC_BILLING: 166 return AutofillMetrics::DIALOG_UI_CC_BILLING_SELECTED_SUGGESTION_CHANGED; 167 168 case SECTION_SHIPPING: 169 return AutofillMetrics::DIALOG_UI_SHIPPING_SELECTED_SUGGESTION_CHANGED; 170 171 case SECTION_CC: 172 return AutofillMetrics::DIALOG_UI_CC_SELECTED_SUGGESTION_CHANGED; 173 } 174 175 NOTREACHED(); 176 return AutofillMetrics::NUM_DIALOG_UI_EVENTS; 177} 178 179} // namespace common 180} // namespace autofill 181