2 * Copyright (C) 2010 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 "IDBDatabase.h"
29 #if ENABLE(INDEXED_DATABASE)
31 #include "EventQueue.h"
32 #include "ExceptionCode.h"
34 #include "IDBDatabaseCallbacksImpl.h"
35 #include "IDBDatabaseError.h"
36 #include "IDBDatabaseException.h"
37 #include "IDBEventDispatcher.h"
38 #include "IDBFactoryBackendInterface.h"
40 #include "IDBKeyPath.h"
41 #include "IDBObjectStore.h"
42 #include "IDBTracing.h"
43 #include "IDBTransaction.h"
44 #include "IDBVersionChangeEvent.h"
45 #include "IDBVersionChangeRequest.h"
46 #include "ScriptExecutionContext.h"
51 PassRefPtr<IDBDatabase> IDBDatabase::create(ScriptExecutionContext* context, PassRefPtr<IDBDatabaseBackendInterface> database)
53 RefPtr<IDBDatabase> idbDatabase(adoptRef(new IDBDatabase(context, database)));
54 idbDatabase->suspendIfNeeded();
55 return idbDatabase.release();
58 IDBDatabase::IDBDatabase(ScriptExecutionContext* context, PassRefPtr<IDBDatabaseBackendInterface> backend)
59 : ActiveDOMObject(context, this)
61 , m_closePending(false)
62 , m_contextStopped(false)
64 // We pass a reference of this object before it can be adopted.
65 relaxAdoptionRequirement();
66 m_databaseCallbacks = IDBDatabaseCallbacksImpl::create(this);
69 IDBDatabase::~IDBDatabase()
72 m_databaseCallbacks->unregisterDatabase(this);
75 void IDBDatabase::transactionCreated(IDBTransaction* transaction)
78 ASSERT(!m_transactions.contains(transaction));
79 m_transactions.add(transaction);
81 if (transaction->isVersionChange()) {
82 ASSERT(!m_versionChangeTransaction);
83 m_versionChangeTransaction = transaction;
87 void IDBDatabase::transactionFinished(IDBTransaction* transaction)
90 ASSERT(m_transactions.contains(transaction));
91 m_transactions.remove(transaction);
93 if (transaction->isVersionChange()) {
94 ASSERT(m_versionChangeTransaction == transaction);
95 m_versionChangeTransaction = 0;
98 if (m_closePending && m_transactions.isEmpty())
102 PassRefPtr<IDBObjectStore> IDBDatabase::createObjectStore(const String& name, const Dictionary& options, ExceptionCode& ec)
104 if (!m_versionChangeTransaction) {
105 ec = IDBDatabaseException::IDB_INVALID_STATE_ERR;
110 if (!options.isUndefinedOrNull()) {
111 String keyPathString;
112 Vector<String> keyPathArray;
113 if (options.get("keyPath", keyPathArray))
114 keyPath = IDBKeyPath(keyPathArray);
115 else if (options.getWithUndefinedOrNullCheck("keyPath", keyPathString))
116 keyPath = IDBKeyPath(keyPathString);
119 if (!keyPath.isNull() && !keyPath.isValid()) {
120 ec = IDBDatabaseException::IDB_SYNTAX_ERR;
124 bool autoIncrement = false;
125 if (!options.isUndefinedOrNull())
126 options.get("autoIncrement", autoIncrement);
128 if (autoIncrement && ((keyPath.type() == IDBKeyPath::StringType && keyPath.string().isEmpty()) || keyPath.type() == IDBKeyPath::ArrayType)) {
129 ec = IDBDatabaseException::IDB_INVALID_ACCESS_ERR;
133 RefPtr<IDBObjectStoreBackendInterface> objectStoreBackend = m_backend->createObjectStore(name, keyPath, autoIncrement, m_versionChangeTransaction->backend(), ec);
134 if (!objectStoreBackend) {
139 RefPtr<IDBObjectStore> objectStore = IDBObjectStore::create(objectStoreBackend.release(), m_versionChangeTransaction.get());
140 m_versionChangeTransaction->objectStoreCreated(name, objectStore);
141 return objectStore.release();
144 void IDBDatabase::deleteObjectStore(const String& name, ExceptionCode& ec)
146 if (!m_versionChangeTransaction) {
147 ec = IDBDatabaseException::IDB_INVALID_STATE_ERR;
151 m_backend->deleteObjectStore(name, m_versionChangeTransaction->backend(), ec);
153 m_versionChangeTransaction->objectStoreDeleted(name);
156 PassRefPtr<IDBVersionChangeRequest> IDBDatabase::setVersion(ScriptExecutionContext* context, const String& version, ExceptionCode& ec)
158 if (version.isNull()) {
159 ec = IDBDatabaseException::IDB_TYPE_ERR;
163 if (m_versionChangeTransaction) {
164 ec = IDBDatabaseException::IDB_INVALID_STATE_ERR;
168 RefPtr<IDBVersionChangeRequest> request = IDBVersionChangeRequest::create(context, IDBAny::create(this), version);
169 m_backend->setVersion(version, request, m_databaseCallbacks, ec);
173 PassRefPtr<IDBTransaction> IDBDatabase::transaction(ScriptExecutionContext* context, PassRefPtr<DOMStringList> prpStoreNames, const String& modeString, ExceptionCode& ec)
175 RefPtr<DOMStringList> storeNames = prpStoreNames;
176 if (!storeNames || storeNames->isEmpty()) {
177 ec = IDBDatabaseException::IDB_INVALID_ACCESS_ERR;
181 IDBTransaction::Mode mode = IDBTransaction::stringToMode(modeString, ec);
185 if (m_versionChangeTransaction || m_closePending) {
186 ec = IDBDatabaseException::IDB_INVALID_STATE_ERR;
190 // We need to create a new transaction synchronously. Locks are acquired asynchronously. Operations
191 // can be queued against the transaction at any point. They will start executing as soon as the
192 // appropriate locks have been acquired.
193 // Also note that each backend object corresponds to exactly one IDBTransaction object.
194 RefPtr<IDBTransactionBackendInterface> transactionBackend = m_backend->transaction(storeNames.get(), mode, ec);
195 if (!transactionBackend) {
199 RefPtr<IDBTransaction> transaction = IDBTransaction::create(context, transactionBackend, mode, this);
200 transactionBackend->setCallbacks(transaction.get());
201 return transaction.release();
204 PassRefPtr<IDBTransaction> IDBDatabase::transaction(ScriptExecutionContext* context, const String& storeName, const String& mode, ExceptionCode& ec)
206 RefPtr<DOMStringList> storeNames = DOMStringList::create();
207 storeNames->append(storeName);
208 return transaction(context, storeNames, mode, ec);
211 PassRefPtr<IDBTransaction> IDBDatabase::transaction(ScriptExecutionContext* context, const String& storeName, unsigned short mode, ExceptionCode& ec)
213 RefPtr<DOMStringList> storeNames = DOMStringList::create();
214 storeNames->append(storeName);
215 return transaction(context, storeNames, mode, ec);
218 PassRefPtr<IDBTransaction> IDBDatabase::transaction(ScriptExecutionContext* context, PassRefPtr<DOMStringList> prpStoreNames, unsigned short mode, ExceptionCode& ec)
220 DEFINE_STATIC_LOCAL(String, consoleMessage, ("Numeric transaction modes are deprecated in IDBDatabase.transaction. Use \"readonly\" or \"readwrite\"."));
221 context->addConsoleMessage(JSMessageSource, LogMessageType, WarningMessageLevel, consoleMessage);
222 AtomicString modeString = IDBTransaction::modeToString(IDBTransaction::Mode(mode), ec);
226 return transaction(context, prpStoreNames, modeString, ec);
229 void IDBDatabase::close()
234 m_closePending = true;
236 if (m_transactions.isEmpty())
240 void IDBDatabase::closeConnection()
242 ASSERT(m_closePending);
243 ASSERT(m_transactions.isEmpty());
245 m_backend->close(m_databaseCallbacks);
247 if (m_contextStopped || !scriptExecutionContext())
250 EventQueue* eventQueue = scriptExecutionContext()->eventQueue();
251 // Remove any pending versionchange events scheduled to fire on this
252 // connection. They would have been scheduled by the backend when another
253 // connection called setVersion, but the frontend connection is being
254 // closed before they could fire.
255 for (size_t i = 0; i < m_enqueuedEvents.size(); ++i) {
256 bool removed = eventQueue->cancelEvent(m_enqueuedEvents[i].get());
257 ASSERT_UNUSED(removed, removed);
261 void IDBDatabase::onVersionChange(const String& version)
263 if (m_contextStopped || !scriptExecutionContext())
266 enqueueEvent(IDBVersionChangeEvent::create(version, eventNames().versionchangeEvent));
269 void IDBDatabase::registerFrontendCallbacks()
271 m_backend->registerFrontendCallbacks(m_databaseCallbacks);
274 void IDBDatabase::enqueueEvent(PassRefPtr<Event> event)
276 ASSERT(!m_contextStopped);
277 ASSERT(scriptExecutionContext());
278 EventQueue* eventQueue = scriptExecutionContext()->eventQueue();
279 event->setTarget(this);
280 eventQueue->enqueueEvent(event.get());
281 m_enqueuedEvents.append(event);
284 bool IDBDatabase::dispatchEvent(PassRefPtr<Event> event)
286 IDB_TRACE("IDBDatabase::dispatchEvent");
287 ASSERT(event->type() == eventNames().versionchangeEvent);
288 for (size_t i = 0; i < m_enqueuedEvents.size(); ++i) {
289 if (m_enqueuedEvents[i].get() == event.get())
290 m_enqueuedEvents.remove(i);
292 return EventTarget::dispatchEvent(event.get());
295 void IDBDatabase::stop()
297 ActiveDOMObject::stop();
298 // Stop fires at a deterministic time, so we need to call close in it.
301 m_contextStopped = true;
304 const AtomicString& IDBDatabase::interfaceName() const
306 return eventNames().interfaceForIDBDatabase;
309 ScriptExecutionContext* IDBDatabase::scriptExecutionContext() const
311 return ActiveDOMObject::scriptExecutionContext();
314 EventTargetData* IDBDatabase::eventTargetData()
316 return &m_eventTargetData;
319 EventTargetData* IDBDatabase::ensureEventTargetData()
321 return &m_eventTargetData;
324 } // namespace WebCore
326 #endif // ENABLE(INDEXED_DATABASE)