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 "IDBTransaction.h"
29 #if ENABLE(INDEXED_DATABASE)
31 #include "EventException.h"
32 #include "EventQueue.h"
33 #include "IDBDatabase.h"
34 #include "IDBDatabaseException.h"
35 #include "IDBEventDispatcher.h"
37 #include "IDBObjectStore.h"
38 #include "IDBObjectStoreBackendInterface.h"
39 #include "IDBPendingTransactionMonitor.h"
40 #include "IDBTracing.h"
44 PassRefPtr<IDBTransaction> IDBTransaction::create(ScriptExecutionContext* context, PassRefPtr<IDBTransactionBackendInterface> backend, IDBTransaction::Mode mode, IDBDatabase* db)
46 RefPtr<IDBTransaction> transaction(adoptRef(new IDBTransaction(context, backend, mode, db)));
47 transaction->suspendIfNeeded();
48 return transaction.release();
51 const AtomicString& IDBTransaction::modeReadOnly()
53 DEFINE_STATIC_LOCAL(AtomicString, readonly, ("readonly"));
57 const AtomicString& IDBTransaction::modeReadWrite()
59 DEFINE_STATIC_LOCAL(AtomicString, readwrite, ("readwrite"));
63 const AtomicString& IDBTransaction::modeVersionChange()
65 DEFINE_STATIC_LOCAL(AtomicString, versionchange, ("versionchange"));
69 const AtomicString& IDBTransaction::modeReadOnlyLegacy()
71 DEFINE_STATIC_LOCAL(AtomicString, readonly, ("0"));
75 const AtomicString& IDBTransaction::modeReadWriteLegacy()
77 DEFINE_STATIC_LOCAL(AtomicString, readwrite, ("1"));
82 IDBTransaction::IDBTransaction(ScriptExecutionContext* context, PassRefPtr<IDBTransactionBackendInterface> backend, IDBTransaction::Mode mode, IDBDatabase* db)
83 : ActiveDOMObject(context, this)
88 , m_transactionFinished(false)
89 , m_contextStopped(false)
92 ASSERT(m_mode == m_backend->mode());
93 IDBPendingTransactionMonitor::addPendingTransaction(m_backend.get());
94 // We pass a reference of this object before it can be adopted.
95 relaxAdoptionRequirement();
96 m_database->transactionCreated(this);
99 IDBTransaction::~IDBTransaction()
103 IDBTransactionBackendInterface* IDBTransaction::backend() const
105 return m_backend.get();
108 bool IDBTransaction::isFinished() const
110 return m_transactionFinished;
113 const String& IDBTransaction::mode() const
115 ExceptionCode ec = 0;
116 const AtomicString& mode = modeToString(m_mode, ec);
121 IDBDatabase* IDBTransaction::db() const
123 return m_database.get();
126 PassRefPtr<DOMError> IDBTransaction::error(ExceptionCode& ec) const
128 if (!m_transactionFinished) {
129 ec = IDBDatabaseException::IDB_INVALID_STATE_ERR;
135 void IDBTransaction::setError(PassRefPtr<DOMError> error)
137 ASSERT(!m_transactionFinished);
140 // The first error to be set is the true cause of the
141 // transaction abort.
146 PassRefPtr<IDBObjectStore> IDBTransaction::objectStore(const String& name, ExceptionCode& ec)
148 if (m_transactionFinished) {
149 ec = IDBDatabaseException::IDB_INVALID_STATE_ERR;
153 IDBObjectStoreMap::iterator it = m_objectStoreMap.find(name);
154 if (it != m_objectStoreMap.end())
157 RefPtr<IDBObjectStoreBackendInterface> objectStoreBackend = m_backend->objectStore(name, ec);
158 ASSERT(!objectStoreBackend != !ec); // If we didn't get a store, we should have gotten an exception code. And vice versa.
162 const IDBDatabaseMetadata& metadata = m_database->metadata();
163 IDBDatabaseMetadata::ObjectStoreMap::const_iterator mdit = metadata.objectStores.find(name);
164 ASSERT(mdit != metadata.objectStores.end());
166 RefPtr<IDBObjectStore> objectStore = IDBObjectStore::create(mdit->second, objectStoreBackend, this);
167 objectStoreCreated(name, objectStore);
168 return objectStore.release();
171 void IDBTransaction::objectStoreCreated(const String& name, PassRefPtr<IDBObjectStore> prpObjectStore)
173 ASSERT(!m_transactionFinished);
174 RefPtr<IDBObjectStore> objectStore = prpObjectStore;
175 m_objectStoreMap.set(name, objectStore);
176 if (isVersionChange())
177 m_objectStoreCleanupMap.set(objectStore, objectStore->metadata());
180 void IDBTransaction::objectStoreDeleted(const String& name)
182 ASSERT(!m_transactionFinished);
183 ASSERT(isVersionChange());
184 IDBObjectStoreMap::iterator it = m_objectStoreMap.find(name);
185 if (it != m_objectStoreMap.end()) {
186 RefPtr<IDBObjectStore> objectStore = it->second;
187 m_objectStoreMap.remove(name);
188 objectStore->markDeleted();
189 m_objectStoreCleanupMap.set(objectStore, objectStore->metadata());
193 void IDBTransaction::abort()
195 if (m_transactionFinished)
198 RefPtr<IDBTransaction> selfRef = this;
203 IDBTransaction::OpenCursorNotifier::OpenCursorNotifier(PassRefPtr<IDBTransaction> transaction, IDBCursor* cursor)
204 : m_transaction(transaction),
207 m_transaction->registerOpenCursor(m_cursor);
210 IDBTransaction::OpenCursorNotifier::~OpenCursorNotifier()
212 m_transaction->unregisterOpenCursor(m_cursor);
215 void IDBTransaction::registerOpenCursor(IDBCursor* cursor)
217 m_openCursors.add(cursor);
220 void IDBTransaction::unregisterOpenCursor(IDBCursor* cursor)
222 m_openCursors.remove(cursor);
225 void IDBTransaction::closeOpenCursors()
227 HashSet<IDBCursor*> cursors;
228 cursors.swap(m_openCursors);
229 for (HashSet<IDBCursor*>::iterator i = cursors.begin(); i != cursors.end(); ++i)
233 void IDBTransaction::registerRequest(IDBRequest* request)
235 m_childRequests.add(request);
238 void IDBTransaction::unregisterRequest(IDBRequest* request)
240 // If we aborted the request, it will already have been removed.
241 m_childRequests.remove(request);
244 void IDBTransaction::onAbort()
246 ASSERT(!m_transactionFinished);
248 while (!m_childRequests.isEmpty()) {
249 IDBRequest* request = *m_childRequests.begin();
250 m_childRequests.remove(request);
254 if (isVersionChange()) {
255 for (IDBObjectStoreMetadataMap::iterator it = m_objectStoreCleanupMap.begin(); it != m_objectStoreCleanupMap.end(); ++it)
256 it->first->setMetadata(it->second);
258 m_objectStoreCleanupMap.clear();
260 m_database->transactionFinished(this);
262 if (m_contextStopped || !scriptExecutionContext())
265 enqueueEvent(Event::create(eventNames().abortEvent, true, false));
268 void IDBTransaction::onComplete()
270 ASSERT(!m_transactionFinished);
272 m_objectStoreCleanupMap.clear();
274 m_database->transactionFinished(this);
276 if (m_contextStopped || !scriptExecutionContext())
279 enqueueEvent(Event::create(eventNames().completeEvent, false, false));
282 bool IDBTransaction::hasPendingActivity() const
284 // FIXME: In an ideal world, we should return true as long as anyone has a or can
285 // get a handle to us or any child request object and any of those have
286 // event listeners. This is in order to handle user generated events properly.
287 return !m_transactionFinished || ActiveDOMObject::hasPendingActivity();
290 IDBTransaction::Mode IDBTransaction::stringToMode(const String& modeString, ExceptionCode& ec)
292 if (modeString.isNull()
293 || modeString == IDBTransaction::modeReadOnly())
294 return IDBTransaction::READ_ONLY;
295 if (modeString == IDBTransaction::modeReadWrite())
296 return IDBTransaction::READ_WRITE;
297 ec = IDBDatabaseException::IDB_TYPE_ERR;
298 return IDBTransaction::READ_ONLY;
301 const AtomicString& IDBTransaction::modeToString(IDBTransaction::Mode mode, ExceptionCode& ec)
304 case IDBTransaction::READ_ONLY:
305 return IDBTransaction::modeReadOnly();
308 case IDBTransaction::READ_WRITE:
309 return IDBTransaction::modeReadWrite();
312 case IDBTransaction::VERSION_CHANGE:
313 return IDBTransaction::modeVersionChange();
317 ec = IDBDatabaseException::IDB_TYPE_ERR;
318 return IDBTransaction::modeReadOnly();
322 const AtomicString& IDBTransaction::interfaceName() const
324 return eventNames().interfaceForIDBTransaction;
327 ScriptExecutionContext* IDBTransaction::scriptExecutionContext() const
329 return ActiveDOMObject::scriptExecutionContext();
332 bool IDBTransaction::dispatchEvent(PassRefPtr<Event> event)
334 IDB_TRACE("IDBTransaction::dispatchEvent");
335 ASSERT(!m_transactionFinished);
336 ASSERT(scriptExecutionContext());
337 ASSERT(event->target() == this);
338 m_transactionFinished = true;
340 // Break reference cycles.
341 for (IDBObjectStoreMap::iterator it = m_objectStoreMap.begin(); it != m_objectStoreMap.end(); ++it)
342 it->second->transactionFinished();
343 m_objectStoreMap.clear();
345 Vector<RefPtr<EventTarget> > targets;
346 targets.append(this);
347 targets.append(db());
349 // FIXME: When we allow custom event dispatching, this will probably need to change.
350 ASSERT(event->type() == eventNames().completeEvent || event->type() == eventNames().abortEvent);
351 return IDBEventDispatcher::dispatch(event.get(), targets);
354 bool IDBTransaction::canSuspend() const
356 // FIXME: Technically we can suspend before the first request is schedule
357 // and after the complete/abort event is enqueued.
358 return m_transactionFinished;
361 void IDBTransaction::stop()
363 ActiveDOMObject::stop();
364 m_contextStopped = true;
369 void IDBTransaction::enqueueEvent(PassRefPtr<Event> event)
371 ASSERT_WITH_MESSAGE(!m_transactionFinished, "A finished transaction tried to enqueue an event of type %s.", event->type().string().utf8().data());
372 if (m_contextStopped || !scriptExecutionContext())
375 EventQueue* eventQueue = scriptExecutionContext()->eventQueue();
376 event->setTarget(this);
377 eventQueue->enqueueEvent(event);
380 EventTargetData* IDBTransaction::eventTargetData()
382 return &m_eventTargetData;
385 EventTargetData* IDBTransaction::ensureEventTargetData()
387 return &m_eventTargetData;
392 #endif // ENABLE(INDEXED_DATABASE)