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 29 #ifndef DatabaseTask_h 30 #define DatabaseTask_h 31 32 #include "modules/webdatabase/DatabaseBackend.h" 33 #include "modules/webdatabase/DatabaseBasicTypes.h" 34 #include "modules/webdatabase/DatabaseError.h" 35 #include "modules/webdatabase/SQLTransactionBackend.h" 36 #include "platform/Task.h" 37 #include "platform/TaskSynchronizer.h" 38 #include "platform/heap/Handle.h" 39 #include "wtf/OwnPtr.h" 40 #include "wtf/PassOwnPtr.h" 41 #include "wtf/PassRefPtr.h" 42 #include "wtf/RefPtr.h" 43 #include "wtf/Threading.h" 44 #include "wtf/Vector.h" 45 #include "wtf/text/WTFString.h" 46 47 namespace WebCore { 48 49 class DatabaseTask : public blink::WebThread::Task { 50 WTF_MAKE_NONCOPYABLE(DatabaseTask); WTF_MAKE_FAST_ALLOCATED; 51 public: 52 virtual ~DatabaseTask(); 53 54 virtual void run() OVERRIDE FINAL; 55 56 DatabaseBackend* database() const { return m_database.get(); } 57 #ifndef NDEBUG 58 bool hasSynchronizer() const { return m_synchronizer; } 59 bool hasCheckedForTermination() const { return m_synchronizer->hasCheckedForTermination(); } 60 #endif 61 62 protected: 63 DatabaseTask(DatabaseBackend*, TaskSynchronizer*); 64 65 private: 66 virtual void doPerformTask() = 0; 67 virtual void taskCancelled() { } 68 69 RefPtrWillBeCrossThreadPersistent<DatabaseBackend> m_database; 70 TaskSynchronizer* m_synchronizer; 71 72 #if !LOG_DISABLED 73 virtual const char* debugTaskName() const = 0; 74 bool m_complete; 75 #endif 76 }; 77 78 class DatabaseBackend::DatabaseOpenTask FINAL : public DatabaseTask { 79 public: 80 static PassOwnPtr<DatabaseOpenTask> create(DatabaseBackend* db, bool setVersionInNewDatabase, TaskSynchronizer* synchronizer, DatabaseError& error, String& errorMessage, bool& success) 81 { 82 return adoptPtr(new DatabaseOpenTask(db, setVersionInNewDatabase, synchronizer, error, errorMessage, success)); 83 } 84 85 private: 86 DatabaseOpenTask(DatabaseBackend*, bool setVersionInNewDatabase, TaskSynchronizer*, DatabaseError&, String& errorMessage, bool& success); 87 88 virtual void doPerformTask() OVERRIDE; 89 #if !LOG_DISABLED 90 virtual const char* debugTaskName() const OVERRIDE; 91 #endif 92 93 bool m_setVersionInNewDatabase; 94 DatabaseError& m_error; 95 String& m_errorMessage; 96 bool& m_success; 97 }; 98 99 class DatabaseBackend::DatabaseCloseTask FINAL : public DatabaseTask { 100 public: 101 static PassOwnPtr<DatabaseCloseTask> create(DatabaseBackend* db, TaskSynchronizer* synchronizer) 102 { 103 return adoptPtr(new DatabaseCloseTask(db, synchronizer)); 104 } 105 106 private: 107 DatabaseCloseTask(DatabaseBackend*, TaskSynchronizer*); 108 109 virtual void doPerformTask() OVERRIDE; 110 #if !LOG_DISABLED 111 virtual const char* debugTaskName() const OVERRIDE; 112 #endif 113 }; 114 115 class DatabaseBackend::DatabaseTransactionTask FINAL : public DatabaseTask { 116 public: 117 virtual ~DatabaseTransactionTask(); 118 119 // Transaction task is never synchronous, so no 'synchronizer' parameter. 120 static PassOwnPtr<DatabaseTransactionTask> create(PassRefPtrWillBeRawPtr<SQLTransactionBackend> transaction) 121 { 122 return adoptPtr(new DatabaseTransactionTask(transaction)); 123 } 124 125 SQLTransactionBackend* transaction() const { return m_transaction.get(); } 126 127 private: 128 explicit DatabaseTransactionTask(PassRefPtrWillBeRawPtr<SQLTransactionBackend>); 129 130 virtual void doPerformTask() OVERRIDE; 131 virtual void taskCancelled() OVERRIDE; 132 #if !LOG_DISABLED 133 virtual const char* debugTaskName() const OVERRIDE; 134 #endif 135 136 RefPtrWillBeCrossThreadPersistent<SQLTransactionBackend> m_transaction; 137 }; 138 139 class DatabaseBackend::DatabaseTableNamesTask FINAL : public DatabaseTask { 140 public: 141 static PassOwnPtr<DatabaseTableNamesTask> create(DatabaseBackend* db, TaskSynchronizer* synchronizer, Vector<String>& names) 142 { 143 return adoptPtr(new DatabaseTableNamesTask(db, synchronizer, names)); 144 } 145 146 private: 147 DatabaseTableNamesTask(DatabaseBackend*, TaskSynchronizer*, Vector<String>& names); 148 149 virtual void doPerformTask() OVERRIDE; 150 #if !LOG_DISABLED 151 virtual const char* debugTaskName() const OVERRIDE; 152 #endif 153 154 Vector<String>& m_tableNames; 155 }; 156 157 } // namespace WebCore 158 159 #endif // DatabaseTask_h 160