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

Side by Side Diff: components/password_manager/core/browser/login_database.cc

Issue 2265103002: LoginDatabase::Init: rollback before closing a DB (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 4 months 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
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 "components/password_manager/core/browser/login_database.h" 5 #include "components/password_manager/core/browser/login_database.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 #include <stdint.h> 8 #include <stdint.h>
9 #include <algorithm> 9 #include <algorithm>
10 #include <limits> 10 #include <limits>
(...skipping 513 matching lines...) Expand 10 before | Expand all | Expand 10 after
524 if (!db_.Open(db_path_)) { 524 if (!db_.Open(db_path_)) {
525 LogDatabaseInitError(OPEN_FILE_ERROR); 525 LogDatabaseInitError(OPEN_FILE_ERROR);
526 LOG(ERROR) << "Unable to open the password store database."; 526 LOG(ERROR) << "Unable to open the password store database.";
527 return false; 527 return false;
528 } 528 }
529 529
530 sql::Transaction transaction(&db_); 530 sql::Transaction transaction(&db_);
531 if (!transaction.Begin()) { 531 if (!transaction.Begin()) {
532 LogDatabaseInitError(START_TRANSACTION_ERROR); 532 LogDatabaseInitError(START_TRANSACTION_ERROR);
533 LOG(ERROR) << "Unable to start a transaction."; 533 LOG(ERROR) << "Unable to start a transaction.";
534 transaction.Rollback();
vasilii 2016/08/22 14:31:54 If it didn't begin there is nothing to rollback. T
vabr (Chromium) 2016/08/22 14:39:48 Done.
534 db_.Close(); 535 db_.Close();
535 return false; 536 return false;
536 } 537 }
537 538
538 // Check the database version. 539 // Check the database version.
539 if (!meta_table_.Init(&db_, kCurrentVersionNumber, 540 if (!meta_table_.Init(&db_, kCurrentVersionNumber,
540 kCompatibleVersionNumber)) { 541 kCompatibleVersionNumber)) {
541 LogDatabaseInitError(META_TABLE_INIT_ERROR); 542 LogDatabaseInitError(META_TABLE_INIT_ERROR);
542 LOG(ERROR) << "Unable to create the meta table."; 543 LOG(ERROR) << "Unable to create the meta table.";
544 transaction.Rollback();
543 db_.Close(); 545 db_.Close();
544 return false; 546 return false;
545 } 547 }
546 if (meta_table_.GetCompatibleVersionNumber() > kCurrentVersionNumber) { 548 if (meta_table_.GetCompatibleVersionNumber() > kCurrentVersionNumber) {
547 LogDatabaseInitError(INCOMPATIBLE_VERSION); 549 LogDatabaseInitError(INCOMPATIBLE_VERSION);
548 LOG(ERROR) << "Password store database is too new, kCurrentVersionNumber=" 550 LOG(ERROR) << "Password store database is too new, kCurrentVersionNumber="
549 << kCurrentVersionNumber << ", GetCompatibleVersionNumber=" 551 << kCurrentVersionNumber << ", GetCompatibleVersionNumber="
550 << meta_table_.GetCompatibleVersionNumber(); 552 << meta_table_.GetCompatibleVersionNumber();
553 transaction.Rollback();
551 db_.Close(); 554 db_.Close();
552 return false; 555 return false;
553 } 556 }
554 557
555 SQLTableBuilder builder; 558 SQLTableBuilder builder;
556 InitializeBuilder(&builder); 559 InitializeBuilder(&builder);
557 InitializeStatementStrings(builder); 560 InitializeStatementStrings(builder);
558 561
559 if (!db_.DoesTableExist("logins")) { 562 if (!db_.DoesTableExist("logins")) {
560 if (!builder.CreateTable(&db_)) { 563 if (!builder.CreateTable(&db_)) {
561 VLOG(0) << "Failed to create the 'logins' table"; 564 VLOG(0) << "Failed to create the 'logins' table";
565 transaction.Rollback();
562 db_.Close(); 566 db_.Close();
563 return false; 567 return false;
564 } 568 }
565 } 569 }
566 570
567 stats_table_.Init(&db_); 571 stats_table_.Init(&db_);
568 572
569 int current_version = meta_table_.GetVersionNumber(); 573 int current_version = meta_table_.GetVersionNumber();
570 bool migration_success = FixVersionIfNeeded(&db_, &current_version); 574 bool migration_success = FixVersionIfNeeded(&db_, &current_version);
571 DCHECK_LE(current_version, kCurrentVersionNumber); 575 DCHECK_LE(current_version, kCurrentVersionNumber);
572 576
573 // If the file on disk is an older database version, bring it up to date. 577 // If the file on disk is an older database version, bring it up to date.
574 if (migration_success && current_version < kCurrentVersionNumber) { 578 if (migration_success && current_version < kCurrentVersionNumber) {
575 migration_success = MigrateLogins( 579 migration_success = MigrateLogins(
576 base::checked_cast<unsigned>(current_version), &builder, &db_); 580 base::checked_cast<unsigned>(current_version), &builder, &db_);
577 } 581 }
578 if (migration_success && current_version <= 15) { 582 if (migration_success && current_version <= 15) {
579 migration_success = stats_table_.MigrateToVersion(16); 583 migration_success = stats_table_.MigrateToVersion(16);
580 } 584 }
581 if (migration_success) { 585 if (migration_success) {
582 meta_table_.SetCompatibleVersionNumber(kCompatibleVersionNumber); 586 meta_table_.SetCompatibleVersionNumber(kCompatibleVersionNumber);
583 meta_table_.SetVersionNumber(kCurrentVersionNumber); 587 meta_table_.SetVersionNumber(kCurrentVersionNumber);
584 } else { 588 } else {
585 LogDatabaseInitError(MIGRATION_ERROR); 589 LogDatabaseInitError(MIGRATION_ERROR);
586 UMA_HISTOGRAM_SPARSE_SLOWLY("PasswordManager.LoginDatabaseFailedVersion", 590 UMA_HISTOGRAM_SPARSE_SLOWLY("PasswordManager.LoginDatabaseFailedVersion",
587 meta_table_.GetVersionNumber()); 591 meta_table_.GetVersionNumber());
588 LOG(ERROR) << "Unable to migrate database from " 592 LOG(ERROR) << "Unable to migrate database from "
589 << meta_table_.GetVersionNumber() << " to " 593 << meta_table_.GetVersionNumber() << " to "
590 << kCurrentVersionNumber; 594 << kCurrentVersionNumber;
595 transaction.Rollback();
591 db_.Close(); 596 db_.Close();
592 return false; 597 return false;
593 } 598 }
594 599
595 if (!stats_table_.CreateTableIfNecessary()) { 600 if (!stats_table_.CreateTableIfNecessary()) {
596 LogDatabaseInitError(INIT_STATS_ERROR); 601 LogDatabaseInitError(INIT_STATS_ERROR);
597 LOG(ERROR) << "Unable to create the stats table."; 602 LOG(ERROR) << "Unable to create the stats table.";
603 transaction.Rollback();
598 db_.Close(); 604 db_.Close();
599 return false; 605 return false;
600 } 606 }
601 607
602 if (!transaction.Commit()) { 608 if (!transaction.Commit()) {
603 LogDatabaseInitError(COMMIT_TRANSACTION_ERROR); 609 LogDatabaseInitError(COMMIT_TRANSACTION_ERROR);
604 LOG(ERROR) << "Unable to commit a transaction."; 610 LOG(ERROR) << "Unable to commit a transaction.";
611 transaction.Rollback();
vasilii 2016/08/22 14:31:54 That's a bug to call Rollback() after Commit().
vabr (Chromium) 2016/08/22 14:39:48 Done.
605 db_.Close(); 612 db_.Close();
606 return false; 613 return false;
607 } 614 }
608 615
609 LogDatabaseInitError(INIT_OK); 616 LogDatabaseInitError(INIT_OK);
610 return true; 617 return true;
611 } 618 }
612 619
613 void LoginDatabase::ReportMetrics(const std::string& sync_username, 620 void LoginDatabase::ReportMetrics(const std::string& sync_username,
614 bool custom_passphrase_sync_enabled) { 621 bool custom_passphrase_sync_enabled) {
(...skipping 683 matching lines...) Expand 10 before | Expand all | Expand 10 after
1298 DCHECK(blacklisted_statement_.empty()); 1305 DCHECK(blacklisted_statement_.empty());
1299 blacklisted_statement_ = 1306 blacklisted_statement_ =
1300 "SELECT " + all_column_names + 1307 "SELECT " + all_column_names +
1301 " FROM logins WHERE blacklisted_by_user == ? ORDER BY origin_url"; 1308 " FROM logins WHERE blacklisted_by_user == ? ORDER BY origin_url";
1302 DCHECK(encrypted_statement_.empty()); 1309 DCHECK(encrypted_statement_.empty());
1303 encrypted_statement_ = 1310 encrypted_statement_ =
1304 "SELECT password_value FROM logins WHERE " + all_unique_key_column_names; 1311 "SELECT password_value FROM logins WHERE " + all_unique_key_column_names;
1305 } 1312 }
1306 1313
1307 } // namespace password_manager 1314 } // namespace password_manager
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698