| 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" | 5 #include "chrome/browser/webdata/web_database.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <limits> | 8 #include <limits> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 98 // | 98 // |
| 99 // web_apps | 99 // web_apps |
| 100 // url URL of the web app. | 100 // url URL of the web app. |
| 101 // has_all_images Do we have all the images? | 101 // has_all_images Do we have all the images? |
| 102 // | 102 // |
| 103 //////////////////////////////////////////////////////////////////////////////// | 103 //////////////////////////////////////////////////////////////////////////////// |
| 104 | 104 |
| 105 using base::Time; | 105 using base::Time; |
| 106 | 106 |
| 107 // Current version number. | 107 // Current version number. |
| 108 static const int kCurrentVersionNumber = 21; | 108 static const int kCurrentVersionNumber = 22; |
| 109 static const int kCompatibleVersionNumber = 21; | 109 static const int kCompatibleVersionNumber = 21; |
| 110 | 110 |
| 111 // Keys used in the meta table. | 111 // Keys used in the meta table. |
| 112 static const char* kDefaultSearchProviderKey = "Default Search Provider ID"; | 112 static const char* kDefaultSearchProviderKey = "Default Search Provider ID"; |
| 113 static const char* kBuiltinKeywordVersion = "Builtin Keyword Version"; | 113 static const char* kBuiltinKeywordVersion = "Builtin Keyword Version"; |
| 114 | 114 |
| 115 std::string JoinStrings(const std::string& separator, | 115 std::string JoinStrings(const std::string& separator, |
| 116 const std::vector<std::string>& strings) { | 116 const std::vector<std::string>& strings) { |
| 117 if (strings.empty()) | 117 if (strings.empty()) |
| 118 return std::string(); | 118 return std::string(); |
| (...skipping 720 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 839 bool result = true; | 839 bool result = true; |
| 840 for (std::vector<AutofillForm::Element>::const_iterator | 840 for (std::vector<AutofillForm::Element>::const_iterator |
| 841 itr = elements.begin(); | 841 itr = elements.begin(); |
| 842 itr != elements.end(); | 842 itr != elements.end(); |
| 843 itr++) { | 843 itr++) { |
| 844 result = result && AddAutofillFormElement(*itr); | 844 result = result && AddAutofillFormElement(*itr); |
| 845 } | 845 } |
| 846 return result; | 846 return result; |
| 847 } | 847 } |
| 848 | 848 |
| 849 bool WebDatabase::ClearAutofillEmptyValueElements() { |
| 850 SQLStatement s; |
| 851 |
| 852 if (s.prepare(db_, "SELECT pair_id FROM autofill " |
| 853 "WHERE TRIM(value)= \"\"") != SQLITE_OK) { |
| 854 NOTREACHED() << "Statement prepare failed"; |
| 855 return false; |
| 856 } |
| 857 |
| 858 std::set<int64> ids; |
| 859 int result; |
| 860 while ((result = s.step()) == SQLITE_ROW) |
| 861 ids.insert(s.column_int64(0)); |
| 862 |
| 863 bool success = true; |
| 864 for (std::set<int64>::const_iterator iter = ids.begin(); iter != ids.end(); |
| 865 ++iter) { |
| 866 if (!RemoveFormElement(*iter)) |
| 867 success = false; |
| 868 } |
| 869 |
| 870 return success; |
| 871 } |
| 872 |
| 849 bool WebDatabase::GetIDAndCountOfFormElement( | 873 bool WebDatabase::GetIDAndCountOfFormElement( |
| 850 const AutofillForm::Element& element, int64* pair_id, int* count) { | 874 const AutofillForm::Element& element, int64* pair_id, int* count) { |
| 851 SQLStatement s; | 875 SQLStatement s; |
| 852 | 876 |
| 853 if (s.prepare(db_, "SELECT pair_id, count FROM autofill " | 877 if (s.prepare(db_, "SELECT pair_id, count FROM autofill " |
| 854 " WHERE name = ? AND value = ?") != SQLITE_OK) { | 878 " WHERE name = ? AND value = ?") != SQLITE_OK) { |
| 855 NOTREACHED() << "Statement prepare failed"; | 879 NOTREACHED() << "Statement prepare failed"; |
| 856 return false; | 880 return false; |
| 857 } | 881 } |
| 858 | 882 |
| (...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1047 | 1071 |
| 1048 if (result != SQLITE_DONE) { | 1072 if (result != SQLITE_DONE) { |
| 1049 NOTREACHED(); | 1073 NOTREACHED(); |
| 1050 return false; | 1074 return false; |
| 1051 } | 1075 } |
| 1052 | 1076 |
| 1053 for (std::vector<int64>::iterator itr = pair_ids.begin(); | 1077 for (std::vector<int64>::iterator itr = pair_ids.begin(); |
| 1054 itr != pair_ids.end(); | 1078 itr != pair_ids.end(); |
| 1055 itr++) { | 1079 itr++) { |
| 1056 int how_many = 0; | 1080 int how_many = 0; |
| 1057 if (!RemovePairIDAndDate(*itr, delete_begin, delete_end, &how_many)) | 1081 if (!RemoveFormElementForTimeRange(*itr, delete_begin, delete_end, |
| 1082 &how_many)) { |
| 1058 return false; | 1083 return false; |
| 1084 } |
| 1059 if (!AddToCountOfFormElement(*itr, -how_many)) | 1085 if (!AddToCountOfFormElement(*itr, -how_many)) |
| 1060 return false; | 1086 return false; |
| 1061 } | 1087 } |
| 1062 | 1088 |
| 1063 return true; | 1089 return true; |
| 1064 } | 1090 } |
| 1065 | 1091 |
| 1066 bool WebDatabase::RemovePairIDAndDate(int64 pair_id, const Time delete_begin, | 1092 bool WebDatabase::RemoveFormElementForTimeRange(int64 pair_id, |
| 1067 const Time delete_end, int* how_many) { | 1093 const Time delete_begin, |
| 1094 const Time delete_end, |
| 1095 int* how_many) { |
| 1068 SQLStatement s; | 1096 SQLStatement s; |
| 1069 if (s.prepare(db_, | 1097 if (s.prepare(db_, |
| 1070 "DELETE FROM autofill_dates WHERE pair_id = ? AND " | 1098 "DELETE FROM autofill_dates WHERE pair_id = ? AND " |
| 1071 "date_created >= ? AND date_created < ?") != SQLITE_OK) { | 1099 "date_created >= ? AND date_created < ?") != SQLITE_OK) { |
| 1072 NOTREACHED() << "Statement 1 prepare failed"; | 1100 NOTREACHED() << "Statement 1 prepare failed"; |
| 1073 return false; | 1101 return false; |
| 1074 } | 1102 } |
| 1075 s.bind_int64(0, pair_id); | 1103 s.bind_int64(0, pair_id); |
| 1076 s.bind_int64(1, delete_begin.ToTimeT()); | 1104 s.bind_int64(1, delete_begin.is_null() ? 0 : delete_begin.ToTimeT()); |
| 1077 s.bind_int64(2, | 1105 s.bind_int64(2, delete_end.is_null() ? std::numeric_limits<int64>::max() : |
| 1078 delete_end.is_null() ? | 1106 delete_end.ToTimeT()); |
| 1079 std::numeric_limits<int64>::max() : | |
| 1080 delete_end.ToTimeT()); | |
| 1081 | 1107 |
| 1082 bool result = (s.step() == SQLITE_DONE); | 1108 bool result = (s.step() == SQLITE_DONE); |
| 1083 *how_many = sqlite3_changes(db_); | 1109 if (how_many) |
| 1110 *how_many = sqlite3_changes(db_); |
| 1084 | 1111 |
| 1085 return result; | 1112 return result; |
| 1086 } | 1113 } |
| 1087 | 1114 |
| 1088 bool WebDatabase::AddToCountOfFormElement(int64 pair_id, int delta) { | 1115 bool WebDatabase::AddToCountOfFormElement(int64 pair_id, int delta) { |
| 1089 int count=0; | 1116 int count=0; |
| 1090 | 1117 |
| 1091 if (!GetCountOfFormElement(pair_id, &count)) | 1118 if (!GetCountOfFormElement(pair_id, &count)) |
| 1092 return false; | 1119 return false; |
| 1093 | 1120 |
| 1094 if (count + delta == 0) { | 1121 if (count + delta == 0) { |
| 1095 if (!RemoveFormElement(pair_id)) | 1122 if (!RemoveFormElement(pair_id)) |
| 1096 return false; | 1123 return false; |
| 1097 } else { | 1124 } else { |
| 1098 if (!SetCountOfFormElement(pair_id, count + delta)) | 1125 if (!SetCountOfFormElement(pair_id, count + delta)) |
| 1099 return false; | 1126 return false; |
| 1100 } | 1127 } |
| 1101 return true; | 1128 return true; |
| 1102 } | 1129 } |
| 1103 | 1130 |
| 1104 bool WebDatabase::RemoveFormElement(int64 pair_id) { | 1131 bool WebDatabase::RemoveFormElement(int64 pair_id) { |
| 1105 SQLStatement s; | 1132 SQLStatement s; |
| 1106 if (s.prepare(db_, | 1133 if (s.prepare(db_, |
| 1107 "DELETE FROM autofill WHERE pair_id = ?") != SQLITE_OK) { | 1134 "DELETE FROM autofill WHERE pair_id = ?") != SQLITE_OK) { |
| 1108 NOTREACHED() << "Statement 1 prepare failed"; | 1135 NOTREACHED() << "Statement prepare failed"; |
| 1109 return false; | 1136 return false; |
| 1110 } | 1137 } |
| 1111 s.bind_int64(0, pair_id); | 1138 s.bind_int64(0, pair_id); |
| 1139 if (s.step() != SQLITE_DONE) |
| 1140 return false; |
| 1112 | 1141 |
| 1113 return (s.step() == SQLITE_DONE); | 1142 return RemoveFormElementForTimeRange(pair_id, Time(), Time(), NULL); |
| 1114 } | 1143 } |
| 1115 | 1144 |
| 1116 void WebDatabase::MigrateOldVersionsAsNeeded() { | 1145 void WebDatabase::MigrateOldVersionsAsNeeded() { |
| 1117 // Migrate if necessary. | 1146 // Migrate if necessary. |
| 1118 int current_version = meta_table_.GetVersionNumber(); | 1147 int current_version = meta_table_.GetVersionNumber(); |
| 1119 switch(current_version) { | 1148 switch(current_version) { |
| 1120 // Versions 1 - 19 are unhandled. Version numbers greater than | 1149 // Versions 1 - 19 are unhandled. Version numbers greater than |
| 1121 // kCurrentVersionNumber should have already been weeded out by the caller. | 1150 // kCurrentVersionNumber should have already been weeded out by the caller. |
| 1122 default: | 1151 default: |
| 1123 // When the version is too old, we just try to continue anyway. There | 1152 // When the version is too old, we just try to continue anyway. There |
| (...skipping 10 matching lines...) Expand all Loading... |
| 1134 "INTEGER DEFAULT 0", NULL, NULL, NULL) != SQLITE_OK) { | 1163 "INTEGER DEFAULT 0", NULL, NULL, NULL) != SQLITE_OK) { |
| 1135 NOTREACHED(); | 1164 NOTREACHED(); |
| 1136 LOG(WARNING) << "Unable to update web database to version 21."; | 1165 LOG(WARNING) << "Unable to update web database to version 21."; |
| 1137 return; | 1166 return; |
| 1138 } | 1167 } |
| 1139 meta_table_.SetVersionNumber(21); | 1168 meta_table_.SetVersionNumber(21); |
| 1140 meta_table_.SetCompatibleVersionNumber( | 1169 meta_table_.SetCompatibleVersionNumber( |
| 1141 std::min(21, kCompatibleVersionNumber)); | 1170 std::min(21, kCompatibleVersionNumber)); |
| 1142 // FALL THROUGH | 1171 // FALL THROUGH |
| 1143 | 1172 |
| 1173 case 21: |
| 1174 if (!ClearAutofillEmptyValueElements()) { |
| 1175 NOTREACHED() << "Failed to clean-up autofill DB."; |
| 1176 } |
| 1177 meta_table_.SetVersionNumber(22); |
| 1178 // No change in the compatibility version number. |
| 1179 |
| 1180 // FALL THROUGH |
| 1181 |
| 1144 // Add successive versions here. Each should set the version number and | 1182 // Add successive versions here. Each should set the version number and |
| 1145 // compatible version number as appropriate, then fall through to the next | 1183 // compatible version number as appropriate, then fall through to the next |
| 1146 // case. | 1184 // case. |
| 1147 | 1185 |
| 1148 case kCurrentVersionNumber: | 1186 case kCurrentVersionNumber: |
| 1149 // No migration needed. | 1187 // No migration needed. |
| 1150 return; | 1188 return; |
| 1151 } | 1189 } |
| 1152 } | 1190 } |
| OLD | NEW |