• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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"
49 #include "SQLiteDatabase.h"
50 #include "SQLiteFileSystem.h"
51 #include "SQLiteStatement.h"
52 #include "SQLResultSet.h"
53 #include <wtf/MainThread.h>
54 #endif
55 
56 #if USE(JSC)
57 #include "JSDOMWindow.h"
58 #include <runtime/InitializeThreading.h>
59 #elif USE(V8)
60 #include "InitializeThreading.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 
databaseInfoTableName()68 const String& Database::databaseInfoTableName()
69 {
70     DEFINE_STATIC_LOCAL(String, name, ("__WebKitDatabaseInfoTable__"));
71     return name;
72 }
73 
74 #if ENABLE(DATABASE)
75 
guidMutex()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;
guidToVersionMap()86 static GuidVersionMap& guidToVersionMap()
87 {
88     DEFINE_STATIC_LOCAL(GuidVersionMap, map, ());
89     return map;
90 }
91 
92 typedef HashMap<int, HashSet<Database*>*> GuidDatabaseMap;
guidToDatabaseMap()93 static GuidDatabaseMap& guidToDatabaseMap()
94 {
95     DEFINE_STATIC_LOCAL(GuidDatabaseMap, map, ());
96     return map;
97 }
98 
databaseVersionKey()99 static const String& databaseVersionKey()
100 {
101     DEFINE_STATIC_LOCAL(String, key, ("WebKitDatabaseVersionKey"));
102     return key;
103 }
104 
105 static int guidForOriginAndName(const String& origin, const String& name);
106 
openDatabase(Document * document,const String & name,const String & expectedVersion,const String & displayName,unsigned long estimatedSize,ExceptionCode & e)107 PassRefPtr<Database> Database::openDatabase(Document* document, const String& name, const String& expectedVersion, const String& displayName, unsigned long estimatedSize, ExceptionCode& e)
108 {
109     if (!DatabaseTracker::tracker().canEstablishDatabase(document, name, displayName, estimatedSize)) {
110         // FIXME: There should be an exception raised here in addition to returning a null Database object.  The question has been raised with the WHATWG.
111         LOG(StorageAPI, "Database %s for origin %s not allowed to be established", name.ascii().data(), document->securityOrigin()->toString().ascii().data());
112         return 0;
113     }
114 
115     RefPtr<Database> database = adoptRef(new Database(document, name, expectedVersion));
116 
117     if (!database->openAndVerifyVersion(e)) {
118        LOG(StorageAPI, "Failed to open and verify version (expected %s) of database %s", expectedVersion.ascii().data(), database->databaseDebugName().ascii().data());
119        return 0;
120     }
121 
122     DatabaseTracker::tracker().setDatabaseDetails(document->securityOrigin(), name, displayName, estimatedSize);
123 
124     document->setHasOpenDatabases();
125 
126     if (Page* page = document->frame()->page())
127         page->inspectorController()->didOpenDatabase(database.get(), document->securityOrigin()->host(), name, expectedVersion);
128 
129     return database;
130 }
131 
Database(Document * document,const String & name,const String & expectedVersion)132 Database::Database(Document* document, const String& name, const String& expectedVersion)
133     : m_transactionInProgress(false)
134     , m_document(document)
135     , m_name(name.copy())
136     , m_guid(0)
137     , m_expectedVersion(expectedVersion)
138     , m_deleted(false)
139     , m_stopped(false)
140     , m_opened(false)
141 {
142     ASSERT(document);
143     m_securityOrigin = document->securityOrigin();
144 
145     if (m_name.isNull())
146         m_name = "";
147 
148 #if USE(JSC)
149     JSC::initializeThreading();
150     // Database code violates the normal JSCore contract by calling jsUnprotect from a secondary thread, and thus needs additional locking.
151     JSDOMWindow::commonJSGlobalData()->heap.setGCProtectNeedsLocking();
152 #elif USE(V8)
153     // TODO(benm): do we need the extra locking in V8 too? (See JSC comment above)
154     V8::initializeThreading();
155 #endif
156 
157     m_guid = guidForOriginAndName(m_securityOrigin->toString(), name);
158 
159     {
160         MutexLocker locker(guidMutex());
161 
162         HashSet<Database*>* hashSet = guidToDatabaseMap().get(m_guid);
163         if (!hashSet) {
164             hashSet = new HashSet<Database*>;
165             guidToDatabaseMap().set(m_guid, hashSet);
166         }
167 
168         hashSet->add(this);
169     }
170 
171     ASSERT(m_document->databaseThread());
172 
173     m_filename = DatabaseTracker::tracker().fullPathForDatabase(m_securityOrigin.get(), m_name);
174 
175     DatabaseTracker::tracker().addOpenDatabase(this);
176     m_document->addOpenDatabase(this);
177 }
178 
~Database()179 Database::~Database()
180 {
181     {
182         MutexLocker locker(guidMutex());
183 
184         HashSet<Database*>* hashSet = guidToDatabaseMap().get(m_guid);
185         ASSERT(hashSet);
186         ASSERT(hashSet->contains(this));
187         hashSet->remove(this);
188         if (hashSet->isEmpty()) {
189             guidToDatabaseMap().remove(m_guid);
190             delete hashSet;
191             guidToVersionMap().remove(m_guid);
192         }
193     }
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 
openAndVerifyVersion(ExceptionCode & e)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 
retrieveTextResultFromDatabase(SQLiteDatabase & db,const String & query,String & resultString)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 
getVersionFromDatabase(String & version)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.copy(), 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 
setTextValueInDatabase(SQLiteDatabase & db,const String & query,const String & value)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 
setVersionInDatabase(const String & version)279 bool Database::setVersionInDatabase(const String& version)
280 {
281     DEFINE_STATIC_LOCAL(String, setVersionQuery, ("INSERT INTO " + databaseInfoTableName() + " (key, value) VALUES ('" + databaseVersionKey() + "', ?);"));
282 
283     m_databaseAuthorizer->disable();
284 
285     bool result = setTextValueInDatabase(m_sqliteDatabase, setVersionQuery.copy(), version);
286     if (!result)
287         LOG_ERROR("Failed to set version %s in database (%s)", version.ascii().data(), setVersionQuery.ascii().data());
288 
289     m_databaseAuthorizer->enable();
290 
291     return result;
292 }
293 
versionMatchesExpected() const294 bool Database::versionMatchesExpected() const
295 {
296     if (!m_expectedVersion.isEmpty()) {
297         MutexLocker locker(guidMutex());
298         return m_expectedVersion == guidToVersionMap().get(m_guid);
299     }
300 
301     return true;
302 }
303 
markAsDeletedAndClose()304 void Database::markAsDeletedAndClose()
305 {
306     if (m_deleted || !m_document->databaseThread())
307         return;
308 
309     LOG(StorageAPI, "Marking %s (%p) as deleted", stringIdentifier().ascii().data(), this);
310     m_deleted = true;
311 
312     if (m_document->databaseThread()->terminationRequested()) {
313         LOG(StorageAPI, "Database handle %p is on a terminated DatabaseThread, cannot be marked for normal closure\n", this);
314         return;
315     }
316 
317     m_document->databaseThread()->unscheduleDatabaseTasks(this);
318 
319     RefPtr<DatabaseCloseTask> task = DatabaseCloseTask::create(this);
320 
321     task->lockForSynchronousScheduling();
322     m_document->databaseThread()->scheduleImmediateTask(task);
323     task->waitForSynchronousCompletion();
324 }
325 
close()326 void Database::close()
327 {
328     if (m_opened) {
329         ASSERT(m_document->databaseThread());
330         ASSERT(currentThread() == document()->databaseThread()->getThreadID());
331         m_sqliteDatabase.close();
332         m_document->databaseThread()->recordDatabaseClosed(this);
333         m_opened = false;
334     }
335 }
336 
stop()337 void Database::stop()
338 {
339     // FIXME: The net effect of the following code is to remove all pending transactions and statements, but allow the current statement
340     // to run to completion.  In the future we can use the sqlite3_progress_handler or sqlite3_interrupt interfaces to cancel the current
341     // statement in response to close(), as well.
342 
343     // 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
344     // a page load or closing the page
345     m_stopped = true;
346 
347     {
348         MutexLocker locker(m_transactionInProgressMutex);
349         m_transactionQueue.kill();
350         m_transactionInProgress = false;
351     }
352 }
353 
databaseSize() const354 unsigned long long Database::databaseSize() const
355 {
356     return SQLiteFileSystem::getDatabaseFileSize(m_filename);
357 }
358 
maximumSize() const359 unsigned long long Database::maximumSize() const
360 {
361     // The maximum size for this database is the full quota for this origin, minus the current usage within this origin,
362     // except for the current usage of this database
363 
364     OriginQuotaManager& manager(DatabaseTracker::tracker().originQuotaManager());
365     Locker<OriginQuotaManager> locker(manager);
366 
367     return DatabaseTracker::tracker().quotaForOrigin(m_securityOrigin.get()) - manager.diskUsage(m_securityOrigin.get()) + databaseSize();
368 }
369 
disableAuthorizer()370 void Database::disableAuthorizer()
371 {
372     ASSERT(m_databaseAuthorizer);
373     m_databaseAuthorizer->disable();
374 }
375 
enableAuthorizer()376 void Database::enableAuthorizer()
377 {
378     ASSERT(m_databaseAuthorizer);
379     m_databaseAuthorizer->enable();
380 }
381 
setAuthorizerReadOnly()382 void Database::setAuthorizerReadOnly()
383 {
384     ASSERT(m_databaseAuthorizer);
385     m_databaseAuthorizer->setReadOnly();
386 }
387 
guidForOriginAndName(const String & origin,const String & name)388 static int guidForOriginAndName(const String& origin, const String& name)
389 {
390     String stringID;
391     if (origin.endsWith("/"))
392         stringID = origin + name;
393     else
394         stringID = origin + "/" + name;
395 
396     // Note: We don't have to use AtomicallyInitializedStatic here because
397     // this function is called once in the constructor on the main thread
398     // before any other threads that call this function are used.
399     DEFINE_STATIC_LOCAL(Mutex, stringIdentifierMutex, ());
400     MutexLocker locker(stringIdentifierMutex);
401     typedef HashMap<String, int> IDGuidMap;
402     DEFINE_STATIC_LOCAL(IDGuidMap, stringIdentifierToGUIDMap, ());
403     int guid = stringIdentifierToGUIDMap.get(stringID);
404     if (!guid) {
405         static int currentNewGUID = 1;
406         guid = currentNewGUID++;
407         stringIdentifierToGUIDMap.set(stringID, guid);
408     }
409 
410     return guid;
411 }
412 
resetAuthorizer()413 void Database::resetAuthorizer()
414 {
415     if (m_databaseAuthorizer)
416         m_databaseAuthorizer->reset();
417 }
418 
performPolicyChecks()419 void Database::performPolicyChecks()
420 {
421     // FIXME: Code similar to the following will need to be run to enforce the per-origin size limit the spec mandates.
422     // Additionally, we might need a way to pause the database thread while the UA prompts the user for permission to
423     // increase the size limit
424 
425     /*
426     if (m_databaseAuthorizer->lastActionIncreasedSize())
427         DatabaseTracker::scheduleFileSizeCheckOnMainThread(this);
428     */
429 
430     notImplemented();
431 }
432 
performOpenAndVerify(ExceptionCode & e)433 bool Database::performOpenAndVerify(ExceptionCode& e)
434 {
435     if (!m_sqliteDatabase.open(m_filename)) {
436         LOG_ERROR("Unable to open database at path %s", m_filename.ascii().data());
437         e = INVALID_STATE_ERR;
438         return false;
439     }
440 
441     m_opened = true;
442     if (m_document->databaseThread())
443         m_document->databaseThread()->recordDatabaseOpen(this);
444 
445     ASSERT(m_databaseAuthorizer);
446     m_sqliteDatabase.setAuthorizer(m_databaseAuthorizer);
447     m_sqliteDatabase.setBusyTimeout(maxSqliteBusyWaitTime);
448 
449     if (!m_sqliteDatabase.tableExists(databaseInfoTableName())) {
450         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);")) {
451             LOG_ERROR("Unable to create table %s in database %s", databaseInfoTableName().ascii().data(), databaseDebugName().ascii().data());
452             e = INVALID_STATE_ERR;
453             return false;
454         }
455     }
456 
457     String currentVersion;
458     {
459         MutexLocker locker(guidMutex());
460 
461         // Note: It is not safe to put an empty string into the guidToVersionMap() map.
462         // That's because the map is cross-thread, but empty strings are per-thread.
463         // The copy() function makes a version of the string you can use on the current
464         // thread, but we need a string we can keep in a cross-thread data structure.
465         // FIXME: This is a quite-awkward restriction to have to program with.
466 
467         GuidVersionMap::iterator entry = guidToVersionMap().find(m_guid);
468         if (entry != guidToVersionMap().end()) {
469             // Map null string to empty string (see comment above).
470             currentVersion = entry->second.isNull() ? String("") : entry->second;
471             LOG(StorageAPI, "Current cached version for guid %i is %s", m_guid, currentVersion.ascii().data());
472         } else {
473             LOG(StorageAPI, "No cached version for guid %i", m_guid);
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             // Map empty string to null string (see comment above).
492             guidToVersionMap().set(m_guid, currentVersion.isEmpty() ? String() : currentVersion.copy());
493         }
494     }
495 
496     if (currentVersion.isNull()) {
497         LOG(StorageAPI, "Database %s does not have its version set", databaseDebugName().ascii().data());
498         currentVersion = "";
499     }
500 
501     // 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
502     // changed, and here's where we would change it
503     if (m_expectedVersion.length()) {
504         if (currentVersion.length() && m_expectedVersion != currentVersion) {
505             LOG(StorageAPI, "page expects version %s from database %s, which actually has version name %s - openDatabase() call will fail", m_expectedVersion.ascii().data(),
506                 databaseDebugName().ascii().data(), currentVersion.ascii().data());
507             e = INVALID_STATE_ERR;
508             return false;
509         }
510     }
511 
512     return true;
513 }
514 
changeVersion(const String & oldVersion,const String & newVersion,PassRefPtr<SQLTransactionCallback> callback,PassRefPtr<SQLTransactionErrorCallback> errorCallback,PassRefPtr<VoidCallback> successCallback)515 void Database::changeVersion(const String& oldVersion, const String& newVersion,
516                              PassRefPtr<SQLTransactionCallback> callback, PassRefPtr<SQLTransactionErrorCallback> errorCallback,
517                              PassRefPtr<VoidCallback> successCallback)
518 {
519     m_transactionQueue.append(SQLTransaction::create(this, callback, errorCallback, successCallback, ChangeVersionWrapper::create(oldVersion, newVersion)));
520     MutexLocker locker(m_transactionInProgressMutex);
521     if (!m_transactionInProgress)
522         scheduleTransaction();
523 }
524 
transaction(PassRefPtr<SQLTransactionCallback> callback,PassRefPtr<SQLTransactionErrorCallback> errorCallback,PassRefPtr<VoidCallback> successCallback)525 void Database::transaction(PassRefPtr<SQLTransactionCallback> callback, PassRefPtr<SQLTransactionErrorCallback> errorCallback,
526                            PassRefPtr<VoidCallback> successCallback)
527 {
528     m_transactionQueue.append(SQLTransaction::create(this, callback, errorCallback, successCallback, 0));
529     MutexLocker locker(m_transactionInProgressMutex);
530     if (!m_transactionInProgress)
531         scheduleTransaction();
532 }
533 
scheduleTransaction()534 void Database::scheduleTransaction()
535 {
536     ASSERT(!m_transactionInProgressMutex.tryLock()); // Locked by caller.
537     RefPtr<SQLTransaction> transaction;
538     if (m_transactionQueue.tryGetMessage(transaction) && m_document->databaseThread()) {
539         RefPtr<DatabaseTransactionTask> task = DatabaseTransactionTask::create(transaction);
540         LOG(StorageAPI, "Scheduling DatabaseTransactionTask %p for transaction %p\n", task.get(), task->transaction());
541         m_transactionInProgress = true;
542         m_document->databaseThread()->scheduleTask(task.release());
543     } else
544         m_transactionInProgress = false;
545 }
546 
scheduleTransactionStep(SQLTransaction * transaction)547 void Database::scheduleTransactionStep(SQLTransaction* transaction)
548 {
549     if (m_document->databaseThread()) {
550         RefPtr<DatabaseTransactionTask> task = DatabaseTransactionTask::create(transaction);
551         LOG(StorageAPI, "Scheduling DatabaseTransactionTask %p for the transaction step\n", task.get());
552         m_document->databaseThread()->scheduleTask(task.release());
553     }
554 }
555 
scheduleTransactionCallback(SQLTransaction * transaction)556 void Database::scheduleTransactionCallback(SQLTransaction* transaction)
557 {
558     transaction->ref();
559     callOnMainThread(deliverPendingCallback, transaction);
560 }
561 
performGetTableNames()562 Vector<String> Database::performGetTableNames()
563 {
564     disableAuthorizer();
565 
566     SQLiteStatement statement(m_sqliteDatabase, "SELECT name FROM sqlite_master WHERE type='table';");
567     if (statement.prepare() != SQLResultOk) {
568         LOG_ERROR("Unable to retrieve list of tables for database %s", databaseDebugName().ascii().data());
569         enableAuthorizer();
570         return Vector<String>();
571     }
572 
573     Vector<String> tableNames;
574     int result;
575     while ((result = statement.step()) == SQLResultRow) {
576         String name = statement.getColumnText(0);
577         if (name != databaseInfoTableName())
578             tableNames.append(name);
579     }
580 
581     enableAuthorizer();
582 
583     if (result != SQLResultDone) {
584         LOG_ERROR("Error getting tables for database %s", databaseDebugName().ascii().data());
585         return Vector<String>();
586     }
587 
588     return tableNames;
589 }
590 
version() const591 String Database::version() const
592 {
593     if (m_deleted)
594         return String();
595     MutexLocker locker(guidMutex());
596     return guidToVersionMap().get(m_guid).copy();
597 }
598 
deliverPendingCallback(void * context)599 void Database::deliverPendingCallback(void* context)
600 {
601     SQLTransaction* transaction = static_cast<SQLTransaction*>(context);
602     transaction->performPendingCallback();
603     transaction->deref(); // Was ref'd in scheduleTransactionCallback().
604 }
605 
tableNames()606 Vector<String> Database::tableNames()
607 {
608     if (!m_document->databaseThread())
609         return Vector<String>();
610     RefPtr<DatabaseTableNamesTask> task = DatabaseTableNamesTask::create(this);
611 
612     task->lockForSynchronousScheduling();
613     m_document->databaseThread()->scheduleImmediateTask(task);
614     task->waitForSynchronousCompletion();
615 
616     return task->tableNames();
617 }
618 
setExpectedVersion(const String & version)619 void Database::setExpectedVersion(const String& version)
620 {
621     m_expectedVersion = version.copy();
622 }
623 
securityOriginCopy() const624 PassRefPtr<SecurityOrigin> Database::securityOriginCopy() const
625 {
626     return m_securityOrigin->copy();
627 }
628 
stringIdentifier() const629 String Database::stringIdentifier() const
630 {
631     // Return a deep copy for ref counting thread safety
632     return m_name.copy();
633 }
634 
635 #endif
636 
637 }
638