6e3b5421aafb4bcd005d6253ae94a0a8df6e070f
[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 "IDBKeyRangeData.h"
32 #include "IDBOpenDBRequestImpl.h"
33 #include "IDBRequestData.h"
34 #include "IDBResultData.h"
35 #include "Logging.h"
36 #include "TransactionOperation.h"
37
38 namespace WebCore {
39 namespace IDBClient {
40
41 Ref<IDBConnectionToServer> IDBConnectionToServer::create(IDBConnectionToServerDelegate& delegate)
42 {
43     return adoptRef(*new IDBConnectionToServer(delegate));
44 }
45
46 IDBConnectionToServer::IDBConnectionToServer(IDBConnectionToServerDelegate& delegate)
47     : m_delegate(delegate)
48 {
49 }
50
51 uint64_t IDBConnectionToServer::identifier() const
52 {
53     return m_delegate->identifier();
54 }
55
56 void IDBConnectionToServer::deleteDatabase(IDBOpenDBRequest& request)
57 {
58     LOG(IndexedDB, "IDBConnectionToServer::deleteDatabase - %s", request.databaseIdentifier().debugString().utf8().data());
59
60     ASSERT(!m_openDBRequestMap.contains(request.resourceIdentifier()));
61     m_openDBRequestMap.set(request.resourceIdentifier(), &request);
62     
63     IDBRequestData requestData(*this, request);
64     m_delegate->deleteDatabase(requestData);
65 }
66
67 void IDBConnectionToServer::didDeleteDatabase(const IDBResultData& resultData)
68 {
69     LOG(IndexedDB, "IDBConnectionToServer::didDeleteDatabase");
70
71     auto request = m_openDBRequestMap.take(resultData.requestIdentifier());
72     ASSERT(request);
73
74     request->requestCompleted(resultData);
75 }
76
77 void IDBConnectionToServer::openDatabase(IDBOpenDBRequest& request)
78 {
79     LOG(IndexedDB, "IDBConnectionToServer::openDatabase - %s", request.databaseIdentifier().debugString().utf8().data());
80
81     ASSERT(!m_openDBRequestMap.contains(request.resourceIdentifier()));
82     m_openDBRequestMap.set(request.resourceIdentifier(), &request);
83     
84     IDBRequestData requestData(*this, request);
85     m_delegate->openDatabase(requestData);
86 }
87
88 void IDBConnectionToServer::didOpenDatabase(const IDBResultData& resultData)
89 {
90     LOG(IndexedDB, "IDBConnectionToServer::didOpenDatabase");
91
92     auto request = m_openDBRequestMap.take(resultData.requestIdentifier());
93     ASSERT(request);
94
95     request->requestCompleted(resultData);
96 }
97
98 void IDBConnectionToServer::createObjectStore(TransactionOperation& operation, const IDBObjectStoreInfo& info)
99 {
100     LOG(IndexedDB, "IDBConnectionToServer::createObjectStore");
101
102     saveOperation(operation);
103
104     m_delegate->createObjectStore(IDBRequestData(operation), info);
105 }
106
107 void IDBConnectionToServer::didCreateObjectStore(const IDBResultData& resultData)
108 {
109     LOG(IndexedDB, "IDBConnectionToServer::didCreateObjectStore");
110     completeOperation(resultData);
111 }
112
113 void IDBConnectionToServer::deleteObjectStore(TransactionOperation& operation, const String& objectStoreName)
114 {
115     LOG(IndexedDB, "IDBConnectionToServer::deleteObjectStore");
116
117     saveOperation(operation);
118
119     m_delegate->deleteObjectStore(IDBRequestData(operation), objectStoreName);
120 }
121
122 void IDBConnectionToServer::didDeleteObjectStore(const IDBResultData& resultData)
123 {
124     LOG(IndexedDB, "IDBConnectionToServer::didDeleteObjectStore");
125     completeOperation(resultData);
126 }
127
128 void IDBConnectionToServer::clearObjectStore(TransactionOperation& operation, uint64_t objectStoreIdentifier)
129 {
130     LOG(IndexedDB, "IDBConnectionToServer::clearObjectStore");
131
132     saveOperation(operation);
133
134     m_delegate->clearObjectStore(IDBRequestData(operation), objectStoreIdentifier);
135 }
136
137 void IDBConnectionToServer::didClearObjectStore(const IDBResultData& resultData)
138 {
139     LOG(IndexedDB, "IDBConnectionToServer::didClearObjectStore");
140     completeOperation(resultData);
141 }
142
143 void IDBConnectionToServer::createIndex(TransactionOperation& operation, const IDBIndexInfo& info)
144 {
145     LOG(IndexedDB, "IDBConnectionToServer::createIndex");
146
147     saveOperation(operation);
148
149     m_delegate->createIndex(IDBRequestData(operation), info);
150 }
151
152 void IDBConnectionToServer::didCreateIndex(const IDBResultData& resultData)
153 {
154     LOG(IndexedDB, "IDBConnectionToServer::didCreateIndex");
155     completeOperation(resultData);
156 }
157
158 void IDBConnectionToServer::deleteIndex(TransactionOperation& operation, uint64_t objectStoreIdentifier, const String& indexName)
159 {
160     LOG(IndexedDB, "IDBConnectionToServer::deleteIndex");
161
162     saveOperation(operation);
163
164     m_delegate->deleteIndex(IDBRequestData(operation), objectStoreIdentifier, indexName);
165 }
166
167 void IDBConnectionToServer::didDeleteIndex(const IDBResultData& resultData)
168 {
169     LOG(IndexedDB, "IDBConnectionToServer::didDeleteIndex");
170     completeOperation(resultData);
171 }
172
173 void IDBConnectionToServer::putOrAdd(TransactionOperation& operation, RefPtr<IDBKey>& key, RefPtr<SerializedScriptValue>& value, const IndexedDB::ObjectStoreOverwriteMode overwriteMode)
174 {
175     LOG(IndexedDB, "IDBConnectionToServer::putOrAdd");
176
177     saveOperation(operation);
178     m_delegate->putOrAdd(IDBRequestData(operation), key.get(), *value, overwriteMode);
179 }
180
181 void IDBConnectionToServer::didPutOrAdd(const IDBResultData& resultData)
182 {
183     LOG(IndexedDB, "IDBConnectionToServer::didPutOrAdd");
184     completeOperation(resultData);
185 }
186
187 void IDBConnectionToServer::getRecord(TransactionOperation& operation, const IDBKeyRangeData& keyRangeData)
188 {
189     LOG(IndexedDB, "IDBConnectionToServer::getRecord");
190
191     ASSERT(!keyRangeData.isNull);
192
193     saveOperation(operation);
194     m_delegate->getRecord(IDBRequestData(operation), keyRangeData);
195 }
196
197 void IDBConnectionToServer::didGetRecord(const IDBResultData& resultData)
198 {
199     LOG(IndexedDB, "IDBConnectionToServer::didGetRecord");
200     completeOperation(resultData);
201 }
202
203 void IDBConnectionToServer::getCount(TransactionOperation& operation, const IDBKeyRangeData& keyRangeData)
204 {
205     LOG(IndexedDB, "IDBConnectionToServer::getCount");
206
207     ASSERT(!keyRangeData.isNull);
208
209     saveOperation(operation);
210     m_delegate->getCount(IDBRequestData(operation), keyRangeData);
211 }
212
213 void IDBConnectionToServer::didGetCount(const IDBResultData& resultData)
214 {
215     LOG(IndexedDB, "IDBConnectionToServer::didGetCount");
216     completeOperation(resultData);
217 }
218
219 void IDBConnectionToServer::deleteRecord(TransactionOperation& operation, const IDBKeyRangeData& keyRangeData)
220 {
221     LOG(IndexedDB, "IDBConnectionToServer::deleteRecord");
222
223     ASSERT(!keyRangeData.isNull);
224
225     saveOperation(operation);
226     m_delegate->deleteRecord(IDBRequestData(operation), keyRangeData);
227 }
228
229 void IDBConnectionToServer::didDeleteRecord(const IDBResultData& resultData)
230 {
231     LOG(IndexedDB, "IDBConnectionToServer::didDeleteRecord");
232     completeOperation(resultData);
233 }
234
235 void IDBConnectionToServer::openCursor(TransactionOperation& operation, const IDBCursorInfo& info)
236 {
237     LOG(IndexedDB, "IDBConnectionToServer::openCursor");
238
239     saveOperation(operation);
240     m_delegate->openCursor(IDBRequestData(operation), info);
241 }
242
243 void IDBConnectionToServer::didOpenCursor(const IDBResultData& resultData)
244 {
245     LOG(IndexedDB, "IDBConnectionToServer::didOpenCursor");
246     completeOperation(resultData);
247 }
248
249 void IDBConnectionToServer::iterateCursor(TransactionOperation& operation, const IDBKeyData& key, unsigned long count)
250 {
251     LOG(IndexedDB, "IDBConnectionToServer::iterateCursor");
252
253     saveOperation(operation);
254     m_delegate->iterateCursor(IDBRequestData(operation), key, count);
255 }
256
257 void IDBConnectionToServer::didIterateCursor(const IDBResultData& resultData)
258 {
259     LOG(IndexedDB, "IDBConnectionToServer::didIterateCursor");
260     completeOperation(resultData);
261 }
262
263 void IDBConnectionToServer::establishTransaction(IDBTransaction& transaction)
264 {
265     LOG(IndexedDB, "IDBConnectionToServer::establishTransaction");
266
267     ASSERT(!hasRecordOfTransaction(transaction));
268     m_pendingTransactions.set(transaction.info().identifier(), &transaction);
269
270     m_delegate->establishTransaction(transaction.database().databaseConnectionIdentifier(), transaction.info());
271 }
272
273 void IDBConnectionToServer::commitTransaction(IDBTransaction& transaction)
274 {
275     LOG(IndexedDB, "IDBConnectionToServer::commitTransaction");
276     ASSERT(!m_committingTransactions.contains(transaction.info().identifier()));
277     m_committingTransactions.set(transaction.info().identifier(), &transaction);
278
279     auto identifier = transaction.info().identifier();
280     m_delegate->commitTransaction(identifier);
281 }
282
283 void IDBConnectionToServer::didCommitTransaction(const IDBResourceIdentifier& transactionIdentifier, const IDBError& error)
284 {
285     LOG(IndexedDB, "IDBConnectionToServer::didCommitTransaction");
286
287     auto transaction = m_committingTransactions.take(transactionIdentifier);
288     ASSERT(transaction);
289
290     transaction->didCommit(error);
291 }
292
293 void IDBConnectionToServer::abortTransaction(IDBTransaction& transaction)
294 {
295     LOG(IndexedDB, "IDBConnectionToServer::abortTransaction");
296     ASSERT(!m_abortingTransactions.contains(transaction.info().identifier()));
297     m_abortingTransactions.set(transaction.info().identifier(), &transaction);
298
299     auto identifier = transaction.info().identifier();
300     m_delegate->abortTransaction(identifier);
301 }
302
303 void IDBConnectionToServer::didAbortTransaction(const IDBResourceIdentifier& transactionIdentifier, const IDBError& error)
304 {
305     LOG(IndexedDB, "IDBConnectionToServer::didAbortTransaction");
306
307     auto transaction = m_abortingTransactions.take(transactionIdentifier);
308     ASSERT(transaction);
309
310     transaction->didAbort(error);
311 }
312
313 void IDBConnectionToServer::fireVersionChangeEvent(uint64_t databaseConnectionIdentifier, uint64_t requestedVersion)
314 {
315     LOG(IndexedDB, "IDBConnectionToServer::fireVersionChangeEvent");
316
317     auto connection = m_databaseConnectionMap.get(databaseConnectionIdentifier);
318     if (!connection)
319         return;
320
321     connection->fireVersionChangeEvent(requestedVersion);
322 }
323
324 void IDBConnectionToServer::didStartTransaction(const IDBResourceIdentifier& transactionIdentifier, const IDBError& error)
325 {
326     LOG(IndexedDB, "IDBConnectionToServer::didStartTransaction");
327
328     auto transaction = m_pendingTransactions.take(transactionIdentifier);
329     ASSERT(transaction);
330
331     transaction->didStart(error);
332 }
333
334 void IDBConnectionToServer::databaseConnectionClosed(IDBDatabase& database)
335 {
336     LOG(IndexedDB, "IDBConnectionToServer::databaseConnectionClosed");
337
338     m_delegate->databaseConnectionClosed(database.databaseConnectionIdentifier());
339 }
340
341 void IDBConnectionToServer::registerDatabaseConnection(IDBDatabase& database)
342 {
343     ASSERT(!m_databaseConnectionMap.contains(database.databaseConnectionIdentifier()));
344     m_databaseConnectionMap.set(database.databaseConnectionIdentifier(), &database);
345 }
346
347 void IDBConnectionToServer::unregisterDatabaseConnection(IDBDatabase& database)
348 {
349     ASSERT(m_databaseConnectionMap.contains(database.databaseConnectionIdentifier()));
350     ASSERT(m_databaseConnectionMap.get(database.databaseConnectionIdentifier()) == &database);
351     m_databaseConnectionMap.remove(database.databaseConnectionIdentifier());
352 }
353
354 void IDBConnectionToServer::saveOperation(TransactionOperation& operation)
355 {
356     ASSERT(!m_activeOperations.contains(operation.identifier()));
357     m_activeOperations.set(operation.identifier(), &operation);
358 }
359
360 void IDBConnectionToServer::completeOperation(const IDBResultData& resultData)
361 {
362     auto operation = m_activeOperations.take(resultData.requestIdentifier());
363     ASSERT(operation);
364
365     operation->completed(resultData);
366 }
367
368 bool IDBConnectionToServer::hasRecordOfTransaction(const IDBTransaction& transaction) const
369 {
370     auto identifier = transaction.info().identifier();
371     return m_pendingTransactions.contains(identifier) || m_committingTransactions.contains(identifier) || m_abortingTransactions.contains(identifier);
372 }
373
374 } // namespace IDBClient
375 } // namespace WebCore
376
377 #endif // ENABLE(INDEXED_DATABASE)