4298a2274db0a2adac39171c006102eb89858fd8
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / modules / webdatabase / DatabaseSync.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 "modules/webdatabase/DatabaseSync.h"
33
34 #include "bindings/v8/ExceptionState.h"
35 #include "core/dom/ExceptionCode.h"
36 #include "core/dom/ExecutionContext.h"
37 #include "platform/Logging.h"
38 #include "modules/webdatabase/DatabaseCallback.h"
39 #include "modules/webdatabase/DatabaseContext.h"
40 #include "modules/webdatabase/DatabaseManager.h"
41 #include "modules/webdatabase/DatabaseTracker.h"
42 #include "modules/webdatabase/SQLError.h"
43 #include "modules/webdatabase/SQLTransactionSync.h"
44 #include "modules/webdatabase/SQLTransactionSyncCallback.h"
45 #include "wtf/PassRefPtr.h"
46 #include "wtf/RefPtr.h"
47 #include "wtf/text/CString.h"
48
49 namespace WebCore {
50
51 PassRefPtr<DatabaseSync> DatabaseSync::create(ExecutionContext*, PassRefPtr<DatabaseBackendBase> backend)
52 {
53     return static_cast<DatabaseSync*>(backend.get());
54 }
55
56 DatabaseSync::DatabaseSync(PassRefPtr<DatabaseContext> databaseContext,
57     const String& name, const String& expectedVersion, const String& displayName, unsigned long estimatedSize)
58     : DatabaseBase(databaseContext->executionContext())
59     , DatabaseBackendSync(databaseContext, name, expectedVersion, displayName, estimatedSize)
60 {
61     ScriptWrappable::init(this);
62     setFrontend(this);
63 }
64
65 DatabaseSync::~DatabaseSync()
66 {
67     ASSERT(m_executionContext->isContextThread());
68 }
69
70 void DatabaseSync::changeVersion(const String& oldVersion, const String& newVersion, PassOwnPtr<SQLTransactionSyncCallback> changeVersionCallback, ExceptionState& exceptionState)
71 {
72     ASSERT(m_executionContext->isContextThread());
73
74     if (sqliteDatabase().transactionInProgress()) {
75         reportChangeVersionResult(1, SQLError::DATABASE_ERR, 0);
76         setLastErrorMessage("unable to changeVersion from within a transaction");
77         exceptionState.throwDOMException(SQLDatabaseError, "Unable to change version from within a transaction.");
78         return;
79     }
80
81     RefPtr<SQLTransactionSync> transaction = SQLTransactionSync::create(this, changeVersionCallback, false);
82     transaction->begin(exceptionState);
83     if (exceptionState.hadException()) {
84         ASSERT(!lastErrorMessage().isEmpty());
85         return;
86     }
87
88     String actualVersion;
89     if (!getVersionFromDatabase(actualVersion)) {
90         reportChangeVersionResult(2, SQLError::UNKNOWN_ERR, sqliteDatabase().lastError());
91         setLastErrorMessage("unable to read the current version", sqliteDatabase().lastError(), sqliteDatabase().lastErrorMsg());
92         exceptionState.throwDOMException(UnknownError, SQLError::unknownErrorMessage);
93         return;
94     }
95
96     if (actualVersion != oldVersion) {
97         reportChangeVersionResult(3, SQLError::VERSION_ERR, 0);
98         setLastErrorMessage("current version of the database and `oldVersion` argument do not match");
99         exceptionState.throwDOMException(VersionError, SQLError::versionErrorMessage);
100         return;
101     }
102
103
104     transaction->execute(exceptionState);
105     if (exceptionState.hadException()) {
106         ASSERT(!lastErrorMessage().isEmpty());
107         return;
108     }
109
110     if (!setVersionInDatabase(newVersion)) {
111         reportChangeVersionResult(4, SQLError::UNKNOWN_ERR, sqliteDatabase().lastError());
112         setLastErrorMessage("unable to set the new version", sqliteDatabase().lastError(), sqliteDatabase().lastErrorMsg());
113         exceptionState.throwDOMException(UnknownError, SQLError::unknownErrorMessage);
114         return;
115     }
116
117     transaction->commit(exceptionState);
118     if (exceptionState.hadException()) {
119         ASSERT(!lastErrorMessage().isEmpty());
120         setCachedVersion(oldVersion);
121         return;
122     }
123
124     reportChangeVersionResult(0, -1, 0); // OK
125
126     setExpectedVersion(newVersion);
127     setLastErrorMessage("");
128 }
129
130 void DatabaseSync::transaction(PassOwnPtr<SQLTransactionSyncCallback> callback, ExceptionState& exceptionState)
131 {
132     runTransaction(callback, false, exceptionState);
133 }
134
135 void DatabaseSync::readTransaction(PassOwnPtr<SQLTransactionSyncCallback> callback, ExceptionState& exceptionState)
136 {
137     runTransaction(callback, true, exceptionState);
138 }
139
140 void DatabaseSync::rollbackTransaction(PassRefPtr<SQLTransactionSync> transaction)
141 {
142     ASSERT(!lastErrorMessage().isEmpty());
143     transaction->rollback();
144     setLastErrorMessage("");
145     return;
146 }
147
148 void DatabaseSync::runTransaction(PassOwnPtr<SQLTransactionSyncCallback> callback, bool readOnly, ExceptionState& exceptionState)
149 {
150     ASSERT(m_executionContext->isContextThread());
151
152     if (sqliteDatabase().transactionInProgress()) {
153         setLastErrorMessage("unable to start a transaction from within a transaction");
154         exceptionState.throwDOMException(SQLDatabaseError, "Unable to start a transaction from within a transaction.");
155         return;
156     }
157
158     RefPtr<SQLTransactionSync> transaction = SQLTransactionSync::create(this, callback, readOnly);
159     transaction->begin(exceptionState);
160     if (exceptionState.hadException()) {
161         rollbackTransaction(transaction);
162         return;
163     }
164
165     transaction->execute(exceptionState);
166     if (exceptionState.hadException()) {
167         rollbackTransaction(transaction);
168         return;
169     }
170
171     transaction->commit(exceptionState);
172     if (exceptionState.hadException()) {
173         rollbackTransaction(transaction);
174         return;
175     }
176
177     setLastErrorMessage("");
178 }
179
180 void DatabaseSync::closeImmediately()
181 {
182     ASSERT(m_executionContext->isContextThread());
183
184     if (!opened())
185         return;
186
187     logErrorMessage("forcibly closing database");
188     closeDatabase();
189 }
190
191 } // namespace WebCore