1e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block/*
2e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block * Copyright (C) 2010 Google Inc. All rights reserved.
3e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block *
4e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block * Redistribution and use in source and binary forms, with or without
5e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block * modification, are permitted provided that the following conditions
6e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block * are met:
7e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block *
8e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block * 1.  Redistributions of source code must retain the above copyright
9e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block *     notice, this list of conditions and the following disclaimer.
10e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block * 2.  Redistributions in binary form must reproduce the above copyright
11e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block *     notice, this list of conditions and the following disclaimer in the
12e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block *     documentation and/or other materials provided with the distribution.
13e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block *
14e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
15e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
18e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block */
25e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block
26e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block#include "config.h"
27e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block#include "IDBTransactionCoordinator.h"
28e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block
29e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block#if ENABLE(INDEXED_DATABASE)
30e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block
31e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block#include "IDBDatabaseBackendImpl.h"
32e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block#include "IDBObjectStoreBackendInterface.h"
33e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block#include "IDBTransactionBackendImpl.h"
34e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block#include "ScriptExecutionContext.h"
35e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block
36e8b154fd68f9b33be40a3590e58347f353835f5cSteve Blocknamespace WebCore {
37e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block
3828040489d744e0c5d475a88663056c9040ed5320Teng-Hui ZhuPassRefPtr<IDBTransactionCoordinator> IDBTransactionCoordinator::create()
3928040489d744e0c5d475a88663056c9040ed5320Teng-Hui Zhu{
4028040489d744e0c5d475a88663056c9040ed5320Teng-Hui Zhu    return adoptRef(new IDBTransactionCoordinator());
4128040489d744e0c5d475a88663056c9040ed5320Teng-Hui Zhu}
4228040489d744e0c5d475a88663056c9040ed5320Teng-Hui Zhu
43e8b154fd68f9b33be40a3590e58347f353835f5cSteve BlockIDBTransactionCoordinator::IDBTransactionCoordinator()
44e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block{
45e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block}
46e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block
47e8b154fd68f9b33be40a3590e58347f353835f5cSteve BlockIDBTransactionCoordinator::~IDBTransactionCoordinator()
48e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block{
49e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block}
50e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block
5128040489d744e0c5d475a88663056c9040ed5320Teng-Hui Zhuvoid IDBTransactionCoordinator::didCreateTransaction(IDBTransactionBackendImpl* transaction)
52e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block{
5328040489d744e0c5d475a88663056c9040ed5320Teng-Hui Zhu    ASSERT(!m_transactions.contains(transaction));
5428040489d744e0c5d475a88663056c9040ed5320Teng-Hui Zhu    m_transactions.add(transaction, transaction);
55e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block}
56e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block
57bec39347bb3bb5bf1187ccaf471d26247f28b585Kristian Monsenvoid IDBTransactionCoordinator::didStartTransaction(IDBTransactionBackendImpl* transaction)
58e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block{
5928040489d744e0c5d475a88663056c9040ed5320Teng-Hui Zhu    ASSERT(m_transactions.contains(transaction));
60bec39347bb3bb5bf1187ccaf471d26247f28b585Kristian Monsen
61bec39347bb3bb5bf1187ccaf471d26247f28b585Kristian Monsen    m_startedTransactions.add(transaction);
62bec39347bb3bb5bf1187ccaf471d26247f28b585Kristian Monsen    processStartedTransactions();
63bec39347bb3bb5bf1187ccaf471d26247f28b585Kristian Monsen}
64bec39347bb3bb5bf1187ccaf471d26247f28b585Kristian Monsen
65bec39347bb3bb5bf1187ccaf471d26247f28b585Kristian Monsenvoid IDBTransactionCoordinator::didFinishTransaction(IDBTransactionBackendImpl* transaction)
66bec39347bb3bb5bf1187ccaf471d26247f28b585Kristian Monsen{
6728040489d744e0c5d475a88663056c9040ed5320Teng-Hui Zhu    ASSERT(m_transactions.contains(transaction));
68bec39347bb3bb5bf1187ccaf471d26247f28b585Kristian Monsen
69bec39347bb3bb5bf1187ccaf471d26247f28b585Kristian Monsen    if (m_startedTransactions.contains(transaction)) {
70bec39347bb3bb5bf1187ccaf471d26247f28b585Kristian Monsen        ASSERT(!m_runningTransactions.contains(transaction));
71bec39347bb3bb5bf1187ccaf471d26247f28b585Kristian Monsen        m_startedTransactions.remove(transaction);
72bec39347bb3bb5bf1187ccaf471d26247f28b585Kristian Monsen    } else if (m_runningTransactions.contains(transaction))
73bec39347bb3bb5bf1187ccaf471d26247f28b585Kristian Monsen        m_runningTransactions.remove(transaction);
74bec39347bb3bb5bf1187ccaf471d26247f28b585Kristian Monsen
7528040489d744e0c5d475a88663056c9040ed5320Teng-Hui Zhu    m_transactions.remove(transaction);
76bec39347bb3bb5bf1187ccaf471d26247f28b585Kristian Monsen
77bec39347bb3bb5bf1187ccaf471d26247f28b585Kristian Monsen    processStartedTransactions();
78bec39347bb3bb5bf1187ccaf471d26247f28b585Kristian Monsen}
79bec39347bb3bb5bf1187ccaf471d26247f28b585Kristian Monsen
8028040489d744e0c5d475a88663056c9040ed5320Teng-Hui Zhu#ifndef NDEBUG
8128040489d744e0c5d475a88663056c9040ed5320Teng-Hui Zhu// Verifies internal consistiency while returning whether anything is found.
8228040489d744e0c5d475a88663056c9040ed5320Teng-Hui Zhubool IDBTransactionCoordinator::isActive(IDBTransactionBackendImpl* transaction)
8328040489d744e0c5d475a88663056c9040ed5320Teng-Hui Zhu{
8428040489d744e0c5d475a88663056c9040ed5320Teng-Hui Zhu    bool found = false;
8528040489d744e0c5d475a88663056c9040ed5320Teng-Hui Zhu    if (m_startedTransactions.contains(transaction))
8628040489d744e0c5d475a88663056c9040ed5320Teng-Hui Zhu        found = true;
8728040489d744e0c5d475a88663056c9040ed5320Teng-Hui Zhu    if (m_runningTransactions.contains(transaction)) {
8828040489d744e0c5d475a88663056c9040ed5320Teng-Hui Zhu        ASSERT(!found);
8928040489d744e0c5d475a88663056c9040ed5320Teng-Hui Zhu        found = true;
9028040489d744e0c5d475a88663056c9040ed5320Teng-Hui Zhu    }
9128040489d744e0c5d475a88663056c9040ed5320Teng-Hui Zhu    ASSERT(found == m_transactions.contains(transaction));
9228040489d744e0c5d475a88663056c9040ed5320Teng-Hui Zhu    return found;
9328040489d744e0c5d475a88663056c9040ed5320Teng-Hui Zhu}
9428040489d744e0c5d475a88663056c9040ed5320Teng-Hui Zhu#endif
9528040489d744e0c5d475a88663056c9040ed5320Teng-Hui Zhu
96bec39347bb3bb5bf1187ccaf471d26247f28b585Kristian Monsenvoid IDBTransactionCoordinator::processStartedTransactions()
97bec39347bb3bb5bf1187ccaf471d26247f28b585Kristian Monsen{
9828040489d744e0c5d475a88663056c9040ed5320Teng-Hui Zhu    // FIXME: For now, we only allow one transaction to run at a time.
9928040489d744e0c5d475a88663056c9040ed5320Teng-Hui Zhu    if (!m_runningTransactions.isEmpty())
10028040489d744e0c5d475a88663056c9040ed5320Teng-Hui Zhu        return;
10128040489d744e0c5d475a88663056c9040ed5320Teng-Hui Zhu
10228040489d744e0c5d475a88663056c9040ed5320Teng-Hui Zhu    if (m_startedTransactions.isEmpty())
103bec39347bb3bb5bf1187ccaf471d26247f28b585Kristian Monsen        return;
104bec39347bb3bb5bf1187ccaf471d26247f28b585Kristian Monsen
105bec39347bb3bb5bf1187ccaf471d26247f28b585Kristian Monsen    IDBTransactionBackendImpl* transaction = *m_startedTransactions.begin();
106bec39347bb3bb5bf1187ccaf471d26247f28b585Kristian Monsen    m_startedTransactions.remove(transaction);
107bec39347bb3bb5bf1187ccaf471d26247f28b585Kristian Monsen    m_runningTransactions.add(transaction);
108bec39347bb3bb5bf1187ccaf471d26247f28b585Kristian Monsen    transaction->run();
109e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block}
110e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block
111e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block};
112e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block
113e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block#endif // ENABLE(INDEXED_DATABASE)
114