1 /* 2 * Copyright (C) 2011 Google Inc. All rights reserved. 3 * Copyright (C) 2013 Apple Inc. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 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. 17 * 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. 28 */ 29 30 #include "config.h" 31 #include "modules/webdatabase/DatabaseBackendBase.h" 32 33 #include "core/dom/ExceptionCode.h" 34 #include "core/platform/Logging.h" 35 #include "core/platform/sql/SQLiteStatement.h" 36 #include "core/platform/sql/SQLiteTransaction.h" 37 #include "modules/webdatabase/DatabaseAuthorizer.h" 38 #include "modules/webdatabase/DatabaseBackendContext.h" 39 #include "modules/webdatabase/DatabaseBase.h" 40 #include "modules/webdatabase/DatabaseContext.h" 41 #include "modules/webdatabase/DatabaseManager.h" 42 #include "modules/webdatabase/DatabaseObserver.h" 43 #include "modules/webdatabase/DatabaseTracker.h" 44 #include "weborigin/SecurityOrigin.h" 45 #include "wtf/HashMap.h" 46 #include "wtf/HashSet.h" 47 #include "wtf/PassRefPtr.h" 48 #include "wtf/RefPtr.h" 49 #include "wtf/StdLibExtras.h" 50 #include "wtf/text/CString.h" 51 #include "wtf/text/StringHash.h" 52 53 // Registering "opened" databases with the DatabaseTracker 54 // ======================================================= 55 // The DatabaseTracker maintains a list of databases that have been 56 // "opened" so that the client can call interrupt or delete on every database 57 // associated with a DatabaseBackendContext. 58 // 59 // We will only call DatabaseTracker::addOpenDatabase() to add the database 60 // to the tracker as opened when we've succeeded in opening the database, 61 // and will set m_opened to true. Similarly, we only call 62 // DatabaseTracker::removeOpenDatabase() to remove the database from the 63 // tracker when we set m_opened to false in closeDatabase(). This sets up 64 // a simple symmetry between open and close operations, and a direct 65 // correlation to adding and removing databases from the tracker's list, 66 // thus ensuring that we have a correct list for the interrupt and 67 // delete operations to work on. 68 // 69 // The only databases instances not tracked by the tracker's open database 70 // list are the ones that have not been added yet, or the ones that we 71 // attempted an open on but failed to. Such instances only exist in the 72 // DatabaseServer's factory methods for creating database backends. 73 // 74 // The factory methods will either call openAndVerifyVersion() or 75 // performOpenAndVerify(). These methods will add the newly instantiated 76 // database backend if they succeed in opening the requested database. 77 // In the case of failure to open the database, the factory methods will 78 // simply discard the newly instantiated database backend when they return. 79 // The ref counting mechanims will automatically destruct the un-added 80 // (and un-returned) databases instances. 81 82 namespace WebCore { 83 84 static const char versionKey[] = "WebKitDatabaseVersionKey"; 85 static const char infoTableName[] = "__WebKitDatabaseInfoTable__"; 86 87 static String formatErrorMessage(const char* message, int sqliteErrorCode, const char* sqliteErrorMessage) 88 { 89 return String::format("%s (%d %s)", message, sqliteErrorCode, sqliteErrorMessage); 90 } 91 92 static bool retrieveTextResultFromDatabase(SQLiteDatabase& db, const String& query, String& resultString) 93 { 94 SQLiteStatement statement(db, query); 95 int result = statement.prepare(); 96 97 if (result != SQLResultOk) { 98 LOG_ERROR("Error (%i) preparing statement to read text result from database (%s)", result, query.ascii().data()); 99 return false; 100 } 101 102 result = statement.step(); 103 if (result == SQLResultRow) { 104 resultString = statement.getColumnText(0); 105 return true; 106 } 107 if (result == SQLResultDone) { 108 resultString = String(); 109 return true; 110 } 111 112 LOG_ERROR("Error (%i) reading text result from database (%s)", result, query.ascii().data()); 113 return false; 114 } 115 116 static bool setTextValueInDatabase(SQLiteDatabase& db, const String& query, const String& value) 117 { 118 SQLiteStatement statement(db, query); 119 int result = statement.prepare(); 120 121 if (result != SQLResultOk) { 122 LOG_ERROR("Failed to prepare statement to set value in database (%s)", query.ascii().data()); 123 return false; 124 } 125 126 statement.bindText(1, value); 127 128 result = statement.step(); 129 if (result != SQLResultDone) { 130 LOG_ERROR("Failed to step statement to set value in database (%s)", query.ascii().data()); 131 return false; 132 } 133 134 return true; 135 } 136 137 // FIXME: move all guid-related functions to a DatabaseVersionTracker class. 138 static Mutex& guidMutex() 139 { 140 AtomicallyInitializedStatic(Mutex&, mutex = *new Mutex); 141 return mutex; 142 } 143 144 typedef HashMap<DatabaseGuid, String> GuidVersionMap; 145 static GuidVersionMap& guidToVersionMap() 146 { 147 // Ensure the the mutex is locked. 148 ASSERT(!guidMutex().tryLock()); 149 DEFINE_STATIC_LOCAL(GuidVersionMap, map, ()); 150 return map; 151 } 152 153 // NOTE: Caller must lock guidMutex(). 154 static inline void updateGuidVersionMap(DatabaseGuid guid, String newVersion) 155 { 156 // Ensure the the mutex is locked. 157 ASSERT(!guidMutex().tryLock()); 158 159 // Note: It is not safe to put an empty string into the guidToVersionMap() map. 160 // That's because the map is cross-thread, but empty strings are per-thread. 161 // The copy() function makes a version of the string you can use on the current 162 // thread, but we need a string we can keep in a cross-thread data structure. 163 // FIXME: This is a quite-awkward restriction to have to program with. 164 165 // Map null string to empty string (see comment above). 166 guidToVersionMap().set(guid, newVersion.isEmpty() ? String() : newVersion.isolatedCopy()); 167 } 168 169 typedef HashMap<DatabaseGuid, HashSet<DatabaseBackendBase*>*> GuidDatabaseMap; 170 static GuidDatabaseMap& guidToDatabaseMap() 171 { 172 // Ensure the the mutex is locked. 173 ASSERT(!guidMutex().tryLock()); 174 DEFINE_STATIC_LOCAL(GuidDatabaseMap, map, ()); 175 return map; 176 } 177 178 static DatabaseGuid guidForOriginAndName(const String& origin, const String& name) 179 { 180 // Ensure the the mutex is locked. 181 ASSERT(!guidMutex().tryLock()); 182 183 String stringID = origin + "/" + name; 184 185 typedef HashMap<String, int> IDGuidMap; 186 DEFINE_STATIC_LOCAL(IDGuidMap, stringIdentifierToGUIDMap, ()); 187 DatabaseGuid guid = stringIdentifierToGUIDMap.get(stringID); 188 if (!guid) { 189 static int currentNewGUID = 1; 190 guid = currentNewGUID++; 191 stringIdentifierToGUIDMap.set(stringID, guid); 192 } 193 194 return guid; 195 } 196 197 // static 198 const char* DatabaseBackendBase::databaseInfoTableName() 199 { 200 return infoTableName; 201 } 202 203 DatabaseBackendBase::DatabaseBackendBase(PassRefPtr<DatabaseBackendContext> databaseContext, const String& name, 204 const String& expectedVersion, const String& displayName, unsigned long estimatedSize, DatabaseType databaseType) 205 : m_databaseContext(databaseContext) 206 , m_name(name.isolatedCopy()) 207 , m_expectedVersion(expectedVersion.isolatedCopy()) 208 , m_displayName(displayName.isolatedCopy()) 209 , m_estimatedSize(estimatedSize) 210 , m_guid(0) 211 , m_opened(false) 212 , m_new(false) 213 , m_isSyncDatabase(databaseType == DatabaseType::Sync) 214 { 215 m_contextThreadSecurityOrigin = m_databaseContext->securityOrigin()->isolatedCopy(); 216 217 m_databaseAuthorizer = DatabaseAuthorizer::create(infoTableName); 218 219 if (m_name.isNull()) 220 m_name = ""; 221 222 { 223 MutexLocker locker(guidMutex()); 224 m_guid = guidForOriginAndName(securityOrigin()->toString(), name); 225 HashSet<DatabaseBackendBase*>* hashSet = guidToDatabaseMap().get(m_guid); 226 if (!hashSet) { 227 hashSet = new HashSet<DatabaseBackendBase*>; 228 guidToDatabaseMap().set(m_guid, hashSet); 229 } 230 231 hashSet->add(this); 232 } 233 234 m_filename = DatabaseManager::manager().fullPathForDatabase(securityOrigin(), m_name); 235 } 236 237 DatabaseBackendBase::~DatabaseBackendBase() 238 { 239 // SQLite is "multi-thread safe", but each database handle can only be used 240 // on a single thread at a time. 241 // 242 // For DatabaseBackend, we open the SQLite database on the DatabaseThread, 243 // and hence we should also close it on that same thread. This means that the 244 // SQLite database need to be closed by another mechanism (see 245 // DatabaseContext::stopDatabases()). By the time we get here, the SQLite 246 // database should have already been closed. 247 248 ASSERT(!m_opened); 249 } 250 251 void DatabaseBackendBase::closeDatabase() 252 { 253 if (!m_opened) 254 return; 255 256 m_sqliteDatabase.close(); 257 m_opened = false; 258 // See comment at the top this file regarding calling removeOpenDatabase(). 259 DatabaseTracker::tracker().removeOpenDatabase(this); 260 { 261 MutexLocker locker(guidMutex()); 262 263 HashSet<DatabaseBackendBase*>* hashSet = guidToDatabaseMap().get(m_guid); 264 ASSERT(hashSet); 265 ASSERT(hashSet->contains(this)); 266 hashSet->remove(this); 267 if (hashSet->isEmpty()) { 268 guidToDatabaseMap().remove(m_guid); 269 delete hashSet; 270 guidToVersionMap().remove(m_guid); 271 } 272 } 273 } 274 275 String DatabaseBackendBase::version() const 276 { 277 // Note: In multi-process browsers the cached value may be accurate, but we cannot read the 278 // actual version from the database without potentially inducing a deadlock. 279 // FIXME: Add an async version getter to the DatabaseAPI. 280 return getCachedVersion(); 281 } 282 283 class DoneCreatingDatabaseOnExitCaller { 284 public: 285 DoneCreatingDatabaseOnExitCaller(DatabaseBackendBase* database) 286 : m_database(database) 287 , m_openSucceeded(false) 288 { 289 } 290 ~DoneCreatingDatabaseOnExitCaller() 291 { 292 if (!m_openSucceeded) 293 DatabaseTracker::tracker().failedToOpenDatabase(m_database); 294 } 295 296 void setOpenSucceeded() { m_openSucceeded = true; } 297 298 private: 299 DatabaseBackendBase* m_database; 300 bool m_openSucceeded; 301 }; 302 303 bool DatabaseBackendBase::performOpenAndVerify(bool shouldSetVersionInNewDatabase, DatabaseError& error, String& errorMessage) 304 { 305 DoneCreatingDatabaseOnExitCaller onExitCaller(this); 306 ASSERT(errorMessage.isEmpty()); 307 ASSERT(error == DatabaseError::None); // Better not have any errors already. 308 error = DatabaseError::InvalidDatabaseState; // Presumed failure. We'll clear it if we succeed below. 309 310 const int maxSqliteBusyWaitTime = 30000; 311 312 if (!m_sqliteDatabase.open(m_filename, true)) { 313 reportOpenDatabaseResult(1, InvalidStateError, m_sqliteDatabase.lastError()); 314 errorMessage = formatErrorMessage("unable to open database", m_sqliteDatabase.lastError(), m_sqliteDatabase.lastErrorMsg()); 315 return false; 316 } 317 if (!m_sqliteDatabase.turnOnIncrementalAutoVacuum()) 318 LOG_ERROR("Unable to turn on incremental auto-vacuum (%d %s)", m_sqliteDatabase.lastError(), m_sqliteDatabase.lastErrorMsg()); 319 320 m_sqliteDatabase.setBusyTimeout(maxSqliteBusyWaitTime); 321 322 String currentVersion; 323 { 324 MutexLocker locker(guidMutex()); 325 326 GuidVersionMap::iterator entry = guidToVersionMap().find(m_guid); 327 if (entry != guidToVersionMap().end()) { 328 // Map null string to empty string (see updateGuidVersionMap()). 329 currentVersion = entry->value.isNull() ? emptyString() : entry->value.isolatedCopy(); 330 LOG(StorageAPI, "Current cached version for guid %i is %s", m_guid, currentVersion.ascii().data()); 331 332 // Note: In multi-process browsers the cached value may be inaccurate, but 333 // we cannot read the actual version from the database without potentially 334 // inducing a form of deadlock, a busytimeout error when trying to 335 // access the database. So we'll use the cached value if we're unable to read 336 // the value from the database file without waiting. 337 // FIXME: Add an async openDatabase method to the DatabaseAPI. 338 const int noSqliteBusyWaitTime = 0; 339 m_sqliteDatabase.setBusyTimeout(noSqliteBusyWaitTime); 340 String versionFromDatabase; 341 if (getVersionFromDatabase(versionFromDatabase, false)) { 342 currentVersion = versionFromDatabase; 343 updateGuidVersionMap(m_guid, currentVersion); 344 } 345 m_sqliteDatabase.setBusyTimeout(maxSqliteBusyWaitTime); 346 } else { 347 LOG(StorageAPI, "No cached version for guid %i", m_guid); 348 349 SQLiteTransaction transaction(m_sqliteDatabase); 350 transaction.begin(); 351 if (!transaction.inProgress()) { 352 reportOpenDatabaseResult(2, InvalidStateError, m_sqliteDatabase.lastError()); 353 errorMessage = formatErrorMessage("unable to open database, failed to start transaction", m_sqliteDatabase.lastError(), m_sqliteDatabase.lastErrorMsg()); 354 m_sqliteDatabase.close(); 355 return false; 356 } 357 358 String tableName(infoTableName); 359 if (!m_sqliteDatabase.tableExists(tableName)) { 360 m_new = true; 361 362 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);")) { 363 reportOpenDatabaseResult(3, InvalidStateError, m_sqliteDatabase.lastError()); 364 errorMessage = formatErrorMessage("unable to open database, failed to create 'info' table", m_sqliteDatabase.lastError(), m_sqliteDatabase.lastErrorMsg()); 365 transaction.rollback(); 366 m_sqliteDatabase.close(); 367 return false; 368 } 369 } else if (!getVersionFromDatabase(currentVersion, false)) { 370 reportOpenDatabaseResult(4, InvalidStateError, m_sqliteDatabase.lastError()); 371 errorMessage = formatErrorMessage("unable to open database, failed to read current version", m_sqliteDatabase.lastError(), m_sqliteDatabase.lastErrorMsg()); 372 transaction.rollback(); 373 m_sqliteDatabase.close(); 374 return false; 375 } 376 377 if (currentVersion.length()) { 378 LOG(StorageAPI, "Retrieved current version %s from database %s", currentVersion.ascii().data(), databaseDebugName().ascii().data()); 379 } else if (!m_new || shouldSetVersionInNewDatabase) { 380 LOG(StorageAPI, "Setting version %s in database %s that was just created", m_expectedVersion.ascii().data(), databaseDebugName().ascii().data()); 381 if (!setVersionInDatabase(m_expectedVersion, false)) { 382 reportOpenDatabaseResult(5, InvalidStateError, m_sqliteDatabase.lastError()); 383 errorMessage = formatErrorMessage("unable to open database, failed to write current version", m_sqliteDatabase.lastError(), m_sqliteDatabase.lastErrorMsg()); 384 transaction.rollback(); 385 m_sqliteDatabase.close(); 386 return false; 387 } 388 currentVersion = m_expectedVersion; 389 } 390 updateGuidVersionMap(m_guid, currentVersion); 391 transaction.commit(); 392 } 393 } 394 395 if (currentVersion.isNull()) { 396 LOG(StorageAPI, "Database %s does not have its version set", databaseDebugName().ascii().data()); 397 currentVersion = ""; 398 } 399 400 // If the expected version isn't the empty string, ensure that the current database version we have matches that version. Otherwise, set an exception. 401 // If the expected version is the empty string, then we always return with whatever version of the database we have. 402 if ((!m_new || shouldSetVersionInNewDatabase) && m_expectedVersion.length() && m_expectedVersion != currentVersion) { 403 reportOpenDatabaseResult(6, InvalidStateError, 0); 404 errorMessage = "unable to open database, version mismatch, '" + m_expectedVersion + "' does not match the currentVersion of '" + currentVersion + "'"; 405 m_sqliteDatabase.close(); 406 return false; 407 } 408 409 ASSERT(m_databaseAuthorizer); 410 m_sqliteDatabase.setAuthorizer(m_databaseAuthorizer); 411 412 // See comment at the top this file regarding calling addOpenDatabase(). 413 DatabaseTracker::tracker().addOpenDatabase(this); 414 m_opened = true; 415 416 // Declare success: 417 error = DatabaseError::None; // Clear the presumed error from above. 418 onExitCaller.setOpenSucceeded(); 419 420 if (m_new && !shouldSetVersionInNewDatabase) 421 m_expectedVersion = ""; // The caller provided a creationCallback which will set the expected version. 422 423 reportOpenDatabaseResult(0, -1, 0); // OK 424 return true; 425 } 426 427 SecurityOrigin* DatabaseBackendBase::securityOrigin() const 428 { 429 return m_contextThreadSecurityOrigin.get(); 430 } 431 432 String DatabaseBackendBase::stringIdentifier() const 433 { 434 // Return a deep copy for ref counting thread safety 435 return m_name.isolatedCopy(); 436 } 437 438 String DatabaseBackendBase::displayName() const 439 { 440 // Return a deep copy for ref counting thread safety 441 return m_displayName.isolatedCopy(); 442 } 443 444 unsigned long DatabaseBackendBase::estimatedSize() const 445 { 446 return m_estimatedSize; 447 } 448 449 String DatabaseBackendBase::fileName() const 450 { 451 // Return a deep copy for ref counting thread safety 452 return m_filename.isolatedCopy(); 453 } 454 455 DatabaseDetails DatabaseBackendBase::details() const 456 { 457 return DatabaseDetails(stringIdentifier(), displayName(), estimatedSize(), 0); 458 } 459 460 bool DatabaseBackendBase::getVersionFromDatabase(String& version, bool shouldCacheVersion) 461 { 462 String query(String("SELECT value FROM ") + infoTableName + " WHERE key = '" + versionKey + "';"); 463 464 m_databaseAuthorizer->disable(); 465 466 bool result = retrieveTextResultFromDatabase(m_sqliteDatabase, query, version); 467 if (result) { 468 if (shouldCacheVersion) 469 setCachedVersion(version); 470 } else 471 LOG_ERROR("Failed to retrieve version from database %s", databaseDebugName().ascii().data()); 472 473 m_databaseAuthorizer->enable(); 474 475 return result; 476 } 477 478 bool DatabaseBackendBase::setVersionInDatabase(const String& version, bool shouldCacheVersion) 479 { 480 // The INSERT will replace an existing entry for the database with the new version number, due to the UNIQUE ON CONFLICT REPLACE 481 // clause in the CREATE statement (see Database::performOpenAndVerify()). 482 String query(String("INSERT INTO ") + infoTableName + " (key, value) VALUES ('" + versionKey + "', ?);"); 483 484 m_databaseAuthorizer->disable(); 485 486 bool result = setTextValueInDatabase(m_sqliteDatabase, query, version); 487 if (result) { 488 if (shouldCacheVersion) 489 setCachedVersion(version); 490 } else 491 LOG_ERROR("Failed to set version %s in database (%s)", version.ascii().data(), query.ascii().data()); 492 493 m_databaseAuthorizer->enable(); 494 495 return result; 496 } 497 498 void DatabaseBackendBase::setExpectedVersion(const String& version) 499 { 500 m_expectedVersion = version.isolatedCopy(); 501 } 502 503 String DatabaseBackendBase::getCachedVersion() const 504 { 505 MutexLocker locker(guidMutex()); 506 return guidToVersionMap().get(m_guid).isolatedCopy(); 507 } 508 509 void DatabaseBackendBase::setCachedVersion(const String& actualVersion) 510 { 511 // Update the in memory database version map. 512 MutexLocker locker(guidMutex()); 513 updateGuidVersionMap(m_guid, actualVersion); 514 } 515 516 bool DatabaseBackendBase::getActualVersionForTransaction(String &actualVersion) 517 { 518 ASSERT(m_sqliteDatabase.transactionInProgress()); 519 // Note: In multi-process browsers the cached value may be inaccurate. 520 // So we retrieve the value from the database and update the cached value here. 521 return getVersionFromDatabase(actualVersion, true); 522 } 523 524 void DatabaseBackendBase::disableAuthorizer() 525 { 526 ASSERT(m_databaseAuthorizer); 527 m_databaseAuthorizer->disable(); 528 } 529 530 void DatabaseBackendBase::enableAuthorizer() 531 { 532 ASSERT(m_databaseAuthorizer); 533 m_databaseAuthorizer->enable(); 534 } 535 536 void DatabaseBackendBase::setAuthorizerReadOnly() 537 { 538 ASSERT(m_databaseAuthorizer); 539 m_databaseAuthorizer->setReadOnly(); 540 } 541 542 void DatabaseBackendBase::setAuthorizerPermissions(int permissions) 543 { 544 ASSERT(m_databaseAuthorizer); 545 m_databaseAuthorizer->setPermissions(permissions); 546 } 547 548 bool DatabaseBackendBase::lastActionChangedDatabase() 549 { 550 ASSERT(m_databaseAuthorizer); 551 return m_databaseAuthorizer->lastActionChangedDatabase(); 552 } 553 554 bool DatabaseBackendBase::lastActionWasInsert() 555 { 556 ASSERT(m_databaseAuthorizer); 557 return m_databaseAuthorizer->lastActionWasInsert(); 558 } 559 560 void DatabaseBackendBase::resetDeletes() 561 { 562 ASSERT(m_databaseAuthorizer); 563 m_databaseAuthorizer->resetDeletes(); 564 } 565 566 bool DatabaseBackendBase::hadDeletes() 567 { 568 ASSERT(m_databaseAuthorizer); 569 return m_databaseAuthorizer->hadDeletes(); 570 } 571 572 void DatabaseBackendBase::resetAuthorizer() 573 { 574 if (m_databaseAuthorizer) 575 m_databaseAuthorizer->reset(); 576 } 577 578 unsigned long long DatabaseBackendBase::maximumSize() const 579 { 580 return DatabaseTracker::tracker().getMaxSizeForDatabase(this); 581 } 582 583 void DatabaseBackendBase::incrementalVacuumIfNeeded() 584 { 585 int64_t freeSpaceSize = m_sqliteDatabase.freeSpaceSize(); 586 int64_t totalSize = m_sqliteDatabase.totalSize(); 587 if (totalSize <= 10 * freeSpaceSize) { 588 int result = m_sqliteDatabase.runIncrementalVacuumCommand(); 589 reportVacuumDatabaseResult(result); 590 if (result != SQLResultOk) 591 m_frontend->logErrorMessage(formatErrorMessage("error vacuuming database", result, m_sqliteDatabase.lastErrorMsg())); 592 } 593 } 594 595 void DatabaseBackendBase::interrupt() 596 { 597 m_sqliteDatabase.interrupt(); 598 } 599 600 bool DatabaseBackendBase::isInterrupted() 601 { 602 MutexLocker locker(m_sqliteDatabase.databaseMutex()); 603 return m_sqliteDatabase.isInterrupted(); 604 } 605 606 // These are used to generate histograms of errors seen with websql. 607 // See about:histograms in chromium. 608 void DatabaseBackendBase::reportOpenDatabaseResult(int errorSite, int webSqlErrorCode, int sqliteErrorCode) 609 { 610 DatabaseObserver::reportOpenDatabaseResult(this, errorSite, webSqlErrorCode, sqliteErrorCode); 611 } 612 613 void DatabaseBackendBase::reportChangeVersionResult(int errorSite, int webSqlErrorCode, int sqliteErrorCode) 614 { 615 DatabaseObserver::reportChangeVersionResult(this, errorSite, webSqlErrorCode, sqliteErrorCode); 616 } 617 618 void DatabaseBackendBase::reportStartTransactionResult(int errorSite, int webSqlErrorCode, int sqliteErrorCode) 619 { 620 DatabaseObserver::reportStartTransactionResult(this, errorSite, webSqlErrorCode, sqliteErrorCode); 621 } 622 623 void DatabaseBackendBase::reportCommitTransactionResult(int errorSite, int webSqlErrorCode, int sqliteErrorCode) 624 { 625 DatabaseObserver::reportCommitTransactionResult(this, errorSite, webSqlErrorCode, sqliteErrorCode); 626 } 627 628 void DatabaseBackendBase::reportExecuteStatementResult(int errorSite, int webSqlErrorCode, int sqliteErrorCode) 629 { 630 DatabaseObserver::reportExecuteStatementResult(this, errorSite, webSqlErrorCode, sqliteErrorCode); 631 } 632 633 void DatabaseBackendBase::reportVacuumDatabaseResult(int sqliteErrorCode) 634 { 635 DatabaseObserver::reportVacuumDatabaseResult(this, sqliteErrorCode); 636 } 637 638 639 } // namespace WebCore 640