|
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 |
|
38 DatabaseTaskSynchronizer::DatabaseTaskSynchronizer() |
|
39 : m_taskCompleted(false) |
|
40 { |
|
41 } |
|
42 |
|
43 void DatabaseTaskSynchronizer::waitForTaskCompletion() |
|
44 { |
|
45 m_synchronousMutex.lock(); |
|
46 if (!m_taskCompleted) |
|
47 m_synchronousCondition.wait(m_synchronousMutex); |
|
48 m_synchronousMutex.unlock(); |
|
49 } |
|
50 |
|
51 void DatabaseTaskSynchronizer::taskCompleted() |
|
52 { |
|
53 m_synchronousMutex.lock(); |
|
54 m_taskCompleted = true; |
|
55 m_synchronousCondition.signal(); |
|
56 m_synchronousMutex.unlock(); |
|
57 } |
|
58 |
|
59 DatabaseTask::DatabaseTask(Database* database, DatabaseTaskSynchronizer* synchronizer) |
|
60 : m_database(database) |
|
61 , m_synchronizer(synchronizer) |
|
62 #ifndef NDEBUG |
|
63 , m_complete(false) |
|
64 #endif |
|
65 { |
|
66 } |
|
67 |
|
68 DatabaseTask::~DatabaseTask() |
|
69 { |
|
70 } |
|
71 |
|
72 void DatabaseTask::performTask() |
|
73 { |
|
74 // Database tasks are meant to be used only once, so make sure this one hasn't been performed before. |
|
75 ASSERT(!m_complete); |
|
76 |
|
77 LOG(StorageAPI, "Performing %s %p\n", debugTaskName(), this); |
|
78 |
|
79 m_database->resetAuthorizer(); |
|
80 doPerformTask(); |
|
81 |
|
82 if (m_synchronizer) |
|
83 m_synchronizer->taskCompleted(); |
|
84 } |
|
85 |
|
86 // *** DatabaseOpenTask *** |
|
87 // Opens the database file and verifies the version matches the expected version. |
|
88 |
|
89 Database::DatabaseOpenTask::DatabaseOpenTask(Database* database, bool setVersionInNewDatabase, DatabaseTaskSynchronizer* synchronizer, ExceptionCode& code, bool& success) |
|
90 : DatabaseTask(database, synchronizer) |
|
91 , m_setVersionInNewDatabase(setVersionInNewDatabase) |
|
92 , m_code(code) |
|
93 , m_success(success) |
|
94 { |
|
95 ASSERT(synchronizer); // A task with output parameters is supposed to be synchronous. |
|
96 } |
|
97 |
|
98 void Database::DatabaseOpenTask::doPerformTask() |
|
99 { |
|
100 m_success = database()->performOpenAndVerify(m_setVersionInNewDatabase, m_code); |
|
101 } |
|
102 |
|
103 #ifndef NDEBUG |
|
104 const char* Database::DatabaseOpenTask::debugTaskName() const |
|
105 { |
|
106 return "DatabaseOpenTask"; |
|
107 } |
|
108 #endif |
|
109 |
|
110 // *** DatabaseCloseTask *** |
|
111 // Closes the database. |
|
112 |
|
113 Database::DatabaseCloseTask::DatabaseCloseTask(Database* database, DatabaseTaskSynchronizer* synchronizer) |
|
114 : DatabaseTask(database, synchronizer) |
|
115 { |
|
116 } |
|
117 |
|
118 void Database::DatabaseCloseTask::doPerformTask() |
|
119 { |
|
120 database()->close(); |
|
121 } |
|
122 |
|
123 #ifndef NDEBUG |
|
124 const char* Database::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 |
|
133 Database::DatabaseTransactionTask::DatabaseTransactionTask(PassRefPtr<SQLTransaction> transaction) |
|
134 : DatabaseTask(transaction->database(), 0) |
|
135 , m_transaction(transaction) |
|
136 { |
|
137 } |
|
138 |
|
139 void Database::DatabaseTransactionTask::doPerformTask() |
|
140 { |
|
141 if (m_transaction->performNextStep()) |
|
142 m_transaction->database()->inProgressTransactionCompleted(); |
|
143 } |
|
144 |
|
145 #ifndef NDEBUG |
|
146 const char* Database::DatabaseTransactionTask::debugTaskName() const |
|
147 { |
|
148 return "DatabaseTransactionTask"; |
|
149 } |
|
150 #endif |
|
151 |
|
152 // *** DatabaseTableNamesTask *** |
|
153 // Retrieves a list of all tables in the database - for WebInspector support. |
|
154 |
|
155 Database::DatabaseTableNamesTask::DatabaseTableNamesTask(Database* database, DatabaseTaskSynchronizer* synchronizer, Vector<String>& names) |
|
156 : DatabaseTask(database, synchronizer) |
|
157 , m_tableNames(names) |
|
158 { |
|
159 ASSERT(synchronizer); // A task with output parameters is supposed to be synchronous. |
|
160 } |
|
161 |
|
162 void Database::DatabaseTableNamesTask::doPerformTask() |
|
163 { |
|
164 m_tableNames = database()->performGetTableNames(); |
|
165 } |
|
166 |
|
167 #ifndef NDEBUG |
|
168 const char* Database::DatabaseTableNamesTask::debugTaskName() const |
|
169 { |
|
170 return "DatabaseTableNamesTask"; |
|
171 } |
|
172 #endif |
|
173 |
|
174 } // namespace WebCore |
|
175 |
|
176 #endif |