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 IndexedDBCursor
;
24 class IndexedDBDatabaseCallbacks
;
26 class CONTENT_EXPORT IndexedDBTransaction
27 : public NON_EXPORTED_BASE(base::RefCounted
<IndexedDBTransaction
>) {
29 typedef base::Callback
<void(IndexedDBTransaction
*)> Operation
;
33 scoped_refptr
<IndexedDBDatabaseCallbacks
> callbacks
,
34 const std::set
<int64
>& object_store_ids
,
35 indexed_db::TransactionMode
,
36 IndexedDBDatabase
* db
,
37 IndexedDBBackingStore::Transaction
* backing_store_transaction
);
41 void Abort(const IndexedDBDatabaseError
& error
);
43 // Called by the transaction coordinator when this transaction is unblocked.
46 indexed_db::TransactionMode
mode() const { return mode_
; }
47 const std::set
<int64
>& scope() const { return object_store_ids_
; }
49 void ScheduleTask(Operation task
) {
50 ScheduleTask(IndexedDBDatabase::NORMAL_TASK
, task
);
52 void ScheduleTask(Operation task
, Operation abort_task
);
53 void ScheduleTask(IndexedDBDatabase::TaskType
, Operation task
);
54 void RegisterOpenCursor(IndexedDBCursor
* cursor
);
55 void UnregisterOpenCursor(IndexedDBCursor
* cursor
);
56 void AddPreemptiveEvent() { pending_preemptive_events_
++; }
57 void DidCompletePreemptiveEvent() {
58 pending_preemptive_events_
--;
59 DCHECK_GE(pending_preemptive_events_
, 0);
61 IndexedDBBackingStore::Transaction
* BackingStoreTransaction() {
62 return transaction_
.get();
64 int64
id() const { return id_
; }
66 IndexedDBDatabase
* database() const { return database_
; }
67 IndexedDBDatabaseCallbacks
* connection() const { return callbacks_
; }
70 CREATED
, // Created, but not yet started by coordinator.
71 STARTED
, // Started by the coordinator.
72 FINISHED
, // Either aborted or committed.
75 State
state() const { return state_
; }
76 bool IsTimeoutTimerRunning() const { return timeout_timer_
.IsRunning(); }
79 base::Time creation_time
;
80 base::Time start_time
;
85 const Diagnostics
& diagnostics() const { return diagnostics_
; }
88 FRIEND_TEST_ALL_PREFIXES(IndexedDBTransactionTestMode
, AbortPreemptive
);
89 FRIEND_TEST_ALL_PREFIXES(IndexedDBTransactionTest
, Timeout
);
90 FRIEND_TEST_ALL_PREFIXES(IndexedDBTransactionTest
,
91 SchedulePreemptiveTask
);
92 FRIEND_TEST_ALL_PREFIXES(IndexedDBTransactionTestMode
,
95 friend class base::RefCounted
<IndexedDBTransaction
>;
96 virtual ~IndexedDBTransaction();
98 void RunTasksIfStarted();
100 bool IsTaskQueueEmpty() const;
101 bool HasPendingTasks() const;
103 void ProcessTaskQueue();
104 void CloseOpenCursors();
108 const std::set
<int64
> object_store_ids_
;
109 const indexed_db::TransactionMode mode_
;
113 bool commit_pending_
;
114 scoped_refptr
<IndexedDBDatabaseCallbacks
> callbacks_
;
115 scoped_refptr
<IndexedDBDatabase
> database_
;
121 bool empty() const { return queue_
.empty(); }
122 void push(Operation task
) { queue_
.push(task
); }
127 std::queue
<Operation
> queue_
;
134 bool empty() const { return stack_
.empty(); }
135 void push(Operation task
) { stack_
.push(task
); }
140 std::stack
<Operation
> stack_
;
143 TaskQueue task_queue_
;
144 TaskQueue preemptive_task_queue_
;
145 TaskStack abort_task_stack_
;
147 scoped_ptr
<IndexedDBBackingStore::Transaction
> transaction_
;
148 bool backing_store_transaction_begun_
;
150 bool should_process_queue_
;
151 int pending_preemptive_events_
;
153 std::set
<IndexedDBCursor
*> open_cursors_
;
155 // This timer is started after requests have been processed. If no subsequent
156 // requests are processed before the timer fires, assume the script is
157 // unresponsive and abort to unblock the transaction queue.
158 base::OneShotTimer
<IndexedDBTransaction
> timeout_timer_
;
160 Diagnostics diagnostics_
;
163 } // namespace content
165 #endif // CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_TRANSACTION_H_