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);
67 m_metadata = m_backend->metadata();
70 IDBDatabase::~IDBDatabase()
73 m_databaseCallbacks->unregisterDatabase(this);
76 void IDBDatabase::transactionCreated(IDBTransaction* transaction)
79 ASSERT(!m_transactions.contains(transaction));
80 m_transactions.add(transaction);
82 if (transaction->isVersionChange()) {
83 ASSERT(!m_versionChangeTransaction);
84 m_versionChangeTransaction = transaction;
85 m_metadata = m_backend->metadata();
89 void IDBDatabase::transactionFinished(IDBTransaction* transaction)
92 ASSERT(m_transactions.contains(transaction));
93 m_transactions.remove(transaction);
95 if (transaction->isVersionChange()) {
96 ASSERT(m_versionChangeTransaction == transaction);
97 m_versionChangeTransaction = 0;
98 m_metadata = m_backend->metadata();
101 if (m_closePending && m_transactions.isEmpty())
105 PassRefPtr<DOMStringList> IDBDatabase::objectStoreNames() const
107 RefPtr<DOMStringList> objectStoreNames = DOMStringList::create();
108 for (IDBDatabaseMetadata::ObjectStoreMap::const_iterator it = m_metadata.objectStores.begin(); it != m_metadata.objectStores.end(); ++it)
109 objectStoreNames->append(it->first);
110 objectStoreNames->sort();
111 return objectStoreNames.release();
114 PassRefPtr<IDBObjectStore> IDBDatabase::createObjectStore(const String& name, const Dictionary& options, ExceptionCode& ec)
116 if (!m_versionChangeTransaction) {
117 ec = IDBDatabaseException::IDB_INVALID_STATE_ERR;
122 if (!options.isUndefinedOrNull()) {
123 String keyPathString;
124 Vector<String> keyPathArray;
125 if (options.get("keyPath", keyPathArray))
126 keyPath = IDBKeyPath(keyPathArray);
127 else if (options.getWithUndefinedOrNullCheck("keyPath", keyPathString))
128 keyPath = IDBKeyPath(keyPathString);
131 if (!keyPath.isNull() && !keyPath.isValid()) {
132 ec = IDBDatabaseException::IDB_SYNTAX_ERR;
136 bool autoIncrement = false;
137 if (!options.isUndefinedOrNull())
138 options.get("autoIncrement", autoIncrement);
140 if (autoIncrement && ((keyPath.type() == IDBKeyPath::StringType && keyPath.string().isEmpty()) || keyPath.type() == IDBKeyPath::ArrayType)) {
141 ec = IDBDatabaseException::IDB_INVALID_ACCESS_ERR;
145 RefPtr<IDBObjectStoreBackendInterface> objectStoreBackend = m_backend->createObjectStore(name, keyPath, autoIncrement, m_versionChangeTransaction->backend(), ec);
146 if (!objectStoreBackend) {
151 IDBObjectStoreMetadata metadata(name, keyPath, autoIncrement);
152 RefPtr<IDBObjectStore> objectStore = IDBObjectStore::create(metadata, objectStoreBackend.release(), m_versionChangeTransaction.get());
153 m_metadata.objectStores.set(name, metadata);
155 m_versionChangeTransaction->objectStoreCreated(name, objectStore);
156 return objectStore.release();
159 void IDBDatabase::deleteObjectStore(const String& name, ExceptionCode& ec)
161 if (!m_versionChangeTransaction) {
162 ec = IDBDatabaseException::IDB_INVALID_STATE_ERR;
166 m_backend->deleteObjectStore(name, m_versionChangeTransaction->backend(), ec);
168 m_versionChangeTransaction->objectStoreDeleted(name);
169 m_metadata.objectStores.remove(name);
173 PassRefPtr<IDBVersionChangeRequest> IDBDatabase::setVersion(ScriptExecutionContext* context, const String& version, ExceptionCode& ec)
175 if (version.isNull()) {
176 ec = IDBDatabaseException::IDB_TYPE_ERR;
180 if (m_versionChangeTransaction) {
181 ec = IDBDatabaseException::IDB_INVALID_STATE_ERR;
185 RefPtr<IDBVersionChangeRequest> request = IDBVersionChangeRequest::create(context, IDBAny::create(this), version);
186 m_backend->setVersion(version, request, m_databaseCallbacks, ec);
190 PassRefPtr<IDBTransaction> IDBDatabase::transaction(ScriptExecutionContext* context, PassRefPtr<DOMStringList> prpStoreNames, const String& modeString, ExceptionCode& ec)
192 RefPtr<DOMStringList> storeNames = prpStoreNames;
193 if (!storeNames || storeNames->isEmpty()) {
194 ec = IDBDatabaseException::IDB_INVALID_ACCESS_ERR;
198 IDBTransaction::Mode mode = IDBTransaction::stringToMode(modeString, ec);
202 if (m_versionChangeTransaction || m_closePending) {
203 ec = IDBDatabaseException::IDB_INVALID_STATE_ERR;
207 // We need to create a new transaction synchronously. Locks are acquired asynchronously. Operations
208 // can be queued against the transaction at any point. They will start executing as soon as the
209 // appropriate locks have been acquired.
210 // Also note that each backend object corresponds to exactly one IDBTransaction object.
211 RefPtr<IDBTransactionBackendInterface> transactionBackend = m_backend->transaction(storeNames.get(), mode, ec);
212 if (!transactionBackend) {
216 RefPtr<IDBTransaction> transaction = IDBTransaction::create(context, transactionBackend, mode, this);
217 transactionBackend->setCallbacks(transaction.get());
218 return transaction.release();
221 PassRefPtr<IDBTransaction> IDBDatabase::transaction(ScriptExecutionContext* context, const String& storeName, const String& mode, ExceptionCode& ec)
223 RefPtr<DOMStringList> storeNames = DOMStringList::create();
224 storeNames->append(storeName);
225 return transaction(context, storeNames, mode, ec);
228 PassRefPtr<IDBTransaction> IDBDatabase::transaction(ScriptExecutionContext* context, const String& storeName, unsigned short mode, ExceptionCode& ec)
230 RefPtr<DOMStringList> storeNames = DOMStringList::create();
231 storeNames->append(storeName);
232 return transaction(context, storeNames, mode, ec);
235 PassRefPtr<IDBTransaction> IDBDatabase::transaction(ScriptExecutionContext* context, PassRefPtr<DOMStringList> prpStoreNames, unsigned short mode, ExceptionCode& ec)
237 DEFINE_STATIC_LOCAL(String, consoleMessage, ("Numeric transaction modes are deprecated in IDBDatabase.transaction. Use \"readonly\" or \"readwrite\"."));
238 context->addConsoleMessage(JSMessageSource, LogMessageType, WarningMessageLevel, consoleMessage);
239 AtomicString modeString = IDBTransaction::modeToString(IDBTransaction::Mode(mode), ec);
243 return transaction(context, prpStoreNames, modeString, ec);
246 void IDBDatabase::close()
251 m_closePending = true;
253 if (m_transactions.isEmpty())
257 void IDBDatabase::closeConnection()
259 ASSERT(m_closePending);
260 ASSERT(m_transactions.isEmpty());
262 m_backend->close(m_databaseCallbacks);
264 if (m_contextStopped || !scriptExecutionContext())
267 EventQueue* eventQueue = scriptExecutionContext()->eventQueue();
268 // Remove any pending versionchange events scheduled to fire on this
269 // connection. They would have been scheduled by the backend when another
270 // connection called setVersion, but the frontend connection is being
271 // closed before they could fire.
272 for (size_t i = 0; i < m_enqueuedEvents.size(); ++i) {
273 bool removed = eventQueue->cancelEvent(m_enqueuedEvents[i].get());
274 ASSERT_UNUSED(removed, removed);
278 void IDBDatabase::onVersionChange(const String& version)
280 if (m_contextStopped || !scriptExecutionContext())
283 enqueueEvent(IDBVersionChangeEvent::create(version, eventNames().versionchangeEvent));
286 void IDBDatabase::registerFrontendCallbacks()
288 m_backend->registerFrontendCallbacks(m_databaseCallbacks);
291 void IDBDatabase::enqueueEvent(PassRefPtr<Event> event)
293 ASSERT(!m_contextStopped);
294 ASSERT(scriptExecutionContext());
295 EventQueue* eventQueue = scriptExecutionContext()->eventQueue();
296 event->setTarget(this);
297 eventQueue->enqueueEvent(event.get());
298 m_enqueuedEvents.append(event);
301 bool IDBDatabase::dispatchEvent(PassRefPtr<Event> event)
303 IDB_TRACE("IDBDatabase::dispatchEvent");
304 ASSERT(event->type() == eventNames().versionchangeEvent);
305 for (size_t i = 0; i < m_enqueuedEvents.size(); ++i) {
306 if (m_enqueuedEvents[i].get() == event.get())
307 m_enqueuedEvents.remove(i);
309 return EventTarget::dispatchEvent(event.get());
312 void IDBDatabase::stop()
314 ActiveDOMObject::stop();
315 // Stop fires at a deterministic time, so we need to call close in it.
318 m_contextStopped = true;
321 const AtomicString& IDBDatabase::interfaceName() const
323 return eventNames().interfaceForIDBDatabase;
326 ScriptExecutionContext* IDBDatabase::scriptExecutionContext() const
328 return ActiveDOMObject::scriptExecutionContext();
331 EventTargetData* IDBDatabase::eventTargetData()
333 return &m_eventTargetData;
336 EventTargetData* IDBDatabase::ensureEventTargetData()
338 return &m_eventTargetData;
341 } // namespace WebCore
343 #endif // ENABLE(INDEXED_DATABASE)