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 #include "config.h"
     29 #include "DatabaseTask.h"
     30 
     31 #if ENABLE(DATABASE)
     32 
     33 #include "Database.h"
     34 #include "Logging.h"
     35 
     36 namespace WebCore {
     37 
     38 DatabaseTaskSynchronizer::DatabaseTaskSynchronizer()
     39     : m_taskCompleted(false)
     40 {
     41 }
     42 
     43 void DatabaseTaskSynchronizer::waitForTaskCompletion()
     44 {
     45     m_synchronousMutex.lock();
     46     if (!m_taskCompleted)
     47         m_synchronousCondition.wait(m_synchronousMutex);
     48     m_synchronousMutex.unlock();
     49 }
     50 
     51 void DatabaseTaskSynchronizer::taskCompleted()
     52 {
     53     m_synchronousMutex.lock();
     54     m_taskCompleted = true;
     55     m_synchronousCondition.signal();
     56     m_synchronousMutex.unlock();
     57 }
     58 
     59 DatabaseTask::DatabaseTask(Database* database, DatabaseTaskSynchronizer* synchronizer)
     60     : m_database(database)
     61     , m_synchronizer(synchronizer)
     62 #ifndef NDEBUG
     63     , m_complete(false)
     64 #endif
     65 {
     66 }
     67 
     68 DatabaseTask::~DatabaseTask()
     69 {
     70 }
     71 
     72 void DatabaseTask::performTask()
     73 {
     74     // Database tasks are meant to be used only once, so make sure this one hasn't been performed before.
     75     ASSERT(!m_complete);
     76 
     77     LOG(StorageAPI, "Performing %s %p\n", debugTaskName(), this);
     78 
     79     m_database->resetAuthorizer();
     80     doPerformTask();
     81     m_database->performPolicyChecks();
     82 
     83     if (m_synchronizer)
     84         m_synchronizer->taskCompleted();
     85 }
     86 
     87 // *** DatabaseOpenTask ***
     88 // Opens the database file and verifies the version matches the expected version.
     89 
     90 DatabaseOpenTask::DatabaseOpenTask(Database* database, DatabaseTaskSynchronizer* synchronizer, ExceptionCode& code, bool& success)
     91     : DatabaseTask(database, synchronizer)
     92     , m_code(code)
     93     , m_success(success)
     94 {
     95     ASSERT(synchronizer); // A task with output parameters is supposed to be synchronous.
     96 }
     97 
     98 void DatabaseOpenTask::doPerformTask()
     99 {
    100     m_success = database()->performOpenAndVerify(m_code);
    101 }
    102 
    103 #ifndef NDEBUG
    104 const char* DatabaseOpenTask::debugTaskName() const
    105 {
    106     return "DatabaseOpenTask";
    107 }
    108 #endif
    109 
    110 // *** DatabaseCloseTask ***
    111 // Closes the database.
    112 
    113 DatabaseCloseTask::DatabaseCloseTask(Database* database, DatabaseTaskSynchronizer* synchronizer)
    114     : DatabaseTask(database, synchronizer)
    115 {
    116 }
    117 
    118 void DatabaseCloseTask::doPerformTask()
    119 {
    120     database()->close();
    121 }
    122 
    123 #ifndef NDEBUG
    124 const char* DatabaseCloseTask::debugTaskName() const
    125 {
    126     return "DatabaseCloseTask";
    127 }
    128 #endif
    129 
    130 // *** DatabaseTransactionTask ***
    131 // Starts a transaction that will report its results via a callback.
    132 
    133 DatabaseTransactionTask::DatabaseTransactionTask(PassRefPtr<SQLTransaction> transaction)
    134     : DatabaseTask(transaction->database(), 0)
    135     , m_transaction(transaction)
    136 {
    137 }
    138 
    139 DatabaseTransactionTask::~DatabaseTransactionTask()
    140 {
    141 }
    142 
    143 void DatabaseTransactionTask::doPerformTask()
    144 {
    145     if (m_transaction->performNextStep()) {
    146         // The transaction is complete, we can move on to the next one.
    147         MutexLocker locker(m_transaction->database()->m_transactionInProgressMutex);
    148         m_transaction->database()->scheduleTransaction();
    149     }
    150 }
    151 
    152 #ifndef NDEBUG
    153 const char* DatabaseTransactionTask::debugTaskName() const
    154 {
    155     return "DatabaseTransactionTask";
    156 }
    157 #endif
    158 
    159 // *** DatabaseTableNamesTask ***
    160 // Retrieves a list of all tables in the database - for WebInspector support.
    161 
    162 DatabaseTableNamesTask::DatabaseTableNamesTask(Database* database, DatabaseTaskSynchronizer* synchronizer, Vector<String>& names)
    163     : DatabaseTask(database, synchronizer)
    164     , m_tableNames(names)
    165 {
    166     ASSERT(synchronizer); // A task with output parameters is supposed to be synchronous.
    167 }
    168 
    169 void DatabaseTableNamesTask::doPerformTask()
    170 {
    171     m_tableNames = database()->performGetTableNames();
    172 }
    173 
    174 #ifndef NDEBUG
    175 const char* DatabaseTableNamesTask::debugTaskName() const
    176 {
    177     return "DatabaseTableNamesTask";
    178 }
    179 #endif
    180 
    181 } // namespace WebCore
    182 
    183 #endif
    184