Replace WTF::move with WTFMove
[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, IndexedDB::RequestType::Delete));
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, IndexedDB::RequestType::Open));
51 }
52     
53 IDBOpenDBRequest::IDBOpenDBRequest(IDBConnectionToServer& connection, ScriptExecutionContext* context, const IDBDatabaseIdentifier& databaseIdentifier, uint64_t version, IndexedDB::RequestType requestType)
54     : IDBRequest(connection, context)
55     , m_databaseIdentifier(databaseIdentifier)
56     , m_version(version)
57 {
58     m_requestType = requestType;
59 }
60
61 IDBOpenDBRequest::~IDBOpenDBRequest()
62 {
63 }
64
65 void IDBOpenDBRequest::onError(const IDBResultData& data)
66 {
67     m_domError = DOMError::create(data.error().name());
68     enqueueEvent(Event::create(eventNames().errorEvent, true, true));
69 }
70
71 void IDBOpenDBRequest::versionChangeTransactionDidFinish()
72 {
73     // 3.3.7 "versionchange" transaction steps
74     // When the transaction is finished, after firing complete/abort on the transaction, immediately set request's transaction property to null.
75     m_shouldExposeTransactionToDOM = false;
76 }
77
78 void IDBOpenDBRequest::fireSuccessAfterVersionChangeCommit()
79 {
80     LOG(IndexedDB, "IDBOpenDBRequest::fireSuccessAfterVersionChangeCommit()");
81
82     ASSERT(hasPendingActivity());
83     ASSERT(m_result);
84     ASSERT(m_result->type() == IDBAny::Type::IDBDatabase);
85     m_transaction->addRequest(*this);
86
87     enqueueEvent(Event::create(eventNames().successEvent, false, false));
88 }
89
90 void IDBOpenDBRequest::fireErrorAfterVersionChangeCompletion()
91 {
92     LOG(IndexedDB, "IDBOpenDBRequest::fireErrorAfterVersionChangeCompletion()");
93
94     ASSERT(hasPendingActivity());
95
96     IDBError idbError(IDBDatabaseException::AbortError);
97     m_domError = DOMError::create(idbError.name());
98     m_result = IDBAny::createUndefined();
99
100     m_transaction->addRequest(*this);
101     enqueueEvent(Event::create(eventNames().errorEvent, true, true));
102 }
103
104 bool IDBOpenDBRequest::dispatchEvent(Event& event)
105 {
106     bool result = IDBRequest::dispatchEvent(event);
107
108     if (m_transaction && m_transaction->isVersionChange() && (event.type() == eventNames().errorEvent || event.type() == eventNames().successEvent))
109         m_transaction->database().serverConnection().didFinishHandlingVersionChangeTransaction(*m_transaction);
110
111     return result;
112 }
113
114 void IDBOpenDBRequest::onSuccess(const IDBResultData& resultData)
115 {
116     LOG(IndexedDB, "IDBOpenDBRequest::onSuccess()");
117
118     if (!scriptExecutionContext())
119         return;
120
121     Ref<IDBDatabase> database = IDBDatabase::create(*scriptExecutionContext(), connection(), resultData);
122     m_result = IDBAny::create(WTFMove(database));
123     m_readyState = IDBRequestReadyState::Done;
124
125     enqueueEvent(Event::create(eventNames().successEvent, false, false));
126 }
127
128 void IDBOpenDBRequest::onUpgradeNeeded(const IDBResultData& resultData)
129 {
130     Ref<IDBDatabase> database = IDBDatabase::create(*scriptExecutionContext(), connection(), resultData);
131     Ref<IDBTransaction> transaction = database->startVersionChangeTransaction(resultData.transactionInfo(), *this);
132
133     ASSERT(transaction->info().mode() == IndexedDB::TransactionMode::VersionChange);
134     ASSERT(transaction->originalDatabaseInfo());
135
136     uint64_t oldVersion = transaction->originalDatabaseInfo()->version();
137     uint64_t newVersion = transaction->info().newVersion();
138
139     LOG(IndexedDB, "IDBOpenDBRequest::onUpgradeNeeded() - current version is %" PRIu64 ", new is %" PRIu64, oldVersion, newVersion);
140
141     m_result = IDBAny::create(WTFMove(database));
142     m_readyState = IDBRequestReadyState::Done;
143     m_transaction = adoptRef(&transaction.leakRef());
144     m_transaction->addRequest(*this);
145
146     enqueueEvent(IDBVersionChangeEvent::create(oldVersion, newVersion, eventNames().upgradeneededEvent));
147 }
148
149 void IDBOpenDBRequest::onDeleteDatabaseSuccess(const IDBResultData& resultData)
150 {
151     uint64_t oldVersion = resultData.databaseInfo().version();
152
153     LOG(IndexedDB, "IDBOpenDBRequest::onDeleteDatabaseSuccess() - current version is %" PRIu64, oldVersion);
154
155     m_readyState = IDBRequestReadyState::Done;
156     m_result = IDBAny::createUndefined();
157
158     enqueueEvent(IDBVersionChangeEvent::create(oldVersion, 0, eventNames().successEvent));
159 }
160
161 void IDBOpenDBRequest::requestCompleted(const IDBResultData& data)
162 {
163     LOG(IndexedDB, "IDBOpenDBRequest::requestCompleted");
164
165     switch (data.type()) {
166     case IDBResultType::Error:
167         onError(data);
168         break;
169     case IDBResultType::OpenDatabaseSuccess:
170         onSuccess(data);
171         break;
172     case IDBResultType::OpenDatabaseUpgradeNeeded:
173         onUpgradeNeeded(data);
174         break;
175     case IDBResultType::DeleteDatabaseSuccess:
176         onDeleteDatabaseSuccess(data);
177         break;
178     default:
179         RELEASE_ASSERT_NOT_REACHED();
180     }
181 }
182
183 void IDBOpenDBRequest::requestBlocked(uint64_t oldVersion, uint64_t newVersion)
184 {
185     LOG(IndexedDB, "IDBOpenDBRequest::requestBlocked");
186     enqueueEvent(IDBVersionChangeEvent::create(oldVersion, newVersion, eventNames().blockedEvent));
187 }
188
189 } // namespace IDBClient
190 } // namespace WebCore
191
192 #endif // ENABLE(INDEXED_DATABASE)