autofill_dialog_common.cc revision f2477e01787aa58f445919b809d89e252beef54f
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 17namespace autofill { 18namespace common { 19 20// Returns true if |input| should be shown when |field_type| has been requested. 21bool 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|. 46bool 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 60bool IsCreditCardType(ServerFieldType type) { 61 return AutofillType(type).group() == CREDIT_CARD; 62} 63 64// Constructs |inputs| from template data. 65void 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|. 75void BuildInputsForSection(DialogSection dialog_section, 76 DetailInputs* inputs) { 77 const DetailInput kCCInputs[] = { 78 { 1, CREDIT_CARD_NUMBER, IDS_AUTOFILL_DIALOG_PLACEHOLDER_CARD_NUMBER }, 79 { 2, CREDIT_CARD_EXP_MONTH, IDS_AUTOFILL_DIALOG_PLACEHOLDER_EXPIRY_MONTH }, 80 { 2, CREDIT_CARD_EXP_4_DIGIT_YEAR, 81 IDS_AUTOFILL_DIALOG_PLACEHOLDER_EXPIRY_YEAR }, 82 { 2, CREDIT_CARD_VERIFICATION_CODE, IDS_AUTOFILL_DIALOG_PLACEHOLDER_CVC, 83 1.5 }, 84 }; 85 86 const DetailInput kBillingInputs[] = { 87 { 3, NAME_BILLING_FULL, IDS_AUTOFILL_DIALOG_PLACEHOLDER_CARDHOLDER_NAME }, 88 { 4, ADDRESS_BILLING_LINE1, 89 IDS_AUTOFILL_DIALOG_PLACEHOLDER_ADDRESS_LINE_1 }, 90 { 5, ADDRESS_BILLING_LINE2, 91 IDS_AUTOFILL_DIALOG_PLACEHOLDER_ADDRESS_LINE_2 }, 92 { 6, ADDRESS_BILLING_CITY, 93 IDS_AUTOFILL_DIALOG_PLACEHOLDER_LOCALITY }, 94 // TODO(estade): state placeholder should depend on locale. 95 { 7, ADDRESS_BILLING_STATE, IDS_AUTOFILL_FIELD_LABEL_STATE }, 96 { 7, ADDRESS_BILLING_ZIP, 97 IDS_AUTOFILL_DIALOG_PLACEHOLDER_POSTAL_CODE }, 98 // We don't allow the user to change the country: http://crbug.com/247518 99 { -1, ADDRESS_BILLING_COUNTRY, 0 }, 100 { 9, PHONE_BILLING_WHOLE_NUMBER, 101 IDS_AUTOFILL_DIALOG_PLACEHOLDER_PHONE_NUMBER }, 102 }; 103 104 const DetailInput kEmailInputs[] = { 105 { 10, EMAIL_ADDRESS, IDS_AUTOFILL_DIALOG_PLACEHOLDER_EMAIL }, 106 }; 107 108 const DetailInput kShippingInputs[] = { 109 { 11, NAME_FULL, IDS_AUTOFILL_DIALOG_PLACEHOLDER_ADDRESSEE_NAME }, 110 { 12, ADDRESS_HOME_LINE1, IDS_AUTOFILL_DIALOG_PLACEHOLDER_ADDRESS_LINE_1 }, 111 { 13, ADDRESS_HOME_LINE2, IDS_AUTOFILL_DIALOG_PLACEHOLDER_ADDRESS_LINE_2 }, 112 { 14, ADDRESS_HOME_CITY, IDS_AUTOFILL_DIALOG_PLACEHOLDER_LOCALITY }, 113 { 15, ADDRESS_HOME_STATE, IDS_AUTOFILL_FIELD_LABEL_STATE }, 114 { 15, ADDRESS_HOME_ZIP, IDS_AUTOFILL_DIALOG_PLACEHOLDER_POSTAL_CODE }, 115 { -1, ADDRESS_HOME_COUNTRY, 0 }, 116 { 17, PHONE_HOME_WHOLE_NUMBER, 117 IDS_AUTOFILL_DIALOG_PLACEHOLDER_PHONE_NUMBER }, 118 }; 119 120 switch (dialog_section) { 121 case SECTION_CC: 122 BuildInputs(kCCInputs, arraysize(kCCInputs), inputs); 123 break; 124 125 case SECTION_BILLING: 126 BuildInputs(kBillingInputs, arraysize(kBillingInputs), inputs); 127 BuildInputs(kEmailInputs, arraysize(kEmailInputs), inputs); 128 break; 129 130 case SECTION_CC_BILLING: 131 BuildInputs(kCCInputs, arraysize(kCCInputs), inputs); 132 BuildInputs(kBillingInputs, arraysize(kBillingInputs), inputs); 133 break; 134 135 case SECTION_SHIPPING: 136 BuildInputs(kShippingInputs, arraysize(kShippingInputs), inputs); 137 break; 138 } 139} 140 141AutofillMetrics::DialogUiEvent DialogSectionToUiItemAddedEvent( 142 DialogSection section) { 143 switch (section) { 144 case SECTION_BILLING: 145 return AutofillMetrics::DIALOG_UI_BILLING_ITEM_ADDED; 146 147 case SECTION_CC_BILLING: 148 return AutofillMetrics::DIALOG_UI_CC_BILLING_ITEM_ADDED; 149 150 case SECTION_SHIPPING: 151 return AutofillMetrics::DIALOG_UI_SHIPPING_ITEM_ADDED; 152 153 case SECTION_CC: 154 return AutofillMetrics::DIALOG_UI_CC_ITEM_ADDED; 155 } 156 157 NOTREACHED(); 158 return AutofillMetrics::NUM_DIALOG_UI_EVENTS; 159} 160 161AutofillMetrics::DialogUiEvent DialogSectionToUiSelectionChangedEvent( 162 DialogSection section) { 163 switch (section) { 164 case SECTION_BILLING: 165 return AutofillMetrics::DIALOG_UI_BILLING_SELECTED_SUGGESTION_CHANGED; 166 167 case SECTION_CC_BILLING: 168 return AutofillMetrics::DIALOG_UI_CC_BILLING_SELECTED_SUGGESTION_CHANGED; 169 170 case SECTION_SHIPPING: 171 return AutofillMetrics::DIALOG_UI_SHIPPING_SELECTED_SUGGESTION_CHANGED; 172 173 case SECTION_CC: 174 return AutofillMetrics::DIALOG_UI_CC_SELECTED_SUGGESTION_CHANGED; 175 } 176 177 NOTREACHED(); 178 return AutofillMetrics::NUM_DIALOG_UI_EVENTS; 179} 180 181string16 GetHardcodedValueForType(ServerFieldType type) { 182 if (AutofillType(type).GetStorableType() == ADDRESS_HOME_COUNTRY) { 183 AutofillCountry country("US", g_browser_process->GetApplicationLocale()); 184 return country.name(); 185 } 186 187 return string16(); 188} 189 190} // namespace common 191} // namespace autofill 192