2 * Copyright (C) 2007, 2008, 2013 Apple 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
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
14 * its contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include "modules/webdatabase/Database.h"
32 #include "core/dom/CrossThreadTask.h"
33 #include "core/dom/Document.h"
34 #include "core/dom/ExecutionContext.h"
35 #include "core/html/VoidCallback.h"
36 #include "core/page/Page.h"
37 #include "platform/Logging.h"
38 #include "modules/webdatabase/sqlite/SQLiteStatement.h"
39 #include "modules/webdatabase/ChangeVersionData.h"
40 #include "modules/webdatabase/DatabaseCallback.h"
41 #include "modules/webdatabase/DatabaseContext.h"
42 #include "modules/webdatabase/DatabaseManager.h"
43 #include "modules/webdatabase/DatabaseTask.h"
44 #include "modules/webdatabase/DatabaseThread.h"
45 #include "modules/webdatabase/DatabaseTracker.h"
46 #include "modules/webdatabase/SQLError.h"
47 #include "modules/webdatabase/SQLTransaction.h"
48 #include "modules/webdatabase/SQLTransactionCallback.h"
49 #include "modules/webdatabase/SQLTransactionErrorCallback.h"
50 #include "platform/weborigin/SecurityOrigin.h"
51 #include "wtf/OwnPtr.h"
52 #include "wtf/PassOwnPtr.h"
53 #include "wtf/PassRefPtr.h"
54 #include "wtf/RefPtr.h"
55 #include "wtf/StdLibExtras.h"
56 #include "wtf/text/CString.h"
60 PassRefPtrWillBeRawPtr<Database> Database::create(ExecutionContext*, PassRefPtrWillBeRawPtr<DatabaseBackendBase> backend)
62 // FIXME: Currently, we're only simulating the backend by return the
63 // frontend database as its own the backend. When we split the 2 apart,
64 // this create() function should be changed to be a factory method for
65 // instantiating the backend.
66 return static_cast<Database*>(backend.get());
69 Database::Database(DatabaseContext* databaseContext,
70 const String& name, const String& expectedVersion, const String& displayName, unsigned long estimatedSize)
71 : DatabaseBackend(databaseContext, name, expectedVersion, displayName, estimatedSize)
72 , DatabaseBase(databaseContext->executionContext())
73 , m_databaseContext(DatabaseBackend::databaseContext())
75 ScriptWrappable::init(this);
76 m_databaseThreadSecurityOrigin = m_contextThreadSecurityOrigin->isolatedCopy();
79 ASSERT(m_databaseContext->databaseThread());
86 void Database::trace(Visitor* visitor)
88 visitor->trace(m_databaseContext);
89 DatabaseBackend::trace(visitor);
92 Database* Database::from(DatabaseBackend* backend)
94 return static_cast<Database*>(backend->m_frontend);
97 PassRefPtrWillBeRawPtr<DatabaseBackend> Database::backend()
102 String Database::version() const
104 return DatabaseBackendBase::version();
107 void Database::closeImmediately()
109 ASSERT(executionContext()->isContextThread());
110 DatabaseThread* databaseThread = databaseContext()->databaseThread();
111 if (databaseThread && !databaseThread->terminationRequested() && opened()) {
112 logErrorMessage("forcibly closing database");
113 databaseThread->scheduleTask(DatabaseCloseTask::create(this, 0));
117 void Database::changeVersion(const String& oldVersion, const String& newVersion, PassOwnPtr<SQLTransactionCallback> callback, PassOwnPtr<SQLTransactionErrorCallback> errorCallback, PassOwnPtr<VoidCallback> successCallback)
119 ChangeVersionData data(oldVersion, newVersion);
120 runTransaction(callback, errorCallback, successCallback, false, &data);
123 void Database::transaction(PassOwnPtr<SQLTransactionCallback> callback, PassOwnPtr<SQLTransactionErrorCallback> errorCallback, PassOwnPtr<VoidCallback> successCallback)
125 runTransaction(callback, errorCallback, successCallback, false);
128 void Database::readTransaction(PassOwnPtr<SQLTransactionCallback> callback, PassOwnPtr<SQLTransactionErrorCallback> errorCallback, PassOwnPtr<VoidCallback> successCallback)
130 runTransaction(callback, errorCallback, successCallback, true);
133 static void callTransactionErrorCallback(ExecutionContext*, PassOwnPtr<SQLTransactionErrorCallback> callback, PassOwnPtr<SQLErrorData> errorData)
135 RefPtrWillBeRawPtr<SQLError> error = SQLError::create(*errorData);
136 callback->handleEvent(error.get());
139 void Database::runTransaction(PassOwnPtr<SQLTransactionCallback> callback, PassOwnPtr<SQLTransactionErrorCallback> errorCallback,
140 PassOwnPtr<VoidCallback> successCallback, bool readOnly, const ChangeVersionData* changeVersionData)
142 // FIXME: Rather than passing errorCallback to SQLTransaction and then sometimes firing it ourselves,
143 // this code should probably be pushed down into DatabaseBackend so that we only create the SQLTransaction
144 // if we're actually going to run it.
146 SQLTransactionErrorCallback* originalErrorCallback = errorCallback.get();
148 RefPtrWillBeRawPtr<SQLTransaction> transaction = SQLTransaction::create(this, callback, successCallback, errorCallback, readOnly);
149 RefPtrWillBeRawPtr<SQLTransactionBackend> transactionBackend = backend()->runTransaction(transaction, readOnly, changeVersionData);
150 if (!transactionBackend) {
151 OwnPtr<SQLTransactionErrorCallback> callback = transaction->releaseErrorCallback();
152 ASSERT(callback == originalErrorCallback);
154 OwnPtr<SQLErrorData> error = SQLErrorData::create(SQLError::UNKNOWN_ERR, "database has been closed");
155 executionContext()->postTask(createCallbackTask(&callTransactionErrorCallback, callback.release(), error.release()));
160 // This object is constructed in a database thread, and destructed in the
162 class DeliverPendingCallbackTask FINAL : public ExecutionContextTask {
164 static PassOwnPtr<DeliverPendingCallbackTask> create(PassRefPtrWillBeRawPtr<SQLTransaction> transaction)
166 return adoptPtr(new DeliverPendingCallbackTask(transaction));
169 virtual void performTask(ExecutionContext*) OVERRIDE
171 m_transaction->performPendingCallback();
175 DeliverPendingCallbackTask(PassRefPtrWillBeRawPtr<SQLTransaction> transaction)
176 : m_transaction(transaction)
180 RefPtrWillBeCrossThreadPersistent<SQLTransaction> m_transaction;
183 void Database::scheduleTransactionCallback(SQLTransaction* transaction)
185 executionContext()->postTask(DeliverPendingCallbackTask::create(transaction));
188 Vector<String> Database::performGetTableNames()
192 SQLiteStatement statement(sqliteDatabase(), "SELECT name FROM sqlite_master WHERE type='table';");
193 if (statement.prepare() != SQLResultOk) {
194 WTF_LOG_ERROR("Unable to retrieve list of tables for database %s", databaseDebugName().ascii().data());
196 return Vector<String>();
199 Vector<String> tableNames;
201 while ((result = statement.step()) == SQLResultRow) {
202 String name = statement.getColumnText(0);
203 if (name != databaseInfoTableName())
204 tableNames.append(name);
209 if (result != SQLResultDone) {
210 WTF_LOG_ERROR("Error getting tables for database %s", databaseDebugName().ascii().data());
211 return Vector<String>();
217 Vector<String> Database::tableNames()
219 // FIXME: Not using isolatedCopy on these strings looks ok since threads take strict turns
220 // in dealing with them. However, if the code changes, this may not be true anymore.
221 Vector<String> result;
222 TaskSynchronizer synchronizer;
223 if (!databaseContext()->databaseThread() || databaseContext()->databaseThread()->terminationRequested(&synchronizer))
226 OwnPtr<DatabaseTableNamesTask> task = DatabaseTableNamesTask::create(this, &synchronizer, result);
227 databaseContext()->databaseThread()->scheduleTask(task.release());
228 synchronizer.waitForTaskCompletion();
233 SecurityOrigin* Database::securityOrigin() const
235 if (executionContext()->isContextThread())
236 return m_contextThreadSecurityOrigin.get();
237 if (databaseContext()->databaseThread()->isDatabaseThread())
238 return m_databaseThreadSecurityOrigin.get();
242 void Database::reportStartTransactionResult(int errorSite, int webSqlErrorCode, int sqliteErrorCode)
244 backend()->reportStartTransactionResult(errorSite, webSqlErrorCode, sqliteErrorCode);
247 void Database::reportCommitTransactionResult(int errorSite, int webSqlErrorCode, int sqliteErrorCode)
249 backend()->reportCommitTransactionResult(errorSite, webSqlErrorCode, sqliteErrorCode);
253 } // namespace WebCore