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