| 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> |
| 11 #include <string> | 11 #include <string> |
| 12 #include <vector> | 12 #include <vector> |
| 13 | 13 |
| 14 #include "base/guid.h" | 14 #include "base/guid.h" |
| 15 #include "base/i18n/case_conversion.h" | 15 #include "base/i18n/case_conversion.h" |
| 16 #include "base/logging.h" | 16 #include "base/logging.h" |
| 17 #include "base/string_number_conversions.h" | 17 #include "base/string_number_conversions.h" |
| 18 #include "base/time.h" | 18 #include "base/time.h" |
| 19 #include "base/tuple.h" | 19 #include "base/tuple.h" |
| 20 #include "chrome/browser/autofill/autofill_country.h" | 20 #include "chrome/browser/autofill/autofill_country.h" |
| 21 #include "chrome/browser/autofill/autofill_profile.h" | 21 #include "chrome/browser/autofill/autofill_profile.h" |
| 22 #include "chrome/browser/autofill/autofill_type.h" | 22 #include "chrome/browser/autofill/autofill_type.h" |
| 23 #include "chrome/browser/autofill/credit_card.h" | 23 #include "chrome/browser/autofill/credit_card.h" |
| 24 #include "chrome/browser/autofill/personal_data_manager.h" | 24 #include "chrome/browser/autofill/personal_data_manager.h" |
| 25 #include "chrome/browser/password_manager/encryptor.h" | 25 #include "chrome/browser/password_manager/encryptor.h" |
| 26 #include "chrome/browser/webdata/autofill_change.h" | 26 #include "chrome/browser/webdata/autofill_change.h" |
| 27 #include "chrome/browser/webdata/autofill_entry.h" | 27 #include "chrome/browser/webdata/autofill_entry.h" |
| 28 #include "chrome/common/form_field_data.h" |
| 28 #include "sql/statement.h" | 29 #include "sql/statement.h" |
| 29 #include "ui/base/l10n/l10n_util.h" | 30 #include "ui/base/l10n/l10n_util.h" |
| 30 #include "webkit/forms/form_field.h" | |
| 31 | 31 |
| 32 using base::Time; | 32 using base::Time; |
| 33 using webkit::forms::FormField; | |
| 34 | 33 |
| 35 namespace { | 34 namespace { |
| 36 | 35 |
| 37 typedef std::vector<Tuple3<int64, string16, string16> > AutofillElementList; | 36 typedef std::vector<Tuple3<int64, string16, string16> > AutofillElementList; |
| 38 | 37 |
| 39 // TODO(dhollowa): Find a common place for this. It is duplicated in | 38 // TODO(dhollowa): Find a common place for this. It is duplicated in |
| 40 // personal_data_manager.cc. | 39 // personal_data_manager.cc. |
| 41 template<typename T> | 40 template<typename T> |
| 42 T* address_of(T& v) { | 41 T* address_of(T& v) { |
| 43 return &v; | 42 return &v; |
| (...skipping 292 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 336 return (InitMainTable() && InitCreditCardsTable() && InitDatesTable() && | 335 return (InitMainTable() && InitCreditCardsTable() && InitDatesTable() && |
| 337 InitProfilesTable() && InitProfileNamesTable() && | 336 InitProfilesTable() && InitProfileNamesTable() && |
| 338 InitProfileEmailsTable() && InitProfilePhonesTable() && | 337 InitProfileEmailsTable() && InitProfilePhonesTable() && |
| 339 InitProfileTrashTable()); | 338 InitProfileTrashTable()); |
| 340 } | 339 } |
| 341 | 340 |
| 342 bool AutofillTable::IsSyncable() { | 341 bool AutofillTable::IsSyncable() { |
| 343 return true; | 342 return true; |
| 344 } | 343 } |
| 345 | 344 |
| 346 bool AutofillTable::AddFormFieldValues(const std::vector<FormField>& elements, | 345 bool AutofillTable::AddFormFieldValues( |
| 347 std::vector<AutofillChange>* changes) { | 346 const std::vector<FormFieldData>& elements, |
| 347 std::vector<AutofillChange>* changes) { |
| 348 return AddFormFieldValuesTime(elements, changes, Time::Now()); | 348 return AddFormFieldValuesTime(elements, changes, Time::Now()); |
| 349 } | 349 } |
| 350 | 350 |
| 351 bool AutofillTable::AddFormFieldValue(const FormField& element, | 351 bool AutofillTable::AddFormFieldValue(const FormFieldData& element, |
| 352 std::vector<AutofillChange>* changes) { | 352 std::vector<AutofillChange>* changes) { |
| 353 return AddFormFieldValueTime(element, changes, Time::Now()); | 353 return AddFormFieldValueTime(element, changes, Time::Now()); |
| 354 } | 354 } |
| 355 | 355 |
| 356 bool AutofillTable::GetFormValuesForElementName(const string16& name, | 356 bool AutofillTable::GetFormValuesForElementName(const string16& name, |
| 357 const string16& prefix, | 357 const string16& prefix, |
| 358 std::vector<string16>* values, | 358 std::vector<string16>* values, |
| 359 int limit) { | 359 int limit) { |
| 360 DCHECK(values); | 360 DCHECK(values); |
| 361 sql::Statement s; | 361 sql::Statement s; |
| (...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 559 NOTREACHED(); | 559 NOTREACHED(); |
| 560 return false; | 560 return false; |
| 561 } else { | 561 } else { |
| 562 if (!SetCountOfFormElement(pair_id, count + delta)) | 562 if (!SetCountOfFormElement(pair_id, count + delta)) |
| 563 return false; | 563 return false; |
| 564 } | 564 } |
| 565 return true; | 565 return true; |
| 566 } | 566 } |
| 567 | 567 |
| 568 bool AutofillTable::GetIDAndCountOfFormElement( | 568 bool AutofillTable::GetIDAndCountOfFormElement( |
| 569 const FormField& element, | 569 const FormFieldData& element, |
| 570 int64* pair_id, | 570 int64* pair_id, |
| 571 int* count) { | 571 int* count) { |
| 572 DCHECK(pair_id); | 572 DCHECK(pair_id); |
| 573 DCHECK(count); | 573 DCHECK(count); |
| 574 | 574 |
| 575 sql::Statement s(db_->GetUniqueStatement( | 575 sql::Statement s(db_->GetUniqueStatement( |
| 576 "SELECT pair_id, count FROM autofill " | 576 "SELECT pair_id, count FROM autofill " |
| 577 "WHERE name = ? AND value = ?")); | 577 "WHERE name = ? AND value = ?")); |
| 578 s.BindString16(0, element.name); | 578 s.BindString16(0, element.name); |
| 579 s.BindString16(1, element.value); | 579 s.BindString16(1, element.value); |
| (...skipping 27 matching lines...) Expand all Loading... |
| 607 | 607 |
| 608 bool AutofillTable::SetCountOfFormElement(int64 pair_id, int count) { | 608 bool AutofillTable::SetCountOfFormElement(int64 pair_id, int count) { |
| 609 sql::Statement s(db_->GetUniqueStatement( | 609 sql::Statement s(db_->GetUniqueStatement( |
| 610 "UPDATE autofill SET count = ? WHERE pair_id = ?")); | 610 "UPDATE autofill SET count = ? WHERE pair_id = ?")); |
| 611 s.BindInt(0, count); | 611 s.BindInt(0, count); |
| 612 s.BindInt64(1, pair_id); | 612 s.BindInt64(1, pair_id); |
| 613 | 613 |
| 614 return s.Run(); | 614 return s.Run(); |
| 615 } | 615 } |
| 616 | 616 |
| 617 bool AutofillTable::InsertFormElement(const FormField& element, | 617 bool AutofillTable::InsertFormElement(const FormFieldData& element, |
| 618 int64* pair_id) { | 618 int64* pair_id) { |
| 619 DCHECK(pair_id); | 619 DCHECK(pair_id); |
| 620 sql::Statement s(db_->GetUniqueStatement( | 620 sql::Statement s(db_->GetUniqueStatement( |
| 621 "INSERT INTO autofill (name, value, value_lower) VALUES (?,?,?)")); | 621 "INSERT INTO autofill (name, value, value_lower) VALUES (?,?,?)")); |
| 622 s.BindString16(0, element.name); | 622 s.BindString16(0, element.name); |
| 623 s.BindString16(1, element.value); | 623 s.BindString16(1, element.value); |
| 624 s.BindString16(2, base::i18n::ToLower(element.value)); | 624 s.BindString16(2, base::i18n::ToLower(element.value)); |
| 625 | 625 |
| 626 if (!s.Run()) | 626 if (!s.Run()) |
| 627 return false; | 627 return false; |
| (...skipping 20 matching lines...) Expand all Loading... |
| 648 "DELETE FROM autofill_dates WHERE pair_id = ? and date_created IN " | 648 "DELETE FROM autofill_dates WHERE pair_id = ? and date_created IN " |
| 649 "(SELECT date_created FROM autofill_dates WHERE pair_id = ? " | 649 "(SELECT date_created FROM autofill_dates WHERE pair_id = ? " |
| 650 "ORDER BY date_created DESC LIMIT 1)")); | 650 "ORDER BY date_created DESC LIMIT 1)")); |
| 651 s.BindInt64(0, pair_id); | 651 s.BindInt64(0, pair_id); |
| 652 s.BindInt64(1, pair_id); | 652 s.BindInt64(1, pair_id); |
| 653 | 653 |
| 654 return s.Run(); | 654 return s.Run(); |
| 655 } | 655 } |
| 656 | 656 |
| 657 bool AutofillTable::AddFormFieldValuesTime( | 657 bool AutofillTable::AddFormFieldValuesTime( |
| 658 const std::vector<FormField>& elements, | 658 const std::vector<FormFieldData>& elements, |
| 659 std::vector<AutofillChange>* changes, | 659 std::vector<AutofillChange>* changes, |
| 660 Time time) { | 660 Time time) { |
| 661 // Only add one new entry for each unique element name. Use |seen_names| to | 661 // Only add one new entry for each unique element name. Use |seen_names| to |
| 662 // track this. Add up to |kMaximumUniqueNames| unique entries per form. | 662 // track this. Add up to |kMaximumUniqueNames| unique entries per form. |
| 663 const size_t kMaximumUniqueNames = 256; | 663 const size_t kMaximumUniqueNames = 256; |
| 664 std::set<string16> seen_names; | 664 std::set<string16> seen_names; |
| 665 bool result = true; | 665 bool result = true; |
| 666 for (std::vector<FormField>::const_iterator itr = elements.begin(); | 666 for (std::vector<FormFieldData>::const_iterator itr = elements.begin(); |
| 667 itr != elements.end(); ++itr) { | 667 itr != elements.end(); ++itr) { |
| 668 if (seen_names.size() >= kMaximumUniqueNames) | 668 if (seen_names.size() >= kMaximumUniqueNames) |
| 669 break; | 669 break; |
| 670 if (seen_names.find(itr->name) != seen_names.end()) | 670 if (seen_names.find(itr->name) != seen_names.end()) |
| 671 continue; | 671 continue; |
| 672 result = result && AddFormFieldValueTime(*itr, changes, time); | 672 result = result && AddFormFieldValueTime(*itr, changes, time); |
| 673 seen_names.insert(itr->name); | 673 seen_names.insert(itr->name); |
| 674 } | 674 } |
| 675 return result; | 675 return result; |
| 676 } | 676 } |
| (...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 811 | 811 |
| 812 int64 pair_id = db_->GetLastInsertRowId(); | 812 int64 pair_id = db_->GetLastInsertRowId(); |
| 813 for (size_t i = 0; i < entry.timestamps().size(); i++) { | 813 for (size_t i = 0; i < entry.timestamps().size(); i++) { |
| 814 if (!InsertPairIDAndDate(pair_id, entry.timestamps()[i])) | 814 if (!InsertPairIDAndDate(pair_id, entry.timestamps()[i])) |
| 815 return false; | 815 return false; |
| 816 } | 816 } |
| 817 | 817 |
| 818 return true; | 818 return true; |
| 819 } | 819 } |
| 820 | 820 |
| 821 bool AutofillTable::AddFormFieldValueTime(const FormField& element, | 821 bool AutofillTable::AddFormFieldValueTime(const FormFieldData& element, |
| 822 std::vector<AutofillChange>* changes, | 822 std::vector<AutofillChange>* changes, |
| 823 Time time) { | 823 Time time) { |
| 824 int count = 0; | 824 int count = 0; |
| 825 int64 pair_id; | 825 int64 pair_id; |
| 826 | 826 |
| 827 if (!GetIDAndCountOfFormElement(element, &pair_id, &count)) | 827 if (!GetIDAndCountOfFormElement(element, &pair_id, &count)) |
| 828 return false; | 828 return false; |
| 829 | 829 |
| 830 if (count == 0 && !InsertFormElement(element, &pair_id)) | 830 if (count == 0 && !InsertFormElement(element, &pair_id)) |
| 831 return false; | 831 return false; |
| (...skipping 1146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1978 "WHERE guid=?")); | 1978 "WHERE guid=?")); |
| 1979 s_date.BindInt64(0, date_item->second); | 1979 s_date.BindInt64(0, date_item->second); |
| 1980 s_date.BindString(1, iter->guid()); | 1980 s_date.BindString(1, iter->guid()); |
| 1981 | 1981 |
| 1982 if (!s_date.Run()) | 1982 if (!s_date.Run()) |
| 1983 return false; | 1983 return false; |
| 1984 } | 1984 } |
| 1985 | 1985 |
| 1986 return true; | 1986 return true; |
| 1987 } | 1987 } |
| OLD | NEW |