Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / modules / webdatabase / SQLTransactionCoordinator.cpp
1 /*
2  * Copyright (C) 2009 Google Inc. All rights reserved.
3  * Copyright (C) 2013 Apple Inc. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  *     * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *     * Redistributions in binary form must reproduce the above
12  * copyright notice, this list of conditions and the following disclaimer
13  * in the documentation and/or other materials provided with the
14  * distribution.
15  *     * Neither the name of Google Inc. nor the names of its
16  * contributors may be used to endorse or promote products derived from
17  * this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31
32 #include "config.h"
33 #include "modules/webdatabase/SQLTransactionCoordinator.h"
34
35 #include "modules/webdatabase/DatabaseBackend.h"
36 #include "modules/webdatabase/SQLTransactionBackend.h"
37
38 namespace WebCore {
39
40 static String getDatabaseIdentifier(SQLTransactionBackend* transaction)
41 {
42     DatabaseBackend* database = transaction->database();
43     ASSERT(database);
44     return database->stringIdentifier();
45 }
46
47 SQLTransactionCoordinator::SQLTransactionCoordinator()
48     : m_isShuttingDown(false)
49 {
50 }
51
52 void SQLTransactionCoordinator::processPendingTransactions(CoordinationInfo& info)
53 {
54     if (info.activeWriteTransaction || info.pendingTransactions.isEmpty())
55         return;
56
57     RefPtr<SQLTransactionBackend> firstPendingTransaction = info.pendingTransactions.first();
58     if (firstPendingTransaction->isReadOnly()) {
59         do {
60             firstPendingTransaction = info.pendingTransactions.takeFirst();
61             info.activeReadTransactions.add(firstPendingTransaction);
62             firstPendingTransaction->lockAcquired();
63         } while (!info.pendingTransactions.isEmpty() && info.pendingTransactions.first()->isReadOnly());
64     } else if (info.activeReadTransactions.isEmpty()) {
65         info.pendingTransactions.removeFirst();
66         info.activeWriteTransaction = firstPendingTransaction;
67         firstPendingTransaction->lockAcquired();
68     }
69 }
70
71 void SQLTransactionCoordinator::acquireLock(SQLTransactionBackend* transaction)
72 {
73     ASSERT(!m_isShuttingDown);
74
75     String dbIdentifier = getDatabaseIdentifier(transaction);
76
77     CoordinationInfoMap::iterator coordinationInfoIterator = m_coordinationInfoMap.find(dbIdentifier);
78     if (coordinationInfoIterator == m_coordinationInfoMap.end()) {
79         // No pending transactions for this DB
80         CoordinationInfo& info = m_coordinationInfoMap.add(dbIdentifier, CoordinationInfo()).storedValue->value;
81         info.pendingTransactions.append(transaction);
82         processPendingTransactions(info);
83     } else {
84         CoordinationInfo& info = coordinationInfoIterator->value;
85         info.pendingTransactions.append(transaction);
86         processPendingTransactions(info);
87     }
88
89 }
90
91 void SQLTransactionCoordinator::releaseLock(SQLTransactionBackend* transaction)
92 {
93     if (m_isShuttingDown)
94         return;
95
96     String dbIdentifier = getDatabaseIdentifier(transaction);
97
98     CoordinationInfoMap::iterator coordinationInfoIterator = m_coordinationInfoMap.find(dbIdentifier);
99     ASSERT_WITH_SECURITY_IMPLICATION(coordinationInfoIterator != m_coordinationInfoMap.end());
100     CoordinationInfo& info = coordinationInfoIterator->value;
101
102     if (transaction->isReadOnly()) {
103         ASSERT(info.activeReadTransactions.contains(transaction));
104         info.activeReadTransactions.remove(transaction);
105     } else {
106         ASSERT(info.activeWriteTransaction == transaction);
107         info.activeWriteTransaction = 0;
108     }
109
110     processPendingTransactions(info);
111 }
112
113 void SQLTransactionCoordinator::shutdown()
114 {
115     // Prevent releaseLock() from accessing / changing the coordinationInfo
116     // while we're shutting down.
117     m_isShuttingDown = true;
118
119     // Notify all transactions in progress that the database thread is shutting down
120     for (CoordinationInfoMap::iterator coordinationInfoIterator = m_coordinationInfoMap.begin();
121          coordinationInfoIterator != m_coordinationInfoMap.end(); ++coordinationInfoIterator) {
122         CoordinationInfo& info = coordinationInfoIterator->value;
123
124         // Clean up transactions that have reached "lockAcquired":
125         // Transaction phase 4 cleanup. See comment on "What happens if a
126         // transaction is interrupted?" at the top of SQLTransactionBackend.cpp.
127         if (info.activeWriteTransaction)
128             info.activeWriteTransaction->notifyDatabaseThreadIsShuttingDown();
129         for (HashSet<RefPtr<SQLTransactionBackend> >::iterator activeReadTransactionsIterator =
130                      info.activeReadTransactions.begin();
131              activeReadTransactionsIterator != info.activeReadTransactions.end();
132              ++activeReadTransactionsIterator) {
133             (*activeReadTransactionsIterator)->notifyDatabaseThreadIsShuttingDown();
134         }
135
136         // Clean up transactions that have NOT reached "lockAcquired":
137         // Transaction phase 3 cleanup. See comment on "What happens if a
138         // transaction is interrupted?" at the top of SQLTransactionBackend.cpp.
139         while (!info.pendingTransactions.isEmpty()) {
140             RefPtr<SQLTransactionBackend> transaction = info.pendingTransactions.first();
141             transaction->notifyDatabaseThreadIsShuttingDown();
142         }
143     }
144
145     // Clean up all pending transactions for all databases
146     m_coordinationInfoMap.clear();
147 }
148
149 } // namespace WebCore