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