1 /*
2 * Copyright (C) 2007, 2008, 2013 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
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.
16 *
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.
27 */
28
29 #include "config.h"
30 #include "modules/webdatabase/Database.h"
31
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"
57
58 namespace WebCore {
59
create(ExecutionContext *,PassRefPtrWillBeRawPtr<DatabaseBackendBase> backend)60 PassRefPtrWillBeRawPtr<Database> Database::create(ExecutionContext*, PassRefPtrWillBeRawPtr<DatabaseBackendBase> backend)
61 {
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());
67 }
68
Database(DatabaseContext * databaseContext,const String & name,const String & expectedVersion,const String & displayName,unsigned long estimatedSize)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())
74 {
75 ScriptWrappable::init(this);
76 m_databaseThreadSecurityOrigin = m_contextThreadSecurityOrigin->isolatedCopy();
77 setFrontend(this);
78
79 ASSERT(m_databaseContext->databaseThread());
80 }
81
~Database()82 Database::~Database()
83 {
84 }
85
trace(Visitor * visitor)86 void Database::trace(Visitor* visitor)
87 {
88 visitor->trace(m_databaseContext);
89 DatabaseBackend::trace(visitor);
90 }
91
from(DatabaseBackend * backend)92 Database* Database::from(DatabaseBackend* backend)
93 {
94 return static_cast<Database*>(backend->m_frontend);
95 }
96
backend()97 PassRefPtrWillBeRawPtr<DatabaseBackend> Database::backend()
98 {
99 return this;
100 }
101
version() const102 String Database::version() const
103 {
104 return DatabaseBackendBase::version();
105 }
106
closeImmediately()107 void Database::closeImmediately()
108 {
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));
114 }
115 }
116
changeVersion(const String & oldVersion,const String & newVersion,PassOwnPtr<SQLTransactionCallback> callback,PassOwnPtr<SQLTransactionErrorCallback> errorCallback,PassOwnPtr<VoidCallback> successCallback)117 void Database::changeVersion(const String& oldVersion, const String& newVersion, PassOwnPtr<SQLTransactionCallback> callback, PassOwnPtr<SQLTransactionErrorCallback> errorCallback, PassOwnPtr<VoidCallback> successCallback)
118 {
119 ChangeVersionData data(oldVersion, newVersion);
120 runTransaction(callback, errorCallback, successCallback, false, &data);
121 }
122
transaction(PassOwnPtr<SQLTransactionCallback> callback,PassOwnPtr<SQLTransactionErrorCallback> errorCallback,PassOwnPtr<VoidCallback> successCallback)123 void Database::transaction(PassOwnPtr<SQLTransactionCallback> callback, PassOwnPtr<SQLTransactionErrorCallback> errorCallback, PassOwnPtr<VoidCallback> successCallback)
124 {
125 runTransaction(callback, errorCallback, successCallback, false);
126 }
127
readTransaction(PassOwnPtr<SQLTransactionCallback> callback,PassOwnPtr<SQLTransactionErrorCallback> errorCallback,PassOwnPtr<VoidCallback> successCallback)128 void Database::readTransaction(PassOwnPtr<SQLTransactionCallback> callback, PassOwnPtr<SQLTransactionErrorCallback> errorCallback, PassOwnPtr<VoidCallback> successCallback)
129 {
130 runTransaction(callback, errorCallback, successCallback, true);
131 }
132
callTransactionErrorCallback(ExecutionContext *,PassOwnPtr<SQLTransactionErrorCallback> callback,PassOwnPtr<SQLErrorData> errorData)133 static void callTransactionErrorCallback(ExecutionContext*, PassOwnPtr<SQLTransactionErrorCallback> callback, PassOwnPtr<SQLErrorData> errorData)
134 {
135 RefPtrWillBeRawPtr<SQLError> error = SQLError::create(*errorData);
136 callback->handleEvent(error.get());
137 }
138
runTransaction(PassOwnPtr<SQLTransactionCallback> callback,PassOwnPtr<SQLTransactionErrorCallback> errorCallback,PassOwnPtr<VoidCallback> successCallback,bool readOnly,const ChangeVersionData * changeVersionData)139 void Database::runTransaction(PassOwnPtr<SQLTransactionCallback> callback, PassOwnPtr<SQLTransactionErrorCallback> errorCallback,
140 PassOwnPtr<VoidCallback> successCallback, bool readOnly, const ChangeVersionData* changeVersionData)
141 {
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.
145 #if ASSERT_ENABLED
146 SQLTransactionErrorCallback* originalErrorCallback = errorCallback.get();
147 #endif
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);
153 if (callback) {
154 OwnPtr<SQLErrorData> error = SQLErrorData::create(SQLError::UNKNOWN_ERR, "database has been closed");
155 executionContext()->postTask(createCallbackTask(&callTransactionErrorCallback, callback.release(), error.release()));
156 }
157 }
158 }
159
160 // This object is constructed in a database thread, and destructed in the
161 // context thread.
162 class DeliverPendingCallbackTask FINAL : public ExecutionContextTask {
163 public:
create(PassRefPtrWillBeRawPtr<SQLTransaction> transaction)164 static PassOwnPtr<DeliverPendingCallbackTask> create(PassRefPtrWillBeRawPtr<SQLTransaction> transaction)
165 {
166 return adoptPtr(new DeliverPendingCallbackTask(transaction));
167 }
168
performTask(ExecutionContext *)169 virtual void performTask(ExecutionContext*) OVERRIDE
170 {
171 m_transaction->performPendingCallback();
172 }
173
174 private:
DeliverPendingCallbackTask(PassRefPtrWillBeRawPtr<SQLTransaction> transaction)175 DeliverPendingCallbackTask(PassRefPtrWillBeRawPtr<SQLTransaction> transaction)
176 : m_transaction(transaction)
177 {
178 }
179
180 RefPtrWillBeCrossThreadPersistent<SQLTransaction> m_transaction;
181 };
182
scheduleTransactionCallback(SQLTransaction * transaction)183 void Database::scheduleTransactionCallback(SQLTransaction* transaction)
184 {
185 executionContext()->postTask(DeliverPendingCallbackTask::create(transaction));
186 }
187
performGetTableNames()188 Vector<String> Database::performGetTableNames()
189 {
190 disableAuthorizer();
191
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());
195 enableAuthorizer();
196 return Vector<String>();
197 }
198
199 Vector<String> tableNames;
200 int result;
201 while ((result = statement.step()) == SQLResultRow) {
202 String name = statement.getColumnText(0);
203 if (name != databaseInfoTableName())
204 tableNames.append(name);
205 }
206
207 enableAuthorizer();
208
209 if (result != SQLResultDone) {
210 WTF_LOG_ERROR("Error getting tables for database %s", databaseDebugName().ascii().data());
211 return Vector<String>();
212 }
213
214 return tableNames;
215 }
216
tableNames()217 Vector<String> Database::tableNames()
218 {
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))
224 return result;
225
226 OwnPtr<DatabaseTableNamesTask> task = DatabaseTableNamesTask::create(this, &synchronizer, result);
227 databaseContext()->databaseThread()->scheduleTask(task.release());
228 synchronizer.waitForTaskCompletion();
229
230 return result;
231 }
232
securityOrigin() const233 SecurityOrigin* Database::securityOrigin() const
234 {
235 if (executionContext()->isContextThread())
236 return m_contextThreadSecurityOrigin.get();
237 if (databaseContext()->databaseThread()->isDatabaseThread())
238 return m_databaseThreadSecurityOrigin.get();
239 return 0;
240 }
241
reportStartTransactionResult(int errorSite,int webSqlErrorCode,int sqliteErrorCode)242 void Database::reportStartTransactionResult(int errorSite, int webSqlErrorCode, int sqliteErrorCode)
243 {
244 backend()->reportStartTransactionResult(errorSite, webSqlErrorCode, sqliteErrorCode);
245 }
246
reportCommitTransactionResult(int errorSite,int webSqlErrorCode,int sqliteErrorCode)247 void Database::reportCommitTransactionResult(int errorSite, int webSqlErrorCode, int sqliteErrorCode)
248 {
249 backend()->reportCommitTransactionResult(errorSite, webSqlErrorCode, sqliteErrorCode);
250 }
251
252
253 } // namespace WebCore
254