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

Side by Side Diff: chrome/browser/webdata/web_database.cc

Issue 506047: Return a list of changed from WebDatabase::RemoveFormElementsAddedBetween() (Closed)
Patch Set: Remove unneeded include. Created 11 years 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
« no previous file with comments | « chrome/browser/webdata/web_database.h ('k') | chrome/browser/webdata/web_database_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2009 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" 5 #include "chrome/browser/webdata/web_database.h"
6 6
7 #include <limits> 7 #include <limits>
8 8
9 #include "app/gfx/codec/png_codec.h" 9 #include "app/gfx/codec/png_codec.h"
10 #include "app/l10n_util.h" 10 #include "app/l10n_util.h"
11 #include "app/sql/statement.h" 11 #include "app/sql/statement.h"
12 #include "app/sql/transaction.h" 12 #include "app/sql/transaction.h"
13 #include "base/tuple.h"
13 #include "base/utf_string_conversions.h" 14 #include "base/utf_string_conversions.h"
14 #include "chrome/browser/diagnostics/sqlite_diagnostics.h" 15 #include "chrome/browser/diagnostics/sqlite_diagnostics.h"
15 #include "chrome/browser/history/history_database.h" 16 #include "chrome/browser/history/history_database.h"
17 #include "chrome/browser/webdata/autofill_change.h"
16 #include "webkit/glue/password_form.h" 18 #include "webkit/glue/password_form.h"
17 19
18 // Encryptor is the *wrong* way of doing things; we need to turn it into a 20 // Encryptor is the *wrong* way of doing things; we need to turn it into a
19 // bottleneck to use the platform methods (e.g. Keychain on the Mac, Gnome 21 // bottleneck to use the platform methods (e.g. Keychain on the Mac, Gnome
20 // Keyring / KWallet on Linux). That's going to take a massive change in its 22 // Keyring / KWallet on Linux). That's going to take a massive change in its
21 // API... see: 23 // API... see:
22 // http://code.google.com/p/chromium/issues/detail?id=25404 (Linux) 24 // http://code.google.com/p/chromium/issues/detail?id=25404 (Linux)
23 // but the (possibly-now-unused) Mac encryptor stub code needs to die too. 25 // but the (possibly-now-unused) Mac encryptor stub code needs to die too.
24 #include "chrome/browser/password_manager/encryptor.h" 26 #include "chrome/browser/password_manager/encryptor.h"
25 27
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
109 const std::vector<std::string>& strings) { 111 const std::vector<std::string>& strings) {
110 if (strings.empty()) 112 if (strings.empty())
111 return std::string(); 113 return std::string();
112 std::vector<std::string>::const_iterator i(strings.begin()); 114 std::vector<std::string>::const_iterator i(strings.begin());
113 std::string result(*i); 115 std::string result(*i);
114 while (++i != strings.end()) 116 while (++i != strings.end())
115 result += separator + *i; 117 result += separator + *i;
116 return result; 118 return result;
117 } 119 }
118 120
121 namespace {
122 typedef std::vector<Tuple3<int64, string16, string16> > AutofillElementList;
123 }
124
119 WebDatabase::WebDatabase() { 125 WebDatabase::WebDatabase() {
120 } 126 }
121 127
122 WebDatabase::~WebDatabase() { 128 WebDatabase::~WebDatabase() {
123 } 129 }
124 130
125 void WebDatabase::BeginTransaction() { 131 void WebDatabase::BeginTransaction() {
126 db_.BeginTransaction(); 132 db_.BeginTransaction();
127 } 133 }
128 134
(...skipping 662 matching lines...) Expand 10 before | Expand all | Expand 10 after
791 797
792 while (s.Step()) { 798 while (s.Step()) {
793 PasswordForm* new_form = new PasswordForm(); 799 PasswordForm* new_form = new PasswordForm();
794 InitPasswordFormFromStatement(new_form, &s); 800 InitPasswordFormFromStatement(new_form, &s);
795 801
796 forms->push_back(new_form); 802 forms->push_back(new_form);
797 } 803 }
798 return s.Succeeded(); 804 return s.Succeeded();
799 } 805 }
800 806
801 bool WebDatabase::AddFormFieldValues( 807 bool WebDatabase::AddFormFieldValues(const std::vector<FormField>& elements) {
802 const std::vector<FormField>& elements) { 808 return AddFormFieldValuesTime(elements, Time::Now());
809 }
810
811 bool WebDatabase::AddFormFieldValuesTime(const std::vector<FormField>& elements,
812 base::Time time) {
803 bool result = true; 813 bool result = true;
804 for (std::vector<FormField>::const_iterator 814 for (std::vector<FormField>::const_iterator
805 itr = elements.begin(); 815 itr = elements.begin();
806 itr != elements.end(); 816 itr != elements.end();
807 itr++) { 817 itr++) {
808 result = result && AddFormFieldValue(*itr); 818 result = result && AddFormFieldValueTime(*itr, time);
809 } 819 }
810 return result; 820 return result;
811 } 821 }
812 822
813 bool WebDatabase::ClearAutofillEmptyValueElements() { 823 bool WebDatabase::ClearAutofillEmptyValueElements() {
814 sql::Statement s(db_.GetUniqueStatement( 824 sql::Statement s(db_.GetUniqueStatement(
815 "SELECT pair_id FROM autofill WHERE TRIM(value)= \"\"")); 825 "SELECT pair_id FROM autofill WHERE TRIM(value)= \"\""));
816 if (!s) { 826 if (!s) {
817 NOTREACHED() << "Statement prepare failed"; 827 NOTREACHED() << "Statement prepare failed";
818 return false; 828 return false;
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after
929 s.BindInt64(1, pair_id); 939 s.BindInt64(1, pair_id);
930 if (!s.Run()) { 940 if (!s.Run()) {
931 NOTREACHED(); 941 NOTREACHED();
932 return false; 942 return false;
933 } 943 }
934 944
935 return true; 945 return true;
936 } 946 }
937 947
938 bool WebDatabase::AddFormFieldValue(const FormField& element) { 948 bool WebDatabase::AddFormFieldValue(const FormField& element) {
949 return AddFormFieldValueTime(element, base::Time::Now());
950 }
951
952 bool WebDatabase::AddFormFieldValueTime(const FormField& element,
953 base::Time time) {
939 int count = 0; 954 int count = 0;
940 int64 pair_id; 955 int64 pair_id;
941 956
942 if (!GetIDAndCountOfFormElement(element, &pair_id, &count)) 957 if (!GetIDAndCountOfFormElement(element, &pair_id, &count))
943 return false; 958 return false;
944 959
945 if (count == 0 && !InsertFormElement(element, &pair_id)) 960 if (count == 0 && !InsertFormElement(element, &pair_id))
946 return false; 961 return false;
947 962
948 return SetCountOfFormElement(pair_id, count + 1) && 963 return SetCountOfFormElement(pair_id, count + 1) &&
949 InsertPairIDAndDate(pair_id, Time::Now()); 964 InsertPairIDAndDate(pair_id, time);
950 } 965 }
951 966
952 bool WebDatabase::GetFormValuesForElementName(const string16& name, 967 bool WebDatabase::GetFormValuesForElementName(const string16& name,
953 const string16& prefix, 968 const string16& prefix,
954 std::vector<string16>* values, 969 std::vector<string16>* values,
955 int limit) { 970 int limit) {
956 DCHECK(values); 971 DCHECK(values);
957 sql::Statement s; 972 sql::Statement s;
958 973
959 if (prefix.empty()) { 974 if (prefix.empty()) {
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
991 s.BindString(2, UTF16ToUTF8(next_prefix)); 1006 s.BindString(2, UTF16ToUTF8(next_prefix));
992 s.BindInt(3, limit); 1007 s.BindInt(3, limit);
993 } 1008 }
994 1009
995 values->clear(); 1010 values->clear();
996 while (s.Step()) 1011 while (s.Step())
997 values->push_back(UTF8ToUTF16(s.ColumnString(0))); 1012 values->push_back(UTF8ToUTF16(s.ColumnString(0)));
998 return s.Succeeded(); 1013 return s.Succeeded();
999 } 1014 }
1000 1015
1001 bool WebDatabase::RemoveFormElementsAddedBetween(base::Time delete_begin, 1016 bool WebDatabase::RemoveFormElementsAddedBetween(
1002 base::Time delete_end) { 1017 base::Time delete_begin,
1018 base::Time delete_end,
1019 std::vector<AutofillChange>* changes) {
1020 DCHECK(changes);
1021 // Query for the pair_id, name, and value of all form elements that
1022 // were used between the given times.
1003 sql::Statement s(db_.GetUniqueStatement( 1023 sql::Statement s(db_.GetUniqueStatement(
1004 "SELECT DISTINCT pair_id FROM autofill_dates " 1024 "SELECT DISTINCT a.pair_id, a.name, a.value "
1005 "WHERE date_created >= ? AND date_created < ?")); 1025 "FROM autofill_dates ad JOIN autofill a ON ad.pair_id = a.pair_id "
1026 "WHERE ad.date_created >= ? AND ad.date_created < ?"));
1006 if (!s) { 1027 if (!s) {
1007 NOTREACHED() << "Statement 1 prepare failed"; 1028 NOTREACHED() << "Statement 1 prepare failed";
1008 return false; 1029 return false;
1009 } 1030 }
1010 s.BindInt64(0, delete_begin.ToTimeT()); 1031 s.BindInt64(0, delete_begin.ToTimeT());
1011 s.BindInt64(1, 1032 s.BindInt64(1,
1012 delete_end.is_null() ? 1033 delete_end.is_null() ?
1013 std::numeric_limits<int64>::max() : 1034 std::numeric_limits<int64>::max() :
1014 delete_end.ToTimeT()); 1035 delete_end.ToTimeT());
1015 1036
1016 std::vector<int64> pair_ids; 1037 AutofillElementList elements;
1017 while (s.Step()) 1038 while (s.Step())
1018 pair_ids.push_back(s.ColumnInt64(0)); 1039 elements.push_back(MakeTuple(s.ColumnInt64(0),
1040 UTF8ToUTF16(s.ColumnString(1)),
1041 UTF8ToUTF16(s.ColumnString(2))));
1019 1042
1020 if (!s.Succeeded()) { 1043 if (!s.Succeeded()) {
1021 NOTREACHED(); 1044 NOTREACHED();
1022 return false; 1045 return false;
1023 } 1046 }
1024 1047
1025 for (std::vector<int64>::iterator itr = pair_ids.begin(); 1048 for (AutofillElementList::iterator itr = elements.begin();
1026 itr != pair_ids.end(); 1049 itr != elements.end();
1027 itr++) { 1050 itr++) {
1028 int how_many = 0; 1051 int how_many = 0;
1029 if (!RemoveFormElementForTimeRange(*itr, delete_begin, delete_end, 1052 if (!RemoveFormElementForTimeRange(itr->a, delete_begin, delete_end,
1030 &how_many)) { 1053 &how_many)) {
1031 return false; 1054 return false;
1032 } 1055 }
1033 if (!AddToCountOfFormElement(*itr, -how_many)) 1056 bool was_removed = false;
1057 if (!AddToCountOfFormElement(itr->a, -how_many, &was_removed))
1034 return false; 1058 return false;
1059 AutofillChange::Type change_type =
1060 was_removed ? AutofillChange::REMOVE : AutofillChange::UPDATE;
1061 changes->push_back(AutofillChange(change_type,
1062 AutofillKey(itr->b, itr->c)));
1035 } 1063 }
1036 1064
1037 return true; 1065 return true;
1038 } 1066 }
1039 1067
1040 bool WebDatabase::RemoveFormElementForTimeRange(int64 pair_id, 1068 bool WebDatabase::RemoveFormElementForTimeRange(int64 pair_id,
1041 const Time delete_begin, 1069 const Time delete_begin,
1042 const Time delete_end, 1070 const Time delete_end,
1043 int* how_many) { 1071 int* how_many) {
1044 sql::Statement s(db_.GetUniqueStatement( 1072 sql::Statement s(db_.GetUniqueStatement(
(...skipping 25 matching lines...) Expand all
1070 return false; 1098 return false;
1071 } 1099 }
1072 s.BindString(0, UTF16ToUTF8(name)); 1100 s.BindString(0, UTF16ToUTF8(name));
1073 s.BindString(1, UTF16ToUTF8(value)); 1101 s.BindString(1, UTF16ToUTF8(value));
1074 1102
1075 if (s.Step()) 1103 if (s.Step())
1076 return RemoveFormElementForID(s.ColumnInt64(0)); 1104 return RemoveFormElementForID(s.ColumnInt64(0));
1077 return false; 1105 return false;
1078 } 1106 }
1079 1107
1080 bool WebDatabase::AddToCountOfFormElement(int64 pair_id, int delta) { 1108 bool WebDatabase::AddToCountOfFormElement(int64 pair_id,
1109 int delta,
1110 bool* was_removed) {
1111 DCHECK(was_removed);
1081 int count = 0; 1112 int count = 0;
1113 *was_removed = false;
1082 1114
1083 if (!GetCountOfFormElement(pair_id, &count)) 1115 if (!GetCountOfFormElement(pair_id, &count))
1084 return false; 1116 return false;
1085 1117
1086 if (count + delta == 0) { 1118 if (count + delta == 0) {
1087 if (!RemoveFormElementForID(pair_id)) 1119 if (!RemoveFormElementForID(pair_id))
1088 return false; 1120 return false;
1121 *was_removed = true;
1089 } else { 1122 } else {
1090 if (!SetCountOfFormElement(pair_id, count + delta)) 1123 if (!SetCountOfFormElement(pair_id, count + delta))
1091 return false; 1124 return false;
1092 } 1125 }
1093 return true; 1126 return true;
1094 } 1127 }
1095 1128
1096 bool WebDatabase::RemoveFormElementForID(int64 pair_id) { 1129 bool WebDatabase::RemoveFormElementForID(int64 pair_id) {
1097 sql::Statement s(db_.GetUniqueStatement( 1130 sql::Statement s(db_.GetUniqueStatement(
1098 "DELETE FROM autofill WHERE pair_id = ?")); 1131 "DELETE FROM autofill WHERE pair_id = ?"));
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
1146 1179
1147 // Add successive versions here. Each should set the version number and 1180 // Add successive versions here. Each should set the version number and
1148 // compatible version number as appropriate, then fall through to the next 1181 // compatible version number as appropriate, then fall through to the next
1149 // case. 1182 // case.
1150 1183
1151 case kCurrentVersionNumber: 1184 case kCurrentVersionNumber:
1152 // No migration needed. 1185 // No migration needed.
1153 return; 1186 return;
1154 } 1187 }
1155 } 1188 }
OLDNEW
« no previous file with comments | « chrome/browser/webdata/web_database.h ('k') | chrome/browser/webdata/web_database_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698