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 "modules/webdatabase/DatabaseSync.h" 33 34 #include "bindings/v8/ExceptionState.h" 35 #include "core/dom/ExceptionCode.h" 36 #include "core/dom/ScriptExecutionContext.h" 37 #include "core/platform/Logging.h" 38 #include "modules/webdatabase/DatabaseBackendContext.h" 39 #include "modules/webdatabase/DatabaseBackendSync.h" 40 #include "modules/webdatabase/DatabaseCallback.h" 41 #include "modules/webdatabase/DatabaseContext.h" 42 #include "modules/webdatabase/DatabaseManager.h" 43 #include "modules/webdatabase/DatabaseTracker.h" 44 #include "modules/webdatabase/SQLError.h" 45 #include "modules/webdatabase/SQLTransactionSync.h" 46 #include "modules/webdatabase/SQLTransactionSyncCallback.h" 47 #include "weborigin/SecurityOrigin.h" 48 #include "wtf/PassRefPtr.h" 49 #include "wtf/RefPtr.h" 50 #include "wtf/text/CString.h" 51 52 namespace WebCore { 53 54 PassRefPtr<DatabaseSync> DatabaseSync::create(ScriptExecutionContext*, PassRefPtr<DatabaseBackendBase> backend) 55 { 56 return static_cast<DatabaseSync*>(backend.get()); 57 } 58 59 DatabaseSync::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 ScriptWrappable::init(this); 65 setFrontend(this); 66 } 67 68 DatabaseSync::~DatabaseSync() 69 { 70 ASSERT(m_scriptExecutionContext->isContextThread()); 71 } 72 73 PassRefPtr<DatabaseBackendSync> DatabaseSync::backend() 74 { 75 return this; 76 } 77 78 void DatabaseSync::changeVersion(const String& oldVersion, const String& newVersion, PassRefPtr<SQLTransactionSyncCallback> changeVersionCallback, ExceptionState& es) 79 { 80 ASSERT(m_scriptExecutionContext->isContextThread()); 81 82 if (sqliteDatabase().transactionInProgress()) { 83 reportChangeVersionResult(1, SQLError::DATABASE_ERR, 0); 84 setLastErrorMessage("unable to changeVersion from within a transaction"); 85 es.throwDOMException(SQLDatabaseError); 86 return; 87 } 88 89 RefPtr<SQLTransactionSync> transaction = SQLTransactionSync::create(this, changeVersionCallback, false); 90 transaction->begin(es); 91 if (es.hadException()) { 92 ASSERT(!lastErrorMessage().isEmpty()); 93 return; 94 } 95 96 String actualVersion; 97 if (!getVersionFromDatabase(actualVersion)) { 98 reportChangeVersionResult(2, SQLError::UNKNOWN_ERR, sqliteDatabase().lastError()); 99 setLastErrorMessage("unable to read the current version", sqliteDatabase().lastError(), sqliteDatabase().lastErrorMsg()); 100 es.throwDOMException(UnknownError, SQLError::unknownErrorMessage); 101 return; 102 } 103 104 if (actualVersion != oldVersion) { 105 reportChangeVersionResult(3, SQLError::VERSION_ERR, 0); 106 setLastErrorMessage("current version of the database and `oldVersion` argument do not match"); 107 es.throwDOMException(VersionError, SQLError::versionErrorMessage); 108 return; 109 } 110 111 112 transaction->execute(es); 113 if (es.hadException()) { 114 ASSERT(!lastErrorMessage().isEmpty()); 115 return; 116 } 117 118 if (!setVersionInDatabase(newVersion)) { 119 reportChangeVersionResult(4, SQLError::UNKNOWN_ERR, sqliteDatabase().lastError()); 120 setLastErrorMessage("unable to set the new version", sqliteDatabase().lastError(), sqliteDatabase().lastErrorMsg()); 121 es.throwDOMException(UnknownError, SQLError::unknownErrorMessage); 122 return; 123 } 124 125 transaction->commit(es); 126 if (es.hadException()) { 127 ASSERT(!lastErrorMessage().isEmpty()); 128 setCachedVersion(oldVersion); 129 return; 130 } 131 132 reportChangeVersionResult(0, -1, 0); // OK 133 134 setExpectedVersion(newVersion); 135 setLastErrorMessage(""); 136 } 137 138 void DatabaseSync::transaction(PassRefPtr<SQLTransactionSyncCallback> callback, ExceptionState& es) 139 { 140 runTransaction(callback, false, es); 141 } 142 143 void DatabaseSync::readTransaction(PassRefPtr<SQLTransactionSyncCallback> callback, ExceptionState& es) 144 { 145 runTransaction(callback, true, es); 146 } 147 148 void DatabaseSync::rollbackTransaction(PassRefPtr<SQLTransactionSync> transaction) 149 { 150 ASSERT(!lastErrorMessage().isEmpty()); 151 transaction->rollback(); 152 setLastErrorMessage(""); 153 return; 154 } 155 156 void DatabaseSync::runTransaction(PassRefPtr<SQLTransactionSyncCallback> callback, bool readOnly, ExceptionState& es) 157 { 158 ASSERT(m_scriptExecutionContext->isContextThread()); 159 160 if (sqliteDatabase().transactionInProgress()) { 161 setLastErrorMessage("unable to start a transaction from within a transaction"); 162 es.throwDOMException(SQLDatabaseError); 163 return; 164 } 165 166 RefPtr<SQLTransactionSync> transaction = SQLTransactionSync::create(this, callback, readOnly); 167 transaction->begin(es); 168 if (es.hadException()) { 169 rollbackTransaction(transaction); 170 return; 171 } 172 173 transaction->execute(es); 174 if (es.hadException()) { 175 rollbackTransaction(transaction); 176 return; 177 } 178 179 transaction->commit(es); 180 if (es.hadException()) { 181 rollbackTransaction(transaction); 182 return; 183 } 184 185 setLastErrorMessage(""); 186 } 187 188 void DatabaseSync::markAsDeletedAndClose() 189 { 190 // FIXME: need to do something similar to closeImmediately(), but in a sync way 191 } 192 193 class CloseSyncDatabaseOnContextThreadTask : public ScriptExecutionContext::Task { 194 public: 195 static PassOwnPtr<CloseSyncDatabaseOnContextThreadTask> create(PassRefPtr<DatabaseSync> database) 196 { 197 return adoptPtr(new CloseSyncDatabaseOnContextThreadTask(database)); 198 } 199 200 virtual void performTask(ScriptExecutionContext*) 201 { 202 m_database->closeImmediately(); 203 } 204 205 private: 206 CloseSyncDatabaseOnContextThreadTask(PassRefPtr<DatabaseSync> database) 207 : m_database(database) 208 { 209 } 210 211 RefPtr<DatabaseSync> m_database; 212 }; 213 214 void DatabaseSync::closeImmediately() 215 { 216 ASSERT(m_scriptExecutionContext->isContextThread()); 217 218 if (!opened()) 219 return; 220 221 logErrorMessage("forcibly closing database"); 222 closeDatabase(); 223 } 224 225 } // namespace WebCore 226