Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / modules / webdatabase / Database.cpp
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/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"
56
57 namespace blink {
58
59 PassRefPtrWillBeRawPtr<Database> Database::create(ExecutionContext*, PassRefPtrWillBeRawPtr<DatabaseBackendBase> backend)
60 {
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());
66 }
67
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())
73 {
74     ScriptWrappable::init(this);
75     m_databaseThreadSecurityOrigin = m_contextThreadSecurityOrigin->isolatedCopy();
76     setFrontend(this);
77
78     ASSERT(m_databaseContext->databaseThread());
79 }
80
81 Database::~Database()
82 {
83 }
84
85 void Database::trace(Visitor* visitor)
86 {
87     visitor->trace(m_databaseContext);
88     DatabaseBackend::trace(visitor);
89 }
90
91 Database* Database::from(DatabaseBackend* backend)
92 {
93     return static_cast<Database*>(backend->m_frontend);
94 }
95
96 PassRefPtrWillBeRawPtr<DatabaseBackend> Database::backend()
97 {
98     return this;
99 }
100
101 String Database::version() const
102 {
103     return DatabaseBackendBase::version();
104 }
105
106 void Database::closeImmediately()
107 {
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));
113     }
114 }
115
116 void Database::changeVersion(const String& oldVersion, const String& newVersion, PassOwnPtr<SQLTransactionCallback> callback, PassOwnPtr<SQLTransactionErrorCallback> errorCallback, PassOwnPtr<VoidCallback> successCallback)
117 {
118     ChangeVersionData data(oldVersion, newVersion);
119     runTransaction(callback, errorCallback, successCallback, false, &data);
120 }
121
122 void Database::transaction(PassOwnPtr<SQLTransactionCallback> callback, PassOwnPtr<SQLTransactionErrorCallback> errorCallback, PassOwnPtr<VoidCallback> successCallback)
123 {
124     runTransaction(callback, errorCallback, successCallback, false);
125 }
126
127 void Database::readTransaction(PassOwnPtr<SQLTransactionCallback> callback, PassOwnPtr<SQLTransactionErrorCallback> errorCallback, PassOwnPtr<VoidCallback> successCallback)
128 {
129     runTransaction(callback, errorCallback, successCallback, true);
130 }
131
132 static void callTransactionErrorCallback(ExecutionContext*, PassOwnPtr<SQLTransactionErrorCallback> callback, PassOwnPtr<SQLErrorData> errorData)
133 {
134     RefPtrWillBeRawPtr<SQLError> error = SQLError::create(*errorData);
135     callback->handleEvent(error.get());
136 }
137
138 void Database::runTransaction(PassOwnPtr<SQLTransactionCallback> callback, PassOwnPtr<SQLTransactionErrorCallback> errorCallback,
139     PassOwnPtr<VoidCallback> successCallback, bool readOnly, const ChangeVersionData* changeVersionData)
140 {
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.
144 #if ENABLE(ASSERT)
145     SQLTransactionErrorCallback* originalErrorCallback = errorCallback.get();
146 #endif
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);
152         if (callback) {
153             OwnPtr<SQLErrorData> error = SQLErrorData::create(SQLError::UNKNOWN_ERR, "database has been closed");
154             executionContext()->postTask(createCrossThreadTask(&callTransactionErrorCallback, callback.release(), error.release()));
155         }
156     }
157 }
158
159 // This object is constructed in a database thread, and destructed in the
160 // context thread.
161 class DeliverPendingCallbackTask FINAL : public ExecutionContextTask {
162 public:
163     static PassOwnPtr<DeliverPendingCallbackTask> create(PassRefPtrWillBeRawPtr<SQLTransaction> transaction)
164     {
165         return adoptPtr(new DeliverPendingCallbackTask(transaction));
166     }
167
168     virtual void performTask(ExecutionContext*) OVERRIDE
169     {
170         m_transaction->performPendingCallback();
171     }
172
173 private:
174     DeliverPendingCallbackTask(PassRefPtrWillBeRawPtr<SQLTransaction> transaction)
175         : m_transaction(transaction)
176     {
177     }
178
179     RefPtrWillBeCrossThreadPersistent<SQLTransaction> m_transaction;
180 };
181
182 void Database::scheduleTransactionCallback(SQLTransaction* transaction)
183 {
184     executionContext()->postTask(DeliverPendingCallbackTask::create(transaction));
185 }
186
187 Vector<String> Database::performGetTableNames()
188 {
189     disableAuthorizer();
190
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());
194         enableAuthorizer();
195         return Vector<String>();
196     }
197
198     Vector<String> tableNames;
199     int result;
200     while ((result = statement.step()) == SQLResultRow) {
201         String name = statement.getColumnText(0);
202         if (name != databaseInfoTableName())
203             tableNames.append(name);
204     }
205
206     enableAuthorizer();
207
208     if (result != SQLResultDone) {
209         WTF_LOG_ERROR("Error getting tables for database %s", databaseDebugName().ascii().data());
210         return Vector<String>();
211     }
212
213     return tableNames;
214 }
215
216 Vector<String> Database::tableNames()
217 {
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))
223         return result;
224
225     OwnPtr<DatabaseTableNamesTask> task = DatabaseTableNamesTask::create(this, &synchronizer, result);
226     databaseContext()->databaseThread()->scheduleTask(task.release());
227     synchronizer.waitForTaskCompletion();
228
229     return result;
230 }
231
232 SecurityOrigin* Database::securityOrigin() const
233 {
234     if (executionContext()->isContextThread())
235         return m_contextThreadSecurityOrigin.get();
236     if (databaseContext()->databaseThread()->isDatabaseThread())
237         return m_databaseThreadSecurityOrigin.get();
238     return 0;
239 }
240
241 void Database::reportStartTransactionResult(int errorSite, int webSqlErrorCode, int sqliteErrorCode)
242 {
243     backend()->reportStartTransactionResult(errorSite, webSqlErrorCode, sqliteErrorCode);
244 }
245
246 void Database::reportCommitTransactionResult(int errorSite, int webSqlErrorCode, int sqliteErrorCode)
247 {
248     backend()->reportCommitTransactionResult(errorSite, webSqlErrorCode, sqliteErrorCode);
249 }
250
251
252 } // namespace blink