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)
32 #include "EventException.h"
33 #include "EventQueue.h"
34 #include "IDBDatabase.h"
35 #include "IDBDatabaseException.h"
36 #include "IDBEventDispatcher.h"
38 #include "IDBObjectStore.h"
39 #include "IDBObjectStoreBackendInterface.h"
40 #include "IDBPendingTransactionMonitor.h"
44 PassRefPtr<IDBTransaction> IDBTransaction::create(ScriptExecutionContext* context, PassRefPtr<IDBTransactionBackendInterface> backend, IDBDatabase* db)
46 return adoptRef(new IDBTransaction(context, backend, db));
49 IDBTransaction::IDBTransaction(ScriptExecutionContext* context, PassRefPtr<IDBTransactionBackendInterface> backend, IDBDatabase* db)
50 : ActiveDOMObject(context, this)
53 , m_mode(m_backend->mode())
57 IDBPendingTransactionMonitor::addPendingTransaction(m_backend.get());
60 IDBTransaction::~IDBTransaction()
64 IDBTransactionBackendInterface* IDBTransaction::backend() const
66 return m_backend.get();
69 bool IDBTransaction::finished() const
74 unsigned short IDBTransaction::mode() const
79 IDBDatabase* IDBTransaction::db() const
81 return m_database.get();
84 PassRefPtr<IDBObjectStore> IDBTransaction::objectStore(const String& name, ExceptionCode& ec)
87 ec = IDBDatabaseException::NOT_ALLOWED_ERR;
90 RefPtr<IDBObjectStoreBackendInterface> objectStoreBackend = m_backend->objectStore(name, ec);
91 if (!objectStoreBackend) {
95 RefPtr<IDBObjectStore> objectStore = IDBObjectStore::create(objectStoreBackend, this);
96 return objectStore.release();
99 void IDBTransaction::abort()
101 RefPtr<IDBTransaction> selfRef = this;
106 void IDBTransaction::registerRequest(IDBRequest* request)
108 m_childRequests.add(request);
111 void IDBTransaction::unregisterRequest(IDBRequest* request)
113 // If we aborted the request, it will already have been removed.
114 m_childRequests.remove(request);
117 void IDBTransaction::onAbort()
119 while (!m_childRequests.isEmpty()) {
120 IDBRequest* request = *m_childRequests.begin();
121 m_childRequests.remove(request);
125 enqueueEvent(Event::create(eventNames().abortEvent, true, false));
128 void IDBTransaction::onComplete()
130 enqueueEvent(Event::create(eventNames().completeEvent, false, false));
133 bool IDBTransaction::hasPendingActivity() const
135 // FIXME: In an ideal world, we should return true as long as anyone has a or can
136 // get a handle to us or any child request object and any of those have
137 // event listeners. This is in order to handle user generated events properly.
138 return !m_finished || ActiveDOMObject::hasPendingActivity();
141 ScriptExecutionContext* IDBTransaction::scriptExecutionContext() const
143 return ActiveDOMObject::scriptExecutionContext();
146 bool IDBTransaction::dispatchEvent(PassRefPtr<Event> event)
149 ASSERT(scriptExecutionContext());
150 ASSERT(event->target() == this);
154 Vector<RefPtr<EventTarget> > targets;
155 targets.append(this);
156 targets.append(db());
158 // FIXME: When we allow custom event dispatching, this will probably need to change.
159 ASSERT(event->type() == eventNames().completeEvent || event->type() == eventNames().abortEvent);
160 return IDBEventDispatcher::dispatch(event.get(), targets);
163 bool IDBTransaction::canSuspend() const
165 // FIXME: Technically we can suspend before the first request is schedule
166 // and after the complete/abort event is enqueued.
170 void IDBTransaction::contextDestroyed()
172 ActiveDOMObject::contextDestroyed();
174 // Must happen in contextDestroyed since it can result in ActiveDOMObjects being destructed
175 // (and contextDestroyed is the only one resilient against this).
176 RefPtr<IDBTransaction> selfRef = this;
183 void IDBTransaction::enqueueEvent(PassRefPtr<Event> event)
185 ASSERT_WITH_MESSAGE(!m_finished, "A finished transaction tried to enqueue an event of type %s.", event->type().string().utf8().data());
186 if (!scriptExecutionContext())
189 ASSERT(scriptExecutionContext()->isDocument());
190 EventQueue* eventQueue = static_cast<Document*>(scriptExecutionContext())->eventQueue();
191 event->setTarget(this);
192 eventQueue->enqueueEvent(event);
195 EventTargetData* IDBTransaction::eventTargetData()
197 return &m_eventTargetData;
200 EventTargetData* IDBTransaction::ensureEventTargetData()
202 return &m_eventTargetData;
207 #endif // ENABLE(INDEXED_DATABASE)