Linux: Depend on liberation-fonts package for RPMs.
[chromium-blink-merge.git] / content / browser / indexed_db / indexed_db_transaction.h
blob5bec07b00f35e7a010d04b6d06f29deaf0b609c4
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_
8 #include <queue>
9 #include <set>
10 #include <stack>
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/modules/indexeddb/WebIDBTypes.h"
22 namespace content {
24 class BlobWriteCallbackImpl;
25 class IndexedDBCursor;
26 class IndexedDBDatabaseCallbacks;
28 class CONTENT_EXPORT IndexedDBTransaction
29 : public NON_EXPORTED_BASE(base::RefCounted<IndexedDBTransaction>) {
30 public:
31 typedef base::Callback<void(IndexedDBTransaction*)> Operation;
33 enum State {
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
37 // to be written.
38 FINISHED, // Either aborted or committed.
41 virtual void Abort();
42 leveldb::Status Commit();
43 void Abort(const IndexedDBDatabaseError& error);
45 // Called by the transaction coordinator when this transaction is unblocked.
46 void Start();
48 blink::WebIDBTransactionMode mode() const { return mode_; }
49 const std::set<int64>& scope() const { return object_store_ids_; }
51 void ScheduleTask(Operation task) {
52 ScheduleTask(blink::WebIDBTaskTypeNormal, task);
54 void ScheduleTask(blink::WebIDBTaskType, Operation task);
55 void ScheduleAbortTask(Operation abort_task);
56 void RegisterOpenCursor(IndexedDBCursor* cursor);
57 void UnregisterOpenCursor(IndexedDBCursor* cursor);
58 void AddPreemptiveEvent() { pending_preemptive_events_++; }
59 void DidCompletePreemptiveEvent() {
60 pending_preemptive_events_--;
61 DCHECK_GE(pending_preemptive_events_, 0);
63 IndexedDBBackingStore::Transaction* BackingStoreTransaction() {
64 return transaction_.get();
66 int64 id() const { return id_; }
68 IndexedDBDatabase* database() const { return database_.get(); }
69 IndexedDBDatabaseCallbacks* connection() const { return callbacks_.get(); }
71 State state() const { return state_; }
72 bool IsTimeoutTimerRunning() const { return timeout_timer_.IsRunning(); }
74 struct Diagnostics {
75 base::Time creation_time;
76 base::Time start_time;
77 int tasks_scheduled;
78 int tasks_completed;
81 const Diagnostics& diagnostics() const { return diagnostics_; }
83 protected:
84 // Test classes may derive, but most creation should be done via
85 // IndexedDBClassFactory.
86 IndexedDBTransaction(
87 int64 id,
88 scoped_refptr<IndexedDBDatabaseCallbacks> callbacks,
89 const std::set<int64>& object_store_ids,
90 blink::WebIDBTransactionMode mode,
91 IndexedDBDatabase* db,
92 IndexedDBBackingStore::Transaction* backing_store_transaction);
93 virtual ~IndexedDBTransaction();
95 // May be overridden in tests.
96 virtual base::TimeDelta GetInactivityTimeout() const;
98 private:
99 friend class BlobWriteCallbackImpl;
100 friend class IndexedDBClassFactory;
101 friend class base::RefCounted<IndexedDBTransaction>;
103 FRIEND_TEST_ALL_PREFIXES(IndexedDBTransactionTestMode, AbortPreemptive);
104 FRIEND_TEST_ALL_PREFIXES(IndexedDBTransactionTestMode, AbortTasks);
105 FRIEND_TEST_ALL_PREFIXES(IndexedDBTransactionTest, NoTimeoutReadOnly);
106 FRIEND_TEST_ALL_PREFIXES(IndexedDBTransactionTest,
107 SchedulePreemptiveTask);
108 FRIEND_TEST_ALL_PREFIXES(IndexedDBTransactionTestMode,
109 ScheduleNormalTask);
110 FRIEND_TEST_ALL_PREFIXES(IndexedDBTransactionTest, Timeout);
112 void RunTasksIfStarted();
114 bool IsTaskQueueEmpty() const;
115 bool HasPendingTasks() const;
117 void BlobWriteComplete(bool success);
118 void ProcessTaskQueue();
119 void CloseOpenCursors();
120 leveldb::Status CommitPhaseTwo();
121 void Timeout();
123 const int64 id_;
124 const std::set<int64> object_store_ids_;
125 const blink::WebIDBTransactionMode mode_;
127 bool used_;
128 State state_;
129 bool commit_pending_;
130 scoped_refptr<IndexedDBDatabaseCallbacks> callbacks_;
131 scoped_refptr<IndexedDBDatabase> database_;
133 class TaskQueue {
134 public:
135 TaskQueue();
136 ~TaskQueue();
137 bool empty() const { return queue_.empty(); }
138 void push(Operation task) { queue_.push(task); }
139 Operation pop();
140 void clear();
142 private:
143 std::queue<Operation> queue_;
145 DISALLOW_COPY_AND_ASSIGN(TaskQueue);
148 class TaskStack {
149 public:
150 TaskStack();
151 ~TaskStack();
152 bool empty() const { return stack_.empty(); }
153 void push(Operation task) { stack_.push(task); }
154 Operation pop();
155 void clear();
157 private:
158 std::stack<Operation> stack_;
160 DISALLOW_COPY_AND_ASSIGN(TaskStack);
163 TaskQueue task_queue_;
164 TaskQueue preemptive_task_queue_;
165 TaskStack abort_task_stack_;
167 scoped_ptr<IndexedDBBackingStore::Transaction> transaction_;
168 bool backing_store_transaction_begun_;
170 bool should_process_queue_;
171 int pending_preemptive_events_;
173 std::set<IndexedDBCursor*> open_cursors_;
175 // This timer is started after requests have been processed. If no subsequent
176 // requests are processed before the timer fires, assume the script is
177 // unresponsive and abort to unblock the transaction queue.
178 base::OneShotTimer<IndexedDBTransaction> timeout_timer_;
180 Diagnostics diagnostics_;
183 } // namespace content
185 #endif // CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_TRANSACTION_H_