1 /*
2 * Copyright (C) 2007, 2008 Apple 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 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
14 * its contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28 #include "config.h"
29 #include "DatabaseTask.h"
30
31 #if ENABLE(DATABASE)
32
33 #include "Database.h"
34 #include "Logging.h"
35
36 namespace WebCore {
37
DatabaseTask(Database * database)38 DatabaseTask::DatabaseTask(Database* database)
39 : m_database(database)
40 , m_complete(false)
41 {
42 }
43
~DatabaseTask()44 DatabaseTask::~DatabaseTask()
45 {
46 }
47
performTask()48 void DatabaseTask::performTask()
49 {
50 // Database tasks are meant to be used only once, so make sure this one hasn't been performed before.
51 ASSERT(!m_complete);
52
53 LOG(StorageAPI, "Performing %s %p\n", debugTaskName(), this);
54
55 m_database->resetAuthorizer();
56 doPerformTask();
57 m_database->performPolicyChecks();
58
59 if (m_synchronousMutex)
60 m_synchronousMutex->lock();
61
62 m_complete = true;
63
64 if (m_synchronousMutex) {
65 m_synchronousCondition->signal();
66 m_synchronousMutex->unlock();
67 }
68 }
69
lockForSynchronousScheduling()70 void DatabaseTask::lockForSynchronousScheduling()
71 {
72 // Called from main thread.
73 ASSERT(!m_synchronousMutex);
74 ASSERT(!m_synchronousCondition);
75 m_synchronousMutex.set(new Mutex);
76 m_synchronousCondition.set(new ThreadCondition);
77 }
78
waitForSynchronousCompletion()79 void DatabaseTask::waitForSynchronousCompletion()
80 {
81 // Called from main thread.
82 m_synchronousMutex->lock();
83 if (!m_complete)
84 m_synchronousCondition->wait(*m_synchronousMutex);
85 m_synchronousMutex->unlock();
86 }
87
88 // *** DatabaseOpenTask ***
89 // Opens the database file and verifies the version matches the expected version.
90
DatabaseOpenTask(Database * database)91 DatabaseOpenTask::DatabaseOpenTask(Database* database)
92 : DatabaseTask(database)
93 , m_code(0)
94 , m_success(false)
95 {
96 }
97
doPerformTask()98 void DatabaseOpenTask::doPerformTask()
99 {
100 m_success = database()->performOpenAndVerify(m_code);
101 }
102
103 #ifndef NDEBUG
debugTaskName() const104 const char* DatabaseOpenTask::debugTaskName() const
105 {
106 return "DatabaseOpenTask";
107 }
108 #endif
109
110 // *** DatabaseCloseTask ***
111 // Closes the database.
112
DatabaseCloseTask(Database * database)113 DatabaseCloseTask::DatabaseCloseTask(Database* database)
114 : DatabaseTask(database)
115 {
116 }
117
doPerformTask()118 void DatabaseCloseTask::doPerformTask()
119 {
120 database()->close();
121 }
122
123 #ifndef NDEBUG
debugTaskName() const124 const char* DatabaseCloseTask::debugTaskName() const
125 {
126 return "DatabaseCloseTask";
127 }
128 #endif
129
130 // *** DatabaseTransactionTask ***
131 // Starts a transaction that will report its results via a callback.
132
DatabaseTransactionTask(PassRefPtr<SQLTransaction> transaction)133 DatabaseTransactionTask::DatabaseTransactionTask(PassRefPtr<SQLTransaction> transaction)
134 : DatabaseTask(transaction->database())
135 , m_transaction(transaction)
136 {
137 }
138
~DatabaseTransactionTask()139 DatabaseTransactionTask::~DatabaseTransactionTask()
140 {
141 }
142
doPerformTask()143 void DatabaseTransactionTask::doPerformTask()
144 {
145 if (m_transaction->performNextStep()) {
146 // The transaction is complete, we can move on to the next one.
147 MutexLocker locker(m_transaction->database()->m_transactionInProgressMutex);
148 m_transaction->database()->scheduleTransaction();
149 }
150 }
151
152 #ifndef NDEBUG
debugTaskName() const153 const char* DatabaseTransactionTask::debugTaskName() const
154 {
155 return "DatabaseTransactionTask";
156 }
157 #endif
158
159 // *** DatabaseTableNamesTask ***
160 // Retrieves a list of all tables in the database - for WebInspector support.
161
DatabaseTableNamesTask(Database * database)162 DatabaseTableNamesTask::DatabaseTableNamesTask(Database* database)
163 : DatabaseTask(database)
164 {
165 }
166
doPerformTask()167 void DatabaseTableNamesTask::doPerformTask()
168 {
169 m_tableNames = database()->performGetTableNames();
170 }
171
172 #ifndef NDEBUG
debugTaskName() const173 const char* DatabaseTableNamesTask::debugTaskName() const
174 {
175 return "DatabaseTableNamesTask";
176 }
177 #endif
178
179 } // namespace WebCore
180
181 #endif
182