Modern IDB: Support IDBDatabase.close().
authorbeidson@apple.com <beidson@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 16 Oct 2015 00:25:02 +0000 (00:25 +0000)
committerbeidson@apple.com <beidson@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 16 Oct 2015 00:25:02 +0000 (00:25 +0000)
https://bugs.webkit.org/show_bug.cgi?id=150150

Reviewed by Alex Christensen.

Source/WebCore:

No new tests (Covered by changes to storage/indexeddb/modern/opendatabase-versions.html).

* Modules/indexeddb/client/IDBConnectionToServer.cpp:
(WebCore::IDBClient::IDBConnectionToServer::databaseConnectionClosed):
* Modules/indexeddb/client/IDBConnectionToServer.h:
* Modules/indexeddb/client/IDBConnectionToServerDelegate.h:

* Modules/indexeddb/client/IDBDatabaseImpl.cpp:
(WebCore::IDBClient::IDBDatabase::IDBDatabase):
(WebCore::IDBClient::IDBDatabase::~IDBDatabase):
(WebCore::IDBClient::IDBDatabase::close):
(WebCore::IDBClient::IDBDatabase::maybeCloseInServer):
(WebCore::IDBClient::IDBDatabase::commitTransaction):
* Modules/indexeddb/client/IDBDatabaseImpl.h:
(WebCore::IDBClient::IDBDatabase::databaseConnectionIdentifier):

* Modules/indexeddb/client/IDBRequestImpl.cpp:
(WebCore::IDBClient::IDBRequest::result):

* Modules/indexeddb/server/IDBServer.cpp:
(WebCore::IDBServer::IDBServer::databaseConnectionClosed):
* Modules/indexeddb/server/IDBServer.h:

* Modules/indexeddb/server/UniqueIDBDatabase.cpp:
(WebCore::IDBServer::UniqueIDBDatabase::connectionClosedFromClient):
(WebCore::IDBServer::UniqueIDBDatabase::handleOpenDatabaseOperations): Deleted.
* Modules/indexeddb/server/UniqueIDBDatabase.h:

* Modules/indexeddb/server/UniqueIDBDatabaseConnection.cpp:
(WebCore::IDBServer::UniqueIDBDatabaseConnection::UniqueIDBDatabaseConnection):
(WebCore::IDBServer::UniqueIDBDatabaseConnection::~UniqueIDBDatabaseConnection):
(WebCore::IDBServer::UniqueIDBDatabaseConnection::hasNonFinishedTransactions):
(WebCore::IDBServer::UniqueIDBDatabaseConnection::connectionClosedFromClient):
* Modules/indexeddb/server/UniqueIDBDatabaseConnection.h:

* Modules/indexeddb/shared/InProcessIDBServer.cpp:
(WebCore::InProcessIDBServer::databaseConnectionClosed):
* Modules/indexeddb/shared/InProcessIDBServer.h:

LayoutTests:

* storage/indexeddb/modern/opendatabase-versions-expected.txt:
* storage/indexeddb/modern/opendatabase-versions.html:

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@191156 268f45cc-cd09-0410-ab3c-d52691b4dbfc

18 files changed:
LayoutTests/ChangeLog
LayoutTests/storage/indexeddb/modern/opendatabase-versions-expected.txt
LayoutTests/storage/indexeddb/modern/opendatabase-versions.html
Source/WebCore/ChangeLog
Source/WebCore/Modules/indexeddb/client/IDBConnectionToServer.cpp
Source/WebCore/Modules/indexeddb/client/IDBConnectionToServer.h
Source/WebCore/Modules/indexeddb/client/IDBConnectionToServerDelegate.h
Source/WebCore/Modules/indexeddb/client/IDBDatabaseImpl.cpp
Source/WebCore/Modules/indexeddb/client/IDBDatabaseImpl.h
Source/WebCore/Modules/indexeddb/client/IDBRequestImpl.cpp
Source/WebCore/Modules/indexeddb/server/IDBServer.cpp
Source/WebCore/Modules/indexeddb/server/IDBServer.h
Source/WebCore/Modules/indexeddb/server/UniqueIDBDatabase.cpp
Source/WebCore/Modules/indexeddb/server/UniqueIDBDatabase.h
Source/WebCore/Modules/indexeddb/server/UniqueIDBDatabaseConnection.cpp
Source/WebCore/Modules/indexeddb/server/UniqueIDBDatabaseConnection.h
Source/WebCore/Modules/indexeddb/shared/InProcessIDBServer.cpp
Source/WebCore/Modules/indexeddb/shared/InProcessIDBServer.h

index 53babcd..cda9812 100644 (file)
@@ -1,3 +1,13 @@
+2015-10-15  Brady Eidson  <beidson@apple.com>
+
+        Modern IDB: Support IDBDatabase.close().
+        https://bugs.webkit.org/show_bug.cgi?id=150150
+
+        Reviewed by Alex Christensen.
+
+        * storage/indexeddb/modern/opendatabase-versions-expected.txt:
+        * storage/indexeddb/modern/opendatabase-versions.html:
+
 2015-10-15  Ryan Haddad  <ryanhaddad@apple.com>
 
         Marking another disk-cache test as flaky on mac-wk2
index ebcc163..7250b95 100644 (file)
@@ -4,6 +4,10 @@ ALERT: [object IDBTransaction]
 ALERT: Version change complete (firstPhase). Database version is now - 1
 ALERT: [object IDBOpenDBRequest] (secondPhase)
 ALERT: Successfully opened database at version 1 (secondPhase)
+ALERT: [object IDBOpenDBRequest] (thirdPhase)
+ALERT: upgradeneeded (thirdPhase): old version - 1 new version - 2
+ALERT: [object IDBTransaction]
+ALERT: Version change complete (thirdPhase). Database version is now - 2
 ALERT: Done
 This test creates a new database with the default version, commits that versionchange transaction, and then reopens it at different versions to make sure the IDBOpenDBRequests behave appropriately.
 
index 7be934c..fb1d806 100644 (file)
@@ -34,6 +34,7 @@ request.onupgradeneeded = function(e)
     request.transaction.oncomplete = function()
     {
         alert("Version change complete (firstPhase). Database version is now - " + request.transaction.db.version);
+        request.transaction.db.close();
         secondPhase();
     }
     request.transaction.onabort = function()
@@ -55,10 +56,9 @@ function secondPhase()
     request.onsuccess = function()
     {
         alert("Successfully opened database at version 1 (secondPhase)");
-
-        // FIXME: The following call will be used once bugs 150147-150150 are resolved.
-        // thirdPhase();
-        done();
+        request.result.close();
+        request.result.close(); // Close it twice just for the heck of it
+        thirdPhase();
     }
     request.onerror = function(e)
     {
@@ -78,13 +78,13 @@ function thirdPhase()
     alert(request + " (thirdPhase)");
     request.onsuccess = function()
     {
-     alert("Unexpected success (thirdPhase)");
-     done();
+        alert("Unexpected success (thirdPhase)");
+        done();
     }
     request.onerror = function(e)
     {
-     alert("Unexpected error (thirdPhase)");
-     done();
+        alert("Unexpected error (thirdPhase)");
+        done();
     }
     request.onupgradeneeded = function(e)
     {
@@ -93,6 +93,7 @@ function thirdPhase()
         request.transaction.oncomplete = function()
         {
             alert("Version change complete (thirdPhase). Database version is now - " + request.transaction.db.version);
+            request.transaction.db.close();
             fourthPhase();
         }
         request.transaction.onabort = function()
index e54716d..e3a6067 100644 (file)
@@ -1,3 +1,49 @@
+2015-10-15  Brady Eidson  <beidson@apple.com>
+
+        Modern IDB: Support IDBDatabase.close().
+        https://bugs.webkit.org/show_bug.cgi?id=150150
+
+        Reviewed by Alex Christensen.
+
+        No new tests (Covered by changes to storage/indexeddb/modern/opendatabase-versions.html).
+
+        * Modules/indexeddb/client/IDBConnectionToServer.cpp:
+        (WebCore::IDBClient::IDBConnectionToServer::databaseConnectionClosed):
+        * Modules/indexeddb/client/IDBConnectionToServer.h:
+        * Modules/indexeddb/client/IDBConnectionToServerDelegate.h:
+
+        * Modules/indexeddb/client/IDBDatabaseImpl.cpp:
+        (WebCore::IDBClient::IDBDatabase::IDBDatabase):
+        (WebCore::IDBClient::IDBDatabase::~IDBDatabase):
+        (WebCore::IDBClient::IDBDatabase::close):
+        (WebCore::IDBClient::IDBDatabase::maybeCloseInServer):
+        (WebCore::IDBClient::IDBDatabase::commitTransaction):
+        * Modules/indexeddb/client/IDBDatabaseImpl.h:
+        (WebCore::IDBClient::IDBDatabase::databaseConnectionIdentifier):
+
+        * Modules/indexeddb/client/IDBRequestImpl.cpp:
+        (WebCore::IDBClient::IDBRequest::result):
+
+        * Modules/indexeddb/server/IDBServer.cpp:
+        (WebCore::IDBServer::IDBServer::databaseConnectionClosed):
+        * Modules/indexeddb/server/IDBServer.h:
+
+        * Modules/indexeddb/server/UniqueIDBDatabase.cpp:
+        (WebCore::IDBServer::UniqueIDBDatabase::connectionClosedFromClient):
+        (WebCore::IDBServer::UniqueIDBDatabase::handleOpenDatabaseOperations): Deleted.
+        * Modules/indexeddb/server/UniqueIDBDatabase.h:
+
+        * Modules/indexeddb/server/UniqueIDBDatabaseConnection.cpp:
+        (WebCore::IDBServer::UniqueIDBDatabaseConnection::UniqueIDBDatabaseConnection):
+        (WebCore::IDBServer::UniqueIDBDatabaseConnection::~UniqueIDBDatabaseConnection):
+        (WebCore::IDBServer::UniqueIDBDatabaseConnection::hasNonFinishedTransactions):
+        (WebCore::IDBServer::UniqueIDBDatabaseConnection::connectionClosedFromClient):
+        * Modules/indexeddb/server/UniqueIDBDatabaseConnection.h:
+
+        * Modules/indexeddb/shared/InProcessIDBServer.cpp:
+        (WebCore::InProcessIDBServer::databaseConnectionClosed):
+        * Modules/indexeddb/shared/InProcessIDBServer.h:
+
 2015-10-15  David Hyatt  <hyatt@apple.com>
 
         Patch parseKeywordValue to accept "unset" so that it goes down the faster parsing path.
index 018fc0e..ddd0fff 100644 (file)
@@ -120,6 +120,13 @@ void IDBConnectionToServer::fireVersionChangeEvent(uint64_t /*databaseConnection
     // FIXME: Implement versionchange events firing on already-open transactions.
 }
 
+void IDBConnectionToServer::databaseConnectionClosed(IDBDatabase& database)
+{
+    LOG(IndexedDB, "IDBConnectionToServer::databaseConnectionClosed");
+
+    m_delegate->databaseConnectionClosed(database.databaseConnectionIdentifier());
+}
+
 void IDBConnectionToServer::registerDatabaseConnection(IDBDatabase& database)
 {
     ASSERT(!m_databaseConnections.contains(&database));
index 4468f98..41ad432 100644 (file)
@@ -63,6 +63,7 @@ public:
 
     void fireVersionChangeEvent(uint64_t databaseConnectionIdentifier, uint64_t requestedVersion);
 
+    void databaseConnectionClosed(IDBDatabase&);
     void registerDatabaseConnection(IDBDatabase&);
     void unregisterDatabaseConnection(IDBDatabase&);
 
index 6d2288e..5607bbb 100644 (file)
@@ -43,6 +43,7 @@ public:
     virtual void deleteDatabase(IDBRequestData&) = 0;
     virtual void openDatabase(IDBRequestData&) = 0;
     virtual void commitTransaction(IDBResourceIdentifier&) = 0;
+    virtual void databaseConnectionClosed(uint64_t databaseConnectionIdentifier) = 0;
 
     virtual void ref() = 0;
     virtual void deref() = 0;
index 6034b3f..7e7f548 100644 (file)
@@ -44,17 +44,18 @@ Ref<IDBDatabase> IDBDatabase::create(ScriptExecutionContext& context, IDBConnect
 
 IDBDatabase::IDBDatabase(ScriptExecutionContext& context, IDBConnectionToServer& connection, const IDBResultData& resultData)
     : WebCore::IDBDatabase(&context)
-    , m_connection(connection)
+    , m_serverConnection(connection)
     , m_info(resultData.databaseInfo())
+    , m_databaseConnectionIdentifier(resultData.databaseConnectionIdentifier())
 {
     suspendIfNeeded();
     relaxAdoptionRequirement();
-    m_connection->registerDatabaseConnection(*this);
+    m_serverConnection->registerDatabaseConnection(*this);
 }
 
 IDBDatabase::~IDBDatabase()
 {
-    m_connection->unregisterDatabaseConnection(*this);
+    m_serverConnection->unregisterDatabaseConnection(*this);
 }
 
 const String IDBDatabase::name() const
@@ -104,7 +105,23 @@ void IDBDatabase::deleteObjectStore(const String&, ExceptionCode&)
 
 void IDBDatabase::close()
 {
-    ASSERT_NOT_REACHED();
+    m_closePending = true;
+    maybeCloseInServer();
+}
+
+void IDBDatabase::maybeCloseInServer()
+{
+    if (m_closedInServer)
+        return;
+
+    // 3.3.9 Database closing steps
+    // Wait for all transactions created using this connection to complete.
+    // Once they are complete, this connection is closed.
+    if (!m_activeTransactions.isEmpty() || !m_committingTransactions.isEmpty())
+        return;
+
+    m_closedInServer = true;
+    m_serverConnection->databaseConnectionClosed(*this);
 }
 
 const char* IDBDatabase::activeDOMObjectName() const
@@ -142,7 +159,7 @@ void IDBDatabase::commitTransaction(IDBTransaction& transaction)
     ASSERT(refTransaction);
     m_committingTransactions.set(transaction.info().identifier(), WTF::move(refTransaction));
 
-    m_connection->commitTransaction(transaction);
+    m_serverConnection->commitTransaction(transaction);
 }
 
 void IDBDatabase::didCommitTransaction(IDBTransaction& transaction)
index fe31c2f..5ff67ac 100644 (file)
@@ -70,6 +70,7 @@ public:
     virtual bool canSuspendForPageCache() const override final;
 
     const IDBDatabaseInfo& info() const { return m_info; }
+    uint64_t databaseConnectionIdentifier() const { return m_databaseConnectionIdentifier; }
 
     Ref<IDBTransaction> startVersionChangeTransaction(const IDBTransactionInfo&);
     void commitTransaction(IDBTransaction&);
@@ -80,9 +81,15 @@ private:
     IDBDatabase(ScriptExecutionContext&, IDBConnectionToServer&, const IDBResultData&);
 
     void didCommitOrAbortTransaction(IDBTransaction&);
-    
-    Ref<IDBConnectionToServer> m_connection;
+
+    void maybeCloseInServer();
+
+    Ref<IDBConnectionToServer> m_serverConnection;
     IDBDatabaseInfo m_info;
+    uint64_t m_databaseConnectionIdentifier { 0 };
+
+    bool m_closePending { false };
+    bool m_closedInServer { false };
 
     RefPtr<IDBTransaction> m_versionChangeTransaction;
     HashMap<IDBResourceIdentifier, RefPtr<IDBTransaction>> m_activeTransactions;
index 664e082..4a9369c 100644 (file)
@@ -44,7 +44,7 @@ IDBRequest::IDBRequest(IDBConnectionToServer& connection, ScriptExecutionContext
 
 RefPtr<WebCore::IDBAny> IDBRequest::result(ExceptionCode&) const
 {
-    return nullptr;
+    return m_result;
 }
 
 unsigned short IDBRequest::errorCode(ExceptionCode&) const
index 6e91285..45e16fe 100644 (file)
@@ -156,6 +156,17 @@ void IDBServer::commitTransaction(const IDBResourceIdentifier& transactionIdenti
     transaction->commit();
 }
 
+void IDBServer::databaseConnectionClosed(uint64_t databaseConnectionIdentifier)
+{
+    LOG(IndexedDB, "IDBServer::databaseConnectionClosed");
+
+    auto databaseConnection = m_databaseConnections.get(databaseConnectionIdentifier);
+    if (!databaseConnection)
+        return;
+
+    databaseConnection->connectionClosedFromClient();
+}
+
 void IDBServer::postDatabaseTask(std::unique_ptr<CrossThreadTask>&& task)
 {
     ASSERT(isMainThread());
index 00d71ae..f07e87d 100644 (file)
@@ -58,6 +58,7 @@ public:
     void openDatabase(const IDBRequestData&);
     void deleteDatabase(const IDBRequestData&);
     void commitTransaction(const IDBResourceIdentifier&);
+    void databaseConnectionClosed(uint64_t databaseConnectionIdentifier);
 
     void postDatabaseTask(std::unique_ptr<CrossThreadTask>&&);
     void postDatabaseTaskReply(std::unique_ptr<CrossThreadTask>&&);
index 9ff1c8e..a6c2f3d 100644 (file)
@@ -92,7 +92,6 @@ void UniqueIDBDatabase::handleOpenDatabaseOperations()
 
     Ref<UniqueIDBDatabaseConnection> connection = UniqueIDBDatabaseConnection::create(*this, operation->connection());
     UniqueIDBDatabaseConnection* rawConnection = &connection.get();
-    m_server.registerDatabaseConnection(*rawConnection);
 
     if (requestedVersion == m_databaseInfo->version()) {
         addOpenDatabaseConnection(WTF::move(connection));
@@ -251,6 +250,26 @@ void UniqueIDBDatabase::transactionDestroyed(UniqueIDBDatabaseTransaction& trans
         m_versionChangeTransaction = nullptr;
 }
 
+void UniqueIDBDatabase::connectionClosedFromClient(UniqueIDBDatabaseConnection& connection)
+{
+    ASSERT(isMainThread());
+    LOG(IndexedDB, "(main) UniqueIDBDatabase::connectionClosedFromClient");
+
+    if (m_versionChangeDatabaseConnection == &connection)
+        m_versionChangeDatabaseConnection = nullptr;
+
+    ASSERT(m_openDatabaseConnections.contains(&connection));
+
+    auto removedConnection = m_openDatabaseConnections.take(&connection);
+    if (removedConnection->hasNonFinishedTransactions()) {
+        m_closePendingDatabaseConnections.add(WTF::move(removedConnection));
+        return;
+    }
+
+    // FIXME: Now that a database connection has closed, previously blocked transactions might be runnable.
+    // Try to run them now.
+}
+
 void UniqueIDBDatabase::performErrorCallback(uint64_t callbackIdentifier, const IDBError& error)
 {
     auto callback = m_errorCallbacks.take(callbackIdentifier);
index ce68452..4dcf308 100644 (file)
@@ -65,6 +65,7 @@ public:
 
     void commitTransaction(UniqueIDBDatabaseTransaction&, ErrorCallback);
     void transactionDestroyed(UniqueIDBDatabaseTransaction&);
+    void connectionClosedFromClient(UniqueIDBDatabaseConnection&);
 
 private:
     UniqueIDBDatabase(IDBServer&, const IDBDatabaseIdentifier&);
@@ -94,6 +95,7 @@ private:
     Deque<Ref<IDBServerOperation>> m_pendingOpenDatabaseOperations;
 
     HashSet<RefPtr<UniqueIDBDatabaseConnection>> m_openDatabaseConnections;
+    HashSet<RefPtr<UniqueIDBDatabaseConnection>> m_closePendingDatabaseConnections;
 
     RefPtr<IDBServerOperation> m_versionChangeOperation;
     RefPtr<UniqueIDBDatabaseConnection> m_versionChangeDatabaseConnection;
index d7c4ac2..2b0bbc6 100644 (file)
 #if ENABLE(INDEXED_DATABASE)
 
 #include "IDBConnectionToClient.h"
+#include "IDBServer.h"
 #include "IDBTransactionInfo.h"
 #include "Logging.h"
+#include "UniqueIDBDatabase.h"
 
 namespace WebCore {
 namespace IDBServer {
@@ -51,6 +53,25 @@ UniqueIDBDatabaseConnection::UniqueIDBDatabaseConnection(UniqueIDBDatabase& data
     , m_database(database)
     , m_connectionToClient(connection)
 {
+    m_database.server().registerDatabaseConnection(*this);
+}
+
+UniqueIDBDatabaseConnection::~UniqueIDBDatabaseConnection()
+{
+    m_database.server().unregisterDatabaseConnection(*this);
+}
+
+bool UniqueIDBDatabaseConnection::hasNonFinishedTransactions() const
+{
+    return !m_transactionMap.isEmpty();
+}
+
+void UniqueIDBDatabaseConnection::connectionClosedFromClient()
+{
+    LOG(IndexedDB, "UniqueIDBDatabaseConnection::connectionClosedFromClient");
+
+    m_closePending = true;
+    m_database.connectionClosedFromClient(*this);
 }
 
 void UniqueIDBDatabaseConnection::fireVersionChangeEvent(uint64_t requestedVersion)
index 59850a8..6ee8e7f 100644 (file)
@@ -47,12 +47,18 @@ class UniqueIDBDatabaseConnection : public RefCounted<UniqueIDBDatabaseConnectio
 public:
     static Ref<UniqueIDBDatabaseConnection> create(UniqueIDBDatabase&, IDBConnectionToClient&);
 
+    ~UniqueIDBDatabaseConnection();
+
     uint64_t identifier() const { return m_identifier; }
     UniqueIDBDatabase& database() { return m_database; }
     IDBConnectionToClient& connectionToClient() { return m_connectionToClient; }
 
+    void connectionClosedFromClient();
+
     bool closePending() const { return m_closePending; }
 
+    bool hasNonFinishedTransactions() const;
+
     void fireVersionChangeEvent(uint64_t requestedVersion);
     UniqueIDBDatabaseTransaction& createVersionChangeTransaction(uint64_t newVersion);
 
index 080a186..7c2f9bf 100644 (file)
@@ -127,6 +127,14 @@ void InProcessIDBServer::fireVersionChangeEvent(IDBServer::UniqueIDBDatabaseConn
     });
 }
 
+void InProcessIDBServer::databaseConnectionClosed(uint64_t databaseConnectionIdentifier)
+{
+    RefPtr<InProcessIDBServer> self(this);
+    RunLoop::current().dispatch([this, self, databaseConnectionIdentifier] {
+        m_server->databaseConnectionClosed(databaseConnectionIdentifier);
+    });
+}
+
 } // namespace WebCore
 
 #endif // ENABLE(INDEXED_DATABASE)
index 0f1c393..dc09451 100644 (file)
@@ -57,6 +57,7 @@ public:
     virtual void deleteDatabase(IDBRequestData&) override final;
     virtual void openDatabase(IDBRequestData&) override final;
     virtual void commitTransaction(IDBResourceIdentifier&) override final;
+    virtual void databaseConnectionClosed(uint64_t databaseConnectionIdentifier) override final;
 
     // IDBConnectionToClient
     virtual uint64_t identifier() const override;