| 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 | #pragma once |
| 30 | |
| 31 | #include "ExceptionOr.h" |
| 32 | #include <wtf/Condition.h> |
| 33 | #include <wtf/Forward.h> |
| 34 | #include <wtf/Lock.h> |
| 35 | |
| 36 | namespace WebCore { |
| 37 | |
| 38 | class Database; |
| 39 | class SQLTransaction; |
| 40 | |
| 41 | // Can be used to wait until DatabaseTask is completed. |
| 42 | // Has to be passed into DatabaseTask::create to be associated with the task. |
| 43 | class DatabaseTaskSynchronizer { |
| 44 | WTF_MAKE_NONCOPYABLE(DatabaseTaskSynchronizer); |
| 45 | public: |
| 46 | DatabaseTaskSynchronizer(); |
| 47 | |
| 48 | // Called from main thread to wait until task is completed. |
| 49 | void waitForTaskCompletion(); |
| 50 | |
| 51 | // Called by the task. |
| 52 | void taskCompleted(); |
| 53 | |
| 54 | #ifndef NDEBUG |
| 55 | bool hasCheckedForTermination() const { return m_hasCheckedForTermination; } |
| 56 | void setHasCheckedForTermination() { m_hasCheckedForTermination = true; } |
| 57 | #endif |
| 58 | |
| 59 | private: |
| 60 | bool m_taskCompleted { false }; |
| 61 | Lock m_synchronousMutex; |
| 62 | Condition m_synchronousCondition; |
| 63 | #ifndef NDEBUG |
| 64 | bool m_hasCheckedForTermination { false }; |
| 65 | #endif |
| 66 | }; |
| 67 | |
| 68 | class DatabaseTask { |
| 69 | WTF_MAKE_FAST_ALLOCATED; |
| 70 | public: |
| 71 | virtual ~DatabaseTask(); |
| 72 | |
| 73 | void performTask(); |
| 74 | |
| 75 | Database& database() const { return m_database; } |
| 76 | |
| 77 | #if !ASSERT_DISABLED |
| 78 | bool hasSynchronizer() const { return m_synchronizer; } |
| 79 | bool hasCheckedForTermination() const { return m_synchronizer->hasCheckedForTermination(); } |
| 80 | #endif |
| 81 | |
| 82 | protected: |
| 83 | DatabaseTask(Database&, DatabaseTaskSynchronizer*); |
| 84 | |
| 85 | private: |
| 86 | virtual void doPerformTask() = 0; |
| 87 | |
| 88 | Database& m_database; |
| 89 | DatabaseTaskSynchronizer* m_synchronizer; |
| 90 | |
| 91 | #if !LOG_DISABLED |
| 92 | virtual const char* debugTaskName() const = 0; |
| 93 | #endif |
| 94 | |
| 95 | #if !ASSERT_DISABLED |
| 96 | bool m_complete { false }; |
| 97 | #endif |
| 98 | }; |
| 99 | |
| 100 | class DatabaseOpenTask final : public DatabaseTask { |
| 101 | public: |
| 102 | DatabaseOpenTask(Database&, bool setVersionInNewDatabase, DatabaseTaskSynchronizer&, ExceptionOr<void>& result); |
| 103 | |
| 104 | private: |
| 105 | void doPerformTask() final; |
| 106 | |
| 107 | #if !LOG_DISABLED |
| 108 | const char* debugTaskName() const final; |
| 109 | #endif |
| 110 | |
| 111 | bool m_setVersionInNewDatabase; |
| 112 | ExceptionOr<void>& m_result; |
| 113 | }; |
| 114 | |
| 115 | class DatabaseCloseTask final : public DatabaseTask { |
| 116 | public: |
| 117 | DatabaseCloseTask(Database&, DatabaseTaskSynchronizer&); |
| 118 | |
| 119 | private: |
| 120 | void doPerformTask() final; |
| 121 | |
| 122 | #if !LOG_DISABLED |
| 123 | const char* debugTaskName() const final; |
| 124 | #endif |
| 125 | }; |
| 126 | |
| 127 | class DatabaseTransactionTask final : public DatabaseTask { |
| 128 | public: |
| 129 | explicit DatabaseTransactionTask(RefPtr<SQLTransaction>&&); |
| 130 | virtual ~DatabaseTransactionTask(); |
| 131 | |
| 132 | SQLTransaction* transaction() const { return m_transaction.get(); } |
| 133 | |
| 134 | private: |
| 135 | void doPerformTask() final; |
| 136 | |
| 137 | #if !LOG_DISABLED |
| 138 | const char* debugTaskName() const final; |
| 139 | #endif |
| 140 | |
| 141 | RefPtr<SQLTransaction> m_transaction; |
| 142 | bool m_didPerformTask; |
| 143 | }; |
| 144 | |
| 145 | class DatabaseTableNamesTask final : public DatabaseTask { |
| 146 | public: |
| 147 | DatabaseTableNamesTask(Database&, DatabaseTaskSynchronizer&, Vector<String>& result); |
| 148 | |
| 149 | private: |
| 150 | void doPerformTask() final; |
| 151 | |
| 152 | #if !LOG_DISABLED |
| 153 | const char* debugTaskName() const override; |
| 154 | #endif |
| 155 | |
| 156 | Vector<String>& m_result; |
| 157 | }; |
| 158 | |
| 159 | } // namespace WebCore |
| 160 | |