Refactor data passed along for a "GetRecord" request.
[WebKit-https.git] / Source / WebCore / Modules / indexeddb / server / UniqueIDBDatabase.h
1 /*
2  * Copyright (C) 2015 Apple 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  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #pragma once
27
28 #if ENABLE(INDEXED_DATABASE)
29
30 #include "IDBBackingStore.h"
31 #include "IDBBindingUtilities.h"
32 #include "IDBDatabaseIdentifier.h"
33 #include "IDBDatabaseInfo.h"
34 #include "IDBGetResult.h"
35 #include "ServerOpenDBRequest.h"
36 #include "ThreadSafeDataBuffer.h"
37 #include "Timer.h"
38 #include "UniqueIDBDatabaseConnection.h"
39 #include "UniqueIDBDatabaseTransaction.h"
40 #include <wtf/CrossThreadQueue.h>
41 #include <wtf/CrossThreadTask.h>
42 #include <wtf/Deque.h>
43 #include <wtf/HashCountedSet.h>
44 #include <wtf/HashSet.h>
45 #include <wtf/ListHashSet.h>
46 #include <wtf/MessageQueue.h>
47 #include <wtf/Ref.h>
48 #include <wtf/ThreadSafeRefCounted.h>
49
50 namespace JSC {
51 class VM;
52 }
53
54 namespace WebCore {
55
56 class IDBError;
57 class IDBRequestData;
58 class IDBTransactionInfo;
59
60 namespace IndexedDB {
61 enum class IndexRecordType;
62 }
63
64 namespace IDBServer {
65
66 class IDBConnectionToClient;
67 class IDBServer;
68
69 typedef std::function<void(const IDBError&)> ErrorCallback;
70 typedef std::function<void(const IDBError&, const IDBKeyData&)> KeyDataCallback;
71 typedef std::function<void(const IDBError&, const IDBGetResult&)> GetResultCallback;
72 typedef std::function<void(const IDBError&, uint64_t)> CountCallback;
73
74 class UniqueIDBDatabase : public ThreadSafeRefCounted<UniqueIDBDatabase> {
75 public:
76     static Ref<UniqueIDBDatabase> create(IDBServer& server, const IDBDatabaseIdentifier& identifier)
77     {
78         return adoptRef(*new UniqueIDBDatabase(server, identifier));
79     }
80
81     WEBCORE_EXPORT ~UniqueIDBDatabase();
82
83     void openDatabaseConnection(IDBConnectionToClient&, const IDBRequestData&);
84
85     const IDBDatabaseInfo& info() const;
86     IDBServer& server() { return m_server; }
87     const IDBDatabaseIdentifier& identifier() const { return m_identifier; }
88
89     void createObjectStore(UniqueIDBDatabaseTransaction&, const IDBObjectStoreInfo&, ErrorCallback);
90     void deleteObjectStore(UniqueIDBDatabaseTransaction&, const String& objectStoreName, ErrorCallback);
91     void clearObjectStore(UniqueIDBDatabaseTransaction&, uint64_t objectStoreIdentifier, ErrorCallback);
92     void createIndex(UniqueIDBDatabaseTransaction&, const IDBIndexInfo&, ErrorCallback);
93     void deleteIndex(UniqueIDBDatabaseTransaction&, uint64_t objectStoreIdentifier, const String& indexName, ErrorCallback);
94     void putOrAdd(const IDBRequestData&, const IDBKeyData&, const IDBValue&, IndexedDB::ObjectStoreOverwriteMode, KeyDataCallback);
95     void getRecord(const IDBRequestData&, const IDBGetRecordData&, GetResultCallback);
96     void getCount(const IDBRequestData&, const IDBKeyRangeData&, CountCallback);
97     void deleteRecord(const IDBRequestData&, const IDBKeyRangeData&, ErrorCallback);
98     void openCursor(const IDBRequestData&, const IDBCursorInfo&, GetResultCallback);
99     void iterateCursor(const IDBRequestData&, const IDBKeyData&, unsigned long count, GetResultCallback);
100     void commitTransaction(UniqueIDBDatabaseTransaction&, ErrorCallback);
101     void abortTransaction(UniqueIDBDatabaseTransaction&, ErrorCallback);
102     void didFinishHandlingVersionChange(UniqueIDBDatabaseConnection&, const IDBResourceIdentifier& transactionIdentifier);
103     void transactionDestroyed(UniqueIDBDatabaseTransaction&);
104     void connectionClosedFromClient(UniqueIDBDatabaseConnection&);
105     void confirmConnectionClosedOnServer(UniqueIDBDatabaseConnection&);
106     void didFireVersionChangeEvent(UniqueIDBDatabaseConnection&, const IDBResourceIdentifier& requestIdentifier);
107     void openDBRequestCancelled(const IDBResourceIdentifier& requestIdentifier);
108     void confirmDidCloseFromServer(UniqueIDBDatabaseConnection&);
109
110     void enqueueTransaction(Ref<UniqueIDBDatabaseTransaction>&&);
111
112     void handleDelete(IDBConnectionToClient&, const IDBRequestData&);
113     void immediateCloseForUserDelete();
114
115     static JSC::VM& databaseThreadVM();
116     static JSC::ExecState& databaseThreadExecState();
117
118     bool hardClosedForUserDelete() const { return m_hardClosedForUserDelete; }
119
120 private:
121     UniqueIDBDatabase(IDBServer&, const IDBDatabaseIdentifier&);
122     
123     void handleDatabaseOperations();
124     void handleCurrentOperation();
125     void performCurrentOpenOperation();
126     void performCurrentDeleteOperation();
127     void addOpenDatabaseConnection(Ref<UniqueIDBDatabaseConnection>&&);
128     bool hasAnyOpenConnections() const;
129
130     void startVersionChangeTransaction();
131     void maybeNotifyConnectionsOfVersionChange();
132     void notifyCurrentRequestConnectionClosedOrFiredVersionChangeEvent(uint64_t connectionIdentifier);
133     bool isVersionChangeInProgress();
134
135     void activateTransactionInBackingStore(UniqueIDBDatabaseTransaction&);
136     void transactionCompleted(RefPtr<UniqueIDBDatabaseTransaction>&&);
137
138     void connectionClosedFromServer(UniqueIDBDatabaseConnection&);
139
140     // Database thread operations
141     void deleteBackingStore(const IDBDatabaseIdentifier&);
142     void openBackingStore(const IDBDatabaseIdentifier&);
143     void performCommitTransaction(uint64_t callbackIdentifier, const IDBResourceIdentifier& transactionIdentifier);
144     void performAbortTransaction(uint64_t callbackIdentifier, const IDBResourceIdentifier& transactionIdentifier);
145     void beginTransactionInBackingStore(const IDBTransactionInfo&);
146     void performCreateObjectStore(uint64_t callbackIdentifier, const IDBResourceIdentifier& transactionIdentifier, const IDBObjectStoreInfo&);
147     void performDeleteObjectStore(uint64_t callbackIdentifier, const IDBResourceIdentifier& transactionIdentifier, uint64_t objectStoreIdentifier);
148     void performClearObjectStore(uint64_t callbackIdentifier, const IDBResourceIdentifier& transactionIdentifier, uint64_t objectStoreIdentifier);
149     void performCreateIndex(uint64_t callbackIdentifier, const IDBResourceIdentifier& transactionIdentifier, const IDBIndexInfo&);
150     void performDeleteIndex(uint64_t callbackIdentifier, const IDBResourceIdentifier& transactionIdentifier, uint64_t objectStoreIdentifier, uint64_t indexIdentifier);
151     void performPutOrAdd(uint64_t callbackIdentifier, const IDBResourceIdentifier& transactionIdentifier, uint64_t objectStoreIdentifier, const IDBKeyData&, const IDBValue&, IndexedDB::ObjectStoreOverwriteMode);
152     void performGetRecord(uint64_t callbackIdentifier, const IDBResourceIdentifier& transactionIdentifier, uint64_t objectStoreIdentifier, const IDBKeyRangeData&);
153     void performGetIndexRecord(uint64_t callbackIdentifier, const IDBResourceIdentifier& transactionIdentifier, uint64_t objectStoreIdentifier, uint64_t indexIdentifier, IndexedDB::IndexRecordType, const IDBKeyRangeData&);
154     void performGetCount(uint64_t callbackIdentifier, const IDBResourceIdentifier& transactionIdentifier, uint64_t objectStoreIdentifier, uint64_t indexIdentifier, const IDBKeyRangeData&);
155     void performDeleteRecord(uint64_t callbackIdentifier, const IDBResourceIdentifier& transactionIdentifier, uint64_t objectStoreIdentifier, const IDBKeyRangeData&);
156     void performOpenCursor(uint64_t callbackIdentifier, const IDBResourceIdentifier& transactionIdentifier, const IDBCursorInfo&);
157     void performIterateCursor(uint64_t callbackIdentifier, const IDBResourceIdentifier& transactionIdentifier, const IDBResourceIdentifier& cursorIdentifier, const IDBKeyData&, unsigned long count);
158     void performActivateTransactionInBackingStore(uint64_t callbackIdentifier, const IDBTransactionInfo&);
159     void performUnconditionalDeleteBackingStore();
160
161     // Main thread callbacks
162     void didDeleteBackingStore(uint64_t deletedVersion);
163     void didOpenBackingStore(const IDBDatabaseInfo&, const IDBError&);
164     void didPerformCreateObjectStore(uint64_t callbackIdentifier, const IDBError&, const IDBObjectStoreInfo&);
165     void didPerformDeleteObjectStore(uint64_t callbackIdentifier, const IDBError&, uint64_t objectStoreIdentifier);
166     void didPerformClearObjectStore(uint64_t callbackIdentifier, const IDBError&);
167     void didPerformCreateIndex(uint64_t callbackIdentifier, const IDBError&, const IDBIndexInfo&);
168     void didPerformDeleteIndex(uint64_t callbackIdentifier, const IDBError&, uint64_t objectStoreIdentifier, uint64_t indexIdentifier);
169     void didPerformPutOrAdd(uint64_t callbackIdentifier, const IDBError&, const IDBKeyData&);
170     void didPerformGetRecord(uint64_t callbackIdentifier, const IDBError&, const IDBGetResult&);
171     void didPerformGetCount(uint64_t callbackIdentifier, const IDBError&, uint64_t);
172     void didPerformDeleteRecord(uint64_t callbackIdentifier, const IDBError&);
173     void didPerformOpenCursor(uint64_t callbackIdentifier, const IDBError&, const IDBGetResult&);
174     void didPerformIterateCursor(uint64_t callbackIdentifier, const IDBError&, const IDBGetResult&);
175     void didPerformCommitTransaction(uint64_t callbackIdentifier, const IDBError&, const IDBResourceIdentifier& transactionIdentifier);
176     void didPerformAbortTransaction(uint64_t callbackIdentifier, const IDBError&, const IDBResourceIdentifier& transactionIdentifier);
177     void didPerformActivateTransactionInBackingStore(uint64_t callbackIdentifier, const IDBError&);
178     void didPerformUnconditionalDeleteBackingStore();
179
180     uint64_t storeCallbackOrFireError(ErrorCallback);
181     uint64_t storeCallbackOrFireError(KeyDataCallback);
182     uint64_t storeCallbackOrFireError(GetResultCallback);
183     uint64_t storeCallbackOrFireError(CountCallback);
184
185     void performErrorCallback(uint64_t callbackIdentifier, const IDBError&);
186     void performKeyDataCallback(uint64_t callbackIdentifier, const IDBError&, const IDBKeyData&);
187     void performGetResultCallback(uint64_t callbackIdentifier, const IDBError&, const IDBGetResult&);
188     void performCountCallback(uint64_t callbackIdentifier, const IDBError&, uint64_t);
189
190     void forgetErrorCallback(uint64_t callbackIdentifier);
191
192     bool hasAnyPendingCallbacks() const;
193     bool isCurrentlyInUse() const;
194     bool hasUnfinishedTransactions() const;
195
196     void invokeOperationAndTransactionTimer();
197     void operationAndTransactionTimerFired();
198     RefPtr<UniqueIDBDatabaseTransaction> takeNextRunnableTransaction(bool& hadDeferredTransactions);
199
200     bool prepareToFinishTransaction(UniqueIDBDatabaseTransaction&);
201
202     void postDatabaseTask(CrossThreadTask&&);
203     void postDatabaseTaskReply(CrossThreadTask&&);
204     void executeNextDatabaseTask();
205     void executeNextDatabaseTaskReply();
206
207     void maybeFinishHardClose();
208     bool isDoneWithHardClose();
209
210     IDBServer& m_server;
211     IDBDatabaseIdentifier m_identifier;
212     
213     ListHashSet<RefPtr<ServerOpenDBRequest>> m_pendingOpenDBRequests;
214     RefPtr<ServerOpenDBRequest> m_currentOpenDBRequest;
215
216     ListHashSet<RefPtr<UniqueIDBDatabaseConnection>> m_openDatabaseConnections;
217     HashSet<RefPtr<UniqueIDBDatabaseConnection>> m_clientClosePendingDatabaseConnections;
218     HashSet<RefPtr<UniqueIDBDatabaseConnection>> m_serverClosePendingDatabaseConnections;
219
220     RefPtr<UniqueIDBDatabaseConnection> m_versionChangeDatabaseConnection;
221     RefPtr<UniqueIDBDatabaseTransaction> m_versionChangeTransaction;
222
223     bool m_isOpeningBackingStore { false };
224     IDBError m_backingStoreOpenError;
225     std::unique_ptr<IDBBackingStore> m_backingStore;
226     std::unique_ptr<IDBDatabaseInfo> m_databaseInfo;
227     std::unique_ptr<IDBDatabaseInfo> m_mostRecentDeletedDatabaseInfo;
228
229     bool m_backingStoreSupportsSimultaneousTransactions { false };
230     bool m_backingStoreIsEphemeral { false };
231
232     HashMap<uint64_t, ErrorCallback> m_errorCallbacks;
233     HashMap<uint64_t, KeyDataCallback> m_keyDataCallbacks;
234     HashMap<uint64_t, GetResultCallback> m_getResultCallbacks;
235     HashMap<uint64_t, CountCallback> m_countCallbacks;
236
237     Timer m_operationAndTransactionTimer;
238
239     Deque<RefPtr<UniqueIDBDatabaseTransaction>> m_pendingTransactions;
240     HashMap<IDBResourceIdentifier, RefPtr<UniqueIDBDatabaseTransaction>> m_inProgressTransactions;
241     HashMap<IDBResourceIdentifier, RefPtr<UniqueIDBDatabaseTransaction>> m_finishingTransactions;
242
243     // The keys into these sets are the object store ID.
244     // These sets help to decide which transactions can be started and which must be deferred.
245     HashCountedSet<uint64_t> m_objectStoreTransactionCounts;
246     HashSet<uint64_t> m_objectStoreWriteTransactions;
247
248     bool m_deleteBackingStoreInProgress { false };
249
250     CrossThreadQueue<CrossThreadTask> m_databaseQueue;
251     CrossThreadQueue<CrossThreadTask> m_databaseReplyQueue;
252     std::atomic<uint64_t> m_queuedTaskCount { 0 };
253
254     bool m_hardClosedForUserDelete { false };
255     RefPtr<UniqueIDBDatabase> m_hardCloseProtector;
256 };
257
258 } // namespace IDBServer
259 } // namespace WebCore
260
261 #endif // ENABLE(INDEXED_DATABASE)