Unreviewed test gardening, mark fast/text/variations/ipc2.html as slow.
[WebKit-https.git] / Source / WebKit / NetworkProcess / IndexedDB / WebIDBConnectionToClient.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 "WebIDBConnectionToClient.h"
28
29 #if ENABLE(INDEXED_DATABASE)
30
31 #include "DataReference.h"
32 #include "NetworkProcess.h"
33 #include "WebCoreArgumentCoders.h"
34 #include "WebIDBConnectionToServerMessages.h"
35 #include "WebIDBResult.h"
36 #include <WebCore/IDBError.h>
37 #include <WebCore/IDBGetAllRecordsData.h>
38 #include <WebCore/IDBGetRecordData.h>
39 #include <WebCore/IDBResultData.h>
40 #include <WebCore/IDBValue.h>
41 #include <WebCore/ThreadSafeDataBuffer.h>
42 #include <WebCore/UniqueIDBDatabaseConnection.h>
43
44 using namespace WebCore;
45
46 namespace WebKit {
47
48 Ref<WebIDBConnectionToClient> WebIDBConnectionToClient::create(NetworkConnectionToWebProcess& connection, uint64_t serverConnectionIdentifier, PAL::SessionID sessionID)
49 {
50     return adoptRef(*new WebIDBConnectionToClient(connection, serverConnectionIdentifier, sessionID));
51 }
52
53 WebIDBConnectionToClient::WebIDBConnectionToClient(NetworkConnectionToWebProcess& connection, uint64_t serverConnectionIdentifier, PAL::SessionID sessionID)
54     : m_connection(connection)
55     , m_identifier(serverConnectionIdentifier)
56     , m_sessionID(sessionID)
57 {
58     relaxAdoptionRequirement();
59     m_connectionToClient = IDBServer::IDBConnectionToClient::create(*this);
60     NetworkProcess::singleton().idbServer(m_sessionID).registerConnection(*m_connectionToClient);
61 }
62
63 WebIDBConnectionToClient::~WebIDBConnectionToClient()
64 {
65 }
66
67 void WebIDBConnectionToClient::disconnectedFromWebProcess()
68 {
69     NetworkProcess::singleton().idbServer(m_sessionID).unregisterConnection(*m_connectionToClient);
70 }
71
72 IPC::Connection* WebIDBConnectionToClient::messageSenderConnection()
73 {
74     return &m_connection->connection();
75 }
76
77 WebCore::IDBServer::IDBConnectionToClient& WebIDBConnectionToClient::connectionToClient()
78 {
79     return *m_connectionToClient;
80 }
81
82 void WebIDBConnectionToClient::didDeleteDatabase(const WebCore::IDBResultData& resultData)
83 {
84     send(Messages::WebIDBConnectionToServer::DidDeleteDatabase(resultData));
85 }
86
87 void WebIDBConnectionToClient::didOpenDatabase(const WebCore::IDBResultData& resultData)
88 {
89     send(Messages::WebIDBConnectionToServer::DidOpenDatabase(resultData));
90 }
91
92 void WebIDBConnectionToClient::didAbortTransaction(const WebCore::IDBResourceIdentifier& transactionIdentifier, const WebCore::IDBError& error)
93 {
94     send(Messages::WebIDBConnectionToServer::DidAbortTransaction(transactionIdentifier, error));
95 }
96
97 void WebIDBConnectionToClient::didCommitTransaction(const WebCore::IDBResourceIdentifier& transactionIdentifier, const WebCore::IDBError& error)
98 {
99     send(Messages::WebIDBConnectionToServer::DidCommitTransaction(transactionIdentifier, error));
100 }
101
102 void WebIDBConnectionToClient::didCreateObjectStore(const WebCore::IDBResultData& resultData)
103 {
104     send(Messages::WebIDBConnectionToServer::DidCreateObjectStore(resultData));
105 }
106
107 void WebIDBConnectionToClient::didDeleteObjectStore(const WebCore::IDBResultData& resultData)
108 {
109     send(Messages::WebIDBConnectionToServer::DidDeleteObjectStore(resultData));
110 }
111
112 void WebIDBConnectionToClient::didRenameObjectStore(const WebCore::IDBResultData& resultData)
113 {
114     send(Messages::WebIDBConnectionToServer::DidRenameObjectStore(resultData));
115 }
116
117 void WebIDBConnectionToClient::didClearObjectStore(const WebCore::IDBResultData& resultData)
118 {
119     send(Messages::WebIDBConnectionToServer::DidClearObjectStore(resultData));
120 }
121
122 void WebIDBConnectionToClient::didCreateIndex(const WebCore::IDBResultData& resultData)
123 {
124     send(Messages::WebIDBConnectionToServer::DidCreateIndex(resultData));
125 }
126
127 void WebIDBConnectionToClient::didDeleteIndex(const WebCore::IDBResultData& resultData)
128 {
129     send(Messages::WebIDBConnectionToServer::DidDeleteIndex(resultData));
130 }
131
132 void WebIDBConnectionToClient::didRenameIndex(const WebCore::IDBResultData& resultData)
133 {
134     send(Messages::WebIDBConnectionToServer::DidRenameIndex(resultData));
135 }
136
137 void WebIDBConnectionToClient::didPutOrAdd(const WebCore::IDBResultData& resultData)
138 {
139     send(Messages::WebIDBConnectionToServer::DidPutOrAdd(resultData));
140 }
141
142 template<class MessageType> void WebIDBConnectionToClient::handleGetResult(const WebCore::IDBResultData& resultData)
143 {
144     if (resultData.type() == IDBResultType::Error) {
145         send(MessageType(resultData));
146         return;
147     }
148
149     if (resultData.type() == IDBResultType::GetAllRecordsSuccess && resultData.getAllResult().type() == IndexedDB::GetAllType::Keys) {
150         send(MessageType(resultData));
151         return;
152     }
153
154     auto blobFilePaths = resultData.type() == IDBResultType::GetAllRecordsSuccess ? resultData.getAllResult().allBlobFilePaths() : resultData.getResult().value().blobFilePaths();
155     if (blobFilePaths.isEmpty()) {
156         send(MessageType(resultData));
157         return;
158     }
159
160 #if ENABLE(SANDBOX_EXTENSIONS)
161     NetworkProcess::singleton().getSandboxExtensionsForBlobFiles(blobFilePaths, [protectedThis = makeRef(*this), this, resultData](SandboxExtension::HandleArray&& handles) {
162         send(MessageType({ resultData, WTFMove(handles) }));
163     });
164 #else
165     send(MessageType(resultData));
166 #endif
167 }
168
169 void WebIDBConnectionToClient::didGetRecord(const WebCore::IDBResultData& resultData)
170 {
171     handleGetResult<Messages::WebIDBConnectionToServer::DidGetRecord>(resultData);
172 }
173
174 void WebIDBConnectionToClient::didGetAllRecords(const WebCore::IDBResultData& resultData)
175 {
176     handleGetResult<Messages::WebIDBConnectionToServer::DidGetAllRecords>(resultData);
177 }
178
179 void WebIDBConnectionToClient::didGetCount(const WebCore::IDBResultData& resultData)
180 {
181     send(Messages::WebIDBConnectionToServer::DidGetCount(resultData));
182 }
183
184 void WebIDBConnectionToClient::didDeleteRecord(const WebCore::IDBResultData& resultData)
185 {
186     send(Messages::WebIDBConnectionToServer::DidDeleteRecord(resultData));
187 }
188
189 void WebIDBConnectionToClient::didOpenCursor(const WebCore::IDBResultData& resultData)
190 {
191     handleGetResult<Messages::WebIDBConnectionToServer::DidOpenCursor>(resultData);
192 }
193
194 void WebIDBConnectionToClient::didIterateCursor(const WebCore::IDBResultData& resultData)
195 {
196     handleGetResult<Messages::WebIDBConnectionToServer::DidIterateCursor>(resultData);
197 }
198
199 void WebIDBConnectionToClient::fireVersionChangeEvent(WebCore::IDBServer::UniqueIDBDatabaseConnection& connection, const WebCore::IDBResourceIdentifier& requestIdentifier, uint64_t requestedVersion)
200 {
201     send(Messages::WebIDBConnectionToServer::FireVersionChangeEvent(connection.identifier(), requestIdentifier, requestedVersion));
202 }
203
204 void WebIDBConnectionToClient::didStartTransaction(const WebCore::IDBResourceIdentifier& transactionIdentifier, const WebCore::IDBError& error)
205 {
206     send(Messages::WebIDBConnectionToServer::DidStartTransaction(transactionIdentifier, error));
207 }
208
209 void WebIDBConnectionToClient::didCloseFromServer(WebCore::IDBServer::UniqueIDBDatabaseConnection& connection, const WebCore::IDBError& error)
210 {
211     send(Messages::WebIDBConnectionToServer::DidCloseFromServer(connection.identifier(), error));
212 }
213
214 void WebIDBConnectionToClient::notifyOpenDBRequestBlocked(const WebCore::IDBResourceIdentifier& requestIdentifier, uint64_t oldVersion, uint64_t newVersion)
215 {
216     send(Messages::WebIDBConnectionToServer::NotifyOpenDBRequestBlocked(requestIdentifier, oldVersion, newVersion));
217 }
218
219 void WebIDBConnectionToClient::didGetAllDatabaseNames(uint64_t callbackID, const Vector<String>& databaseNames)
220 {
221     send(Messages::WebIDBConnectionToServer::DidGetAllDatabaseNames(callbackID, databaseNames));
222 }
223
224 void WebIDBConnectionToClient::deleteDatabase(const IDBRequestData& request)
225 {
226     NetworkProcess::singleton().idbServer(m_sessionID).deleteDatabase(request);
227 }
228
229 void WebIDBConnectionToClient::openDatabase(const IDBRequestData& request)
230 {
231     NetworkProcess::singleton().idbServer(m_sessionID).openDatabase(request);
232 }
233
234 void WebIDBConnectionToClient::abortTransaction(const IDBResourceIdentifier& transactionIdentifier)
235 {
236     NetworkProcess::singleton().idbServer(m_sessionID).abortTransaction(transactionIdentifier);
237 }
238
239 void WebIDBConnectionToClient::commitTransaction(const IDBResourceIdentifier& transactionIdentifier)
240 {
241     NetworkProcess::singleton().idbServer(m_sessionID).commitTransaction(transactionIdentifier);
242 }
243
244 void WebIDBConnectionToClient::didFinishHandlingVersionChangeTransaction(uint64_t databaseConnectionIdentifier, const IDBResourceIdentifier& transactionIdentifier)
245 {
246     NetworkProcess::singleton().idbServer(m_sessionID).didFinishHandlingVersionChangeTransaction(databaseConnectionIdentifier, transactionIdentifier);
247 }
248
249 void WebIDBConnectionToClient::createObjectStore(const IDBRequestData& request, const IDBObjectStoreInfo& info)
250 {
251     NetworkProcess::singleton().idbServer(m_sessionID).createObjectStore(request, info);
252 }
253
254 void WebIDBConnectionToClient::deleteObjectStore(const IDBRequestData& request, const String& name)
255 {
256     NetworkProcess::singleton().idbServer(m_sessionID).deleteObjectStore(request, name);
257 }
258
259 void WebIDBConnectionToClient::renameObjectStore(const IDBRequestData& request, uint64_t objectStoreIdentifier, const String& newName)
260 {
261     NetworkProcess::singleton().idbServer(m_sessionID).renameObjectStore(request, objectStoreIdentifier, newName);
262 }
263
264 void WebIDBConnectionToClient::clearObjectStore(const IDBRequestData& request, uint64_t objectStoreIdentifier)
265 {
266     NetworkProcess::singleton().idbServer(m_sessionID).clearObjectStore(request, objectStoreIdentifier);
267 }
268
269 void WebIDBConnectionToClient::createIndex(const IDBRequestData& request, const IDBIndexInfo& info)
270 {
271     NetworkProcess::singleton().idbServer(m_sessionID).createIndex(request, info);
272 }
273
274 void WebIDBConnectionToClient::deleteIndex(const IDBRequestData& request, uint64_t objectStoreIdentifier, const String& name)
275 {
276     NetworkProcess::singleton().idbServer(m_sessionID).deleteIndex(request, objectStoreIdentifier, name);
277 }
278
279 void WebIDBConnectionToClient::renameIndex(const IDBRequestData& request, uint64_t objectStoreIdentifier, uint64_t indexIdentifier, const String& newName)
280 {
281     NetworkProcess::singleton().idbServer(m_sessionID).renameIndex(request, objectStoreIdentifier, indexIdentifier, newName);
282 }
283
284 void WebIDBConnectionToClient::putOrAdd(const IDBRequestData& request, const IDBKeyData& key, const IDBValue& value, unsigned overwriteMode)
285 {
286     if (overwriteMode != static_cast<unsigned>(IndexedDB::ObjectStoreOverwriteMode::NoOverwrite)
287         && overwriteMode != static_cast<unsigned>(IndexedDB::ObjectStoreOverwriteMode::Overwrite)
288         && overwriteMode != static_cast<unsigned>(IndexedDB::ObjectStoreOverwriteMode::OverwriteForCursor)) {
289         // FIXME: This message from the WebProcess is corrupt.
290         // The StorageProcess should return early at this point, but can we also kill the bad WebProcess?
291         return;
292     }
293
294     IndexedDB::ObjectStoreOverwriteMode mode = static_cast<IndexedDB::ObjectStoreOverwriteMode>(overwriteMode);
295
296     NetworkProcess::singleton().idbServer(m_sessionID).putOrAdd(request, key, value, mode);
297 }
298
299 void WebIDBConnectionToClient::getRecord(const IDBRequestData& request, const IDBGetRecordData& getRecordData)
300 {
301     NetworkProcess::singleton().idbServer(m_sessionID).getRecord(request, getRecordData);
302 }
303
304 void WebIDBConnectionToClient::getAllRecords(const IDBRequestData& request, const IDBGetAllRecordsData& getAllRecordsData)
305 {
306     NetworkProcess::singleton().idbServer(m_sessionID).getAllRecords(request, getAllRecordsData);
307 }
308
309 void WebIDBConnectionToClient::getCount(const IDBRequestData& request, const IDBKeyRangeData& range)
310 {
311     NetworkProcess::singleton().idbServer(m_sessionID).getCount(request, range);
312 }
313
314 void WebIDBConnectionToClient::deleteRecord(const IDBRequestData& request, const IDBKeyRangeData& range)
315 {
316     NetworkProcess::singleton().idbServer(m_sessionID).deleteRecord(request, range);
317 }
318
319 void WebIDBConnectionToClient::openCursor(const IDBRequestData& request, const IDBCursorInfo& info)
320 {
321     NetworkProcess::singleton().idbServer(m_sessionID).openCursor(request, info);
322 }
323
324 void WebIDBConnectionToClient::iterateCursor(const IDBRequestData& request, const IDBIterateCursorData& data)
325 {
326     NetworkProcess::singleton().idbServer(m_sessionID).iterateCursor(request, data);
327 }
328
329 void WebIDBConnectionToClient::establishTransaction(uint64_t databaseConnectionIdentifier, const IDBTransactionInfo& info)
330 {
331     NetworkProcess::singleton().idbServer(m_sessionID).establishTransaction(databaseConnectionIdentifier, info);
332 }
333
334 void WebIDBConnectionToClient::databaseConnectionPendingClose(uint64_t databaseConnectionIdentifier)
335 {
336     NetworkProcess::singleton().idbServer(m_sessionID).databaseConnectionPendingClose(databaseConnectionIdentifier);
337 }
338
339 void WebIDBConnectionToClient::databaseConnectionClosed(uint64_t databaseConnectionIdentifier)
340 {
341     NetworkProcess::singleton().idbServer(m_sessionID).databaseConnectionClosed(databaseConnectionIdentifier);
342 }
343
344 void WebIDBConnectionToClient::abortOpenAndUpgradeNeeded(uint64_t databaseConnectionIdentifier, const IDBResourceIdentifier& transactionIdentifier)
345 {
346     NetworkProcess::singleton().idbServer(m_sessionID).abortOpenAndUpgradeNeeded(databaseConnectionIdentifier, transactionIdentifier);
347 }
348
349 void WebIDBConnectionToClient::didFireVersionChangeEvent(uint64_t databaseConnectionIdentifier, const IDBResourceIdentifier& transactionIdentifier)
350 {
351     NetworkProcess::singleton().idbServer(m_sessionID).didFireVersionChangeEvent(databaseConnectionIdentifier, transactionIdentifier);
352 }
353
354 void WebIDBConnectionToClient::openDBRequestCancelled(const IDBRequestData& requestData)
355 {
356     NetworkProcess::singleton().idbServer(m_sessionID).openDBRequestCancelled(requestData);
357 }
358
359 void WebIDBConnectionToClient::confirmDidCloseFromServer(uint64_t databaseConnectionIdentifier)
360 {
361     NetworkProcess::singleton().idbServer(m_sessionID).confirmDidCloseFromServer(databaseConnectionIdentifier);
362 }
363
364 void WebIDBConnectionToClient::getAllDatabaseNames(uint64_t serverConnectionIdentifier, const WebCore::SecurityOriginData& topOrigin, const WebCore::SecurityOriginData& openingOrigin, uint64_t callbackID)
365 {
366     NetworkProcess::singleton().idbServer(m_sessionID).getAllDatabaseNames(serverConnectionIdentifier, topOrigin, openingOrigin, callbackID);
367 }
368
369 } // namespace WebKit
370
371 #endif // ENABLE(INDEXED_DATABASE)