| 1 | /* |
| 2 | * Copyright (C) 2007, 2008, 2013, 2015 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 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 | |
| 29 | #include "config.h" |
| 30 | #include "DatabaseTask.h" |
| 31 | |
| 32 | #include "Database.h" |
| 33 | #include "Logging.h" |
| 34 | #include "SQLTransaction.h" |
| 35 | |
| 36 | namespace WebCore { |
| 37 | |
| 38 | DatabaseTaskSynchronizer::DatabaseTaskSynchronizer() |
| 39 | { |
| 40 | } |
| 41 | |
| 42 | void DatabaseTaskSynchronizer::waitForTaskCompletion() |
| 43 | { |
| 44 | m_synchronousMutex.lock(); |
| 45 | while (!m_taskCompleted) |
| 46 | m_synchronousCondition.wait(m_synchronousMutex); |
| 47 | m_synchronousMutex.unlock(); |
| 48 | } |
| 49 | |
| 50 | void DatabaseTaskSynchronizer::taskCompleted() |
| 51 | { |
| 52 | m_synchronousMutex.lock(); |
| 53 | m_taskCompleted = true; |
| 54 | m_synchronousCondition.notifyOne(); |
| 55 | m_synchronousMutex.unlock(); |
| 56 | } |
| 57 | |
| 58 | DatabaseTask::DatabaseTask(Database& database, DatabaseTaskSynchronizer* synchronizer) |
| 59 | : m_database(database) |
| 60 | , m_synchronizer(synchronizer) |
| 61 | { |
| 62 | } |
| 63 | |
| 64 | DatabaseTask::~DatabaseTask() |
| 65 | { |
| 66 | ASSERT(m_complete || !m_synchronizer); |
| 67 | } |
| 68 | |
| 69 | void DatabaseTask::performTask() |
| 70 | { |
| 71 | // Database tasks are meant to be used only once, so make sure this one hasn't been performed before. |
| 72 | ASSERT(!m_complete); |
| 73 | |
| 74 | LOG(StorageAPI, "Performing %s %p\n" , debugTaskName(), this); |
| 75 | |
| 76 | m_database.resetAuthorizer(); |
| 77 | |
| 78 | doPerformTask(); |
| 79 | |
| 80 | if (m_synchronizer) |
| 81 | m_synchronizer->taskCompleted(); |
| 82 | |
| 83 | #if !ASSERT_DISABLED |
| 84 | m_complete = true; |
| 85 | #endif |
| 86 | } |
| 87 | |
| 88 | // *** DatabaseOpenTask *** |
| 89 | // Opens the database file and verifies the version matches the expected version. |
| 90 | |
| 91 | DatabaseOpenTask::DatabaseOpenTask(Database& database, bool setVersionInNewDatabase, DatabaseTaskSynchronizer& synchronizer, ExceptionOr<void>& result) |
| 92 | : DatabaseTask(database, &synchronizer) |
| 93 | , m_setVersionInNewDatabase(setVersionInNewDatabase) |
| 94 | , m_result(result) |
| 95 | { |
| 96 | } |
| 97 | |
| 98 | void DatabaseOpenTask::doPerformTask() |
| 99 | { |
| 100 | m_result = isolatedCopy(database().performOpenAndVerify(m_setVersionInNewDatabase)); |
| 101 | } |
| 102 | |
| 103 | #if !LOG_DISABLED |
| 104 | |
| 105 | const char* DatabaseOpenTask::debugTaskName() const |
| 106 | { |
| 107 | return "DatabaseOpenTask" ; |
| 108 | } |
| 109 | |
| 110 | #endif |
| 111 | |
| 112 | // *** DatabaseCloseTask *** |
| 113 | // Closes the database. |
| 114 | |
| 115 | DatabaseCloseTask::DatabaseCloseTask(Database& database, DatabaseTaskSynchronizer& synchronizer) |
| 116 | : DatabaseTask(database, &synchronizer) |
| 117 | { |
| 118 | } |
| 119 | |
| 120 | void DatabaseCloseTask::doPerformTask() |
| 121 | { |
| 122 | database().performClose(); |
| 123 | } |
| 124 | |
| 125 | #if !LOG_DISABLED |
| 126 | |
| 127 | const char* DatabaseCloseTask::debugTaskName() const |
| 128 | { |
| 129 | return "DatabaseCloseTask" ; |
| 130 | } |
| 131 | |
| 132 | #endif |
| 133 | |
| 134 | // *** DatabaseTransactionTask *** |
| 135 | // Starts a transaction that will report its results via a callback. |
| 136 | |
| 137 | DatabaseTransactionTask::DatabaseTransactionTask(RefPtr<SQLTransaction>&& transaction) |
| 138 | : DatabaseTask(transaction->database(), 0) |
| 139 | , m_transaction(WTFMove(transaction)) |
| 140 | , m_didPerformTask(false) |
| 141 | { |
| 142 | } |
| 143 | |
| 144 | DatabaseTransactionTask::~DatabaseTransactionTask() |
| 145 | { |
| 146 | // If the task is being destructed without the transaction ever being run, |
| 147 | // then we must either have an error or an interruption. Give the |
| 148 | // transaction a chance to clean up since it may not have been able to |
| 149 | // run to its clean up state. |
| 150 | |
| 151 | // Transaction phase 2 cleanup. See comment on "What happens if a |
| 152 | // transaction is interrupted?" at the top of SQLTransactionBackend.cpp. |
| 153 | |
| 154 | if (!m_didPerformTask) |
| 155 | m_transaction->notifyDatabaseThreadIsShuttingDown(); |
| 156 | } |
| 157 | |
| 158 | void DatabaseTransactionTask::doPerformTask() |
| 159 | { |
| 160 | m_transaction->performNextStep(); |
| 161 | m_didPerformTask = true; |
| 162 | } |
| 163 | |
| 164 | #if !LOG_DISABLED |
| 165 | |
| 166 | const char* DatabaseTransactionTask::debugTaskName() const |
| 167 | { |
| 168 | return "DatabaseTransactionTask" ; |
| 169 | } |
| 170 | |
| 171 | #endif |
| 172 | |
| 173 | // *** DatabaseTableNamesTask *** |
| 174 | // Retrieves a list of all tables in the database - for WebInspector support. |
| 175 | |
| 176 | DatabaseTableNamesTask::DatabaseTableNamesTask(Database& database, DatabaseTaskSynchronizer& synchronizer, Vector<String>& result) |
| 177 | : DatabaseTask(database, &synchronizer) |
| 178 | , m_result(result) |
| 179 | { |
| 180 | } |
| 181 | |
| 182 | void DatabaseTableNamesTask::doPerformTask() |
| 183 | { |
| 184 | // FIXME: Why no need for an isolatedCopy here? |
| 185 | m_result = database().performGetTableNames(); |
| 186 | } |
| 187 | |
| 188 | #if !LOG_DISABLED |
| 189 | |
| 190 | const char* DatabaseTableNamesTask::debugTaskName() const |
| 191 | { |
| 192 | return "DatabaseTableNamesTask" ; |
| 193 | } |
| 194 | |
| 195 | #endif |
| 196 | |
| 197 | } // namespace WebCore |
| 198 | |