| OLD | NEW |
| 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 "net/extras/sqlite/sqlite_channel_id_store.h" | 5 #include "net/extras/sqlite/sqlite_channel_id_store.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 #include <set> | 8 #include <set> |
| 9 #include <utility> | 9 #include <utility> |
| 10 #include <vector> | 10 #include <vector> |
| (...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 187 | 187 |
| 188 background_task_runner_->PostTaskAndReply( | 188 background_task_runner_->PostTaskAndReply( |
| 189 FROM_HERE, | 189 FROM_HERE, |
| 190 base::Bind(&Backend::LoadInBackground, this, channel_ids_ptr), | 190 base::Bind(&Backend::LoadInBackground, this, channel_ids_ptr), |
| 191 base::Bind(loaded_callback, base::Passed(&channel_ids))); | 191 base::Bind(loaded_callback, base::Passed(&channel_ids))); |
| 192 } | 192 } |
| 193 | 193 |
| 194 void SQLiteChannelIDStore::Backend::LoadInBackground( | 194 void SQLiteChannelIDStore::Backend::LoadInBackground( |
| 195 std::vector<std::unique_ptr<DefaultChannelIDStore::ChannelID>>* | 195 std::vector<std::unique_ptr<DefaultChannelIDStore::ChannelID>>* |
| 196 channel_ids) { | 196 channel_ids) { |
| 197 DCHECK(background_task_runner_->RunsTasksOnCurrentThread()); | 197 DCHECK(background_task_runner_->RunsTasksInCurrentSequence()); |
| 198 | 198 |
| 199 // This method should be called only once per instance. | 199 // This method should be called only once per instance. |
| 200 DCHECK(!db_.get()); | 200 DCHECK(!db_.get()); |
| 201 | 201 |
| 202 base::TimeTicks start = base::TimeTicks::Now(); | 202 base::TimeTicks start = base::TimeTicks::Now(); |
| 203 | 203 |
| 204 // Ensure the parent directory for storing certs is created before reading | 204 // Ensure the parent directory for storing certs is created before reading |
| 205 // from it. | 205 // from it. |
| 206 const base::FilePath dir = path_.DirName(); | 206 const base::FilePath dir = path_.DirName(); |
| 207 if (!base::PathExists(dir) && !base::CreateDirectory(dir)) { | 207 if (!base::PathExists(dir) && !base::CreateDirectory(dir)) { |
| (...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 418 transaction.Commit(); | 418 transaction.Commit(); |
| 419 | 419 |
| 420 // Put future migration cases here. | 420 // Put future migration cases here. |
| 421 | 421 |
| 422 return true; | 422 return true; |
| 423 } | 423 } |
| 424 | 424 |
| 425 void SQLiteChannelIDStore::Backend::DatabaseErrorCallback( | 425 void SQLiteChannelIDStore::Backend::DatabaseErrorCallback( |
| 426 int error, | 426 int error, |
| 427 sql::Statement* stmt) { | 427 sql::Statement* stmt) { |
| 428 DCHECK(background_task_runner_->RunsTasksOnCurrentThread()); | 428 DCHECK(background_task_runner_->RunsTasksInCurrentSequence()); |
| 429 | 429 |
| 430 if (!sql::IsErrorCatastrophic(error)) | 430 if (!sql::IsErrorCatastrophic(error)) |
| 431 return; | 431 return; |
| 432 | 432 |
| 433 // TODO(shess): Running KillDatabase() multiple times should be | 433 // TODO(shess): Running KillDatabase() multiple times should be |
| 434 // safe. | 434 // safe. |
| 435 if (corruption_detected_) | 435 if (corruption_detected_) |
| 436 return; | 436 return; |
| 437 | 437 |
| 438 corruption_detected_ = true; | 438 corruption_detected_ = true; |
| 439 | 439 |
| 440 // TODO(shess): Consider just calling RazeAndClose() immediately. | 440 // TODO(shess): Consider just calling RazeAndClose() immediately. |
| 441 // db_ may not be safe to reset at this point, but RazeAndClose() | 441 // db_ may not be safe to reset at this point, but RazeAndClose() |
| 442 // would cause the stack to unwind safely with errors. | 442 // would cause the stack to unwind safely with errors. |
| 443 background_task_runner_->PostTask(FROM_HERE, | 443 background_task_runner_->PostTask(FROM_HERE, |
| 444 base::Bind(&Backend::KillDatabase, this)); | 444 base::Bind(&Backend::KillDatabase, this)); |
| 445 } | 445 } |
| 446 | 446 |
| 447 void SQLiteChannelIDStore::Backend::KillDatabase() { | 447 void SQLiteChannelIDStore::Backend::KillDatabase() { |
| 448 DCHECK(background_task_runner_->RunsTasksOnCurrentThread()); | 448 DCHECK(background_task_runner_->RunsTasksInCurrentSequence()); |
| 449 | 449 |
| 450 if (db_) { | 450 if (db_) { |
| 451 // This Backend will now be in-memory only. In a future run the database | 451 // This Backend will now be in-memory only. In a future run the database |
| 452 // will be recreated. Hopefully things go better then! | 452 // will be recreated. Hopefully things go better then! |
| 453 bool success = db_->RazeAndClose(); | 453 bool success = db_->RazeAndClose(); |
| 454 UMA_HISTOGRAM_BOOLEAN("DomainBoundCerts.KillDatabaseResult", success); | 454 UMA_HISTOGRAM_BOOLEAN("DomainBoundCerts.KillDatabaseResult", success); |
| 455 meta_table_.Reset(); | 455 meta_table_.Reset(); |
| 456 db_.reset(); | 456 db_.reset(); |
| 457 } | 457 } |
| 458 } | 458 } |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 511 base::TimeDelta::FromMilliseconds(kCommitIntervalMs)); | 511 base::TimeDelta::FromMilliseconds(kCommitIntervalMs)); |
| 512 } else if (num_pending == kCommitAfterBatchSize) { | 512 } else if (num_pending == kCommitAfterBatchSize) { |
| 513 // We've reached a big enough batch, fire off a commit now. | 513 // We've reached a big enough batch, fire off a commit now. |
| 514 background_task_runner_->PostTask(FROM_HERE, | 514 background_task_runner_->PostTask(FROM_HERE, |
| 515 base::Bind(&Backend::Commit, this)); | 515 base::Bind(&Backend::Commit, this)); |
| 516 } | 516 } |
| 517 } | 517 } |
| 518 | 518 |
| 519 void SQLiteChannelIDStore::Backend::PrunePendingOperationsForDeletes( | 519 void SQLiteChannelIDStore::Backend::PrunePendingOperationsForDeletes( |
| 520 const std::list<std::string>& server_identifiers) { | 520 const std::list<std::string>& server_identifiers) { |
| 521 DCHECK(background_task_runner_->RunsTasksOnCurrentThread()); | 521 DCHECK(background_task_runner_->RunsTasksInCurrentSequence()); |
| 522 base::AutoLock locked(lock_); | 522 base::AutoLock locked(lock_); |
| 523 | 523 |
| 524 for (PendingOperationsList::iterator it = pending_.begin(); | 524 for (PendingOperationsList::iterator it = pending_.begin(); |
| 525 it != pending_.end();) { | 525 it != pending_.end();) { |
| 526 bool remove = | 526 bool remove = |
| 527 std::find(server_identifiers.begin(), server_identifiers.end(), | 527 std::find(server_identifiers.begin(), server_identifiers.end(), |
| 528 (*it)->channel_id().server_identifier()) != | 528 (*it)->channel_id().server_identifier()) != |
| 529 server_identifiers.end(); | 529 server_identifiers.end(); |
| 530 | 530 |
| 531 if (remove) { | 531 if (remove) { |
| 532 std::unique_ptr<PendingOperation> po(*it); | 532 std::unique_ptr<PendingOperation> po(*it); |
| 533 it = pending_.erase(it); | 533 it = pending_.erase(it); |
| 534 --num_pending_; | 534 --num_pending_; |
| 535 } else { | 535 } else { |
| 536 ++it; | 536 ++it; |
| 537 } | 537 } |
| 538 } | 538 } |
| 539 } | 539 } |
| 540 | 540 |
| 541 void SQLiteChannelIDStore::Backend::Commit() { | 541 void SQLiteChannelIDStore::Backend::Commit() { |
| 542 DCHECK(background_task_runner_->RunsTasksOnCurrentThread()); | 542 DCHECK(background_task_runner_->RunsTasksInCurrentSequence()); |
| 543 | 543 |
| 544 PendingOperationsList ops; | 544 PendingOperationsList ops; |
| 545 { | 545 { |
| 546 base::AutoLock locked(lock_); | 546 base::AutoLock locked(lock_); |
| 547 pending_.swap(ops); | 547 pending_.swap(ops); |
| 548 num_pending_ = 0; | 548 num_pending_ = 0; |
| 549 } | 549 } |
| 550 | 550 |
| 551 // Maybe an old timer fired or we are already Close()'ed. | 551 // Maybe an old timer fired or we are already Close()'ed. |
| 552 if (!db_.get() || ops.empty()) | 552 if (!db_.get() || ops.empty()) |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 605 // Fire off a close message to the background task runner. We could still have a | 605 // Fire off a close message to the background task runner. We could still have a |
| 606 // pending commit timer that will be holding a reference on us, but if/when | 606 // pending commit timer that will be holding a reference on us, but if/when |
| 607 // this fires we will already have been cleaned up and it will be ignored. | 607 // this fires we will already have been cleaned up and it will be ignored. |
| 608 void SQLiteChannelIDStore::Backend::Close() { | 608 void SQLiteChannelIDStore::Backend::Close() { |
| 609 // Must close the backend on the background task runner. | 609 // Must close the backend on the background task runner. |
| 610 background_task_runner_->PostTask( | 610 background_task_runner_->PostTask( |
| 611 FROM_HERE, base::Bind(&Backend::InternalBackgroundClose, this)); | 611 FROM_HERE, base::Bind(&Backend::InternalBackgroundClose, this)); |
| 612 } | 612 } |
| 613 | 613 |
| 614 void SQLiteChannelIDStore::Backend::InternalBackgroundClose() { | 614 void SQLiteChannelIDStore::Backend::InternalBackgroundClose() { |
| 615 DCHECK(background_task_runner_->RunsTasksOnCurrentThread()); | 615 DCHECK(background_task_runner_->RunsTasksInCurrentSequence()); |
| 616 // Commit any pending operations | 616 // Commit any pending operations |
| 617 Commit(); | 617 Commit(); |
| 618 db_.reset(); | 618 db_.reset(); |
| 619 } | 619 } |
| 620 | 620 |
| 621 void SQLiteChannelIDStore::Backend::BackgroundDeleteAllInList( | 621 void SQLiteChannelIDStore::Backend::BackgroundDeleteAllInList( |
| 622 const std::list<std::string>& server_identifiers) { | 622 const std::list<std::string>& server_identifiers) { |
| 623 DCHECK(background_task_runner_->RunsTasksOnCurrentThread()); | 623 DCHECK(background_task_runner_->RunsTasksInCurrentSequence()); |
| 624 | 624 |
| 625 if (!db_.get()) | 625 if (!db_.get()) |
| 626 return; | 626 return; |
| 627 | 627 |
| 628 PrunePendingOperationsForDeletes(server_identifiers); | 628 PrunePendingOperationsForDeletes(server_identifiers); |
| 629 | 629 |
| 630 sql::Statement del_smt(db_->GetCachedStatement( | 630 sql::Statement del_smt(db_->GetCachedStatement( |
| 631 SQL_FROM_HERE, "DELETE FROM channel_id WHERE host=?")); | 631 SQL_FROM_HERE, "DELETE FROM channel_id WHERE host=?")); |
| 632 if (!del_smt.is_valid()) { | 632 if (!del_smt.is_valid()) { |
| 633 LOG(WARNING) << "Unable to delete channel ids."; | 633 LOG(WARNING) << "Unable to delete channel ids."; |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 687 backend_->SetForceKeepSessionState(); | 687 backend_->SetForceKeepSessionState(); |
| 688 } | 688 } |
| 689 | 689 |
| 690 SQLiteChannelIDStore::~SQLiteChannelIDStore() { | 690 SQLiteChannelIDStore::~SQLiteChannelIDStore() { |
| 691 backend_->Close(); | 691 backend_->Close(); |
| 692 // We release our reference to the Backend, though it will probably still have | 692 // We release our reference to the Backend, though it will probably still have |
| 693 // a reference if the background task runner has not run Close() yet. | 693 // a reference if the background task runner has not run Close() yet. |
| 694 } | 694 } |
| 695 | 695 |
| 696 } // namespace net | 696 } // namespace net |
| OLD | NEW |