OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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/web_database.h" |
| 6 |
5 #include <algorithm> | 7 #include <algorithm> |
6 #include <limits> | 8 #include <limits> |
7 #include <vector> | 9 #include <vector> |
8 | 10 |
9 #include "chrome/browser/webdata/web_database.h" | |
10 | |
11 #include "base/gfx/png_decoder.h" | 11 #include "base/gfx/png_decoder.h" |
12 #include "base/gfx/png_encoder.h" | 12 #include "base/gfx/png_encoder.h" |
13 #include "base/string_util.h" | 13 #include "base/string_util.h" |
14 #include "base/time.h" | 14 #include "base/time.h" |
15 #include "base/values.h" | 15 #include "base/values.h" |
16 #include "chrome/browser/history/history_database.h" | 16 #include "chrome/browser/history/history_database.h" |
17 #include "chrome/browser/ie7_password.h" | |
18 #include "chrome/browser/template_url.h" | 17 #include "chrome/browser/template_url.h" |
19 #include "chrome/browser/encryptor.h" | 18 #include "chrome/browser/encryptor.h" |
20 #include "chrome/common/l10n_util.h" | 19 #include "chrome/common/l10n_util.h" |
21 #include "chrome/common/scoped_vector.h" | 20 #include "chrome/common/scoped_vector.h" |
22 #include "webkit/glue/password_form.h" | 21 #include "webkit/glue/password_form.h" |
23 | 22 |
24 //////////////////////////////////////////////////////////////////////////////// | 23 //////////////////////////////////////////////////////////////////////////////// |
25 // | 24 // |
26 // Schema | 25 // Schema |
27 // | 26 // |
(...skipping 507 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
535 | 534 |
536 s.column_string16(1, &tmp); | 535 s.column_string16(1, &tmp); |
537 DCHECK(!tmp.empty()); | 536 DCHECK(!tmp.empty()); |
538 template_url->set_short_name(tmp); | 537 template_url->set_short_name(tmp); |
539 | 538 |
540 s.column_string16(2, &tmp); | 539 s.column_string16(2, &tmp); |
541 template_url->set_keyword(tmp); | 540 template_url->set_keyword(tmp); |
542 | 541 |
543 s.column_string16(3, &tmp); | 542 s.column_string16(3, &tmp); |
544 if (!tmp.empty()) | 543 if (!tmp.empty()) |
545 template_url->SetFavIconURL(GURL(tmp)); | 544 template_url->SetFavIconURL(GURL(WideToUTF8(tmp))); |
546 | 545 |
547 s.column_string16(4, &tmp); | 546 s.column_string16(4, &tmp); |
548 template_url->SetURL(tmp, 0, 0); | 547 template_url->SetURL(tmp, 0, 0); |
549 | 548 |
550 template_url->set_safe_for_autoreplace(s.column_int(5) == 1); | 549 template_url->set_safe_for_autoreplace(s.column_int(5) == 1); |
551 | 550 |
552 s.column_string16(6, &tmp); | 551 s.column_string16(6, &tmp); |
553 if (!tmp.empty()) | 552 if (!tmp.empty()) |
554 template_url->set_originating_url(GURL(tmp)); | 553 template_url->set_originating_url(GURL(WideToUTF8(tmp))); |
555 | 554 |
556 template_url->set_date_created(Time::FromTimeT(s.column_int64(7))); | 555 template_url->set_date_created(Time::FromTimeT(s.column_int64(7))); |
557 | 556 |
558 template_url->set_usage_count(s.column_int(8)); | 557 template_url->set_usage_count(s.column_int(8)); |
559 | 558 |
560 std::vector<std::string> encodings; | 559 std::vector<std::string> encodings; |
561 SplitString(s.column_string(9), ';', &encodings); | 560 SplitString(s.column_string(9), ';', &encodings); |
562 template_url->set_input_encodings(encodings); | 561 template_url->set_input_encodings(encodings); |
563 | 562 |
564 template_url->set_show_in_default_list(s.column_int(10) == 1); | 563 template_url->set_show_in_default_list(s.column_int(10) == 1); |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
607 bool WebDatabase::SetBuitinKeywordVersion(int version) { | 606 bool WebDatabase::SetBuitinKeywordVersion(int version) { |
608 return meta_table_.SetValue(kBuiltinKeywordVersion, version); | 607 return meta_table_.SetValue(kBuiltinKeywordVersion, version); |
609 } | 608 } |
610 | 609 |
611 int WebDatabase::GetBuitinKeywordVersion() { | 610 int WebDatabase::GetBuitinKeywordVersion() { |
612 int version = 0; | 611 int version = 0; |
613 meta_table_.GetValue(kBuiltinKeywordVersion, &version); | 612 meta_table_.GetValue(kBuiltinKeywordVersion, &version); |
614 return version; | 613 return version; |
615 } | 614 } |
616 | 615 |
617 // Return a new GURL like url, but without any "#foo" bit on the end. | |
618 static GURL GURLWithoutRef(const GURL& url) { | |
619 url_canon::Replacements<char> replacements; | |
620 replacements.ClearRef(); | |
621 return url.ReplaceComponents(replacements); | |
622 } | |
623 | |
624 // Convert a list of GUIDs from the in-memory form to the form we keep in | |
625 // the database (tab-separated string). | |
626 static std::string SerializeGUIDs(const std::vector<std::string>& guids) { | |
627 std::string result; | |
628 for (size_t i = 0; i < guids.size(); ++i) { | |
629 if (!result.empty()) | |
630 result.push_back('\t'); | |
631 const std::string& guid = guids[i]; | |
632 for (size_t j = 0; j < guid.size(); ++j) { | |
633 char ch = guid[j]; | |
634 // If we have any embedded tabs in the GUID (a pathological case), | |
635 // we normalize them to spaces. | |
636 if (ch == '\t') | |
637 ch = ' '; | |
638 result.push_back(ch); | |
639 } | |
640 } | |
641 return result; | |
642 } | |
643 | |
644 // The partner of SerializeGUIDs. Converts a serialized GUIDs string | |
645 // back to a vector. | |
646 static void DeserializeGUIDs(const std::string& str, | |
647 std::vector<std::string>* guids) { | |
648 SplitString(str, '\t', guids); | |
649 } | |
650 | |
651 bool WebDatabase::AddLogin(const PasswordForm& form) { | 616 bool WebDatabase::AddLogin(const PasswordForm& form) { |
652 SQLStatement s; | 617 SQLStatement s; |
653 std::string encrypted_password; | 618 std::string encrypted_password; |
654 if (s.prepare(db_, | 619 if (s.prepare(db_, |
655 "INSERT OR REPLACE INTO logins " | 620 "INSERT OR REPLACE INTO logins " |
656 "(origin_url, action_url, username_element, username_value, " | 621 "(origin_url, action_url, username_element, username_value, " |
657 " password_element, password_value, submit_element, " | 622 " password_element, password_value, submit_element, " |
658 " signon_realm, ssl_valid, preferred, date_created, " | 623 " signon_realm, ssl_valid, preferred, date_created, " |
659 " blacklisted_by_user, scheme) " | 624 " blacklisted_by_user, scheme) " |
660 "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)") != SQLITE_OK)
{ | 625 "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)") != SQLITE_OK)
{ |
(...skipping 16 matching lines...) Expand all Loading... |
677 s.bind_int64(10, form.date_created.ToTimeT()); | 642 s.bind_int64(10, form.date_created.ToTimeT()); |
678 s.bind_int(11, form.blacklisted_by_user); | 643 s.bind_int(11, form.blacklisted_by_user); |
679 s.bind_int(12, form.scheme); | 644 s.bind_int(12, form.scheme); |
680 if (s.step() != SQLITE_DONE) { | 645 if (s.step() != SQLITE_DONE) { |
681 NOTREACHED(); | 646 NOTREACHED(); |
682 return false; | 647 return false; |
683 } | 648 } |
684 return true; | 649 return true; |
685 } | 650 } |
686 | 651 |
687 bool WebDatabase::AddIE7Login(const IE7PasswordInfo& info) { | |
688 SQLStatement s; | |
689 if (s.prepare(db_, | |
690 "INSERT OR REPLACE INTO ie7_logins " | |
691 "(url_hash, password_value, date_created) " | |
692 "VALUES (?, ?, ?)") != SQLITE_OK) { | |
693 NOTREACHED() << "Statement prepare failed"; | |
694 return false; | |
695 } | |
696 | |
697 s.bind_wstring(0, info.url_hash); | |
698 s.bind_blob(1, &info.encrypted_data.front(), | |
699 static_cast<int>(info.encrypted_data.size())); | |
700 s.bind_int64(2, info.date_created.ToTimeT()); | |
701 if (s.step() != SQLITE_DONE) { | |
702 NOTREACHED(); | |
703 return false; | |
704 } | |
705 return true; | |
706 } | |
707 | |
708 bool WebDatabase::UpdateLogin(const PasswordForm& form) { | 652 bool WebDatabase::UpdateLogin(const PasswordForm& form) { |
709 SQLStatement s; | 653 SQLStatement s; |
710 std::string encrypted_password; | 654 std::string encrypted_password; |
711 if (s.prepare(db_, "UPDATE logins SET " | 655 if (s.prepare(db_, "UPDATE logins SET " |
712 "action_url = ?, " | 656 "action_url = ?, " |
713 "password_value = ?, " | 657 "password_value = ?, " |
714 "ssl_valid = ?, " | 658 "ssl_valid = ?, " |
715 "preferred = ? " | 659 "preferred = ? " |
716 "WHERE origin_url = ? AND " | 660 "WHERE origin_url = ? AND " |
717 "username_element = ? AND " | 661 "username_element = ? AND " |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
762 s.bind_wstring(4, form.submit_element); | 706 s.bind_wstring(4, form.submit_element); |
763 s.bind_string(5, form.signon_realm); | 707 s.bind_string(5, form.signon_realm); |
764 | 708 |
765 if (s.step() != SQLITE_DONE) { | 709 if (s.step() != SQLITE_DONE) { |
766 NOTREACHED(); | 710 NOTREACHED(); |
767 return false; | 711 return false; |
768 } | 712 } |
769 return true; | 713 return true; |
770 } | 714 } |
771 | 715 |
772 bool WebDatabase::RemoveIE7Login(const IE7PasswordInfo& info) { | |
773 SQLStatement s; | |
774 // Remove a login by UNIQUE-constrained fields. | |
775 if (s.prepare(db_, | |
776 "DELETE FROM ie7_logins WHERE " | |
777 "url_hash = ?") != SQLITE_OK) { | |
778 NOTREACHED() << "Statement prepare failed"; | |
779 return false; | |
780 } | |
781 s.bind_wstring(0, info.url_hash); | |
782 | |
783 if (s.step() != SQLITE_DONE) { | |
784 NOTREACHED(); | |
785 return false; | |
786 } | |
787 return true; | |
788 } | |
789 | |
790 bool WebDatabase::RemoveLoginsCreatedBetween(const Time delete_begin, | 716 bool WebDatabase::RemoveLoginsCreatedBetween(const Time delete_begin, |
791 const Time delete_end) { | 717 const Time delete_end) { |
792 SQLStatement s1; | 718 SQLStatement s1; |
793 if (s1.prepare(db_, | 719 if (s1.prepare(db_, |
794 "DELETE FROM logins WHERE " | 720 "DELETE FROM logins WHERE " |
795 "date_created >= ? AND date_created < ?") != SQLITE_OK) { | 721 "date_created >= ? AND date_created < ?") != SQLITE_OK) { |
796 NOTREACHED() << "Statement 1 prepare failed"; | 722 NOTREACHED() << "Statement 1 prepare failed"; |
797 return false; | 723 return false; |
798 } | 724 } |
799 s1.bind_int64(0, delete_begin.ToTimeT()); | 725 s1.bind_int64(0, delete_begin.ToTimeT()); |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
864 int result; | 790 int result; |
865 while ((result = s.step()) == SQLITE_ROW) { | 791 while ((result = s.step()) == SQLITE_ROW) { |
866 PasswordForm* new_form = new PasswordForm(); | 792 PasswordForm* new_form = new PasswordForm(); |
867 InitPasswordFormFromStatement(new_form, &s); | 793 InitPasswordFormFromStatement(new_form, &s); |
868 | 794 |
869 forms->push_back(new_form); | 795 forms->push_back(new_form); |
870 } | 796 } |
871 return result == SQLITE_DONE; | 797 return result == SQLITE_DONE; |
872 } | 798 } |
873 | 799 |
874 bool WebDatabase::GetIE7Login(const IE7PasswordInfo& info, | |
875 IE7PasswordInfo* result) { | |
876 DCHECK(result); | |
877 SQLStatement s; | |
878 if (s.prepare(db_, | |
879 "SELECT password_value, date_created FROM ie7_logins " | |
880 "WHERE url_hash == ? ") != SQLITE_OK) { | |
881 NOTREACHED() << "Statement prepare failed"; | |
882 return false; | |
883 } | |
884 | |
885 s.bind_wstring(0, info.url_hash); | |
886 | |
887 int64 query_result = s.step(); | |
888 if (query_result == SQLITE_ROW) { | |
889 s.column_blob_as_vector(0, &result->encrypted_data); | |
890 result->date_created = Time::FromTimeT(s.column_int64(1)); | |
891 result->url_hash = info.url_hash; | |
892 s.step(); | |
893 } | |
894 return query_result == SQLITE_DONE; | |
895 } | |
896 | |
897 bool WebDatabase::GetAllLogins(std::vector<PasswordForm*>* forms, | 800 bool WebDatabase::GetAllLogins(std::vector<PasswordForm*>* forms, |
898 bool include_blacklisted) { | 801 bool include_blacklisted) { |
899 DCHECK(forms); | 802 DCHECK(forms); |
900 SQLStatement s; | 803 SQLStatement s; |
901 std::string stmt = "SELECT origin_url, action_url, " | 804 std::string stmt = "SELECT origin_url, action_url, " |
902 "username_element, username_value, " | 805 "username_element, username_value, " |
903 "password_element, password_value, " | 806 "password_element, password_value, " |
904 "submit_element, signon_realm, ssl_valid, preferred, " | 807 "submit_element, signon_realm, ssl_valid, preferred, " |
905 "date_created, blacklisted_by_user, scheme FROM logins "; | 808 "date_created, blacklisted_by_user, scheme FROM logins "; |
906 if (!include_blacklisted) | 809 if (!include_blacklisted) |
(...skipping 324 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1231 | 1134 |
1232 // Add successive versions here. Each should set the version number and | 1135 // Add successive versions here. Each should set the version number and |
1233 // compatible version number as appropriate, then fall through to the next | 1136 // compatible version number as appropriate, then fall through to the next |
1234 // case. | 1137 // case. |
1235 | 1138 |
1236 case kCurrentVersionNumber: | 1139 case kCurrentVersionNumber: |
1237 // No migration needed. | 1140 // No migration needed. |
1238 return; | 1141 return; |
1239 } | 1142 } |
1240 } | 1143 } |
OLD | NEW |