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 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 SQLTransactionBackend_h
29#define SQLTransactionBackend_h
30
31#if ENABLE(SQL_DATABASE)
32
33#include "AbstractSQLStatement.h"
34#include "AbstractSQLTransactionBackend.h"
35#include "DatabaseBasicTypes.h"
36#include "SQLTransactionStateMachine.h"
37#include <wtf/Deque.h>
38#include <wtf/Forward.h>
39#include <wtf/text/WTFString.h>
40
41namespace WebCore {
42
43class AbstractSQLTransaction;
44class DatabaseBackend;
45class OriginLock;
46class SQLError;
47class SQLiteTransaction;
48class SQLStatementBackend;
49class SQLTransactionBackend;
50class SQLValue;
51
52class SQLTransactionWrapper : public ThreadSafeRefCounted<SQLTransactionWrapper> {
53public:
54    virtual ~SQLTransactionWrapper() { }
55    virtual bool performPreflight(SQLTransactionBackend*) = 0;
56    virtual bool performPostflight(SQLTransactionBackend*) = 0;
57    virtual SQLError* sqlError() const = 0;
58    virtual void handleCommitFailedAfterPostflight(SQLTransactionBackend*) = 0;
59};
60
61class SQLTransactionBackend : public SQLTransactionStateMachine<SQLTransactionBackend>, public AbstractSQLTransactionBackend {
62public:
63    static PassRefPtr<SQLTransactionBackend> create(DatabaseBackend*,
64        PassRefPtr<AbstractSQLTransaction>, PassRefPtr<SQLTransactionWrapper>, bool readOnly);
65
66    virtual ~SQLTransactionBackend();
67
68    void lockAcquired();
69    void performNextStep();
70
71    DatabaseBackend* database() { return m_database.get(); }
72    bool isReadOnly() { return m_readOnly; }
73    void notifyDatabaseThreadIsShuttingDown();
74
75private:
76    SQLTransactionBackend(DatabaseBackend*, PassRefPtr<AbstractSQLTransaction>,
77        PassRefPtr<SQLTransactionWrapper>, bool readOnly);
78
79    // APIs called from the frontend published via AbstractSQLTransactionBackend:
80    virtual void requestTransitToState(SQLTransactionState) OVERRIDE;
81    virtual PassRefPtr<SQLError> transactionError() OVERRIDE;
82    virtual AbstractSQLStatement* currentStatement() OVERRIDE;
83    virtual void setShouldRetryCurrentStatement(bool) OVERRIDE;
84    virtual void executeSQL(PassOwnPtr<AbstractSQLStatement>, const String& statement,
85        const Vector<SQLValue>& arguments, int permissions) OVERRIDE;
86
87    void doCleanup();
88
89    void enqueueStatementBackend(PassRefPtr<SQLStatementBackend>);
90
91    // State Machine functions:
92    virtual StateFunction stateFunctionFor(SQLTransactionState) OVERRIDE;
93    void computeNextStateAndCleanupIfNeeded();
94
95    // State functions:
96    SQLTransactionState acquireLock();
97    SQLTransactionState openTransactionAndPreflight();
98    SQLTransactionState runStatements();
99    SQLTransactionState postflightAndCommit();
100    SQLTransactionState cleanupAndTerminate();
101    SQLTransactionState cleanupAfterTransactionErrorCallback();
102
103    SQLTransactionState unreachableState();
104    SQLTransactionState sendToFrontendState();
105
106    SQLTransactionState nextStateForCurrentStatementError();
107    SQLTransactionState nextStateForTransactionError();
108    SQLTransactionState runCurrentStatementAndGetNextState();
109
110    void getNextStatement();
111
112    void acquireOriginLock();
113    void releaseOriginLockIfNeeded();
114
115    RefPtr<AbstractSQLTransaction> m_frontend; // Has a reference cycle, and will break in doCleanup().
116    RefPtr<SQLStatementBackend> m_currentStatementBackend;
117
118    RefPtr<DatabaseBackend> m_database;
119    RefPtr<SQLTransactionWrapper> m_wrapper;
120    RefPtr<SQLError> m_transactionError;
121
122    bool m_hasCallback;
123    bool m_hasSuccessCallback;
124    bool m_hasErrorCallback;
125    bool m_shouldRetryCurrentStatement;
126    bool m_modifiedDatabase;
127    bool m_lockAcquired;
128    bool m_readOnly;
129    bool m_hasVersionMismatch;
130
131    Mutex m_statementMutex;
132    Deque<RefPtr<SQLStatementBackend> > m_statementQueue;
133
134    OwnPtr<SQLiteTransaction> m_sqliteTransaction;
135    RefPtr<OriginLock> m_originLock;
136};
137
138} // namespace WebCore
139
140#endif
141
142#endif // SQLTransactionBackend_h
143