1/*
2 * Copyright (C) 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 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#ifndef SQLTransactionStateMachine_h
27#define SQLTransactionStateMachine_h
28
29#if ENABLE(SQL_DATABASE)
30
31#include "SQLTransactionState.h"
32#include <wtf/ThreadSafeRefCounted.h>
33
34namespace WebCore {
35
36template<typename T>
37class SQLTransactionStateMachine {
38public:
39    virtual ~SQLTransactionStateMachine() { }
40
41protected:
42    SQLTransactionStateMachine();
43
44    typedef SQLTransactionState (T::* StateFunction)();
45    virtual StateFunction stateFunctionFor(SQLTransactionState) = 0;
46
47    void setStateToRequestedState();
48    void runStateMachine();
49
50    SQLTransactionState m_nextState;
51    SQLTransactionState m_requestedState;
52
53#ifndef NDEBUG
54    // The state audit trail (i.e. bread crumbs) keeps track of up to the last
55    // s_sizeOfStateAuditTrail states that the state machine enters. The audit
56    // trail is updated before entering each state. This is for debugging use
57    // only.
58    static const int s_sizeOfStateAuditTrail = 20;
59    int m_nextStateAuditEntry;
60    SQLTransactionState m_stateAuditTrail[s_sizeOfStateAuditTrail];
61#endif
62};
63
64#if !LOG_DISABLED
65extern const char* nameForSQLTransactionState(SQLTransactionState);
66#endif
67
68template<typename T>
69SQLTransactionStateMachine<T>::SQLTransactionStateMachine()
70    : m_nextState(SQLTransactionState::Idle)
71    , m_requestedState(SQLTransactionState::Idle)
72#ifndef NDEBUG
73    , m_nextStateAuditEntry(0)
74#endif
75{
76#ifndef NDEBUG
77    for (int i = 0; i < s_sizeOfStateAuditTrail; i++)
78        m_stateAuditTrail[i] = SQLTransactionState::NumberOfStates;
79#endif
80}
81
82template<typename T>
83void SQLTransactionStateMachine<T>::setStateToRequestedState()
84{
85    ASSERT(m_nextState == SQLTransactionState::Idle);
86    ASSERT(m_requestedState != SQLTransactionState::Idle);
87    m_nextState = m_requestedState;
88    m_requestedState = SQLTransactionState::Idle;
89}
90
91template<typename T>
92void SQLTransactionStateMachine<T>::runStateMachine()
93{
94    ASSERT(SQLTransactionState::End < SQLTransactionState::Idle);
95    while (m_nextState > SQLTransactionState::Idle) {
96        ASSERT(m_nextState < SQLTransactionState::NumberOfStates);
97        StateFunction stateFunction = stateFunctionFor(m_nextState);
98        ASSERT(stateFunction);
99
100#ifndef NDEBUG
101        m_stateAuditTrail[m_nextStateAuditEntry] = m_nextState;
102        m_nextStateAuditEntry = (m_nextStateAuditEntry + 1) % s_sizeOfStateAuditTrail;
103#endif
104        m_nextState = (static_cast<T*>(this)->*stateFunction)();
105    }
106}
107
108} // namespace WebCore
109
110#endif // ENABLE(SQL_DATABASE)
111
112#endif // SQLTransactionStateMachine_h
113