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/ExecutionContext.h"
34 #include "core/html/VoidCallback.h"
35 #include "core/page/Page.h"
36 #include "platform/Logging.h"
37 #include "modules/webdatabase/sqlite/SQLiteStatement.h"
38 #include "modules/webdatabase/ChangeVersionData.h"
39 #include "modules/webdatabase/DatabaseCallback.h"
40 #include "modules/webdatabase/DatabaseContext.h"
41 #include "modules/webdatabase/DatabaseManager.h"
42 #include "modules/webdatabase/DatabaseTask.h"
43 #include "modules/webdatabase/DatabaseThread.h"
44 #include "modules/webdatabase/DatabaseTracker.h"
45 #include "modules/webdatabase/SQLError.h"
46 #include "modules/webdatabase/SQLTransaction.h"
47 #include "modules/webdatabase/SQLTransactionCallback.h"
48 #include "modules/webdatabase/SQLTransactionErrorCallback.h"
49 #include "platform/weborigin/SecurityOrigin.h"
50 #include "wtf/OwnPtr.h"
51 #include "wtf/PassOwnPtr.h"
52 #include "wtf/PassRefPtr.h"
53 #include "wtf/RefPtr.h"
54 #include "wtf/StdLibExtras.h"
55 #include "wtf/text/CString.h"
59 PassRefPtrWillBeRawPtr<Database> Database::create(ExecutionContext*, PassRefPtrWillBeRawPtr<DatabaseBackendBase> backend)
61 // FIXME: Currently, we're only simulating the backend by return the
62 // frontend database as its own the backend. When we split the 2 apart,
63 // this create() function should be changed to be a factory method for
64 // instantiating the backend.
65 return static_cast<Database*>(backend.get());
68 Database::Database(DatabaseContext* databaseContext,
69 const String& name, const String& expectedVersion, const String& displayName, unsigned long estimatedSize)
70 : DatabaseBackend(databaseContext, name, expectedVersion, displayName, estimatedSize)
71 , DatabaseBase(databaseContext->executionContext())
72 , m_databaseContext(DatabaseBackend::databaseContext())
74 ScriptWrappable::init(this);
75 m_databaseThreadSecurityOrigin = m_contextThreadSecurityOrigin->isolatedCopy();
78 ASSERT(m_databaseContext->databaseThread());
85 void Database::trace(Visitor* visitor)
87 visitor->trace(m_databaseContext);
88 DatabaseBackend::trace(visitor);
91 Database* Database::from(DatabaseBackend* backend)
93 return static_cast<Database*>(backend->m_frontend);
96 PassRefPtrWillBeRawPtr<DatabaseBackend> Database::backend()
101 String Database::version() const
103 return DatabaseBackendBase::version();
106 void Database::closeImmediately()
108 ASSERT(executionContext()->isContextThread());
109 DatabaseThread* databaseThread = databaseContext()->databaseThread();
110 if (databaseThread && !databaseThread->terminationRequested() && opened()) {
111 logErrorMessage("forcibly closing database");
112 databaseThread->scheduleTask(DatabaseCloseTask::create(this, 0));
116 void Database::changeVersion(const String& oldVersion, const String& newVersion, PassOwnPtr<SQLTransactionCallback> callback, PassOwnPtr<SQLTransactionErrorCallback> errorCallback, PassOwnPtr<VoidCallback> successCallback)
118 ChangeVersionData data(oldVersion, newVersion);
119 runTransaction(callback, errorCallback, successCallback, false, &data);
122 void Database::transaction(PassOwnPtr<SQLTransactionCallback> callback, PassOwnPtr<SQLTransactionErrorCallback> errorCallback, PassOwnPtr<VoidCallback> successCallback)
124 runTransaction(callback, errorCallback, successCallback, false);
127 void Database::readTransaction(PassOwnPtr<SQLTransactionCallback> callback, PassOwnPtr<SQLTransactionErrorCallback> errorCallback, PassOwnPtr<VoidCallback> successCallback)
129 runTransaction(callback, errorCallback, successCallback, true);
132 static void callTransactionErrorCallback(ExecutionContext*, PassOwnPtr<SQLTransactionErrorCallback> callback, PassOwnPtr<SQLErrorData> errorData)
134 RefPtrWillBeRawPtr<SQLError> error = SQLError::create(*errorData);
135 callback->handleEvent(error.get());
138 void Database::runTransaction(PassOwnPtr<SQLTransactionCallback> callback, PassOwnPtr<SQLTransactionErrorCallback> errorCallback,
139 PassOwnPtr<VoidCallback> successCallback, bool readOnly, const ChangeVersionData* changeVersionData)
141 // FIXME: Rather than passing errorCallback to SQLTransaction and then sometimes firing it ourselves,
142 // this code should probably be pushed down into DatabaseBackend so that we only create the SQLTransaction
143 // if we're actually going to run it.
145 SQLTransactionErrorCallback* originalErrorCallback = errorCallback.get();
147 RefPtrWillBeRawPtr<SQLTransaction> transaction = SQLTransaction::create(this, callback, successCallback, errorCallback, readOnly);
148 RefPtrWillBeRawPtr<SQLTransactionBackend> transactionBackend = backend()->runTransaction(transaction, readOnly, changeVersionData);
149 if (!transactionBackend) {
150 OwnPtr<SQLTransactionErrorCallback> callback = transaction->releaseErrorCallback();
151 ASSERT(callback == originalErrorCallback);
153 OwnPtr<SQLErrorData> error = SQLErrorData::create(SQLError::UNKNOWN_ERR, "database has been closed");
154 executionContext()->postTask(createCrossThreadTask(&callTransactionErrorCallback, callback.release(), error.release()));
159 // This object is constructed in a database thread, and destructed in the
161 class DeliverPendingCallbackTask FINAL : public ExecutionContextTask {
163 static PassOwnPtr<DeliverPendingCallbackTask> create(PassRefPtrWillBeRawPtr<SQLTransaction> transaction)
165 return adoptPtr(new DeliverPendingCallbackTask(transaction));
168 virtual void performTask(ExecutionContext*) OVERRIDE
170 m_transaction->performPendingCallback();
174 DeliverPendingCallbackTask(PassRefPtrWillBeRawPtr<SQLTransaction> transaction)
175 : m_transaction(transaction)
179 RefPtrWillBeCrossThreadPersistent<SQLTransaction> m_transaction;
182 void Database::scheduleTransactionCallback(SQLTransaction* transaction)
184 executionContext()->postTask(DeliverPendingCallbackTask::create(transaction));
187 Vector<String> Database::performGetTableNames()
191 SQLiteStatement statement(sqliteDatabase(), "SELECT name FROM sqlite_master WHERE type='table';");
192 if (statement.prepare() != SQLResultOk) {
193 WTF_LOG_ERROR("Unable to retrieve list of tables for database %s", databaseDebugName().ascii().data());
195 return Vector<String>();
198 Vector<String> tableNames;
200 while ((result = statement.step()) == SQLResultRow) {
201 String name = statement.getColumnText(0);
202 if (name != databaseInfoTableName())
203 tableNames.append(name);
208 if (result != SQLResultDone) {
209 WTF_LOG_ERROR("Error getting tables for database %s", databaseDebugName().ascii().data());
210 return Vector<String>();
216 Vector<String> Database::tableNames()
218 // FIXME: Not using isolatedCopy on these strings looks ok since threads take strict turns
219 // in dealing with them. However, if the code changes, this may not be true anymore.
220 Vector<String> result;
221 TaskSynchronizer synchronizer;
222 if (!databaseContext()->databaseThread() || databaseContext()->databaseThread()->terminationRequested(&synchronizer))
225 OwnPtr<DatabaseTableNamesTask> task = DatabaseTableNamesTask::create(this, &synchronizer, result);
226 databaseContext()->databaseThread()->scheduleTask(task.release());
227 synchronizer.waitForTaskCompletion();
232 SecurityOrigin* Database::securityOrigin() const
234 if (executionContext()->isContextThread())
235 return m_contextThreadSecurityOrigin.get();
236 if (databaseContext()->databaseThread()->isDatabaseThread())
237 return m_databaseThreadSecurityOrigin.get();
241 void Database::reportStartTransactionResult(int errorSite, int webSqlErrorCode, int sqliteErrorCode)
243 backend()->reportStartTransactionResult(errorSite, webSqlErrorCode, sqliteErrorCode);
246 void Database::reportCommitTransactionResult(int errorSite, int webSqlErrorCode, int sqliteErrorCode)
248 backend()->reportCommitTransactionResult(errorSite, webSqlErrorCode, sqliteErrorCode);