| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/password_manager/login_database.h" | 5 #include "chrome/browser/password_manager/login_database.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <limits> | 8 #include <limits> |
| 9 | 9 |
| 10 #include "base/file_path.h" | 10 #include "base/file_path.h" |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 131 return false; | 131 return false; |
| 132 } | 132 } |
| 133 } | 133 } |
| 134 return true; | 134 return true; |
| 135 } | 135 } |
| 136 | 136 |
| 137 void LoginDatabase::ReportMetrics() { | 137 void LoginDatabase::ReportMetrics() { |
| 138 sql::Statement s(db_.GetCachedStatement(SQL_FROM_HERE, | 138 sql::Statement s(db_.GetCachedStatement(SQL_FROM_HERE, |
| 139 "SELECT signon_realm, COUNT(username_value) FROM logins " | 139 "SELECT signon_realm, COUNT(username_value) FROM logins " |
| 140 "GROUP BY signon_realm")); | 140 "GROUP BY signon_realm")); |
| 141 if (!s) { | 141 |
| 142 NOTREACHED() << "Statement prepare failed"; | 142 if (!s.is_valid()) |
| 143 return; | 143 return; |
| 144 } | |
| 145 | 144 |
| 146 int total_accounts = 0; | 145 int total_accounts = 0; |
| 147 while (s.Step()) { | 146 while (s.Step()) { |
| 148 int accounts_per_site = s.ColumnInt(1); | 147 int accounts_per_site = s.ColumnInt(1); |
| 149 total_accounts += accounts_per_site; | 148 total_accounts += accounts_per_site; |
| 150 UMA_HISTOGRAM_CUSTOM_COUNTS("PasswordManager.AccountsPerSite", | 149 UMA_HISTOGRAM_CUSTOM_COUNTS("PasswordManager.AccountsPerSite", |
| 151 accounts_per_site, 0, 32, 6); | 150 accounts_per_site, 0, 32, 6); |
| 152 } | 151 } |
| 153 UMA_HISTOGRAM_CUSTOM_COUNTS("PasswordManager.TotalAccounts", | 152 UMA_HISTOGRAM_CUSTOM_COUNTS("PasswordManager.TotalAccounts", |
| 154 total_accounts, 0, 32, 6); | 153 total_accounts, 0, 32, 6); |
| 155 | |
| 156 return; | |
| 157 } | 154 } |
| 158 | 155 |
| 159 bool LoginDatabase::AddLogin(const PasswordForm& form) { | 156 bool LoginDatabase::AddLogin(const PasswordForm& form) { |
| 160 // You *must* change LoginTableColumns if this query changes. | 157 // You *must* change LoginTableColumns if this query changes. |
| 161 sql::Statement s(db_.GetCachedStatement(SQL_FROM_HERE, | 158 sql::Statement s(db_.GetCachedStatement(SQL_FROM_HERE, |
| 162 "INSERT OR REPLACE INTO logins " | 159 "INSERT OR REPLACE INTO logins " |
| 163 "(origin_url, action_url, username_element, username_value, " | 160 "(origin_url, action_url, username_element, username_value, " |
| 164 " password_element, password_value, submit_element, " | 161 " password_element, password_value, submit_element, " |
| 165 " signon_realm, ssl_valid, preferred, date_created, " | 162 " signon_realm, ssl_valid, preferred, date_created, " |
| 166 " blacklisted_by_user, scheme) " | 163 " blacklisted_by_user, scheme) " |
| 167 "VALUES " | 164 "VALUES " |
| 168 "(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")); | 165 "(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")); |
| 169 if (!s) { | |
| 170 NOTREACHED() << "Statement prepare failed"; | |
| 171 return false; | |
| 172 } | |
| 173 | |
| 174 s.BindString(COLUMN_ORIGIN_URL, form.origin.spec()); | 166 s.BindString(COLUMN_ORIGIN_URL, form.origin.spec()); |
| 175 s.BindString(COLUMN_ACTION_URL, form.action.spec()); | 167 s.BindString(COLUMN_ACTION_URL, form.action.spec()); |
| 176 s.BindString16(COLUMN_USERNAME_ELEMENT, form.username_element); | 168 s.BindString16(COLUMN_USERNAME_ELEMENT, form.username_element); |
| 177 s.BindString16(COLUMN_USERNAME_VALUE, form.username_value); | 169 s.BindString16(COLUMN_USERNAME_VALUE, form.username_value); |
| 178 s.BindString16(COLUMN_PASSWORD_ELEMENT, form.password_element); | 170 s.BindString16(COLUMN_PASSWORD_ELEMENT, form.password_element); |
| 179 std::string encrypted_password = EncryptedString(form.password_value); | 171 std::string encrypted_password = EncryptedString(form.password_value); |
| 180 s.BindBlob(COLUMN_PASSWORD_VALUE, encrypted_password.data(), | 172 s.BindBlob(COLUMN_PASSWORD_VALUE, encrypted_password.data(), |
| 181 static_cast<int>(encrypted_password.length())); | 173 static_cast<int>(encrypted_password.length())); |
| 182 s.BindString16(COLUMN_SUBMIT_ELEMENT, form.submit_element); | 174 s.BindString16(COLUMN_SUBMIT_ELEMENT, form.submit_element); |
| 183 s.BindString(COLUMN_SIGNON_REALM, form.signon_realm); | 175 s.BindString(COLUMN_SIGNON_REALM, form.signon_realm); |
| 184 s.BindInt(COLUMN_SSL_VALID, form.ssl_valid); | 176 s.BindInt(COLUMN_SSL_VALID, form.ssl_valid); |
| 185 s.BindInt(COLUMN_PREFERRED, form.preferred); | 177 s.BindInt(COLUMN_PREFERRED, form.preferred); |
| 186 s.BindInt64(COLUMN_DATE_CREATED, form.date_created.ToTimeT()); | 178 s.BindInt64(COLUMN_DATE_CREATED, form.date_created.ToTimeT()); |
| 187 s.BindInt(COLUMN_BLACKLISTED_BY_USER, form.blacklisted_by_user); | 179 s.BindInt(COLUMN_BLACKLISTED_BY_USER, form.blacklisted_by_user); |
| 188 s.BindInt(COLUMN_SCHEME, form.scheme); | 180 s.BindInt(COLUMN_SCHEME, form.scheme); |
| 189 if (!s.Run()) { | 181 |
| 190 NOTREACHED(); | 182 return s.Run(); |
| 191 return false; | |
| 192 } | |
| 193 return true; | |
| 194 } | 183 } |
| 195 | 184 |
| 196 bool LoginDatabase::UpdateLogin(const PasswordForm& form, int* items_changed) { | 185 bool LoginDatabase::UpdateLogin(const PasswordForm& form, int* items_changed) { |
| 197 sql::Statement s(db_.GetCachedStatement(SQL_FROM_HERE, | 186 sql::Statement s(db_.GetCachedStatement(SQL_FROM_HERE, |
| 198 "UPDATE logins SET " | 187 "UPDATE logins SET " |
| 199 "action_url = ?, " | 188 "action_url = ?, " |
| 200 "password_value = ?, " | 189 "password_value = ?, " |
| 201 "ssl_valid = ?, " | 190 "ssl_valid = ?, " |
| 202 "preferred = ? " | 191 "preferred = ? " |
| 203 "WHERE origin_url = ? AND " | 192 "WHERE origin_url = ? AND " |
| 204 "username_element = ? AND " | 193 "username_element = ? AND " |
| 205 "username_value = ? AND " | 194 "username_value = ? AND " |
| 206 "password_element = ? AND " | 195 "password_element = ? AND " |
| 207 "signon_realm = ?")); | 196 "signon_realm = ?")); |
| 208 if (!s) { | |
| 209 NOTREACHED() << "Statement prepare failed"; | |
| 210 return false; | |
| 211 } | |
| 212 | |
| 213 s.BindString(0, form.action.spec()); | 197 s.BindString(0, form.action.spec()); |
| 214 std::string encrypted_password = EncryptedString(form.password_value); | 198 std::string encrypted_password = EncryptedString(form.password_value); |
| 215 s.BindBlob(1, encrypted_password.data(), | 199 s.BindBlob(1, encrypted_password.data(), |
| 216 static_cast<int>(encrypted_password.length())); | 200 static_cast<int>(encrypted_password.length())); |
| 217 s.BindInt(2, form.ssl_valid); | 201 s.BindInt(2, form.ssl_valid); |
| 218 s.BindInt(3, form.preferred); | 202 s.BindInt(3, form.preferred); |
| 219 s.BindString(4, form.origin.spec()); | 203 s.BindString(4, form.origin.spec()); |
| 220 s.BindString16(5, form.username_element); | 204 s.BindString16(5, form.username_element); |
| 221 s.BindString16(6, form.username_value); | 205 s.BindString16(6, form.username_value); |
| 222 s.BindString16(7, form.password_element); | 206 s.BindString16(7, form.password_element); |
| 223 s.BindString(8, form.signon_realm); | 207 s.BindString(8, form.signon_realm); |
| 224 | 208 |
| 225 if (!s.Run()) { | 209 if (!s.Run()) |
| 226 NOTREACHED(); | |
| 227 return false; | 210 return false; |
| 228 } | 211 |
| 229 if (items_changed) { | 212 if (items_changed) |
| 230 *items_changed = db_.GetLastChangeCount(); | 213 *items_changed = db_.GetLastChangeCount(); |
| 231 } | 214 |
| 232 return true; | 215 return true; |
| 233 } | 216 } |
| 234 | 217 |
| 235 bool LoginDatabase::RemoveLogin(const PasswordForm& form) { | 218 bool LoginDatabase::RemoveLogin(const PasswordForm& form) { |
| 236 // Remove a login by UNIQUE-constrained fields. | 219 // Remove a login by UNIQUE-constrained fields. |
| 237 sql::Statement s(db_.GetCachedStatement(SQL_FROM_HERE, | 220 sql::Statement s(db_.GetCachedStatement(SQL_FROM_HERE, |
| 238 "DELETE FROM logins WHERE " | 221 "DELETE FROM logins WHERE " |
| 239 "origin_url = ? AND " | 222 "origin_url = ? AND " |
| 240 "username_element = ? AND " | 223 "username_element = ? AND " |
| 241 "username_value = ? AND " | 224 "username_value = ? AND " |
| 242 "password_element = ? AND " | 225 "password_element = ? AND " |
| 243 "submit_element = ? AND " | 226 "submit_element = ? AND " |
| 244 "signon_realm = ? ")); | 227 "signon_realm = ? ")); |
| 245 if (!s) { | |
| 246 NOTREACHED() << "Statement prepare failed"; | |
| 247 return false; | |
| 248 } | |
| 249 | |
| 250 s.BindString(0, form.origin.spec()); | 228 s.BindString(0, form.origin.spec()); |
| 251 s.BindString16(1, form.username_element); | 229 s.BindString16(1, form.username_element); |
| 252 s.BindString16(2, form.username_value); | 230 s.BindString16(2, form.username_value); |
| 253 s.BindString16(3, form.password_element); | 231 s.BindString16(3, form.password_element); |
| 254 s.BindString16(4, form.submit_element); | 232 s.BindString16(4, form.submit_element); |
| 255 s.BindString(5, form.signon_realm); | 233 s.BindString(5, form.signon_realm); |
| 256 | 234 |
| 257 if (!s.Run()) { | 235 return s.Run(); |
| 258 NOTREACHED(); | |
| 259 return false; | |
| 260 } | |
| 261 return true; | |
| 262 } | 236 } |
| 263 | 237 |
| 264 bool LoginDatabase::RemoveLoginsCreatedBetween(const base::Time delete_begin, | 238 bool LoginDatabase::RemoveLoginsCreatedBetween(const base::Time delete_begin, |
| 265 const base::Time delete_end) { | 239 const base::Time delete_end) { |
| 266 sql::Statement s(db_.GetCachedStatement(SQL_FROM_HERE, | 240 sql::Statement s(db_.GetCachedStatement(SQL_FROM_HERE, |
| 267 "DELETE FROM logins WHERE " | 241 "DELETE FROM logins WHERE " |
| 268 "date_created >= ? AND date_created < ?")); | 242 "date_created >= ? AND date_created < ?")); |
| 269 if (!s) { | |
| 270 NOTREACHED() << "Statement prepare failed"; | |
| 271 return false; | |
| 272 } | |
| 273 s.BindInt64(0, delete_begin.ToTimeT()); | 243 s.BindInt64(0, delete_begin.ToTimeT()); |
| 274 s.BindInt64(1, delete_end.is_null() ? std::numeric_limits<int64>::max() | 244 s.BindInt64(1, delete_end.is_null() ? std::numeric_limits<int64>::max() |
| 275 : delete_end.ToTimeT()); | 245 : delete_end.ToTimeT()); |
| 276 | 246 |
| 277 return s.Run(); | 247 return s.Run(); |
| 278 } | 248 } |
| 279 | 249 |
| 280 void LoginDatabase::InitPasswordFormFromStatement(PasswordForm* form, | 250 void LoginDatabase::InitPasswordFormFromStatement(PasswordForm* form, |
| 281 sql::Statement& s) const { | 251 sql::Statement& s) const { |
| 282 std::string tmp = s.ColumnString(COLUMN_ORIGIN_URL); | 252 std::string tmp = s.ColumnString(COLUMN_ORIGIN_URL); |
| (...skipping 23 matching lines...) Expand all Loading... |
| 306 std::vector<PasswordForm*>* forms) const { | 276 std::vector<PasswordForm*>* forms) const { |
| 307 DCHECK(forms); | 277 DCHECK(forms); |
| 308 // You *must* change LoginTableColumns if this query changes. | 278 // You *must* change LoginTableColumns if this query changes. |
| 309 sql::Statement s(db_.GetCachedStatement(SQL_FROM_HERE, | 279 sql::Statement s(db_.GetCachedStatement(SQL_FROM_HERE, |
| 310 "SELECT origin_url, action_url, " | 280 "SELECT origin_url, action_url, " |
| 311 "username_element, username_value, " | 281 "username_element, username_value, " |
| 312 "password_element, password_value, " | 282 "password_element, password_value, " |
| 313 "submit_element, signon_realm, ssl_valid, preferred, " | 283 "submit_element, signon_realm, ssl_valid, preferred, " |
| 314 "date_created, blacklisted_by_user, scheme FROM logins " | 284 "date_created, blacklisted_by_user, scheme FROM logins " |
| 315 "WHERE signon_realm == ? ")); | 285 "WHERE signon_realm == ? ")); |
| 316 if (!s) { | |
| 317 NOTREACHED() << "Statement prepare failed"; | |
| 318 return false; | |
| 319 } | |
| 320 | |
| 321 s.BindString(0, form.signon_realm); | 286 s.BindString(0, form.signon_realm); |
| 322 | 287 |
| 323 while (s.Step()) { | 288 while (s.Step()) { |
| 324 PasswordForm* new_form = new PasswordForm(); | 289 PasswordForm* new_form = new PasswordForm(); |
| 325 InitPasswordFormFromStatement(new_form, s); | 290 InitPasswordFormFromStatement(new_form, s); |
| 326 | 291 |
| 327 forms->push_back(new_form); | 292 forms->push_back(new_form); |
| 328 } | 293 } |
| 329 return s.Succeeded(); | 294 return s.Succeeded(); |
| 330 } | 295 } |
| 331 | 296 |
| 332 bool LoginDatabase::GetLoginsCreatedBetween( | 297 bool LoginDatabase::GetLoginsCreatedBetween( |
| 333 const base::Time begin, | 298 const base::Time begin, |
| 334 const base::Time end, | 299 const base::Time end, |
| 335 std::vector<webkit::forms::PasswordForm*>* forms) const { | 300 std::vector<webkit::forms::PasswordForm*>* forms) const { |
| 336 DCHECK(forms); | 301 DCHECK(forms); |
| 337 sql::Statement s(db_.GetCachedStatement(SQL_FROM_HERE, | 302 sql::Statement s(db_.GetCachedStatement(SQL_FROM_HERE, |
| 338 "SELECT origin_url, action_url, " | 303 "SELECT origin_url, action_url, " |
| 339 "username_element, username_value, " | 304 "username_element, username_value, " |
| 340 "password_element, password_value, " | 305 "password_element, password_value, " |
| 341 "submit_element, signon_realm, ssl_valid, preferred, " | 306 "submit_element, signon_realm, ssl_valid, preferred, " |
| 342 "date_created, blacklisted_by_user, scheme FROM logins " | 307 "date_created, blacklisted_by_user, scheme FROM logins " |
| 343 "WHERE date_created >= ? AND date_created < ?" | 308 "WHERE date_created >= ? AND date_created < ?" |
| 344 "ORDER BY origin_url")); | 309 "ORDER BY origin_url")); |
| 345 | |
| 346 if (!s) { | |
| 347 NOTREACHED() << "Statement prepare failed"; | |
| 348 return false; | |
| 349 } | |
| 350 s.BindInt64(0, begin.ToTimeT()); | 310 s.BindInt64(0, begin.ToTimeT()); |
| 351 s.BindInt64(1, end.is_null() ? std::numeric_limits<int64>::max() | 311 s.BindInt64(1, end.is_null() ? std::numeric_limits<int64>::max() |
| 352 : end.ToTimeT()); | 312 : end.ToTimeT()); |
| 353 | 313 |
| 354 while (s.Step()) { | 314 while (s.Step()) { |
| 355 PasswordForm* new_form = new PasswordForm(); | 315 PasswordForm* new_form = new PasswordForm(); |
| 356 InitPasswordFormFromStatement(new_form, s); | 316 InitPasswordFormFromStatement(new_form, s); |
| 357 | 317 |
| 358 forms->push_back(new_form); | 318 forms->push_back(new_form); |
| 359 } | 319 } |
| (...skipping 15 matching lines...) Expand all Loading... |
| 375 DCHECK(forms); | 335 DCHECK(forms); |
| 376 // You *must* change LoginTableColumns if this query changes. | 336 // You *must* change LoginTableColumns if this query changes. |
| 377 sql::Statement s(db_.GetCachedStatement(SQL_FROM_HERE, | 337 sql::Statement s(db_.GetCachedStatement(SQL_FROM_HERE, |
| 378 "SELECT origin_url, action_url, " | 338 "SELECT origin_url, action_url, " |
| 379 "username_element, username_value, " | 339 "username_element, username_value, " |
| 380 "password_element, password_value, " | 340 "password_element, password_value, " |
| 381 "submit_element, signon_realm, ssl_valid, preferred, " | 341 "submit_element, signon_realm, ssl_valid, preferred, " |
| 382 "date_created, blacklisted_by_user, scheme FROM logins " | 342 "date_created, blacklisted_by_user, scheme FROM logins " |
| 383 "WHERE blacklisted_by_user == ? " | 343 "WHERE blacklisted_by_user == ? " |
| 384 "ORDER BY origin_url")); | 344 "ORDER BY origin_url")); |
| 385 | |
| 386 if (!s) { | |
| 387 NOTREACHED() << "Statement prepare failed"; | |
| 388 return false; | |
| 389 } | |
| 390 s.BindInt(0, blacklisted ? 1 : 0); | 345 s.BindInt(0, blacklisted ? 1 : 0); |
| 391 | 346 |
| 392 while (s.Step()) { | 347 while (s.Step()) { |
| 393 PasswordForm* new_form = new PasswordForm(); | 348 PasswordForm* new_form = new PasswordForm(); |
| 394 InitPasswordFormFromStatement(new_form, s); | 349 InitPasswordFormFromStatement(new_form, s); |
| 395 | 350 |
| 396 forms->push_back(new_form); | 351 forms->push_back(new_form); |
| 397 } | 352 } |
| 398 return s.Succeeded(); | 353 return s.Succeeded(); |
| 399 } | 354 } |
| 400 | 355 |
| 401 bool LoginDatabase::DeleteAndRecreateDatabaseFile() { | 356 bool LoginDatabase::DeleteAndRecreateDatabaseFile() { |
| 402 DCHECK(db_.is_open()); | 357 DCHECK(db_.is_open()); |
| 403 meta_table_.Reset(); | 358 meta_table_.Reset(); |
| 404 db_.Close(); | 359 db_.Close(); |
| 405 file_util::Delete(db_path_, false); | 360 file_util::Delete(db_path_, false); |
| 406 return Init(db_path_); | 361 return Init(db_path_); |
| 407 } | 362 } |
| OLD | NEW |