<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[187999] trunk/Source/WebCore</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://trac.webkit.org/projects/webkit/changeset/187999">187999</a></dd>
<dt>Author</dt> <dd>andersca@apple.com</dd>
<dt>Date</dt> <dd>2015-08-05 16:23:30 -0700 (Wed, 05 Aug 2015)</dd>
</dl>

<h3>Log Message</h3>
<pre>Move more functions from DatabaseBackendBase to Database
https://bugs.webkit.org/show_bug.cgi?id=147705

Reviewed by Tim Horton.

* Modules/webdatabase/Database.cpp:
(WebCore::guidMutex):
(WebCore::guidToVersionMap):
(WebCore::updateGuidVersionMap):
(WebCore::guidToDatabaseMap):
(WebCore::guidForOriginAndName):
(WebCore::Database::Database):
(WebCore::DoneCreatingDatabaseOnExitCaller::DoneCreatingDatabaseOnExitCaller):
(WebCore::DoneCreatingDatabaseOnExitCaller::~DoneCreatingDatabaseOnExitCaller):
(WebCore::DoneCreatingDatabaseOnExitCaller::setOpenSucceeded):
(WebCore::Database::performOpenAndVerify):
(WebCore::Database::closeDatabase):
(WebCore::Database::getCachedVersion):
(WebCore::Database::setCachedVersion):
* Modules/webdatabase/Database.h:
* Modules/webdatabase/DatabaseBackendBase.cpp:
(WebCore::DatabaseBackendBase::version):
(WebCore::formatErrorMessage): Deleted.
(WebCore::guidMutex): Deleted.
(WebCore::guidToVersionMap): Deleted.
(WebCore::updateGuidVersionMap): Deleted.
(WebCore::guidToDatabaseMap): Deleted.
(WebCore::guidForOriginAndName): Deleted.
(WebCore::DatabaseBackendBase::DatabaseBackendBase): Deleted.
(WebCore::DatabaseBackendBase::closeDatabase): Deleted.
(WebCore::DoneCreatingDatabaseOnExitCaller::DoneCreatingDatabaseOnExitCaller): Deleted.
(WebCore::DoneCreatingDatabaseOnExitCaller::~DoneCreatingDatabaseOnExitCaller): Deleted.
(WebCore::DoneCreatingDatabaseOnExitCaller::setOpenSucceeded): Deleted.
(WebCore::DatabaseBackendBase::performOpenAndVerify): Deleted.
(WebCore::DatabaseBackendBase::getCachedVersion): Deleted.
(WebCore::DatabaseBackendBase::setCachedVersion): Deleted.
* Modules/webdatabase/DatabaseBackendBase.h:</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkSourceWebCoreChangeLog">trunk/Source/WebCore/ChangeLog</a></li>
<li><a href="#trunkSourceWebCoreModuleswebdatabaseDatabasecpp">trunk/Source/WebCore/Modules/webdatabase/Database.cpp</a></li>
<li><a href="#trunkSourceWebCoreModuleswebdatabaseDatabaseh">trunk/Source/WebCore/Modules/webdatabase/Database.h</a></li>
<li><a href="#trunkSourceWebCoreModuleswebdatabaseDatabaseBackendBasecpp">trunk/Source/WebCore/Modules/webdatabase/DatabaseBackendBase.cpp</a></li>
<li><a href="#trunkSourceWebCoreModuleswebdatabaseDatabaseBackendBaseh">trunk/Source/WebCore/Modules/webdatabase/DatabaseBackendBase.h</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkSourceWebCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/ChangeLog (187998 => 187999)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/ChangeLog        2015-08-05 23:17:18 UTC (rev 187998)
+++ trunk/Source/WebCore/ChangeLog        2015-08-05 23:23:30 UTC (rev 187999)
</span><span class="lines">@@ -1,6 +1,46 @@
</span><span class="cx"> 2015-08-05  Anders Carlsson  &lt;andersca@apple.com&gt;
</span><span class="cx"> 
</span><span class="cx">         Move more functions from DatabaseBackendBase to Database
</span><ins>+        https://bugs.webkit.org/show_bug.cgi?id=147705
+
+        Reviewed by Tim Horton.
+
+        * Modules/webdatabase/Database.cpp:
+        (WebCore::guidMutex):
+        (WebCore::guidToVersionMap):
+        (WebCore::updateGuidVersionMap):
+        (WebCore::guidToDatabaseMap):
+        (WebCore::guidForOriginAndName):
+        (WebCore::Database::Database):
+        (WebCore::DoneCreatingDatabaseOnExitCaller::DoneCreatingDatabaseOnExitCaller):
+        (WebCore::DoneCreatingDatabaseOnExitCaller::~DoneCreatingDatabaseOnExitCaller):
+        (WebCore::DoneCreatingDatabaseOnExitCaller::setOpenSucceeded):
+        (WebCore::Database::performOpenAndVerify):
+        (WebCore::Database::closeDatabase):
+        (WebCore::Database::getCachedVersion):
+        (WebCore::Database::setCachedVersion):
+        * Modules/webdatabase/Database.h:
+        * Modules/webdatabase/DatabaseBackendBase.cpp:
+        (WebCore::DatabaseBackendBase::version):
+        (WebCore::formatErrorMessage): Deleted.
+        (WebCore::guidMutex): Deleted.
+        (WebCore::guidToVersionMap): Deleted.
+        (WebCore::updateGuidVersionMap): Deleted.
+        (WebCore::guidToDatabaseMap): Deleted.
+        (WebCore::guidForOriginAndName): Deleted.
+        (WebCore::DatabaseBackendBase::DatabaseBackendBase): Deleted.
+        (WebCore::DatabaseBackendBase::closeDatabase): Deleted.
+        (WebCore::DoneCreatingDatabaseOnExitCaller::DoneCreatingDatabaseOnExitCaller): Deleted.
+        (WebCore::DoneCreatingDatabaseOnExitCaller::~DoneCreatingDatabaseOnExitCaller): Deleted.
+        (WebCore::DoneCreatingDatabaseOnExitCaller::setOpenSucceeded): Deleted.
+        (WebCore::DatabaseBackendBase::performOpenAndVerify): Deleted.
+        (WebCore::DatabaseBackendBase::getCachedVersion): Deleted.
+        (WebCore::DatabaseBackendBase::setCachedVersion): Deleted.
+        * Modules/webdatabase/DatabaseBackendBase.h:
+
+2015-08-05  Anders Carlsson  &lt;andersca@apple.com&gt;
+
+        Move more functions from DatabaseBackendBase to Database
</ins><span class="cx">         https://bugs.webkit.org/show_bug.cgi?id=147703
</span><span class="cx"> 
</span><span class="cx">         Reviewed by Tim Horton.
</span></span></pre></div>
<a id="trunkSourceWebCoreModuleswebdatabaseDatabasecpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/Modules/webdatabase/Database.cpp (187998 => 187999)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/Modules/webdatabase/Database.cpp        2015-08-05 23:17:18 UTC (rev 187998)
+++ trunk/Source/WebCore/Modules/webdatabase/Database.cpp        2015-08-05 23:23:30 UTC (rev 187999)
</span><span class="lines">@@ -48,9 +48,11 @@
</span><span class="cx"> #include &quot;SQLTransactionErrorCallback.h&quot;
</span><span class="cx"> #include &quot;SQLiteDatabaseTracker.h&quot;
</span><span class="cx"> #include &quot;SQLiteStatement.h&quot;
</span><ins>+#include &quot;SQLiteTransaction.h&quot;
</ins><span class="cx"> #include &quot;ScriptExecutionContext.h&quot;
</span><span class="cx"> #include &quot;SecurityOrigin.h&quot;
</span><span class="cx"> #include &quot;VoidCallback.h&quot;
</span><ins>+#include &lt;wtf/NeverDestroyed.h&gt;
</ins><span class="cx"> #include &lt;wtf/PassRefPtr.h&gt;
</span><span class="cx"> #include &lt;wtf/RefPtr.h&gt;
</span><span class="cx"> #include &lt;wtf/StdLibExtras.h&gt;
</span><span class="lines">@@ -125,6 +127,62 @@
</span><span class="cx">     return false;
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+// FIXME: move all guid-related functions to a DatabaseVersionTracker class.
+static std::mutex&amp; guidMutex()
+{
+    static std::once_flag onceFlag;
+    static LazyNeverDestroyed&lt;std::mutex&gt; mutex;
+
+    std::call_once(onceFlag, [] {
+        mutex.construct();
+    });
+
+    return mutex;
+}
+
+typedef HashMap&lt;DatabaseGuid, String&gt; GuidVersionMap;
+static GuidVersionMap&amp; guidToVersionMap()
+{
+    static NeverDestroyed&lt;GuidVersionMap&gt; map;
+    return map;
+}
+
+// NOTE: Caller must lock guidMutex().
+static inline void updateGuidVersionMap(DatabaseGuid guid, String newVersion)
+{
+    // Note: It is not safe to put an empty string into the guidToVersionMap() map.
+    // That's because the map is cross-thread, but empty strings are per-thread.
+    // The copy() function makes a version of the string you can use on the current
+    // thread, but we need a string we can keep in a cross-thread data structure.
+    // FIXME: This is a quite-awkward restriction to have to program with.
+
+    // Map null string to empty string (see comment above).
+    guidToVersionMap().set(guid, newVersion.isEmpty() ? String() : newVersion.isolatedCopy());
+}
+
+typedef HashMap&lt;DatabaseGuid, std::unique_ptr&lt;HashSet&lt;DatabaseBackendBase*&gt;&gt;&gt; GuidDatabaseMap;
+
+static GuidDatabaseMap&amp; guidToDatabaseMap()
+{
+    static NeverDestroyed&lt;GuidDatabaseMap&gt; map;
+    return map;
+}
+
+static DatabaseGuid guidForOriginAndName(const String&amp; origin, const String&amp; name)
+{
+    String stringID = origin + &quot;/&quot; + name;
+
+    static NeverDestroyed&lt;HashMap&lt;String, int&gt;&gt; map;
+    DatabaseGuid guid = map.get().get(stringID);
+    if (!guid) {
+        static int currentNewGUID = 1;
+        guid = currentNewGUID++;
+        map.get().set(stringID, guid);
+    }
+
+    return guid;
+}
+
</ins><span class="cx"> Database::Database(PassRefPtr&lt;DatabaseContext&gt; databaseContext, const String&amp; name, const String&amp; expectedVersion, const String&amp; displayName, unsigned long estimatedSize)
</span><span class="cx">     : DatabaseBackendBase(databaseContext.get(), name, expectedVersion, displayName, estimatedSize)
</span><span class="cx">     , m_transactionInProgress(false)
</span><span class="lines">@@ -133,6 +191,25 @@
</span><span class="cx">     , m_databaseContext(databaseContext)
</span><span class="cx">     , m_deleted(false)
</span><span class="cx"> {
</span><ins>+    m_contextThreadSecurityOrigin = m_databaseContext-&gt;securityOrigin()-&gt;isolatedCopy();
+
+    m_databaseAuthorizer = DatabaseAuthorizer::create(unqualifiedInfoTableName);
+
+    if (m_name.isNull())
+        m_name = emptyString();
+
+    {
+        std::lock_guard&lt;std::mutex&gt; locker(guidMutex());
+
+        m_guid = guidForOriginAndName(securityOrigin()-&gt;toString(), name);
+        std::unique_ptr&lt;HashSet&lt;DatabaseBackendBase*&gt;&gt;&amp; hashSet = guidToDatabaseMap().add(m_guid, nullptr).iterator-&gt;value;
+        if (!hashSet)
+            hashSet = std::make_unique&lt;HashSet&lt;DatabaseBackendBase*&gt;&gt;();
+        hashSet-&gt;add(this);
+    }
+
+    m_filename = DatabaseManager::singleton().fullPathForDatabase(securityOrigin(), m_name);
+
</ins><span class="cx">     m_databaseThreadSecurityOrigin = m_contextThreadSecurityOrigin-&gt;isolatedCopy();
</span><span class="cx">     setFrontend(this);
</span><span class="cx"> 
</span><span class="lines">@@ -201,18 +278,163 @@
</span><span class="cx">     databaseContext()-&gt;databaseThread()-&gt;unscheduleDatabaseTasks(this);
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-bool Database::performOpenAndVerify(bool setVersionInNewDatabase, DatabaseError&amp; error, String&amp; errorMessage)
</del><ins>+class DoneCreatingDatabaseOnExitCaller {
+public:
+    DoneCreatingDatabaseOnExitCaller(DatabaseBackendBase* database)
+        : m_database(database)
+        , m_openSucceeded(false)
+    {
+    }
+    ~DoneCreatingDatabaseOnExitCaller()
+    {
+        DatabaseTracker::tracker().doneCreatingDatabase(static_cast&lt;Database*&gt;(m_database));
+    }
+
+    void setOpenSucceeded() { m_openSucceeded = true; }
+
+private:
+    DatabaseBackendBase* m_database;
+    bool m_openSucceeded;
+};
+
+bool Database::performOpenAndVerify(bool shouldSetVersionInNewDatabase, DatabaseError&amp; error, String&amp; errorMessage)
</ins><span class="cx"> {
</span><del>-    if (DatabaseBackendBase::performOpenAndVerify(setVersionInNewDatabase, error, errorMessage)) {
-        if (databaseContext()-&gt;databaseThread())
-            databaseContext()-&gt;databaseThread()-&gt;recordDatabaseOpen(this);
</del><ins>+    DoneCreatingDatabaseOnExitCaller onExitCaller(this);
+    ASSERT(errorMessage.isEmpty());
+    ASSERT(error == DatabaseError::None); // Better not have any errors already.
+    error = DatabaseError::InvalidDatabaseState; // Presumed failure. We'll clear it if we succeed below.
</ins><span class="cx"> 
</span><del>-        return true;
</del><ins>+    const int maxSqliteBusyWaitTime = 30000;
+
+#if PLATFORM(IOS)
+    {
+        // Make sure we wait till the background removal of the empty database files finished before trying to open any database.
+        DeprecatedMutexLocker locker(DatabaseTracker::openDatabaseMutex());
</ins><span class="cx">     }
</span><ins>+#endif
</ins><span class="cx"> 
</span><del>-    return false;
</del><ins>+    SQLiteTransactionInProgressAutoCounter transactionCounter;
+
+    if (!m_sqliteDatabase.open(m_filename, true)) {
+        errorMessage = formatErrorMessage(&quot;unable to open database&quot;, m_sqliteDatabase.lastError(), m_sqliteDatabase.lastErrorMsg());
+        return false;
+    }
+    if (!m_sqliteDatabase.turnOnIncrementalAutoVacuum())
+        LOG_ERROR(&quot;Unable to turn on incremental auto-vacuum (%d %s)&quot;, m_sqliteDatabase.lastError(), m_sqliteDatabase.lastErrorMsg());
+
+    m_sqliteDatabase.setBusyTimeout(maxSqliteBusyWaitTime);
+
+    String currentVersion;
+    {
+        std::lock_guard&lt;std::mutex&gt; locker(guidMutex());
+
+        auto entry = guidToVersionMap().find(m_guid);
+        if (entry != guidToVersionMap().end()) {
+            // Map null string to empty string (see updateGuidVersionMap()).
+            currentVersion = entry-&gt;value.isNull() ? emptyString() : entry-&gt;value.isolatedCopy();
+            LOG(StorageAPI, &quot;Current cached version for guid %i is %s&quot;, m_guid, currentVersion.ascii().data());
+        } else {
+            LOG(StorageAPI, &quot;No cached version for guid %i&quot;, m_guid);
+
+            SQLiteTransaction transaction(m_sqliteDatabase);
+            transaction.begin();
+            if (!transaction.inProgress()) {
+                errorMessage = formatErrorMessage(&quot;unable to open database, failed to start transaction&quot;, m_sqliteDatabase.lastError(), m_sqliteDatabase.lastErrorMsg());
+                m_sqliteDatabase.close();
+                return false;
+            }
+
+            String tableName(unqualifiedInfoTableName);
+            if (!m_sqliteDatabase.tableExists(tableName)) {
+                m_new = true;
+
+                if (!m_sqliteDatabase.executeCommand(&quot;CREATE TABLE &quot; + tableName + &quot; (key TEXT NOT NULL ON CONFLICT FAIL UNIQUE ON CONFLICT REPLACE,value TEXT NOT NULL ON CONFLICT FAIL);&quot;)) {
+                    errorMessage = formatErrorMessage(&quot;unable to open database, failed to create 'info' table&quot;, m_sqliteDatabase.lastError(), m_sqliteDatabase.lastErrorMsg());
+                    transaction.rollback();
+                    m_sqliteDatabase.close();
+                    return false;
+                }
+            } else if (!m_frontend-&gt;getVersionFromDatabase(currentVersion, false)) {
+                errorMessage = formatErrorMessage(&quot;unable to open database, failed to read current version&quot;, m_sqliteDatabase.lastError(), m_sqliteDatabase.lastErrorMsg());
+                transaction.rollback();
+                m_sqliteDatabase.close();
+                return false;
+            }
+
+            if (currentVersion.length()) {
+                LOG(StorageAPI, &quot;Retrieved current version %s from database %s&quot;, currentVersion.ascii().data(), databaseDebugName().ascii().data());
+            } else if (!m_new || shouldSetVersionInNewDatabase) {
+                LOG(StorageAPI, &quot;Setting version %s in database %s that was just created&quot;, m_expectedVersion.ascii().data(), databaseDebugName().ascii().data());
+                if (!m_frontend-&gt;setVersionInDatabase(m_expectedVersion, false)) {
+                    errorMessage = formatErrorMessage(&quot;unable to open database, failed to write current version&quot;, m_sqliteDatabase.lastError(), m_sqliteDatabase.lastErrorMsg());
+                    transaction.rollback();
+                    m_sqliteDatabase.close();
+                    return false;
+                }
+                currentVersion = m_expectedVersion;
+            }
+            updateGuidVersionMap(m_guid, currentVersion);
+            transaction.commit();
+        }
+    }
+
+    if (currentVersion.isNull()) {
+        LOG(StorageAPI, &quot;Database %s does not have its version set&quot;, databaseDebugName().ascii().data());
+        currentVersion = &quot;&quot;;
+    }
+
+    // If the expected version isn't the empty string, ensure that the current database version we have matches that version. Otherwise, set an exception.
+    // If the expected version is the empty string, then we always return with whatever version of the database we have.
+    if ((!m_new || shouldSetVersionInNewDatabase) &amp;&amp; m_expectedVersion.length() &amp;&amp; m_expectedVersion != currentVersion) {
+        errorMessage = &quot;unable to open database, version mismatch, '&quot; + m_expectedVersion + &quot;' does not match the currentVersion of '&quot; + currentVersion + &quot;'&quot;;
+        m_sqliteDatabase.close();
+        return false;
+    }
+
+    ASSERT(m_databaseAuthorizer);
+    m_sqliteDatabase.setAuthorizer(m_databaseAuthorizer);
+
+    // See comment at the top this file regarding calling addOpenDatabase().
+    DatabaseTracker::tracker().addOpenDatabase(static_cast&lt;Database*&gt;(this));
+    m_opened = true;
+
+    // Declare success:
+    error = DatabaseError::None; // Clear the presumed error from above.
+    onExitCaller.setOpenSucceeded();
+
+    if (m_new &amp;&amp; !shouldSetVersionInNewDatabase)
+        m_expectedVersion = &quot;&quot;; // The caller provided a creationCallback which will set the expected version.
+
+    if (databaseContext()-&gt;databaseThread())
+        databaseContext()-&gt;databaseThread()-&gt;recordDatabaseOpen(this);
+
+    return true;
</ins><span class="cx"> }
</span><span class="cx"> 
</span><ins>+void Database::closeDatabase()
+{
+    if (!m_opened)
+        return;
+
+    m_sqliteDatabase.close();
+    m_opened = false;
+    // See comment at the top this file regarding calling removeOpenDatabase().
+    DatabaseTracker::tracker().removeOpenDatabase(this);
+    {
+        std::lock_guard&lt;std::mutex&gt; locker(guidMutex());
+
+        auto it = guidToDatabaseMap().find(m_guid);
+        ASSERT(it != guidToDatabaseMap().end());
+        ASSERT(it-&gt;value);
+        ASSERT(it-&gt;value-&gt;contains(this));
+        it-&gt;value-&gt;remove(this);
+        if (it-&gt;value-&gt;isEmpty()) {
+            guidToDatabaseMap().remove(it);
+            guidToVersionMap().remove(m_guid);
+        }
+    }
+}
+
</ins><span class="cx"> bool Database::getVersionFromDatabase(String&amp; version, bool shouldCacheVersion)
</span><span class="cx"> {
</span><span class="cx">     String query(String(&quot;SELECT value FROM &quot;) + fullyQualifiedInfoTableName() +  &quot; WHERE key = '&quot; + versionKey + &quot;';&quot;);
</span><span class="lines">@@ -251,6 +473,21 @@
</span><span class="cx">     return result;
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+String Database::getCachedVersion() const
+{
+    std::lock_guard&lt;std::mutex&gt; locker(guidMutex());
+
+    return guidToVersionMap().get(m_guid).isolatedCopy();
+}
+
+void Database::setCachedVersion(const String&amp; actualVersion)
+{
+    // Update the in memory database version map.
+    std::lock_guard&lt;std::mutex&gt; locker(guidMutex());
+
+    updateGuidVersionMap(m_guid, actualVersion);
+}
+
</ins><span class="cx"> void Database::scheduleTransaction()
</span><span class="cx"> {
</span><span class="cx">     ASSERT(!m_transactionInProgressMutex.tryLock()); // Locked by caller.
</span></span></pre></div>
<a id="trunkSourceWebCoreModuleswebdatabaseDatabaseh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/Modules/webdatabase/Database.h (187998 => 187999)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/Modules/webdatabase/Database.h        2015-08-05 23:17:18 UTC (rev 187998)
+++ trunk/Source/WebCore/Modules/webdatabase/Database.h        2015-08-05 23:23:30 UTC (rev 187999)
</span><span class="lines">@@ -100,7 +100,7 @@
</span><span class="cx"> 
</span><span class="cx">     void scheduleTransactionCallback(SQLTransaction*);
</span><span class="cx"> 
</span><del>-    virtual bool performOpenAndVerify(bool setVersionInNewDatabase, DatabaseError&amp;, String&amp; errorMessage);
</del><ins>+    bool performOpenAndVerify(bool shouldSetVersionInNewDatabase, DatabaseError&amp;, String&amp; errorMessage);
</ins><span class="cx">     Vector&lt;String&gt; performGetTableNames();
</span><span class="cx"> 
</span><span class="cx">     void incrementalVacuumIfNeeded();
</span><span class="lines">@@ -108,8 +108,12 @@
</span><span class="cx"> private:
</span><span class="cx">     Database(PassRefPtr&lt;DatabaseContext&gt;, const String&amp; name, const String&amp; expectedVersion, const String&amp; displayName, unsigned long estimatedSize);
</span><span class="cx"> 
</span><ins>+    void closeDatabase();
+
</ins><span class="cx">     bool getVersionFromDatabase(String&amp; version, bool shouldCacheVersion = true);
</span><span class="cx">     bool setVersionInDatabase(const String&amp; version, bool shouldCacheVersion = true);
</span><ins>+    String getCachedVersion()const;
+    void setCachedVersion(const String&amp;);
</ins><span class="cx"> 
</span><span class="cx">     void scheduleTransaction();
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkSourceWebCoreModuleswebdatabaseDatabaseBackendBasecpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/Modules/webdatabase/DatabaseBackendBase.cpp (187998 => 187999)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/Modules/webdatabase/DatabaseBackendBase.cpp        2015-08-05 23:17:18 UTC (rev 187998)
+++ trunk/Source/WebCore/Modules/webdatabase/DatabaseBackendBase.cpp        2015-08-05 23:23:30 UTC (rev 187999)
</span><span class="lines">@@ -39,7 +39,6 @@
</span><span class="cx"> #include &quot;Logging.h&quot;
</span><span class="cx"> #include &quot;SQLiteDatabaseTracker.h&quot;
</span><span class="cx"> #include &quot;SQLiteStatement.h&quot;
</span><del>-#include &quot;SQLiteTransaction.h&quot;
</del><span class="cx"> #include &quot;SecurityOrigin.h&quot;
</span><span class="cx"> #include &lt;wtf/HashMap.h&gt;
</span><span class="cx"> #include &lt;wtf/HashSet.h&gt;
</span><span class="lines">@@ -81,69 +80,6 @@
</span><span class="cx"> 
</span><span class="cx"> namespace WebCore {
</span><span class="cx"> 
</span><del>-static const char unqualifiedInfoTableName[] = &quot;__WebKitDatabaseInfoTable__&quot;;
-
-static String formatErrorMessage(const char* message, int sqliteErrorCode, const char* sqliteErrorMessage)
-{
-    return String::format(&quot;%s (%d %s)&quot;, message, sqliteErrorCode, sqliteErrorMessage);
-}
-
-// FIXME: move all guid-related functions to a DatabaseVersionTracker class.
-static std::mutex&amp; guidMutex()
-{
-    static std::once_flag onceFlag;
-    static LazyNeverDestroyed&lt;std::mutex&gt; mutex;
-
-    std::call_once(onceFlag, []{
-        mutex.construct();
-    });
-
-    return mutex;
-}
-
-typedef HashMap&lt;DatabaseGuid, String&gt; GuidVersionMap;
-static GuidVersionMap&amp; guidToVersionMap()
-{
-    static NeverDestroyed&lt;GuidVersionMap&gt; map;
-    return map;
-}
-
-// NOTE: Caller must lock guidMutex().
-static inline void updateGuidVersionMap(DatabaseGuid guid, String newVersion)
-{
-    // Note: It is not safe to put an empty string into the guidToVersionMap() map.
-    // That's because the map is cross-thread, but empty strings are per-thread.
-    // The copy() function makes a version of the string you can use on the current
-    // thread, but we need a string we can keep in a cross-thread data structure.
-    // FIXME: This is a quite-awkward restriction to have to program with.
-
-    // Map null string to empty string (see comment above).
-    guidToVersionMap().set(guid, newVersion.isEmpty() ? String() : newVersion.isolatedCopy());
-}
-
-typedef HashMap&lt;DatabaseGuid, std::unique_ptr&lt;HashSet&lt;DatabaseBackendBase*&gt;&gt;&gt; GuidDatabaseMap;
-
-static GuidDatabaseMap&amp; guidToDatabaseMap()
-{
-    static NeverDestroyed&lt;GuidDatabaseMap&gt; map;
-    return map;
-}
-
-static DatabaseGuid guidForOriginAndName(const String&amp; origin, const String&amp; name)
-{
-    String stringID = origin + &quot;/&quot; + name;
-
-    static NeverDestroyed&lt;HashMap&lt;String, int&gt;&gt; map;
-    DatabaseGuid guid = map.get().get(stringID);
-    if (!guid) {
-        static int currentNewGUID = 1;
-        guid = currentNewGUID++;
-        map.get().set(stringID, guid);
-    }
-
-    return guid;
-}
-
</del><span class="cx"> #if !LOG_DISABLED || !ERROR_DISABLED
</span><span class="cx"> String DatabaseBackendBase::databaseDebugName() const
</span><span class="cx"> {
</span><span class="lines">@@ -160,24 +96,6 @@
</span><span class="cx">     , m_opened(false)
</span><span class="cx">     , m_new(false)
</span><span class="cx"> {
</span><del>-    m_contextThreadSecurityOrigin = m_databaseContext-&gt;securityOrigin()-&gt;isolatedCopy();
-
-    m_databaseAuthorizer = DatabaseAuthorizer::create(unqualifiedInfoTableName);
-
-    if (m_name.isNull())
-        m_name = emptyString();
-
-    {
-        std::lock_guard&lt;std::mutex&gt; locker(guidMutex());
-
-        m_guid = guidForOriginAndName(securityOrigin()-&gt;toString(), name);
-        std::unique_ptr&lt;HashSet&lt;DatabaseBackendBase*&gt;&gt;&amp; hashSet = guidToDatabaseMap().add(m_guid, nullptr).iterator-&gt;value;
-        if (!hashSet)
-            hashSet = std::make_unique&lt;HashSet&lt;DatabaseBackendBase*&gt;&gt;();
-        hashSet-&gt;add(this);
-    }
-
-    m_filename = DatabaseManager::singleton().fullPathForDatabase(securityOrigin(), m_name);
</del><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> DatabaseBackendBase::~DatabaseBackendBase()
</span><span class="lines">@@ -194,167 +112,14 @@
</span><span class="cx">     ASSERT(!m_opened);
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-void DatabaseBackendBase::closeDatabase()
-{
-    if (!m_opened)
-        return;
-
-    m_sqliteDatabase.close();
-    m_opened = false;
-    // See comment at the top this file regarding calling removeOpenDatabase().
-    DatabaseTracker::tracker().removeOpenDatabase(static_cast&lt;Database*&gt;(this));
-    {
-        std::lock_guard&lt;std::mutex&gt; locker(guidMutex());
-
-        auto it = guidToDatabaseMap().find(m_guid);
-        ASSERT(it != guidToDatabaseMap().end());
-        ASSERT(it-&gt;value);
-        ASSERT(it-&gt;value-&gt;contains(this));
-        it-&gt;value-&gt;remove(this);
-        if (it-&gt;value-&gt;isEmpty()) {
-            guidToDatabaseMap().remove(it);
-            guidToVersionMap().remove(m_guid);
-        }
-    }
-}
-
</del><span class="cx"> String DatabaseBackendBase::version() const
</span><span class="cx"> {
</span><span class="cx">     // Note: In multi-process browsers the cached value may be accurate, but we cannot read the
</span><span class="cx">     // actual version from the database without potentially inducing a deadlock.
</span><span class="cx">     // FIXME: Add an async version getter to the DatabaseAPI.
</span><del>-    return getCachedVersion();
</del><ins>+    return m_frontend-&gt;getCachedVersion();
</ins><span class="cx"> }
</span><span class="cx"> 
</span><del>-class DoneCreatingDatabaseOnExitCaller {
-public:
-    DoneCreatingDatabaseOnExitCaller(DatabaseBackendBase* database)
-        : m_database(database)
-        , m_openSucceeded(false)
-    {
-    }
-    ~DoneCreatingDatabaseOnExitCaller()
-    {
-        DatabaseTracker::tracker().doneCreatingDatabase(static_cast&lt;Database*&gt;(m_database));
-    }
-
-    void setOpenSucceeded() { m_openSucceeded = true; }
-
-private:
-    DatabaseBackendBase* m_database;
-    bool m_openSucceeded;
-};
-
-bool DatabaseBackendBase::performOpenAndVerify(bool shouldSetVersionInNewDatabase, DatabaseError&amp; error, String&amp; errorMessage)
-{
-    DoneCreatingDatabaseOnExitCaller onExitCaller(this);
-    ASSERT(errorMessage.isEmpty());
-    ASSERT(error == DatabaseError::None); // Better not have any errors already.
-    error = DatabaseError::InvalidDatabaseState; // Presumed failure. We'll clear it if we succeed below.
-
-    const int maxSqliteBusyWaitTime = 30000;
-
-#if PLATFORM(IOS)
-    {
-        // Make sure we wait till the background removal of the empty database files finished before trying to open any database.
-        DeprecatedMutexLocker locker(DatabaseTracker::openDatabaseMutex());
-    }
-#endif
-
-    SQLiteTransactionInProgressAutoCounter transactionCounter;
-
-    if (!m_sqliteDatabase.open(m_filename, true)) {
-        errorMessage = formatErrorMessage(&quot;unable to open database&quot;, m_sqliteDatabase.lastError(), m_sqliteDatabase.lastErrorMsg());
-        return false;
-    }
-    if (!m_sqliteDatabase.turnOnIncrementalAutoVacuum())
-        LOG_ERROR(&quot;Unable to turn on incremental auto-vacuum (%d %s)&quot;, m_sqliteDatabase.lastError(), m_sqliteDatabase.lastErrorMsg());
-
-    m_sqliteDatabase.setBusyTimeout(maxSqliteBusyWaitTime);
-
-    String currentVersion;
-    {
-        std::lock_guard&lt;std::mutex&gt; locker(guidMutex());
-
-        auto entry = guidToVersionMap().find(m_guid);
-        if (entry != guidToVersionMap().end()) {
-            // Map null string to empty string (see updateGuidVersionMap()).
-            currentVersion = entry-&gt;value.isNull() ? emptyString() : entry-&gt;value.isolatedCopy();
-            LOG(StorageAPI, &quot;Current cached version for guid %i is %s&quot;, m_guid, currentVersion.ascii().data());
-        } else {
-            LOG(StorageAPI, &quot;No cached version for guid %i&quot;, m_guid);
-
-            SQLiteTransaction transaction(m_sqliteDatabase);
-            transaction.begin();
-            if (!transaction.inProgress()) {
-                errorMessage = formatErrorMessage(&quot;unable to open database, failed to start transaction&quot;, m_sqliteDatabase.lastError(), m_sqliteDatabase.lastErrorMsg());
-                m_sqliteDatabase.close();
-                return false;
-            }
-
-            String tableName(unqualifiedInfoTableName);
-            if (!m_sqliteDatabase.tableExists(tableName)) {
-                m_new = true;
-
-                if (!m_sqliteDatabase.executeCommand(&quot;CREATE TABLE &quot; + tableName + &quot; (key TEXT NOT NULL ON CONFLICT FAIL UNIQUE ON CONFLICT REPLACE,value TEXT NOT NULL ON CONFLICT FAIL);&quot;)) {
-                    errorMessage = formatErrorMessage(&quot;unable to open database, failed to create 'info' table&quot;, m_sqliteDatabase.lastError(), m_sqliteDatabase.lastErrorMsg());
-                    transaction.rollback();
-                    m_sqliteDatabase.close();
-                    return false;
-                }
-            } else if (!m_frontend-&gt;getVersionFromDatabase(currentVersion, false)) {
-                errorMessage = formatErrorMessage(&quot;unable to open database, failed to read current version&quot;, m_sqliteDatabase.lastError(), m_sqliteDatabase.lastErrorMsg());
-                transaction.rollback();
-                m_sqliteDatabase.close();
-                return false;
-            }
-
-            if (currentVersion.length()) {
-                LOG(StorageAPI, &quot;Retrieved current version %s from database %s&quot;, currentVersion.ascii().data(), databaseDebugName().ascii().data());
-            } else if (!m_new || shouldSetVersionInNewDatabase) {
-                LOG(StorageAPI, &quot;Setting version %s in database %s that was just created&quot;, m_expectedVersion.ascii().data(), databaseDebugName().ascii().data());
-                if (!m_frontend-&gt;setVersionInDatabase(m_expectedVersion, false)) {
-                    errorMessage = formatErrorMessage(&quot;unable to open database, failed to write current version&quot;, m_sqliteDatabase.lastError(), m_sqliteDatabase.lastErrorMsg());
-                    transaction.rollback();
-                    m_sqliteDatabase.close();
-                    return false;
-                }
-                currentVersion = m_expectedVersion;
-            }
-            updateGuidVersionMap(m_guid, currentVersion);
-            transaction.commit();
-        }
-    }
-
-    if (currentVersion.isNull()) {
-        LOG(StorageAPI, &quot;Database %s does not have its version set&quot;, databaseDebugName().ascii().data());
-        currentVersion = &quot;&quot;;
-    }
-
-    // If the expected version isn't the empty string, ensure that the current database version we have matches that version. Otherwise, set an exception.
-    // If the expected version is the empty string, then we always return with whatever version of the database we have.
-    if ((!m_new || shouldSetVersionInNewDatabase) &amp;&amp; m_expectedVersion.length() &amp;&amp; m_expectedVersion != currentVersion) {
-        errorMessage = &quot;unable to open database, version mismatch, '&quot; + m_expectedVersion + &quot;' does not match the currentVersion of '&quot; + currentVersion + &quot;'&quot;;
-        m_sqliteDatabase.close();
-        return false;
-    }
-
-    ASSERT(m_databaseAuthorizer);
-    m_sqliteDatabase.setAuthorizer(m_databaseAuthorizer);
-
-    // See comment at the top this file regarding calling addOpenDatabase().
-    DatabaseTracker::tracker().addOpenDatabase(static_cast&lt;Database*&gt;(this));
-    m_opened = true;
-
-    // Declare success:
-    error = DatabaseError::None; // Clear the presumed error from above.
-    onExitCaller.setOpenSucceeded();
-
-    if (m_new &amp;&amp; !shouldSetVersionInNewDatabase)
-        m_expectedVersion = &quot;&quot;; // The caller provided a creationCallback which will set the expected version.
-    return true;
-}
-
</del><span class="cx"> SecurityOrigin* DatabaseBackendBase::securityOrigin() const
</span><span class="cx"> {
</span><span class="cx">     return m_contextThreadSecurityOrigin.get();
</span><span class="lines">@@ -394,21 +159,6 @@
</span><span class="cx">     m_expectedVersion = version.isolatedCopy();
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-String DatabaseBackendBase::getCachedVersion() const
-{
-    std::lock_guard&lt;std::mutex&gt; locker(guidMutex());
-
-    return guidToVersionMap().get(m_guid).isolatedCopy();
-}
-
-void DatabaseBackendBase::setCachedVersion(const String&amp; actualVersion)
-{
-    // Update the in memory database version map.
-    std::lock_guard&lt;std::mutex&gt; locker(guidMutex());
-
-    updateGuidVersionMap(m_guid, actualVersion);
-}
-
</del><span class="cx"> bool DatabaseBackendBase::getActualVersionForTransaction(String &amp;actualVersion)
</span><span class="cx"> {
</span><span class="cx">     ASSERT(m_sqliteDatabase.transactionInProgress());
</span></span></pre></div>
<a id="trunkSourceWebCoreModuleswebdatabaseDatabaseBackendBaseh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/Modules/webdatabase/DatabaseBackendBase.h (187998 => 187999)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/Modules/webdatabase/DatabaseBackendBase.h        2015-08-05 23:17:18 UTC (rev 187998)
+++ trunk/Source/WebCore/Modules/webdatabase/DatabaseBackendBase.h        2015-08-05 23:23:30 UTC (rev 187999)
</span><span class="lines">@@ -77,14 +77,9 @@
</span><span class="cx"> 
</span><span class="cx">     DatabaseBackendBase(PassRefPtr&lt;DatabaseContext&gt;, const String&amp; name, const String&amp; expectedVersion, const String&amp; displayName, unsigned long estimatedSize);
</span><span class="cx"> 
</span><del>-    void closeDatabase();
</del><span class="cx"> 
</span><del>-    virtual bool performOpenAndVerify(bool shouldSetVersionInNewDatabase, DatabaseError&amp;, String&amp; errorMessage);
-
</del><span class="cx">     void setExpectedVersion(const String&amp;);
</span><span class="cx">     const String&amp; expectedVersion() const { return m_expectedVersion; }
</span><del>-    String getCachedVersion()const;
-    void setCachedVersion(const String&amp;);
</del><span class="cx">     bool getActualVersionForTransaction(String&amp; version);
</span><span class="cx"> 
</span><span class="cx"> #if !LOG_DISABLED || !ERROR_DISABLED
</span></span></pre>
</div>
</div>

</body>
</html>