e4085df05d8ec965c36edbae0a7f2fee1773913b
[WebKit-https.git] / Source / WebCore / Modules / indexeddb / client / IDBTransactionImpl.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 "IDBTransactionImpl.h"
28
29 #if ENABLE(INDEXED_DATABASE)
30
31 #include "DOMError.h"
32 #include "EventQueue.h"
33 #include "IDBCursorWithValueImpl.h"
34 #include "IDBDatabaseImpl.h"
35 #include "IDBError.h"
36 #include "IDBEventDispatcher.h"
37 #include "IDBKeyData.h"
38 #include "IDBKeyRangeData.h"
39 #include "IDBObjectStore.h"
40 #include "IDBOpenDBRequestImpl.h"
41 #include "IDBRequestImpl.h"
42 #include "IDBResultData.h"
43 #include "JSDOMWindowBase.h"
44 #include "Logging.h"
45 #include "ScriptExecutionContext.h"
46 #include "TransactionOperation.h"
47
48 namespace WebCore {
49 namespace IDBClient {
50
51 Ref<IDBTransaction> IDBTransaction::create(IDBDatabase& database, const IDBTransactionInfo& info)
52 {
53     return adoptRef(*new IDBTransaction(database, info, nullptr));
54 }
55
56 Ref<IDBTransaction> IDBTransaction::create(IDBDatabase& database, const IDBTransactionInfo& info, IDBOpenDBRequest& request)
57 {
58     return adoptRef(*new IDBTransaction(database, info, &request));
59 }
60
61 IDBTransaction::IDBTransaction(IDBDatabase& database, const IDBTransactionInfo& info, IDBOpenDBRequest* request)
62     : WebCore::IDBTransaction(database.scriptExecutionContext())
63     , m_database(database)
64     , m_info(info)
65     , m_operationTimer(*this, &IDBTransaction::operationTimerFired)
66     , m_openDBRequest(request)
67
68 {
69     relaxAdoptionRequirement();
70
71     if (m_info.mode() == IndexedDB::TransactionMode::VersionChange) {
72         ASSERT(m_openDBRequest);
73         m_originalDatabaseInfo = std::make_unique<IDBDatabaseInfo>(m_database->info());
74         m_startedOnServer = true;
75     } else {
76         activate();
77
78         RefPtr<IDBTransaction> self;
79         JSC::VM& vm = JSDOMWindowBase::commonVM();
80         vm.whenIdle([self, this]() {
81             deactivate();
82         });
83
84         establishOnServer();
85     }
86
87     suspendIfNeeded();
88 }
89
90 IDBTransaction::~IDBTransaction()
91 {
92 }
93
94 const String& IDBTransaction::mode() const
95 {
96     switch (m_info.mode()) {
97     case IndexedDB::TransactionMode::ReadOnly:
98         return IDBTransaction::modeReadOnly();
99     case IndexedDB::TransactionMode::ReadWrite:
100         return IDBTransaction::modeReadWrite();
101     case IndexedDB::TransactionMode::VersionChange:
102         return IDBTransaction::modeVersionChange();
103     }
104
105     RELEASE_ASSERT_NOT_REACHED();
106 }
107
108 WebCore::IDBDatabase* IDBTransaction::db()
109 {
110     return &m_database.get();
111 }
112
113 IDBConnectionToServer& IDBTransaction::serverConnection()
114 {
115     return m_database->serverConnection();
116 }
117
118 RefPtr<DOMError> IDBTransaction::error() const
119 {
120     ASSERT_NOT_REACHED();
121     return nullptr;
122 }
123
124 RefPtr<WebCore::IDBObjectStore> IDBTransaction::objectStore(const String& objectStoreName, ExceptionCode& ec)
125 {
126     LOG(IndexedDB, "IDBTransaction::objectStore");
127
128     if (objectStoreName.isEmpty()) {
129         ec = NOT_FOUND_ERR;
130         return nullptr;
131     }
132
133     if (isFinishedOrFinishing()) {
134         ec = INVALID_STATE_ERR;
135         return nullptr;
136     }
137
138     auto iterator = m_referencedObjectStores.find(objectStoreName);
139     if (iterator != m_referencedObjectStores.end())
140         return iterator->value;
141
142     bool found = false;
143     for (auto& objectStore : m_info.objectStores()) {
144         if (objectStore == objectStoreName) {
145             found = true;
146             break;
147         }
148     }
149
150     auto* info = m_database->info().infoForExistingObjectStore(objectStoreName);
151     if (!info) {
152         ec = NOT_FOUND_ERR;
153         return nullptr;
154     }
155
156     // Version change transactions are scoped to every object store in the database.
157     if (!found && !isVersionChange()) {
158         ec = NOT_FOUND_ERR;
159         return nullptr;
160     }
161
162     auto objectStore = IDBObjectStore::create(*info, *this);
163     m_referencedObjectStores.set(objectStoreName, &objectStore.get());
164
165     return adoptRef(&objectStore.leakRef());
166 }
167
168 void IDBTransaction::abort(ExceptionCode& ec)
169 {
170     LOG(IndexedDB, "IDBTransaction::abort");
171
172     if (isFinishedOrFinishing()) {
173         ec = INVALID_STATE_ERR;
174         return;
175     }
176
177     m_state = IndexedDB::TransactionState::Aborting;
178     m_database->willAbortTransaction(*this);
179
180     m_abortQueue.swap(m_transactionOperationQueue);
181
182     auto operation = createTransactionOperation(*this, nullptr, &IDBTransaction::abortOnServerAndCancelRequests);
183     scheduleOperation(WTF::move(operation));
184 }
185
186 void IDBTransaction::abortOnServerAndCancelRequests(TransactionOperation&)
187 {
188     LOG(IndexedDB, "IDBTransaction::abortOnServerAndCancelRequests");
189
190     ASSERT(m_transactionOperationQueue.isEmpty());
191
192     serverConnection().abortTransaction(*this);
193
194     IDBError error(IDBExceptionCode::AbortError);
195     for (auto& operation : m_abortQueue)
196         operation->completed(IDBResultData::error(operation->identifier(), error));
197
198     // Since we're aborting, this abortOnServerAndCancelRequests() operation should be the only
199     // in-progress operation, and it should be impossible to have queued any further operations.
200     ASSERT(m_transactionOperationMap.size() == 1);
201     ASSERT(m_transactionOperationQueue.isEmpty());
202 }
203
204 const char* IDBTransaction::activeDOMObjectName() const
205 {
206     return "IDBTransaction";
207 }
208
209 bool IDBTransaction::canSuspendForPageCache() const
210 {
211     return false;
212 }
213
214 bool IDBTransaction::hasPendingActivity() const
215 {
216     return m_state != IndexedDB::TransactionState::Finished;
217 }
218
219 bool IDBTransaction::isActive() const
220 {
221     return m_state == IndexedDB::TransactionState::Active;
222 }
223
224 bool IDBTransaction::isFinishedOrFinishing() const
225 {
226     return m_state == IndexedDB::TransactionState::Committing
227         || m_state == IndexedDB::TransactionState::Aborting
228         || m_state == IndexedDB::TransactionState::Finished;
229 }
230
231 void IDBTransaction::addRequest(IDBRequest& request)
232 {
233     m_openRequests.add(&request);
234 }
235
236 void IDBTransaction::removeRequest(IDBRequest& request)
237 {
238     ASSERT(m_openRequests.contains(&request));
239     m_openRequests.remove(&request);
240 }
241
242 void IDBTransaction::scheduleOperation(RefPtr<TransactionOperation>&& operation)
243 {
244     ASSERT(!m_transactionOperationMap.contains(operation->identifier()));
245
246     m_transactionOperationQueue.append(operation);
247     m_transactionOperationMap.set(operation->identifier(), WTF::move(operation));
248
249     scheduleOperationTimer();
250 }
251
252 void IDBTransaction::scheduleOperationTimer()
253 {
254     if (!m_operationTimer.isActive())
255         m_operationTimer.startOneShot(0);
256 }
257
258 void IDBTransaction::operationTimerFired()
259 {
260     LOG(IndexedDB, "IDBTransaction::operationTimerFired (%p)", this);
261
262     if (!m_startedOnServer)
263         return;
264
265     if (!m_transactionOperationQueue.isEmpty()) {
266         auto operation = m_transactionOperationQueue.takeFirst();
267         operation->perform();
268
269         return;
270     }
271
272     if (!m_transactionOperationMap.isEmpty() || !m_openRequests.isEmpty())
273         return;
274
275     if (!isFinishedOrFinishing())
276         commit();
277 }
278
279 void IDBTransaction::commit()
280 {
281     LOG(IndexedDB, "IDBTransaction::commit");
282
283     ASSERT(!isFinishedOrFinishing());
284
285     m_state = IndexedDB::TransactionState::Committing;
286     m_database->willCommitTransaction(*this);
287
288     auto operation = createTransactionOperation(*this, nullptr, &IDBTransaction::commitOnServer);
289     scheduleOperation(WTF::move(operation));
290 }
291
292 void IDBTransaction::commitOnServer(TransactionOperation&)
293 {
294     LOG(IndexedDB, "IDBTransaction::commitOnServer");
295     serverConnection().commitTransaction(*this);
296 }
297
298 void IDBTransaction::finishAbortOrCommit()
299 {
300     ASSERT(m_state != IndexedDB::TransactionState::Finished);
301     m_state = IndexedDB::TransactionState::Finished;
302
303     m_originalDatabaseInfo = nullptr;
304 }
305
306 void IDBTransaction::didStart(const IDBError& error)
307 {
308     LOG(IndexedDB, "IDBTransaction::didStart");
309
310     m_database->didStartTransaction(*this);
311
312     m_startedOnServer = true;
313
314     // It's possible the transaction failed to start on the server.
315     // That equates to an abort.
316     if (!error.isNull()) {
317         didAbort(error);
318         return;
319     }
320
321     scheduleOperationTimer();
322 }
323
324 void IDBTransaction::didAbort(const IDBError& error)
325 {
326     LOG(IndexedDB, "IDBTransaction::didAbort");
327
328     if (m_state == IndexedDB::TransactionState::Finished)
329         return;
330
331     m_database->didAbortTransaction(*this);
332
333     m_idbError = error;
334     fireOnAbort();
335
336     finishAbortOrCommit();
337 }
338
339 void IDBTransaction::didCommit(const IDBError& error)
340 {
341     LOG(IndexedDB, "IDBTransaction::didCommit");
342
343     ASSERT(m_state == IndexedDB::TransactionState::Committing);
344
345     if (error.isNull()) {
346         m_database->didCommitTransaction(*this);
347         fireOnComplete();
348     } else {
349         m_database->didAbortTransaction(*this);
350         m_idbError = error;
351         fireOnAbort();
352     }
353
354     finishAbortOrCommit();
355 }
356
357 void IDBTransaction::fireOnComplete()
358 {
359     LOG(IndexedDB, "IDBTransaction::fireOnComplete");
360     enqueueEvent(Event::create(eventNames().completeEvent, false, false));
361 }
362
363 void IDBTransaction::fireOnAbort()
364 {
365     LOG(IndexedDB, "IDBTransaction::fireOnAbort");
366     enqueueEvent(Event::create(eventNames().abortEvent, true, false));
367 }
368
369 void IDBTransaction::enqueueEvent(Ref<Event>&& event)
370 {
371     ASSERT(m_state != IndexedDB::TransactionState::Finished);
372
373     if (!scriptExecutionContext())
374         return;
375
376     event->setTarget(this);
377     scriptExecutionContext()->eventQueue().enqueueEvent(WTF::move(event));
378 }
379
380 bool IDBTransaction::dispatchEvent(Event& event)
381 {
382     LOG(IndexedDB, "IDBTransaction::dispatchEvent");
383
384     ASSERT(scriptExecutionContext());
385     ASSERT(event.target() == this);
386     ASSERT(event.type() == eventNames().completeEvent || event.type() == eventNames().abortEvent);
387
388     Vector<RefPtr<EventTarget>> targets;
389     targets.append(this);
390     targets.append(db());
391
392     bool result = IDBEventDispatcher::dispatch(event, targets);
393
394     if (isVersionChange() && event.type() == eventNames().completeEvent) {
395         ASSERT(m_openDBRequest);
396         m_openDBRequest->fireSuccessAfterVersionChangeCommit();
397     }
398
399     return result;
400 }
401
402 Ref<IDBObjectStore> IDBTransaction::createObjectStore(const IDBObjectStoreInfo& info)
403 {
404     LOG(IndexedDB, "IDBTransaction::createObjectStore");
405     ASSERT(isVersionChange());
406
407     Ref<IDBObjectStore> objectStore = IDBObjectStore::create(info, *this);
408     m_referencedObjectStores.set(info.name(), &objectStore.get());
409
410     auto operation = createTransactionOperation(*this, &IDBTransaction::didCreateObjectStoreOnServer, &IDBTransaction::createObjectStoreOnServer, info);
411     scheduleOperation(WTF::move(operation));
412
413     return WTF::move(objectStore);
414 }
415
416 void IDBTransaction::createObjectStoreOnServer(TransactionOperation& operation, const IDBObjectStoreInfo& info)
417 {
418     LOG(IndexedDB, "IDBTransaction::createObjectStoreOnServer");
419
420     ASSERT(isVersionChange());
421
422     m_database->serverConnection().createObjectStore(operation, info);
423 }
424
425 void IDBTransaction::didCreateObjectStoreOnServer(const IDBResultData& resultData)
426 {
427     LOG(IndexedDB, "IDBTransaction::didCreateObjectStoreOnServer");
428
429     ASSERT_UNUSED(resultData, resultData.type() == IDBResultType::CreateObjectStoreSuccess || resultData.type() == IDBResultType::Error);
430 }
431
432 Ref<IDBIndex> IDBTransaction::createIndex(IDBObjectStore& objectStore, const IDBIndexInfo& info)
433 {
434     LOG(IndexedDB, "IDBTransaction::createIndex");
435     ASSERT(isVersionChange());
436
437     Ref<IDBIndex> index = IDBIndex::create(info, objectStore);
438
439     auto operation = createTransactionOperation(*this, &IDBTransaction::didCreateIndexOnServer, &IDBTransaction::createIndexOnServer, info);
440     scheduleOperation(WTF::move(operation));
441
442     return WTF::move(index);
443 }
444
445 void IDBTransaction::createIndexOnServer(TransactionOperation& operation, const IDBIndexInfo& info)
446 {
447     LOG(IndexedDB, "IDBTransaction::createIndexOnServer");
448
449     ASSERT(isVersionChange());
450
451     m_database->serverConnection().createIndex(operation, info);
452 }
453
454 void IDBTransaction::didCreateIndexOnServer(const IDBResultData& resultData)
455 {
456     LOG(IndexedDB, "IDBTransaction::didCreateIndexOnServer");
457
458     if (resultData.type() == IDBResultType::CreateIndexSuccess)
459         return;
460
461     ASSERT(resultData.type() == IDBResultType::Error);
462
463     // This operation might have failed because the transaction is already aborting.
464     if (m_state == IndexedDB::TransactionState::Aborting)
465         return;
466
467     // Otherwise, failure to create an index forced abortion of the transaction.
468     ExceptionCode ec;
469     abort(ec);
470 }
471
472 Ref<IDBRequest> IDBTransaction::requestOpenCursor(ScriptExecutionContext& context, IDBObjectStore& objectStore, const IDBCursorInfo& info)
473 {
474     LOG(IndexedDB, "IDBTransaction::requestOpenCursor");
475
476     return doRequestOpenCursor(context, IDBCursorWithValue::create(*this, objectStore, info));
477 }
478
479 Ref<IDBRequest> IDBTransaction::requestOpenCursor(ScriptExecutionContext& context, IDBIndex& index, const IDBCursorInfo& info)
480 {
481     LOG(IndexedDB, "IDBTransaction::requestOpenCursor");
482
483     if (info.cursorType() == IndexedDB::CursorType::KeyOnly)
484         return doRequestOpenCursor(context, IDBCursor::create(*this, index, info));
485
486     return doRequestOpenCursor(context, IDBCursorWithValue::create(*this, index, info));
487 }
488
489 Ref<IDBRequest> IDBTransaction::doRequestOpenCursor(ScriptExecutionContext& context, Ref<IDBCursor>&& cursor)
490 {
491     ASSERT(isActive());
492
493     Ref<IDBRequest> request = IDBRequest::create(context, cursor.get(), *this);
494     addRequest(request.get());
495
496     auto operation = createTransactionOperation(*this, request.get(), &IDBTransaction::didOpenCursorOnServer, &IDBTransaction::openCursorOnServer, cursor->info());
497     scheduleOperation(WTF::move(operation));
498
499     return WTF::move(request);
500 }
501
502 void IDBTransaction::openCursorOnServer(TransactionOperation& operation, const IDBCursorInfo& info)
503 {
504     LOG(IndexedDB, "IDBTransaction::openCursorOnServer");
505
506     m_database->serverConnection().openCursor(operation, info);
507 }
508
509 void IDBTransaction::didOpenCursorOnServer(IDBRequest& request, const IDBResultData& resultData)
510 {
511     LOG(IndexedDB, "IDBTransaction::didOpenCursorOnServer");
512
513     request.didOpenOrIterateCursor(resultData);
514 }
515
516 void IDBTransaction::iterateCursor(IDBCursor& cursor, const IDBKeyData& key, unsigned long count)
517 {
518     LOG(IndexedDB, "IDBTransaction::iterateCursor");
519     ASSERT(isActive());
520     ASSERT(cursor.request());
521
522     addRequest(*cursor.request());
523
524     auto operation = createTransactionOperation(*this, *cursor.request(), &IDBTransaction::didIterateCursorOnServer, &IDBTransaction::iterateCursorOnServer, key, count);
525     scheduleOperation(WTF::move(operation));
526 }
527
528 void IDBTransaction::iterateCursorOnServer(TransactionOperation& operation, const IDBKeyData& key, const unsigned long& count)
529 {
530     LOG(IndexedDB, "IDBTransaction::iterateCursorOnServer");
531
532     serverConnection().iterateCursor(operation, key, count);
533 }
534
535 void IDBTransaction::didIterateCursorOnServer(IDBRequest& request, const IDBResultData& resultData)
536 {
537     LOG(IndexedDB, "IDBTransaction::didIterateCursorOnServer");
538
539     request.didOpenOrIterateCursor(resultData);
540 }
541
542 Ref<IDBRequest> IDBTransaction::requestGetRecord(ScriptExecutionContext& context, IDBObjectStore& objectStore, const IDBKeyRangeData& keyRangeData)
543 {
544     LOG(IndexedDB, "IDBTransaction::requestGetRecord");
545     ASSERT(isActive());
546     ASSERT(!keyRangeData.isNull);
547
548     Ref<IDBRequest> request = IDBRequest::create(context, objectStore, *this);
549     addRequest(request.get());
550
551     auto operation = createTransactionOperation(*this, request.get(), &IDBTransaction::didGetRecordOnServer, &IDBTransaction::getRecordOnServer, keyRangeData);
552     scheduleOperation(WTF::move(operation));
553
554     return WTF::move(request);
555 }
556
557 Ref<IDBRequest> IDBTransaction::requestGetValue(ScriptExecutionContext& context, IDBIndex& index, const IDBKeyRangeData& range)
558 {
559     LOG(IndexedDB, "IDBTransaction::requestGetValue");
560     return requestIndexRecord(context, index, IndexedDB::IndexRecordType::Value, range);
561 }
562
563 Ref<IDBRequest> IDBTransaction::requestGetKey(ScriptExecutionContext& context, IDBIndex& index, const IDBKeyRangeData& range)
564 {
565     LOG(IndexedDB, "IDBTransaction::requestGetValue");
566     return requestIndexRecord(context, index, IndexedDB::IndexRecordType::Key, range);
567 }
568
569 Ref<IDBRequest> IDBTransaction::requestIndexRecord(ScriptExecutionContext& context, IDBIndex& index, IndexedDB::IndexRecordType type, const IDBKeyRangeData&range)
570 {
571     LOG(IndexedDB, "IDBTransaction::requestGetValue");
572     ASSERT(isActive());
573     ASSERT(!range.isNull);
574
575     Ref<IDBRequest> request = IDBRequest::createGet(context, index, type, *this);
576     addRequest(request.get());
577
578     auto operation = createTransactionOperation(*this, request.get(), &IDBTransaction::didGetRecordOnServer, &IDBTransaction::getRecordOnServer, range);
579     scheduleOperation(WTF::move(operation));
580
581     return WTF::move(request);
582 }
583
584 void IDBTransaction::getRecordOnServer(TransactionOperation& operation, const IDBKeyRangeData& keyRange)
585 {
586     LOG(IndexedDB, "IDBTransaction::getRecordOnServer");
587
588     serverConnection().getRecord(operation, keyRange);
589 }
590
591 void IDBTransaction::didGetRecordOnServer(IDBRequest& request, const IDBResultData& resultData)
592 {
593     LOG(IndexedDB, "IDBTransaction::didGetRecordOnServer");
594
595     if (resultData.type() == IDBResultType::Error) {
596         request.requestCompleted(resultData);
597         return;
598     }
599
600     ASSERT(resultData.type() == IDBResultType::GetRecordSuccess);
601
602     const IDBGetResult& result = resultData.getResult();
603
604     if (request.sourceIndexIdentifier() && request.requestedIndexRecordType() == IndexedDB::IndexRecordType::Key) {
605         if (!result.keyData().isNull())
606             request.setResult(&result.keyData());
607         else
608             request.setResultToUndefined();
609     } else {
610         if (resultData.getResult().valueBuffer().data())
611             request.setResultToStructuredClone(resultData.getResult().valueBuffer());
612         else
613             request.setResultToUndefined();
614     }
615
616     request.requestCompleted(resultData);
617 }
618
619 Ref<IDBRequest> IDBTransaction::requestCount(ScriptExecutionContext& context, IDBObjectStore& objectStore, const IDBKeyRangeData& range)
620 {
621     LOG(IndexedDB, "IDBTransaction::requestCount (IDBObjectStore)");
622     ASSERT(isActive());
623     ASSERT(!range.isNull);
624
625     Ref<IDBRequest> request = IDBRequest::create(context, objectStore, *this);
626     addRequest(request.get());
627
628     scheduleOperation(createTransactionOperation(*this, request.get(), &IDBTransaction::didGetCountOnServer, &IDBTransaction::getCountOnServer, range));
629
630     return request;
631 }
632
633 Ref<IDBRequest> IDBTransaction::requestCount(ScriptExecutionContext& context, IDBIndex& index, const IDBKeyRangeData& range)
634 {
635     LOG(IndexedDB, "IDBTransaction::requestCount (IDBIndex)");
636     ASSERT(isActive());
637     ASSERT(!range.isNull);
638
639     Ref<IDBRequest> request = IDBRequest::createCount(context, index, *this);
640     addRequest(request.get());
641
642     scheduleOperation(createTransactionOperation(*this, request.get(), &IDBTransaction::didGetCountOnServer, &IDBTransaction::getCountOnServer, range));
643
644     return request;
645 }
646
647 void IDBTransaction::getCountOnServer(TransactionOperation& operation, const IDBKeyRangeData& keyRange)
648 {
649     LOG(IndexedDB, "IDBTransaction::getCountOnServer");
650
651     serverConnection().getCount(operation, keyRange);
652 }
653
654 void IDBTransaction::didGetCountOnServer(IDBRequest& request, const IDBResultData& resultData)
655 {
656     LOG(IndexedDB, "IDBTransaction::didGetCountOnServer");
657
658     request.setResult(resultData.resultInteger());
659     request.requestCompleted(resultData);
660 }
661
662 Ref<IDBRequest> IDBTransaction::requestDeleteRecord(ScriptExecutionContext& context, IDBObjectStore& objectStore, const IDBKeyRangeData& range)
663 {
664     LOG(IndexedDB, "IDBTransaction::requestDeleteRecord");
665     ASSERT(isActive());
666     ASSERT(!range.isNull);
667
668     Ref<IDBRequest> request = IDBRequest::create(context, objectStore, *this);
669     addRequest(request.get());
670
671     scheduleOperation(createTransactionOperation(*this, request.get(), &IDBTransaction::didDeleteRecordOnServer, &IDBTransaction::deleteRecordOnServer, range));
672     return request;
673 }
674
675 void IDBTransaction::deleteRecordOnServer(TransactionOperation& operation, const IDBKeyRangeData& keyRange)
676 {
677     LOG(IndexedDB, "IDBTransaction::deleteRecordOnServer");
678
679     serverConnection().deleteRecord(operation, keyRange);
680 }
681
682 void IDBTransaction::didDeleteRecordOnServer(IDBRequest& request, const IDBResultData& resultData)
683 {
684     LOG(IndexedDB, "IDBTransaction::didDeleteRecordOnServer");
685
686     request.setResultToUndefined();
687     request.requestCompleted(resultData);
688 }
689
690 Ref<IDBRequest> IDBTransaction::requestClearObjectStore(ScriptExecutionContext& context, IDBObjectStore& objectStore)
691 {
692     LOG(IndexedDB, "IDBTransaction::requestClearObjectStore");
693     ASSERT(isActive());
694
695     Ref<IDBRequest> request = IDBRequest::create(context, objectStore, *this);
696     addRequest(request.get());
697
698     uint64_t objectStoreIdentifier = objectStore.info().identifier();
699     auto operation = createTransactionOperation(*this, request.get(), &IDBTransaction::didClearObjectStoreOnServer, &IDBTransaction::clearObjectStoreOnServer, objectStoreIdentifier);
700     scheduleOperation(WTF::move(operation));
701
702     return WTF::move(request);
703 }
704
705 void IDBTransaction::clearObjectStoreOnServer(TransactionOperation& operation, const uint64_t& objectStoreIdentifier)
706 {
707     LOG(IndexedDB, "IDBTransaction::clearObjectStoreOnServer");
708
709     serverConnection().clearObjectStore(operation, objectStoreIdentifier);
710 }
711
712 void IDBTransaction::didClearObjectStoreOnServer(IDBRequest& request, const IDBResultData& resultData)
713 {
714     LOG(IndexedDB, "IDBTransaction::didClearObjectStoreOnServer");
715
716     request.setResultToUndefined();
717     request.requestCompleted(resultData);
718 }
719
720 Ref<IDBRequest> IDBTransaction::requestPutOrAdd(ScriptExecutionContext& context, IDBObjectStore& objectStore, IDBKey* key, SerializedScriptValue& value, IndexedDB::ObjectStoreOverwriteMode overwriteMode)
721 {
722     LOG(IndexedDB, "IDBTransaction::requestPutOrAdd");
723     ASSERT(isActive());
724     ASSERT(!isReadOnly());
725     ASSERT(objectStore.info().autoIncrement() || key);
726
727     Ref<IDBRequest> request = IDBRequest::create(context, objectStore, *this);
728     addRequest(request.get());
729
730     auto operation = createTransactionOperation(*this, request.get(), &IDBTransaction::didPutOrAddOnServer, &IDBTransaction::putOrAddOnServer, key, &value, overwriteMode);
731     scheduleOperation(WTF::move(operation));
732
733     return WTF::move(request);
734 }
735
736 void IDBTransaction::putOrAddOnServer(TransactionOperation& operation, RefPtr<IDBKey> key, RefPtr<SerializedScriptValue> value, const IndexedDB::ObjectStoreOverwriteMode& overwriteMode)
737 {
738     LOG(IndexedDB, "IDBTransaction::putOrAddOnServer");
739
740     ASSERT(!isReadOnly());
741
742     serverConnection().putOrAdd(operation, key, value, overwriteMode);
743 }
744
745 void IDBTransaction::didPutOrAddOnServer(IDBRequest& request, const IDBResultData& resultData)
746 {
747     LOG(IndexedDB, "IDBTransaction::didPutOrAddOnServer");
748
749     request.setResult(resultData.resultKey());
750     request.requestCompleted(resultData);
751 }
752
753 void IDBTransaction::deleteObjectStore(const String& objectStoreName)
754 {
755     LOG(IndexedDB, "IDBTransaction::deleteObjectStore");
756
757     ASSERT(isVersionChange());
758
759     if (auto objectStore = m_referencedObjectStores.take(objectStoreName))
760         objectStore->markAsDeleted();
761
762     auto operation = createTransactionOperation(*this, &IDBTransaction::didDeleteObjectStoreOnServer, &IDBTransaction::deleteObjectStoreOnServer, objectStoreName);
763     scheduleOperation(WTF::move(operation));
764 }
765
766 void IDBTransaction::deleteObjectStoreOnServer(TransactionOperation& operation, const String& objectStoreName)
767 {
768     LOG(IndexedDB, "IDBTransaction::deleteObjectStoreOnServer");
769     ASSERT(isVersionChange());
770
771     serverConnection().deleteObjectStore(operation, objectStoreName);
772 }
773
774 void IDBTransaction::didDeleteObjectStoreOnServer(const IDBResultData& resultData)
775 {
776     LOG(IndexedDB, "IDBTransaction::didDeleteObjectStoreOnServer");
777     ASSERT_UNUSED(resultData, resultData.type() == IDBResultType::DeleteObjectStoreSuccess || resultData.type() == IDBResultType::Error);
778 }
779
780 void IDBTransaction::deleteIndex(uint64_t objectStoreIdentifier, const String& indexName)
781 {
782     LOG(IndexedDB, "IDBTransaction::deleteIndex");
783
784     ASSERT(isVersionChange());
785
786     auto operation = createTransactionOperation(*this, &IDBTransaction::didDeleteIndexOnServer, &IDBTransaction::deleteIndexOnServer, objectStoreIdentifier, indexName);
787     scheduleOperation(WTF::move(operation));
788 }
789
790 void IDBTransaction::deleteIndexOnServer(TransactionOperation& operation, const uint64_t& objectStoreIdentifier, const String& indexName)
791 {
792     LOG(IndexedDB, "IDBTransaction::deleteIndexOnServer");
793     ASSERT(isVersionChange());
794
795     serverConnection().deleteIndex(operation, objectStoreIdentifier, indexName);
796 }
797
798 void IDBTransaction::didDeleteIndexOnServer(const IDBResultData& resultData)
799 {
800     LOG(IndexedDB, "IDBTransaction::didDeleteIndexOnServer");
801     ASSERT_UNUSED(resultData, resultData.type() == IDBResultType::DeleteIndexSuccess || resultData.type() == IDBResultType::Error);
802 }
803
804 void IDBTransaction::operationDidComplete(TransactionOperation& operation)
805 {
806     ASSERT(m_transactionOperationMap.get(operation.identifier()) == &operation);
807     m_transactionOperationMap.remove(operation.identifier());
808
809     scheduleOperationTimer();
810 }
811
812 void IDBTransaction::establishOnServer()
813 {
814     LOG(IndexedDB, "IDBTransaction::establishOnServer");
815
816     serverConnection().establishTransaction(*this);
817 }
818
819 void IDBTransaction::activate()
820 {
821     if (isFinishedOrFinishing())
822         return;
823
824     m_state = IndexedDB::TransactionState::Active;
825 }
826
827 void IDBTransaction::deactivate()
828 {
829     if (m_state == IndexedDB::TransactionState::Active)
830         m_state = IndexedDB::TransactionState::Inactive;
831
832     scheduleOperationTimer();
833 }
834
835 } // namespace IDBClient
836 } // namespace WebCore
837
838 #endif // ENABLE(INDEXED_DATABASE)