2 * Copyright (C) 2010 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
32 #include "modules/webdatabase/DatabaseSync.h"
34 #include "bindings/v8/ExceptionState.h"
35 #include "core/dom/ExceptionCode.h"
36 #include "core/dom/ExecutionContext.h"
37 #include "platform/Logging.h"
38 #include "modules/webdatabase/DatabaseCallback.h"
39 #include "modules/webdatabase/DatabaseContext.h"
40 #include "modules/webdatabase/DatabaseManager.h"
41 #include "modules/webdatabase/DatabaseTracker.h"
42 #include "modules/webdatabase/SQLError.h"
43 #include "modules/webdatabase/SQLTransactionSync.h"
44 #include "modules/webdatabase/SQLTransactionSyncCallback.h"
45 #include "wtf/PassRefPtr.h"
46 #include "wtf/RefPtr.h"
47 #include "wtf/text/CString.h"
51 PassRefPtr<DatabaseSync> DatabaseSync::create(ExecutionContext*, PassRefPtr<DatabaseBackendBase> backend)
53 return static_cast<DatabaseSync*>(backend.get());
56 DatabaseSync::DatabaseSync(PassRefPtr<DatabaseContext> databaseContext,
57 const String& name, const String& expectedVersion, const String& displayName, unsigned long estimatedSize)
58 : DatabaseBase(databaseContext->executionContext())
59 , DatabaseBackendSync(databaseContext, name, expectedVersion, displayName, estimatedSize)
61 ScriptWrappable::init(this);
65 DatabaseSync::~DatabaseSync()
67 ASSERT(m_executionContext->isContextThread());
70 void DatabaseSync::changeVersion(const String& oldVersion, const String& newVersion, PassOwnPtr<SQLTransactionSyncCallback> changeVersionCallback, ExceptionState& exceptionState)
72 ASSERT(m_executionContext->isContextThread());
74 if (sqliteDatabase().transactionInProgress()) {
75 reportChangeVersionResult(1, SQLError::DATABASE_ERR, 0);
76 setLastErrorMessage("unable to changeVersion from within a transaction");
77 exceptionState.throwDOMException(SQLDatabaseError, "Unable to change version from within a transaction.");
81 RefPtr<SQLTransactionSync> transaction = SQLTransactionSync::create(this, changeVersionCallback, false);
82 transaction->begin(exceptionState);
83 if (exceptionState.hadException()) {
84 ASSERT(!lastErrorMessage().isEmpty());
89 if (!getVersionFromDatabase(actualVersion)) {
90 reportChangeVersionResult(2, SQLError::UNKNOWN_ERR, sqliteDatabase().lastError());
91 setLastErrorMessage("unable to read the current version", sqliteDatabase().lastError(), sqliteDatabase().lastErrorMsg());
92 exceptionState.throwDOMException(UnknownError, SQLError::unknownErrorMessage);
96 if (actualVersion != oldVersion) {
97 reportChangeVersionResult(3, SQLError::VERSION_ERR, 0);
98 setLastErrorMessage("current version of the database and `oldVersion` argument do not match");
99 exceptionState.throwDOMException(VersionError, SQLError::versionErrorMessage);
104 transaction->execute(exceptionState);
105 if (exceptionState.hadException()) {
106 ASSERT(!lastErrorMessage().isEmpty());
110 if (!setVersionInDatabase(newVersion)) {
111 reportChangeVersionResult(4, SQLError::UNKNOWN_ERR, sqliteDatabase().lastError());
112 setLastErrorMessage("unable to set the new version", sqliteDatabase().lastError(), sqliteDatabase().lastErrorMsg());
113 exceptionState.throwDOMException(UnknownError, SQLError::unknownErrorMessage);
117 transaction->commit(exceptionState);
118 if (exceptionState.hadException()) {
119 ASSERT(!lastErrorMessage().isEmpty());
120 setCachedVersion(oldVersion);
124 reportChangeVersionResult(0, -1, 0); // OK
126 setExpectedVersion(newVersion);
127 setLastErrorMessage("");
130 void DatabaseSync::transaction(PassOwnPtr<SQLTransactionSyncCallback> callback, ExceptionState& exceptionState)
132 runTransaction(callback, false, exceptionState);
135 void DatabaseSync::readTransaction(PassOwnPtr<SQLTransactionSyncCallback> callback, ExceptionState& exceptionState)
137 runTransaction(callback, true, exceptionState);
140 void DatabaseSync::rollbackTransaction(PassRefPtr<SQLTransactionSync> transaction)
142 ASSERT(!lastErrorMessage().isEmpty());
143 transaction->rollback();
144 setLastErrorMessage("");
148 void DatabaseSync::runTransaction(PassOwnPtr<SQLTransactionSyncCallback> callback, bool readOnly, ExceptionState& exceptionState)
150 ASSERT(m_executionContext->isContextThread());
152 if (sqliteDatabase().transactionInProgress()) {
153 setLastErrorMessage("unable to start a transaction from within a transaction");
154 exceptionState.throwDOMException(SQLDatabaseError, "Unable to start a transaction from within a transaction.");
158 RefPtr<SQLTransactionSync> transaction = SQLTransactionSync::create(this, callback, readOnly);
159 transaction->begin(exceptionState);
160 if (exceptionState.hadException()) {
161 rollbackTransaction(transaction);
165 transaction->execute(exceptionState);
166 if (exceptionState.hadException()) {
167 rollbackTransaction(transaction);
171 transaction->commit(exceptionState);
172 if (exceptionState.hadException()) {
173 rollbackTransaction(transaction);
177 setLastErrorMessage("");
180 void DatabaseSync::closeImmediately()
182 ASSERT(m_executionContext->isContextThread());
187 logErrorMessage("forcibly closing database");
191 } // namespace WebCore