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