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"
20 #include "third_party/WebKit/public/platform/WebIDBTypes.h"
24 class BlobWriteCallbackImpl
;
25 class IndexedDBCursor
;
26 class IndexedDBDatabaseCallbacks
;
28 class CONTENT_EXPORT IndexedDBTransaction
29 : public NON_EXPORTED_BASE(base::RefCounted
<IndexedDBTransaction
>) {
31 typedef base::Callback
<void(IndexedDBTransaction
*)> Operation
;
34 CREATED
, // Created, but not yet started by coordinator.
35 STARTED
, // Started by the coordinator.
36 COMMITTING
, // In the process of committing, possibly waiting for blobs
38 FINISHED
, // Either aborted or committed.
43 scoped_refptr
<IndexedDBDatabaseCallbacks
> callbacks
,
44 const std::set
<int64
>& object_store_ids
,
45 blink::WebIDBTransactionMode
,
46 IndexedDBDatabase
* db
,
47 IndexedDBBackingStore::Transaction
* backing_store_transaction
);
50 leveldb::Status
Commit();
51 void Abort(const IndexedDBDatabaseError
& error
);
53 // Called by the transaction coordinator when this transaction is unblocked.
56 blink::WebIDBTransactionMode
mode() const { return mode_
; }
57 const std::set
<int64
>& scope() const { return object_store_ids_
; }
59 void ScheduleTask(Operation task
) {
60 ScheduleTask(blink::WebIDBTaskTypeNormal
, task
);
62 void ScheduleTask(blink::WebIDBTaskType
, Operation task
);
63 void ScheduleAbortTask(Operation abort_task
);
64 void RegisterOpenCursor(IndexedDBCursor
* cursor
);
65 void UnregisterOpenCursor(IndexedDBCursor
* cursor
);
66 void AddPreemptiveEvent() { pending_preemptive_events_
++; }
67 void DidCompletePreemptiveEvent() {
68 pending_preemptive_events_
--;
69 DCHECK_GE(pending_preemptive_events_
, 0);
71 IndexedDBBackingStore::Transaction
* BackingStoreTransaction() {
72 return transaction_
.get();
74 int64
id() const { return id_
; }
76 IndexedDBDatabase
* database() const { return database_
.get(); }
77 IndexedDBDatabaseCallbacks
* connection() const { return callbacks_
.get(); }
79 State
state() const { return state_
; }
80 bool IsTimeoutTimerRunning() const { return timeout_timer_
.IsRunning(); }
83 base::Time creation_time
;
84 base::Time start_time
;
89 const Diagnostics
& diagnostics() const { return diagnostics_
; }
92 friend class BlobWriteCallbackImpl
;
94 FRIEND_TEST_ALL_PREFIXES(IndexedDBTransactionTestMode
, AbortPreemptive
);
95 FRIEND_TEST_ALL_PREFIXES(IndexedDBTransactionTest
, Timeout
);
96 FRIEND_TEST_ALL_PREFIXES(IndexedDBTransactionTest
,
97 SchedulePreemptiveTask
);
98 FRIEND_TEST_ALL_PREFIXES(IndexedDBTransactionTestMode
,
101 friend class base::RefCounted
<IndexedDBTransaction
>;
102 virtual ~IndexedDBTransaction();
104 void RunTasksIfStarted();
106 bool IsTaskQueueEmpty() const;
107 bool HasPendingTasks() const;
109 void BlobWriteComplete(bool success
);
110 void ProcessTaskQueue();
111 void CloseOpenCursors();
112 leveldb::Status
CommitPhaseTwo();
116 const std::set
<int64
> object_store_ids_
;
117 const blink::WebIDBTransactionMode mode_
;
121 bool commit_pending_
;
122 scoped_refptr
<IndexedDBDatabaseCallbacks
> callbacks_
;
123 scoped_refptr
<IndexedDBDatabase
> database_
;
129 bool empty() const { return queue_
.empty(); }
130 void push(Operation task
) { queue_
.push(task
); }
135 std::queue
<Operation
> queue_
;
137 DISALLOW_COPY_AND_ASSIGN(TaskQueue
);
144 bool empty() const { return stack_
.empty(); }
145 void push(Operation task
) { stack_
.push(task
); }
150 std::stack
<Operation
> stack_
;
152 DISALLOW_COPY_AND_ASSIGN(TaskStack
);
155 TaskQueue task_queue_
;
156 TaskQueue preemptive_task_queue_
;
157 TaskStack abort_task_stack_
;
159 scoped_ptr
<IndexedDBBackingStore::Transaction
> transaction_
;
160 bool backing_store_transaction_begun_
;
162 bool should_process_queue_
;
163 int pending_preemptive_events_
;
165 std::set
<IndexedDBCursor
*> open_cursors_
;
167 // This timer is started after requests have been processed. If no subsequent
168 // requests are processed before the timer fires, assume the script is
169 // unresponsive and abort to unblock the transaction queue.
170 base::OneShotTimer
<IndexedDBTransaction
> timeout_timer_
;
172 Diagnostics diagnostics_
;
175 } // namespace content
177 #endif // CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_TRANSACTION_H_