Use SecurityOriginData more consistently in Service Worker code
[WebKit-https.git] / Source / WebCore / Modules / indexeddb / client / IDBConnectionToServer.cpp
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 #include "config.h"
27 #include "IDBConnectionToServer.h"
28
29 #if ENABLE(INDEXED_DATABASE)
30
31 #include "IDBConnectionProxy.h"
32 #include "IDBDatabase.h"
33 #include "IDBGetRecordData.h"
34 #include "IDBKeyRangeData.h"
35 #include "IDBOpenDBRequest.h"
36 #include "IDBRequestData.h"
37 #include "IDBResultData.h"
38 #include "Logging.h"
39 #include "TransactionOperation.h"
40 #include <wtf/MainThread.h>
41
42 namespace WebCore {
43 namespace IDBClient {
44
45 Ref<IDBConnectionToServer> IDBConnectionToServer::create(IDBConnectionToServerDelegate& delegate)
46 {
47     return adoptRef(*new IDBConnectionToServer(delegate));
48 }
49
50 IDBConnectionToServer::IDBConnectionToServer(IDBConnectionToServerDelegate& delegate)
51     : m_delegate(delegate)
52     , m_proxy(std::make_unique<IDBConnectionProxy>(*this))
53 {
54 }
55
56 uint64_t IDBConnectionToServer::identifier() const
57 {
58     return m_delegate->identifier();
59 }
60
61 IDBConnectionProxy& IDBConnectionToServer::proxy()
62 {
63     ASSERT(m_proxy);
64     return *m_proxy;
65 }
66
67 void IDBConnectionToServer::deleteDatabase(const IDBRequestData& request)
68 {
69     LOG(IndexedDB, "IDBConnectionToServer::deleteDatabase - %s", request.databaseIdentifier().debugString().utf8().data());
70     m_delegate->deleteDatabase(request);
71 }
72
73 void IDBConnectionToServer::didDeleteDatabase(const IDBResultData& resultData)
74 {
75     LOG(IndexedDB, "IDBConnectionToServer::didDeleteDatabase");
76     m_proxy->didDeleteDatabase(resultData);
77 }
78
79 void IDBConnectionToServer::openDatabase(const IDBRequestData& request)
80 {
81     LOG(IndexedDB, "IDBConnectionToServer::openDatabase - %s (%s) (%" PRIu64 ")", request.databaseIdentifier().debugString().utf8().data(), request.requestIdentifier().loggingString().utf8().data(), request.requestedVersion());
82     m_delegate->openDatabase(request);
83 }
84
85 void IDBConnectionToServer::didOpenDatabase(const IDBResultData& resultData)
86 {
87     LOG(IndexedDB, "IDBConnectionToServer::didOpenDatabase");
88     m_proxy->didOpenDatabase(resultData);
89 }
90
91 void IDBConnectionToServer::createObjectStore(const IDBRequestData& requestData, const IDBObjectStoreInfo& info)
92 {
93     LOG(IndexedDB, "IDBConnectionToServer::createObjectStore");
94     ASSERT(isMainThread());
95
96     m_delegate->createObjectStore(requestData, info);
97 }
98
99 void IDBConnectionToServer::didCreateObjectStore(const IDBResultData& resultData)
100 {
101     LOG(IndexedDB, "IDBConnectionToServer::didCreateObjectStore");
102     m_proxy->completeOperation(resultData);
103 }
104
105 void IDBConnectionToServer::deleteObjectStore(const IDBRequestData& requestData, const String& objectStoreName)
106 {
107     LOG(IndexedDB, "IDBConnectionToServer::deleteObjectStore");
108     ASSERT(isMainThread());
109
110     m_delegate->deleteObjectStore(requestData, objectStoreName);
111 }
112
113 void IDBConnectionToServer::didDeleteObjectStore(const IDBResultData& resultData)
114 {
115     LOG(IndexedDB, "IDBConnectionToServer::didDeleteObjectStore");
116     m_proxy->completeOperation(resultData);
117 }
118
119 void IDBConnectionToServer::renameObjectStore(const IDBRequestData& requestData, uint64_t objectStoreIdentifier, const String& newName)
120 {
121     LOG(IndexedDB, "IDBConnectionToServer::renameObjectStore");
122     ASSERT(isMainThread());
123
124     m_delegate->renameObjectStore(requestData, objectStoreIdentifier, newName);
125 }
126
127 void IDBConnectionToServer::didRenameObjectStore(const IDBResultData& resultData)
128 {
129     LOG(IndexedDB, "IDBConnectionToServer::didRenameObjectStore");
130     m_proxy->completeOperation(resultData);
131 }
132
133 void IDBConnectionToServer::clearObjectStore(const IDBRequestData& requestData, uint64_t objectStoreIdentifier)
134 {
135     LOG(IndexedDB, "IDBConnectionToServer::clearObjectStore");
136     ASSERT(isMainThread());
137
138     m_delegate->clearObjectStore(requestData, objectStoreIdentifier);
139 }
140
141 void IDBConnectionToServer::didClearObjectStore(const IDBResultData& resultData)
142 {
143     LOG(IndexedDB, "IDBConnectionToServer::didClearObjectStore");
144     m_proxy->completeOperation(resultData);
145 }
146
147 void IDBConnectionToServer::createIndex(const IDBRequestData& requestData, const IDBIndexInfo& info)
148 {
149     LOG(IndexedDB, "IDBConnectionToServer::createIndex");
150     ASSERT(isMainThread());
151
152     m_delegate->createIndex(requestData, info);
153 }
154
155 void IDBConnectionToServer::didCreateIndex(const IDBResultData& resultData)
156 {
157     LOG(IndexedDB, "IDBConnectionToServer::didCreateIndex");
158     m_proxy->completeOperation(resultData);
159 }
160
161 void IDBConnectionToServer::deleteIndex(const IDBRequestData& requestData, uint64_t objectStoreIdentifier, const String& indexName)
162 {
163     LOG(IndexedDB, "IDBConnectionToServer::deleteIndex");
164     ASSERT(isMainThread());
165
166     m_delegate->deleteIndex(requestData, objectStoreIdentifier, indexName);
167 }
168
169 void IDBConnectionToServer::didDeleteIndex(const IDBResultData& resultData)
170 {
171     LOG(IndexedDB, "IDBConnectionToServer::didDeleteIndex");
172     m_proxy->completeOperation(resultData);
173 }
174
175 void IDBConnectionToServer::renameIndex(const IDBRequestData& requestData, uint64_t objectStoreIdentifier, uint64_t indexIdentifier, const String& newName)
176 {
177     LOG(IndexedDB, "IDBConnectionToServer::renameIndex");
178     ASSERT(isMainThread());
179
180     m_delegate->renameIndex(requestData, objectStoreIdentifier, indexIdentifier, newName);
181 }
182
183 void IDBConnectionToServer::didRenameIndex(const IDBResultData& resultData)
184 {
185     LOG(IndexedDB, "IDBConnectionToServer::didRenameIndex");
186     m_proxy->completeOperation(resultData);
187 }
188
189 void IDBConnectionToServer::putOrAdd(const IDBRequestData& requestData, const IDBKeyData& key, const IDBValue& value, const IndexedDB::ObjectStoreOverwriteMode overwriteMode)
190 {
191     LOG(IndexedDB, "IDBConnectionToServer::putOrAdd");
192     ASSERT(isMainThread());
193
194     m_delegate->putOrAdd(requestData, key, value, overwriteMode);
195 }
196
197 void IDBConnectionToServer::didPutOrAdd(const IDBResultData& resultData)
198 {
199     LOG(IndexedDB, "IDBConnectionToServer::didPutOrAdd");
200     m_proxy->completeOperation(resultData);
201 }
202
203 void IDBConnectionToServer::getRecord(const IDBRequestData& requestData, const IDBGetRecordData& getRecordData)
204 {
205     LOG(IndexedDB, "IDBConnectionToServer::getRecord");
206     ASSERT(isMainThread());
207     ASSERT(!getRecordData.keyRangeData.isNull);
208
209     m_delegate->getRecord(requestData, getRecordData);
210 }
211
212 void IDBConnectionToServer::didGetRecord(const IDBResultData& resultData)
213 {
214     LOG(IndexedDB, "IDBConnectionToServer::didGetRecord");
215     m_proxy->completeOperation(resultData);
216 }
217
218 void IDBConnectionToServer::getAllRecords(const IDBRequestData& requestData, const IDBGetAllRecordsData& getAllRecordsData)
219 {
220     LOG(IndexedDB, "IDBConnectionToServer::getAllRecords");
221     ASSERT(isMainThread());
222
223     m_delegate->getAllRecords(requestData, getAllRecordsData);
224 }
225
226 void IDBConnectionToServer::didGetAllRecords(const IDBResultData& resultData)
227 {
228     LOG(IndexedDB, "IDBConnectionToServer::didGetAllRecords");
229     m_proxy->completeOperation(resultData);
230 }
231
232 void IDBConnectionToServer::getCount(const IDBRequestData& requestData, const IDBKeyRangeData& keyRangeData)
233 {
234     LOG(IndexedDB, "IDBConnectionToServer::getCount");
235     ASSERT(isMainThread());
236     ASSERT(!keyRangeData.isNull);
237
238     m_delegate->getCount(requestData, keyRangeData);
239 }
240
241 void IDBConnectionToServer::didGetCount(const IDBResultData& resultData)
242 {
243     LOG(IndexedDB, "IDBConnectionToServer::didGetCount");
244     m_proxy->completeOperation(resultData);
245 }
246
247 void IDBConnectionToServer::deleteRecord(const IDBRequestData& requestData, const IDBKeyRangeData& keyRangeData)
248 {
249     LOG(IndexedDB, "IDBConnectionToServer::deleteRecord");
250     ASSERT(isMainThread());
251     ASSERT(!keyRangeData.isNull);
252
253     m_delegate->deleteRecord(requestData, keyRangeData);
254 }
255
256 void IDBConnectionToServer::didDeleteRecord(const IDBResultData& resultData)
257 {
258     LOG(IndexedDB, "IDBConnectionToServer::didDeleteRecord");
259     m_proxy->completeOperation(resultData);
260 }
261
262 void IDBConnectionToServer::openCursor(const IDBRequestData& requestData, const IDBCursorInfo& info)
263 {
264     LOG(IndexedDB, "IDBConnectionToServer::openCursor");
265     ASSERT(isMainThread());
266
267     m_delegate->openCursor(requestData, info);
268 }
269
270 void IDBConnectionToServer::didOpenCursor(const IDBResultData& resultData)
271 {
272     LOG(IndexedDB, "IDBConnectionToServer::didOpenCursor");
273     m_proxy->completeOperation(resultData);
274 }
275
276 void IDBConnectionToServer::iterateCursor(const IDBRequestData& requestData, const IDBIterateCursorData& data)
277 {
278     LOG(IndexedDB, "IDBConnectionToServer::iterateCursor");
279     ASSERT(isMainThread());
280
281     m_delegate->iterateCursor(requestData, data);
282 }
283
284 void IDBConnectionToServer::didIterateCursor(const IDBResultData& resultData)
285 {
286     LOG(IndexedDB, "IDBConnectionToServer::didIterateCursor");
287     m_proxy->completeOperation(resultData);
288 }
289
290 void IDBConnectionToServer::establishTransaction(uint64_t databaseConnectionIdentifier, const IDBTransactionInfo& info)
291 {
292     LOG(IndexedDB, "IDBConnectionToServer::establishTransaction");
293     ASSERT(isMainThread());
294
295     m_delegate->establishTransaction(databaseConnectionIdentifier, info);
296 }
297
298 void IDBConnectionToServer::commitTransaction(const IDBResourceIdentifier& transactionIdentifier)
299 {
300     LOG(IndexedDB, "IDBConnectionToServer::commitTransaction");
301     ASSERT(isMainThread());
302
303     m_delegate->commitTransaction(transactionIdentifier);
304 }
305
306 void IDBConnectionToServer::didCommitTransaction(const IDBResourceIdentifier& transactionIdentifier, const IDBError& error)
307 {
308     LOG(IndexedDB, "IDBConnectionToServer::didCommitTransaction");
309     ASSERT(isMainThread());
310
311     m_proxy->didCommitTransaction(transactionIdentifier, error);
312 }
313
314 void IDBConnectionToServer::didFinishHandlingVersionChangeTransaction(uint64_t databaseConnectionIdentifier, const IDBResourceIdentifier& transactionIdentifier)
315 {
316     LOG(IndexedDB, "IDBConnectionToServer::didFinishHandlingVersionChangeTransaction");
317     ASSERT(isMainThread());
318
319     m_delegate->didFinishHandlingVersionChangeTransaction(databaseConnectionIdentifier, transactionIdentifier);
320 }
321
322 void IDBConnectionToServer::abortTransaction(const IDBResourceIdentifier& transactionIdentifier)
323 {
324     LOG(IndexedDB, "IDBConnectionToServer::abortTransaction");
325     ASSERT(isMainThread());
326
327     m_delegate->abortTransaction(transactionIdentifier);
328 }
329
330 void IDBConnectionToServer::didAbortTransaction(const IDBResourceIdentifier& transactionIdentifier, const IDBError& error)
331 {
332     LOG(IndexedDB, "IDBConnectionToServer::didAbortTransaction");
333     ASSERT(isMainThread());
334
335     m_proxy->didAbortTransaction(transactionIdentifier, error);
336 }
337
338 void IDBConnectionToServer::fireVersionChangeEvent(uint64_t databaseConnectionIdentifier, const IDBResourceIdentifier& requestIdentifier, uint64_t requestedVersion)
339 {
340     LOG(IndexedDB, "IDBConnectionToServer::fireVersionChangeEvent");
341     ASSERT(isMainThread());
342
343     m_proxy->fireVersionChangeEvent(databaseConnectionIdentifier, requestIdentifier, requestedVersion);
344 }
345
346 void IDBConnectionToServer::didFireVersionChangeEvent(uint64_t databaseConnectionIdentifier, const IDBResourceIdentifier& requestIdentifier)
347 {
348     LOG(IndexedDB, "IDBConnectionToServer::didFireVersionChangeEvent");
349     ASSERT(isMainThread());
350
351     m_delegate->didFireVersionChangeEvent(databaseConnectionIdentifier, requestIdentifier);
352 }
353
354 void IDBConnectionToServer::didStartTransaction(const IDBResourceIdentifier& transactionIdentifier, const IDBError& error)
355 {
356     LOG(IndexedDB, "IDBConnectionToServer::didStartTransaction");
357     ASSERT(isMainThread());
358
359     m_proxy->didStartTransaction(transactionIdentifier, error);
360 }
361
362 void IDBConnectionToServer::didCloseFromServer(uint64_t databaseConnectionIdentifier, const IDBError& error)
363 {
364     LOG(IndexedDB, "IDBConnectionToServer::didCloseFromServer");
365     ASSERT(isMainThread());
366
367     m_proxy->didCloseFromServer(databaseConnectionIdentifier, error);
368 }
369
370 void IDBConnectionToServer::confirmDidCloseFromServer(uint64_t databaseConnectionIdentifier)
371 {
372     LOG(IndexedDB, "IDBConnectionToServer::confirmDidCloseFromServer");
373     ASSERT(isMainThread());
374
375     m_delegate->confirmDidCloseFromServer(databaseConnectionIdentifier);
376 }
377
378 void IDBConnectionToServer::connectionToServerLost(const IDBError& error)
379 {
380     LOG(IndexedDB, "IDBConnectionToServer::connectionToServerLost");
381     ASSERT(isMainThread());
382
383     m_proxy->connectionToServerLost(error);
384 }
385
386 void IDBConnectionToServer::notifyOpenDBRequestBlocked(const IDBResourceIdentifier& requestIdentifier, uint64_t oldVersion, uint64_t newVersion)
387 {
388     LOG(IndexedDB, "IDBConnectionToServer::didStartTransaction");
389     ASSERT(isMainThread());
390
391     m_proxy->notifyOpenDBRequestBlocked(requestIdentifier, oldVersion, newVersion);
392 }
393
394 void IDBConnectionToServer::openDBRequestCancelled(const IDBRequestData& requestData)
395 {
396     LOG(IndexedDB, "IDBConnectionToServer::openDBRequestCancelled");
397     ASSERT(isMainThread());
398
399     m_delegate->openDBRequestCancelled(requestData);
400 }
401
402 void IDBConnectionToServer::databaseConnectionPendingClose(uint64_t databaseConnectionIdentifier)
403 {
404     LOG(IndexedDB, "IDBConnectionToServer::databaseConnectionPendingClose");
405     ASSERT(isMainThread());
406
407     m_delegate->databaseConnectionPendingClose(databaseConnectionIdentifier);
408 }
409
410 void IDBConnectionToServer::databaseConnectionClosed(uint64_t databaseConnectionIdentifier)
411 {
412     LOG(IndexedDB, "IDBConnectionToServer::databaseConnectionClosed");
413     ASSERT(isMainThread());
414
415     m_delegate->databaseConnectionClosed(databaseConnectionIdentifier);
416 }
417
418 void IDBConnectionToServer::abortOpenAndUpgradeNeeded(uint64_t databaseConnectionIdentifier, const IDBResourceIdentifier& transactionIdentifier)
419 {
420     LOG(IndexedDB, "IDBConnectionToServer::abortOpenAndUpgradeNeeded");
421     ASSERT(isMainThread());
422
423     m_delegate->abortOpenAndUpgradeNeeded(databaseConnectionIdentifier, transactionIdentifier);
424 }
425
426 void IDBConnectionToServer::getAllDatabaseNames(const SecurityOrigin& mainFrameOrigin, const SecurityOrigin& openingOrigin, Function<void (const Vector<String>&)>&& callback)
427 {
428     static uint64_t callbackID = 0;
429
430     m_getAllDatabaseNamesCallbacks.add(++callbackID, WTFMove(callback));
431
432     m_delegate->getAllDatabaseNames(mainFrameOrigin.data(), openingOrigin.data(), callbackID);
433 }
434
435 void IDBConnectionToServer::didGetAllDatabaseNames(uint64_t callbackID, const Vector<String>& databaseNames)
436 {
437     auto callback = m_getAllDatabaseNamesCallbacks.take(callbackID);
438     ASSERT(callback);
439
440     callback(databaseNames);
441 }
442
443 } // namespace IDBClient
444 } // namespace WebCore
445
446 #endif // ENABLE(INDEXED_DATABASE)