IndexedDB 2.0: "close pending flag" and firing blocked events all need fixing.
[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/Ref.h>
47 #include <wtf/ThreadSafeRefCounted.h>
48
49 namespace JSC {
50 class VM;
51 }
52
53 namespace WebCore {
54
55 class IDBError;
56 class IDBGetAllResult;
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&, const IDBGetAllResult&)> GetAllResultsCallback;
73 typedef std::function<void(const IDBError&, uint64_t)> CountCallback;
74
75 class UniqueIDBDatabase : public ThreadSafeRefCounted<UniqueIDBDatabase> {
76 public:
77     static Ref<UniqueIDBDatabase> create(IDBServer& server, const IDBDatabaseIdentifier& identifier)
78     {
79         return adoptRef(*new UniqueIDBDatabase(server, identifier));
80     }
81
82     WEBCORE_EXPORT ~UniqueIDBDatabase();
83
84     void openDatabaseConnection(IDBConnectionToClient&, const IDBRequestData&);
85
86     const IDBDatabaseInfo& info() const;
87     IDBServer& server() { return m_server; }
88     const IDBDatabaseIdentifier& identifier() const { return m_identifier; }
89
90     void createObjectStore(UniqueIDBDatabaseTransaction&, const IDBObjectStoreInfo&, ErrorCallback);
91     void deleteObjectStore(UniqueIDBDatabaseTransaction&, const String& objectStoreName, ErrorCallback);
92     void renameObjectStore(UniqueIDBDatabaseTransaction&, uint64_t objectStoreIdentifier, const String& newName, ErrorCallback);
93     void clearObjectStore(UniqueIDBDatabaseTransaction&, uint64_t objectStoreIdentifier, ErrorCallback);
94     void createIndex(UniqueIDBDatabaseTransaction&, const IDBIndexInfo&, ErrorCallback);
95     void deleteIndex(UniqueIDBDatabaseTransaction&, uint64_t objectStoreIdentifier, const String& indexName, ErrorCallback);
96     void renameIndex(UniqueIDBDatabaseTransaction&, uint64_t objectStoreIdentifier, uint64_t indexIdentifier, const String& newName, ErrorCallback);
97     void putOrAdd(const IDBRequestData&, const IDBKeyData&, const IDBValue&, IndexedDB::ObjectStoreOverwriteMode, KeyDataCallback);
98     void getRecord(const IDBRequestData&, const IDBGetRecordData&, GetResultCallback);
99     void getAllRecords(const IDBRequestData&, const IDBGetAllRecordsData&, GetAllResultsCallback);
100     void getCount(const IDBRequestData&, const IDBKeyRangeData&, CountCallback);
101     void deleteRecord(const IDBRequestData&, const IDBKeyRangeData&, ErrorCallback);
102     void openCursor(const IDBRequestData&, const IDBCursorInfo&, GetResultCallback);
103     void iterateCursor(const IDBRequestData&, const IDBIterateCursorData&, GetResultCallback);
104     void commitTransaction(UniqueIDBDatabaseTransaction&, ErrorCallback);
105     void abortTransaction(UniqueIDBDatabaseTransaction&, ErrorCallback);
106     void didFinishHandlingVersionChange(UniqueIDBDatabaseConnection&, const IDBResourceIdentifier& transactionIdentifier);
107     void transactionDestroyed(UniqueIDBDatabaseTransaction&);
108     void connectionClosedFromClient(UniqueIDBDatabaseConnection&);
109     void confirmConnectionClosedOnServer(UniqueIDBDatabaseConnection&);
110     void didFireVersionChangeEvent(UniqueIDBDatabaseConnection&, const IDBResourceIdentifier& requestIdentifier);
111     void openDBRequestCancelled(const IDBResourceIdentifier& requestIdentifier);
112     void confirmDidCloseFromServer(UniqueIDBDatabaseConnection&);
113
114     void enqueueTransaction(Ref<UniqueIDBDatabaseTransaction>&&);
115
116     void handleDelete(IDBConnectionToClient&, const IDBRequestData&);
117     void immediateCloseForUserDelete();
118
119     static JSC::VM& databaseThreadVM();
120     static JSC::ExecState& databaseThreadExecState();
121
122     bool hardClosedForUserDelete() const { return m_hardClosedForUserDelete; }
123
124 private:
125     UniqueIDBDatabase(IDBServer&, const IDBDatabaseIdentifier&);
126     
127     void handleDatabaseOperations();
128     void handleCurrentOperation();
129     void performCurrentOpenOperation();
130     void performCurrentDeleteOperation();
131     void addOpenDatabaseConnection(Ref<UniqueIDBDatabaseConnection>&&);
132     bool hasAnyOpenConnections() const;
133     bool allConnectionsAreClosedOrClosing() const;
134
135     void startVersionChangeTransaction();
136     void maybeNotifyConnectionsOfVersionChange();
137     void notifyCurrentRequestConnectionClosedOrFiredVersionChangeEvent(uint64_t connectionIdentifier);
138     bool isVersionChangeInProgress();
139
140     void activateTransactionInBackingStore(UniqueIDBDatabaseTransaction&);
141     void transactionCompleted(RefPtr<UniqueIDBDatabaseTransaction>&&);
142
143     void connectionClosedFromServer(UniqueIDBDatabaseConnection&);
144
145     // Database thread operations
146     void deleteBackingStore(const IDBDatabaseIdentifier&);
147     void openBackingStore(const IDBDatabaseIdentifier&);
148     void performCommitTransaction(uint64_t callbackIdentifier, const IDBResourceIdentifier& transactionIdentifier);
149     void performAbortTransaction(uint64_t callbackIdentifier, const IDBResourceIdentifier& transactionIdentifier);
150     void beginTransactionInBackingStore(const IDBTransactionInfo&);
151     void performCreateObjectStore(uint64_t callbackIdentifier, const IDBResourceIdentifier& transactionIdentifier, const IDBObjectStoreInfo&);
152     void performDeleteObjectStore(uint64_t callbackIdentifier, const IDBResourceIdentifier& transactionIdentifier, uint64_t objectStoreIdentifier);
153     void performRenameObjectStore(uint64_t callbackIdentifier, const IDBResourceIdentifier& transactionIdentifier, uint64_t objectStoreIdentifier, const String& newName);
154     void performClearObjectStore(uint64_t callbackIdentifier, const IDBResourceIdentifier& transactionIdentifier, uint64_t objectStoreIdentifier);
155     void performCreateIndex(uint64_t callbackIdentifier, const IDBResourceIdentifier& transactionIdentifier, const IDBIndexInfo&);
156     void performDeleteIndex(uint64_t callbackIdentifier, const IDBResourceIdentifier& transactionIdentifier, uint64_t objectStoreIdentifier, uint64_t indexIdentifier);
157     void performRenameIndex(uint64_t callbackIdentifier, const IDBResourceIdentifier& transactionIdentifier, uint64_t objectStoreIdentifier, uint64_t indexIdentifier, const String& newName);
158     void performPutOrAdd(uint64_t callbackIdentifier, const IDBResourceIdentifier& transactionIdentifier, uint64_t objectStoreIdentifier, const IDBKeyData&, const IDBValue&, IndexedDB::ObjectStoreOverwriteMode);
159     void performGetRecord(uint64_t callbackIdentifier, const IDBResourceIdentifier& transactionIdentifier, uint64_t objectStoreIdentifier, const IDBKeyRangeData&);
160     void performGetAllRecords(uint64_t callbackIdentifier, const IDBResourceIdentifier& transactionIdentifier, const IDBGetAllRecordsData&);
161     void performGetIndexRecord(uint64_t callbackIdentifier, const IDBResourceIdentifier& transactionIdentifier, uint64_t objectStoreIdentifier, uint64_t indexIdentifier, IndexedDB::IndexRecordType, const IDBKeyRangeData&);
162     void performGetCount(uint64_t callbackIdentifier, const IDBResourceIdentifier& transactionIdentifier, uint64_t objectStoreIdentifier, uint64_t indexIdentifier, const IDBKeyRangeData&);
163     void performDeleteRecord(uint64_t callbackIdentifier, const IDBResourceIdentifier& transactionIdentifier, uint64_t objectStoreIdentifier, const IDBKeyRangeData&);
164     void performOpenCursor(uint64_t callbackIdentifier, const IDBResourceIdentifier& transactionIdentifier, const IDBCursorInfo&);
165     void performIterateCursor(uint64_t callbackIdentifier, const IDBResourceIdentifier& transactionIdentifier, const IDBResourceIdentifier& cursorIdentifier, const IDBIterateCursorData&);
166     void performActivateTransactionInBackingStore(uint64_t callbackIdentifier, const IDBTransactionInfo&);
167     void performUnconditionalDeleteBackingStore();
168
169     // Main thread callbacks
170     void didDeleteBackingStore(uint64_t deletedVersion);
171     void didOpenBackingStore(const IDBDatabaseInfo&, const IDBError&);
172     void didPerformCreateObjectStore(uint64_t callbackIdentifier, const IDBError&, const IDBObjectStoreInfo&);
173     void didPerformDeleteObjectStore(uint64_t callbackIdentifier, const IDBError&, uint64_t objectStoreIdentifier);
174     void didPerformRenameObjectStore(uint64_t callbackIdentifier, const IDBError&, uint64_t objectStoreIdentifier, const String& newName);
175     void didPerformClearObjectStore(uint64_t callbackIdentifier, const IDBError&);
176     void didPerformCreateIndex(uint64_t callbackIdentifier, const IDBError&, const IDBIndexInfo&);
177     void didPerformDeleteIndex(uint64_t callbackIdentifier, const IDBError&, uint64_t objectStoreIdentifier, uint64_t indexIdentifier);
178     void didPerformRenameIndex(uint64_t callbackIdentifier, const IDBError&, uint64_t objectStoreIdentifier, uint64_t indexIdentifier, const String& newName);
179     void didPerformPutOrAdd(uint64_t callbackIdentifier, const IDBError&, const IDBKeyData&);
180     void didPerformGetRecord(uint64_t callbackIdentifier, const IDBError&, const IDBGetResult&);
181     void didPerformGetAllRecords(uint64_t callbackIdentifier, const IDBError&, const IDBGetAllResult&);
182     void didPerformGetCount(uint64_t callbackIdentifier, const IDBError&, uint64_t);
183     void didPerformDeleteRecord(uint64_t callbackIdentifier, const IDBError&);
184     void didPerformOpenCursor(uint64_t callbackIdentifier, const IDBError&, const IDBGetResult&);
185     void didPerformIterateCursor(uint64_t callbackIdentifier, const IDBError&, const IDBGetResult&);
186     void didPerformCommitTransaction(uint64_t callbackIdentifier, const IDBError&, const IDBResourceIdentifier& transactionIdentifier);
187     void didPerformAbortTransaction(uint64_t callbackIdentifier, const IDBError&, const IDBResourceIdentifier& transactionIdentifier);
188     void didPerformActivateTransactionInBackingStore(uint64_t callbackIdentifier, const IDBError&);
189     void didPerformUnconditionalDeleteBackingStore();
190
191     uint64_t storeCallbackOrFireError(ErrorCallback);
192     uint64_t storeCallbackOrFireError(KeyDataCallback);
193     uint64_t storeCallbackOrFireError(GetAllResultsCallback);
194     uint64_t storeCallbackOrFireError(GetResultCallback);
195     uint64_t storeCallbackOrFireError(CountCallback);
196
197     void performErrorCallback(uint64_t callbackIdentifier, const IDBError&);
198     void performKeyDataCallback(uint64_t callbackIdentifier, const IDBError&, const IDBKeyData&);
199     void performGetResultCallback(uint64_t callbackIdentifier, const IDBError&, const IDBGetResult&);
200     void performGetAllResultsCallback(uint64_t callbackIdentifier, const IDBError&, const IDBGetAllResult&);
201     void performCountCallback(uint64_t callbackIdentifier, const IDBError&, uint64_t);
202
203     void forgetErrorCallback(uint64_t callbackIdentifier);
204
205     bool hasAnyPendingCallbacks() const;
206     bool isCurrentlyInUse() const;
207     bool hasUnfinishedTransactions() const;
208
209     void invokeOperationAndTransactionTimer();
210     void operationAndTransactionTimerFired();
211     RefPtr<UniqueIDBDatabaseTransaction> takeNextRunnableTransaction(bool& hadDeferredTransactions);
212
213     bool prepareToFinishTransaction(UniqueIDBDatabaseTransaction&);
214
215     void postDatabaseTask(CrossThreadTask&&);
216     void postDatabaseTaskReply(CrossThreadTask&&);
217     void executeNextDatabaseTask();
218     void executeNextDatabaseTaskReply();
219
220     void maybeFinishHardClose();
221     bool isDoneWithHardClose();
222
223     IDBServer& m_server;
224     IDBDatabaseIdentifier m_identifier;
225     
226     ListHashSet<RefPtr<ServerOpenDBRequest>> m_pendingOpenDBRequests;
227     RefPtr<ServerOpenDBRequest> m_currentOpenDBRequest;
228
229     ListHashSet<RefPtr<UniqueIDBDatabaseConnection>> m_openDatabaseConnections;
230     HashSet<RefPtr<UniqueIDBDatabaseConnection>> m_clientClosePendingDatabaseConnections;
231     HashSet<RefPtr<UniqueIDBDatabaseConnection>> m_serverClosePendingDatabaseConnections;
232
233     RefPtr<UniqueIDBDatabaseConnection> m_versionChangeDatabaseConnection;
234     RefPtr<UniqueIDBDatabaseTransaction> m_versionChangeTransaction;
235
236     bool m_isOpeningBackingStore { false };
237     IDBError m_backingStoreOpenError;
238     std::unique_ptr<IDBBackingStore> m_backingStore;
239     std::unique_ptr<IDBDatabaseInfo> m_databaseInfo;
240     std::unique_ptr<IDBDatabaseInfo> m_mostRecentDeletedDatabaseInfo;
241
242     bool m_backingStoreSupportsSimultaneousTransactions { false };
243     bool m_backingStoreIsEphemeral { false };
244
245     HashMap<uint64_t, ErrorCallback> m_errorCallbacks;
246     HashMap<uint64_t, KeyDataCallback> m_keyDataCallbacks;
247     HashMap<uint64_t, GetResultCallback> m_getResultCallbacks;
248     HashMap<uint64_t, GetAllResultsCallback> m_getAllResultsCallbacks;
249     HashMap<uint64_t, CountCallback> m_countCallbacks;
250
251     Timer m_operationAndTransactionTimer;
252
253     Deque<RefPtr<UniqueIDBDatabaseTransaction>> m_pendingTransactions;
254     HashMap<IDBResourceIdentifier, RefPtr<UniqueIDBDatabaseTransaction>> m_inProgressTransactions;
255     HashMap<IDBResourceIdentifier, RefPtr<UniqueIDBDatabaseTransaction>> m_finishingTransactions;
256
257     // The keys into these sets are the object store ID.
258     // These sets help to decide which transactions can be started and which must be deferred.
259     HashCountedSet<uint64_t> m_objectStoreTransactionCounts;
260     HashSet<uint64_t> m_objectStoreWriteTransactions;
261
262     bool m_deleteBackingStoreInProgress { false };
263
264     CrossThreadQueue<CrossThreadTask> m_databaseQueue;
265     CrossThreadQueue<CrossThreadTask> m_databaseReplyQueue;
266     std::atomic<uint64_t> m_queuedTaskCount { 0 };
267
268     bool m_hardClosedForUserDelete { false };
269     RefPtr<UniqueIDBDatabase> m_hardCloseProtector;
270 };
271
272 } // namespace IDBServer
273 } // namespace WebCore
274
275 #endif // ENABLE(INDEXED_DATABASE)