2 * Copyright (C) 2012 Google 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
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
18 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include "modules/indexeddb/IDBOpenDBRequest.h"
29 #include "bindings/v8/Nullable.h"
30 #include "core/dom/ExceptionCode.h"
31 #include "core/dom/ExecutionContext.h"
32 #include "modules/indexeddb/IDBDatabase.h"
33 #include "modules/indexeddb/IDBDatabaseCallbacks.h"
34 #include "modules/indexeddb/IDBPendingTransactionMonitor.h"
35 #include "modules/indexeddb/IDBTracing.h"
36 #include "modules/indexeddb/IDBVersionChangeEvent.h"
38 using blink::WebIDBDatabase;
42 PassRefPtr<IDBOpenDBRequest> IDBOpenDBRequest::create(ExecutionContext* context, PassRefPtr<IDBDatabaseCallbacks> callbacks, int64_t transactionId, int64_t version)
44 RefPtr<IDBOpenDBRequest> request(adoptRef(new IDBOpenDBRequest(context, callbacks, transactionId, version)));
45 request->suspendIfNeeded();
46 return request.release();
49 IDBOpenDBRequest::IDBOpenDBRequest(ExecutionContext* context, PassRefPtr<IDBDatabaseCallbacks> callbacks, int64_t transactionId, int64_t version)
50 : IDBRequest(context, IDBAny::createNull(), 0)
51 , m_databaseCallbacks(callbacks)
52 , m_transactionId(transactionId)
55 ASSERT(!resultAsAny());
56 ScriptWrappable::init(this);
59 IDBOpenDBRequest::~IDBOpenDBRequest()
63 const AtomicString& IDBOpenDBRequest::interfaceName() const
65 return EventTargetNames::IDBOpenDBRequest;
68 void IDBOpenDBRequest::onBlocked(int64_t oldVersion)
70 IDB_TRACE("IDBOpenDBRequest::onBlocked()");
71 if (!shouldEnqueueEvent())
73 Nullable<unsigned long long> newVersionNullable = (m_version == IDBDatabaseMetadata::DefaultIntVersion) ? Nullable<unsigned long long>() : Nullable<unsigned long long>(m_version);
74 enqueueEvent(IDBVersionChangeEvent::create(EventTypeNames::blocked, oldVersion, newVersionNullable));
77 void IDBOpenDBRequest::onUpgradeNeeded(int64_t oldVersion, PassOwnPtr<WebIDBDatabase> backend, const IDBDatabaseMetadata& metadata, blink::WebIDBDataLoss dataLoss, String dataLossMessage)
79 IDB_TRACE("IDBOpenDBRequest::onUpgradeNeeded()");
80 if (m_contextStopped || !executionContext()) {
81 OwnPtr<WebIDBDatabase> db = backend;
82 db->abort(m_transactionId);
86 if (!shouldEnqueueEvent())
89 ASSERT(m_databaseCallbacks);
91 RefPtr<IDBDatabase> idbDatabase = IDBDatabase::create(executionContext(), backend, m_databaseCallbacks.release());
92 idbDatabase->setMetadata(metadata);
94 if (oldVersion == IDBDatabaseMetadata::NoIntVersion) {
95 // This database hasn't had an integer version before.
96 oldVersion = IDBDatabaseMetadata::DefaultIntVersion;
98 IDBDatabaseMetadata oldMetadata(metadata);
99 oldMetadata.intVersion = oldVersion;
101 m_transaction = IDBTransaction::create(executionContext(), m_transactionId, idbDatabase.get(), this, oldMetadata);
102 setResult(IDBAny::create(idbDatabase.release()));
104 if (m_version == IDBDatabaseMetadata::NoIntVersion)
106 enqueueEvent(IDBVersionChangeEvent::create(EventTypeNames::upgradeneeded, oldVersion, m_version, dataLoss, dataLossMessage));
109 void IDBOpenDBRequest::onSuccess(PassOwnPtr<WebIDBDatabase> backend, const IDBDatabaseMetadata& metadata)
111 IDB_TRACE("IDBOpenDBRequest::onSuccess()");
112 if (m_contextStopped || !executionContext()) {
113 OwnPtr<WebIDBDatabase> db = backend;
118 if (!shouldEnqueueEvent())
121 RefPtr<IDBDatabase> idbDatabase;
123 // Previous onUpgradeNeeded call delivered the backend.
124 ASSERT(!backend.get());
125 idbDatabase = resultAsAny()->idbDatabase();
127 ASSERT(!m_databaseCallbacks);
129 ASSERT(backend.get());
130 ASSERT(m_databaseCallbacks);
131 idbDatabase = IDBDatabase::create(executionContext(), backend, m_databaseCallbacks.release());
132 setResult(IDBAny::create(idbDatabase.get()));
134 idbDatabase->setMetadata(metadata);
135 enqueueEvent(Event::create(EventTypeNames::success));
138 bool IDBOpenDBRequest::shouldEnqueueEvent() const
140 if (m_contextStopped || !executionContext())
142 ASSERT(m_readyState == PENDING || m_readyState == DONE);
143 if (m_requestAborted)
148 bool IDBOpenDBRequest::dispatchEvent(PassRefPtr<Event> event)
150 // If the connection closed between onUpgradeNeeded and the delivery of the "success" event,
151 // an "error" event should be fired instead.
152 if (event->type() == EventTypeNames::success && resultAsAny()->type() == IDBAny::IDBDatabaseType && resultAsAny()->idbDatabase()->isClosePending()) {
153 dequeueEvent(event.get());
155 onError(DOMError::create(AbortError, "The connection was closed."));
159 return IDBRequest::dispatchEvent(event);
162 } // namespace WebCore