1/* 2 * Copyright (C) 2010 Google 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 are 6 * met: 7 * 8 * * Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * * Redistributions in binary form must reproduce the above 11 * copyright notice, this list of conditions and the following disclaimer 12 * in the documentation and/or other materials provided with the 13 * distribution. 14 * * Neither the name of Google Inc. nor the names of its 15 * contributors may be used to endorse or promote products derived from 16 * this software without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 */ 30 31#include "config.h" 32#include "DatabaseSync.h" 33 34#if ENABLE(SQL_DATABASE) 35 36#include "DatabaseBackendContext.h" 37#include "DatabaseBackendSync.h" 38#include "DatabaseCallback.h" 39#include "DatabaseContext.h" 40#include "DatabaseManager.h" 41#include "DatabaseTracker.h" 42#include "Logging.h" 43#include "SQLException.h" 44#include "SQLTransactionSync.h" 45#include "SQLTransactionSyncCallback.h" 46#include "ScriptExecutionContext.h" 47#include "SecurityOrigin.h" 48#include <wtf/PassRefPtr.h> 49#include <wtf/RefPtr.h> 50#include <wtf/text/CString.h> 51 52namespace WebCore { 53 54PassRefPtr<DatabaseSync> DatabaseSync::create(ScriptExecutionContext*, PassRefPtr<DatabaseBackendBase> backend) 55{ 56 return static_cast<DatabaseSync*>(backend.get()); 57} 58 59DatabaseSync::DatabaseSync(PassRefPtr<DatabaseBackendContext> databaseContext, 60 const String& name, const String& expectedVersion, const String& displayName, unsigned long estimatedSize) 61 : DatabaseBase(databaseContext->scriptExecutionContext()) 62 , DatabaseBackendSync(databaseContext, name, expectedVersion, displayName, estimatedSize) 63{ 64 setFrontend(this); 65} 66 67DatabaseSync::~DatabaseSync() 68{ 69 ASSERT(m_scriptExecutionContext->isContextThread()); 70} 71 72PassRefPtr<DatabaseBackendSync> DatabaseSync::backend() 73{ 74 return this; 75} 76 77void DatabaseSync::changeVersion(const String& oldVersion, const String& newVersion, PassRefPtr<SQLTransactionSyncCallback> changeVersionCallback, ExceptionCode& ec) 78{ 79 ASSERT(m_scriptExecutionContext->isContextThread()); 80 81 if (sqliteDatabase().transactionInProgress()) { 82 setLastErrorMessage("unable to changeVersion from within a transaction"); 83 ec = SQLException::DATABASE_ERR; 84 return; 85 } 86 87 RefPtr<SQLTransactionSync> transaction = SQLTransactionSync::create(this, changeVersionCallback, false); 88 if ((ec = transaction->begin())) { 89 ASSERT(!lastErrorMessage().isEmpty()); 90 return; 91 } 92 93 String actualVersion; 94 if (!getVersionFromDatabase(actualVersion)) { 95 setLastErrorMessage("unable to read the current version", sqliteDatabase().lastError(), sqliteDatabase().lastErrorMsg()); 96 ec = SQLException::UNKNOWN_ERR; 97 return; 98 } 99 100 if (actualVersion != oldVersion) { 101 setLastErrorMessage("current version of the database and `oldVersion` argument do not match"); 102 ec = SQLException::VERSION_ERR; 103 return; 104 } 105 106 if ((ec = transaction->execute())) { 107 ASSERT(!lastErrorMessage().isEmpty()); 108 return; 109 } 110 111 if (!setVersionInDatabase(newVersion)) { 112 setLastErrorMessage("unable to set the new version", sqliteDatabase().lastError(), sqliteDatabase().lastErrorMsg()); 113 ec = SQLException::UNKNOWN_ERR; 114 return; 115 } 116 117 if ((ec = transaction->commit())) { 118 ASSERT(!lastErrorMessage().isEmpty()); 119 setCachedVersion(oldVersion); 120 return; 121 } 122 123 setExpectedVersion(newVersion); 124 setLastErrorMessage(""); 125} 126 127void DatabaseSync::transaction(PassRefPtr<SQLTransactionSyncCallback> callback, ExceptionCode& ec) 128{ 129 runTransaction(callback, false, ec); 130} 131 132void DatabaseSync::readTransaction(PassRefPtr<SQLTransactionSyncCallback> callback, ExceptionCode& ec) 133{ 134 runTransaction(callback, true, ec); 135} 136 137void DatabaseSync::runTransaction(PassRefPtr<SQLTransactionSyncCallback> callback, bool readOnly, ExceptionCode& ec) 138{ 139 ASSERT(m_scriptExecutionContext->isContextThread()); 140 141 if (sqliteDatabase().transactionInProgress()) { 142 setLastErrorMessage("unable to start a transaction from within a transaction"); 143 ec = SQLException::DATABASE_ERR; 144 return; 145 } 146 147 RefPtr<SQLTransactionSync> transaction = SQLTransactionSync::create(this, callback, readOnly); 148 if ((ec = transaction->begin()) || (ec = transaction->execute()) || (ec = transaction->commit())) { 149 ASSERT(!lastErrorMessage().isEmpty()); 150 transaction->rollback(); 151 } 152 153 setLastErrorMessage(""); 154} 155 156void DatabaseSync::markAsDeletedAndClose() 157{ 158 // FIXME: need to do something similar to closeImmediately(), but in a sync way 159} 160 161void DatabaseSync::closeImmediately() 162{ 163 ASSERT(m_scriptExecutionContext->isContextThread()); 164 165 if (!opened()) 166 return; 167 168 logErrorMessage("forcibly closing database"); 169 closeDatabase(); 170} 171 172} // namespace WebCore 173 174#endif // ENABLE(SQL_DATABASE) 175