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