1 /* 2 * Copyright (C) 2007, 2008, 2013 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 #ifndef DatabaseTask_h 29 #define DatabaseTask_h 30 31 #include "modules/webdatabase/DatabaseBackend.h" 32 #include "modules/webdatabase/DatabaseBasicTypes.h" 33 #include "modules/webdatabase/DatabaseError.h" 34 #include "modules/webdatabase/SQLTransactionBackend.h" 35 #include "wtf/OwnPtr.h" 36 #include "wtf/PassOwnPtr.h" 37 #include "wtf/PassRefPtr.h" 38 #include "wtf/Threading.h" 39 #include "wtf/Vector.h" 40 #include "wtf/text/WTFString.h" 41 42 namespace WebCore { 43 44 // Can be used to wait until DatabaseTask is completed. 45 // Has to be passed into DatabaseTask::create to be associated with the task. 46 class DatabaseTaskSynchronizer { 47 WTF_MAKE_NONCOPYABLE(DatabaseTaskSynchronizer); 48 public: 49 DatabaseTaskSynchronizer(); 50 51 // Called from main thread to wait until task is completed. 52 void waitForTaskCompletion(); 53 54 // Called by the task. 55 void taskCompleted(); 56 57 #ifndef NDEBUG 58 bool hasCheckedForTermination() const { return m_hasCheckedForTermination; } 59 void setHasCheckedForTermination() { m_hasCheckedForTermination = true; } 60 #endif 61 62 private: 63 bool m_taskCompleted; 64 Mutex m_synchronousMutex; 65 ThreadCondition m_synchronousCondition; 66 #ifndef NDEBUG 67 bool m_hasCheckedForTermination; 68 #endif 69 }; 70 71 class DatabaseTask { 72 WTF_MAKE_NONCOPYABLE(DatabaseTask); WTF_MAKE_FAST_ALLOCATED; 73 public: 74 virtual ~DatabaseTask(); 75 76 void performTask(); 77 78 DatabaseBackend* database() const { return m_database; } 79 #ifndef NDEBUG 80 bool hasSynchronizer() const { return m_synchronizer; } 81 bool hasCheckedForTermination() const { return m_synchronizer->hasCheckedForTermination(); } 82 #endif 83 84 protected: 85 DatabaseTask(DatabaseBackend*, DatabaseTaskSynchronizer*); 86 87 private: 88 virtual void doPerformTask() = 0; 89 90 DatabaseBackend* m_database; 91 DatabaseTaskSynchronizer* m_synchronizer; 92 93 #if !LOG_DISABLED 94 virtual const char* debugTaskName() const = 0; 95 bool m_complete; 96 #endif 97 }; 98 99 class DatabaseBackend::DatabaseOpenTask : public DatabaseTask { 100 public: 101 static PassOwnPtr<DatabaseOpenTask> create(DatabaseBackend* db, bool setVersionInNewDatabase, DatabaseTaskSynchronizer* synchronizer, DatabaseError& error, String& errorMessage, bool& success) 102 { 103 return adoptPtr(new DatabaseOpenTask(db, setVersionInNewDatabase, synchronizer, error, errorMessage, success)); 104 } 105 106 private: 107 DatabaseOpenTask(DatabaseBackend*, bool setVersionInNewDatabase, DatabaseTaskSynchronizer*, DatabaseError&, String& errorMessage, bool& success); 108 109 virtual void doPerformTask(); 110 #if !LOG_DISABLED 111 virtual const char* debugTaskName() const; 112 #endif 113 114 bool m_setVersionInNewDatabase; 115 DatabaseError& m_error; 116 String& m_errorMessage; 117 bool& m_success; 118 }; 119 120 class DatabaseBackend::DatabaseCloseTask : public DatabaseTask { 121 public: 122 static PassOwnPtr<DatabaseCloseTask> create(DatabaseBackend* db, DatabaseTaskSynchronizer* synchronizer) 123 { 124 return adoptPtr(new DatabaseCloseTask(db, synchronizer)); 125 } 126 127 private: 128 DatabaseCloseTask(DatabaseBackend*, DatabaseTaskSynchronizer*); 129 130 virtual void doPerformTask(); 131 #if !LOG_DISABLED 132 virtual const char* debugTaskName() const; 133 #endif 134 }; 135 136 class DatabaseBackend::DatabaseTransactionTask : public DatabaseTask { 137 public: 138 virtual ~DatabaseTransactionTask(); 139 140 // Transaction task is never synchronous, so no 'synchronizer' parameter. 141 static PassOwnPtr<DatabaseTransactionTask> create(PassRefPtr<SQLTransactionBackend> transaction) 142 { 143 return adoptPtr(new DatabaseTransactionTask(transaction)); 144 } 145 146 SQLTransactionBackend* transaction() const { return m_transaction.get(); } 147 148 private: 149 explicit DatabaseTransactionTask(PassRefPtr<SQLTransactionBackend>); 150 151 virtual void doPerformTask(); 152 #if !LOG_DISABLED 153 virtual const char* debugTaskName() const; 154 #endif 155 156 RefPtr<SQLTransactionBackend> m_transaction; 157 bool m_didPerformTask; 158 }; 159 160 class DatabaseBackend::DatabaseTableNamesTask : public DatabaseTask { 161 public: 162 static PassOwnPtr<DatabaseTableNamesTask> create(DatabaseBackend* db, DatabaseTaskSynchronizer* synchronizer, Vector<String>& names) 163 { 164 return adoptPtr(new DatabaseTableNamesTask(db, synchronizer, names)); 165 } 166 167 private: 168 DatabaseTableNamesTask(DatabaseBackend*, DatabaseTaskSynchronizer*, Vector<String>& names); 169 170 virtual void doPerformTask(); 171 #if !LOG_DISABLED 172 virtual const char* debugTaskName() const; 173 #endif 174 175 Vector<String>& m_tableNames; 176 }; 177 178 } // namespace WebCore 179 180 #endif // DatabaseTask_h 181