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