e9208b254def9a02508c71dc0f99d7f395ebc5bd
[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::putOrAdd(TransactionOperation& operation, RefPtr<IDBKey>& key, RefPtr<SerializedScriptValue>& value, const IndexedDB::ObjectStoreOverwriteMode overwriteMode)
159 {
160     LOG(IndexedDB, "IDBConnectionToServer::putOrAdd");
161
162     saveOperation(operation);
163     m_delegate->putOrAdd(IDBRequestData(operation), key.get(), *value, overwriteMode);
164 }
165
166 void IDBConnectionToServer::didPutOrAdd(const IDBResultData& resultData)
167 {
168     LOG(IndexedDB, "IDBConnectionToServer::didPutOrAdd");
169     completeOperation(resultData);
170 }
171
172 void IDBConnectionToServer::getRecord(TransactionOperation& operation, const IDBKeyRangeData& keyRangeData)
173 {
174     LOG(IndexedDB, "IDBConnectionToServer::getRecord");
175
176     ASSERT(!keyRangeData.isNull);
177
178     saveOperation(operation);
179     m_delegate->getRecord(IDBRequestData(operation), keyRangeData);
180 }
181
182 void IDBConnectionToServer::didGetRecord(const IDBResultData& resultData)
183 {
184     LOG(IndexedDB, "IDBConnectionToServer::didGetRecord");
185     completeOperation(resultData);
186 }
187
188 void IDBConnectionToServer::getCount(TransactionOperation& operation, const IDBKeyRangeData& keyRangeData)
189 {
190     LOG(IndexedDB, "IDBConnectionToServer::getCount");
191
192     ASSERT(!keyRangeData.isNull);
193
194     saveOperation(operation);
195     m_delegate->getCount(IDBRequestData(operation), keyRangeData);
196 }
197
198 void IDBConnectionToServer::didGetCount(const IDBResultData& resultData)
199 {
200     LOG(IndexedDB, "IDBConnectionToServer::didGetCount");
201     completeOperation(resultData);
202 }
203
204 void IDBConnectionToServer::deleteRecord(TransactionOperation& operation, const IDBKeyRangeData& keyRangeData)
205 {
206     LOG(IndexedDB, "IDBConnectionToServer::deleteRecord");
207
208     ASSERT(!keyRangeData.isNull);
209
210     saveOperation(operation);
211     m_delegate->deleteRecord(IDBRequestData(operation), keyRangeData);
212 }
213
214 void IDBConnectionToServer::didDeleteRecord(const IDBResultData& resultData)
215 {
216     LOG(IndexedDB, "IDBConnectionToServer::didDeleteRecord");
217     completeOperation(resultData);
218 }
219
220 void IDBConnectionToServer::establishTransaction(IDBTransaction& transaction)
221 {
222     LOG(IndexedDB, "IDBConnectionToServer::establishTransaction");
223
224     ASSERT(!hasRecordOfTransaction(transaction));
225     m_pendingTransactions.set(transaction.info().identifier(), &transaction);
226
227     m_delegate->establishTransaction(transaction.database().databaseConnectionIdentifier(), transaction.info());
228 }
229
230 void IDBConnectionToServer::commitTransaction(IDBTransaction& transaction)
231 {
232     LOG(IndexedDB, "IDBConnectionToServer::commitTransaction");
233     ASSERT(!m_committingTransactions.contains(transaction.info().identifier()));
234     m_committingTransactions.set(transaction.info().identifier(), &transaction);
235
236     auto identifier = transaction.info().identifier();
237     m_delegate->commitTransaction(identifier);
238 }
239
240 void IDBConnectionToServer::didCommitTransaction(const IDBResourceIdentifier& transactionIdentifier, const IDBError& error)
241 {
242     LOG(IndexedDB, "IDBConnectionToServer::didCommitTransaction");
243
244     auto transaction = m_committingTransactions.take(transactionIdentifier);
245     ASSERT(transaction);
246
247     transaction->didCommit(error);
248 }
249
250 void IDBConnectionToServer::abortTransaction(IDBTransaction& transaction)
251 {
252     LOG(IndexedDB, "IDBConnectionToServer::abortTransaction");
253     ASSERT(!m_abortingTransactions.contains(transaction.info().identifier()));
254     m_abortingTransactions.set(transaction.info().identifier(), &transaction);
255
256     auto identifier = transaction.info().identifier();
257     m_delegate->abortTransaction(identifier);
258 }
259
260 void IDBConnectionToServer::didAbortTransaction(const IDBResourceIdentifier& transactionIdentifier, const IDBError& error)
261 {
262     LOG(IndexedDB, "IDBConnectionToServer::didAbortTransaction");
263
264     auto transaction = m_abortingTransactions.take(transactionIdentifier);
265     ASSERT(transaction);
266
267     transaction->didAbort(error);
268 }
269
270 void IDBConnectionToServer::fireVersionChangeEvent(uint64_t databaseConnectionIdentifier, uint64_t requestedVersion)
271 {
272     LOG(IndexedDB, "IDBConnectionToServer::fireVersionChangeEvent");
273
274     auto connection = m_databaseConnectionMap.get(databaseConnectionIdentifier);
275     if (!connection)
276         return;
277
278     connection->fireVersionChangeEvent(requestedVersion);
279 }
280
281 void IDBConnectionToServer::didStartTransaction(const IDBResourceIdentifier& transactionIdentifier, const IDBError& error)
282 {
283     LOG(IndexedDB, "IDBConnectionToServer::didStartTransaction");
284
285     auto transaction = m_pendingTransactions.take(transactionIdentifier);
286     ASSERT(transaction);
287
288     transaction->didStart(error);
289 }
290
291 void IDBConnectionToServer::databaseConnectionClosed(IDBDatabase& database)
292 {
293     LOG(IndexedDB, "IDBConnectionToServer::databaseConnectionClosed");
294
295     m_delegate->databaseConnectionClosed(database.databaseConnectionIdentifier());
296 }
297
298 void IDBConnectionToServer::registerDatabaseConnection(IDBDatabase& database)
299 {
300     ASSERT(!m_databaseConnectionMap.contains(database.databaseConnectionIdentifier()));
301     m_databaseConnectionMap.set(database.databaseConnectionIdentifier(), &database);
302 }
303
304 void IDBConnectionToServer::unregisterDatabaseConnection(IDBDatabase& database)
305 {
306     ASSERT(m_databaseConnectionMap.contains(database.databaseConnectionIdentifier()));
307     ASSERT(m_databaseConnectionMap.get(database.databaseConnectionIdentifier()) == &database);
308     m_databaseConnectionMap.remove(database.databaseConnectionIdentifier());
309 }
310
311 void IDBConnectionToServer::saveOperation(TransactionOperation& operation)
312 {
313     ASSERT(!m_activeOperations.contains(operation.identifier()));
314     m_activeOperations.set(operation.identifier(), &operation);
315 }
316
317 void IDBConnectionToServer::completeOperation(const IDBResultData& resultData)
318 {
319     auto operation = m_activeOperations.take(resultData.requestIdentifier());
320     ASSERT(operation);
321
322     operation->completed(resultData);
323 }
324
325 bool IDBConnectionToServer::hasRecordOfTransaction(const IDBTransaction& transaction) const
326 {
327     auto identifier = transaction.info().identifier();
328     return m_pendingTransactions.contains(identifier) || m_committingTransactions.contains(identifier) || m_abortingTransactions.contains(identifier);
329 }
330
331 } // namespace IDBClient
332 } // namespace WebCore
333
334 #endif // ENABLE(INDEXED_DATABASE)