OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "content/browser/indexed_db/indexed_db_transaction_coordinator.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #include "content/browser/indexed_db/indexed_db_transaction.h" |
| 9 |
| 10 namespace content { |
| 11 |
| 12 IndexedDBTransactionCoordinator::IndexedDBTransactionCoordinator() {} |
| 13 |
| 14 IndexedDBTransactionCoordinator::~IndexedDBTransactionCoordinator() { |
| 15 // TODO(jsbell): DCHECK that all queues are empty. http://crbug.com/241821 |
| 16 } |
| 17 |
| 18 void IndexedDBTransactionCoordinator::DidCreateTransaction( |
| 19 IndexedDBTransaction* transaction) { |
| 20 DCHECK(transactions_.find(transaction) == transactions_.end()); |
| 21 transactions_[transaction] = transaction; |
| 22 } |
| 23 |
| 24 void IndexedDBTransactionCoordinator::DidStartTransaction( |
| 25 IndexedDBTransaction* transaction) { |
| 26 DCHECK(transactions_.find(transaction) != transactions_.end()); |
| 27 |
| 28 queued_transactions_.insert(transaction); |
| 29 ProcessStartedTransactions(); |
| 30 } |
| 31 |
| 32 void IndexedDBTransactionCoordinator::DidFinishTransaction( |
| 33 IndexedDBTransaction* transaction) { |
| 34 DCHECK(transactions_.find(transaction) != transactions_.end()); |
| 35 |
| 36 if (queued_transactions_.has(transaction)) { |
| 37 DCHECK(started_transactions_.find(transaction) == |
| 38 started_transactions_.end()); |
| 39 queued_transactions_.erase(transaction); |
| 40 } else { |
| 41 if (started_transactions_.find(transaction) != started_transactions_.end()) |
| 42 started_transactions_.erase(transaction); |
| 43 } |
| 44 transactions_.erase(transaction); |
| 45 |
| 46 ProcessStartedTransactions(); |
| 47 } |
| 48 |
| 49 #ifndef NDEBUG |
| 50 // Verifies internal consistency while returning whether anything is found. |
| 51 bool IndexedDBTransactionCoordinator::IsActive( |
| 52 IndexedDBTransaction* transaction) { |
| 53 bool found = false; |
| 54 if (queued_transactions_.has(transaction)) |
| 55 found = true; |
| 56 if (started_transactions_.find(transaction) != started_transactions_.end()) { |
| 57 DCHECK(!found); |
| 58 found = true; |
| 59 } |
| 60 DCHECK_EQ(found, (transactions_.find(transaction) != transactions_.end())); |
| 61 return found; |
| 62 } |
| 63 #endif |
| 64 |
| 65 void IndexedDBTransactionCoordinator::ProcessStartedTransactions() { |
| 66 if (queued_transactions_.empty()) |
| 67 return; |
| 68 |
| 69 DCHECK(started_transactions_.empty() || |
| 70 (*started_transactions_.begin())->mode() != |
| 71 indexed_db::TRANSACTION_VERSION_CHANGE); |
| 72 |
| 73 list_set<IndexedDBTransaction*>::const_iterator it = |
| 74 queued_transactions_.begin(); |
| 75 while (it != queued_transactions_.end()) { |
| 76 IndexedDBTransaction* transaction = *it; |
| 77 ++it; |
| 78 if (CanRunTransaction(transaction)) { |
| 79 queued_transactions_.erase(transaction); |
| 80 started_transactions_.insert(transaction); |
| 81 transaction->Run(); |
| 82 } |
| 83 } |
| 84 } |
| 85 |
| 86 static bool DoScopesOverlap(const std::set<int64_t>& scope1, |
| 87 const std::set<int64_t>& scope2) { |
| 88 for (std::set<int64_t>::const_iterator it = scope1.begin(); |
| 89 it != scope1.end(); |
| 90 ++it) { |
| 91 if (scope2.find(*it) != scope2.end()) |
| 92 return true; |
| 93 } |
| 94 return false; |
| 95 } |
| 96 |
| 97 bool IndexedDBTransactionCoordinator::CanRunTransaction( |
| 98 IndexedDBTransaction* transaction) { |
| 99 DCHECK(queued_transactions_.has(transaction)); |
| 100 switch (transaction->mode()) { |
| 101 case indexed_db::TRANSACTION_VERSION_CHANGE: |
| 102 DCHECK(queued_transactions_.size() == 1); |
| 103 DCHECK(started_transactions_.empty()); |
| 104 return true; |
| 105 |
| 106 case indexed_db::TRANSACTION_READ_ONLY: |
| 107 return true; |
| 108 |
| 109 case indexed_db::TRANSACTION_READ_WRITE: |
| 110 for (std::set<IndexedDBTransaction*>::const_iterator it = |
| 111 started_transactions_.begin(); |
| 112 it != started_transactions_.end(); |
| 113 ++it) { |
| 114 IndexedDBTransaction* other = *it; |
| 115 if (other->mode() == indexed_db::TRANSACTION_READ_WRITE && |
| 116 DoScopesOverlap(transaction->scope(), other->scope())) |
| 117 return false; |
| 118 } |
| 119 for (list_set<IndexedDBTransaction*>::const_iterator it = |
| 120 queued_transactions_.begin(); |
| 121 *it != transaction; |
| 122 ++it) { |
| 123 DCHECK(it != queued_transactions_.end()); |
| 124 IndexedDBTransaction* other = *it; |
| 125 if (other->mode() == indexed_db::TRANSACTION_READ_WRITE && |
| 126 DoScopesOverlap(transaction->scope(), other->scope())) |
| 127 return false; |
| 128 } |
| 129 return true; |
| 130 } |
| 131 NOTREACHED(); |
| 132 return false; |
| 133 } |
| 134 |
| 135 } // namespace content |
OLD | NEW |