OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/ui/autofill/autofill_dialog_controller_impl.h" | 5 #include "chrome/browser/ui/autofill/autofill_dialog_controller_impl.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/base64.h" | 10 #include "base/base64.h" |
(...skipping 292 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
303 if (type != ADDRESS_HOME_LINE2 && | 303 if (type != ADDRESS_HOME_LINE2 && |
304 type != CREDIT_CARD_VERIFICATION_CODE && | 304 type != CREDIT_CARD_VERIFICATION_CODE && |
305 data_model.GetInfo(type, app_locale).empty()) { | 305 data_model.GetInfo(type, app_locale).empty()) { |
306 return false; | 306 return false; |
307 } | 307 } |
308 } | 308 } |
309 | 309 |
310 return true; | 310 return true; |
311 } | 311 } |
312 | 312 |
| 313 bool IsCardHolderNameValidForWallet(const string16& name) { |
| 314 base::string16 whitespace_collapsed_name = CollapseWhitespace(name, true); |
| 315 std::vector<base::string16> split_name; |
| 316 base::SplitString(whitespace_collapsed_name, ' ', &split_name); |
| 317 return split_name.size() >= 2; |
| 318 } |
| 319 |
313 } // namespace | 320 } // namespace |
314 | 321 |
315 AutofillDialogController::~AutofillDialogController() {} | 322 AutofillDialogController::~AutofillDialogController() {} |
316 | 323 |
317 AutofillDialogControllerImpl::~AutofillDialogControllerImpl() { | 324 AutofillDialogControllerImpl::~AutofillDialogControllerImpl() { |
318 if (popup_controller_) | 325 if (popup_controller_) |
319 popup_controller_->Hide(); | 326 popup_controller_->Hide(); |
320 | 327 |
321 GetMetricLogger().LogDialogInitialUserState( | 328 GetMetricLogger().LogDialogInitialUserState( |
322 GetDialogType(), initial_user_state_); | 329 GetDialogType(), initial_user_state_); |
(...skipping 798 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1121 } | 1128 } |
1122 | 1129 |
1123 return gfx::Image(); | 1130 return gfx::Image(); |
1124 } | 1131 } |
1125 | 1132 |
1126 // TODO(estade): Replace all the error messages here with more helpful and | 1133 // TODO(estade): Replace all the error messages here with more helpful and |
1127 // translateable ones. TODO(groby): Also add tests. | 1134 // translateable ones. TODO(groby): Also add tests. |
1128 string16 AutofillDialogControllerImpl::InputValidityMessage( | 1135 string16 AutofillDialogControllerImpl::InputValidityMessage( |
1129 AutofillFieldType type, | 1136 AutofillFieldType type, |
1130 const string16& value) const { | 1137 const string16& value) const { |
1131 if (InputIsValid(type, value)) | 1138 switch (AutofillType::GetEquivalentFieldType(type)) { |
1132 return string16(); | 1139 case EMAIL_ADDRESS: |
| 1140 if (!value.empty() && !IsValidEmailAddress(value)) |
| 1141 return ASCIIToUTF16("Are you sure this is right?"); |
| 1142 break; |
1133 | 1143 |
1134 if (value.empty()) | 1144 case CREDIT_CARD_NUMBER: |
1135 return ASCIIToUTF16("You forgot one"); | 1145 if (!value.empty() && !autofill::IsValidCreditCardNumber(value)) |
| 1146 return ASCIIToUTF16("Are you sure this is right?"); |
| 1147 break; |
1136 | 1148 |
1137 return ASCIIToUTF16("Are you sure this is right?"); | 1149 case CREDIT_CARD_NAME: |
| 1150 // Wallet requires a first and last name. |
| 1151 if (!value.empty() && IsPayingWithWallet() && |
| 1152 !IsCardHolderNameValidForWallet(value)) { |
| 1153 return l10n_util::GetStringUTF16( |
| 1154 IDS_AUTOFILL_DIALOG_VALIDATION_WALLET_REQUIRES_TWO_NAMES); |
| 1155 } |
| 1156 break; |
| 1157 |
| 1158 case CREDIT_CARD_EXP_MONTH: |
| 1159 case CREDIT_CARD_EXP_4_DIGIT_YEAR: |
| 1160 break; |
| 1161 |
| 1162 case CREDIT_CARD_VERIFICATION_CODE: |
| 1163 if (!value.empty() && !autofill::IsValidCreditCardSecurityCode(value)) |
| 1164 return ASCIIToUTF16("Are you sure this is right?"); |
| 1165 break; |
| 1166 |
| 1167 case ADDRESS_HOME_LINE1: |
| 1168 break; |
| 1169 |
| 1170 case ADDRESS_HOME_LINE2: |
| 1171 return base::string16(); // Line 2 is optional - always valid. |
| 1172 |
| 1173 case ADDRESS_HOME_CITY: |
| 1174 case ADDRESS_HOME_STATE: |
| 1175 case ADDRESS_HOME_ZIP: |
| 1176 case ADDRESS_HOME_COUNTRY: |
| 1177 break; |
| 1178 |
| 1179 case NAME_FULL: // Used for shipping. |
| 1180 break; |
| 1181 |
| 1182 case PHONE_HOME_WHOLE_NUMBER: // Used in billing section. |
| 1183 break; |
| 1184 |
| 1185 default: |
| 1186 NOTREACHED(); // Trying to validate unknown field. |
| 1187 break; |
| 1188 } |
| 1189 |
| 1190 return !value.empty() ? base::string16() : ASCIIToUTF16("You forgot one"); |
1138 } | 1191 } |
1139 | 1192 |
1140 // TODO(estade): Replace all the error messages here with more helpful and | 1193 // TODO(estade): Replace all the error messages here with more helpful and |
1141 // translateable ones. TODO(groby): Also add tests. | 1194 // translateable ones. TODO(groby): Also add tests. |
1142 ValidityData AutofillDialogControllerImpl::InputsAreValid( | 1195 ValidityData AutofillDialogControllerImpl::InputsAreValid( |
1143 const DetailOutputMap& inputs, ValidationType validation_type) const { | 1196 const DetailOutputMap& inputs, ValidationType validation_type) const { |
1144 ValidityData invalid_messages; | 1197 ValidityData invalid_messages; |
1145 std::map<AutofillFieldType, string16> field_values; | 1198 std::map<AutofillFieldType, string16> field_values; |
1146 for (DetailOutputMap::const_iterator iter = inputs.begin(); | 1199 for (DetailOutputMap::const_iterator iter = inputs.begin(); |
1147 iter != inputs.end(); ++iter) { | 1200 iter != inputs.end(); ++iter) { |
(...skipping 20 matching lines...) Expand all Loading... |
1168 invalid_messages[CREDIT_CARD_EXP_MONTH] = | 1221 invalid_messages[CREDIT_CARD_EXP_MONTH] = |
1169 ASCIIToUTF16("more complicated message"); | 1222 ASCIIToUTF16("more complicated message"); |
1170 invalid_messages[CREDIT_CARD_EXP_4_DIGIT_YEAR] = | 1223 invalid_messages[CREDIT_CARD_EXP_4_DIGIT_YEAR] = |
1171 ASCIIToUTF16("more complicated message"); | 1224 ASCIIToUTF16("more complicated message"); |
1172 } | 1225 } |
1173 } | 1226 } |
1174 | 1227 |
1175 // If there is a credit card number and a CVC, validate them together. | 1228 // If there is a credit card number and a CVC, validate them together. |
1176 if (field_values.count(CREDIT_CARD_NUMBER) && | 1229 if (field_values.count(CREDIT_CARD_NUMBER) && |
1177 field_values.count(CREDIT_CARD_VERIFICATION_CODE) && | 1230 field_values.count(CREDIT_CARD_VERIFICATION_CODE) && |
1178 InputIsValid(CREDIT_CARD_NUMBER, field_values[CREDIT_CARD_NUMBER])) { | 1231 InputValidityMessage(CREDIT_CARD_NUMBER, |
| 1232 field_values[CREDIT_CARD_NUMBER]).empty()) { |
1179 if (!autofill::IsValidCreditCardSecurityCode( | 1233 if (!autofill::IsValidCreditCardSecurityCode( |
1180 field_values[CREDIT_CARD_VERIFICATION_CODE], | 1234 field_values[CREDIT_CARD_VERIFICATION_CODE], |
1181 field_values[CREDIT_CARD_NUMBER])) { | 1235 field_values[CREDIT_CARD_NUMBER])) { |
1182 invalid_messages[CREDIT_CARD_VERIFICATION_CODE] = | 1236 invalid_messages[CREDIT_CARD_VERIFICATION_CODE] = |
1183 ASCIIToUTF16("CVC doesn't match card type!"); | 1237 ASCIIToUTF16("CVC doesn't match card type!"); |
1184 } | 1238 } |
1185 } | 1239 } |
1186 | 1240 |
1187 // Validate the phone number against the country code of the address. | 1241 // Validate the phone number against the country code of the address. |
1188 if (field_values.count(ADDRESS_HOME_COUNTRY) && | 1242 if (field_values.count(ADDRESS_HOME_COUNTRY) && |
(...skipping 1065 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2254 } | 2308 } |
2255 | 2309 |
2256 bool AutofillDialogControllerImpl::IsManuallyEditingAnySection() const { | 2310 bool AutofillDialogControllerImpl::IsManuallyEditingAnySection() const { |
2257 for (size_t section = SECTION_MIN; section <= SECTION_MAX; ++section) { | 2311 for (size_t section = SECTION_MIN; section <= SECTION_MAX; ++section) { |
2258 if (IsManuallyEditingSection(static_cast<DialogSection>(section))) | 2312 if (IsManuallyEditingSection(static_cast<DialogSection>(section))) |
2259 return true; | 2313 return true; |
2260 } | 2314 } |
2261 return false; | 2315 return false; |
2262 } | 2316 } |
2263 | 2317 |
2264 bool AutofillDialogControllerImpl::InputIsValid(AutofillFieldType type, | |
2265 const string16& value) const { | |
2266 switch (AutofillType::GetEquivalentFieldType(type)) { | |
2267 case EMAIL_ADDRESS: | |
2268 return IsValidEmailAddress(value); | |
2269 | |
2270 case CREDIT_CARD_NUMBER: | |
2271 return autofill::IsValidCreditCardNumber(value); | |
2272 case CREDIT_CARD_NAME: | |
2273 break; | |
2274 case CREDIT_CARD_EXP_MONTH: | |
2275 case CREDIT_CARD_EXP_4_DIGIT_YEAR: | |
2276 break; | |
2277 case CREDIT_CARD_VERIFICATION_CODE: | |
2278 return autofill::IsValidCreditCardSecurityCode(value); | |
2279 | |
2280 case ADDRESS_HOME_LINE1: | |
2281 break; | |
2282 case ADDRESS_HOME_LINE2: | |
2283 return true; // Line 2 is optional - always valid. | |
2284 case ADDRESS_HOME_CITY: | |
2285 case ADDRESS_HOME_STATE: | |
2286 case ADDRESS_HOME_ZIP: | |
2287 case ADDRESS_HOME_COUNTRY: | |
2288 break; | |
2289 | |
2290 case NAME_FULL: // Used for shipping. | |
2291 break; | |
2292 | |
2293 case PHONE_HOME_WHOLE_NUMBER: // Used in billing section. | |
2294 break; | |
2295 | |
2296 default: | |
2297 NOTREACHED(); // Trying to validate unknown field. | |
2298 break; | |
2299 } | |
2300 | |
2301 return !value.empty(); | |
2302 } | |
2303 | |
2304 bool AutofillDialogControllerImpl::AllSectionsAreValid() const { | 2318 bool AutofillDialogControllerImpl::AllSectionsAreValid() const { |
2305 for (size_t section = SECTION_MIN; section <= SECTION_MAX; ++section) { | 2319 for (size_t section = SECTION_MIN; section <= SECTION_MAX; ++section) { |
2306 if (!SectionIsValid(static_cast<DialogSection>(section))) | 2320 if (!SectionIsValid(static_cast<DialogSection>(section))) |
2307 return false; | 2321 return false; |
2308 } | 2322 } |
2309 return true; | 2323 return true; |
2310 } | 2324 } |
2311 | 2325 |
2312 bool AutofillDialogControllerImpl::SectionIsValid( | 2326 bool AutofillDialogControllerImpl::SectionIsValid( |
2313 DialogSection section) const { | 2327 DialogSection section) const { |
(...skipping 463 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2777 AutofillMetrics::DIALOG_USER_SIGNED_IN_NO_WALLET_NO_AUTOFILL; | 2791 AutofillMetrics::DIALOG_USER_SIGNED_IN_NO_WALLET_NO_AUTOFILL; |
2778 } | 2792 } |
2779 | 2793 |
2780 // Has Wallet items. | 2794 // Has Wallet items. |
2781 return has_autofill_profiles ? | 2795 return has_autofill_profiles ? |
2782 AutofillMetrics::DIALOG_USER_SIGNED_IN_HAS_WALLET_HAS_AUTOFILL : | 2796 AutofillMetrics::DIALOG_USER_SIGNED_IN_HAS_WALLET_HAS_AUTOFILL : |
2783 AutofillMetrics::DIALOG_USER_SIGNED_IN_HAS_WALLET_NO_AUTOFILL; | 2797 AutofillMetrics::DIALOG_USER_SIGNED_IN_HAS_WALLET_NO_AUTOFILL; |
2784 } | 2798 } |
2785 | 2799 |
2786 } // namespace autofill | 2800 } // namespace autofill |
OLD | NEW |