6c696dbf8ed7d0078d9ec803da28383275a4c9b1
[platform/framework/web/crosswalk.git] / src / content / browser / indexed_db / indexed_db_transaction.h
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
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
21 namespace content {
22
23 class IndexedDBCursor;
24 class IndexedDBDatabaseCallbacks;
25
26 class CONTENT_EXPORT IndexedDBTransaction
27     : public NON_EXPORTED_BASE(base::RefCounted<IndexedDBTransaction>) {
28  public:
29   typedef base::Callback<void(IndexedDBTransaction*)> Operation;
30
31   IndexedDBTransaction(
32       int64 id,
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);
38
39   virtual void Abort();
40   void Commit();
41   void Abort(const IndexedDBDatabaseError& error);
42
43   // Called by the transaction coordinator when this transaction is unblocked.
44   void Start();
45
46   indexed_db::TransactionMode mode() const { return mode_; }
47   const std::set<int64>& scope() const { return object_store_ids_; }
48
49   void ScheduleTask(Operation task) {
50     ScheduleTask(IndexedDBDatabase::NORMAL_TASK, task);
51   }
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);
60   }
61   IndexedDBBackingStore::Transaction* BackingStoreTransaction() {
62     return transaction_.get();
63   }
64   int64 id() const { return id_; }
65
66   IndexedDBDatabase* database() const { return database_; }
67   IndexedDBDatabaseCallbacks* connection() const { return callbacks_; }
68
69   enum State {
70     CREATED,   // Created, but not yet started by coordinator.
71     STARTED,   // Started by the coordinator.
72     FINISHED,  // Either aborted or committed.
73   };
74
75   State state() const { return state_; }
76   bool IsTimeoutTimerRunning() const { return timeout_timer_.IsRunning(); }
77
78   struct Diagnostics {
79     base::Time creation_time;
80     base::Time start_time;
81     int tasks_scheduled;
82     int tasks_completed;
83   };
84
85   const Diagnostics& diagnostics() const { return diagnostics_; }
86
87  private:
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,
93                            ScheduleNormalTask);
94
95   friend class base::RefCounted<IndexedDBTransaction>;
96   virtual ~IndexedDBTransaction();
97
98   void RunTasksIfStarted();
99
100   bool IsTaskQueueEmpty() const;
101   bool HasPendingTasks() const;
102
103   void ProcessTaskQueue();
104   void CloseOpenCursors();
105   void Timeout();
106
107   const int64 id_;
108   const std::set<int64> object_store_ids_;
109   const indexed_db::TransactionMode mode_;
110
111   bool used_;
112   State state_;
113   bool commit_pending_;
114   scoped_refptr<IndexedDBDatabaseCallbacks> callbacks_;
115   scoped_refptr<IndexedDBDatabase> database_;
116
117   class TaskQueue {
118    public:
119     TaskQueue();
120     ~TaskQueue();
121     bool empty() const { return queue_.empty(); }
122     void push(Operation task) { queue_.push(task); }
123     Operation pop();
124     void clear();
125
126    private:
127     std::queue<Operation> queue_;
128   };
129
130   class TaskStack {
131    public:
132     TaskStack();
133     ~TaskStack();
134     bool empty() const { return stack_.empty(); }
135     void push(Operation task) { stack_.push(task); }
136     Operation pop();
137     void clear();
138
139    private:
140     std::stack<Operation> stack_;
141   };
142
143   TaskQueue task_queue_;
144   TaskQueue preemptive_task_queue_;
145   TaskStack abort_task_stack_;
146
147   scoped_ptr<IndexedDBBackingStore::Transaction> transaction_;
148   bool backing_store_transaction_begun_;
149
150   bool should_process_queue_;
151   int pending_preemptive_events_;
152
153   std::set<IndexedDBCursor*> open_cursors_;
154
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_;
159
160   Diagnostics diagnostics_;
161 };
162
163 }  // namespace content
164
165 #endif  // CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_TRANSACTION_H_