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
44namespace WebKit {
45using namespace WebCore;
46
47Ref<WebIDBConnectionToClient> WebIDBConnectionToClient::create(NetworkProcess& networkProcess, IPC::Connection& connection, uint64_t serverConnectionIdentifier, PAL::SessionID sessionID)
48{
49 return adoptRef(*new WebIDBConnectionToClient(networkProcess, connection, serverConnectionIdentifier, sessionID));
50}
51
52WebIDBConnectionToClient::WebIDBConnectionToClient(NetworkProcess& networkProcess, IPC::Connection& 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
63WebIDBConnectionToClient::~WebIDBConnectionToClient()
64{
65}
66
67void WebIDBConnectionToClient::disconnectedFromWebProcess()
68{
69 m_networkProcess->idbServer(m_sessionID).unregisterConnection(*m_connectionToClient);
70}
71
72IPC::Connection* WebIDBConnectionToClient::messageSenderConnection() const
73{
74 return m_connection.ptr();
75}
76
77WebCore::IDBServer::IDBConnectionToClient& WebIDBConnectionToClient::connectionToClient()
78{
79 return *m_connectionToClient;
80}
81
82void WebIDBConnectionToClient::didDeleteDatabase(const WebCore::IDBResultData& resultData)
83{
84 send(Messages::WebIDBConnectionToServer::DidDeleteDatabase(resultData));
85}
86
87void WebIDBConnectionToClient::didOpenDatabase(const WebCore::IDBResultData& resultData)
88{
89 send(Messages::WebIDBConnectionToServer::DidOpenDatabase(resultData));
90}
91
92void WebIDBConnectionToClient::didAbortTransaction(const WebCore::IDBResourceIdentifier& transactionIdentifier, const WebCore::IDBError& error)
93{
94 send(Messages::WebIDBConnectionToServer::DidAbortTransaction(transactionIdentifier, error));
95}
96
97void WebIDBConnectionToClient::didCommitTransaction(const WebCore::IDBResourceIdentifier& transactionIdentifier, const WebCore::IDBError& error)
98{
99 send(Messages::WebIDBConnectionToServer::DidCommitTransaction(transactionIdentifier, error));
100}
101
102void WebIDBConnectionToClient::didCreateObjectStore(const WebCore::IDBResultData& resultData)
103{
104 send(Messages::WebIDBConnectionToServer::DidCreateObjectStore(resultData));
105}
106
107void WebIDBConnectionToClient::didDeleteObjectStore(const WebCore::IDBResultData& resultData)
108{
109 send(Messages::WebIDBConnectionToServer::DidDeleteObjectStore(resultData));
110}
111
112void WebIDBConnectionToClient::didRenameObjectStore(const WebCore::IDBResultData& resultData)
113{
114 send(Messages::WebIDBConnectionToServer::DidRenameObjectStore(resultData));
115}
116
117void WebIDBConnectionToClient::didClearObjectStore(const WebCore::IDBResultData& resultData)
118{
119 send(Messages::WebIDBConnectionToServer::DidClearObjectStore(resultData));
120}
121
122void WebIDBConnectionToClient::didCreateIndex(const WebCore::IDBResultData& resultData)
123{
124 send(Messages::WebIDBConnectionToServer::DidCreateIndex(resultData));
125}
126
127void WebIDBConnectionToClient::didDeleteIndex(const WebCore::IDBResultData& resultData)
128{
129 send(Messages::WebIDBConnectionToServer::DidDeleteIndex(resultData));
130}
131
132void WebIDBConnectionToClient::didRenameIndex(const WebCore::IDBResultData& resultData)
133{
134 send(Messages::WebIDBConnectionToServer::DidRenameIndex(resultData));
135}
136
137void WebIDBConnectionToClient::didPutOrAdd(const WebCore::IDBResultData& resultData)
138{
139 send(Messages::WebIDBConnectionToServer::DidPutOrAdd(resultData));
140}
141
142template<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
163void WebIDBConnectionToClient::didGetRecord(const WebCore::IDBResultData& resultData)
164{
165 handleGetResult<Messages::WebIDBConnectionToServer::DidGetRecord>(resultData);
166}
167
168void WebIDBConnectionToClient::didGetAllRecords(const WebCore::IDBResultData& resultData)
169{
170 handleGetResult<Messages::WebIDBConnectionToServer::DidGetAllRecords>(resultData);
171}
172
173void WebIDBConnectionToClient::didGetCount(const WebCore::IDBResultData& resultData)
174{
175 send(Messages::WebIDBConnectionToServer::DidGetCount(resultData));
176}
177
178void WebIDBConnectionToClient::didDeleteRecord(const WebCore::IDBResultData& resultData)
179{
180 send(Messages::WebIDBConnectionToServer::DidDeleteRecord(resultData));
181}
182
183void WebIDBConnectionToClient::didOpenCursor(const WebCore::IDBResultData& resultData)
184{
185 handleGetResult<Messages::WebIDBConnectionToServer::DidOpenCursor>(resultData);
186}
187
188void WebIDBConnectionToClient::didIterateCursor(const WebCore::IDBResultData& resultData)
189{
190 handleGetResult<Messages::WebIDBConnectionToServer::DidIterateCursor>(resultData);
191}
192
193void 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
198void WebIDBConnectionToClient::didStartTransaction(const WebCore::IDBResourceIdentifier& transactionIdentifier, const WebCore::IDBError& error)
199{
200 send(Messages::WebIDBConnectionToServer::DidStartTransaction(transactionIdentifier, error));
201}
202
203void WebIDBConnectionToClient::didCloseFromServer(WebCore::IDBServer::UniqueIDBDatabaseConnection& connection, const WebCore::IDBError& error)
204{
205 send(Messages::WebIDBConnectionToServer::DidCloseFromServer(connection.identifier(), error));
206}
207
208void WebIDBConnectionToClient::notifyOpenDBRequestBlocked(const WebCore::IDBResourceIdentifier& requestIdentifier, uint64_t oldVersion, uint64_t newVersion)
209{
210 send(Messages::WebIDBConnectionToServer::NotifyOpenDBRequestBlocked(requestIdentifier, oldVersion, newVersion));
211}
212
213void WebIDBConnectionToClient::didGetAllDatabaseNames(uint64_t callbackID, const Vector<String>& databaseNames)
214{
215 send(Messages::WebIDBConnectionToServer::DidGetAllDatabaseNames(callbackID, databaseNames));
216}
217
218void WebIDBConnectionToClient::deleteDatabase(const IDBRequestData& request)
219{
220 m_networkProcess->idbServer(m_sessionID).deleteDatabase(request);
221}
222
223void WebIDBConnectionToClient::openDatabase(const IDBRequestData& request)
224{
225 m_networkProcess->idbServer(m_sessionID).openDatabase(request);
226}
227
228void WebIDBConnectionToClient::abortTransaction(const IDBResourceIdentifier& transactionIdentifier)
229{
230 m_networkProcess->idbServer(m_sessionID).abortTransaction(transactionIdentifier);
231}
232
233void WebIDBConnectionToClient::commitTransaction(const IDBResourceIdentifier& transactionIdentifier)
234{
235 m_networkProcess->idbServer(m_sessionID).commitTransaction(transactionIdentifier);
236}
237
238void WebIDBConnectionToClient::didFinishHandlingVersionChangeTransaction(uint64_t databaseConnectionIdentifier, const IDBResourceIdentifier& transactionIdentifier)
239{
240 m_networkProcess->idbServer(m_sessionID).didFinishHandlingVersionChangeTransaction(databaseConnectionIdentifier, transactionIdentifier);
241}
242
243void WebIDBConnectionToClient::createObjectStore(const IDBRequestData& request, const IDBObjectStoreInfo& info)
244{
245 m_networkProcess->idbServer(m_sessionID).createObjectStore(request, info);
246}
247
248void WebIDBConnectionToClient::deleteObjectStore(const IDBRequestData& request, const String& name)
249{
250 m_networkProcess->idbServer(m_sessionID).deleteObjectStore(request, name);
251}
252
253void WebIDBConnectionToClient::renameObjectStore(const IDBRequestData& request, uint64_t objectStoreIdentifier, const String& newName)
254{
255 m_networkProcess->idbServer(m_sessionID).renameObjectStore(request, objectStoreIdentifier, newName);
256}
257
258void WebIDBConnectionToClient::clearObjectStore(const IDBRequestData& request, uint64_t objectStoreIdentifier)
259{
260 m_networkProcess->idbServer(m_sessionID).clearObjectStore(request, objectStoreIdentifier);
261}
262
263void WebIDBConnectionToClient::createIndex(const IDBRequestData& request, const IDBIndexInfo& info)
264{
265 m_networkProcess->idbServer(m_sessionID).createIndex(request, info);
266}
267
268void WebIDBConnectionToClient::deleteIndex(const IDBRequestData& request, uint64_t objectStoreIdentifier, const String& name)
269{
270 m_networkProcess->idbServer(m_sessionID).deleteIndex(request, objectStoreIdentifier, name);
271}
272
273void 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
278void WebIDBConnectionToClient::putOrAdd(const IDBRequestData& request, const IDBKeyData& key, const IDBValue& value, IndexedDB::ObjectStoreOverwriteMode mode)
279{
280 m_networkProcess->idbServer(m_sessionID).putOrAdd(request, key, value, mode);
281}
282
283void WebIDBConnectionToClient::getRecord(const IDBRequestData& request, const IDBGetRecordData& getRecordData)
284{
285 m_networkProcess->idbServer(m_sessionID).getRecord(request, getRecordData);
286}
287
288void WebIDBConnectionToClient::getAllRecords(const IDBRequestData& request, const IDBGetAllRecordsData& getAllRecordsData)
289{
290 m_networkProcess->idbServer(m_sessionID).getAllRecords(request, getAllRecordsData);
291}
292
293void WebIDBConnectionToClient::getCount(const IDBRequestData& request, const IDBKeyRangeData& range)
294{
295 m_networkProcess->idbServer(m_sessionID).getCount(request, range);
296}
297
298void WebIDBConnectionToClient::deleteRecord(const IDBRequestData& request, const IDBKeyRangeData& range)
299{
300 m_networkProcess->idbServer(m_sessionID).deleteRecord(request, range);
301}
302
303void WebIDBConnectionToClient::openCursor(const IDBRequestData& request, const IDBCursorInfo& info)
304{
305 m_networkProcess->idbServer(m_sessionID).openCursor(request, info);
306}
307
308void WebIDBConnectionToClient::iterateCursor(const IDBRequestData& request, const IDBIterateCursorData& data)
309{
310 m_networkProcess->idbServer(m_sessionID).iterateCursor(request, data);
311}
312
313void WebIDBConnectionToClient::establishTransaction(uint64_t databaseConnectionIdentifier, const IDBTransactionInfo& info)
314{
315 m_networkProcess->idbServer(m_sessionID).establishTransaction(databaseConnectionIdentifier, info);
316}
317
318void WebIDBConnectionToClient::databaseConnectionPendingClose(uint64_t databaseConnectionIdentifier)
319{
320 m_networkProcess->idbServer(m_sessionID).databaseConnectionPendingClose(databaseConnectionIdentifier);
321}
322
323void WebIDBConnectionToClient::databaseConnectionClosed(uint64_t databaseConnectionIdentifier)
324{
325 m_networkProcess->idbServer(m_sessionID).databaseConnectionClosed(databaseConnectionIdentifier);
326}
327
328void WebIDBConnectionToClient::abortOpenAndUpgradeNeeded(uint64_t databaseConnectionIdentifier, const IDBResourceIdentifier& transactionIdentifier)
329{
330 m_networkProcess->idbServer(m_sessionID).abortOpenAndUpgradeNeeded(databaseConnectionIdentifier, transactionIdentifier);
331}
332
333void WebIDBConnectionToClient::didFireVersionChangeEvent(uint64_t databaseConnectionIdentifier, const IDBResourceIdentifier& transactionIdentifier)
334{
335 m_networkProcess->idbServer(m_sessionID).didFireVersionChangeEvent(databaseConnectionIdentifier, transactionIdentifier);
336}
337
338void WebIDBConnectionToClient::openDBRequestCancelled(const IDBRequestData& requestData)
339{
340 m_networkProcess->idbServer(m_sessionID).openDBRequestCancelled(requestData);
341}
342
343void WebIDBConnectionToClient::confirmDidCloseFromServer(uint64_t databaseConnectionIdentifier)
344{
345 m_networkProcess->idbServer(m_sessionID).confirmDidCloseFromServer(databaseConnectionIdentifier);
346}
347
348void WebIDBConnectionToClient::getAllDatabaseNames(uint64_t serverConnectionIdentifier, const WebCore::SecurityOriginData& topOrigin, const WebCore::SecurityOriginData& openingOrigin, uint64_t callbackID)
349{
350 m_networkProcess->idbServer(m_sessionID).getAllDatabaseNames(serverConnectionIdentifier, topOrigin, openingOrigin, callbackID);
351}
352
353} // namespace WebKit
354
355#endif // ENABLE(INDEXED_DATABASE)
356