| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * Copyright (C) 2010 Google Inc. All rights reserved. | |
| 3 * Copyright (C) 2013 Apple Inc. All rights reserved. | |
| 4 * | |
| 5 * Redistribution and use in source and binary forms, with or without | |
| 6 * modification, are permitted provided that the following conditions are | |
| 7 * met: | |
| 8 * | |
| 9 * * Redistributions of source code must retain the above copyright | |
| 10 * notice, this list of conditions and the following disclaimer. | |
| 11 * * Redistributions in binary form must reproduce the above | |
| 12 * copyright notice, this list of conditions and the following disclaimer | |
| 13 * in the documentation and/or other materials provided with the | |
| 14 * distribution. | |
| 15 * * Neither the name of Google Inc. nor the names of its | |
| 16 * contributors may be used to endorse or promote products derived from | |
| 17 * this software without specific prior written permission. | |
| 18 * | |
| 19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| 20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
| 21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
| 22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
| 23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
| 24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
| 25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
| 26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
| 27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
| 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
| 29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 30 */ | |
| 31 | |
| 32 #include "config.h" | |
| 33 #include "modules/webdatabase/SQLTransactionBackendSync.h" | |
| 34 | |
| 35 #include "bindings/core/v8/ExceptionState.h" | |
| 36 #include "core/dom/ExceptionCode.h" | |
| 37 #include "core/dom/ExecutionContext.h" | |
| 38 #include "modules/webdatabase/sqlite/SQLValue.h" | |
| 39 #include "modules/webdatabase/sqlite/SQLiteTransaction.h" | |
| 40 #include "modules/webdatabase/DatabaseAuthorizer.h" | |
| 41 #include "modules/webdatabase/DatabaseContext.h" | |
| 42 #include "modules/webdatabase/DatabaseSync.h" | |
| 43 #include "modules/webdatabase/SQLError.h" | |
| 44 #include "modules/webdatabase/SQLResultSet.h" | |
| 45 #include "modules/webdatabase/SQLStatementSync.h" | |
| 46 #include "modules/webdatabase/SQLTransactionClient.h" | |
| 47 #include "modules/webdatabase/SQLTransactionSync.h" | |
| 48 #include "modules/webdatabase/SQLTransactionSyncCallback.h" | |
| 49 #include "wtf/PassRefPtr.h" | |
| 50 #include "wtf/RefPtr.h" | |
| 51 | |
| 52 namespace blink { | |
| 53 | |
| 54 SQLTransactionBackendSync::SQLTransactionBackendSync(DatabaseSync* db, PassOwnPt
rWillBeRawPtr<SQLTransactionSyncCallback> callback, bool readOnly) | |
| 55 : m_database(db) | |
| 56 , m_callback(callback) | |
| 57 , m_readOnly(readOnly) | |
| 58 , m_hasVersionMismatch(false) | |
| 59 , m_modifiedDatabase(false) | |
| 60 , m_transactionClient(adoptPtr(new SQLTransactionClient())) | |
| 61 { | |
| 62 ASSERT(m_database->executionContext()->isContextThread()); | |
| 63 } | |
| 64 | |
| 65 void SQLTransactionBackendSync::rollbackIfInProgress() | |
| 66 { | |
| 67 ASSERT(!m_database->executionContext() || m_database->executionContext()->is
ContextThread()); | |
| 68 if (m_sqliteTransaction && m_sqliteTransaction->inProgress()) | |
| 69 rollback(); | |
| 70 } | |
| 71 | |
| 72 SQLTransactionBackendSync::~SQLTransactionBackendSync() | |
| 73 { | |
| 74 #if ENABLE(OILPAN) | |
| 75 ASSERT(!m_sqliteTransaction || !m_sqliteTransaction->inProgress()); | |
| 76 #else | |
| 77 rollbackIfInProgress(); | |
| 78 #endif | |
| 79 } | |
| 80 | |
| 81 void SQLTransactionBackendSync::trace(Visitor* visitor) | |
| 82 { | |
| 83 visitor->trace(m_database); | |
| 84 visitor->trace(m_callback); | |
| 85 } | |
| 86 | |
| 87 PassRefPtrWillBeRawPtr<SQLResultSet> SQLTransactionBackendSync::executeSQL(const
String& sqlStatement, const Vector<SQLValue>& arguments, ExceptionState& except
ionState) | |
| 88 { | |
| 89 ASSERT(m_database->executionContext()->isContextThread()); | |
| 90 | |
| 91 m_database->setLastErrorMessage(""); | |
| 92 | |
| 93 if (!m_database->opened()) { | |
| 94 m_database->setLastErrorMessage("cannot executeSQL because the database
is not open"); | |
| 95 exceptionState.throwDOMException(UnknownError, SQLError::unknownErrorMes
sage); | |
| 96 return nullptr; | |
| 97 } | |
| 98 | |
| 99 if (m_hasVersionMismatch) { | |
| 100 m_database->setLastErrorMessage("cannot executeSQL because there is a ve
rsion mismatch"); | |
| 101 exceptionState.throwDOMException(VersionError, SQLError::versionErrorMes
sage); | |
| 102 return nullptr; | |
| 103 } | |
| 104 | |
| 105 if (sqlStatement.isEmpty()) | |
| 106 return nullptr; | |
| 107 | |
| 108 int permissions = DatabaseAuthorizer::ReadWriteMask; | |
| 109 if (!m_database->databaseContext()->allowDatabaseAccess()) | |
| 110 permissions |= DatabaseAuthorizer::NoAccessMask; | |
| 111 else if (m_readOnly) | |
| 112 permissions |= DatabaseAuthorizer::ReadOnlyMask; | |
| 113 | |
| 114 SQLStatementSync statement(sqlStatement, arguments, permissions); | |
| 115 | |
| 116 m_database->resetAuthorizer(); | |
| 117 bool retryStatement = true; | |
| 118 RefPtrWillBeRawPtr<SQLResultSet> resultSet; | |
| 119 while (retryStatement) { | |
| 120 retryStatement = false; | |
| 121 resultSet = statement.execute(m_database.get(), exceptionState); | |
| 122 if (!resultSet) { | |
| 123 if (m_sqliteTransaction->wasRolledBackBySqlite()) | |
| 124 return nullptr; | |
| 125 | |
| 126 if (exceptionState.code() == QuotaExceededError) { | |
| 127 if (m_transactionClient->didExceedQuota(database())) { | |
| 128 exceptionState.clearException(); | |
| 129 retryStatement = true; | |
| 130 } else { | |
| 131 m_database->setLastErrorMessage("there was not enough remain
ing storage space"); | |
| 132 return nullptr; | |
| 133 } | |
| 134 } | |
| 135 } | |
| 136 } | |
| 137 | |
| 138 if (m_database->lastActionChangedDatabase()) | |
| 139 m_modifiedDatabase = true; | |
| 140 | |
| 141 return resultSet.release(); | |
| 142 } | |
| 143 | |
| 144 void SQLTransactionBackendSync::begin(ExceptionState& exceptionState) | |
| 145 { | |
| 146 ASSERT(m_database->executionContext()->isContextThread()); | |
| 147 if (!m_database->opened()) { | |
| 148 m_database->reportStartTransactionResult(1, SQLError::UNKNOWN_ERR, 0); | |
| 149 m_database->setLastErrorMessage("cannot begin transaction because the da
tabase is not open"); | |
| 150 exceptionState.throwDOMException(UnknownError, SQLError::unknownErrorMes
sage); | |
| 151 return; | |
| 152 } | |
| 153 | |
| 154 ASSERT(!m_database->sqliteDatabase().transactionInProgress()); | |
| 155 | |
| 156 // Set the maximum usage for this transaction if this transactions is not re
ad-only. | |
| 157 if (!m_readOnly) | |
| 158 m_database->sqliteDatabase().setMaximumSize(m_database->maximumSize()); | |
| 159 | |
| 160 ASSERT(!m_sqliteTransaction); | |
| 161 m_sqliteTransaction = adoptPtr(new SQLiteTransaction(m_database->sqliteDatab
ase(), m_readOnly)); | |
| 162 | |
| 163 m_database->resetDeletes(); | |
| 164 m_database->disableAuthorizer(); | |
| 165 m_sqliteTransaction->begin(); | |
| 166 m_database->enableAuthorizer(); | |
| 167 | |
| 168 // Check if begin() succeeded. | |
| 169 if (!m_sqliteTransaction->inProgress()) { | |
| 170 ASSERT(!m_database->sqliteDatabase().transactionInProgress()); | |
| 171 m_database->reportStartTransactionResult(2, SQLError::DATABASE_ERR, m_da
tabase->sqliteDatabase().lastError()); | |
| 172 m_database->setLastErrorMessage("unable to begin transaction", | |
| 173 m_database->sqliteDatabase().lastError(), m_database->sqliteDatabase
().lastErrorMsg()); | |
| 174 m_sqliteTransaction.clear(); | |
| 175 exceptionState.throwDOMException(SQLDatabaseError, "Unable to begin tran
saction."); | |
| 176 return; | |
| 177 } | |
| 178 | |
| 179 // Note: We intentionally retrieve the actual version even with an empty exp
ected version. | |
| 180 // In multi-process browsers, we take this opportinutiy to update the cached
value for | |
| 181 // the actual version. In single-process browsers, this is just a map lookup
. | |
| 182 String actualVersion; | |
| 183 if (!m_database->getActualVersionForTransaction(actualVersion)) { | |
| 184 m_database->reportStartTransactionResult(3, SQLError::DATABASE_ERR, m_da
tabase->sqliteDatabase().lastError()); | |
| 185 m_database->setLastErrorMessage("unable to read version", | |
| 186 m_database->sqliteDatabase().lastError(), m_database->sqliteDatabase
().lastErrorMsg()); | |
| 187 rollback(); | |
| 188 exceptionState.throwDOMException(SQLDatabaseError, "Unable to read versi
on."); | |
| 189 return; | |
| 190 } | |
| 191 m_hasVersionMismatch = !m_database->expectedVersion().isEmpty() && (m_databa
se->expectedVersion() != actualVersion); | |
| 192 m_database->reportStartTransactionResult(0, -1, 0); // OK | |
| 193 } | |
| 194 | |
| 195 void SQLTransactionBackendSync::execute(ExceptionState& exceptionState) | |
| 196 { | |
| 197 ASSERT(m_database->executionContext()->isContextThread()); | |
| 198 if (!m_database->opened() || (m_callback && !m_callback->handleEvent(SQLTran
sactionSync::from(this)))) { | |
| 199 if (m_database->lastErrorMessage().isEmpty()) | |
| 200 m_database->setLastErrorMessage("failed to execute transaction callb
ack"); | |
| 201 m_callback.clear(); | |
| 202 exceptionState.throwDOMException(UnknownError, SQLError::unknownErrorMes
sage); | |
| 203 return; | |
| 204 } | |
| 205 | |
| 206 m_callback.clear(); | |
| 207 } | |
| 208 | |
| 209 void SQLTransactionBackendSync::commit(ExceptionState& exceptionState) | |
| 210 { | |
| 211 ASSERT(m_database->executionContext()->isContextThread()); | |
| 212 if (!m_database->opened()) { | |
| 213 m_database->reportCommitTransactionResult(1, SQLError::UNKNOWN_ERR, 0); | |
| 214 m_database->setLastErrorMessage("unable to commit transaction because th
e database is not open."); | |
| 215 exceptionState.throwDOMException(UnknownError, SQLError::unknownErrorMes
sage); | |
| 216 return; | |
| 217 } | |
| 218 | |
| 219 ASSERT(m_sqliteTransaction); | |
| 220 | |
| 221 m_database->disableAuthorizer(); | |
| 222 m_sqliteTransaction->commit(); | |
| 223 m_database->enableAuthorizer(); | |
| 224 | |
| 225 // If the commit failed, the transaction will still be marked as "in progres
s" | |
| 226 if (m_sqliteTransaction->inProgress()) { | |
| 227 m_database->reportCommitTransactionResult(2, SQLError::DATABASE_ERR, m_d
atabase->sqliteDatabase().lastError()); | |
| 228 m_database->setLastErrorMessage("unable to commit transaction", | |
| 229 m_database->sqliteDatabase().lastError(), m_database->sqliteDatabase
().lastErrorMsg()); | |
| 230 exceptionState.throwDOMException(SQLDatabaseError, "Unable to commit tra
nsaction."); | |
| 231 return; | |
| 232 } | |
| 233 | |
| 234 m_sqliteTransaction.clear(); | |
| 235 | |
| 236 // Vacuum the database if anything was deleted. | |
| 237 if (m_database->hadDeletes()) | |
| 238 m_database->incrementalVacuumIfNeeded(); | |
| 239 | |
| 240 // The commit was successful. If the transaction modified this database, not
ify the delegates. | |
| 241 if (m_modifiedDatabase) | |
| 242 m_transactionClient->didCommitWriteTransaction(database()); | |
| 243 | |
| 244 m_database->reportCommitTransactionResult(0, -1, 0); // OK | |
| 245 } | |
| 246 | |
| 247 // This can be called during GC. Do not allocate new on-heap objects. | |
| 248 void SQLTransactionBackendSync::rollback() | |
| 249 { | |
| 250 ASSERT(!m_database->executionContext() || m_database->executionContext()->is
ContextThread()); | |
| 251 m_database->disableAuthorizer(); | |
| 252 if (m_sqliteTransaction) { | |
| 253 m_sqliteTransaction->rollback(); | |
| 254 m_sqliteTransaction.clear(); | |
| 255 } | |
| 256 m_database->enableAuthorizer(); | |
| 257 | |
| 258 ASSERT(!m_database->sqliteDatabase().transactionInProgress()); | |
| 259 } | |
| 260 | |
| 261 } // namespace blink | |
| OLD | NEW |