7fb2b97792b3018d5e1e0c378723a136c88d3d41
[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 namespace WebKit {
45 using namespace WebCore;
46
47 Ref<WebIDBConnectionToClient> WebIDBConnectionToClient::create(NetworkProcess& networkProcess, NetworkConnectionToWebProcess& connection, uint64_t serverConnectionIdentifier, PAL::SessionID sessionID)
48 {
49     return adoptRef(*new WebIDBConnectionToClient(networkProcess, connection, serverConnectionIdentifier, sessionID));
50 }
51
52 WebIDBConnectionToClient::WebIDBConnectionToClient(NetworkProcess& networkProcess, NetworkConnectionToWebProcess& connection, uint64_t serverConnectionIdentifier, PAL::SessionID sessionID)
53     : m_connection(connection)
54     , m_networkProcess(networkProcess)
55     , m_identifier(serverConnectionIdentifier)
56     , m_sessionID(sessionID)
57 {
58     relaxAdoptionRequirement();
59     m_connectionToClient = IDBServer::IDBConnectionToClient::create(*this);
60     networkProcess.idbServer(m_sessionID).registerConnection(*m_connectionToClient);
61 }
62
63 WebIDBConnectionToClient::~WebIDBConnectionToClient()
64 {
65 }
66
67 void WebIDBConnectionToClient::disconnectedFromWebProcess()
68 {
69     m_networkProcess->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     send(MessageType(resultData));
161 }
162
163 void WebIDBConnectionToClient::didGetRecord(const WebCore::IDBResultData& resultData)
164 {
165     handleGetResult<Messages::WebIDBConnectionToServer::DidGetRecord>(resultData);
166 }
167
168 void WebIDBConnectionToClient::didGetAllRecords(const WebCore::IDBResultData& resultData)
169 {
170     handleGetResult<Messages::WebIDBConnectionToServer::DidGetAllRecords>(resultData);
171 }
172
173 void WebIDBConnectionToClient::didGetCount(const WebCore::IDBResultData& resultData)
174 {
175     send(Messages::WebIDBConnectionToServer::DidGetCount(resultData));
176 }
177
178 void WebIDBConnectionToClient::didDeleteRecord(const WebCore::IDBResultData& resultData)
179 {
180     send(Messages::WebIDBConnectionToServer::DidDeleteRecord(resultData));
181 }
182
183 void WebIDBConnectionToClient::didOpenCursor(const WebCore::IDBResultData& resultData)
184 {
185     handleGetResult<Messages::WebIDBConnectionToServer::DidOpenCursor>(resultData);
186 }
187
188 void WebIDBConnectionToClient::didIterateCursor(const WebCore::IDBResultData& resultData)
189 {
190     handleGetResult<Messages::WebIDBConnectionToServer::DidIterateCursor>(resultData);
191 }
192
193 void WebIDBConnectionToClient::fireVersionChangeEvent(WebCore::IDBServer::UniqueIDBDatabaseConnection& connection, const WebCore::IDBResourceIdentifier& requestIdentifier, uint64_t requestedVersion)
194 {
195     send(Messages::WebIDBConnectionToServer::FireVersionChangeEvent(connection.identifier(), requestIdentifier, requestedVersion));
196 }
197
198 void WebIDBConnectionToClient::didStartTransaction(const WebCore::IDBResourceIdentifier& transactionIdentifier, const WebCore::IDBError& error)
199 {
200     send(Messages::WebIDBConnectionToServer::DidStartTransaction(transactionIdentifier, error));
201 }
202
203 void WebIDBConnectionToClient::didCloseFromServer(WebCore::IDBServer::UniqueIDBDatabaseConnection& connection, const WebCore::IDBError& error)
204 {
205     send(Messages::WebIDBConnectionToServer::DidCloseFromServer(connection.identifier(), error));
206 }
207
208 void WebIDBConnectionToClient::notifyOpenDBRequestBlocked(const WebCore::IDBResourceIdentifier& requestIdentifier, uint64_t oldVersion, uint64_t newVersion)
209 {
210     send(Messages::WebIDBConnectionToServer::NotifyOpenDBRequestBlocked(requestIdentifier, oldVersion, newVersion));
211 }
212
213 void WebIDBConnectionToClient::didGetAllDatabaseNames(uint64_t callbackID, const Vector<String>& databaseNames)
214 {
215     send(Messages::WebIDBConnectionToServer::DidGetAllDatabaseNames(callbackID, databaseNames));
216 }
217
218 void WebIDBConnectionToClient::deleteDatabase(const IDBRequestData& request)
219 {
220     m_networkProcess->idbServer(m_sessionID).deleteDatabase(request);
221 }
222
223 void WebIDBConnectionToClient::openDatabase(const IDBRequestData& request)
224 {
225     m_networkProcess->idbServer(m_sessionID).openDatabase(request);
226 }
227
228 void WebIDBConnectionToClient::abortTransaction(const IDBResourceIdentifier& transactionIdentifier)
229 {
230     m_networkProcess->idbServer(m_sessionID).abortTransaction(transactionIdentifier);
231 }
232
233 void WebIDBConnectionToClient::commitTransaction(const IDBResourceIdentifier& transactionIdentifier)
234 {
235     m_networkProcess->idbServer(m_sessionID).commitTransaction(transactionIdentifier);
236 }
237
238 void WebIDBConnectionToClient::didFinishHandlingVersionChangeTransaction(uint64_t databaseConnectionIdentifier, const IDBResourceIdentifier& transactionIdentifier)
239 {
240     m_networkProcess->idbServer(m_sessionID).didFinishHandlingVersionChangeTransaction(databaseConnectionIdentifier, transactionIdentifier);
241 }
242
243 void WebIDBConnectionToClient::createObjectStore(const IDBRequestData& request, const IDBObjectStoreInfo& info)
244 {
245     m_networkProcess->idbServer(m_sessionID).createObjectStore(request, info);
246 }
247
248 void WebIDBConnectionToClient::deleteObjectStore(const IDBRequestData& request, const String& name)
249 {
250     m_networkProcess->idbServer(m_sessionID).deleteObjectStore(request, name);
251 }
252
253 void WebIDBConnectionToClient::renameObjectStore(const IDBRequestData& request, uint64_t objectStoreIdentifier, const String& newName)
254 {
255     m_networkProcess->idbServer(m_sessionID).renameObjectStore(request, objectStoreIdentifier, newName);
256 }
257
258 void WebIDBConnectionToClient::clearObjectStore(const IDBRequestData& request, uint64_t objectStoreIdentifier)
259 {
260     m_networkProcess->idbServer(m_sessionID).clearObjectStore(request, objectStoreIdentifier);
261 }
262
263 void WebIDBConnectionToClient::createIndex(const IDBRequestData& request, const IDBIndexInfo& info)
264 {
265     m_networkProcess->idbServer(m_sessionID).createIndex(request, info);
266 }
267
268 void WebIDBConnectionToClient::deleteIndex(const IDBRequestData& request, uint64_t objectStoreIdentifier, const String& name)
269 {
270     m_networkProcess->idbServer(m_sessionID).deleteIndex(request, objectStoreIdentifier, name);
271 }
272
273 void WebIDBConnectionToClient::renameIndex(const IDBRequestData& request, uint64_t objectStoreIdentifier, uint64_t indexIdentifier, const String& newName)
274 {
275     m_networkProcess->idbServer(m_sessionID).renameIndex(request, objectStoreIdentifier, indexIdentifier, newName);
276 }
277
278 void WebIDBConnectionToClient::putOrAdd(const IDBRequestData& request, const IDBKeyData& key, const IDBValue& value, unsigned overwriteMode)
279 {
280     if (overwriteMode != static_cast<unsigned>(IndexedDB::ObjectStoreOverwriteMode::NoOverwrite)
281         && overwriteMode != static_cast<unsigned>(IndexedDB::ObjectStoreOverwriteMode::Overwrite)
282         && overwriteMode != static_cast<unsigned>(IndexedDB::ObjectStoreOverwriteMode::OverwriteForCursor)) {
283         // FIXME: This message from the WebProcess is corrupt.
284         // The Network Process should return early at this point, but can we also kill the bad WebProcess?
285         return;
286     }
287
288     IndexedDB::ObjectStoreOverwriteMode mode = static_cast<IndexedDB::ObjectStoreOverwriteMode>(overwriteMode);
289
290     m_networkProcess->idbServer(m_sessionID).putOrAdd(request, key, value, mode);
291 }
292
293 void WebIDBConnectionToClient::getRecord(const IDBRequestData& request, const IDBGetRecordData& getRecordData)
294 {
295     m_networkProcess->idbServer(m_sessionID).getRecord(request, getRecordData);
296 }
297
298 void WebIDBConnectionToClient::getAllRecords(const IDBRequestData& request, const IDBGetAllRecordsData& getAllRecordsData)
299 {
300     m_networkProcess->idbServer(m_sessionID).getAllRecords(request, getAllRecordsData);
301 }
302
303 void WebIDBConnectionToClient::getCount(const IDBRequestData& request, const IDBKeyRangeData& range)
304 {
305     m_networkProcess->idbServer(m_sessionID).getCount(request, range);
306 }
307
308 void WebIDBConnectionToClient::deleteRecord(const IDBRequestData& request, const IDBKeyRangeData& range)
309 {
310     m_networkProcess->idbServer(m_sessionID).deleteRecord(request, range);
311 }
312
313 void WebIDBConnectionToClient::openCursor(const IDBRequestData& request, const IDBCursorInfo& info)
314 {
315     m_networkProcess->idbServer(m_sessionID).openCursor(request, info);
316 }
317
318 void WebIDBConnectionToClient::iterateCursor(const IDBRequestData& request, const IDBIterateCursorData& data)
319 {
320     m_networkProcess->idbServer(m_sessionID).iterateCursor(request, data);
321 }
322
323 void WebIDBConnectionToClient::establishTransaction(uint64_t databaseConnectionIdentifier, const IDBTransactionInfo& info)
324 {
325     m_networkProcess->idbServer(m_sessionID).establishTransaction(databaseConnectionIdentifier, info);
326 }
327
328 void WebIDBConnectionToClient::databaseConnectionPendingClose(uint64_t databaseConnectionIdentifier)
329 {
330     m_networkProcess->idbServer(m_sessionID).databaseConnectionPendingClose(databaseConnectionIdentifier);
331 }
332
333 void WebIDBConnectionToClient::databaseConnectionClosed(uint64_t databaseConnectionIdentifier)
334 {
335     m_networkProcess->idbServer(m_sessionID).databaseConnectionClosed(databaseConnectionIdentifier);
336 }
337
338 void WebIDBConnectionToClient::abortOpenAndUpgradeNeeded(uint64_t databaseConnectionIdentifier, const IDBResourceIdentifier& transactionIdentifier)
339 {
340     m_networkProcess->idbServer(m_sessionID).abortOpenAndUpgradeNeeded(databaseConnectionIdentifier, transactionIdentifier);
341 }
342
343 void WebIDBConnectionToClient::didFireVersionChangeEvent(uint64_t databaseConnectionIdentifier, const IDBResourceIdentifier& transactionIdentifier)
344 {
345     m_networkProcess->idbServer(m_sessionID).didFireVersionChangeEvent(databaseConnectionIdentifier, transactionIdentifier);
346 }
347
348 void WebIDBConnectionToClient::openDBRequestCancelled(const IDBRequestData& requestData)
349 {
350     m_networkProcess->idbServer(m_sessionID).openDBRequestCancelled(requestData);
351 }
352
353 void WebIDBConnectionToClient::confirmDidCloseFromServer(uint64_t databaseConnectionIdentifier)
354 {
355     m_networkProcess->idbServer(m_sessionID).confirmDidCloseFromServer(databaseConnectionIdentifier);
356 }
357
358 void WebIDBConnectionToClient::getAllDatabaseNames(uint64_t serverConnectionIdentifier, const WebCore::SecurityOriginData& topOrigin, const WebCore::SecurityOriginData& openingOrigin, uint64_t callbackID)
359 {
360     m_networkProcess->idbServer(m_sessionID).getAllDatabaseNames(serverConnectionIdentifier, topOrigin, openingOrigin, callbackID);
361 }
362
363 } // namespace WebKit
364
365 #endif // ENABLE(INDEXED_DATABASE)