| 1 | /* |
| 2 | * Copyright (C) 2007, 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 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 "SQLCallbackWrapper.h" |
| 33 | #include "SQLTransactionBackend.h" |
| 34 | #include "SQLTransactionStateMachine.h" |
| 35 | #include "SQLValue.h" |
| 36 | #include <wtf/Deque.h> |
| 37 | #include <wtf/Lock.h> |
| 38 | |
| 39 | namespace WebCore { |
| 40 | |
| 41 | class Database; |
| 42 | class SQLError; |
| 43 | class SQLStatementCallback; |
| 44 | class SQLStatementErrorCallback; |
| 45 | class SQLTransactionBackend; |
| 46 | class SQLTransactionCallback; |
| 47 | class SQLTransactionErrorCallback; |
| 48 | class VoidCallback; |
| 49 | |
| 50 | class SQLTransactionWrapper : public ThreadSafeRefCounted<SQLTransactionWrapper> { |
| 51 | public: |
| 52 | virtual ~SQLTransactionWrapper() = default; |
| 53 | virtual bool performPreflight(SQLTransaction&) = 0; |
| 54 | virtual bool performPostflight(SQLTransaction&) = 0; |
| 55 | virtual SQLError* sqlError() const = 0; |
| 56 | virtual void handleCommitFailedAfterPostflight(SQLTransaction&) = 0; |
| 57 | }; |
| 58 | |
| 59 | class SQLTransaction : public ThreadSafeRefCounted<SQLTransaction>, public SQLTransactionStateMachine<SQLTransaction> { |
| 60 | public: |
| 61 | static Ref<SQLTransaction> create(Ref<Database>&&, RefPtr<SQLTransactionCallback>&&, RefPtr<VoidCallback>&& successCallback, RefPtr<SQLTransactionErrorCallback>&&, RefPtr<SQLTransactionWrapper>&&, bool readOnly); |
| 62 | ~SQLTransaction(); |
| 63 | |
| 64 | ExceptionOr<void> executeSql(const String& sqlStatement, Optional<Vector<SQLValue>>&& arguments, RefPtr<SQLStatementCallback>&&, RefPtr<SQLStatementErrorCallback>&&); |
| 65 | |
| 66 | void lockAcquired(); |
| 67 | void performNextStep(); |
| 68 | void performPendingCallback(); |
| 69 | |
| 70 | Database& database() { return m_database; } |
| 71 | bool isReadOnly() const { return m_readOnly; } |
| 72 | void notifyDatabaseThreadIsShuttingDown(); |
| 73 | |
| 74 | // APIs called from the backend published via SQLTransaction: |
| 75 | void requestTransitToState(SQLTransactionState); |
| 76 | |
| 77 | private: |
| 78 | friend class SQLTransactionBackend; |
| 79 | |
| 80 | SQLTransaction(Ref<Database>&&, RefPtr<SQLTransactionCallback>&&, RefPtr<VoidCallback>&& successCallback, RefPtr<SQLTransactionErrorCallback>&&, RefPtr<SQLTransactionWrapper>&&, bool readOnly); |
| 81 | |
| 82 | void enqueueStatement(std::unique_ptr<SQLStatement>); |
| 83 | |
| 84 | void checkAndHandleClosedDatabase(); |
| 85 | |
| 86 | void clearCallbackWrappers(); |
| 87 | |
| 88 | void scheduleCallback(void (SQLTransaction::*)()); |
| 89 | |
| 90 | // State Machine functions: |
| 91 | StateFunction stateFunctionFor(SQLTransactionState) override; |
| 92 | void computeNextStateAndCleanupIfNeeded(); |
| 93 | |
| 94 | // State functions: |
| 95 | void acquireLock(); |
| 96 | void openTransactionAndPreflight(); |
| 97 | void runStatements(); |
| 98 | void cleanupAndTerminate(); |
| 99 | void cleanupAfterTransactionErrorCallback(); |
| 100 | void deliverTransactionCallback(); |
| 101 | void deliverTransactionErrorCallback(); |
| 102 | void deliverStatementCallback(); |
| 103 | void deliverQuotaIncreaseCallback(); |
| 104 | void deliverSuccessCallback(); |
| 105 | |
| 106 | NO_RETURN_DUE_TO_ASSERT void unreachableState(); |
| 107 | |
| 108 | void getNextStatement(); |
| 109 | bool runCurrentStatement(); |
| 110 | void handleCurrentStatementError(); |
| 111 | void handleTransactionError(); |
| 112 | void postflightAndCommit(); |
| 113 | |
| 114 | void acquireOriginLock(); |
| 115 | void releaseOriginLockIfNeeded(); |
| 116 | |
| 117 | #if !LOG_DISABLED |
| 118 | static const char* debugStepName(void (SQLTransaction::*)()); |
| 119 | #endif |
| 120 | |
| 121 | Ref<Database> m_database; |
| 122 | SQLCallbackWrapper<SQLTransactionCallback> m_callbackWrapper; |
| 123 | SQLCallbackWrapper<VoidCallback> m_successCallbackWrapper; |
| 124 | SQLCallbackWrapper<SQLTransactionErrorCallback> m_errorCallbackWrapper; |
| 125 | |
| 126 | RefPtr<SQLTransactionWrapper> m_wrapper; |
| 127 | |
| 128 | void (SQLTransaction::*m_nextStep)(); |
| 129 | |
| 130 | bool m_executeSqlAllowed { false }; |
| 131 | RefPtr<SQLError> m_transactionError; |
| 132 | |
| 133 | bool m_shouldRetryCurrentStatement { false }; |
| 134 | bool m_modifiedDatabase { false }; |
| 135 | bool m_lockAcquired { false }; |
| 136 | bool m_readOnly { false }; |
| 137 | bool m_hasVersionMismatch { false }; |
| 138 | |
| 139 | Lock m_statementMutex; |
| 140 | Deque<std::unique_ptr<SQLStatement>> m_statementQueue; |
| 141 | |
| 142 | std::unique_ptr<SQLStatement> m_currentStatement; |
| 143 | |
| 144 | std::unique_ptr<SQLiteTransaction> m_sqliteTransaction; |
| 145 | RefPtr<OriginLock> m_originLock; |
| 146 | |
| 147 | SQLTransactionBackend m_backend; |
| 148 | }; |
| 149 | |
| 150 | } // namespace WebCore |
| 151 | |