2 * Copyright (C) 2010 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
26 #ifndef IDBTransactionBackend_h
27 #define IDBTransactionBackend_h
29 #if ENABLE(INDEXED_DATABASE)
31 #include "IDBBackingStoreInterface.h"
32 #include "IDBDatabaseBackendImpl.h"
33 #include "IDBDatabaseBackendInterface.h"
34 #include "IDBDatabaseError.h"
35 #include "IDBOperation.h"
36 #include "IDBTransactionBackend.h"
38 #include <wtf/Deque.h>
39 #include <wtf/HashSet.h>
40 #include <wtf/RefPtr.h>
44 class IDBDatabaseCallbacks;
46 class IDBTransactionBackend : public RefCounted<IDBTransactionBackend> {
48 static PassRefPtr<IDBTransactionBackend> create(IDBDatabaseBackendImpl*, int64_t transactionId, PassRefPtr<IDBDatabaseCallbacks>, const Vector<int64_t>& objectStoreIds, IndexedDB::TransactionMode);
49 ~IDBTransactionBackend();
53 void abort(PassRefPtr<IDBDatabaseError>);
56 IndexedDB::TransactionMode mode() const { return m_mode; }
57 const HashSet<int64_t>& scope() const { return m_objectStoreIds; }
59 void scheduleTask(PassOwnPtr<IDBOperation> task, PassOwnPtr<IDBOperation> abortTask = nullptr) { scheduleTask(IDBDatabaseBackendInterface::NormalTask, task, abortTask); }
60 void scheduleTask(IDBDatabaseBackendInterface::TaskType, PassOwnPtr<IDBOperation>, PassOwnPtr<IDBOperation> abortTask = nullptr);
62 void registerOpenCursor(IDBCursorBackend*);
63 void unregisterOpenCursor(IDBCursorBackend*);
65 void addPreemptiveEvent() { m_pendingPreemptiveEvents++; }
66 void didCompletePreemptiveEvent() { m_pendingPreemptiveEvents--; ASSERT(m_pendingPreemptiveEvents >= 0); }
67 IDBBackingStoreTransactionInterface& backingStoreTransaction() { return *m_backingStoreTransaction; }
69 IDBDatabaseBackendInterface& database() const { return *m_database; }
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>, IDBDatabaseBackendInterface::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, IDBDatabaseBackendInterface::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>);
84 PassRefPtr<IDBCursorBackend> createCursorBackend(IDBBackingStoreCursorInterface&, IndexedDB::CursorType, IDBDatabaseBackendInterface::TaskType, int64_t objectStoreId);
86 int64_t id() const { return m_id; }
89 IDBTransactionBackend(IDBDatabaseBackendImpl*, int64_t id, PassRefPtr<IDBDatabaseCallbacks>, const HashSet<int64_t>& objectStoreIds, IndexedDB::TransactionMode);
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.
100 bool isTaskQueueEmpty() const;
101 bool hasPendingTasks() const;
103 void taskTimerFired(Timer<IDBTransactionBackend>*);
104 void closeOpenCursors();
106 const HashSet<int64_t> m_objectStoreIds;
107 const IndexedDB::TransactionMode m_mode;
110 bool m_commitPending;
111 RefPtr<IDBDatabaseCallbacks> m_callbacks;
112 RefPtr<IDBDatabaseBackendImpl> m_database;
114 typedef Deque<OwnPtr<IDBOperation>> TaskQueue;
115 TaskQueue m_taskQueue;
116 TaskQueue m_preemptiveTaskQueue;
117 TaskQueue m_abortTaskQueue;
119 std::unique_ptr<IDBBackingStoreTransactionInterface> m_backingStoreTransaction;
121 // FIXME: delete the timer once we have threads instead.
122 Timer<IDBTransactionBackend> m_taskTimer;
123 int m_pendingPreemptiveEvents;
125 HashSet<IDBCursorBackend*> m_openCursors;
127 RefPtr<IDBBackingStoreInterface> m_backingStore;
132 } // namespace WebCore
134 #endif // ENABLE(INDEXED_DATABASE)
136 #endif // IDBTransactionBackend_h