Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / modules / webdatabase / SQLTransactionBackend.h
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 #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"
40
41 namespace blink {
42
43 class AbstractSQLTransaction;
44 class DatabaseBackend;
45 class SQLErrorData;
46 class SQLiteTransaction;
47 class SQLStatementBackend;
48 class SQLTransactionBackend;
49 class SQLValue;
50
51 class SQLTransactionWrapper : public ThreadSafeRefCountedWillBeGarbageCollectedFinalized<SQLTransactionWrapper> {
52 public:
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;
59 };
60
61 class SQLTransactionBackend FINAL : public AbstractSQLTransactionBackend, public SQLTransactionStateMachine<SQLTransactionBackend> {
62 public:
63     static PassRefPtrWillBeRawPtr<SQLTransactionBackend> create(DatabaseBackend*,
64         PassRefPtrWillBeRawPtr<AbstractSQLTransaction>, PassRefPtrWillBeRawPtr<SQLTransactionWrapper>, bool readOnly);
65
66     virtual ~SQLTransactionBackend();
67     virtual void trace(Visitor*) OVERRIDE;
68
69     void lockAcquired();
70     void performNextStep();
71
72     DatabaseBackend* database() { return m_database.get(); }
73     bool isReadOnly() { return m_readOnly; }
74     void notifyDatabaseThreadIsShuttingDown();
75
76 private:
77     SQLTransactionBackend(DatabaseBackend*, PassRefPtrWillBeRawPtr<AbstractSQLTransaction>,
78         PassRefPtrWillBeRawPtr<SQLTransactionWrapper>, bool readOnly);
79
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;
87
88     void doCleanup();
89
90     void enqueueStatementBackend(PassRefPtrWillBeRawPtr<SQLStatementBackend>);
91
92     // State Machine functions:
93     virtual StateFunction stateFunctionFor(SQLTransactionState) OVERRIDE;
94     void computeNextStateAndCleanupIfNeeded();
95
96     // State functions:
97     SQLTransactionState acquireLock();
98     SQLTransactionState openTransactionAndPreflight();
99     SQLTransactionState runStatements();
100     SQLTransactionState postflightAndCommit();
101     SQLTransactionState cleanupAndTerminate();
102     SQLTransactionState cleanupAfterTransactionErrorCallback();
103
104     SQLTransactionState unreachableState();
105     SQLTransactionState sendToFrontendState();
106
107     SQLTransactionState nextStateForCurrentStatementError();
108     SQLTransactionState nextStateForTransactionError();
109     SQLTransactionState runCurrentStatementAndGetNextState();
110
111     void getNextStatement();
112
113     RefPtrWillBeMember<AbstractSQLTransaction> m_frontend; // Has a reference cycle, and will break in doCleanup().
114     RefPtrWillBeMember<SQLStatementBackend> m_currentStatementBackend;
115
116     RefPtrWillBeMember<DatabaseBackend> m_database;
117     RefPtrWillBeMember<SQLTransactionWrapper> m_wrapper;
118     OwnPtr<SQLErrorData> m_transactionError;
119
120     bool m_hasCallback;
121     bool m_hasSuccessCallback;
122     bool m_hasErrorCallback;
123     bool m_shouldRetryCurrentStatement;
124     bool m_modifiedDatabase;
125     bool m_lockAcquired;
126     bool m_readOnly;
127     bool m_hasVersionMismatch;
128
129     Mutex m_statementMutex;
130     Deque<RefPtrWillBeMember<SQLStatementBackend> > m_statementQueue;
131
132     OwnPtr<SQLiteTransaction> m_sqliteTransaction;
133 };
134
135 } // namespace blink
136
137 #endif // SQLTransactionBackend_h