Modern IDB: After versionchange transactions complete, fire onsuccess on the original...
[WebKit-https.git] / Source / WebCore / Modules / indexeddb / client / IDBOpenDBRequestImpl.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 "IDBOpenDBRequestImpl.h"
28
29 #if ENABLE(INDEXED_DATABASE)
30
31 #include "IDBDatabaseImpl.h"
32 #include "IDBError.h"
33 #include "IDBResultData.h"
34 #include "IDBTransactionImpl.h"
35 #include "IDBVersionChangeEventImpl.h"
36 #include "Logging.h"
37
38 namespace WebCore {
39 namespace IDBClient {
40
41 Ref<IDBOpenDBRequest> IDBOpenDBRequest::createDeleteRequest(IDBConnectionToServer& connection, ScriptExecutionContext* context, const IDBDatabaseIdentifier& databaseIdentifier)
42 {
43     ASSERT(databaseIdentifier.isValid());
44     return adoptRef(*new IDBOpenDBRequest(connection, context, databaseIdentifier, 0));
45 }
46
47 Ref<IDBOpenDBRequest> IDBOpenDBRequest::createOpenRequest(IDBConnectionToServer& connection, ScriptExecutionContext* context, const IDBDatabaseIdentifier& databaseIdentifier, uint64_t version)
48 {
49     ASSERT(databaseIdentifier.isValid());
50     return adoptRef(*new IDBOpenDBRequest(connection, context, databaseIdentifier, version));
51 }
52     
53 IDBOpenDBRequest::IDBOpenDBRequest(IDBConnectionToServer& connection, ScriptExecutionContext* context, const IDBDatabaseIdentifier& databaseIdentifier, uint64_t version)
54     : IDBRequest(connection, context)
55     , m_databaseIdentifier(databaseIdentifier)
56     , m_version(version)
57 {
58 }
59
60 IDBOpenDBRequest::~IDBOpenDBRequest()
61 {
62 }
63
64 void IDBOpenDBRequest::onError(const IDBResultData& data)
65 {
66     m_domError = DOMError::create(data.error().name());
67     enqueueEvent(Event::create(eventNames().errorEvent, true, true));
68 }
69
70 void IDBOpenDBRequest::fireSuccessAfterVersionChangeCommit()
71 {
72     LOG(IndexedDB, "IDBOpenDBRequest::fireSuccessAfterVersionChangeCommit()");
73
74     ASSERT(m_result);
75     ASSERT(m_result->type() == IDBAny::Type::IDBDatabase);
76     m_transaction->addRequest(*this);
77
78     enqueueEvent(Event::create(eventNames().successEvent, false, false));
79 }
80
81 void IDBOpenDBRequest::onSuccess(const IDBResultData& resultData)
82 {
83     LOG(IndexedDB, "IDBOpenDBRequest::onSuccess()");
84
85     if (!scriptExecutionContext())
86         return;
87
88     Ref<IDBDatabase> database = IDBDatabase::create(*scriptExecutionContext(), connection(), resultData);
89     m_result = IDBAny::create(WTF::move(database));
90     m_readyState = IDBRequestReadyState::Done;
91
92     enqueueEvent(Event::create(eventNames().successEvent, false, false));
93 }
94
95 void IDBOpenDBRequest::onUpgradeNeeded(const IDBResultData& resultData)
96 {
97     Ref<IDBDatabase> database = IDBDatabase::create(*scriptExecutionContext(), connection(), resultData);
98     Ref<IDBTransaction> transaction = database->startVersionChangeTransaction(resultData.transactionInfo(), *this);
99
100     ASSERT(transaction->info().mode() == IndexedDB::TransactionMode::VersionChange);
101
102     uint64_t oldVersion = database->info().version();
103     uint64_t newVersion = transaction->info().newVersion();
104
105     LOG(IndexedDB, "IDBOpenDBRequest::onUpgradeNeeded() - current version is %" PRIu64 ", new is %" PRIu64, oldVersion, newVersion);
106
107     m_result = IDBAny::create(WTF::move(database));
108     m_readyState = IDBRequestReadyState::Done;
109     m_transaction = adoptRef(&transaction.leakRef());
110     m_transaction->addRequest(*this);
111
112     enqueueEvent(IDBVersionChangeEvent::create(oldVersion, newVersion, eventNames().upgradeneededEvent));
113 }
114
115 void IDBOpenDBRequest::onDeleteDatabaseSuccess(const IDBResultData& resultData)
116 {
117     uint64_t oldVersion = resultData.databaseInfo().version();
118
119     LOG(IndexedDB, "IDBOpenDBRequest::onDeleteDatabaseSuccess() - current version is %" PRIu64, oldVersion);
120
121     m_readyState = IDBRequestReadyState::Done;
122
123     enqueueEvent(IDBVersionChangeEvent::create(oldVersion, 0, eventNames().successEvent));
124 }
125
126 void IDBOpenDBRequest::requestCompleted(const IDBResultData& data)
127 {
128     LOG(IndexedDB, "IDBOpenDBRequest::requestCompleted");
129
130     switch (data.type()) {
131     case IDBResultType::Error:
132         onError(data);
133         break;
134     case IDBResultType::OpenDatabaseSuccess:
135         onSuccess(data);
136         break;
137     case IDBResultType::OpenDatabaseUpgradeNeeded:
138         onUpgradeNeeded(data);
139         break;
140     case IDBResultType::DeleteDatabaseSuccess:
141         onDeleteDatabaseSuccess(data);
142         break;
143     default:
144         RELEASE_ASSERT_NOT_REACHED();
145     }
146 }
147
148 } // namespace IDBClient
149 } // namespace WebCore
150
151 #endif // ENABLE(INDEXED_DATABASE)