21070f61fedaf190361b20b10341a1be4afdf918
[WebKit-https.git] / Source / WebCore / Modules / indexeddb / IDBTransactionBackend.h
1 /*
2  * Copyright (C) 2010 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1.  Redistributions of source code must retain the above copyright
9  *     notice, this list of conditions and the following disclaimer.
10  * 2.  Redistributions in binary form must reproduce the above copyright
11  *     notice, this list of conditions and the following disclaimer in the
12  *     documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
15  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
18  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #ifndef IDBTransactionBackend_h
27 #define IDBTransactionBackend_h
28
29 #if ENABLE(INDEXED_DATABASE)
30
31 #include "IDBBackingStoreInterface.h"
32 #include "IDBDatabaseBackend.h"
33 #include "IDBDatabaseError.h"
34 #include "IDBOperation.h"
35 #include "IDBTransactionBackend.h"
36 #include "Timer.h"
37 #include <wtf/Deque.h>
38 #include <wtf/HashSet.h>
39 #include <wtf/RefPtr.h>
40
41 namespace WebCore {
42
43 class IDBCursorBackend;
44 class IDBDatabaseCallbacks;
45
46 class IDBTransactionBackend : public RefCounted<IDBTransactionBackend> {
47 public:
48     static PassRefPtr<IDBTransactionBackend> create(IDBDatabaseBackend*, int64_t transactionId, PassRefPtr<IDBDatabaseCallbacks>, const Vector<int64_t>& objectStoreIds, IndexedDB::TransactionMode);
49     ~IDBTransactionBackend();
50
51     void commit();
52     void abort();
53     void abort(PassRefPtr<IDBDatabaseError>);
54
55     void run();
56     IndexedDB::TransactionMode mode() const  { return m_mode; }
57     const HashSet<int64_t>& scope() const  { return m_objectStoreIds; }
58
59     void scheduleTask(PassRefPtr<IDBOperation> task, PassRefPtr<IDBSynchronousOperation> abortTask = nullptr) { scheduleTask(IDBDatabaseBackend::NormalTask, task, abortTask); }
60     void scheduleTask(IDBDatabaseBackend::TaskType, PassRefPtr<IDBOperation>, PassRefPtr<IDBSynchronousOperation> abortTask = nullptr);
61
62     void registerOpenCursor(IDBCursorBackend*);
63     void unregisterOpenCursor(IDBCursorBackend*);
64
65     void addPreemptiveEvent()  { m_pendingPreemptiveEvents++; }
66     void didCompletePreemptiveEvent()  { m_pendingPreemptiveEvents--; ASSERT(m_pendingPreemptiveEvents >= 0); }
67     IDBBackingStoreTransactionInterface& backingStoreTransaction() { return *m_backingStoreTransaction; }
68
69     IDBDatabaseBackend& database() const  { return *m_database; }
70
71     void scheduleCreateObjectStoreOperation(const IDBObjectStoreMetadata&);
72     void scheduleDeleteObjectStoreOperation(const IDBObjectStoreMetadata&);
73     void scheduleVersionChangeOperation(int64_t transactionId, int64_t requestedVersion, PassRefPtr<IDBCallbacks>, PassRefPtr<IDBDatabaseCallbacks>, const IDBDatabaseMetadata&);
74     void scheduleCreateIndexOperation(int64_t objectStoreId, const IDBIndexMetadata&);
75     void scheduleDeleteIndexOperation(int64_t objectStoreId, const IDBIndexMetadata&);
76     void scheduleGetOperation(const IDBDatabaseMetadata&, int64_t objectStoreId, int64_t indexId, PassRefPtr<IDBKeyRange>, IndexedDB::CursorType, PassRefPtr<IDBCallbacks>);
77     void schedulePutOperation(const IDBObjectStoreMetadata&, PassRefPtr<SharedBuffer> value, PassRefPtr<IDBKey>, IDBDatabaseBackend::PutMode, PassRefPtr<IDBCallbacks>, const Vector<int64_t>& indexIds, const Vector<IndexKeys>&);
78     void scheduleSetIndexesReadyOperation(size_t indexCount);
79     void scheduleOpenCursorOperation(int64_t objectStoreId, int64_t indexId, PassRefPtr<IDBKeyRange>, IndexedDB::CursorDirection, IndexedDB::CursorType, IDBDatabaseBackend::TaskType, PassRefPtr<IDBCallbacks>);
80     void scheduleCountOperation(int64_t objectStoreId, int64_t indexId, PassRefPtr<IDBKeyRange>, PassRefPtr<IDBCallbacks>);
81     void scheduleDeleteRangeOperation(int64_t objectStoreId, PassRefPtr<IDBKeyRange>, PassRefPtr<IDBCallbacks>);
82     void scheduleClearOperation(int64_t objectStoreId, PassRefPtr<IDBCallbacks>);
83
84     PassRefPtr<IDBCursorBackend> createCursorBackend(IDBBackingStoreCursorInterface&, IndexedDB::CursorType, IDBDatabaseBackend::TaskType, int64_t objectStoreId);
85
86     int64_t id() const { return m_id; }
87
88 private:
89     IDBTransactionBackend(IDBDatabaseBackend*, int64_t id, PassRefPtr<IDBDatabaseCallbacks>, const HashSet<int64_t>& objectStoreIds, IndexedDB::TransactionMode);
90
91     enum State {
92         Unused, // Created, but no tasks yet.
93         StartPending, // Enqueued tasks, but backing store transaction not yet started.
94         Running, // Backing store transaction started but not yet finished.
95         Finished, // Either aborted or committed.
96     };
97
98     void start();
99
100     bool isTaskQueueEmpty() const;
101     bool hasPendingTasks() const;
102
103     void taskTimerFired(Timer<IDBTransactionBackend>*);
104     void closeOpenCursors();
105
106     const HashSet<int64_t> m_objectStoreIds;
107     const IndexedDB::TransactionMode m_mode;
108
109     State m_state;
110     bool m_commitPending;
111     RefPtr<IDBDatabaseCallbacks> m_callbacks;
112     RefPtr<IDBDatabaseBackend> m_database;
113
114     typedef Deque<RefPtr<IDBOperation>> TaskQueue;
115     TaskQueue m_taskQueue;
116     TaskQueue m_preemptiveTaskQueue;
117     Deque<RefPtr<IDBSynchronousOperation>> m_abortTaskQueue;
118
119     RefPtr<IDBBackingStoreTransactionInterface> m_backingStoreTransaction;
120
121     // FIXME: delete the timer once we have threads instead.
122     Timer<IDBTransactionBackend> m_taskTimer;
123     int m_pendingPreemptiveEvents;
124
125     HashSet<IDBCursorBackend*> m_openCursors;
126
127     int64_t m_id;
128 };
129
130 } // namespace WebCore
131
132 #endif // ENABLE(INDEXED_DATABASE)
133
134 #endif // IDBTransactionBackend_h