2 * Copyright (C) 2015 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
27 #include "IDBOpenDBRequestImpl.h"
29 #if ENABLE(INDEXED_DATABASE)
31 #include "IDBDatabaseImpl.h"
33 #include "IDBResultData.h"
34 #include "IDBTransactionImpl.h"
35 #include "IDBVersionChangeEventImpl.h"
41 Ref<IDBOpenDBRequest> IDBOpenDBRequest::createDeleteRequest(IDBConnectionToServer& connection, ScriptExecutionContext* context, const IDBDatabaseIdentifier& databaseIdentifier)
43 ASSERT(databaseIdentifier.isValid());
44 return adoptRef(*new IDBOpenDBRequest(connection, context, databaseIdentifier, 0));
47 Ref<IDBOpenDBRequest> IDBOpenDBRequest::createOpenRequest(IDBConnectionToServer& connection, ScriptExecutionContext* context, const IDBDatabaseIdentifier& databaseIdentifier, uint64_t version)
49 ASSERT(databaseIdentifier.isValid());
50 return adoptRef(*new IDBOpenDBRequest(connection, context, databaseIdentifier, version));
53 IDBOpenDBRequest::IDBOpenDBRequest(IDBConnectionToServer& connection, ScriptExecutionContext* context, const IDBDatabaseIdentifier& databaseIdentifier, uint64_t version)
54 : IDBRequest(connection, context)
55 , m_databaseIdentifier(databaseIdentifier)
60 IDBOpenDBRequest::~IDBOpenDBRequest()
64 void IDBOpenDBRequest::onError(const IDBResultData& data)
66 m_domError = DOMError::create(data.error().name());
67 enqueueEvent(Event::create(eventNames().errorEvent, true, true));
70 void IDBOpenDBRequest::fireSuccessAfterVersionChangeCommit()
72 LOG(IndexedDB, "IDBOpenDBRequest::fireSuccessAfterVersionChangeCommit()");
74 ASSERT(hasPendingActivity());
76 ASSERT(m_result->type() == IDBAny::Type::IDBDatabase);
77 m_transaction->addRequest(*this);
79 enqueueEvent(Event::create(eventNames().successEvent, false, false));
82 void IDBOpenDBRequest::onSuccess(const IDBResultData& resultData)
84 LOG(IndexedDB, "IDBOpenDBRequest::onSuccess()");
86 if (!scriptExecutionContext())
89 Ref<IDBDatabase> database = IDBDatabase::create(*scriptExecutionContext(), connection(), resultData);
90 m_result = IDBAny::create(WTF::move(database));
91 m_readyState = IDBRequestReadyState::Done;
93 enqueueEvent(Event::create(eventNames().successEvent, false, false));
96 void IDBOpenDBRequest::onUpgradeNeeded(const IDBResultData& resultData)
98 Ref<IDBDatabase> database = IDBDatabase::create(*scriptExecutionContext(), connection(), resultData);
99 Ref<IDBTransaction> transaction = database->startVersionChangeTransaction(resultData.transactionInfo(), *this);
101 ASSERT(transaction->info().mode() == IndexedDB::TransactionMode::VersionChange);
103 uint64_t oldVersion = database->info().version();
104 uint64_t newVersion = transaction->info().newVersion();
106 LOG(IndexedDB, "IDBOpenDBRequest::onUpgradeNeeded() - current version is %" PRIu64 ", new is %" PRIu64, oldVersion, newVersion);
108 m_result = IDBAny::create(WTF::move(database));
109 m_readyState = IDBRequestReadyState::Done;
110 m_transaction = adoptRef(&transaction.leakRef());
111 m_transaction->addRequest(*this);
113 enqueueEvent(IDBVersionChangeEvent::create(oldVersion, newVersion, eventNames().upgradeneededEvent));
116 void IDBOpenDBRequest::onDeleteDatabaseSuccess(const IDBResultData& resultData)
118 uint64_t oldVersion = resultData.databaseInfo().version();
120 LOG(IndexedDB, "IDBOpenDBRequest::onDeleteDatabaseSuccess() - current version is %" PRIu64, oldVersion);
122 m_readyState = IDBRequestReadyState::Done;
124 enqueueEvent(IDBVersionChangeEvent::create(oldVersion, 0, eventNames().successEvent));
127 void IDBOpenDBRequest::requestCompleted(const IDBResultData& data)
129 LOG(IndexedDB, "IDBOpenDBRequest::requestCompleted");
131 switch (data.type()) {
132 case IDBResultType::Error:
135 case IDBResultType::OpenDatabaseSuccess:
138 case IDBResultType::OpenDatabaseUpgradeNeeded:
139 onUpgradeNeeded(data);
141 case IDBResultType::DeleteDatabaseSuccess:
142 onDeleteDatabaseSuccess(data);
145 RELEASE_ASSERT_NOT_REACHED();
149 } // namespace IDBClient
150 } // namespace WebCore
152 #endif // ENABLE(INDEXED_DATABASE)