| 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 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 93 | 93 |
| 94 // Post background delete of all channel ids for |server_identifiers|. | 94 // Post background delete of all channel ids for |server_identifiers|. |
| 95 void DeleteAllInList(const std::list<std::string>& server_identifiers); | 95 void DeleteAllInList(const std::list<std::string>& server_identifiers); |
| 96 | 96 |
| 97 // Commit any pending operations and close the database. This must be called | 97 // Commit any pending operations and close the database. This must be called |
| 98 // before the object is destructed. | 98 // before the object is destructed. |
| 99 void Close(); | 99 void Close(); |
| 100 | 100 |
| 101 void SetForceKeepSessionState(); | 101 void SetForceKeepSessionState(); |
| 102 | 102 |
| 103 // Posts a task to flush pending operations to the database. |
| 104 void Flush(); |
| 105 |
| 103 private: | 106 private: |
| 104 friend class base::RefCountedThreadSafe<SQLiteChannelIDStore::Backend>; | 107 friend class base::RefCountedThreadSafe<SQLiteChannelIDStore::Backend>; |
| 105 | 108 |
| 106 // You should call Close() before destructing this object. | 109 // You should call Close() before destructing this object. |
| 107 virtual ~Backend() { | 110 virtual ~Backend() { |
| 108 DCHECK(!db_.get()) << "Close should have already been called."; | 111 DCHECK(!db_.get()) << "Close should have already been called."; |
| 109 DCHECK_EQ(0u, num_pending_); | 112 DCHECK_EQ(0u, num_pending_); |
| 110 DCHECK(pending_.empty()); | 113 DCHECK(pending_.empty()); |
| 111 } | 114 } |
| 112 | 115 |
| (...skipping 361 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 474 // Perform deletion on background task runner. | 477 // Perform deletion on background task runner. |
| 475 background_task_runner_->PostTask( | 478 background_task_runner_->PostTask( |
| 476 FROM_HERE, | 479 FROM_HERE, |
| 477 base::Bind( | 480 base::Bind( |
| 478 &Backend::BackgroundDeleteAllInList, this, server_identifiers)); | 481 &Backend::BackgroundDeleteAllInList, this, server_identifiers)); |
| 479 } | 482 } |
| 480 | 483 |
| 481 void SQLiteChannelIDStore::Backend::BatchOperation( | 484 void SQLiteChannelIDStore::Backend::BatchOperation( |
| 482 PendingOperation::OperationType op, | 485 PendingOperation::OperationType op, |
| 483 const DefaultChannelIDStore::ChannelID& channel_id) { | 486 const DefaultChannelIDStore::ChannelID& channel_id) { |
| 484 // These thresholds used to be 30 seconds or 512 outstanding operations (the | 487 // Commit every 30 seconds. |
| 485 // same values used in CookieMonster). Since cookies can be bound to Channel | 488 static const int kCommitIntervalMs = 30 * 1000; |
| 486 // IDs, it's possible for a cookie to get committed to the cookie database | 489 // Commit right away if we have more than 512 outstanding operations. |
| 487 // before the Channel ID it is bound to gets committed. Decreasing these | 490 static const size_t kCommitAfterBatchSize = 512; |
| 488 // thresholds increases the chance that the Channel ID will be committed | |
| 489 // before or at the same time as the cookie. | |
| 490 | |
| 491 // Commit every 2 seconds. | |
| 492 static const int kCommitIntervalMs = 2 * 1000; | |
| 493 // Commit right away if we have more than 3 outstanding operations. | |
| 494 static const size_t kCommitAfterBatchSize = 3; | |
| 495 | 491 |
| 496 // We do a full copy of the cert here, and hopefully just here. | 492 // We do a full copy of the cert here, and hopefully just here. |
| 497 std::unique_ptr<PendingOperation> po(new PendingOperation(op, channel_id)); | 493 std::unique_ptr<PendingOperation> po(new PendingOperation(op, channel_id)); |
| 498 | 494 |
| 499 PendingOperationsList::size_type num_pending; | 495 PendingOperationsList::size_type num_pending; |
| 500 { | 496 { |
| 501 base::AutoLock locked(lock_); | 497 base::AutoLock locked(lock_); |
| 502 pending_.push_back(po.release()); | 498 pending_.push_back(po.release()); |
| 503 num_pending = ++num_pending_; | 499 num_pending = ++num_pending_; |
| 504 } | 500 } |
| (...skipping 26 matching lines...) Expand all Loading... |
| 531 if (remove) { | 527 if (remove) { |
| 532 std::unique_ptr<PendingOperation> po(*it); | 528 std::unique_ptr<PendingOperation> po(*it); |
| 533 it = pending_.erase(it); | 529 it = pending_.erase(it); |
| 534 --num_pending_; | 530 --num_pending_; |
| 535 } else { | 531 } else { |
| 536 ++it; | 532 ++it; |
| 537 } | 533 } |
| 538 } | 534 } |
| 539 } | 535 } |
| 540 | 536 |
| 537 void SQLiteChannelIDStore::Backend::Flush() { |
| 538 background_task_runner_->PostTask(FROM_HERE, |
| 539 base::Bind(&Backend::Commit, this)); |
| 540 } |
| 541 |
| 541 void SQLiteChannelIDStore::Backend::Commit() { | 542 void SQLiteChannelIDStore::Backend::Commit() { |
| 542 DCHECK(background_task_runner_->RunsTasksOnCurrentThread()); | 543 DCHECK(background_task_runner_->RunsTasksOnCurrentThread()); |
| 543 | 544 |
| 544 PendingOperationsList ops; | 545 PendingOperationsList ops; |
| 545 { | 546 { |
| 546 base::AutoLock locked(lock_); | 547 base::AutoLock locked(lock_); |
| 547 pending_.swap(ops); | 548 pending_.swap(ops); |
| 548 num_pending_ = 0; | 549 num_pending_ = 0; |
| 549 } | 550 } |
| 550 | 551 |
| (...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 680 | 681 |
| 681 void SQLiteChannelIDStore::DeleteAllInList( | 682 void SQLiteChannelIDStore::DeleteAllInList( |
| 682 const std::list<std::string>& server_identifiers) { | 683 const std::list<std::string>& server_identifiers) { |
| 683 backend_->DeleteAllInList(server_identifiers); | 684 backend_->DeleteAllInList(server_identifiers); |
| 684 } | 685 } |
| 685 | 686 |
| 686 void SQLiteChannelIDStore::SetForceKeepSessionState() { | 687 void SQLiteChannelIDStore::SetForceKeepSessionState() { |
| 687 backend_->SetForceKeepSessionState(); | 688 backend_->SetForceKeepSessionState(); |
| 688 } | 689 } |
| 689 | 690 |
| 691 void SQLiteChannelIDStore::Flush() { |
| 692 backend_->Flush(); |
| 693 } |
| 694 |
| 690 SQLiteChannelIDStore::~SQLiteChannelIDStore() { | 695 SQLiteChannelIDStore::~SQLiteChannelIDStore() { |
| 691 backend_->Close(); | 696 backend_->Close(); |
| 692 // 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 |
| 693 // 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. |
| 694 } | 699 } |
| 695 | 700 |
| 696 } // namespace net | 701 } // namespace net |
| OLD | NEW |