Upstream version 10.39.225.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/Database.h"
36 #include "modules/webdatabase/SQLTransactionBackend.h"
37
38 namespace blink {
39
40 static String getDatabaseIdentifier(SQLTransactionBackend* transaction)
41 {
42     Database* 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::trace(Visitor* visitor)
53 {
54 #if ENABLE(OILPAN)
55     visitor->trace(m_coordinationInfoMap);
56 #endif
57 }
58
59 void SQLTransactionCoordinator::processPendingTransactions(CoordinationInfo& info)
60 {
61     if (info.activeWriteTransaction || info.pendingTransactions.isEmpty())
62         return;
63
64     RefPtrWillBeRawPtr<SQLTransactionBackend> firstPendingTransaction = info.pendingTransactions.first();
65     if (firstPendingTransaction->isReadOnly()) {
66         do {
67             firstPendingTransaction = info.pendingTransactions.takeFirst();
68             info.activeReadTransactions.add(firstPendingTransaction);
69             firstPendingTransaction->lockAcquired();
70         } while (!info.pendingTransactions.isEmpty() && info.pendingTransactions.first()->isReadOnly());
71     } else if (info.activeReadTransactions.isEmpty()) {
72         info.pendingTransactions.removeFirst();
73         info.activeWriteTransaction = firstPendingTransaction;
74         firstPendingTransaction->lockAcquired();
75     }
76 }
77
78 void SQLTransactionCoordinator::acquireLock(SQLTransactionBackend* transaction)
79 {
80     ASSERT(!m_isShuttingDown);
81
82     String dbIdentifier = getDatabaseIdentifier(transaction);
83
84     CoordinationInfoHeapMap::iterator coordinationInfoIterator = m_coordinationInfoMap.find(dbIdentifier);
85     if (coordinationInfoIterator == m_coordinationInfoMap.end()) {
86         // No pending transactions for this DB
87         CoordinationInfo& info = m_coordinationInfoMap.add(dbIdentifier, CoordinationInfo()).storedValue->value;
88         info.pendingTransactions.append(transaction);
89         processPendingTransactions(info);
90     } else {
91         CoordinationInfo& info = coordinationInfoIterator->value;
92         info.pendingTransactions.append(transaction);
93         processPendingTransactions(info);
94     }
95
96 }
97
98 void SQLTransactionCoordinator::releaseLock(SQLTransactionBackend* transaction)
99 {
100     if (m_isShuttingDown)
101         return;
102
103     String dbIdentifier = getDatabaseIdentifier(transaction);
104
105     CoordinationInfoHeapMap::iterator coordinationInfoIterator = m_coordinationInfoMap.find(dbIdentifier);
106     ASSERT_WITH_SECURITY_IMPLICATION(coordinationInfoIterator != m_coordinationInfoMap.end());
107     CoordinationInfo& info = coordinationInfoIterator->value;
108
109     if (transaction->isReadOnly()) {
110         ASSERT(info.activeReadTransactions.contains(transaction));
111         info.activeReadTransactions.remove(transaction);
112     } else {
113         ASSERT(info.activeWriteTransaction == transaction);
114         info.activeWriteTransaction = nullptr;
115     }
116
117     processPendingTransactions(info);
118 }
119
120 void SQLTransactionCoordinator::shutdown()
121 {
122     // Prevent releaseLock() from accessing / changing the coordinationInfo
123     // while we're shutting down.
124     m_isShuttingDown = true;
125
126     // Notify all transactions in progress that the database thread is shutting down
127     for (CoordinationInfoHeapMap::iterator coordinationInfoIterator = m_coordinationInfoMap.begin();
128          coordinationInfoIterator != m_coordinationInfoMap.end(); ++coordinationInfoIterator) {
129         CoordinationInfo& info = coordinationInfoIterator->value;
130
131         // Clean up transactions that have reached "lockAcquired":
132         // Transaction phase 4 cleanup. See comment on "What happens if a
133         // transaction is interrupted?" at the top of SQLTransactionBackend.cpp.
134         if (info.activeWriteTransaction)
135             info.activeWriteTransaction->notifyDatabaseThreadIsShuttingDown();
136         for (WillBeHeapHashSet<RefPtrWillBeMember<SQLTransactionBackend> >::iterator activeReadTransactionsIterator =
137                      info.activeReadTransactions.begin();
138              activeReadTransactionsIterator != info.activeReadTransactions.end();
139              ++activeReadTransactionsIterator) {
140             (*activeReadTransactionsIterator)->notifyDatabaseThreadIsShuttingDown();
141         }
142
143         // Clean up transactions that have NOT reached "lockAcquired":
144         // Transaction phase 3 cleanup. See comment on "What happens if a
145         // transaction is interrupted?" at the top of SQLTransactionBackend.cpp.
146         while (!info.pendingTransactions.isEmpty()) {
147             RefPtrWillBeRawPtr<SQLTransactionBackend> transaction = info.pendingTransactions.takeFirst();
148             transaction->notifyDatabaseThreadIsShuttingDown();
149         }
150     }
151
152     // Clean up all pending transactions for all databases
153     m_coordinationInfoMap.clear();
154 }
155
156 } // namespace blink