Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2203)

Side by Side Diff: components/autofill/core/browser/webdata/autofill_table.cc

Issue 962673004: [Autofill/Autocomplete Feature] Substring matching instead of prefix matching. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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...) Expand 10 before | Expand all | Expand 10 after
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 string with sustituted (_), (%) and (!) in supplied |str| by (!_),
374 // (!%) and (!!) respectively.
375 // For e.g. "ex!a_mp%le" -> "ex!!a!_mp!%le".
376 base::string16 SubstringSubstituteText(const base::string16& str) {
377 base::string16 result;
378 base::ReplaceChars(str, base::ASCIIToUTF16("!"), base::ASCIIToUTF16("!!"),
379 &result);
380 base::ReplaceChars(result, base::ASCIIToUTF16("_"), base::ASCIIToUTF16("!_"),
381 &result);
382 base::ReplaceChars(result, base::ASCIIToUTF16("%"), base::ASCIIToUTF16("!%"),
383 &result);
384 return result;
385 }
386
371 } // namespace 387 } // namespace
372 388
373 // The maximum length allowed for form data. 389 // The maximum length allowed for form data.
374 const size_t AutofillTable::kMaxDataLength = 1024; 390 const size_t AutofillTable::kMaxDataLength = 1024;
375 391
376 AutofillTable::AutofillTable(const std::string& app_locale) 392 AutofillTable::AutofillTable(const std::string& app_locale)
377 : app_locale_(app_locale) { 393 : app_locale_(app_locale) {
378 } 394 }
379 395
380 AutofillTable::~AutofillTable() { 396 AutofillTable::~AutofillTable() {
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
449 std::vector<AutofillChange>* changes) { 465 std::vector<AutofillChange>* changes) {
450 return AddFormFieldValueTime(element, changes, Time::Now()); 466 return AddFormFieldValueTime(element, changes, Time::Now());
451 } 467 }
452 468
453 bool AutofillTable::GetFormValuesForElementName( 469 bool AutofillTable::GetFormValuesForElementName(
454 const base::string16& name, 470 const base::string16& name,
455 const base::string16& prefix, 471 const base::string16& prefix,
456 std::vector<base::string16>* values, 472 std::vector<base::string16>* values,
457 int limit) { 473 int limit) {
458 DCHECK(values); 474 DCHECK(values);
459 sql::Statement s; 475 bool succeeded = false;
460 476
461 if (prefix.empty()) { 477 if (prefix.empty()) {
478 sql::Statement s;
462 s.Assign(db_->GetUniqueStatement( 479 s.Assign(db_->GetUniqueStatement(
463 "SELECT value FROM autofill " 480 "SELECT value FROM autofill "
464 "WHERE name = ? " 481 "WHERE name = ? "
465 "ORDER BY count DESC " 482 "ORDER BY count DESC "
466 "LIMIT ?")); 483 "LIMIT ?"));
467 s.BindString16(0, name); 484 s.BindString16(0, name);
468 s.BindInt(1, limit); 485 s.BindInt(1, limit);
486
487 values->clear();
488 while (s.Step())
489 values->push_back(s.ColumnString16(0));
490
491 succeeded = s.Succeeded();
469 } else { 492 } else {
470 base::string16 prefix_lower = base::i18n::ToLower(prefix); 493 base::string16 prefix_lower = base::i18n::ToLower(prefix);
471 base::string16 next_prefix = prefix_lower; 494 base::string16 next_prefix = prefix_lower;
472 next_prefix[next_prefix.length() - 1]++; 495 next_prefix[next_prefix.length() - 1]++;
473 496
474 s.Assign(db_->GetUniqueStatement( 497 sql::Statement s1;
498 s1.Assign(db_->GetUniqueStatement(
475 "SELECT value FROM autofill " 499 "SELECT value FROM autofill "
476 "WHERE name = ? AND " 500 "WHERE name = ? AND "
477 "value_lower >= ? AND " 501 "value_lower >= ? AND "
478 "value_lower < ? " 502 "value_lower < ? "
479 "ORDER BY count DESC " 503 "ORDER BY count DESC "
480 "LIMIT ?")); 504 "LIMIT ?"));
481 s.BindString16(0, name); 505 s1.BindString16(0, name);
482 s.BindString16(1, prefix_lower); 506 s1.BindString16(1, prefix_lower);
483 s.BindString16(2, next_prefix); 507 s1.BindString16(2, next_prefix);
484 s.BindInt(3, limit); 508 s1.BindInt(3, limit);
509
510 values->clear();
511 while (s1.Step())
512 values->push_back(s1.ColumnString16(0));
513
514 succeeded = s1.Succeeded();
515
516 if (IsFeatureSubstringMatchEnabled()) {
517 base::string16 substituted_text = SubstringSubstituteText(prefix_lower);
518 sql::Statement s2;
519 s2.Assign(db_->GetUniqueStatement(
520 "SELECT value FROM autofill "
521 "WHERE name = ? AND ("
522 " value LIKE ? ESCAPE '!' OR "
523 " value LIKE ? ESCAPE '!' OR "
524 " value LIKE ? ESCAPE '!' OR "
525 " value LIKE ? ESCAPE '!' OR "
526 " value LIKE ? ESCAPE '!' OR "
527 " value LIKE ? ESCAPE '!') "
528 "ORDER BY count DESC "
529 "LIMIT ?"));
530 s2.BindString16(0, name);
531 s2.BindString16(1, base::ASCIIToUTF16("% ") + substituted_text +
532 base::ASCIIToUTF16("%"));
533 s2.BindString16(2, base::ASCIIToUTF16("%.") + substituted_text +
534 base::ASCIIToUTF16("%"));
535 s2.BindString16(3, base::ASCIIToUTF16("%,") + substituted_text +
536 base::ASCIIToUTF16("%"));
537 s2.BindString16(4, base::ASCIIToUTF16("%-") + substituted_text +
538 base::ASCIIToUTF16("%"));
539 s2.BindString16(5, base::ASCIIToUTF16("%@") + substituted_text +
540 base::ASCIIToUTF16("%"));
541 s2.BindString16(6, base::ASCIIToUTF16("%!_") + substituted_text +
542 base::ASCIIToUTF16("%"));
543 s2.BindInt(7, limit);
544
545 // Append substring matched suggestions.
546 while (s2.Step())
547 values->push_back(s2.ColumnString16(0));
548
549 succeeded |= s2.Succeeded();
550 }
485 } 551 }
486 552
487 values->clear(); 553 return succeeded;
488 while (s.Step())
489 values->push_back(s.ColumnString16(0));
490 return s.Succeeded();
491 } 554 }
492 555
493 bool AutofillTable::HasFormElements() { 556 bool AutofillTable::HasFormElements() {
494 sql::Statement s(db_->GetUniqueStatement("SELECT COUNT(*) FROM autofill")); 557 sql::Statement s(db_->GetUniqueStatement("SELECT COUNT(*) FROM autofill"));
495 if (!s.Step()) { 558 if (!s.Step()) {
496 NOTREACHED(); 559 NOTREACHED();
497 return false; 560 return false;
498 } 561 }
499 return s.ColumnInt(0) > 0; 562 return s.ColumnInt(0) > 0;
500 } 563 }
(...skipping 1691 matching lines...) Expand 10 before | Expand all | Expand 10 after
2192 insert.BindString16(index++, profile.GetRawInfo(PHONE_HOME_WHOLE_NUMBER)); 2255 insert.BindString16(index++, profile.GetRawInfo(PHONE_HOME_WHOLE_NUMBER));
2193 insert.BindString(index++, profile.language_code()); 2256 insert.BindString(index++, profile.language_code());
2194 insert.Run(); 2257 insert.Run();
2195 insert.Reset(true); 2258 insert.Reset(true);
2196 } 2259 }
2197 2260
2198 return transaction.Commit(); 2261 return transaction.Commit();
2199 } 2262 }
2200 2263
2201 } // namespace autofill 2264 } // namespace autofill
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698