1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
|
/*
* Copyright (C) 2015 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "IDBDatabaseImpl.h"
#if ENABLE(INDEXED_DATABASE)
#include "EventQueue.h"
#include "IDBConnectionToServer.h"
#include "IDBDatabaseException.h"
#include "IDBOpenDBRequestImpl.h"
#include "IDBResultData.h"
#include "IDBTransactionImpl.h"
#include "IDBVersionChangeEventImpl.h"
#include "Logging.h"
#include "ScriptExecutionContext.h"
namespace WebCore {
namespace IDBClient {
Ref<IDBDatabase> IDBDatabase::create(ScriptExecutionContext& context, IDBConnectionToServer& connection, const IDBResultData& resultData)
{
return adoptRef(*new IDBDatabase(context, connection, resultData));
}
IDBDatabase::IDBDatabase(ScriptExecutionContext& context, IDBConnectionToServer& connection, const IDBResultData& resultData)
: WebCore::IDBDatabase(&context)
, m_serverConnection(connection)
, m_info(resultData.databaseInfo())
, m_databaseConnectionIdentifier(resultData.databaseConnectionIdentifier())
{
LOG(IndexedDB, "IDBDatabase::IDBDatabase - Creating database %s with version %" PRIu64 " connection %" PRIu64, m_info.name().utf8().data(), m_info.version(), m_databaseConnectionIdentifier);
suspendIfNeeded();
relaxAdoptionRequirement();
m_serverConnection->registerDatabaseConnection(*this);
}
IDBDatabase::~IDBDatabase()
{
m_serverConnection->unregisterDatabaseConnection(*this);
}
bool IDBDatabase::hasPendingActivity() const
{
return !m_closedInServer;
}
const String IDBDatabase::name() const
{
return m_info.name();
}
uint64_t IDBDatabase::version() const
{
return m_info.version();
}
RefPtr<DOMStringList> IDBDatabase::objectStoreNames() const
{
RefPtr<DOMStringList> objectStoreNames = DOMStringList::create();
for (auto& name : m_info.objectStoreNames())
objectStoreNames->append(name);
objectStoreNames->sort();
return objectStoreNames;
}
RefPtr<WebCore::IDBObjectStore> IDBDatabase::createObjectStore(const String&, const Dictionary&, ExceptionCodeWithMessage&)
{
ASSERT_NOT_REACHED();
return nullptr;
}
RefPtr<WebCore::IDBObjectStore> IDBDatabase::createObjectStore(const String& name, const IDBKeyPath& keyPath, bool autoIncrement, ExceptionCodeWithMessage& ec)
{
LOG(IndexedDB, "IDBDatabase::createObjectStore");
ASSERT(!m_versionChangeTransaction || m_versionChangeTransaction->isVersionChange());
if (!m_versionChangeTransaction) {
ec.code = IDBDatabaseException::InvalidStateError;
ec.message = ASCIILiteral("Failed to execute 'createObjectStore' on 'IDBDatabase': The database is not running a version change transaction.");
return nullptr;
}
if (!m_versionChangeTransaction->isActive()) {
ec.code = IDBDatabaseException::TransactionInactiveError;
return nullptr;
}
if (m_info.hasObjectStore(name)) {
ec.code = IDBDatabaseException::ConstraintError;
ec.message = ASCIILiteral("Failed to execute 'createObjectStore' on 'IDBDatabase': An object store with the specified name already exists.");
return nullptr;
}
if (!keyPath.isNull() && !keyPath.isValid()) {
ec.code = IDBDatabaseException::SyntaxError;
ec.message = ASCIILiteral("Failed to execute 'createObjectStore' on 'IDBDatabase': The keyPath option is not a valid key path.");
return nullptr;
}
if (autoIncrement && !keyPath.isNull()) {
if ((keyPath.type() == IndexedDB::KeyPathType::String && keyPath.string().isEmpty()) || keyPath.type() == IndexedDB::KeyPathType::Array) {
ec.code = IDBDatabaseException::InvalidAccessError;
ec.message = ASCIILiteral("Failed to execute 'createObjectStore' on 'IDBDatabase': The autoIncrement option was set but the keyPath option was empty or an array.");
return nullptr;
}
}
// Install the new ObjectStore into the connection's metadata.
IDBObjectStoreInfo info = m_info.createNewObjectStore(name, keyPath, autoIncrement);
// Create the actual IDBObjectStore from the transaction, which also schedules the operation server side.
Ref<IDBObjectStore> objectStore = m_versionChangeTransaction->createObjectStore(info);
return adoptRef(&objectStore.leakRef());
}
RefPtr<WebCore::IDBTransaction> IDBDatabase::transaction(ScriptExecutionContext*, const Vector<String>& objectStores, const String& modeString, ExceptionCodeWithMessage& ec)
{
LOG(IndexedDB, "IDBDatabase::transaction");
if (m_closePending) {
ec.code = IDBDatabaseException::InvalidStateError;
ec.message = ASCIILiteral("Failed to execute 'transaction' on 'IDBDatabase': The database connection is closing.");
return nullptr;
}
if (objectStores.isEmpty()) {
ec.code = IDBDatabaseException::InvalidAccessError;
ec.message = ASCIILiteral("Failed to execute 'transaction' on 'IDBDatabase': The storeNames parameter was empty.");
return nullptr;
}
IndexedDB::TransactionMode mode = IDBTransaction::stringToMode(modeString, ec.code);
if (ec.code) {
ec.message = makeString(ASCIILiteral("Failed to execute 'transaction' on 'IDBDatabase': The mode provided ('"), modeString, ASCIILiteral("') is not one of 'readonly' or 'readwrite'."));
return nullptr;
}
if (mode != IndexedDB::TransactionMode::ReadOnly && mode != IndexedDB::TransactionMode::ReadWrite) {
ec.code = TypeError;
return nullptr;
}
if (m_versionChangeTransaction && !m_versionChangeTransaction->isFinishedOrFinishing()) {
ec.code = IDBDatabaseException::InvalidStateError;
ec.message = ASCIILiteral("Failed to execute 'transaction' on 'IDBDatabase': A version change transaction is running.");
return nullptr;
}
for (auto& objectStoreName : objectStores) {
if (m_info.hasObjectStore(objectStoreName))
continue;
ec.code = IDBDatabaseException::NotFoundError;
ec.message = ASCIILiteral("Failed to execute 'transaction' on 'IDBDatabase': One of the specified object stores was not found.");
return nullptr;
}
auto info = IDBTransactionInfo::clientTransaction(m_serverConnection.get(), objectStores, mode);
auto transaction = IDBTransaction::create(*this, info);
LOG(IndexedDB, "IDBDatabase::transaction - Added active transaction %s", info.identifier().loggingString().utf8().data());
m_activeTransactions.set(info.identifier(), &transaction.get());
return adoptRef(&transaction.leakRef());
}
RefPtr<WebCore::IDBTransaction> IDBDatabase::transaction(ScriptExecutionContext* context, const String& objectStore, const String& mode, ExceptionCodeWithMessage& ec)
{
Vector<String> objectStores(1);
objectStores[0] = objectStore;
return transaction(context, objectStores, mode, ec);
}
void IDBDatabase::deleteObjectStore(const String& objectStoreName, ExceptionCodeWithMessage& ec)
{
LOG(IndexedDB, "IDBDatabase::deleteObjectStore");
if (!m_versionChangeTransaction) {
ec.code = IDBDatabaseException::InvalidStateError;
ec.message = ASCIILiteral("Failed to execute 'deleteObjectStore' on 'IDBDatabase': The database is not running a version change transaction.");
return;
}
if (!m_versionChangeTransaction->isActive()) {
ec.code = IDBDatabaseException::TransactionInactiveError;
return;
}
if (!m_info.hasObjectStore(objectStoreName)) {
ec.code = IDBDatabaseException::NotFoundError;
ec.message = ASCIILiteral("Failed to execute 'deleteObjectStore' on 'IDBDatabase': The specified object store was not found.");
return;
}
m_info.deleteObjectStore(objectStoreName);
m_versionChangeTransaction->deleteObjectStore(objectStoreName);
}
void IDBDatabase::close()
{
LOG(IndexedDB, "IDBDatabase::close - %" PRIu64, m_databaseConnectionIdentifier);
m_closePending = true;
maybeCloseInServer();
}
void IDBDatabase::maybeCloseInServer()
{
LOG(IndexedDB, "IDBDatabase::maybeCloseInServer - %" PRIu64, m_databaseConnectionIdentifier);
if (m_closedInServer)
return;
// 3.3.9 Database closing steps
// Wait for all transactions created using this connection to complete.
// Once they are complete, this connection is closed.
if (!m_activeTransactions.isEmpty())
return;
m_closedInServer = true;
m_serverConnection->databaseConnectionClosed(*this);
}
const char* IDBDatabase::activeDOMObjectName() const
{
return "IDBDatabase";
}
bool IDBDatabase::canSuspendForDocumentSuspension() const
{
// FIXME: This value will sometimes be false when database operations are actually in progress.
// Such database operations do not yet exist.
return true;
}
void IDBDatabase::stop()
{
LOG(IndexedDB, "IDBDatabase::stop - %" PRIu64, m_databaseConnectionIdentifier);
Vector<IDBResourceIdentifier> transactionIdentifiers;
transactionIdentifiers.reserveInitialCapacity(m_activeTransactions.size());
for (auto& id : m_activeTransactions.keys())
transactionIdentifiers.uncheckedAppend(id);
for (auto& id : transactionIdentifiers) {
IDBTransaction* transaction = m_activeTransactions.get(id);
if (transaction)
transaction->stop();
}
close();
}
Ref<IDBTransaction> IDBDatabase::startVersionChangeTransaction(const IDBTransactionInfo& info, IDBOpenDBRequest& request)
{
LOG(IndexedDB, "IDBDatabase::startVersionChangeTransaction %s", info.identifier().loggingString().utf8().data());
ASSERT(!m_versionChangeTransaction);
ASSERT(info.mode() == IndexedDB::TransactionMode::VersionChange);
ASSERT(!m_closePending);
ASSERT(scriptExecutionContext());
Ref<IDBTransaction> transaction = IDBTransaction::create(*this, info, request);
m_versionChangeTransaction = &transaction.get();
m_activeTransactions.set(transaction->info().identifier(), &transaction.get());
return transaction;
}
void IDBDatabase::didStartTransaction(IDBTransaction& transaction)
{
LOG(IndexedDB, "IDBDatabase::didStartTransaction %s", transaction.info().identifier().loggingString().utf8().data());
ASSERT(!m_versionChangeTransaction);
// It is possible for the client to have aborted a transaction before the server replies back that it has started.
if (m_abortingTransactions.contains(transaction.info().identifier()))
return;
m_activeTransactions.set(transaction.info().identifier(), &transaction);
}
void IDBDatabase::willCommitTransaction(IDBTransaction& transaction)
{
LOG(IndexedDB, "IDBDatabase::willCommitTransaction %s", transaction.info().identifier().loggingString().utf8().data());
auto refTransaction = m_activeTransactions.take(transaction.info().identifier());
ASSERT(refTransaction);
m_committingTransactions.set(transaction.info().identifier(), WTFMove(refTransaction));
}
void IDBDatabase::didCommitTransaction(IDBTransaction& transaction)
{
LOG(IndexedDB, "IDBDatabase::didCommitTransaction %s", transaction.info().identifier().loggingString().utf8().data());
if (m_versionChangeTransaction == &transaction)
m_info.setVersion(transaction.info().newVersion());
didCommitOrAbortTransaction(transaction);
}
void IDBDatabase::willAbortTransaction(IDBTransaction& transaction)
{
LOG(IndexedDB, "IDBDatabase::willAbortTransaction %s", transaction.info().identifier().loggingString().utf8().data());
auto refTransaction = m_activeTransactions.take(transaction.info().identifier());
if (!refTransaction)
refTransaction = m_committingTransactions.take(transaction.info().identifier());
ASSERT(refTransaction);
m_abortingTransactions.set(transaction.info().identifier(), WTFMove(refTransaction));
if (transaction.isVersionChange()) {
ASSERT(transaction.originalDatabaseInfo());
m_info = *transaction.originalDatabaseInfo();
m_closePending = true;
}
}
void IDBDatabase::didAbortTransaction(IDBTransaction& transaction)
{
LOG(IndexedDB, "IDBDatabase::didAbortTransaction %s", transaction.info().identifier().loggingString().utf8().data());
if (transaction.isVersionChange()) {
ASSERT(transaction.originalDatabaseInfo());
ASSERT(m_info.version() == transaction.originalDatabaseInfo()->version());
m_closePending = true;
maybeCloseInServer();
}
didCommitOrAbortTransaction(transaction);
}
void IDBDatabase::didCommitOrAbortTransaction(IDBTransaction& transaction)
{
LOG(IndexedDB, "IDBDatabase::didCommitOrAbortTransaction %s", transaction.info().identifier().loggingString().utf8().data());
if (m_versionChangeTransaction == &transaction)
m_versionChangeTransaction = nullptr;
#ifndef NDBEBUG
unsigned count = 0;
if (m_activeTransactions.contains(transaction.info().identifier()))
++count;
if (m_committingTransactions.contains(transaction.info().identifier()))
++count;
if (m_abortingTransactions.contains(transaction.info().identifier()))
++count;
ASSERT(count == 1);
#endif
m_activeTransactions.remove(transaction.info().identifier());
m_committingTransactions.remove(transaction.info().identifier());
m_abortingTransactions.remove(transaction.info().identifier());
if (m_closePending)
maybeCloseInServer();
}
void IDBDatabase::fireVersionChangeEvent(const IDBResourceIdentifier& requestIdentifier, uint64_t requestedVersion)
{
uint64_t currentVersion = m_info.version();
LOG(IndexedDB, "IDBDatabase::fireVersionChangeEvent - current version %" PRIu64 ", requested version %" PRIu64 ", connection %" PRIu64, currentVersion, requestedVersion, m_databaseConnectionIdentifier);
if (!scriptExecutionContext() || m_closePending) {
serverConnection().didFireVersionChangeEvent(m_databaseConnectionIdentifier, requestIdentifier);
return;
}
Ref<Event> event = IDBVersionChangeEvent::create(requestIdentifier, currentVersion, requestedVersion, eventNames().versionchangeEvent);
event->setTarget(this);
scriptExecutionContext()->eventQueue().enqueueEvent(WTFMove(event));
}
bool IDBDatabase::dispatchEvent(Event& event)
{
LOG(IndexedDB, "IDBDatabase::dispatchEvent (%" PRIu64 ")", m_databaseConnectionIdentifier);
bool result = WebCore::IDBDatabase::dispatchEvent(event);
if (event.isVersionChangeEvent() && event.type() == eventNames().versionchangeEvent)
serverConnection().didFireVersionChangeEvent(m_databaseConnectionIdentifier, downcast<IDBVersionChangeEvent>(event).requestIdentifier());
return result;
}
void IDBDatabase::didCreateIndexInfo(const IDBIndexInfo& info)
{
auto* objectStore = m_info.infoForExistingObjectStore(info.objectStoreIdentifier());
ASSERT(objectStore);
objectStore->addExistingIndex(info);
}
void IDBDatabase::didDeleteIndexInfo(const IDBIndexInfo& info)
{
auto* objectStore = m_info.infoForExistingObjectStore(info.objectStoreIdentifier());
ASSERT(objectStore);
objectStore->deleteIndex(info.name());
}
} // namespace IDBClient
} // namespace WebCore
#endif // ENABLE(INDEXED_DATABASE)
|