|
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 "DatabaseSync.h" |
|
33 |
|
34 #if ENABLE(DATABASE) |
|
35 #include "DatabaseCallback.h" |
|
36 #include "DatabaseTracker.h" |
|
37 #include "Logging.h" |
|
38 #include "SQLException.h" |
|
39 #include "SQLTransactionSync.h" |
|
40 #include "SQLTransactionSyncCallback.h" |
|
41 #include "ScriptExecutionContext.h" |
|
42 #include "SecurityOrigin.h" |
|
43 #include <wtf/PassRefPtr.h> |
|
44 #include <wtf/RefPtr.h> |
|
45 |
|
46 namespace WebCore { |
|
47 |
|
48 PassRefPtr<DatabaseSync> DatabaseSync::openDatabaseSync(ScriptExecutionContext* context, const String& name, const String& expectedVersion, const String& displayName, |
|
49 unsigned long estimatedSize, PassRefPtr<DatabaseCallback> creationCallback, ExceptionCode& ec) |
|
50 { |
|
51 ASSERT(context->isContextThread()); |
|
52 |
|
53 if (!DatabaseTracker::tracker().canEstablishDatabase(context, name, displayName, estimatedSize)) { |
|
54 LOG(StorageAPI, "Database %s for origin %s not allowed to be established", name.ascii().data(), context->securityOrigin()->toString().ascii().data()); |
|
55 return 0; |
|
56 } |
|
57 |
|
58 RefPtr<DatabaseSync> database = adoptRef(new DatabaseSync(context, name, expectedVersion, displayName, estimatedSize)); |
|
59 |
|
60 if (!database->performOpenAndVerify(!creationCallback, ec)) { |
|
61 LOG(StorageAPI, "Failed to open and verify version (expected %s) of database %s", expectedVersion.ascii().data(), database->databaseDebugName().ascii().data()); |
|
62 DatabaseTracker::tracker().removeOpenDatabase(database.get()); |
|
63 return 0; |
|
64 } |
|
65 |
|
66 DatabaseTracker::tracker().setDatabaseDetails(context->securityOrigin(), name, displayName, estimatedSize); |
|
67 |
|
68 if (database->isNew() && creationCallback.get()) { |
|
69 database->m_expectedVersion = ""; |
|
70 LOG(StorageAPI, "Invoking the creation callback for database %p\n", database.get()); |
|
71 creationCallback->handleEvent(context, database.get()); |
|
72 } |
|
73 |
|
74 return database; |
|
75 } |
|
76 |
|
77 DatabaseSync::DatabaseSync(ScriptExecutionContext* context, const String& name, const String& expectedVersion, |
|
78 const String& displayName, unsigned long estimatedSize) |
|
79 : AbstractDatabase(context, name, expectedVersion, displayName, estimatedSize) |
|
80 { |
|
81 } |
|
82 |
|
83 DatabaseSync::~DatabaseSync() |
|
84 { |
|
85 ASSERT(m_scriptExecutionContext->isContextThread()); |
|
86 |
|
87 if (opened()) { |
|
88 DatabaseTracker::tracker().removeOpenDatabase(this); |
|
89 closeDatabase(); |
|
90 } |
|
91 } |
|
92 |
|
93 void DatabaseSync::changeVersion(const String& oldVersion, const String& newVersion, PassRefPtr<SQLTransactionSyncCallback> changeVersionCallback, ExceptionCode& ec) |
|
94 { |
|
95 ASSERT(m_scriptExecutionContext->isContextThread()); |
|
96 |
|
97 if (sqliteDatabase().transactionInProgress()) { |
|
98 ec = SQLException::DATABASE_ERR; |
|
99 return; |
|
100 } |
|
101 |
|
102 RefPtr<SQLTransactionSync> transaction = SQLTransactionSync::create(this, changeVersionCallback, false); |
|
103 if ((ec = transaction->begin())) |
|
104 return; |
|
105 |
|
106 String actualVersion; |
|
107 if (!getVersionFromDatabase(actualVersion)) { |
|
108 ec = SQLException::UNKNOWN_ERR; |
|
109 return; |
|
110 } |
|
111 |
|
112 if (actualVersion != oldVersion) { |
|
113 ec = SQLException::VERSION_ERR; |
|
114 return; |
|
115 } |
|
116 |
|
117 if ((ec = transaction->execute())) |
|
118 return; |
|
119 |
|
120 if (!setVersionInDatabase(newVersion)) { |
|
121 ec = SQLException::UNKNOWN_ERR; |
|
122 return; |
|
123 } |
|
124 |
|
125 if ((ec = transaction->commit())) |
|
126 return; |
|
127 |
|
128 setExpectedVersion(newVersion); |
|
129 } |
|
130 |
|
131 void DatabaseSync::transaction(PassRefPtr<SQLTransactionSyncCallback> callback, bool readOnly, ExceptionCode& ec) |
|
132 { |
|
133 ASSERT(m_scriptExecutionContext->isContextThread()); |
|
134 |
|
135 if (sqliteDatabase().transactionInProgress()) { |
|
136 ec = SQLException::DATABASE_ERR; |
|
137 return; |
|
138 } |
|
139 |
|
140 RefPtr<SQLTransactionSync> transaction = SQLTransactionSync::create(this, callback, readOnly); |
|
141 if ((ec = transaction->begin()) || (ec = transaction->execute()) || (ec = transaction->commit())) |
|
142 transaction->rollback(); |
|
143 } |
|
144 |
|
145 void DatabaseSync::markAsDeletedAndClose() |
|
146 { |
|
147 // FIXME: need to do something similar to closeImmediately(), but in a sync way |
|
148 } |
|
149 |
|
150 class CloseSyncDatabaseOnContextThreadTask : public ScriptExecutionContext::Task { |
|
151 public: |
|
152 static PassOwnPtr<CloseSyncDatabaseOnContextThreadTask> create(PassRefPtr<DatabaseSync> database) |
|
153 { |
|
154 return new CloseSyncDatabaseOnContextThreadTask(database); |
|
155 } |
|
156 |
|
157 virtual void performTask(ScriptExecutionContext*) |
|
158 { |
|
159 m_database->closeImmediately(); |
|
160 } |
|
161 |
|
162 private: |
|
163 CloseSyncDatabaseOnContextThreadTask(PassRefPtr<DatabaseSync> database) |
|
164 : m_database(database) |
|
165 { |
|
166 } |
|
167 |
|
168 RefPtr<DatabaseSync> m_database; |
|
169 }; |
|
170 |
|
171 void DatabaseSync::closeImmediately() |
|
172 { |
|
173 if (!m_scriptExecutionContext->isContextThread()) { |
|
174 m_scriptExecutionContext->postTask(CloseSyncDatabaseOnContextThreadTask::create(this)); |
|
175 return; |
|
176 } |
|
177 |
|
178 if (!opened()) |
|
179 return; |
|
180 |
|
181 DatabaseTracker::tracker().removeOpenDatabase(this); |
|
182 |
|
183 closeDatabase(); |
|
184 } |
|
185 |
|
186 } // namespace WebCore |
|
187 |
|
188 #endif // ENABLE(DATABASE) |