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)
89 , m_contextStopped(false)
93 if (mode == VERSION_CHANGE) {
94 // Not active until the callback.
96 // Implicitly used by the version change itself.
100 // We pass a reference of this object before it can be adopted.
101 relaxAdoptionRequirement();
103 IDBPendingTransactionMonitor::addNewTransaction(this);
104 m_database->transactionCreated(this);
107 IDBTransaction::~IDBTransaction()
109 ASSERT(m_state == Finished);
112 IDBTransactionBackendInterface* IDBTransaction::backend() const
114 return m_backend.get();
117 const String& IDBTransaction::mode() const
119 ExceptionCode ec = 0;
120 const AtomicString& mode = modeToString(m_mode, ec);
125 void IDBTransaction::setError(PassRefPtr<DOMError> error)
127 ASSERT(m_state != Finished);
130 // The first error to be set is the true cause of the
131 // transaction abort.
136 PassRefPtr<IDBObjectStore> IDBTransaction::objectStore(const String& name, ExceptionCode& ec)
138 if (m_state == Finished) {
139 ec = IDBDatabaseException::IDB_INVALID_STATE_ERR;
143 IDBObjectStoreMap::iterator it = m_objectStoreMap.find(name);
144 if (it != m_objectStoreMap.end())
147 RefPtr<IDBObjectStoreBackendInterface> objectStoreBackend = m_backend->objectStore(name, ec);
148 ASSERT(!objectStoreBackend != !ec); // If we didn't get a store, we should have gotten an exception code. And vice versa.
152 const IDBDatabaseMetadata& metadata = m_database->metadata();
153 IDBDatabaseMetadata::ObjectStoreMap::const_iterator mdit = metadata.objectStores.find(name);
154 ASSERT(mdit != metadata.objectStores.end());
156 RefPtr<IDBObjectStore> objectStore = IDBObjectStore::create(mdit->second, objectStoreBackend, this);
157 objectStoreCreated(name, objectStore);
158 return objectStore.release();
161 void IDBTransaction::objectStoreCreated(const String& name, PassRefPtr<IDBObjectStore> prpObjectStore)
163 ASSERT(m_state != Finished);
164 RefPtr<IDBObjectStore> objectStore = prpObjectStore;
165 m_objectStoreMap.set(name, objectStore);
166 if (isVersionChange())
167 m_objectStoreCleanupMap.set(objectStore, objectStore->metadata());
170 void IDBTransaction::objectStoreDeleted(const String& name)
172 ASSERT(m_state != Finished);
173 ASSERT(isVersionChange());
174 IDBObjectStoreMap::iterator it = m_objectStoreMap.find(name);
175 if (it != m_objectStoreMap.end()) {
176 RefPtr<IDBObjectStore> objectStore = it->second;
177 m_objectStoreMap.remove(name);
178 objectStore->markDeleted();
179 m_objectStoreCleanupMap.set(objectStore, objectStore->metadata());
183 void IDBTransaction::setActive(bool active)
185 ASSERT(m_state != Finished);
186 if (m_state == Finishing)
188 ASSERT(m_state == Unused || m_state == Used);
189 ASSERT(active != m_active);
192 if (!active && m_state == Unused)
196 void IDBTransaction::abort(ExceptionCode& ec)
198 if (m_state == Finishing || m_state == Finished) {
199 ec = IDBDatabaseException::IDB_INVALID_STATE_ERR;
206 while (!m_requestList.isEmpty()) {
207 IDBRequest* request = *m_requestList.begin();
208 m_requestList.remove(request);
212 RefPtr<IDBTransaction> selfRef = this;
217 IDBTransaction::OpenCursorNotifier::OpenCursorNotifier(PassRefPtr<IDBTransaction> transaction, IDBCursor* cursor)
218 : m_transaction(transaction),
221 m_transaction->registerOpenCursor(m_cursor);
224 IDBTransaction::OpenCursorNotifier::~OpenCursorNotifier()
226 m_transaction->unregisterOpenCursor(m_cursor);
229 void IDBTransaction::registerOpenCursor(IDBCursor* cursor)
231 m_openCursors.add(cursor);
234 void IDBTransaction::unregisterOpenCursor(IDBCursor* cursor)
236 m_openCursors.remove(cursor);
239 void IDBTransaction::closeOpenCursors()
241 HashSet<IDBCursor*> cursors;
242 cursors.swap(m_openCursors);
243 for (HashSet<IDBCursor*>::iterator i = cursors.begin(); i != cursors.end(); ++i)
247 void IDBTransaction::registerRequest(IDBRequest* request)
250 ASSERT(m_state == Unused || m_state == Used);
252 m_requestList.add(request);
256 void IDBTransaction::unregisterRequest(IDBRequest* request)
259 // If we aborted the request, it will already have been removed.
260 m_requestList.remove(request);
263 void IDBTransaction::onAbort()
265 ASSERT(m_state != Finished);
267 if (m_state != Finishing) {
268 // FIXME: Propagate true cause from back end (e.g. QuotaError, UnknownError, etc.)
269 setError(DOMError::create(IDBDatabaseException::getErrorName(IDBDatabaseException::UNKNOWN_ERR)));
271 // Abort was not triggered by front-end, so outstanding requests must
273 while (!m_requestList.isEmpty()) {
274 IDBRequest* request = *m_requestList.begin();
275 m_requestList.remove(request);
281 if (isVersionChange()) {
282 for (IDBObjectStoreMetadataMap::iterator it = m_objectStoreCleanupMap.begin(); it != m_objectStoreCleanupMap.end(); ++it)
283 it->first->setMetadata(it->second);
285 m_objectStoreCleanupMap.clear();
287 m_database->transactionFinished(this);
289 if (m_contextStopped || !scriptExecutionContext())
292 enqueueEvent(Event::create(eventNames().abortEvent, true, false));
295 void IDBTransaction::onComplete()
297 ASSERT(m_state != Finished);
299 m_objectStoreCleanupMap.clear();
301 m_database->transactionFinished(this);
303 if (m_contextStopped || !scriptExecutionContext())
306 enqueueEvent(Event::create(eventNames().completeEvent, false, false));
309 bool IDBTransaction::hasPendingActivity() const
311 // FIXME: In an ideal world, we should return true as long as anyone has a or can
312 // get a handle to us or any child request object and any of those have
313 // event listeners. This is in order to handle user generated events properly.
314 return m_state != Finished || ActiveDOMObject::hasPendingActivity();
317 IDBTransaction::Mode IDBTransaction::stringToMode(const String& modeString, ExceptionCode& ec)
319 if (modeString.isNull()
320 || modeString == IDBTransaction::modeReadOnly())
321 return IDBTransaction::READ_ONLY;
322 if (modeString == IDBTransaction::modeReadWrite())
323 return IDBTransaction::READ_WRITE;
324 ec = NATIVE_TYPE_ERR;
325 return IDBTransaction::READ_ONLY;
328 const AtomicString& IDBTransaction::modeToString(IDBTransaction::Mode mode, ExceptionCode& ec)
331 case IDBTransaction::READ_ONLY:
332 return IDBTransaction::modeReadOnly();
335 case IDBTransaction::READ_WRITE:
336 return IDBTransaction::modeReadWrite();
339 case IDBTransaction::VERSION_CHANGE:
340 return IDBTransaction::modeVersionChange();
344 ec = NATIVE_TYPE_ERR;
345 return IDBTransaction::modeReadOnly();
349 const AtomicString& IDBTransaction::interfaceName() const
351 return eventNames().interfaceForIDBTransaction;
354 ScriptExecutionContext* IDBTransaction::scriptExecutionContext() const
356 return ActiveDOMObject::scriptExecutionContext();
359 bool IDBTransaction::dispatchEvent(PassRefPtr<Event> event)
361 IDB_TRACE("IDBTransaction::dispatchEvent");
362 ASSERT(m_state != Finished);
363 ASSERT(scriptExecutionContext());
364 ASSERT(event->target() == this);
367 // Break reference cycles.
368 for (IDBObjectStoreMap::iterator it = m_objectStoreMap.begin(); it != m_objectStoreMap.end(); ++it)
369 it->second->transactionFinished();
370 m_objectStoreMap.clear();
372 Vector<RefPtr<EventTarget> > targets;
373 targets.append(this);
374 targets.append(db());
376 // FIXME: When we allow custom event dispatching, this will probably need to change.
377 ASSERT(event->type() == eventNames().completeEvent || event->type() == eventNames().abortEvent);
378 return IDBEventDispatcher::dispatch(event.get(), targets);
381 bool IDBTransaction::canSuspend() const
383 // FIXME: Technically we can suspend before the first request is schedule
384 // and after the complete/abort event is enqueued.
385 return m_state == Finished;
388 void IDBTransaction::stop()
390 ActiveDOMObject::stop();
391 m_contextStopped = true;
393 ExceptionCode unused;
397 void IDBTransaction::enqueueEvent(PassRefPtr<Event> event)
399 ASSERT_WITH_MESSAGE(m_state != Finished, "A finished transaction tried to enqueue an event of type %s.", event->type().string().utf8().data());
400 if (m_contextStopped || !scriptExecutionContext())
403 EventQueue* eventQueue = scriptExecutionContext()->eventQueue();
404 event->setTarget(this);
405 eventQueue->enqueueEvent(event);
408 EventTargetData* IDBTransaction::eventTargetData()
410 return &m_eventTargetData;
413 EventTargetData* IDBTransaction::ensureEventTargetData()
415 return &m_eventTargetData;
420 #endif // ENABLE(INDEXED_DATABASE)