Upstream version 9.38.198.0
[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 #include "third_party/WebKit/public/platform/WebIDBTypes.h"
21
22 namespace content {
23
24 class BlobWriteCallbackImpl;
25 class IndexedDBCursor;
26 class IndexedDBDatabaseCallbacks;
27
28 class CONTENT_EXPORT IndexedDBTransaction
29     : public NON_EXPORTED_BASE(base::RefCounted<IndexedDBTransaction>) {
30  public:
31   typedef base::Callback<void(IndexedDBTransaction*)> Operation;
32
33   IndexedDBTransaction(
34       int64 id,
35       scoped_refptr<IndexedDBDatabaseCallbacks> callbacks,
36       const std::set<int64>& object_store_ids,
37       blink::WebIDBTransactionMode,
38       IndexedDBDatabase* db,
39       IndexedDBBackingStore::Transaction* backing_store_transaction);
40
41   virtual void Abort();
42   leveldb::Status Commit();
43   void Abort(const IndexedDBDatabaseError& error);
44
45   // Called by the transaction coordinator when this transaction is unblocked.
46   void Start();
47
48   blink::WebIDBTransactionMode mode() const { return mode_; }
49   const std::set<int64>& scope() const { return object_store_ids_; }
50
51   void ScheduleTask(Operation task) {
52     ScheduleTask(blink::WebIDBTaskTypeNormal, task);
53   }
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);
62   }
63   IndexedDBBackingStore::Transaction* BackingStoreTransaction() {
64     return transaction_.get();
65   }
66   int64 id() const { return id_; }
67
68   IndexedDBDatabase* database() const { return database_; }
69   IndexedDBDatabaseCallbacks* connection() const { return callbacks_; }
70
71   enum State {
72     CREATED,     // Created, but not yet started by coordinator.
73     STARTED,     // Started by the coordinator.
74     COMMITTING,  // In the process of committing, possibly waiting for blobs
75                  // to be written.
76     FINISHED,    // Either aborted or committed.
77   };
78
79   State state() const { return state_; }
80   bool IsTimeoutTimerRunning() const { return timeout_timer_.IsRunning(); }
81
82   struct Diagnostics {
83     base::Time creation_time;
84     base::Time start_time;
85     int tasks_scheduled;
86     int tasks_completed;
87   };
88
89   const Diagnostics& diagnostics() const { return diagnostics_; }
90
91  private:
92   friend class BlobWriteCallbackImpl;
93
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,
99                            ScheduleNormalTask);
100
101   friend class base::RefCounted<IndexedDBTransaction>;
102   virtual ~IndexedDBTransaction();
103
104   void RunTasksIfStarted();
105
106   bool IsTaskQueueEmpty() const;
107   bool HasPendingTasks() const;
108
109   void BlobWriteComplete(bool success);
110   void ProcessTaskQueue();
111   void CloseOpenCursors();
112   leveldb::Status CommitPhaseTwo();
113   void Timeout();
114
115   const int64 id_;
116   const std::set<int64> object_store_ids_;
117   const blink::WebIDBTransactionMode mode_;
118
119   bool used_;
120   State state_;
121   bool commit_pending_;
122   scoped_refptr<IndexedDBDatabaseCallbacks> callbacks_;
123   scoped_refptr<IndexedDBDatabase> database_;
124
125   class TaskQueue {
126    public:
127     TaskQueue();
128     ~TaskQueue();
129     bool empty() const { return queue_.empty(); }
130     void push(Operation task) { queue_.push(task); }
131     Operation pop();
132     void clear();
133
134    private:
135     std::queue<Operation> queue_;
136
137     DISALLOW_COPY_AND_ASSIGN(TaskQueue);
138   };
139
140   class TaskStack {
141    public:
142     TaskStack();
143     ~TaskStack();
144     bool empty() const { return stack_.empty(); }
145     void push(Operation task) { stack_.push(task); }
146     Operation pop();
147     void clear();
148
149    private:
150     std::stack<Operation> stack_;
151
152     DISALLOW_COPY_AND_ASSIGN(TaskStack);
153   };
154
155   TaskQueue task_queue_;
156   TaskQueue preemptive_task_queue_;
157   TaskStack abort_task_stack_;
158
159   scoped_ptr<IndexedDBBackingStore::Transaction> transaction_;
160   bool backing_store_transaction_begun_;
161
162   bool should_process_queue_;
163   int pending_preemptive_events_;
164
165   std::set<IndexedDBCursor*> open_cursors_;
166
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_;
171
172   Diagnostics diagnostics_;
173 };
174
175 }  // namespace content
176
177 #endif  // CONTENT_BROWSER_INDEXED_DB_INDEXED_DB_TRANSACTION_H_