2 * Copyright (C) 2011 Google Inc. All rights reserved.
3 * Copyright (C) 2013 Apple Inc. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
15 * its contributors may be used to endorse or promote products derived
16 * from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
19 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
22 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #include "DatabaseBackendBase.h"
33 #if ENABLE(SQL_DATABASE)
35 #include "DatabaseAuthorizer.h"
36 #include "DatabaseBackendContext.h"
37 #include "DatabaseBase.h"
38 #include "DatabaseContext.h"
39 #include "DatabaseManager.h"
40 #include "DatabaseTracker.h"
41 #include "ExceptionCode.h"
43 #include "SQLiteStatement.h"
44 #include "SQLiteTransaction.h"
45 #include "SecurityOrigin.h"
46 #include <wtf/HashMap.h>
47 #include <wtf/HashSet.h>
48 #include <wtf/PassRefPtr.h>
49 #include <wtf/RefPtr.h>
50 #include <wtf/StdLibExtras.h>
51 #include <wtf/text/CString.h>
52 #include <wtf/text/StringHash.h>
54 #if PLATFORM(CHROMIUM)
55 #include "DatabaseObserver.h" // For error reporting.
58 // Registering "opened" databases with the DatabaseTracker
59 // =======================================================
60 // The DatabaseTracker maintains a list of databases that have been
61 // "opened" so that the client can call interrupt or delete on every database
62 // associated with a DatabaseBackendContext.
64 // We will only call DatabaseTracker::addOpenDatabase() to add the database
65 // to the tracker as opened when we've succeeded in opening the database,
66 // and will set m_opened to true. Similarly, we only call
67 // DatabaseTracker::removeOpenDatabase() to remove the database from the
68 // tracker when we set m_opened to false in closeDatabase(). This sets up
69 // a simple symmetry between open and close operations, and a direct
70 // correlation to adding and removing databases from the tracker's list,
71 // thus ensuring that we have a correct list for the interrupt and
72 // delete operations to work on.
74 // The only databases instances not tracked by the tracker's open database
75 // list are the ones that have not been added yet, or the ones that we
76 // attempted an open on but failed to. Such instances only exist in the
77 // DatabaseServer's factory methods for creating database backends.
79 // The factory methods will either call openAndVerifyVersion() or
80 // performOpenAndVerify(). These methods will add the newly instantiated
81 // database backend if they succeed in opening the requested database.
82 // In the case of failure to open the database, the factory methods will
83 // simply discard the newly instantiated database backend when they return.
84 // The ref counting mechanims will automatically destruct the un-added
85 // (and un-returned) databases instances.
89 static const char versionKey[] = "WebKitDatabaseVersionKey";
90 static const char infoTableName[] = "__WebKitDatabaseInfoTable__";
92 static String formatErrorMessage(const char* message, int sqliteErrorCode, const char* sqliteErrorMessage)
94 return String::format("%s (%d %s)", message, sqliteErrorCode, sqliteErrorMessage);
97 static bool retrieveTextResultFromDatabase(SQLiteDatabase& db, const String& query, String& resultString)
99 SQLiteStatement statement(db, query);
100 int result = statement.prepare();
102 if (result != SQLResultOk) {
103 LOG_ERROR("Error (%i) preparing statement to read text result from database (%s)", result, query.ascii().data());
107 result = statement.step();
108 if (result == SQLResultRow) {
109 resultString = statement.getColumnText(0);
112 if (result == SQLResultDone) {
113 resultString = String();
117 LOG_ERROR("Error (%i) reading text result from database (%s)", result, query.ascii().data());
121 static bool setTextValueInDatabase(SQLiteDatabase& db, const String& query, const String& value)
123 SQLiteStatement statement(db, query);
124 int result = statement.prepare();
126 if (result != SQLResultOk) {
127 LOG_ERROR("Failed to prepare statement to set value in database (%s)", query.ascii().data());
131 statement.bindText(1, value);
133 result = statement.step();
134 if (result != SQLResultDone) {
135 LOG_ERROR("Failed to step statement to set value in database (%s)", query.ascii().data());
142 // FIXME: move all guid-related functions to a DatabaseVersionTracker class.
143 static Mutex& guidMutex()
145 AtomicallyInitializedStatic(Mutex&, mutex = *new Mutex);
149 typedef HashMap<DatabaseGuid, String> GuidVersionMap;
150 static GuidVersionMap& guidToVersionMap()
152 // Ensure the the mutex is locked.
153 ASSERT(!guidMutex().tryLock());
154 DEFINE_STATIC_LOCAL(GuidVersionMap, map, ());
158 // NOTE: Caller must lock guidMutex().
159 static inline void updateGuidVersionMap(DatabaseGuid guid, String newVersion)
161 // Ensure the the mutex is locked.
162 ASSERT(!guidMutex().tryLock());
164 // Note: It is not safe to put an empty string into the guidToVersionMap() map.
165 // That's because the map is cross-thread, but empty strings are per-thread.
166 // The copy() function makes a version of the string you can use on the current
167 // thread, but we need a string we can keep in a cross-thread data structure.
168 // FIXME: This is a quite-awkward restriction to have to program with.
170 // Map null string to empty string (see comment above).
171 guidToVersionMap().set(guid, newVersion.isEmpty() ? String() : newVersion.isolatedCopy());
174 typedef HashMap<DatabaseGuid, HashSet<DatabaseBackendBase*>*> GuidDatabaseMap;
175 static GuidDatabaseMap& guidToDatabaseMap()
177 // Ensure the the mutex is locked.
178 ASSERT(!guidMutex().tryLock());
179 DEFINE_STATIC_LOCAL(GuidDatabaseMap, map, ());
183 static DatabaseGuid guidForOriginAndName(const String& origin, const String& name)
185 // Ensure the the mutex is locked.
186 ASSERT(!guidMutex().tryLock());
188 String stringID = origin + "/" + name;
190 typedef HashMap<String, int> IDGuidMap;
191 DEFINE_STATIC_LOCAL(IDGuidMap, stringIdentifierToGUIDMap, ());
192 DatabaseGuid guid = stringIdentifierToGUIDMap.get(stringID);
194 static int currentNewGUID = 1;
195 guid = currentNewGUID++;
196 stringIdentifierToGUIDMap.set(stringID, guid);
203 const char* DatabaseBackendBase::databaseInfoTableName()
205 return infoTableName;
208 DatabaseBackendBase::DatabaseBackendBase(PassRefPtr<DatabaseBackendContext> databaseContext, const String& name,
209 const String& expectedVersion, const String& displayName, unsigned long estimatedSize, DatabaseType databaseType)
210 : m_databaseContext(databaseContext)
211 , m_name(name.isolatedCopy())
212 , m_expectedVersion(expectedVersion.isolatedCopy())
213 , m_displayName(displayName.isolatedCopy())
214 , m_estimatedSize(estimatedSize)
218 , m_isSyncDatabase(databaseType == DatabaseType::Sync)
220 m_contextThreadSecurityOrigin = m_databaseContext->securityOrigin()->isolatedCopy();
222 m_databaseAuthorizer = DatabaseAuthorizer::create(infoTableName);
228 MutexLocker locker(guidMutex());
229 m_guid = guidForOriginAndName(securityOrigin()->toString(), name);
230 HashSet<DatabaseBackendBase*>* hashSet = guidToDatabaseMap().get(m_guid);
232 hashSet = new HashSet<DatabaseBackendBase*>;
233 guidToDatabaseMap().set(m_guid, hashSet);
239 m_filename = DatabaseManager::manager().fullPathForDatabase(securityOrigin(), m_name);
242 DatabaseBackendBase::~DatabaseBackendBase()
244 // SQLite is "multi-thread safe", but each database handle can only be used
245 // on a single thread at a time.
247 // For DatabaseBackendAsync, we open the SQLite database on the DatabaseThread,
248 // and hence we should also close it on that same thread. This means that the
249 // SQLite database need to be closed by another mechanism (see
250 // DatabaseContext::stopDatabases()). By the time we get here, the SQLite
251 // database should have already been closed.
256 void DatabaseBackendBase::closeDatabase()
261 m_sqliteDatabase.close();
263 // See comment at the top this file regarding calling removeOpenDatabase().
264 DatabaseTracker::tracker().removeOpenDatabase(this);
266 MutexLocker locker(guidMutex());
268 HashSet<DatabaseBackendBase*>* hashSet = guidToDatabaseMap().get(m_guid);
270 ASSERT(hashSet->contains(this));
271 hashSet->remove(this);
272 if (hashSet->isEmpty()) {
273 guidToDatabaseMap().remove(m_guid);
275 guidToVersionMap().remove(m_guid);
280 String DatabaseBackendBase::version() const
282 // Note: In multi-process browsers the cached value may be accurate, but we cannot read the
283 // actual version from the database without potentially inducing a deadlock.
284 // FIXME: Add an async version getter to the DatabaseAPI.
285 return getCachedVersion();
288 class DoneCreatingDatabaseOnExitCaller {
290 DoneCreatingDatabaseOnExitCaller(DatabaseBackendBase* database)
291 : m_database(database)
292 , m_openSucceeded(false)
295 ~DoneCreatingDatabaseOnExitCaller()
297 #if !PLATFORM(CHROMIUM)
298 DatabaseTracker::tracker().doneCreatingDatabase(m_database);
300 if (!m_openSucceeded)
301 DatabaseTracker::tracker().failedToOpenDatabase(m_database);
305 void setOpenSucceeded() { m_openSucceeded = true; }
308 DatabaseBackendBase* m_database;
309 bool m_openSucceeded;
312 bool DatabaseBackendBase::performOpenAndVerify(bool shouldSetVersionInNewDatabase, DatabaseError& error, String& errorMessage)
314 DoneCreatingDatabaseOnExitCaller onExitCaller(this);
315 ASSERT(errorMessage.isEmpty());
316 ASSERT(error == DatabaseError::None); // Better not have any errors already.
317 error = DatabaseError::InvalidDatabaseState; // Presumed failure. We'll clear it if we succeed below.
319 const int maxSqliteBusyWaitTime = 30000;
321 if (!m_sqliteDatabase.open(m_filename, true)) {
322 reportOpenDatabaseResult(1, INVALID_STATE_ERR, m_sqliteDatabase.lastError());
323 errorMessage = formatErrorMessage("unable to open database", m_sqliteDatabase.lastError(), m_sqliteDatabase.lastErrorMsg());
326 if (!m_sqliteDatabase.turnOnIncrementalAutoVacuum())
327 LOG_ERROR("Unable to turn on incremental auto-vacuum (%d %s)", m_sqliteDatabase.lastError(), m_sqliteDatabase.lastErrorMsg());
329 m_sqliteDatabase.setBusyTimeout(maxSqliteBusyWaitTime);
331 String currentVersion;
333 MutexLocker locker(guidMutex());
335 GuidVersionMap::iterator entry = guidToVersionMap().find(m_guid);
336 if (entry != guidToVersionMap().end()) {
337 // Map null string to empty string (see updateGuidVersionMap()).
338 currentVersion = entry->value.isNull() ? emptyString() : entry->value.isolatedCopy();
339 LOG(StorageAPI, "Current cached version for guid %i is %s", m_guid, currentVersion.ascii().data());
341 #if PLATFORM(CHROMIUM)
342 // Note: In multi-process browsers the cached value may be inaccurate, but
343 // we cannot read the actual version from the database without potentially
344 // inducing a form of deadlock, a busytimeout error when trying to
345 // access the database. So we'll use the cached value if we're unable to read
346 // the value from the database file without waiting.
347 // FIXME: Add an async openDatabase method to the DatabaseAPI.
348 const int noSqliteBusyWaitTime = 0;
349 m_sqliteDatabase.setBusyTimeout(noSqliteBusyWaitTime);
350 String versionFromDatabase;
351 if (getVersionFromDatabase(versionFromDatabase, false)) {
352 currentVersion = versionFromDatabase;
353 updateGuidVersionMap(m_guid, currentVersion);
355 m_sqliteDatabase.setBusyTimeout(maxSqliteBusyWaitTime);
358 LOG(StorageAPI, "No cached version for guid %i", m_guid);
360 SQLiteTransaction transaction(m_sqliteDatabase);
362 if (!transaction.inProgress()) {
363 reportOpenDatabaseResult(2, INVALID_STATE_ERR, m_sqliteDatabase.lastError());
364 errorMessage = formatErrorMessage("unable to open database, failed to start transaction", m_sqliteDatabase.lastError(), m_sqliteDatabase.lastErrorMsg());
365 m_sqliteDatabase.close();
369 String tableName(infoTableName);
370 if (!m_sqliteDatabase.tableExists(tableName)) {
373 if (!m_sqliteDatabase.executeCommand("CREATE TABLE " + tableName + " (key TEXT NOT NULL ON CONFLICT FAIL UNIQUE ON CONFLICT REPLACE,value TEXT NOT NULL ON CONFLICT FAIL);")) {
374 reportOpenDatabaseResult(3, INVALID_STATE_ERR, m_sqliteDatabase.lastError());
375 errorMessage = formatErrorMessage("unable to open database, failed to create 'info' table", m_sqliteDatabase.lastError(), m_sqliteDatabase.lastErrorMsg());
376 transaction.rollback();
377 m_sqliteDatabase.close();
380 } else if (!getVersionFromDatabase(currentVersion, false)) {
381 reportOpenDatabaseResult(4, INVALID_STATE_ERR, m_sqliteDatabase.lastError());
382 errorMessage = formatErrorMessage("unable to open database, failed to read current version", m_sqliteDatabase.lastError(), m_sqliteDatabase.lastErrorMsg());
383 transaction.rollback();
384 m_sqliteDatabase.close();
388 if (currentVersion.length()) {
389 LOG(StorageAPI, "Retrieved current version %s from database %s", currentVersion.ascii().data(), databaseDebugName().ascii().data());
390 } else if (!m_new || shouldSetVersionInNewDatabase) {
391 LOG(StorageAPI, "Setting version %s in database %s that was just created", m_expectedVersion.ascii().data(), databaseDebugName().ascii().data());
392 if (!setVersionInDatabase(m_expectedVersion, false)) {
393 reportOpenDatabaseResult(5, INVALID_STATE_ERR, m_sqliteDatabase.lastError());
394 errorMessage = formatErrorMessage("unable to open database, failed to write current version", m_sqliteDatabase.lastError(), m_sqliteDatabase.lastErrorMsg());
395 transaction.rollback();
396 m_sqliteDatabase.close();
399 currentVersion = m_expectedVersion;
401 updateGuidVersionMap(m_guid, currentVersion);
402 transaction.commit();
406 if (currentVersion.isNull()) {
407 LOG(StorageAPI, "Database %s does not have its version set", databaseDebugName().ascii().data());
411 // If the expected version isn't the empty string, ensure that the current database version we have matches that version. Otherwise, set an exception.
412 // If the expected version is the empty string, then we always return with whatever version of the database we have.
413 if ((!m_new || shouldSetVersionInNewDatabase) && m_expectedVersion.length() && m_expectedVersion != currentVersion) {
414 reportOpenDatabaseResult(6, INVALID_STATE_ERR, 0);
415 errorMessage = "unable to open database, version mismatch, '" + m_expectedVersion + "' does not match the currentVersion of '" + currentVersion + "'";
416 m_sqliteDatabase.close();
420 ASSERT(m_databaseAuthorizer);
421 m_sqliteDatabase.setAuthorizer(m_databaseAuthorizer);
423 // See comment at the top this file regarding calling addOpenDatabase().
424 DatabaseTracker::tracker().addOpenDatabase(this);
428 error = DatabaseError::None; // Clear the presumed error from above.
429 onExitCaller.setOpenSucceeded();
431 if (m_new && !shouldSetVersionInNewDatabase)
432 m_expectedVersion = ""; // The caller provided a creationCallback which will set the expected version.
434 reportOpenDatabaseResult(0, -1, 0); // OK
438 SecurityOrigin* DatabaseBackendBase::securityOrigin() const
440 return m_contextThreadSecurityOrigin.get();
443 String DatabaseBackendBase::stringIdentifier() const
445 // Return a deep copy for ref counting thread safety
446 return m_name.isolatedCopy();
449 String DatabaseBackendBase::displayName() const
451 // Return a deep copy for ref counting thread safety
452 return m_displayName.isolatedCopy();
455 unsigned long DatabaseBackendBase::estimatedSize() const
457 return m_estimatedSize;
460 String DatabaseBackendBase::fileName() const
462 // Return a deep copy for ref counting thread safety
463 return m_filename.isolatedCopy();
466 DatabaseDetails DatabaseBackendBase::details() const
468 return DatabaseDetails(stringIdentifier(), displayName(), estimatedSize(), 0);
471 bool DatabaseBackendBase::getVersionFromDatabase(String& version, bool shouldCacheVersion)
473 String query(String("SELECT value FROM ") + infoTableName + " WHERE key = '" + versionKey + "';");
475 m_databaseAuthorizer->disable();
477 bool result = retrieveTextResultFromDatabase(m_sqliteDatabase, query, version);
479 if (shouldCacheVersion)
480 setCachedVersion(version);
482 LOG_ERROR("Failed to retrieve version from database %s", databaseDebugName().ascii().data());
484 m_databaseAuthorizer->enable();
489 bool DatabaseBackendBase::setVersionInDatabase(const String& version, bool shouldCacheVersion)
491 // The INSERT will replace an existing entry for the database with the new version number, due to the UNIQUE ON CONFLICT REPLACE
492 // clause in the CREATE statement (see Database::performOpenAndVerify()).
493 String query(String("INSERT INTO ") + infoTableName + " (key, value) VALUES ('" + versionKey + "', ?);");
495 m_databaseAuthorizer->disable();
497 bool result = setTextValueInDatabase(m_sqliteDatabase, query, version);
499 if (shouldCacheVersion)
500 setCachedVersion(version);
502 LOG_ERROR("Failed to set version %s in database (%s)", version.ascii().data(), query.ascii().data());
504 m_databaseAuthorizer->enable();
509 void DatabaseBackendBase::setExpectedVersion(const String& version)
511 m_expectedVersion = version.isolatedCopy();
514 String DatabaseBackendBase::getCachedVersion() const
516 MutexLocker locker(guidMutex());
517 return guidToVersionMap().get(m_guid).isolatedCopy();
520 void DatabaseBackendBase::setCachedVersion(const String& actualVersion)
522 // Update the in memory database version map.
523 MutexLocker locker(guidMutex());
524 updateGuidVersionMap(m_guid, actualVersion);
527 bool DatabaseBackendBase::getActualVersionForTransaction(String &actualVersion)
529 ASSERT(m_sqliteDatabase.transactionInProgress());
530 #if PLATFORM(CHROMIUM)
531 // Note: In multi-process browsers the cached value may be inaccurate.
532 // So we retrieve the value from the database and update the cached value here.
533 return getVersionFromDatabase(actualVersion, true);
535 actualVersion = getCachedVersion();
540 void DatabaseBackendBase::disableAuthorizer()
542 ASSERT(m_databaseAuthorizer);
543 m_databaseAuthorizer->disable();
546 void DatabaseBackendBase::enableAuthorizer()
548 ASSERT(m_databaseAuthorizer);
549 m_databaseAuthorizer->enable();
552 void DatabaseBackendBase::setAuthorizerReadOnly()
554 ASSERT(m_databaseAuthorizer);
555 m_databaseAuthorizer->setReadOnly();
558 void DatabaseBackendBase::setAuthorizerPermissions(int permissions)
560 ASSERT(m_databaseAuthorizer);
561 m_databaseAuthorizer->setPermissions(permissions);
564 bool DatabaseBackendBase::lastActionChangedDatabase()
566 ASSERT(m_databaseAuthorizer);
567 return m_databaseAuthorizer->lastActionChangedDatabase();
570 bool DatabaseBackendBase::lastActionWasInsert()
572 ASSERT(m_databaseAuthorizer);
573 return m_databaseAuthorizer->lastActionWasInsert();
576 void DatabaseBackendBase::resetDeletes()
578 ASSERT(m_databaseAuthorizer);
579 m_databaseAuthorizer->resetDeletes();
582 bool DatabaseBackendBase::hadDeletes()
584 ASSERT(m_databaseAuthorizer);
585 return m_databaseAuthorizer->hadDeletes();
588 void DatabaseBackendBase::resetAuthorizer()
590 if (m_databaseAuthorizer)
591 m_databaseAuthorizer->reset();
594 unsigned long long DatabaseBackendBase::maximumSize() const
596 return DatabaseManager::manager().getMaxSizeForDatabase(this);
599 void DatabaseBackendBase::incrementalVacuumIfNeeded()
601 int64_t freeSpaceSize = m_sqliteDatabase.freeSpaceSize();
602 int64_t totalSize = m_sqliteDatabase.totalSize();
603 if (totalSize <= 10 * freeSpaceSize) {
604 int result = m_sqliteDatabase.runIncrementalVacuumCommand();
605 reportVacuumDatabaseResult(result);
606 if (result != SQLResultOk)
607 m_frontend->logErrorMessage(formatErrorMessage("error vacuuming database", result, m_sqliteDatabase.lastErrorMsg()));
611 void DatabaseBackendBase::interrupt()
613 m_sqliteDatabase.interrupt();
616 bool DatabaseBackendBase::isInterrupted()
618 MutexLocker locker(m_sqliteDatabase.databaseMutex());
619 return m_sqliteDatabase.isInterrupted();
622 #if PLATFORM(CHROMIUM)
623 // These are used to generate histograms of errors seen with websql.
624 // See about:histograms in chromium.
625 void DatabaseBackendBase::reportOpenDatabaseResult(int errorSite, int webSqlErrorCode, int sqliteErrorCode)
627 DatabaseObserver::reportOpenDatabaseResult(this, errorSite, webSqlErrorCode, sqliteErrorCode);
630 void DatabaseBackendBase::reportChangeVersionResult(int errorSite, int webSqlErrorCode, int sqliteErrorCode)
632 DatabaseObserver::reportChangeVersionResult(this, errorSite, webSqlErrorCode, sqliteErrorCode);
635 void DatabaseBackendBase::reportStartTransactionResult(int errorSite, int webSqlErrorCode, int sqliteErrorCode)
637 DatabaseObserver::reportStartTransactionResult(this, errorSite, webSqlErrorCode, sqliteErrorCode);
640 void DatabaseBackendBase::reportCommitTransactionResult(int errorSite, int webSqlErrorCode, int sqliteErrorCode)
642 DatabaseObserver::reportCommitTransactionResult(this, errorSite, webSqlErrorCode, sqliteErrorCode);
645 void DatabaseBackendBase::reportExecuteStatementResult(int errorSite, int webSqlErrorCode, int sqliteErrorCode)
647 DatabaseObserver::reportExecuteStatementResult(this, errorSite, webSqlErrorCode, sqliteErrorCode);
650 void DatabaseBackendBase::reportVacuumDatabaseResult(int sqliteErrorCode)
652 DatabaseObserver::reportVacuumDatabaseResult(this, sqliteErrorCode);
655 #endif // PLATFORM(CHROMIUM)
657 } // namespace WebCore
659 #endif // ENABLE(SQL_DATABASE)