Chromium Code Reviews| 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 "components/autofill/core/browser/credit_card_field.h" | 5 #include "components/autofill/core/browser/credit_card_field.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/stl_util.h" | |
| 10 #include "base/strings/string16.h" | 11 #include "base/strings/string16.h" |
| 12 #include "base/strings/string_number_conversions.h" | |
| 11 #include "base/strings/string_util.h" | 13 #include "base/strings/string_util.h" |
| 12 #include "base/strings/utf_string_conversions.h" | 14 #include "base/strings/utf_string_conversions.h" |
| 15 #include "base/time/time.h" | |
| 13 #include "components/autofill/core/browser/autofill_field.h" | 16 #include "components/autofill/core/browser/autofill_field.h" |
| 14 #include "components/autofill/core/browser/autofill_regex_constants.h" | 17 #include "components/autofill/core/browser/autofill_regex_constants.h" |
| 18 #include "components/autofill/core/browser/autofill_regexes.h" | |
| 15 #include "components/autofill/core/browser/autofill_scanner.h" | 19 #include "components/autofill/core/browser/autofill_scanner.h" |
| 16 #include "components/autofill/core/browser/field_types.h" | 20 #include "components/autofill/core/browser/field_types.h" |
| 17 | 21 |
| 18 namespace autofill { | 22 namespace autofill { |
| 19 | 23 |
| 24 namespace { | |
| 25 | |
| 20 // Credit card numbers are at most 19 digits in length. | 26 // Credit card numbers are at most 19 digits in length. |
| 21 // [Ref: http://en.wikipedia.org/wiki/Bank_card_number] | 27 // [Ref: http://en.wikipedia.org/wiki/Bank_card_number] |
| 22 static const size_t kMaxValidCardNumberSize = 19; | 28 const size_t kMaxValidCardNumberSize = 19; |
| 29 | |
| 30 // Look for the vector |regex_needles| in |haystack|. Returns true if a | |
| 31 // consecutive section of |haystack| matches |regex_needles|. | |
| 32 bool FindConsecutiveStrings(const std::vector<base::string16>& regex_needles, | |
| 33 const std::vector<base::string16>& haystack) { | |
| 34 if (regex_needles.empty() || haystack.empty()) | |
| 35 return false; | |
| 36 | |
| 37 for (size_t i = 0; i < haystack.size() - regex_needles.size() + 1; ++i) { | |
| 38 for (size_t j = 0; j < regex_needles.size(); ++j) { | |
| 39 if (!MatchesPattern(haystack[i + j], regex_needles[j])) | |
| 40 break; | |
| 41 | |
| 42 if (j == regex_needles.size() - 1) | |
| 43 return true; | |
| 44 } | |
| 45 } | |
| 46 return false; | |
| 47 } | |
| 48 | |
| 49 } // namespace | |
| 23 | 50 |
| 24 // static | 51 // static |
| 25 scoped_ptr<FormField> CreditCardField::Parse(AutofillScanner* scanner) { | 52 scoped_ptr<FormField> CreditCardField::Parse(AutofillScanner* scanner) { |
| 26 if (scanner->IsEnd()) | 53 if (scanner->IsEnd()) |
| 27 return nullptr; | 54 return nullptr; |
| 28 | 55 |
| 29 scoped_ptr<CreditCardField> credit_card_field(new CreditCardField); | 56 scoped_ptr<CreditCardField> credit_card_field(new CreditCardField); |
| 30 size_t saved_cursor = scanner->SaveCursor(); | 57 size_t saved_cursor = scanner->SaveCursor(); |
| 31 | 58 |
| 32 // Credit card fields can appear in many different orders. | 59 // Credit card fields can appear in many different orders. |
| (...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 150 bool has_date_or_mm_yy = (credit_card_field->expiration_date_ || | 177 bool has_date_or_mm_yy = (credit_card_field->expiration_date_ || |
| 151 (credit_card_field->expiration_month_ && | 178 (credit_card_field->expiration_month_ && |
| 152 credit_card_field->expiration_year_)); | 179 credit_card_field->expiration_year_)); |
| 153 if (has_cc_number_or_verification && has_date_or_mm_yy) | 180 if (has_cc_number_or_verification && has_date_or_mm_yy) |
| 154 return credit_card_field.Pass(); | 181 return credit_card_field.Pass(); |
| 155 | 182 |
| 156 scanner->RewindTo(saved_cursor); | 183 scanner->RewindTo(saved_cursor); |
| 157 return nullptr; | 184 return nullptr; |
| 158 } | 185 } |
| 159 | 186 |
| 187 // static | |
| 188 bool CreditCardField::LikelyCardMonthSelectField(AutofillScanner* scanner) { | |
| 189 if (scanner->IsEnd()) | |
| 190 return false; | |
| 191 | |
| 192 AutofillField* field = scanner->Cursor(); | |
| 193 if (!MatchesFormControlType(field->form_control_type, MATCH_SELECT)) | |
| 194 return false; | |
| 195 | |
| 196 if (field->option_values.size() < 12 || field->option_values.size() > 13) | |
| 197 return false; | |
| 198 | |
| 199 // Filter out years. | |
| 200 const base::string16 kNumericalYearRe = | |
| 201 base::ASCIIToUTF16("[1-9][0-9][0-9][0-9]"); | |
| 202 for (const auto& value : field->option_values) { | |
| 203 if (MatchesPattern(value, kNumericalYearRe)) | |
| 204 return false; | |
| 205 } | |
| 206 for (const auto& value : field->option_contents) { | |
| 207 if (MatchesPattern(value, kNumericalYearRe)) | |
| 208 return false; | |
| 209 } | |
| 210 | |
| 211 // Look for numerical months. | |
| 212 const base::string16 kNumericalMonthRe = base::ASCIIToUTF16("12"); | |
| 213 if (MatchesPattern(field->option_values.back(), kNumericalMonthRe) || | |
| 214 MatchesPattern(field->option_contents.back(), kNumericalMonthRe)) { | |
| 215 return true; | |
| 216 } | |
| 217 | |
| 218 // Maybe do more matches here. e.g. look for (translated) December. | |
| 219 | |
| 220 // Unsure? Return false. | |
| 221 return false; | |
| 222 } | |
| 223 | |
| 224 // static | |
| 225 bool CreditCardField::LikelyCardYearSelectField(AutofillScanner* scanner) { | |
| 226 if (scanner->IsEnd()) | |
| 227 return false; | |
| 228 | |
| 229 AutofillField* field = scanner->Cursor(); | |
| 230 if (!MatchesFormControlType(field->form_control_type, MATCH_SELECT)) | |
| 231 return false; | |
| 232 | |
| 233 const base::Time time_now = base::Time::Now(); | |
| 234 base::Time::Exploded time_exploded; | |
| 235 time_now.UTCExplode(&time_exploded); | |
| 236 | |
| 237 const int kYearsToMatch = 3; | |
| 238 std::vector<base::string16> years_to_check; | |
| 239 for (int year = time_exploded.year; | |
| 240 year < time_exploded.year + kYearsToMatch; | |
| 241 ++year) { | |
| 242 years_to_check.push_back(base::IntToString16(year)); | |
| 243 } | |
| 244 return (FindConsecutiveStrings(years_to_check, field->option_values) || | |
| 245 FindConsecutiveStrings(years_to_check, field->option_contents)); | |
| 246 } | |
| 247 | |
| 160 CreditCardField::CreditCardField() | 248 CreditCardField::CreditCardField() |
| 161 : cardholder_(nullptr), | 249 : cardholder_(nullptr), |
| 162 cardholder_last_(nullptr), | 250 cardholder_last_(nullptr), |
| 163 type_(nullptr), | 251 type_(nullptr), |
| 164 verification_(nullptr), | 252 verification_(nullptr), |
| 165 expiration_month_(nullptr), | 253 expiration_month_(nullptr), |
| 166 expiration_year_(nullptr), | 254 expiration_year_(nullptr), |
| 167 expiration_date_(nullptr), | 255 expiration_date_(nullptr), |
| 168 exp_year_type_(CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR) { | 256 exp_year_type_(CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR) { |
| 169 } | 257 } |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 208 expiration_date_ = scanner->Cursor(); | 296 expiration_date_ = scanner->Cursor(); |
| 209 expiration_month_ = nullptr; | 297 expiration_month_ = nullptr; |
| 210 expiration_year_ = nullptr; | 298 expiration_year_ = nullptr; |
| 211 scanner->Advance(); | 299 scanner->Advance(); |
| 212 return true; | 300 return true; |
| 213 } | 301 } |
| 214 | 302 |
| 215 if (expiration_month_ || expiration_date_) | 303 if (expiration_month_ || expiration_date_) |
| 216 return false; | 304 return false; |
| 217 | 305 |
| 218 // First try to parse split month/year expiration fields. | 306 // First try to parse split month/year expiration fields by looking for a |
| 307 // pair of select fields that look like month/year. | |
| 219 size_t month_year_saved_cursor = scanner->SaveCursor(); | 308 size_t month_year_saved_cursor = scanner->SaveCursor(); |
| 309 | |
| 310 if (LikelyCardMonthSelectField(scanner)) { | |
| 311 expiration_month_ = scanner->Cursor(); | |
| 312 scanner->Advance(); | |
| 313 if (LikelyCardYearSelectField(scanner)) { | |
|
Evan Stade
2015/03/19 15:56:39
I wonder if some locales do year then month?
Lei Zhang
2015/03/19 18:35:57
Probably, but everything below assumes month/year
| |
| 314 expiration_year_ = scanner->Cursor(); | |
| 315 scanner->Advance(); | |
| 316 return true; | |
| 317 } | |
| 318 expiration_month_ = nullptr; | |
| 319 expiration_year_ = nullptr; | |
| 320 } | |
| 321 | |
| 322 // If that fails, do a general regex search. | |
| 323 scanner->RewindTo(month_year_saved_cursor); | |
| 220 const int kMatchTelAndSelect = MATCH_DEFAULT | MATCH_TELEPHONE | MATCH_SELECT; | 324 const int kMatchTelAndSelect = MATCH_DEFAULT | MATCH_TELEPHONE | MATCH_SELECT; |
| 221 if (ParseFieldSpecifics(scanner, | 325 if (ParseFieldSpecifics(scanner, |
| 222 base::UTF8ToUTF16(kExpirationMonthRe), | 326 base::UTF8ToUTF16(kExpirationMonthRe), |
| 223 kMatchTelAndSelect, | 327 kMatchTelAndSelect, |
| 224 &expiration_month_) && | 328 &expiration_month_) && |
| 225 ParseFieldSpecifics(scanner, | 329 ParseFieldSpecifics(scanner, |
| 226 base::UTF8ToUTF16(kExpirationYearRe), | 330 base::UTF8ToUTF16(kExpirationYearRe), |
| 227 kMatchTelAndSelect, | 331 kMatchTelAndSelect, |
| 228 &expiration_year_)) { | 332 &expiration_year_)) { |
| 229 return true; | 333 return true; |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 268 | 372 |
| 269 ServerFieldType CreditCardField::GetExpirationYearType() const { | 373 ServerFieldType CreditCardField::GetExpirationYearType() const { |
| 270 return (expiration_date_ | 374 return (expiration_date_ |
| 271 ? exp_year_type_ | 375 ? exp_year_type_ |
| 272 : ((expiration_year_ && expiration_year_->max_length == 2) | 376 : ((expiration_year_ && expiration_year_->max_length == 2) |
| 273 ? CREDIT_CARD_EXP_2_DIGIT_YEAR | 377 ? CREDIT_CARD_EXP_2_DIGIT_YEAR |
| 274 : CREDIT_CARD_EXP_4_DIGIT_YEAR)); | 378 : CREDIT_CARD_EXP_4_DIGIT_YEAR)); |
| 275 } | 379 } |
| 276 | 380 |
| 277 } // namespace autofill | 381 } // namespace autofill |
| OLD | NEW |