Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/webdata/autofill_table.h" | 5 #include "chrome/browser/webdata/autofill_table.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <limits> | 8 #include <limits> |
| 9 #include <map> | 9 #include <map> |
| 10 #include <set> | 10 #include <set> |
| (...skipping 407 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 418 return false; | 418 return false; |
| 419 | 419 |
| 420 for (AutofillElementList::iterator itr = elements.begin(); | 420 for (AutofillElementList::iterator itr = elements.begin(); |
| 421 itr != elements.end(); itr++) { | 421 itr != elements.end(); itr++) { |
| 422 int how_many = 0; | 422 int how_many = 0; |
| 423 if (!RemoveFormElementForTimeRange(itr->a, delete_begin, delete_end, | 423 if (!RemoveFormElementForTimeRange(itr->a, delete_begin, delete_end, |
| 424 &how_many)) { | 424 &how_many)) { |
| 425 return false; | 425 return false; |
| 426 } | 426 } |
| 427 bool was_removed = false; | 427 bool was_removed = false; |
| 428 if (!AddToCountOfFormElement(itr->a, -how_many, &was_removed)) | 428 if (how_many == 2) { |
| 429 return false; | 429 // We store at most 2 time stamps. |
| 430 was_removed = true; | |
| 431 if (!RemoveFormElementForID(itr->a)) | |
| 432 return false; | |
| 433 } else { | |
| 434 if (!AddToCountOfFormElement(itr->a, -how_many, &was_removed)) | |
| 435 return false; | |
| 436 } | |
| 430 AutofillChange::Type change_type = | 437 AutofillChange::Type change_type = |
| 431 was_removed ? AutofillChange::REMOVE : AutofillChange::UPDATE; | 438 was_removed ? AutofillChange::REMOVE : AutofillChange::UPDATE; |
| 432 changes->push_back(AutofillChange(change_type, | 439 changes->push_back(AutofillChange(change_type, |
| 433 AutofillKey(itr->b, itr->c))); | 440 AutofillKey(itr->b, itr->c))); |
| 434 } | 441 } |
| 435 | 442 |
| 436 return true; | 443 return true; |
| 437 } | 444 } |
| 438 | 445 |
| 446 bool AutofillTable::RemoveFormElementsAccessedBefore( | |
| 447 const base::Time& delete_end, std::vector<AutofillChange>* changes) { | |
| 448 DCHECK(changes); | |
| 449 DCHECK(!delete_end.is_null()); | |
| 450 // Query for the pair_id, name, and value of all form elements that | |
| 451 // were used between the given times. | |
| 452 sql::Statement select_for_delete(db_->GetUniqueStatement( | |
| 453 "SELECT DISTINCT pair_id, name, value " | |
| 454 "FROM autofill WHERE pair_id NOT IN " | |
| 455 "(SELECT DISTINCT pair_id " | |
| 456 "FROM autofill_dates WHERE date_created >= ?)")); | |
| 457 select_for_delete.BindInt64(0, delete_end.ToTimeT()); | |
| 458 AutofillElementList entries_to_delete; | |
| 459 while (select_for_delete.Step()) { | |
| 460 entries_to_delete.push_back(MakeTuple(select_for_delete.ColumnInt64(0), | |
| 461 select_for_delete.ColumnString16(1), | |
| 462 select_for_delete.ColumnString16(2))); | |
| 463 } | |
| 464 if (!select_for_delete.Succeeded()) | |
| 465 return false; | |
| 466 sql::Statement delete_data_statement(db_->GetUniqueStatement( | |
| 467 "DELETE FROM autofill WHERE pair_id NOT IN (" | |
| 468 "SELECT pair_id FROM autofill_dates WHERE date_created >= ?)")); | |
| 469 delete_data_statement.BindInt64(0, delete_end.ToTimeT()); | |
| 470 if (!delete_data_statement.Run()) | |
| 471 return false; | |
| 472 sql::Statement delete_times_statement(db_->GetUniqueStatement( | |
| 473 "DELETE FROM autofill_dates WHERE pair_id NOT IN (" | |
| 474 "SELECT pair_id FROM autofill_dates WHERE date_created >= ?)")); | |
| 475 delete_times_statement.BindInt64(0, delete_end.ToTimeT()); | |
| 476 if (!delete_times_statement.Run()) | |
| 477 return false; | |
| 478 | |
| 479 // Cull remaining entries. | |
| 480 std::vector<AutofillEntry> entries; | |
| 481 if (!GetAllAutofillEntries(&entries)) | |
| 482 return false; | |
| 483 sql::Statement cull_date_entry(db_->GetUniqueStatement( | |
| 484 "DELETE FROM autofill_dates " | |
| 485 "WHERE pair_id == (SELECT pair_id FROM autofill " | |
| 486 "WHERE name = ? and value = ?)" | |
| 487 "AND date_created != ? AND date_created != ?")); | |
| 488 for (size_t i = 0; i < entries.size(); ++i) { | |
| 489 cull_date_entry.BindString16(0, entries[i].key().name()); | |
| 490 cull_date_entry.BindString16(1, entries[i].key().value()); | |
| 491 cull_date_entry.BindInt64(2, | |
| 492 entries[i].timestamps().empty() ? 0 : | |
| 493 entries[i].timestamps().front().ToTimeT()); | |
| 494 cull_date_entry.BindInt64(3, | |
| 495 entries[i].timestamps().empty() ? 0 : | |
| 496 entries[i].timestamps().back().ToTimeT()); | |
| 497 if (!cull_date_entry.Run()) | |
| 498 return false; | |
| 499 cull_date_entry.Reset(); | |
| 500 } | |
| 501 | |
| 502 changes->clear(); | |
| 503 changes->reserve(entries_to_delete.size()); | |
| 504 | |
| 505 for (AutofillElementList::iterator it = entries_to_delete.begin(); | |
| 506 it != entries_to_delete.end(); ++it) { | |
| 507 changes->push_back(AutofillChange( | |
| 508 AutofillChange::REMOVE, AutofillKey(it->b, it->c))); | |
| 509 } | |
| 510 return true; | |
| 511 } | |
| 512 | |
| 439 bool AutofillTable::RemoveFormElementForTimeRange(int64 pair_id, | 513 bool AutofillTable::RemoveFormElementForTimeRange(int64 pair_id, |
| 440 const Time& delete_begin, | 514 const Time& delete_begin, |
| 441 const Time& delete_end, | 515 const Time& delete_end, |
| 442 int* how_many) { | 516 int* how_many) { |
| 443 sql::Statement s(db_->GetUniqueStatement( | 517 sql::Statement s(db_->GetUniqueStatement( |
| 444 "DELETE FROM autofill_dates WHERE pair_id = ? AND " | 518 "DELETE FROM autofill_dates WHERE pair_id = ? AND " |
| 445 "date_created >= ? AND date_created < ?")); | 519 "date_created >= ? AND date_created < ?")); |
| 446 s.BindInt64(0, pair_id); | 520 s.BindInt64(0, pair_id); |
| 447 s.BindInt64(1, delete_begin.is_null() ? 0 : delete_begin.ToTimeT()); | 521 s.BindInt64(1, delete_begin.is_null() ? 0 : delete_begin.ToTimeT()); |
| 448 s.BindInt64(2, delete_end.is_null() ? std::numeric_limits<int64>::max() : | 522 s.BindInt64(2, delete_end.is_null() ? std::numeric_limits<int64>::max() : |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 545 const Time& date_created) { | 619 const Time& date_created) { |
| 546 sql::Statement s(db_->GetUniqueStatement( | 620 sql::Statement s(db_->GetUniqueStatement( |
| 547 "INSERT INTO autofill_dates " | 621 "INSERT INTO autofill_dates " |
| 548 "(pair_id, date_created) VALUES (?, ?)")); | 622 "(pair_id, date_created) VALUES (?, ?)")); |
| 549 s.BindInt64(0, pair_id); | 623 s.BindInt64(0, pair_id); |
| 550 s.BindInt64(1, date_created.ToTimeT()); | 624 s.BindInt64(1, date_created.ToTimeT()); |
| 551 | 625 |
| 552 return s.Run(); | 626 return s.Run(); |
| 553 } | 627 } |
| 554 | 628 |
| 629 bool AutofillTable::DeleteLastAccess(int64 pair_id) { | |
| 630 sql::Statement s(db_->GetUniqueStatement( | |
| 631 "DELETE FROM autofill_dates WHERE pair_id = ? and date_created IN " | |
| 632 "(SELECT date_created FROM autofill_dates WHERE pair_id = ? " | |
| 633 "ORDER BY date_created DESC LIMIT 1)")); | |
|
Ilya Sherman
2012/03/15 21:00:41
The nested lookup is a little hard to follow. Did
GeorgeY
2012/03/17 00:36:16
Yes it is for efficiency reasons: doing two SQL st
| |
| 634 s.BindInt64(0, pair_id); | |
| 635 s.BindInt64(1, pair_id); | |
| 636 | |
| 637 return s.Run(); | |
| 638 } | |
| 639 | |
| 555 bool AutofillTable::AddFormFieldValuesTime( | 640 bool AutofillTable::AddFormFieldValuesTime( |
| 556 const std::vector<FormField>& elements, | 641 const std::vector<FormField>& elements, |
| 557 std::vector<AutofillChange>* changes, | 642 std::vector<AutofillChange>* changes, |
| 558 Time time) { | 643 Time time) { |
| 559 // Only add one new entry for each unique element name. Use |seen_names| to | 644 // Only add one new entry for each unique element name. Use |seen_names| to |
| 560 // track this. Add up to |kMaximumUniqueNames| unique entries per form. | 645 // track this. Add up to |kMaximumUniqueNames| unique entries per form. |
| 561 const size_t kMaximumUniqueNames = 256; | 646 const size_t kMaximumUniqueNames = 256; |
| 562 std::set<string16> seen_names; | 647 std::set<string16> seen_names; |
| 563 bool result = true; | 648 bool result = true; |
| 564 for (std::vector<FormField>::const_iterator | 649 for (std::vector<FormField>::const_iterator |
| (...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 726 | 811 |
| 727 if (!GetIDAndCountOfFormElement(element, &pair_id, &count)) | 812 if (!GetIDAndCountOfFormElement(element, &pair_id, &count)) |
| 728 return false; | 813 return false; |
| 729 | 814 |
| 730 if (count == 0 && !InsertFormElement(element, &pair_id)) | 815 if (count == 0 && !InsertFormElement(element, &pair_id)) |
| 731 return false; | 816 return false; |
| 732 | 817 |
| 733 if (!SetCountOfFormElement(pair_id, count + 1)) | 818 if (!SetCountOfFormElement(pair_id, count + 1)) |
| 734 return false; | 819 return false; |
| 735 | 820 |
| 821 // If we already have more than 2 times delete last one, before adding new | |
| 822 // one. | |
|
Ilya Sherman
2012/03/15 21:00:41
nit: Perhaps we should overwrite it rather than de
GeorgeY
2012/03/17 00:36:16
I am not 100% certain that it will be more efficie
| |
| 823 if (count >= 2 && !DeleteLastAccess(pair_id)) | |
| 824 return false; | |
| 825 | |
| 736 if (!InsertPairIDAndDate(pair_id, time)) | 826 if (!InsertPairIDAndDate(pair_id, time)) |
| 737 return false; | 827 return false; |
| 738 | 828 |
| 739 AutofillChange::Type change_type = | 829 AutofillChange::Type change_type = |
| 740 count == 0 ? AutofillChange::ADD : AutofillChange::UPDATE; | 830 count == 0 ? AutofillChange::ADD : AutofillChange::UPDATE; |
| 741 changes->push_back( | 831 changes->push_back( |
| 742 AutofillChange(change_type, | 832 AutofillChange(change_type, |
| 743 AutofillKey(element.name, element.value))); | 833 AutofillKey(element.name, element.value))); |
| 744 return true; | 834 return true; |
| 745 } | 835 } |
| (...skipping 1127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1873 "WHERE guid=?")); | 1963 "WHERE guid=?")); |
| 1874 s_date.BindInt64(0, date_item->second); | 1964 s_date.BindInt64(0, date_item->second); |
| 1875 s_date.BindString(1, iter->guid()); | 1965 s_date.BindString(1, iter->guid()); |
| 1876 | 1966 |
| 1877 if (!s_date.Run()) | 1967 if (!s_date.Run()) |
| 1878 return false; | 1968 return false; |
| 1879 } | 1969 } |
| 1880 | 1970 |
| 1881 return true; | 1971 return true; |
| 1882 } | 1972 } |
| OLD | NEW |