Home | History | Annotate | Download | only in storage
      1 /*
      2  * Copyright (C) 2007, 2008 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 DatabaseTask_h
     29 #define DatabaseTask_h
     30 
     31 #if ENABLE(DATABASE)
     32 #include "Database.h"
     33 #include "ExceptionCode.h"
     34 #include "PlatformString.h"
     35 #include "SQLTransaction.h"
     36 #include <wtf/OwnPtr.h>
     37 #include <wtf/PassOwnPtr.h>
     38 #include <wtf/PassRefPtr.h>
     39 #include <wtf/Threading.h>
     40 #include <wtf/Vector.h>
     41 
     42 namespace WebCore {
     43 
     44 // Can be used to wait until DatabaseTask is completed.
     45 // Has to be passed into DatabaseTask::create to be associated with the task.
     46 class DatabaseTaskSynchronizer {
     47     WTF_MAKE_NONCOPYABLE(DatabaseTaskSynchronizer);
     48 public:
     49     DatabaseTaskSynchronizer();
     50 
     51     // Called from main thread to wait until task is completed.
     52     void waitForTaskCompletion();
     53 
     54     // Called by the task.
     55     void taskCompleted();
     56 
     57 #ifndef NDEBUG
     58     bool hasCheckedForTermination() const { return m_hasCheckedForTermination; }
     59     void setHasCheckedForTermination() { m_hasCheckedForTermination = true; }
     60 #endif
     61 
     62 private:
     63     bool m_taskCompleted;
     64     Mutex m_synchronousMutex;
     65     ThreadCondition m_synchronousCondition;
     66 #ifndef NDEBUG
     67     bool m_hasCheckedForTermination;
     68 #endif
     69 };
     70 
     71 class DatabaseTask {
     72     WTF_MAKE_NONCOPYABLE(DatabaseTask); WTF_MAKE_FAST_ALLOCATED;
     73 public:
     74     virtual ~DatabaseTask();
     75 
     76     void performTask();
     77 
     78     Database* database() const { return m_database; }
     79 #ifndef NDEBUG
     80     bool hasSynchronizer() const { return m_synchronizer; }
     81     bool hasCheckedForTermination() const { return m_synchronizer->hasCheckedForTermination(); }
     82 #endif
     83 
     84 protected:
     85     DatabaseTask(Database*, DatabaseTaskSynchronizer*);
     86 
     87 private:
     88     virtual void doPerformTask() = 0;
     89 
     90     Database* m_database;
     91     DatabaseTaskSynchronizer* m_synchronizer;
     92 
     93 #if !LOG_DISABLED
     94     virtual const char* debugTaskName() const = 0;
     95     bool m_complete;
     96 #endif
     97 };
     98 
     99 class Database::DatabaseOpenTask : public DatabaseTask {
    100 public:
    101     static PassOwnPtr<DatabaseOpenTask> create(Database* db, bool setVersionInNewDatabase, DatabaseTaskSynchronizer* synchronizer, ExceptionCode& code, bool& success)
    102     {
    103         return adoptPtr(new DatabaseOpenTask(db, setVersionInNewDatabase, synchronizer, code, success));
    104     }
    105 
    106 private:
    107     DatabaseOpenTask(Database*, bool setVersionInNewDatabase, DatabaseTaskSynchronizer*, ExceptionCode&, bool& success);
    108 
    109     virtual void doPerformTask();
    110 #if !LOG_DISABLED
    111     virtual const char* debugTaskName() const;
    112 #endif
    113 
    114     bool m_setVersionInNewDatabase;
    115     ExceptionCode& m_code;
    116     bool& m_success;
    117 };
    118 
    119 class Database::DatabaseCloseTask : public DatabaseTask {
    120 public:
    121     static PassOwnPtr<DatabaseCloseTask> create(Database* db, DatabaseTaskSynchronizer* synchronizer)
    122     {
    123         return adoptPtr(new DatabaseCloseTask(db, synchronizer));
    124     }
    125 
    126 private:
    127     DatabaseCloseTask(Database*, DatabaseTaskSynchronizer*);
    128 
    129     virtual void doPerformTask();
    130 #if !LOG_DISABLED
    131     virtual const char* debugTaskName() const;
    132 #endif
    133 };
    134 
    135 class Database::DatabaseTransactionTask : public DatabaseTask {
    136 public:
    137     // Transaction task is never synchronous, so no 'synchronizer' parameter.
    138     static PassOwnPtr<DatabaseTransactionTask> create(PassRefPtr<SQLTransaction> transaction)
    139     {
    140         return adoptPtr(new DatabaseTransactionTask(transaction));
    141     }
    142 
    143     SQLTransaction* transaction() const { return m_transaction.get(); }
    144 
    145 private:
    146     DatabaseTransactionTask(PassRefPtr<SQLTransaction>);
    147 
    148     virtual void doPerformTask();
    149 #if !LOG_DISABLED
    150     virtual const char* debugTaskName() const;
    151 #endif
    152 
    153     RefPtr<SQLTransaction> m_transaction;
    154 };
    155 
    156 class Database::DatabaseTableNamesTask : public DatabaseTask {
    157 public:
    158     static PassOwnPtr<DatabaseTableNamesTask> create(Database* db, DatabaseTaskSynchronizer* synchronizer, Vector<String>& names)
    159     {
    160         return adoptPtr(new DatabaseTableNamesTask(db, synchronizer, names));
    161     }
    162 
    163 private:
    164     DatabaseTableNamesTask(Database*, DatabaseTaskSynchronizer*, Vector<String>& names);
    165 
    166     virtual void doPerformTask();
    167 #if !LOG_DISABLED
    168     virtual const char* debugTaskName() const;
    169 #endif
    170 
    171     Vector<String>& m_tableNames;
    172 };
    173 
    174 } // namespace WebCore
    175 
    176 #endif // ENABLE(DATABASE)
    177 #endif // DatabaseTask_h
    178