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