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.
5 #ifndef CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_TRANSACTION_H_
6 #define CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_TRANSACTION_H_
12 #include "base/basictypes.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/time/time.h"
16 #include "base/timer/timer.h"
17 #include "content/browser/indexed_db/indexed_db_backing_store.h"
18 #include "content/browser/indexed_db/indexed_db_database.h"
19 #include "content/browser/indexed_db/indexed_db_database_error.h"
23 class BlobWriteCallbackImpl;
24 class IndexedDBCursor;
25 class IndexedDBDatabaseCallbacks;
27 class CONTENT_EXPORT IndexedDBTransaction
28 : public NON_EXPORTED_BASE(base::RefCounted<IndexedDBTransaction>) {
30 typedef base::Callback<void(IndexedDBTransaction*)> Operation;
34 scoped_refptr<IndexedDBDatabaseCallbacks> callbacks,
35 const std::set<int64>& object_store_ids,
36 indexed_db::TransactionMode,
37 IndexedDBDatabase* db,
38 IndexedDBBackingStore::Transaction* backing_store_transaction);
42 void Abort(const IndexedDBDatabaseError& error);
44 // Called by the transaction coordinator when this transaction is unblocked.
47 indexed_db::TransactionMode mode() const { return mode_; }
48 const std::set<int64>& scope() const { return object_store_ids_; }
50 void ScheduleTask(Operation task) {
51 ScheduleTask(IndexedDBDatabase::NORMAL_TASK, task);
53 void ScheduleTask(IndexedDBDatabase::TaskType, Operation task);
54 void ScheduleAbortTask(Operation abort_task);
55 void RegisterOpenCursor(IndexedDBCursor* cursor);
56 void UnregisterOpenCursor(IndexedDBCursor* cursor);
57 void AddPreemptiveEvent() { pending_preemptive_events_++; }
58 void DidCompletePreemptiveEvent() {
59 pending_preemptive_events_--;
60 DCHECK_GE(pending_preemptive_events_, 0);
62 IndexedDBBackingStore::Transaction* BackingStoreTransaction() {
63 return transaction_.get();
65 int64 id() const { return id_; }
67 IndexedDBDatabase* database() const { return database_; }
68 IndexedDBDatabaseCallbacks* connection() const { return callbacks_; }
71 CREATED, // Created, but not yet started by coordinator.
72 STARTED, // Started by the coordinator.
73 COMMITTING, // In the process of committing, possibly waiting for blobs
75 FINISHED, // Either aborted or committed.
78 State state() const { return state_; }
79 bool IsTimeoutTimerRunning() const { return timeout_timer_.IsRunning(); }
82 base::Time creation_time;
83 base::Time start_time;
88 const Diagnostics& diagnostics() const { return diagnostics_; }
91 friend class BlobWriteCallbackImpl;
93 FRIEND_TEST_ALL_PREFIXES(IndexedDBTransactionTestMode, AbortPreemptive);
94 FRIEND_TEST_ALL_PREFIXES(IndexedDBTransactionTest, Timeout);
95 FRIEND_TEST_ALL_PREFIXES(IndexedDBTransactionTest,
96 SchedulePreemptiveTask);
97 FRIEND_TEST_ALL_PREFIXES(IndexedDBTransactionTestMode,
100 friend class base::RefCounted<IndexedDBTransaction>;
101 virtual ~IndexedDBTransaction();
103 void RunTasksIfStarted();
105 bool IsTaskQueueEmpty() const;
106 bool HasPendingTasks() const;
108 void BlobWriteComplete(bool success);
109 void ProcessTaskQueue();
110 void CloseOpenCursors();
111 void CommitPhaseTwo();
115 const std::set<int64> object_store_ids_;
116 const indexed_db::TransactionMode mode_;
120 bool commit_pending_;
121 scoped_refptr<IndexedDBDatabaseCallbacks> callbacks_;
122 scoped_refptr<IndexedDBDatabase> database_;
128 bool empty() const { return queue_.empty(); }
129 void push(Operation task) { queue_.push(task); }
134 std::queue<Operation> queue_;
136 DISALLOW_COPY_AND_ASSIGN(TaskQueue);
143 bool empty() const { return stack_.empty(); }
144 void push(Operation task) { stack_.push(task); }
149 std::stack<Operation> stack_;
151 DISALLOW_COPY_AND_ASSIGN(TaskStack);
154 TaskQueue task_queue_;
155 TaskQueue preemptive_task_queue_;
156 TaskStack abort_task_stack_;
158 scoped_ptr<IndexedDBBackingStore::Transaction> transaction_;
159 bool backing_store_transaction_begun_;
161 bool should_process_queue_;
162 int pending_preemptive_events_;
164 std::set<IndexedDBCursor*> open_cursors_;
166 // This timer is started after requests have been processed. If no subsequent
167 // requests are processed before the timer fires, assume the script is
168 // unresponsive and abort to unblock the transaction queue.
169 base::OneShotTimer<IndexedDBTransaction> timeout_timer_;
171 Diagnostics diagnostics_;
174 } // namespace content
176 #endif // CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_TRANSACTION_H_