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 PassRefPtrWillBeRawPtr<DatabaseSync> DatabaseSync::create(ExecutionContext*, PassRefPtrWillBeRawPtr<DatabaseBackendBase> backend)
53 return static_cast<DatabaseSync*>(backend.get());
56 DatabaseSync::DatabaseSync(DatabaseContext* databaseContext,
57 const String& name, const String& expectedVersion, const String& displayName, unsigned long estimatedSize)
58 : DatabaseBackendSync(databaseContext, name, expectedVersion, displayName, estimatedSize)
59 , DatabaseBase(databaseContext->executionContext())
61 ScriptWrappable::init(this);
65 DatabaseSync::~DatabaseSync()
67 if (executionContext())
68 ASSERT(executionContext()->isContextThread());
71 void DatabaseSync::trace(Visitor* visitor)
73 DatabaseBackendSync::trace(visitor);
76 void DatabaseSync::changeVersion(const String& oldVersion, const String& newVersion, PassOwnPtr<SQLTransactionSyncCallback> changeVersionCallback, ExceptionState& exceptionState)
78 ASSERT(executionContext()->isContextThread());
80 if (sqliteDatabase().transactionInProgress()) {
81 reportChangeVersionResult(1, SQLError::DATABASE_ERR, 0);
82 setLastErrorMessage("unable to changeVersion from within a transaction");
83 exceptionState.throwDOMException(SQLDatabaseError, "Unable to change version from within a transaction.");
87 RefPtrWillBeRawPtr<SQLTransactionSync> transaction = SQLTransactionSync::create(this, changeVersionCallback, false);
88 transaction->begin(exceptionState);
89 if (exceptionState.hadException()) {
90 ASSERT(!lastErrorMessage().isEmpty());
95 if (!getVersionFromDatabase(actualVersion)) {
96 reportChangeVersionResult(2, SQLError::UNKNOWN_ERR, sqliteDatabase().lastError());
97 setLastErrorMessage("unable to read the current version", sqliteDatabase().lastError(), sqliteDatabase().lastErrorMsg());
98 exceptionState.throwDOMException(UnknownError, SQLError::unknownErrorMessage);
102 if (actualVersion != oldVersion) {
103 reportChangeVersionResult(3, SQLError::VERSION_ERR, 0);
104 setLastErrorMessage("current version of the database and `oldVersion` argument do not match");
105 exceptionState.throwDOMException(VersionError, SQLError::versionErrorMessage);
110 transaction->execute(exceptionState);
111 if (exceptionState.hadException()) {
112 ASSERT(!lastErrorMessage().isEmpty());
116 if (!setVersionInDatabase(newVersion)) {
117 reportChangeVersionResult(4, SQLError::UNKNOWN_ERR, sqliteDatabase().lastError());
118 setLastErrorMessage("unable to set the new version", sqliteDatabase().lastError(), sqliteDatabase().lastErrorMsg());
119 exceptionState.throwDOMException(UnknownError, SQLError::unknownErrorMessage);
123 transaction->commit(exceptionState);
124 if (exceptionState.hadException()) {
125 ASSERT(!lastErrorMessage().isEmpty());
126 setCachedVersion(oldVersion);
130 reportChangeVersionResult(0, -1, 0); // OK
132 setExpectedVersion(newVersion);
133 setLastErrorMessage("");
136 void DatabaseSync::transaction(PassOwnPtr<SQLTransactionSyncCallback> callback, ExceptionState& exceptionState)
138 runTransaction(callback, false, exceptionState);
141 void DatabaseSync::readTransaction(PassOwnPtr<SQLTransactionSyncCallback> callback, ExceptionState& exceptionState)
143 runTransaction(callback, true, exceptionState);
146 void DatabaseSync::rollbackTransaction(SQLTransactionSync& transaction)
148 ASSERT(!lastErrorMessage().isEmpty());
149 transaction.rollback();
150 setLastErrorMessage("");
154 void DatabaseSync::runTransaction(PassOwnPtr<SQLTransactionSyncCallback> callback, bool readOnly, ExceptionState& exceptionState)
156 ASSERT(executionContext()->isContextThread());
158 if (sqliteDatabase().transactionInProgress()) {
159 setLastErrorMessage("unable to start a transaction from within a transaction");
160 exceptionState.throwDOMException(SQLDatabaseError, "Unable to start a transaction from within a transaction.");
164 RefPtrWillBeRawPtr<SQLTransactionSync> transaction = SQLTransactionSync::create(this, callback, readOnly);
165 transaction->begin(exceptionState);
166 if (exceptionState.hadException()) {
167 rollbackTransaction(*transaction);
171 transaction->execute(exceptionState);
172 if (exceptionState.hadException()) {
173 rollbackTransaction(*transaction);
177 transaction->commit(exceptionState);
178 if (exceptionState.hadException()) {
179 rollbackTransaction(*transaction);
183 setLastErrorMessage("");
186 void DatabaseSync::closeImmediately()
188 ASSERT(executionContext()->isContextThread());
193 logErrorMessage("forcibly closing database");
197 void DatabaseSync::observeTransaction(SQLTransactionSync& transaction)
200 m_observers.add(&transaction, adoptPtr(new TransactionObserver(transaction)));
205 DatabaseSync::TransactionObserver::~TransactionObserver()
207 m_transaction.rollbackIfInProgress();
211 } // namespace WebCore