Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright (C) 2007, 2008, 2013 Apple Inc. All rights reserved. | 2 * Copyright (C) 2007, 2008, 2013 Apple Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
| 6 * are met: | 6 * are met: |
| 7 * | 7 * |
| 8 * 1. Redistributions of source code must retain the above copyright | 8 * 1. Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
| 10 * 2. Redistributions in binary form must reproduce the above copyright | 10 * 2. Redistributions in binary form must reproduce the above copyright |
| 11 * notice, this list of conditions and the following disclaimer in the | 11 * notice, this list of conditions and the following disclaimer in the |
| 12 * documentation and/or other materials provided with the distribution. | 12 * documentation and/or other materials provided with the distribution. |
| 13 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of | 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 | 14 * its contributors may be used to endorse or promote products derived |
| 15 * from this software without specific prior written permission. | 15 * from this software without specific prior written permission. |
| 16 * | 16 * |
| 17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY | 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 | 18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | 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 | 20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY |
| 21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | 21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | 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 | 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 | 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 | 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. | 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 */ | 27 */ |
| 28 | |
| 28 #include "config.h" | 29 #include "config.h" |
| 29 #include "modules/webdatabase/DatabaseTask.h" | 30 #include "platform/TaskSynchronizer.h" |
| 30 | 31 |
| 31 #include "platform/Logging.h" | 32 #include "heap/Handle.h" |
|
tkent
2014/03/13 09:53:30
ThreadState.h instead?
haraken
2014/03/13 10:24:33
Done.
| |
| 32 #include "modules/webdatabase/Database.h" | |
| 33 #include "modules/webdatabase/DatabaseContext.h" | |
| 34 #include "modules/webdatabase/DatabaseThread.h" | |
| 35 | 33 |
| 36 namespace WebCore { | 34 namespace WebCore { |
| 37 | 35 |
| 38 DatabaseTaskSynchronizer::DatabaseTaskSynchronizer() | 36 TaskSynchronizer::TaskSynchronizer() |
| 39 : m_taskCompleted(false) | 37 : m_taskCompleted(false) |
| 40 #ifndef NDEBUG | 38 #ifndef NDEBUG |
| 41 , m_hasCheckedForTermination(false) | 39 , m_hasCheckedForTermination(false) |
| 42 #endif | 40 #endif |
| 43 { | 41 { |
| 44 } | 42 } |
| 45 | 43 |
| 46 void DatabaseTaskSynchronizer::waitForTaskCompletion() | 44 void TaskSynchronizer::waitForTaskCompletion() |
| 47 { | 45 { |
| 48 // Prevent the deadlock between park request by other threads and blocking | 46 // Prevent the deadlock between park request by other threads and blocking |
| 49 // by m_synchronousCondition. | 47 // by m_synchronousCondition. |
| 50 ThreadState::SafePointScope scope(ThreadState::HeapPointersOnStack); | 48 ThreadState::SafePointScope scope(ThreadState::HeapPointersOnStack); |
| 51 m_synchronousMutex.lock(); | 49 m_synchronousMutex.lock(); |
| 52 while (!m_taskCompleted) | 50 while (!m_taskCompleted) |
| 53 m_synchronousCondition.wait(m_synchronousMutex); | 51 m_synchronousCondition.wait(m_synchronousMutex); |
| 54 m_synchronousMutex.unlock(); | 52 m_synchronousMutex.unlock(); |
| 55 } | 53 } |
| 56 | 54 |
| 57 void DatabaseTaskSynchronizer::taskCompleted() | 55 void TaskSynchronizer::taskCompleted() |
| 58 { | 56 { |
| 59 m_synchronousMutex.lock(); | 57 m_synchronousMutex.lock(); |
| 60 m_taskCompleted = true; | 58 m_taskCompleted = true; |
| 61 m_synchronousCondition.signal(); | 59 m_synchronousCondition.signal(); |
| 62 m_synchronousMutex.unlock(); | 60 m_synchronousMutex.unlock(); |
| 63 } | 61 } |
| 64 | 62 |
| 65 DatabaseTask::DatabaseTask(DatabaseBackend* database, DatabaseTaskSynchronizer* synchronizer) | |
| 66 : m_database(database) | |
| 67 , m_synchronizer(synchronizer) | |
| 68 #if !LOG_DISABLED | |
| 69 , m_complete(false) | |
| 70 #endif | |
| 71 { | |
| 72 } | |
| 73 | |
| 74 DatabaseTask::~DatabaseTask() | |
| 75 { | |
| 76 #if !LOG_DISABLED | |
| 77 ASSERT(m_complete || !m_synchronizer); | |
| 78 #endif | |
| 79 } | |
| 80 | |
| 81 void DatabaseTask::run() | |
| 82 { | |
| 83 // Database tasks are meant to be used only once, so make sure this one hasn 't been performed before. | |
| 84 #if !LOG_DISABLED | |
| 85 ASSERT(!m_complete); | |
| 86 #endif | |
| 87 | |
| 88 if (!m_synchronizer && !m_database->databaseContext()->databaseThread()->isD atabaseOpen(m_database.get())) { | |
| 89 taskCancelled(); | |
| 90 #if !LOG_DISABLED | |
| 91 m_complete = true; | |
| 92 #endif | |
| 93 return; | |
| 94 } | |
| 95 | |
| 96 WTF_LOG(StorageAPI, "Performing %s %p\n", debugTaskName(), this); | |
| 97 | |
| 98 m_database->resetAuthorizer(); | |
| 99 doPerformTask(); | |
| 100 | |
| 101 if (m_synchronizer) | |
| 102 m_synchronizer->taskCompleted(); | |
| 103 | |
| 104 #if !LOG_DISABLED | |
| 105 m_complete = true; | |
| 106 #endif | |
| 107 } | |
| 108 | |
| 109 // *** DatabaseOpenTask *** | |
| 110 // Opens the database file and verifies the version matches the expected version . | |
| 111 | |
| 112 DatabaseBackend::DatabaseOpenTask::DatabaseOpenTask(DatabaseBackend* database, b ool setVersionInNewDatabase, DatabaseTaskSynchronizer* synchronizer, DatabaseErr or& error, String& errorMessage, bool& success) | |
| 113 : DatabaseTask(database, synchronizer) | |
| 114 , m_setVersionInNewDatabase(setVersionInNewDatabase) | |
| 115 , m_error(error) | |
| 116 , m_errorMessage(errorMessage) | |
| 117 , m_success(success) | |
| 118 { | |
| 119 ASSERT(synchronizer); // A task with output parameters is supposed to be syn chronous. | |
| 120 } | |
| 121 | |
| 122 void DatabaseBackend::DatabaseOpenTask::doPerformTask() | |
| 123 { | |
| 124 String errorMessage; | |
| 125 m_success = database()->performOpenAndVerify(m_setVersionInNewDatabase, m_er ror, errorMessage); | |
| 126 if (!m_success) | |
| 127 m_errorMessage = errorMessage.isolatedCopy(); | |
| 128 } | |
| 129 | |
| 130 #if !LOG_DISABLED | |
| 131 const char* DatabaseBackend::DatabaseOpenTask::debugTaskName() const | |
| 132 { | |
| 133 return "DatabaseOpenTask"; | |
| 134 } | |
| 135 #endif | |
| 136 | |
| 137 // *** DatabaseCloseTask *** | |
| 138 // Closes the database. | |
| 139 | |
| 140 DatabaseBackend::DatabaseCloseTask::DatabaseCloseTask(DatabaseBackend* database, DatabaseTaskSynchronizer* synchronizer) | |
| 141 : DatabaseTask(database, synchronizer) | |
| 142 { | |
| 143 } | |
| 144 | |
| 145 void DatabaseBackend::DatabaseCloseTask::doPerformTask() | |
| 146 { | |
| 147 Database::from(database())->close(); | |
| 148 } | |
| 149 | |
| 150 #if !LOG_DISABLED | |
| 151 const char* DatabaseBackend::DatabaseCloseTask::debugTaskName() const | |
| 152 { | |
| 153 return "DatabaseCloseTask"; | |
| 154 } | |
| 155 #endif | |
| 156 | |
| 157 // *** DatabaseTransactionTask *** | |
| 158 // Starts a transaction that will report its results via a callback. | |
| 159 | |
| 160 DatabaseBackend::DatabaseTransactionTask::DatabaseTransactionTask(PassRefPtr<SQL TransactionBackend> transaction) | |
| 161 : DatabaseTask(Database::from(transaction->database()), 0) | |
| 162 , m_transaction(transaction) | |
| 163 { | |
| 164 } | |
| 165 | |
| 166 DatabaseBackend::DatabaseTransactionTask::~DatabaseTransactionTask() | |
| 167 { | |
| 168 } | |
| 169 | |
| 170 void DatabaseBackend::DatabaseTransactionTask::doPerformTask() | |
| 171 { | |
| 172 m_transaction->performNextStep(); | |
| 173 } | |
| 174 | |
| 175 void DatabaseBackend::DatabaseTransactionTask::taskCancelled() | |
| 176 { | |
| 177 // If the task is being destructed without the transaction ever being run, | |
| 178 // then we must either have an error or an interruption. Give the | |
| 179 // transaction a chance to clean up since it may not have been able to | |
| 180 // run to its clean up state. | |
| 181 | |
| 182 // Transaction phase 2 cleanup. See comment on "What happens if a | |
| 183 // transaction is interrupted?" at the top of SQLTransactionBackend.cpp. | |
| 184 | |
| 185 m_transaction->notifyDatabaseThreadIsShuttingDown(); | |
| 186 } | |
| 187 | |
| 188 #if !LOG_DISABLED | |
| 189 const char* DatabaseBackend::DatabaseTransactionTask::debugTaskName() const | |
| 190 { | |
| 191 return "DatabaseTransactionTask"; | |
| 192 } | |
| 193 #endif | |
| 194 | |
| 195 // *** DatabaseTableNamesTask *** | |
| 196 // Retrieves a list of all tables in the database - for WebInspector support. | |
| 197 | |
| 198 DatabaseBackend::DatabaseTableNamesTask::DatabaseTableNamesTask(DatabaseBackend* database, DatabaseTaskSynchronizer* synchronizer, Vector<String>& names) | |
| 199 : DatabaseTask(database, synchronizer) | |
| 200 , m_tableNames(names) | |
| 201 { | |
| 202 ASSERT(synchronizer); // A task with output parameters is supposed to be syn chronous. | |
| 203 } | |
| 204 | |
| 205 void DatabaseBackend::DatabaseTableNamesTask::doPerformTask() | |
| 206 { | |
| 207 m_tableNames = Database::from(database())->performGetTableNames(); | |
| 208 } | |
| 209 | |
| 210 #if !LOG_DISABLED | |
| 211 const char* DatabaseBackend::DatabaseTableNamesTask::debugTaskName() const | |
| 212 { | |
| 213 return "DatabaseTableNamesTask"; | |
| 214 } | |
| 215 #endif | |
| 216 | |
| 217 } // namespace WebCore | 63 } // namespace WebCore |
| OLD | NEW |