tizen beta release
[profile/ivi/webkit-efl.git] / Source / WebCore / storage / SQLTransactionSync.cpp
1 /*
2  * Copyright (C) 2010 Google 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 are
6  * met:
7  *
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
13  * distribution.
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.
17  *
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.
29  */
30
31 #include "config.h"
32 #include "SQLTransactionSync.h"
33
34 #if ENABLE(SQL_DATABASE)
35
36 #include "DatabaseAuthorizer.h"
37 #include "DatabaseSync.h"
38 #include "PlatformString.h"
39 #include "SQLException.h"
40 #include "SQLResultSet.h"
41 #include "SQLStatementSync.h"
42 #include "SQLTransactionClient.h"
43 #include "SQLTransactionSyncCallback.h"
44 #include "SQLValue.h"
45 #include "SQLiteTransaction.h"
46 #include "ScriptExecutionContext.h"
47 #include <wtf/PassRefPtr.h>
48 #include <wtf/RefPtr.h>
49
50 namespace WebCore {
51
52 PassRefPtr<SQLTransactionSync> SQLTransactionSync::create(DatabaseSync* db, PassRefPtr<SQLTransactionSyncCallback> callback, bool readOnly)
53 {
54     return adoptRef(new SQLTransactionSync(db, callback, readOnly));
55 }
56
57 SQLTransactionSync::SQLTransactionSync(DatabaseSync* db, PassRefPtr<SQLTransactionSyncCallback> callback, bool readOnly)
58     : m_database(db)
59     , m_callback(callback)
60     , m_readOnly(readOnly)
61     , m_hasVersionMismatch(false)
62     , m_modifiedDatabase(false)
63     , m_transactionClient(adoptPtr(new SQLTransactionClient()))
64 {
65     ASSERT(m_database->scriptExecutionContext()->isContextThread());
66 }
67
68 SQLTransactionSync::~SQLTransactionSync()
69 {
70     ASSERT(m_database->scriptExecutionContext()->isContextThread());
71     if (m_sqliteTransaction && m_sqliteTransaction->inProgress())
72         rollback();
73 }
74
75 PassRefPtr<SQLResultSet> SQLTransactionSync::executeSQL(const String& sqlStatement, const Vector<SQLValue>& arguments, ExceptionCode& ec)
76 {
77     ASSERT(m_database->scriptExecutionContext()->isContextThread());
78
79     m_database->setLastErrorMessage("");
80
81     if (!m_database->opened()) {
82         m_database->setLastErrorMessage("cannot executeSQL because the database is not open");
83         ec = SQLException::UNKNOWN_ERR;
84         return 0;
85     }
86
87     if (m_hasVersionMismatch) {
88         m_database->setLastErrorMessage("cannot executeSQL because there is a version mismatch");
89         ec = SQLException::VERSION_ERR;
90         return 0;
91     }
92
93     if (sqlStatement.isEmpty())
94         return 0;
95
96     int permissions = DatabaseAuthorizer::ReadWriteMask;
97     if (!m_database->scriptExecutionContext()->allowDatabaseAccess())
98       permissions |= DatabaseAuthorizer::NoAccessMask;
99     else if (m_readOnly)
100       permissions |= DatabaseAuthorizer::ReadOnlyMask;
101
102     SQLStatementSync statement(sqlStatement, arguments, permissions);
103
104     m_database->resetAuthorizer();
105     bool retryStatement = true;
106     RefPtr<SQLResultSet> resultSet;
107     while (retryStatement) {
108         retryStatement = false;
109         resultSet = statement.execute(m_database.get(), ec);
110         if (!resultSet) {
111             if (m_sqliteTransaction->wasRolledBackBySqlite())
112                 return 0;
113
114             if (ec == SQLException::QUOTA_ERR) {
115                 if (m_transactionClient->didExceedQuota(database())) {
116                     ec = 0;
117                     retryStatement = true;
118                 } else {
119                     m_database->setLastErrorMessage("there was not enough remaining storage space");
120                     return 0;
121                 }
122             }
123         }
124     }
125
126     if (m_database->lastActionChangedDatabase()) {
127         m_modifiedDatabase = true;
128         m_transactionClient->didExecuteStatement(database());
129     }
130
131     return resultSet.release();
132 }
133
134 ExceptionCode SQLTransactionSync::begin()
135 {
136     ASSERT(m_database->scriptExecutionContext()->isContextThread());
137     if (!m_database->opened()) {
138         m_database->reportStartTransactionResult(1, SQLException::UNKNOWN_ERR, 0);
139         m_database->setLastErrorMessage("cannot begin transaction because the database is not open");
140         return SQLException::UNKNOWN_ERR;
141     }
142
143     ASSERT(!m_database->sqliteDatabase().transactionInProgress());
144
145     // Set the maximum usage for this transaction if this transactions is not read-only.
146     if (!m_readOnly)
147         m_database->sqliteDatabase().setMaximumSize(m_database->maximumSize());
148
149     ASSERT(!m_sqliteTransaction);
150     m_sqliteTransaction = adoptPtr(new SQLiteTransaction(m_database->sqliteDatabase(), m_readOnly));
151
152     m_database->resetDeletes();
153     m_database->disableAuthorizer();
154     m_sqliteTransaction->begin();
155     m_database->enableAuthorizer();
156
157     // Check if begin() succeeded.
158     if (!m_sqliteTransaction->inProgress()) {
159         ASSERT(!m_database->sqliteDatabase().transactionInProgress());
160         m_database->reportStartTransactionResult(2, SQLException::DATABASE_ERR, m_database->sqliteDatabase().lastError());
161         m_database->setLastErrorMessage("unable to begin transaction",
162                                         m_database->sqliteDatabase().lastError(), m_database->sqliteDatabase().lastErrorMsg());
163         m_sqliteTransaction.clear();
164         return SQLException::DATABASE_ERR;
165     }
166
167     // Note: We intentionally retrieve the actual version even with an empty expected version.
168     // In multi-process browsers, we take this opportinutiy to update the cached value for
169     // the actual version. In single-process browsers, this is just a map lookup.
170     String actualVersion;
171     if (!m_database->getActualVersionForTransaction(actualVersion)) {
172         m_database->reportStartTransactionResult(3, SQLException::DATABASE_ERR, m_database->sqliteDatabase().lastError());
173         m_database->setLastErrorMessage("unable to read version",
174                                         m_database->sqliteDatabase().lastError(), m_database->sqliteDatabase().lastErrorMsg());
175         rollback();
176         return SQLException::DATABASE_ERR;
177     }
178     m_hasVersionMismatch = !m_database->expectedVersion().isEmpty()
179                            && (m_database->expectedVersion() != actualVersion);
180     m_database->reportStartTransactionResult(0, -1, 0); // OK
181     return 0;
182 }
183
184 ExceptionCode SQLTransactionSync::execute()
185 {
186     ASSERT(m_database->scriptExecutionContext()->isContextThread());
187     if (!m_database->opened() || (m_callback && !m_callback->handleEvent(this))) {
188         if (m_database->lastErrorMessage().isEmpty())
189             m_database->setLastErrorMessage("failed to execute transaction callback");
190         m_callback = 0;
191         return SQLException::UNKNOWN_ERR;
192     }
193
194     m_callback = 0;
195     return 0;
196 }
197
198 ExceptionCode SQLTransactionSync::commit()
199 {
200     ASSERT(m_database->scriptExecutionContext()->isContextThread());
201     if (!m_database->opened()) {
202         m_database->reportCommitTransactionResult(1, SQLException::UNKNOWN_ERR, 0);
203         m_database->setLastErrorMessage("unable to commit transaction because the database is not open.");
204         return SQLException::UNKNOWN_ERR;
205     }
206
207     ASSERT(m_sqliteTransaction);
208
209     m_database->disableAuthorizer();
210     m_sqliteTransaction->commit();
211     m_database->enableAuthorizer();
212
213     // If the commit failed, the transaction will still be marked as "in progress"
214     if (m_sqliteTransaction->inProgress()) {
215         m_database->reportCommitTransactionResult(2, SQLException::DATABASE_ERR, m_database->sqliteDatabase().lastError());
216         m_database->setLastErrorMessage("unable to commit transaction",
217                                         m_database->sqliteDatabase().lastError(), m_database->sqliteDatabase().lastErrorMsg());
218         return SQLException::DATABASE_ERR;
219     }
220
221     m_sqliteTransaction.clear();
222
223     // Vacuum the database if anything was deleted.
224     if (m_database->hadDeletes())
225         m_database->incrementalVacuumIfNeeded();
226
227     // The commit was successful. If the transaction modified this database, notify the delegates.
228     if (m_modifiedDatabase)
229         m_transactionClient->didCommitWriteTransaction(database());
230
231     m_database->reportCommitTransactionResult(0, -1, 0); // OK
232     return 0;
233 }
234
235 void SQLTransactionSync::rollback()
236 {
237     ASSERT(m_database->scriptExecutionContext()->isContextThread());
238     m_database->disableAuthorizer();
239     if (m_sqliteTransaction) {
240         m_sqliteTransaction->rollback();
241         m_sqliteTransaction.clear();
242     }
243     m_database->enableAuthorizer();
244
245     ASSERT(!m_database->sqliteDatabase().transactionInProgress());
246 }
247
248 } // namespace WebCore
249
250 #endif // ENABLE(SQL_DATABASE)