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 #ifndef CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_TRANSACTION_H_ |
| 6 #define CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_TRANSACTION_H_ |
| 7 |
| 8 #include <queue> |
| 9 #include <set> |
| 10 #include <stack> |
| 11 #include <vector> |
| 12 |
| 13 #include "base/memory/ref_counted.h" |
| 14 #include "base/memory/scoped_ptr.h" |
| 15 #include "base/timer.h" |
| 16 #include "content/browser/indexed_db/indexed_db_backing_store.h" |
| 17 #include "content/browser/indexed_db/indexed_db_database.h" |
| 18 #include "content/browser/indexed_db/indexed_db_database_error.h" |
| 19 |
| 20 namespace content { |
| 21 |
| 22 class IndexedDBDatabaseImpl; |
| 23 class IndexedDBCursorImpl; |
| 24 class IndexedDBDatabaseCallbacksWrapper; |
| 25 |
| 26 class IndexedDBTransaction : public base::RefCounted<IndexedDBTransaction> { |
| 27 public: |
| 28 static scoped_refptr<IndexedDBTransaction> Create( |
| 29 int64_t transaction_id, |
| 30 scoped_refptr<IndexedDBDatabaseCallbacksWrapper>, |
| 31 const std::vector<int64_t>& scope, |
| 32 indexed_db::TransactionMode, |
| 33 IndexedDBDatabaseImpl*); |
| 34 |
| 35 virtual void Abort(); |
| 36 void Commit(); |
| 37 |
| 38 class Operation { |
| 39 public: |
| 40 Operation() {} |
| 41 virtual ~Operation() {} |
| 42 virtual void Perform(IndexedDBTransaction* transaction) = 0; |
| 43 }; |
| 44 |
| 45 void Abort(scoped_refptr<IndexedDBDatabaseError>); |
| 46 void Run(); |
| 47 indexed_db::TransactionMode mode() const { return mode_; } |
| 48 const std::set<int64_t>& scope() const { return object_store_ids_; } |
| 49 void ScheduleTask(Operation* task, Operation* abort_task = NULL) { |
| 50 ScheduleTask(IndexedDBDatabase::NORMAL_TASK, task, abort_task); |
| 51 } |
| 52 void ScheduleTask(IndexedDBDatabase::TaskType, |
| 53 Operation* task, |
| 54 Operation* abort_task = NULL); |
| 55 void RegisterOpenCursor(IndexedDBCursorImpl* cursor); |
| 56 void UnregisterOpenCursor(IndexedDBCursorImpl* cursor); |
| 57 void AddPreemptiveEvent() { pending_preemptive_events_++; } |
| 58 void DidCompletePreemptiveEvent() { |
| 59 pending_preemptive_events_--; |
| 60 DCHECK_GE(pending_preemptive_events_, 0); |
| 61 } |
| 62 IndexedDBBackingStore::Transaction* BackingStoreTransaction() { |
| 63 return &transaction_; |
| 64 } |
| 65 int64_t id() const { return id_; } |
| 66 |
| 67 IndexedDBDatabaseImpl* database() const { return database_.get(); } |
| 68 IndexedDBDatabaseCallbacksWrapper* connection() const { |
| 69 return callbacks_.get(); |
| 70 } |
| 71 |
| 72 protected: |
| 73 virtual ~IndexedDBTransaction(); |
| 74 friend class base::RefCounted<IndexedDBTransaction>; |
| 75 |
| 76 private: |
| 77 IndexedDBTransaction(int64_t id, |
| 78 scoped_refptr<IndexedDBDatabaseCallbacksWrapper>, |
| 79 const std::set<int64_t>& object_store_ids, |
| 80 indexed_db::TransactionMode, |
| 81 IndexedDBDatabaseImpl*); |
| 82 |
| 83 enum State { |
| 84 UNUSED, // Created, but no tasks yet. |
| 85 START_PENDING, // Enqueued tasks, but backing store transaction not yet |
| 86 // started. |
| 87 RUNNING, // Backing store transaction started but not yet finished. |
| 88 FINISHED, // Either aborted or committed. |
| 89 }; |
| 90 |
| 91 void Start(); |
| 92 |
| 93 bool IsTaskQueueEmpty() const; |
| 94 bool HasPendingTasks() const; |
| 95 |
| 96 void TaskTimerFired(); |
| 97 void CloseOpenCursors(); |
| 98 |
| 99 const int64_t id_; |
| 100 const std::set<int64_t> object_store_ids_; |
| 101 const indexed_db::TransactionMode mode_; |
| 102 |
| 103 State state_; |
| 104 bool commit_pending_; |
| 105 scoped_refptr<IndexedDBDatabaseCallbacksWrapper> callbacks_; |
| 106 scoped_refptr<IndexedDBDatabaseImpl> database_; |
| 107 |
| 108 class TaskQueue { |
| 109 public: |
| 110 TaskQueue(); |
| 111 ~TaskQueue(); |
| 112 bool empty() const { return queue_.empty(); } |
| 113 void push(Operation* task) { queue_.push(task); } |
| 114 scoped_ptr<Operation> pop(); |
| 115 void clear(); |
| 116 |
| 117 private: |
| 118 std::queue<Operation*> queue_; |
| 119 }; |
| 120 |
| 121 class TaskStack { |
| 122 public: |
| 123 TaskStack(); |
| 124 ~TaskStack(); |
| 125 bool empty() const { return stack_.empty(); } |
| 126 void push(Operation* task) { stack_.push(task); } |
| 127 scoped_ptr<Operation> pop(); |
| 128 void clear(); |
| 129 |
| 130 private: |
| 131 std::stack<Operation*> stack_; |
| 132 }; |
| 133 |
| 134 TaskQueue task_queue_; |
| 135 TaskQueue preemptive_task_queue_; |
| 136 TaskStack abort_task_stack_; |
| 137 |
| 138 IndexedDBBackingStore::Transaction transaction_; |
| 139 |
| 140 base::OneShotTimer<IndexedDBTransaction> task_timer_; |
| 141 int pending_preemptive_events_; |
| 142 |
| 143 std::set<IndexedDBCursorImpl*> open_cursors_; |
| 144 }; |
| 145 |
| 146 } // namespace content |
| 147 |
| 148 #endif // CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_TRANSACTION_H_ |
OLD | NEW |