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/webdata/autofill_table.h" | 5 #include "components/autofill/core/browser/webdata/autofill_table.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <cmath> | 8 #include <cmath> |
9 #include <limits> | 9 #include <limits> |
10 #include <map> | 10 #include <map> |
11 #include <set> | 11 #include <set> |
12 #include <string> | 12 #include <string> |
13 #include <vector> | 13 #include <vector> |
14 | 14 |
15 #include "base/command_line.h" | 15 #include "base/command_line.h" |
16 #include "base/guid.h" | 16 #include "base/guid.h" |
17 #include "base/i18n/case_conversion.h" | 17 #include "base/i18n/case_conversion.h" |
18 #include "base/logging.h" | 18 #include "base/logging.h" |
19 #include "base/numerics/safe_conversions.h" | 19 #include "base/numerics/safe_conversions.h" |
20 #include "base/strings/string_number_conversions.h" | 20 #include "base/strings/string_number_conversions.h" |
| 21 #include "base/strings/string_util.h" |
21 #include "base/strings/utf_string_conversions.h" | 22 #include "base/strings/utf_string_conversions.h" |
22 #include "base/time/time.h" | 23 #include "base/time/time.h" |
23 #include "components/autofill/core/browser/autofill_country.h" | 24 #include "components/autofill/core/browser/autofill_country.h" |
24 #include "components/autofill/core/browser/autofill_profile.h" | 25 #include "components/autofill/core/browser/autofill_profile.h" |
25 #include "components/autofill/core/browser/autofill_type.h" | 26 #include "components/autofill/core/browser/autofill_type.h" |
26 #include "components/autofill/core/browser/credit_card.h" | 27 #include "components/autofill/core/browser/credit_card.h" |
27 #include "components/autofill/core/browser/personal_data_manager.h" | 28 #include "components/autofill/core/browser/personal_data_manager.h" |
28 #include "components/autofill/core/browser/webdata/autofill_change.h" | 29 #include "components/autofill/core/browser/webdata/autofill_change.h" |
29 #include "components/autofill/core/browser/webdata/autofill_entry.h" | 30 #include "components/autofill/core/browser/webdata/autofill_entry.h" |
30 #include "components/autofill/core/common/autofill_switches.h" | 31 #include "components/autofill/core/common/autofill_switches.h" |
| 32 #include "components/autofill/core/common/autofill_util.h" |
31 #include "components/autofill/core/common/form_field_data.h" | 33 #include "components/autofill/core/common/form_field_data.h" |
32 #include "components/os_crypt/os_crypt.h" | 34 #include "components/os_crypt/os_crypt.h" |
33 #include "components/webdata/common/web_database.h" | 35 #include "components/webdata/common/web_database.h" |
34 #include "sql/statement.h" | 36 #include "sql/statement.h" |
35 #include "sql/transaction.h" | 37 #include "sql/transaction.h" |
36 #include "ui/base/l10n/l10n_util.h" | 38 #include "ui/base/l10n/l10n_util.h" |
37 #include "url/gurl.h" | 39 #include "url/gurl.h" |
38 | 40 |
39 using base::ASCIIToUTF16; | 41 using base::ASCIIToUTF16; |
40 using base::Time; | 42 using base::Time; |
(...skipping 320 matching lines...) Loading... |
361 } | 363 } |
362 | 364 |
363 CreditCard::ServerStatus ServerStatusStringToEnum(const std::string& status) { | 365 CreditCard::ServerStatus ServerStatusStringToEnum(const std::string& status) { |
364 if (status == "EXPIRED") | 366 if (status == "EXPIRED") |
365 return CreditCard::EXPIRED; | 367 return CreditCard::EXPIRED; |
366 | 368 |
367 DCHECK_EQ("OK", status); | 369 DCHECK_EQ("OK", status); |
368 return CreditCard::OK; | 370 return CreditCard::OK; |
369 } | 371 } |
370 | 372 |
| 373 // Returns |s| with |escaper| in front of each of occurrence of a character from |
| 374 // |special_chars|. Any occurrence of |escaper| in |s| is doubled. For example, |
| 375 // Substitute("hello_world!", "_%", '!'') returns "hello!_world!!". |
| 376 base::string16 Substitute(const base::string16& s, |
| 377 const base::string16& special_chars, |
| 378 const base::char16& escaper) { |
| 379 // Prepend |escaper| to the list of |special_chars|. |
| 380 base::string16 escape_wildcards(special_chars); |
| 381 escape_wildcards.insert(escape_wildcards.begin(), escaper); |
| 382 |
| 383 // Prepend the |escaper| just before |special_chars| in |s|. |
| 384 base::string16 result(s); |
| 385 for (base::char16 c : escape_wildcards) { |
| 386 for (size_t pos = 0; (pos = result.find(c, pos)) != base::string16::npos; |
| 387 pos += 2) { |
| 388 result.insert(result.begin() + pos, escaper); |
| 389 } |
| 390 } |
| 391 |
| 392 return result; |
| 393 } |
| 394 |
371 } // namespace | 395 } // namespace |
372 | 396 |
373 // The maximum length allowed for form data. | 397 // The maximum length allowed for form data. |
374 const size_t AutofillTable::kMaxDataLength = 1024; | 398 const size_t AutofillTable::kMaxDataLength = 1024; |
375 | 399 |
376 AutofillTable::AutofillTable(const std::string& app_locale) | 400 AutofillTable::AutofillTable(const std::string& app_locale) |
377 : app_locale_(app_locale) { | 401 : app_locale_(app_locale) { |
378 } | 402 } |
379 | 403 |
380 AutofillTable::~AutofillTable() { | 404 AutofillTable::~AutofillTable() { |
(...skipping 68 matching lines...) Loading... |
449 std::vector<AutofillChange>* changes) { | 473 std::vector<AutofillChange>* changes) { |
450 return AddFormFieldValueTime(element, changes, Time::Now()); | 474 return AddFormFieldValueTime(element, changes, Time::Now()); |
451 } | 475 } |
452 | 476 |
453 bool AutofillTable::GetFormValuesForElementName( | 477 bool AutofillTable::GetFormValuesForElementName( |
454 const base::string16& name, | 478 const base::string16& name, |
455 const base::string16& prefix, | 479 const base::string16& prefix, |
456 std::vector<base::string16>* values, | 480 std::vector<base::string16>* values, |
457 int limit) { | 481 int limit) { |
458 DCHECK(values); | 482 DCHECK(values); |
459 sql::Statement s; | 483 bool succeeded = false; |
460 | 484 |
461 if (prefix.empty()) { | 485 if (prefix.empty()) { |
| 486 sql::Statement s; |
462 s.Assign(db_->GetUniqueStatement( | 487 s.Assign(db_->GetUniqueStatement( |
463 "SELECT value FROM autofill " | 488 "SELECT value FROM autofill " |
464 "WHERE name = ? " | 489 "WHERE name = ? " |
465 "ORDER BY count DESC " | 490 "ORDER BY count DESC " |
466 "LIMIT ?")); | 491 "LIMIT ?")); |
467 s.BindString16(0, name); | 492 s.BindString16(0, name); |
468 s.BindInt(1, limit); | 493 s.BindInt(1, limit); |
| 494 |
| 495 values->clear(); |
| 496 while (s.Step()) |
| 497 values->push_back(s.ColumnString16(0)); |
| 498 |
| 499 succeeded = s.Succeeded(); |
469 } else { | 500 } else { |
470 base::string16 prefix_lower = base::i18n::ToLower(prefix); | 501 base::string16 prefix_lower = base::i18n::ToLower(prefix); |
471 base::string16 next_prefix = prefix_lower; | 502 base::string16 next_prefix = prefix_lower; |
472 next_prefix[next_prefix.length() - 1]++; | 503 next_prefix[next_prefix.length() - 1]++; |
473 | 504 |
474 s.Assign(db_->GetUniqueStatement( | 505 sql::Statement s1; |
| 506 s1.Assign(db_->GetUniqueStatement( |
475 "SELECT value FROM autofill " | 507 "SELECT value FROM autofill " |
476 "WHERE name = ? AND " | 508 "WHERE name = ? AND " |
477 "value_lower >= ? AND " | 509 "value_lower >= ? AND " |
478 "value_lower < ? " | 510 "value_lower < ? " |
479 "ORDER BY count DESC " | 511 "ORDER BY count DESC " |
480 "LIMIT ?")); | 512 "LIMIT ?")); |
481 s.BindString16(0, name); | 513 s1.BindString16(0, name); |
482 s.BindString16(1, prefix_lower); | 514 s1.BindString16(1, prefix_lower); |
483 s.BindString16(2, next_prefix); | 515 s1.BindString16(2, next_prefix); |
484 s.BindInt(3, limit); | 516 s1.BindInt(3, limit); |
| 517 |
| 518 values->clear(); |
| 519 while (s1.Step()) |
| 520 values->push_back(s1.ColumnString16(0)); |
| 521 |
| 522 succeeded = s1.Succeeded(); |
| 523 |
| 524 if (IsFeatureSubstringMatchEnabled()) { |
| 525 sql::Statement s2; |
| 526 s2.Assign(db_->GetUniqueStatement( |
| 527 "SELECT value FROM autofill " |
| 528 "WHERE name = ? AND (" |
| 529 " value LIKE '% ' || :prefix || '%' ESCAPE '!' OR " |
| 530 " value LIKE '%.' || :prefix || '%' ESCAPE '!' OR " |
| 531 " value LIKE '%,' || :prefix || '%' ESCAPE '!' OR " |
| 532 " value LIKE '%-' || :prefix || '%' ESCAPE '!' OR " |
| 533 " value LIKE '%@' || :prefix || '%' ESCAPE '!' OR " |
| 534 " value LIKE '%!_' || :prefix || '%' ESCAPE '!' ) " |
| 535 "ORDER BY count DESC " |
| 536 "LIMIT ?")); |
| 537 |
| 538 s2.BindString16(0, name); |
| 539 // escaper as L'!' -> 0x21. |
| 540 s2.BindString16(1, Substitute(prefix_lower, ASCIIToUTF16("_%"), 0x21)); |
| 541 s2.BindInt(2, limit); |
| 542 while (s2.Step()) |
| 543 values->push_back(s2.ColumnString16(0)); |
| 544 |
| 545 succeeded &= s2.Succeeded(); |
| 546 } |
485 } | 547 } |
486 | 548 |
487 values->clear(); | 549 return succeeded; |
488 while (s.Step()) | |
489 values->push_back(s.ColumnString16(0)); | |
490 return s.Succeeded(); | |
491 } | 550 } |
492 | 551 |
493 bool AutofillTable::HasFormElements() { | 552 bool AutofillTable::HasFormElements() { |
494 sql::Statement s(db_->GetUniqueStatement("SELECT COUNT(*) FROM autofill")); | 553 sql::Statement s(db_->GetUniqueStatement("SELECT COUNT(*) FROM autofill")); |
495 if (!s.Step()) { | 554 if (!s.Step()) { |
496 NOTREACHED(); | 555 NOTREACHED(); |
497 return false; | 556 return false; |
498 } | 557 } |
499 return s.ColumnInt(0) > 0; | 558 return s.ColumnInt(0) > 0; |
500 } | 559 } |
(...skipping 1691 matching lines...) Loading... |
2192 insert.BindString16(index++, profile.GetRawInfo(PHONE_HOME_WHOLE_NUMBER)); | 2251 insert.BindString16(index++, profile.GetRawInfo(PHONE_HOME_WHOLE_NUMBER)); |
2193 insert.BindString(index++, profile.language_code()); | 2252 insert.BindString(index++, profile.language_code()); |
2194 insert.Run(); | 2253 insert.Run(); |
2195 insert.Reset(true); | 2254 insert.Reset(true); |
2196 } | 2255 } |
2197 | 2256 |
2198 return transaction.Commit(); | 2257 return transaction.Commit(); |
2199 } | 2258 } |
2200 | 2259 |
2201 } // namespace autofill | 2260 } // namespace autofill |
OLD | NEW |