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(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 
create(DatabaseSync * db,PassRefPtr<SQLTransactionSyncCallback> callback,bool readOnly)52 PassRefPtr<SQLTransactionSync> SQLTransactionSync::create(DatabaseSync* db, PassRefPtr<SQLTransactionSyncCallback> callback, bool readOnly)
53 {
54     return adoptRef(new SQLTransactionSync(db, callback, readOnly));
55 }
56 
SQLTransactionSync(DatabaseSync * db,PassRefPtr<SQLTransactionSyncCallback> callback,bool readOnly)57 SQLTransactionSync::SQLTransactionSync(DatabaseSync* db, PassRefPtr<SQLTransactionSyncCallback> callback, bool readOnly)
58     : m_database(db)
59     , m_callback(callback)
60     , m_readOnly(readOnly)
61     , m_modifiedDatabase(false)
62     , m_transactionClient(adoptPtr(new SQLTransactionClient()))
63 {
64     ASSERT(m_database->scriptExecutionContext()->isContextThread());
65 }
66 
~SQLTransactionSync()67 SQLTransactionSync::~SQLTransactionSync()
68 {
69     ASSERT(m_database->scriptExecutionContext()->isContextThread());
70     if (m_sqliteTransaction && m_sqliteTransaction->inProgress())
71         rollback();
72 }
73 
executeSQL(const String & sqlStatement,const Vector<SQLValue> & arguments,ExceptionCode & ec)74 PassRefPtr<SQLResultSet> SQLTransactionSync::executeSQL(const String& sqlStatement, const Vector<SQLValue>& arguments, ExceptionCode& ec)
75 {
76     ASSERT(m_database->scriptExecutionContext()->isContextThread());
77     if (!m_database->opened()) {
78         ec = SQLException::UNKNOWN_ERR;
79         return 0;
80     }
81 
82     if (!m_database->versionMatchesExpected()) {
83         ec = SQLException::VERSION_ERR;
84         return 0;
85     }
86 
87     if (sqlStatement.isEmpty())
88         return 0;
89 
90     int permissions = DatabaseAuthorizer::ReadWriteMask;
91     if (!m_database->scriptExecutionContext()->allowDatabaseAccess())
92       permissions |= DatabaseAuthorizer::NoAccessMask;
93     else if (m_readOnly)
94       permissions |= DatabaseAuthorizer::ReadOnlyMask;
95 
96     SQLStatementSync statement(sqlStatement, arguments, permissions);
97 
98     m_database->resetAuthorizer();
99     bool retryStatement = true;
100     RefPtr<SQLResultSet> resultSet;
101     while (retryStatement) {
102         retryStatement = false;
103         resultSet = statement.execute(m_database.get(), ec);
104         if (!resultSet) {
105             if (m_sqliteTransaction->wasRolledBackBySqlite())
106                 return 0;
107 
108             if (ec == SQLException::QUOTA_ERR) {
109                 if (m_transactionClient->didExceedQuota(database())) {
110                     ec = 0;
111                     retryStatement = true;
112                 } else
113                     return 0;
114             }
115         }
116     }
117 
118     if (m_database->lastActionChangedDatabase()) {
119         m_modifiedDatabase = true;
120         m_transactionClient->didExecuteStatement(database());
121     }
122 
123     return resultSet.release();
124 }
125 
begin()126 ExceptionCode SQLTransactionSync::begin()
127 {
128     ASSERT(m_database->scriptExecutionContext()->isContextThread());
129     if (!m_database->opened())
130         return SQLException::UNKNOWN_ERR;
131 
132     ASSERT(!m_database->sqliteDatabase().transactionInProgress());
133 
134     // Set the maximum usage for this transaction if this transactions is not read-only.
135     if (!m_readOnly)
136         m_database->sqliteDatabase().setMaximumSize(m_database->maximumSize());
137 
138     ASSERT(!m_sqliteTransaction);
139     m_sqliteTransaction = adoptPtr(new SQLiteTransaction(m_database->sqliteDatabase(), m_readOnly));
140 
141     m_database->resetDeletes();
142     m_database->disableAuthorizer();
143     m_sqliteTransaction->begin();
144     m_database->enableAuthorizer();
145 
146     // Check if begin() succeeded.
147     if (!m_sqliteTransaction->inProgress()) {
148         ASSERT(!m_database->sqliteDatabase().transactionInProgress());
149         m_sqliteTransaction.clear();
150         return SQLException::DATABASE_ERR;
151     }
152 
153     return 0;
154 }
155 
execute()156 ExceptionCode SQLTransactionSync::execute()
157 {
158     ASSERT(m_database->scriptExecutionContext()->isContextThread());
159     if (!m_database->opened() || (m_callback && !m_callback->handleEvent(this))) {
160         m_callback = 0;
161         return SQLException::UNKNOWN_ERR;
162     }
163 
164     m_callback = 0;
165     return 0;
166 }
167 
commit()168 ExceptionCode SQLTransactionSync::commit()
169 {
170     ASSERT(m_database->scriptExecutionContext()->isContextThread());
171     if (!m_database->opened())
172         return SQLException::UNKNOWN_ERR;
173 
174     ASSERT(m_sqliteTransaction);
175 
176     m_database->disableAuthorizer();
177     m_sqliteTransaction->commit();
178     m_database->enableAuthorizer();
179 
180     // If the commit failed, the transaction will still be marked as "in progress"
181     if (m_sqliteTransaction->inProgress())
182         return SQLException::DATABASE_ERR;
183 
184     m_sqliteTransaction.clear();
185 
186     // Vacuum the database if anything was deleted.
187     if (m_database->hadDeletes())
188         m_database->incrementalVacuumIfNeeded();
189 
190     // The commit was successful. If the transaction modified this database, notify the delegates.
191     if (m_modifiedDatabase)
192         m_transactionClient->didCommitWriteTransaction(database());
193 
194     return 0;
195 }
196 
rollback()197 void SQLTransactionSync::rollback()
198 {
199     ASSERT(m_database->scriptExecutionContext()->isContextThread());
200     m_database->disableAuthorizer();
201     if (m_sqliteTransaction) {
202         m_sqliteTransaction->rollback();
203         m_sqliteTransaction.clear();
204     }
205     m_database->enableAuthorizer();
206 
207     ASSERT(!m_database->sqliteDatabase().transactionInProgress());
208 }
209 
210 } // namespace WebCore
211 
212 #endif // ENABLE(DATABASE)
213