2 * Copyright (C) 2007, 2013 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
28 #ifndef SQLTransactionBackend_h
29 #define SQLTransactionBackend_h
31 #include "modules/webdatabase/AbstractSQLStatement.h"
32 #include "modules/webdatabase/AbstractSQLTransactionBackend.h"
33 #include "modules/webdatabase/DatabaseBasicTypes.h"
34 #include "modules/webdatabase/SQLTransactionStateMachine.h"
35 #include "platform/heap/Handle.h"
36 #include "wtf/Deque.h"
37 #include "wtf/Forward.h"
38 #include "wtf/ThreadingPrimitives.h"
39 #include "wtf/text/WTFString.h"
43 class AbstractSQLTransaction;
44 class DatabaseBackend;
46 class SQLiteTransaction;
47 class SQLStatementBackend;
48 class SQLTransactionBackend;
51 class SQLTransactionWrapper : public ThreadSafeRefCountedWillBeGarbageCollectedFinalized<SQLTransactionWrapper> {
53 virtual ~SQLTransactionWrapper() { }
54 virtual void trace(Visitor*) { }
55 virtual bool performPreflight(SQLTransactionBackend*) = 0;
56 virtual bool performPostflight(SQLTransactionBackend*) = 0;
57 virtual SQLErrorData* sqlError() const = 0;
58 virtual void handleCommitFailedAfterPostflight(SQLTransactionBackend*) = 0;
61 class SQLTransactionBackend FINAL : public AbstractSQLTransactionBackend, public SQLTransactionStateMachine<SQLTransactionBackend> {
63 static PassRefPtrWillBeRawPtr<SQLTransactionBackend> create(DatabaseBackend*,
64 PassRefPtrWillBeRawPtr<AbstractSQLTransaction>, PassRefPtrWillBeRawPtr<SQLTransactionWrapper>, bool readOnly);
66 virtual ~SQLTransactionBackend();
67 virtual void trace(Visitor*) OVERRIDE;
70 void performNextStep();
72 DatabaseBackend* database() { return m_database.get(); }
73 bool isReadOnly() { return m_readOnly; }
74 void notifyDatabaseThreadIsShuttingDown();
77 SQLTransactionBackend(DatabaseBackend*, PassRefPtrWillBeRawPtr<AbstractSQLTransaction>,
78 PassRefPtrWillBeRawPtr<SQLTransactionWrapper>, bool readOnly);
80 // APIs called from the frontend published via AbstractSQLTransactionBackend:
81 virtual void requestTransitToState(SQLTransactionState) OVERRIDE;
82 virtual SQLErrorData* transactionError() OVERRIDE;
83 virtual AbstractSQLStatement* currentStatement() OVERRIDE;
84 virtual void setShouldRetryCurrentStatement(bool) OVERRIDE;
85 virtual void executeSQL(PassOwnPtrWillBeRawPtr<AbstractSQLStatement>, const String& statement,
86 const Vector<SQLValue>& arguments, int permissions) OVERRIDE;
90 void enqueueStatementBackend(PassRefPtrWillBeRawPtr<SQLStatementBackend>);
92 // State Machine functions:
93 virtual StateFunction stateFunctionFor(SQLTransactionState) OVERRIDE;
94 void computeNextStateAndCleanupIfNeeded();
97 SQLTransactionState acquireLock();
98 SQLTransactionState openTransactionAndPreflight();
99 SQLTransactionState runStatements();
100 SQLTransactionState postflightAndCommit();
101 SQLTransactionState cleanupAndTerminate();
102 SQLTransactionState cleanupAfterTransactionErrorCallback();
104 SQLTransactionState unreachableState();
105 SQLTransactionState sendToFrontendState();
107 SQLTransactionState nextStateForCurrentStatementError();
108 SQLTransactionState nextStateForTransactionError();
109 SQLTransactionState runCurrentStatementAndGetNextState();
111 void getNextStatement();
113 RefPtrWillBeMember<AbstractSQLTransaction> m_frontend; // Has a reference cycle, and will break in doCleanup().
114 RefPtrWillBeMember<SQLStatementBackend> m_currentStatementBackend;
116 RefPtrWillBeMember<DatabaseBackend> m_database;
117 RefPtrWillBeMember<SQLTransactionWrapper> m_wrapper;
118 OwnPtr<SQLErrorData> m_transactionError;
121 bool m_hasSuccessCallback;
122 bool m_hasErrorCallback;
123 bool m_shouldRetryCurrentStatement;
124 bool m_modifiedDatabase;
127 bool m_hasVersionMismatch;
129 Mutex m_statementMutex;
130 Deque<RefPtrWillBeMember<SQLStatementBackend> > m_statementQueue;
132 OwnPtr<SQLiteTransaction> m_sqliteTransaction;
137 #endif // SQLTransactionBackend_h