author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Fri, 12 Mar 2010 15:46:37 +0200 | |
branch | RCL_3 |
changeset 5 | d3bac044e0f0 |
parent 3 | 41300fa6a67c |
child 30 | 5dc02b23752f |
permissions | -rw-r--r-- |
0 | 1 |
/* |
2 |
* Copyright (C) 2007, 2008 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 "Database.h" |
|
31 |
||
32 |
#include <wtf/StdLibExtras.h> |
|
33 |
||
34 |
#if ENABLE(DATABASE) |
|
35 |
#include "ChangeVersionWrapper.h" |
|
36 |
#include "CString.h" |
|
37 |
#include "DatabaseAuthorizer.h" |
|
38 |
#include "DatabaseTask.h" |
|
39 |
#include "DatabaseThread.h" |
|
40 |
#include "DatabaseTracker.h" |
|
41 |
#include "Document.h" |
|
42 |
#include "ExceptionCode.h" |
|
43 |
#include "Frame.h" |
|
44 |
#include "InspectorController.h" |
|
45 |
#include "Logging.h" |
|
46 |
#include "NotImplemented.h" |
|
47 |
#include "Page.h" |
|
48 |
#include "OriginQuotaManager.h" |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
49 |
#include "ScriptController.h" |
0 | 50 |
#include "SQLiteDatabase.h" |
51 |
#include "SQLiteFileSystem.h" |
|
52 |
#include "SQLiteStatement.h" |
|
53 |
#include "SQLResultSet.h" |
|
54 |
#include "SQLTransactionClient.h" |
|
55 |
#include "SQLTransactionCoordinator.h" |
|
56 |
#include <wtf/MainThread.h> |
|
57 |
#endif |
|
58 |
||
59 |
#if USE(JSC) |
|
60 |
#include "JSDOMWindow.h" |
|
61 |
#endif |
|
62 |
||
63 |
namespace WebCore { |
|
64 |
||
65 |
// If we sleep for more the 30 seconds while blocked on SQLITE_BUSY, give up. |
|
66 |
static const int maxSqliteBusyWaitTime = 30000; |
|
67 |
||
68 |
const String& Database::databaseInfoTableName() |
|
69 |
{ |
|
70 |
DEFINE_STATIC_LOCAL(String, name, ("__WebKitDatabaseInfoTable__")); |
|
71 |
return name; |
|
72 |
} |
|
73 |
||
74 |
#if ENABLE(DATABASE) |
|
75 |
||
76 |
static Mutex& guidMutex() |
|
77 |
{ |
|
78 |
// Note: We don't have to use AtomicallyInitializedStatic here because |
|
79 |
// this function is called once in the constructor on the main thread |
|
80 |
// before any other threads that call this function are used. |
|
81 |
DEFINE_STATIC_LOCAL(Mutex, mutex, ()); |
|
82 |
return mutex; |
|
83 |
} |
|
84 |
||
85 |
typedef HashMap<int, String> GuidVersionMap; |
|
86 |
static GuidVersionMap& guidToVersionMap() |
|
87 |
{ |
|
88 |
DEFINE_STATIC_LOCAL(GuidVersionMap, map, ()); |
|
89 |
return map; |
|
90 |
} |
|
91 |
||
92 |
// NOTE: Caller must lock guidMutex(). |
|
93 |
static inline void updateGuidVersionMap(int guid, String newVersion) |
|
94 |
{ |
|
95 |
// Ensure the the mutex is locked. |
|
96 |
ASSERT(!guidMutex().tryLock()); |
|
97 |
||
98 |
// Note: It is not safe to put an empty string into the guidToVersionMap() map. |
|
99 |
// That's because the map is cross-thread, but empty strings are per-thread. |
|
100 |
// The copy() function makes a version of the string you can use on the current |
|
101 |
// thread, but we need a string we can keep in a cross-thread data structure. |
|
102 |
// FIXME: This is a quite-awkward restriction to have to program with. |
|
103 |
||
104 |
// Map null string to empty string (see comment above). |
|
105 |
guidToVersionMap().set(guid, newVersion.isEmpty() ? String() : newVersion.threadsafeCopy()); |
|
106 |
} |
|
107 |
||
108 |
typedef HashMap<int, HashSet<Database*>*> GuidDatabaseMap; |
|
109 |
static GuidDatabaseMap& guidToDatabaseMap() |
|
110 |
{ |
|
111 |
DEFINE_STATIC_LOCAL(GuidDatabaseMap, map, ()); |
|
112 |
return map; |
|
113 |
} |
|
114 |
||
115 |
static const String& databaseVersionKey() |
|
116 |
{ |
|
117 |
DEFINE_STATIC_LOCAL(String, key, ("WebKitDatabaseVersionKey")); |
|
118 |
return key; |
|
119 |
} |
|
120 |
||
121 |
static int guidForOriginAndName(const String& origin, const String& name); |
|
122 |
||
123 |
PassRefPtr<Database> Database::openDatabase(Document* document, const String& name, const String& expectedVersion, const String& displayName, unsigned long estimatedSize, ExceptionCode& e) |
|
124 |
{ |
|
125 |
if (!DatabaseTracker::tracker().canEstablishDatabase(document, name, displayName, estimatedSize)) { |
|
126 |
// FIXME: There should be an exception raised here in addition to returning a null Database object. The question has been raised with the WHATWG. |
|
127 |
LOG(StorageAPI, "Database %s for origin %s not allowed to be established", name.ascii().data(), document->securityOrigin()->toString().ascii().data()); |
|
128 |
return 0; |
|
129 |
} |
|
130 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
131 |
RefPtr<Database> database = adoptRef(new Database(document, name, expectedVersion, displayName, estimatedSize)); |
0 | 132 |
|
133 |
if (!database->openAndVerifyVersion(e)) { |
|
134 |
LOG(StorageAPI, "Failed to open and verify version (expected %s) of database %s", expectedVersion.ascii().data(), database->databaseDebugName().ascii().data()); |
|
135 |
return 0; |
|
136 |
} |
|
137 |
||
138 |
DatabaseTracker::tracker().setDatabaseDetails(document->securityOrigin(), name, displayName, estimatedSize); |
|
139 |
||
140 |
document->setHasOpenDatabases(); |
|
141 |
||
142 |
#if ENABLE(INSPECTOR) |
|
143 |
if (Page* page = document->frame()->page()) |
|
144 |
page->inspectorController()->didOpenDatabase(database.get(), document->securityOrigin()->host(), name, expectedVersion); |
|
145 |
#endif |
|
146 |
||
147 |
return database; |
|
148 |
} |
|
149 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
150 |
Database::Database(Document* document, const String& name, const String& expectedVersion, const String& displayName, unsigned long estimatedSize) |
0 | 151 |
: m_transactionInProgress(false) |
152 |
, m_document(document) |
|
153 |
, m_name(name.crossThreadString()) |
|
154 |
, m_guid(0) |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
155 |
, m_expectedVersion(expectedVersion.crossThreadString()) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
156 |
, m_displayName(displayName.crossThreadString()) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
157 |
, m_estimatedSize(estimatedSize) |
0 | 158 |
, m_deleted(false) |
159 |
, m_stopped(false) |
|
160 |
, m_opened(false) |
|
161 |
{ |
|
162 |
ASSERT(document); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
163 |
m_mainThreadSecurityOrigin = document->securityOrigin(); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
164 |
m_databaseThreadSecurityOrigin = m_mainThreadSecurityOrigin->threadsafeCopy(); |
0 | 165 |
|
166 |
if (m_name.isNull()) |
|
167 |
m_name = ""; |
|
168 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
169 |
ScriptController::initializeThreading(); |
0 | 170 |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
171 |
m_guid = guidForOriginAndName(m_mainThreadSecurityOrigin->toString(), name); |
0 | 172 |
|
173 |
{ |
|
174 |
MutexLocker locker(guidMutex()); |
|
175 |
||
176 |
HashSet<Database*>* hashSet = guidToDatabaseMap().get(m_guid); |
|
177 |
if (!hashSet) { |
|
178 |
hashSet = new HashSet<Database*>; |
|
179 |
guidToDatabaseMap().set(m_guid, hashSet); |
|
180 |
} |
|
181 |
||
182 |
hashSet->add(this); |
|
183 |
} |
|
184 |
||
185 |
ASSERT(m_document->databaseThread()); |
|
186 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
187 |
m_filename = DatabaseTracker::tracker().fullPathForDatabase(m_mainThreadSecurityOrigin.get(), m_name); |
0 | 188 |
|
189 |
DatabaseTracker::tracker().addOpenDatabase(this); |
|
190 |
m_document->addOpenDatabase(this); |
|
191 |
} |
|
192 |
||
193 |
Database::~Database() |
|
194 |
{ |
|
195 |
if (m_document->databaseThread()) |
|
196 |
m_document->databaseThread()->unscheduleDatabaseTasks(this); |
|
197 |
||
198 |
DatabaseTracker::tracker().removeOpenDatabase(this); |
|
199 |
m_document->removeOpenDatabase(this); |
|
200 |
} |
|
201 |
||
202 |
bool Database::openAndVerifyVersion(ExceptionCode& e) |
|
203 |
{ |
|
204 |
if (!m_document->databaseThread()) |
|
205 |
return false; |
|
206 |
m_databaseAuthorizer = DatabaseAuthorizer::create(); |
|
207 |
||
208 |
RefPtr<DatabaseOpenTask> task = DatabaseOpenTask::create(this); |
|
209 |
||
210 |
task->lockForSynchronousScheduling(); |
|
211 |
m_document->databaseThread()->scheduleImmediateTask(task); |
|
212 |
task->waitForSynchronousCompletion(); |
|
213 |
||
214 |
ASSERT(task->isComplete()); |
|
215 |
e = task->exceptionCode(); |
|
216 |
return task->openSuccessful(); |
|
217 |
} |
|
218 |
||
219 |
||
220 |
static bool retrieveTextResultFromDatabase(SQLiteDatabase& db, const String& query, String& resultString) |
|
221 |
{ |
|
222 |
SQLiteStatement statement(db, query); |
|
223 |
int result = statement.prepare(); |
|
224 |
||
225 |
if (result != SQLResultOk) { |
|
226 |
LOG_ERROR("Error (%i) preparing statement to read text result from database (%s)", result, query.ascii().data()); |
|
227 |
return false; |
|
228 |
} |
|
229 |
||
230 |
result = statement.step(); |
|
231 |
if (result == SQLResultRow) { |
|
232 |
resultString = statement.getColumnText(0); |
|
233 |
return true; |
|
234 |
} else if (result == SQLResultDone) { |
|
235 |
resultString = String(); |
|
236 |
return true; |
|
237 |
} else { |
|
238 |
LOG_ERROR("Error (%i) reading text result from database (%s)", result, query.ascii().data()); |
|
239 |
return false; |
|
240 |
} |
|
241 |
} |
|
242 |
||
243 |
bool Database::getVersionFromDatabase(String& version) |
|
244 |
{ |
|
245 |
DEFINE_STATIC_LOCAL(String, getVersionQuery, ("SELECT value FROM " + databaseInfoTableName() + " WHERE key = '" + databaseVersionKey() + "';")); |
|
246 |
||
247 |
m_databaseAuthorizer->disable(); |
|
248 |
||
249 |
bool result = retrieveTextResultFromDatabase(m_sqliteDatabase, getVersionQuery.threadsafeCopy(), version); |
|
250 |
if (!result) |
|
251 |
LOG_ERROR("Failed to retrieve version from database %s", databaseDebugName().ascii().data()); |
|
252 |
||
253 |
m_databaseAuthorizer->enable(); |
|
254 |
||
255 |
return result; |
|
256 |
} |
|
257 |
||
258 |
static bool setTextValueInDatabase(SQLiteDatabase& db, const String& query, const String& value) |
|
259 |
{ |
|
260 |
SQLiteStatement statement(db, query); |
|
261 |
int result = statement.prepare(); |
|
262 |
||
263 |
if (result != SQLResultOk) { |
|
264 |
LOG_ERROR("Failed to prepare statement to set value in database (%s)", query.ascii().data()); |
|
265 |
return false; |
|
266 |
} |
|
267 |
||
268 |
statement.bindText(1, value); |
|
269 |
||
270 |
result = statement.step(); |
|
271 |
if (result != SQLResultDone) { |
|
272 |
LOG_ERROR("Failed to step statement to set value in database (%s)", query.ascii().data()); |
|
273 |
return false; |
|
274 |
} |
|
275 |
||
276 |
return true; |
|
277 |
} |
|
278 |
||
279 |
bool Database::setVersionInDatabase(const String& version) |
|
280 |
{ |
|
281 |
// The INSERT will replace an existing entry for the database with the new version number, due to the UNIQUE ON CONFLICT REPLACE |
|
282 |
// clause in the CREATE statement (see Database::performOpenAndVerify()). |
|
283 |
DEFINE_STATIC_LOCAL(String, setVersionQuery, ("INSERT INTO " + databaseInfoTableName() + " (key, value) VALUES ('" + databaseVersionKey() + "', ?);")); |
|
284 |
||
285 |
m_databaseAuthorizer->disable(); |
|
286 |
||
287 |
bool result = setTextValueInDatabase(m_sqliteDatabase, setVersionQuery.threadsafeCopy(), version); |
|
288 |
if (!result) |
|
289 |
LOG_ERROR("Failed to set version %s in database (%s)", version.ascii().data(), setVersionQuery.ascii().data()); |
|
290 |
||
291 |
m_databaseAuthorizer->enable(); |
|
292 |
||
293 |
return result; |
|
294 |
} |
|
295 |
||
296 |
bool Database::versionMatchesExpected() const |
|
297 |
{ |
|
298 |
if (!m_expectedVersion.isEmpty()) { |
|
299 |
MutexLocker locker(guidMutex()); |
|
300 |
return m_expectedVersion == guidToVersionMap().get(m_guid); |
|
301 |
} |
|
302 |
||
303 |
return true; |
|
304 |
} |
|
305 |
||
306 |
void Database::markAsDeletedAndClose() |
|
307 |
{ |
|
308 |
if (m_deleted || !m_document->databaseThread()) |
|
309 |
return; |
|
310 |
||
311 |
LOG(StorageAPI, "Marking %s (%p) as deleted", stringIdentifier().ascii().data(), this); |
|
312 |
m_deleted = true; |
|
313 |
||
314 |
if (m_document->databaseThread()->terminationRequested()) { |
|
315 |
LOG(StorageAPI, "Database handle %p is on a terminated DatabaseThread, cannot be marked for normal closure\n", this); |
|
316 |
return; |
|
317 |
} |
|
318 |
||
319 |
m_document->databaseThread()->unscheduleDatabaseTasks(this); |
|
320 |
||
321 |
RefPtr<DatabaseCloseTask> task = DatabaseCloseTask::create(this); |
|
322 |
||
323 |
task->lockForSynchronousScheduling(); |
|
324 |
m_document->databaseThread()->scheduleImmediateTask(task); |
|
325 |
task->waitForSynchronousCompletion(); |
|
326 |
} |
|
327 |
||
328 |
void Database::close() |
|
329 |
{ |
|
330 |
if (m_opened) { |
|
331 |
ASSERT(m_document->databaseThread()); |
|
332 |
ASSERT(currentThread() == document()->databaseThread()->getThreadID()); |
|
333 |
m_sqliteDatabase.close(); |
|
334 |
m_document->databaseThread()->recordDatabaseClosed(this); |
|
335 |
m_opened = false; |
|
336 |
||
337 |
{ |
|
338 |
MutexLocker locker(guidMutex()); |
|
339 |
||
340 |
HashSet<Database*>* hashSet = guidToDatabaseMap().get(m_guid); |
|
341 |
ASSERT(hashSet); |
|
342 |
ASSERT(hashSet->contains(this)); |
|
343 |
hashSet->remove(this); |
|
344 |
if (hashSet->isEmpty()) { |
|
345 |
guidToDatabaseMap().remove(m_guid); |
|
346 |
delete hashSet; |
|
347 |
guidToVersionMap().remove(m_guid); |
|
348 |
} |
|
349 |
} |
|
350 |
} |
|
351 |
} |
|
352 |
||
353 |
void Database::stop() |
|
354 |
{ |
|
355 |
// FIXME: The net effect of the following code is to remove all pending transactions and statements, but allow the current statement |
|
356 |
// to run to completion. In the future we can use the sqlite3_progress_handler or sqlite3_interrupt interfaces to cancel the current |
|
357 |
// statement in response to close(), as well. |
|
358 |
||
359 |
// This method is meant to be used as an analog to cancelling a loader, and is used when a document is shut down as the result of |
|
360 |
// a page load or closing the page |
|
361 |
m_stopped = true; |
|
362 |
||
363 |
{ |
|
364 |
MutexLocker locker(m_transactionInProgressMutex); |
|
365 |
m_transactionQueue.kill(); |
|
366 |
m_transactionInProgress = false; |
|
367 |
} |
|
368 |
} |
|
369 |
||
370 |
unsigned long long Database::maximumSize() const |
|
371 |
{ |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
372 |
return DatabaseTracker::tracker().getMaxSizeForDatabase(this); |
0 | 373 |
} |
374 |
||
375 |
void Database::disableAuthorizer() |
|
376 |
{ |
|
377 |
ASSERT(m_databaseAuthorizer); |
|
378 |
m_databaseAuthorizer->disable(); |
|
379 |
} |
|
380 |
||
381 |
void Database::enableAuthorizer() |
|
382 |
{ |
|
383 |
ASSERT(m_databaseAuthorizer); |
|
384 |
m_databaseAuthorizer->enable(); |
|
385 |
} |
|
386 |
||
387 |
void Database::setAuthorizerReadOnly() |
|
388 |
{ |
|
389 |
ASSERT(m_databaseAuthorizer); |
|
390 |
m_databaseAuthorizer->setReadOnly(); |
|
391 |
} |
|
392 |
||
393 |
static int guidForOriginAndName(const String& origin, const String& name) |
|
394 |
{ |
|
395 |
String stringID; |
|
396 |
if (origin.endsWith("/")) |
|
397 |
stringID = origin + name; |
|
398 |
else |
|
399 |
stringID = origin + "/" + name; |
|
400 |
||
401 |
// Note: We don't have to use AtomicallyInitializedStatic here because |
|
402 |
// this function is called once in the constructor on the main thread |
|
403 |
// before any other threads that call this function are used. |
|
404 |
DEFINE_STATIC_LOCAL(Mutex, stringIdentifierMutex, ()); |
|
405 |
MutexLocker locker(stringIdentifierMutex); |
|
406 |
typedef HashMap<String, int> IDGuidMap; |
|
407 |
DEFINE_STATIC_LOCAL(IDGuidMap, stringIdentifierToGUIDMap, ()); |
|
408 |
int guid = stringIdentifierToGUIDMap.get(stringID); |
|
409 |
if (!guid) { |
|
410 |
static int currentNewGUID = 1; |
|
411 |
guid = currentNewGUID++; |
|
412 |
stringIdentifierToGUIDMap.set(stringID, guid); |
|
413 |
} |
|
414 |
||
415 |
return guid; |
|
416 |
} |
|
417 |
||
418 |
void Database::resetAuthorizer() |
|
419 |
{ |
|
420 |
if (m_databaseAuthorizer) |
|
421 |
m_databaseAuthorizer->reset(); |
|
422 |
} |
|
423 |
||
424 |
void Database::performPolicyChecks() |
|
425 |
{ |
|
426 |
// FIXME: Code similar to the following will need to be run to enforce the per-origin size limit the spec mandates. |
|
427 |
// Additionally, we might need a way to pause the database thread while the UA prompts the user for permission to |
|
428 |
// increase the size limit |
|
429 |
||
430 |
/* |
|
431 |
if (m_databaseAuthorizer->lastActionIncreasedSize()) |
|
432 |
DatabaseTracker::scheduleFileSizeCheckOnMainThread(this); |
|
433 |
*/ |
|
434 |
||
435 |
notImplemented(); |
|
436 |
} |
|
437 |
||
438 |
bool Database::performOpenAndVerify(ExceptionCode& e) |
|
439 |
{ |
|
440 |
if (!m_sqliteDatabase.open(m_filename)) { |
|
441 |
LOG_ERROR("Unable to open database at path %s", m_filename.ascii().data()); |
|
442 |
e = INVALID_STATE_ERR; |
|
443 |
return false; |
|
444 |
} |
|
445 |
||
446 |
m_opened = true; |
|
447 |
if (m_document->databaseThread()) |
|
448 |
m_document->databaseThread()->recordDatabaseOpen(this); |
|
449 |
||
450 |
ASSERT(m_databaseAuthorizer); |
|
451 |
m_sqliteDatabase.setAuthorizer(m_databaseAuthorizer); |
|
452 |
m_sqliteDatabase.setBusyTimeout(maxSqliteBusyWaitTime); |
|
453 |
||
454 |
String currentVersion; |
|
455 |
{ |
|
456 |
MutexLocker locker(guidMutex()); |
|
457 |
||
458 |
GuidVersionMap::iterator entry = guidToVersionMap().find(m_guid); |
|
459 |
if (entry != guidToVersionMap().end()) { |
|
460 |
// Map null string to empty string (see updateGuidVersionMap()). |
|
461 |
currentVersion = entry->second.isNull() ? String("") : entry->second; |
|
462 |
LOG(StorageAPI, "Current cached version for guid %i is %s", m_guid, currentVersion.ascii().data()); |
|
463 |
} else { |
|
464 |
LOG(StorageAPI, "No cached version for guid %i", m_guid); |
|
465 |
||
466 |
if (!m_sqliteDatabase.tableExists(databaseInfoTableName())) { |
|
467 |
if (!m_sqliteDatabase.executeCommand("CREATE TABLE " + databaseInfoTableName() + " (key TEXT NOT NULL ON CONFLICT FAIL UNIQUE ON CONFLICT REPLACE,value TEXT NOT NULL ON CONFLICT FAIL);")) { |
|
468 |
LOG_ERROR("Unable to create table %s in database %s", databaseInfoTableName().ascii().data(), databaseDebugName().ascii().data()); |
|
469 |
e = INVALID_STATE_ERR; |
|
470 |
return false; |
|
471 |
} |
|
472 |
} |
|
473 |
||
474 |
if (!getVersionFromDatabase(currentVersion)) { |
|
475 |
LOG_ERROR("Failed to get current version from database %s", databaseDebugName().ascii().data()); |
|
476 |
e = INVALID_STATE_ERR; |
|
477 |
return false; |
|
478 |
} |
|
479 |
if (currentVersion.length()) { |
|
480 |
LOG(StorageAPI, "Retrieved current version %s from database %s", currentVersion.ascii().data(), databaseDebugName().ascii().data()); |
|
481 |
} else { |
|
482 |
LOG(StorageAPI, "Setting version %s in database %s that was just created", m_expectedVersion.ascii().data(), databaseDebugName().ascii().data()); |
|
483 |
if (!setVersionInDatabase(m_expectedVersion)) { |
|
484 |
LOG_ERROR("Failed to set version %s in database %s", m_expectedVersion.ascii().data(), databaseDebugName().ascii().data()); |
|
485 |
e = INVALID_STATE_ERR; |
|
486 |
return false; |
|
487 |
} |
|
488 |
currentVersion = m_expectedVersion; |
|
489 |
} |
|
490 |
||
491 |
updateGuidVersionMap(m_guid, currentVersion); |
|
492 |
} |
|
493 |
} |
|
494 |
||
495 |
if (currentVersion.isNull()) { |
|
496 |
LOG(StorageAPI, "Database %s does not have its version set", databaseDebugName().ascii().data()); |
|
497 |
currentVersion = ""; |
|
498 |
} |
|
499 |
||
500 |
// FIXME: For now, the spec says that if the database has no version, it is valid for any "Expected version" string. That seems silly and I think it should be |
|
501 |
// changed, and here's where we would change it |
|
502 |
if (m_expectedVersion.length()) { |
|
503 |
if (currentVersion.length() && m_expectedVersion != currentVersion) { |
|
504 |
LOG(StorageAPI, "page expects version %s from database %s, which actually has version name %s - openDatabase() call will fail", m_expectedVersion.ascii().data(), |
|
505 |
databaseDebugName().ascii().data(), currentVersion.ascii().data()); |
|
506 |
e = INVALID_STATE_ERR; |
|
507 |
return false; |
|
508 |
} |
|
509 |
} |
|
510 |
||
511 |
return true; |
|
512 |
} |
|
513 |
||
514 |
void Database::changeVersion(const String& oldVersion, const String& newVersion, |
|
515 |
PassRefPtr<SQLTransactionCallback> callback, PassRefPtr<SQLTransactionErrorCallback> errorCallback, |
|
516 |
PassRefPtr<VoidCallback> successCallback) |
|
517 |
{ |
|
518 |
m_transactionQueue.append(SQLTransaction::create(this, callback, errorCallback, successCallback, ChangeVersionWrapper::create(oldVersion, newVersion))); |
|
519 |
MutexLocker locker(m_transactionInProgressMutex); |
|
520 |
if (!m_transactionInProgress) |
|
521 |
scheduleTransaction(); |
|
522 |
} |
|
523 |
||
524 |
void Database::transaction(PassRefPtr<SQLTransactionCallback> callback, PassRefPtr<SQLTransactionErrorCallback> errorCallback, |
|
525 |
PassRefPtr<VoidCallback> successCallback, bool readOnly) |
|
526 |
{ |
|
527 |
m_transactionQueue.append(SQLTransaction::create(this, callback, errorCallback, successCallback, 0, readOnly)); |
|
528 |
MutexLocker locker(m_transactionInProgressMutex); |
|
529 |
if (!m_transactionInProgress) |
|
530 |
scheduleTransaction(); |
|
531 |
} |
|
532 |
||
533 |
void Database::scheduleTransaction() |
|
534 |
{ |
|
535 |
ASSERT(!m_transactionInProgressMutex.tryLock()); // Locked by caller. |
|
536 |
RefPtr<SQLTransaction> transaction; |
|
537 |
if (m_transactionQueue.tryGetMessage(transaction) && m_document->databaseThread()) { |
|
538 |
RefPtr<DatabaseTransactionTask> task = DatabaseTransactionTask::create(transaction); |
|
539 |
LOG(StorageAPI, "Scheduling DatabaseTransactionTask %p for transaction %p\n", task.get(), task->transaction()); |
|
540 |
m_transactionInProgress = true; |
|
541 |
m_document->databaseThread()->scheduleTask(task.release()); |
|
542 |
} else |
|
543 |
m_transactionInProgress = false; |
|
544 |
} |
|
545 |
||
546 |
void Database::scheduleTransactionStep(SQLTransaction* transaction, bool immediately) |
|
547 |
{ |
|
548 |
if (!m_document->databaseThread()) |
|
549 |
return; |
|
550 |
||
551 |
RefPtr<DatabaseTransactionTask> task = DatabaseTransactionTask::create(transaction); |
|
552 |
LOG(StorageAPI, "Scheduling DatabaseTransactionTask %p for the transaction step\n", task.get()); |
|
553 |
if (immediately) |
|
554 |
m_document->databaseThread()->scheduleImmediateTask(task.release()); |
|
555 |
else |
|
556 |
m_document->databaseThread()->scheduleTask(task.release()); |
|
557 |
} |
|
558 |
||
559 |
void Database::scheduleTransactionCallback(SQLTransaction* transaction) |
|
560 |
{ |
|
561 |
transaction->ref(); |
|
562 |
callOnMainThread(deliverPendingCallback, transaction); |
|
563 |
} |
|
564 |
||
565 |
Vector<String> Database::performGetTableNames() |
|
566 |
{ |
|
567 |
disableAuthorizer(); |
|
568 |
||
569 |
SQLiteStatement statement(m_sqliteDatabase, "SELECT name FROM sqlite_master WHERE type='table';"); |
|
570 |
if (statement.prepare() != SQLResultOk) { |
|
571 |
LOG_ERROR("Unable to retrieve list of tables for database %s", databaseDebugName().ascii().data()); |
|
572 |
enableAuthorizer(); |
|
573 |
return Vector<String>(); |
|
574 |
} |
|
575 |
||
576 |
Vector<String> tableNames; |
|
577 |
int result; |
|
578 |
while ((result = statement.step()) == SQLResultRow) { |
|
579 |
String name = statement.getColumnText(0); |
|
580 |
if (name != databaseInfoTableName()) |
|
581 |
tableNames.append(name); |
|
582 |
} |
|
583 |
||
584 |
enableAuthorizer(); |
|
585 |
||
586 |
if (result != SQLResultDone) { |
|
587 |
LOG_ERROR("Error getting tables for database %s", databaseDebugName().ascii().data()); |
|
588 |
return Vector<String>(); |
|
589 |
} |
|
590 |
||
591 |
return tableNames; |
|
592 |
} |
|
593 |
||
594 |
SQLTransactionClient* Database::transactionClient() const |
|
595 |
{ |
|
596 |
return m_document->databaseThread()->transactionClient(); |
|
597 |
} |
|
598 |
||
599 |
SQLTransactionCoordinator* Database::transactionCoordinator() const |
|
600 |
{ |
|
601 |
return m_document->databaseThread()->transactionCoordinator(); |
|
602 |
} |
|
603 |
||
604 |
String Database::version() const |
|
605 |
{ |
|
606 |
if (m_deleted) |
|
607 |
return String(); |
|
608 |
MutexLocker locker(guidMutex()); |
|
609 |
return guidToVersionMap().get(m_guid).threadsafeCopy(); |
|
610 |
} |
|
611 |
||
612 |
void Database::deliverPendingCallback(void* context) |
|
613 |
{ |
|
614 |
SQLTransaction* transaction = static_cast<SQLTransaction*>(context); |
|
615 |
transaction->performPendingCallback(); |
|
616 |
transaction->deref(); // Was ref'd in scheduleTransactionCallback(). |
|
617 |
} |
|
618 |
||
619 |
Vector<String> Database::tableNames() |
|
620 |
{ |
|
621 |
if (!m_document->databaseThread()) |
|
622 |
return Vector<String>(); |
|
623 |
RefPtr<DatabaseTableNamesTask> task = DatabaseTableNamesTask::create(this); |
|
624 |
||
625 |
task->lockForSynchronousScheduling(); |
|
626 |
m_document->databaseThread()->scheduleImmediateTask(task); |
|
627 |
task->waitForSynchronousCompletion(); |
|
628 |
||
629 |
return task->tableNames(); |
|
630 |
} |
|
631 |
||
632 |
void Database::setExpectedVersion(const String& version) |
|
633 |
{ |
|
634 |
m_expectedVersion = version.threadsafeCopy(); |
|
635 |
// Update the in memory database version map. |
|
636 |
MutexLocker locker(guidMutex()); |
|
637 |
updateGuidVersionMap(m_guid, version); |
|
638 |
} |
|
639 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
640 |
SecurityOrigin* Database::securityOrigin() const |
0 | 641 |
{ |
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
642 |
if (isMainThread()) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
643 |
return m_mainThreadSecurityOrigin.get(); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
644 |
if (currentThread() == document()->databaseThread()->getThreadID()) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
645 |
return m_databaseThreadSecurityOrigin.get(); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
646 |
return 0; |
0 | 647 |
} |
648 |
||
649 |
String Database::stringIdentifier() const |
|
650 |
{ |
|
651 |
// Return a deep copy for ref counting thread safety |
|
652 |
return m_name.threadsafeCopy(); |
|
653 |
} |
|
654 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
655 |
String Database::displayName() const |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
656 |
{ |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
657 |
// Return a deep copy for ref counting thread safety |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
658 |
return m_displayName.threadsafeCopy(); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
659 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
660 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
661 |
unsigned long Database::estimatedSize() const |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
662 |
{ |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
663 |
return m_estimatedSize; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
664 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
665 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
666 |
String Database::fileName() const |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
667 |
{ |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
668 |
// Return a deep copy for ref counting thread safety |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
669 |
return m_filename.threadsafeCopy(); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
670 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
671 |
|
0 | 672 |
#endif |
673 |
||
674 |
} |