Unreviewed test gardening.
[WebKit-https.git] / Source / WebCore / Modules / indexeddb / legacy / IDBTransactionCoordinator.cpp
1 /*
2  * Copyright (C) 2010 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
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.
13  *
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.
24  */
25
26 #include "config.h"
27 #include "IDBTransactionCoordinator.h"
28
29 #if ENABLE(INDEXED_DATABASE)
30
31 #include "IDBDatabaseCallbacks.h"
32 #include "IDBTransactionBackend.h"
33
34 namespace WebCore {
35
36 IDBTransactionCoordinator::IDBTransactionCoordinator()
37 {
38 }
39
40 IDBTransactionCoordinator::~IDBTransactionCoordinator()
41 {
42 }
43
44 void IDBTransactionCoordinator::didCreateTransaction(IDBTransactionBackend* transaction)
45 {
46     ASSERT(!m_transactions.contains(transaction));
47     m_transactions.add(transaction, transaction);
48 }
49
50 void IDBTransactionCoordinator::didStartTransaction(IDBTransactionBackend* transaction)
51 {
52     ASSERT(m_transactions.contains(transaction));
53
54     m_queuedTransactions.add(transaction);
55     processStartedTransactions();
56 }
57
58 void IDBTransactionCoordinator::didFinishTransaction(IDBTransactionBackend* transaction)
59 {
60     ASSERT(m_transactions.contains(transaction));
61
62     if (m_queuedTransactions.contains(transaction)) {
63         ASSERT(!m_startedTransactions.contains(transaction));
64         m_queuedTransactions.remove(transaction);
65     } else if (m_startedTransactions.contains(transaction))
66         m_startedTransactions.remove(transaction);
67
68     m_transactions.remove(transaction);
69
70     processStartedTransactions();
71 }
72
73 #ifndef NDEBUG
74 // Verifies internal consistiency while returning whether anything is found.
75 bool IDBTransactionCoordinator::isActive(IDBTransactionBackend* transaction)
76 {
77     bool found = false;
78     if (m_queuedTransactions.contains(transaction))
79         found = true;
80     if (m_startedTransactions.contains(transaction)) {
81         ASSERT(!found);
82         found = true;
83     }
84     ASSERT(found == m_transactions.contains(transaction));
85     return found;
86 }
87 #endif
88
89 void IDBTransactionCoordinator::processStartedTransactions()
90 {
91     if (m_queuedTransactions.isEmpty())
92         return;
93
94     ASSERT(m_startedTransactions.isEmpty() || (*m_startedTransactions.begin())->mode() != IndexedDB::TransactionMode::VersionChange);
95
96     ListHashSet<IDBTransactionBackend*>::const_iterator it = m_queuedTransactions.begin();
97     while (it != m_queuedTransactions.end()) {
98         IDBTransactionBackend* transaction = *it;
99         ++it;
100         if (canRunTransaction(transaction)) {
101             m_queuedTransactions.remove(transaction);
102             m_startedTransactions.add(transaction);
103             transaction->run();
104         }
105     }
106 }
107
108 bool IDBTransactionCoordinator::canRunTransaction(IDBTransactionBackend* transaction)
109 {
110     ASSERT(m_queuedTransactions.contains(transaction));
111
112     UNUSED_PARAM(transaction);
113     return !m_startedTransactions.size();
114 }
115
116 };
117
118 #endif // ENABLE(INDEXED_DATABASE)