IndexedDB 2.0: "close pending flag" and firing blocked events all need fixing.
[WebKit-https.git] / Source / WebKit2 / WebProcess / Databases / IndexedDB / WebIDBConnectionToServer.cpp
1 /*
2  * Copyright (C) 2016 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 #include "config.h"
27 #include "WebIDBConnectionToServer.h"
28
29 #if ENABLE(INDEXED_DATABASE)
30
31 #include "DataReference.h"
32 #include "DatabaseToWebProcessConnectionMessages.h"
33 #include "NetworkConnectionToWebProcessMessages.h"
34 #include "NetworkProcessConnection.h"
35 #include "WebCoreArgumentCoders.h"
36 #include "WebIDBConnectionToClientMessages.h"
37 #include "WebIDBResult.h"
38 #include "WebProcess.h"
39 #include "WebToDatabaseProcessConnection.h"
40 #include <WebCore/IDBConnectionToServer.h>
41 #include <WebCore/IDBCursorInfo.h>
42 #include <WebCore/IDBDatabaseException.h>
43 #include <WebCore/IDBError.h>
44 #include <WebCore/IDBIndexInfo.h>
45 #include <WebCore/IDBIterateCursorData.h>
46 #include <WebCore/IDBKeyRangeData.h>
47 #include <WebCore/IDBObjectStoreInfo.h>
48 #include <WebCore/IDBOpenDBRequest.h>
49 #include <WebCore/IDBRequestData.h>
50 #include <WebCore/IDBResourceIdentifier.h>
51 #include <WebCore/IDBResultData.h>
52 #include <WebCore/IDBTransactionInfo.h>
53 #include <WebCore/IDBValue.h>
54
55 using namespace WebCore;
56
57 namespace WebKit {
58
59 Ref<WebIDBConnectionToServer> WebIDBConnectionToServer::create()
60 {
61     return adoptRef(*new WebIDBConnectionToServer);
62 }
63
64 WebIDBConnectionToServer::WebIDBConnectionToServer()
65 {
66     relaxAdoptionRequirement();
67
68     m_isOpenInServer = sendSync(Messages::DatabaseToWebProcessConnection::EstablishIDBConnectionToServer(), Messages::DatabaseToWebProcessConnection::EstablishIDBConnectionToServer::Reply(m_identifier));
69     m_connectionToServer = IDBClient::IDBConnectionToServer::create(*this);
70 }
71
72 WebIDBConnectionToServer::~WebIDBConnectionToServer()
73 {
74     if (m_isOpenInServer)
75         send(Messages::DatabaseToWebProcessConnection::RemoveIDBConnectionToServer(m_identifier));
76 }
77
78 IPC::Connection* WebIDBConnectionToServer::messageSenderConnection()
79 {
80     return &WebProcess::singleton().webToDatabaseProcessConnection()->connection();
81 }
82
83 IDBClient::IDBConnectionToServer& WebIDBConnectionToServer::coreConnectionToServer()
84 {
85     return *m_connectionToServer;
86 }
87
88 void WebIDBConnectionToServer::deleteDatabase(const IDBRequestData& requestData)
89 {
90     send(Messages::WebIDBConnectionToClient::DeleteDatabase(requestData));
91 }
92
93 void WebIDBConnectionToServer::openDatabase(const IDBRequestData& requestData)
94 {
95     send(Messages::WebIDBConnectionToClient::OpenDatabase(requestData));
96 }
97
98 void WebIDBConnectionToServer::abortTransaction(const IDBResourceIdentifier& transactionIdentifier)
99 {
100     send(Messages::WebIDBConnectionToClient::AbortTransaction(transactionIdentifier));
101 }
102
103 void WebIDBConnectionToServer::commitTransaction(const IDBResourceIdentifier& transactionIdentifier)
104 {
105     send(Messages::WebIDBConnectionToClient::CommitTransaction(transactionIdentifier));
106 }
107
108 void WebIDBConnectionToServer::didFinishHandlingVersionChangeTransaction(uint64_t databaseConnectionIdentifier, const IDBResourceIdentifier& transactionIdentifier)
109 {
110     send(Messages::WebIDBConnectionToClient::DidFinishHandlingVersionChangeTransaction(databaseConnectionIdentifier, transactionIdentifier));
111 }
112
113 void WebIDBConnectionToServer::createObjectStore(const IDBRequestData& requestData, const IDBObjectStoreInfo& info)
114 {
115     send(Messages::WebIDBConnectionToClient::CreateObjectStore(requestData, info));
116 }
117
118 void WebIDBConnectionToServer::deleteObjectStore(const IDBRequestData& requestData, const String& objectStoreName)
119 {
120     send(Messages::WebIDBConnectionToClient::DeleteObjectStore(requestData, objectStoreName));
121 }
122
123 void WebIDBConnectionToServer::renameObjectStore(const IDBRequestData& requestData, uint64_t objectStoreIdentifier, const String& newName)
124 {
125     send(Messages::WebIDBConnectionToClient::RenameObjectStore(requestData, objectStoreIdentifier, newName));
126 }
127
128 void WebIDBConnectionToServer::clearObjectStore(const IDBRequestData& requestData, uint64_t objectStoreIdentifier)
129 {
130     send(Messages::WebIDBConnectionToClient::ClearObjectStore(requestData, objectStoreIdentifier));
131 }
132
133 void WebIDBConnectionToServer::createIndex(const IDBRequestData& requestData, const IDBIndexInfo& info)
134 {
135     send(Messages::WebIDBConnectionToClient::CreateIndex(requestData, info));
136 }
137
138 void WebIDBConnectionToServer::deleteIndex(const IDBRequestData& requestData, uint64_t objectStoreIdentifier, const String& indexName)
139 {
140     send(Messages::WebIDBConnectionToClient::DeleteIndex(requestData, objectStoreIdentifier, indexName));
141 }
142
143 void WebIDBConnectionToServer::renameIndex(const IDBRequestData& requestData, uint64_t objectStoreIdentifier, uint64_t indexIdentifier, const String& newName)
144 {
145     send(Messages::WebIDBConnectionToClient::RenameIndex(requestData, objectStoreIdentifier, indexIdentifier, newName));
146 }
147
148 void WebIDBConnectionToServer::putOrAdd(const IDBRequestData& requestData, const IDBKeyData& keyData, const IDBValue& value, const IndexedDB::ObjectStoreOverwriteMode mode)
149 {
150     send(Messages::WebIDBConnectionToClient::PutOrAdd(requestData, keyData, value, static_cast<unsigned>(mode)));
151 }
152
153 void WebIDBConnectionToServer::getRecord(const IDBRequestData& requestData, const IDBGetRecordData& getRecordData)
154 {
155     send(Messages::WebIDBConnectionToClient::GetRecord(requestData, getRecordData));
156 }
157
158 void WebIDBConnectionToServer::getAllRecords(const IDBRequestData& requestData, const IDBGetAllRecordsData& getAllRecordsData)
159 {
160     send(Messages::WebIDBConnectionToClient::GetAllRecords(requestData, getAllRecordsData));
161 }
162
163 void WebIDBConnectionToServer::getCount(const IDBRequestData& requestData, const IDBKeyRangeData& range)
164 {
165     send(Messages::WebIDBConnectionToClient::GetCount(requestData, range));
166 }
167
168 void WebIDBConnectionToServer::deleteRecord(const IDBRequestData& requestData, const IDBKeyRangeData& range)
169 {
170     send(Messages::WebIDBConnectionToClient::DeleteRecord(requestData, range));
171 }
172
173 void WebIDBConnectionToServer::openCursor(const IDBRequestData& requestData, const IDBCursorInfo& info)
174 {
175     send(Messages::WebIDBConnectionToClient::OpenCursor(requestData, info));
176 }
177
178 void WebIDBConnectionToServer::iterateCursor(const IDBRequestData& requestData, const IDBIterateCursorData& data)
179 {
180     send(Messages::WebIDBConnectionToClient::IterateCursor(requestData, data));
181 }
182
183 void WebIDBConnectionToServer::establishTransaction(uint64_t databaseConnectionIdentifier, const IDBTransactionInfo& info)
184 {
185     send(Messages::WebIDBConnectionToClient::EstablishTransaction(databaseConnectionIdentifier, info));
186 }
187
188 void WebIDBConnectionToServer::databaseConnectionPendingClose(uint64_t databaseConnectionIdentifier)
189 {
190     send(Messages::WebIDBConnectionToClient::DatabaseConnectionPendingClose(databaseConnectionIdentifier));
191 }
192
193 void WebIDBConnectionToServer::databaseConnectionClosed(uint64_t databaseConnectionIdentifier)
194 {
195     send(Messages::WebIDBConnectionToClient::DatabaseConnectionClosed(databaseConnectionIdentifier));
196 }
197
198 void WebIDBConnectionToServer::abortOpenAndUpgradeNeeded(uint64_t databaseConnectionIdentifier, const IDBResourceIdentifier& transactionIdentifier)
199 {
200     send(Messages::WebIDBConnectionToClient::AbortOpenAndUpgradeNeeded(databaseConnectionIdentifier, transactionIdentifier));
201 }
202
203 void WebIDBConnectionToServer::didFireVersionChangeEvent(uint64_t databaseConnectionIdentifier, const IDBResourceIdentifier& requestIdentifier)
204 {
205     send(Messages::WebIDBConnectionToClient::DidFireVersionChangeEvent(databaseConnectionIdentifier, requestIdentifier));
206 }
207
208 void WebIDBConnectionToServer::openDBRequestCancelled(const IDBRequestData& requestData)
209 {
210     send(Messages::WebIDBConnectionToClient::OpenDBRequestCancelled(requestData));
211 }
212
213 void WebIDBConnectionToServer::confirmDidCloseFromServer(uint64_t databaseConnectionIdentifier)
214 {
215     send(Messages::WebIDBConnectionToClient::ConfirmDidCloseFromServer(databaseConnectionIdentifier));
216 }
217
218 void WebIDBConnectionToServer::getAllDatabaseNames(const WebCore::SecurityOriginData& topOrigin, const WebCore::SecurityOriginData& openingOrigin, uint64_t callbackID)
219 {
220     send(Messages::WebIDBConnectionToClient::GetAllDatabaseNames(m_identifier, topOrigin, openingOrigin, callbackID));
221 }
222
223 void WebIDBConnectionToServer::didDeleteDatabase(const IDBResultData& result)
224 {
225     m_connectionToServer->didDeleteDatabase(result);
226 }
227
228 void WebIDBConnectionToServer::didOpenDatabase(const IDBResultData& result)
229 {
230     m_connectionToServer->didOpenDatabase(result);
231 }
232
233 void WebIDBConnectionToServer::didAbortTransaction(const IDBResourceIdentifier& transactionIdentifier, const IDBError& error)
234 {
235     m_connectionToServer->didAbortTransaction(transactionIdentifier, error);
236 }
237
238 void WebIDBConnectionToServer::didCommitTransaction(const IDBResourceIdentifier& transactionIdentifier, const IDBError& error)
239 {
240     m_connectionToServer->didCommitTransaction(transactionIdentifier, error);
241 }
242
243 void WebIDBConnectionToServer::didCreateObjectStore(const IDBResultData& result)
244 {
245     m_connectionToServer->didCreateObjectStore(result);
246 }
247
248 void WebIDBConnectionToServer::didDeleteObjectStore(const IDBResultData& result)
249 {
250     m_connectionToServer->didDeleteObjectStore(result);
251 }
252
253 void WebIDBConnectionToServer::didRenameObjectStore(const IDBResultData& result)
254 {
255     m_connectionToServer->didRenameObjectStore(result);
256 }
257
258 void WebIDBConnectionToServer::didClearObjectStore(const IDBResultData& result)
259 {
260     m_connectionToServer->didClearObjectStore(result);
261 }
262
263 void WebIDBConnectionToServer::didCreateIndex(const IDBResultData& result)
264 {
265     m_connectionToServer->didCreateIndex(result);
266 }
267
268 void WebIDBConnectionToServer::didDeleteIndex(const IDBResultData& result)
269 {
270     m_connectionToServer->didDeleteIndex(result);
271 }
272
273 void WebIDBConnectionToServer::didRenameIndex(const IDBResultData& result)
274 {
275     m_connectionToServer->didRenameIndex(result);
276 }
277
278 void WebIDBConnectionToServer::didPutOrAdd(const IDBResultData& result)
279 {
280     m_connectionToServer->didPutOrAdd(result);
281 }
282
283 static void preregisterSandboxExtensionsIfNecessary(const WebIDBResult& result)
284 {
285     auto resultType = result.resultData().type();
286     if (resultType != IDBResultType::GetRecordSuccess && resultType != IDBResultType::OpenCursorSuccess && resultType != IDBResultType::IterateCursorSuccess && resultType != IDBResultType::GetAllRecordsSuccess) {
287         ASSERT(resultType == IDBResultType::Error);
288         return;
289     }
290
291     const auto filePaths = resultType == IDBResultType::GetAllRecordsSuccess ? result.resultData().getAllResult().allBlobFilePaths() : result.resultData().getResult().value().blobFilePaths();
292
293 #if ENABLE(SANDBOX_EXTENSIONS)
294     ASSERT(filePaths.size() == result.handles().size());
295 #endif
296
297     if (!filePaths.isEmpty())
298         WebProcess::singleton().networkConnection().connection().send(Messages::NetworkConnectionToWebProcess::PreregisterSandboxExtensionsForOptionallyFileBackedBlob(filePaths, result.handles()), 0);
299 }
300
301 void WebIDBConnectionToServer::didGetRecord(const WebIDBResult& result)
302 {
303     preregisterSandboxExtensionsIfNecessary(result);
304     m_connectionToServer->didGetRecord(result.resultData());
305 }
306
307 void WebIDBConnectionToServer::didGetAllRecords(const WebIDBResult& result)
308 {
309     if (result.resultData().getAllResult().type() == IndexedDB::GetAllType::Values)
310         preregisterSandboxExtensionsIfNecessary(result);
311     m_connectionToServer->didGetAllRecords(result.resultData());
312 }
313
314 void WebIDBConnectionToServer::didGetCount(const IDBResultData& result)
315 {
316     m_connectionToServer->didGetCount(result);
317 }
318
319 void WebIDBConnectionToServer::didDeleteRecord(const IDBResultData& result)
320 {
321     m_connectionToServer->didDeleteRecord(result);
322 }
323
324 void WebIDBConnectionToServer::didOpenCursor(const WebIDBResult& result)
325 {
326     preregisterSandboxExtensionsIfNecessary(result);
327     m_connectionToServer->didOpenCursor(result.resultData());
328 }
329
330 void WebIDBConnectionToServer::didIterateCursor(const WebIDBResult& result)
331 {
332     preregisterSandboxExtensionsIfNecessary(result);
333     m_connectionToServer->didIterateCursor(result.resultData());
334 }
335
336 void WebIDBConnectionToServer::fireVersionChangeEvent(uint64_t uniqueDatabaseConnectionIdentifier, const IDBResourceIdentifier& requestIdentifier, uint64_t requestedVersion)
337 {
338     m_connectionToServer->fireVersionChangeEvent(uniqueDatabaseConnectionIdentifier, requestIdentifier, requestedVersion);
339 }
340
341 void WebIDBConnectionToServer::didStartTransaction(const IDBResourceIdentifier& transactionIdentifier, const IDBError& error)
342 {
343     m_connectionToServer->didStartTransaction(transactionIdentifier, error);
344 }
345
346 void WebIDBConnectionToServer::didCloseFromServer(uint64_t databaseConnectionIdentifier, const IDBError& error)
347 {
348     m_connectionToServer->didCloseFromServer(databaseConnectionIdentifier, error);
349 }
350
351 void WebIDBConnectionToServer::notifyOpenDBRequestBlocked(const IDBResourceIdentifier& requestIdentifier, uint64_t oldVersion, uint64_t newVersion)
352 {
353     m_connectionToServer->notifyOpenDBRequestBlocked(requestIdentifier, oldVersion, newVersion);
354 }
355
356 void WebIDBConnectionToServer::didGetAllDatabaseNames(uint64_t callbackID, const Vector<String>& databaseNames)
357 {
358     m_connectionToServer->didGetAllDatabaseNames(callbackID, databaseNames);
359 }
360
361 void WebIDBConnectionToServer::connectionToServerLost()
362 {
363     m_connectionToServer->connectionToServerLost({ WebCore::IDBDatabaseException::UnknownError, ASCIILiteral("An internal error was encountered in the Indexed Database server") });
364 }
365
366 } // namespace WebKit
367
368 #endif // ENABLE(INDEXED_DATABASE)