Move SQLTransactionBackend construction inside SQLTransaction
authorandersca@apple.com <andersca@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 10 Aug 2016 16:47:12 +0000 (16:47 +0000)
committerandersca@apple.com <andersca@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 10 Aug 2016 16:47:12 +0000 (16:47 +0000)
https://bugs.webkit.org/show_bug.cgi?id=160726

Reviewed by Tim Horton.

This is the first step towards re-merging SQLTransactionBackend and SQLTransaction and clean up the code.

* Modules/webdatabase/ChangeVersionWrapper.cpp:
* Modules/webdatabase/ChangeVersionWrapper.h:
* Modules/webdatabase/Database.cpp:
(WebCore::Database::performClose):
(WebCore::Database::scheduleTransactionStep):
(WebCore::Database::changeVersion):
(WebCore::Database::transaction):
(WebCore::Database::readTransaction):
(WebCore::Database::runTransaction):
* Modules/webdatabase/Database.h:
* Modules/webdatabase/DatabaseTask.cpp:
* Modules/webdatabase/SQLTransaction.cpp:
(WebCore::SQLTransaction::create):
(WebCore::SQLTransaction::SQLTransaction):
* Modules/webdatabase/SQLTransaction.h:
(WebCore::SQLTransactionWrapper::~SQLTransactionWrapper):
(WebCore::SQLTransaction::backend):
* Modules/webdatabase/SQLTransactionBackend.cpp:
(WebCore::SQLTransactionBackend::lockAcquired):
(WebCore::SQLTransactionBackend::requestTransitToState):
* Modules/webdatabase/SQLTransactionBackend.h:
(WebCore::SQLTransactionWrapper::~SQLTransactionWrapper): Deleted.

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

Source/WebCore/ChangeLog
Source/WebCore/Modules/webdatabase/ChangeVersionWrapper.cpp
Source/WebCore/Modules/webdatabase/ChangeVersionWrapper.h
Source/WebCore/Modules/webdatabase/Database.cpp
Source/WebCore/Modules/webdatabase/Database.h
Source/WebCore/Modules/webdatabase/DatabaseTask.cpp
Source/WebCore/Modules/webdatabase/SQLTransaction.cpp
Source/WebCore/Modules/webdatabase/SQLTransaction.h
Source/WebCore/Modules/webdatabase/SQLTransactionBackend.cpp
Source/WebCore/Modules/webdatabase/SQLTransactionBackend.h

index 2a2bac6..7db90df 100644 (file)
@@ -1,3 +1,35 @@
+2016-08-09  Anders Carlsson  <andersca@apple.com>
+
+        Move SQLTransactionBackend construction inside SQLTransaction
+        https://bugs.webkit.org/show_bug.cgi?id=160726
+
+        Reviewed by Tim Horton.
+
+        This is the first step towards re-merging SQLTransactionBackend and SQLTransaction and clean up the code.
+
+        * Modules/webdatabase/ChangeVersionWrapper.cpp:
+        * Modules/webdatabase/ChangeVersionWrapper.h:
+        * Modules/webdatabase/Database.cpp:
+        (WebCore::Database::performClose):
+        (WebCore::Database::scheduleTransactionStep):
+        (WebCore::Database::changeVersion):
+        (WebCore::Database::transaction):
+        (WebCore::Database::readTransaction):
+        (WebCore::Database::runTransaction):
+        * Modules/webdatabase/Database.h:
+        * Modules/webdatabase/DatabaseTask.cpp:
+        * Modules/webdatabase/SQLTransaction.cpp:
+        (WebCore::SQLTransaction::create):
+        (WebCore::SQLTransaction::SQLTransaction):
+        * Modules/webdatabase/SQLTransaction.h:
+        (WebCore::SQLTransactionWrapper::~SQLTransactionWrapper):
+        (WebCore::SQLTransaction::backend):
+        * Modules/webdatabase/SQLTransactionBackend.cpp:
+        (WebCore::SQLTransactionBackend::lockAcquired):
+        (WebCore::SQLTransactionBackend::requestTransitToState):
+        * Modules/webdatabase/SQLTransactionBackend.h:
+        (WebCore::SQLTransactionWrapper::~SQLTransactionWrapper): Deleted.
+
 2016-08-10  Chris Dumez  <cdumez@apple.com>
 
         It should be possible to re-initialize a MutationEvent after it's been dispatched
index 515ec28..32d02e9 100644 (file)
@@ -30,6 +30,7 @@
 
 #include "Database.h"
 #include "SQLError.h"
+#include "SQLTransactionBackend.h"
 #include <wtf/RefPtr.h>
 
 namespace WebCore {
index eec31f2..2cce7a4 100644 (file)
@@ -28,7 +28,7 @@
 #ifndef ChangeVersionWrapper_h
 #define ChangeVersionWrapper_h
 
-#include "SQLTransactionBackend.h"
+#include "SQLTransaction.h"
 #include <wtf/Forward.h>
 
 namespace WebCore {
index b8103f1..ee9c2da 100644 (file)
@@ -312,9 +312,8 @@ void Database::performClose()
         // Clean up transactions that have not been scheduled yet:
         // Transaction phase 1 cleanup. See comment on "What happens if a
         // transaction is interrupted?" at the top of SQLTransactionBackend.cpp.
-        RefPtr<SQLTransactionBackend> transaction;
         while (!m_transactionQueue.isEmpty()) {
-            transaction = m_transactionQueue.takeFirst();
+            auto transaction = m_transactionQueue.takeFirst();
             transaction->notifyDatabaseThreadIsShuttingDown();
         }
 
@@ -576,30 +575,12 @@ void Database::scheduleTransaction()
         m_transactionInProgress = false;
 }
 
-RefPtr<SQLTransactionBackend> Database::runTransaction(Ref<SQLTransaction>&& transaction, bool readOnly, const ChangeVersionData* data)
-{
-    LockHolder locker(m_transactionInProgressMutex);
-    if (!m_isTransactionQueueEnabled)
-        return nullptr;
-
-    RefPtr<SQLTransactionWrapper> wrapper;
-    if (data)
-        wrapper = ChangeVersionWrapper::create(data->oldVersion(), data->newVersion());
-
-    RefPtr<SQLTransactionBackend> transactionBackend = SQLTransactionBackend::create(this, WTFMove(transaction), WTFMove(wrapper), readOnly);
-    m_transactionQueue.append(transactionBackend);
-    if (!m_transactionInProgress)
-        scheduleTransaction();
-
-    return transactionBackend;
-}
-
-void Database::scheduleTransactionStep(SQLTransactionBackend* transaction)
+void Database::scheduleTransactionStep(SQLTransactionBackend& transaction)
 {
     if (!databaseContext()->databaseThread())
         return;
 
-    auto task = std::make_unique<DatabaseTransactionTask>(transaction);
+    auto task = std::make_unique<DatabaseTransactionTask>(&transaction);
     LOG(StorageAPI, "Scheduling DatabaseTransactionTask %p for the transaction step\n", task.get());
     databaseContext()->databaseThread()->scheduleTask(WTFMove(task));
 }
@@ -651,18 +632,17 @@ void Database::markAsDeletedAndClose()
 
 void Database::changeVersion(const String& oldVersion, const String& newVersion, RefPtr<SQLTransactionCallback>&& callback, RefPtr<SQLTransactionErrorCallback>&& errorCallback, RefPtr<VoidCallback>&& successCallback)
 {
-    ChangeVersionData data(oldVersion, newVersion);
-    runTransaction(WTFMove(callback), WTFMove(errorCallback), WTFMove(successCallback), false, &data);
+    runTransaction(WTFMove(callback), WTFMove(errorCallback), WTFMove(successCallback), ChangeVersionWrapper::create(oldVersion, newVersion), false);
 }
 
 void Database::transaction(RefPtr<SQLTransactionCallback>&& callback, RefPtr<SQLTransactionErrorCallback>&& errorCallback, RefPtr<VoidCallback>&& successCallback)
 {
-    runTransaction(WTFMove(callback), WTFMove(errorCallback), WTFMove(successCallback), false);
+    runTransaction(WTFMove(callback), WTFMove(errorCallback), WTFMove(successCallback), nullptr, false);
 }
 
 void Database::readTransaction(RefPtr<SQLTransactionCallback>&& callback, RefPtr<SQLTransactionErrorCallback>&& errorCallback, RefPtr<VoidCallback>&& successCallback)
 {
-    runTransaction(WTFMove(callback), WTFMove(errorCallback), WTFMove(successCallback), true);
+    runTransaction(WTFMove(callback), WTFMove(errorCallback), WTFMove(successCallback), nullptr, true);
 }
 
 String Database::stringIdentifier() const
@@ -742,17 +722,23 @@ void Database::resetAuthorizer()
         m_databaseAuthorizer->reset();
 }
 
-void Database::runTransaction(RefPtr<SQLTransactionCallback>&& callback, RefPtr<SQLTransactionErrorCallback>&& errorCallback, RefPtr<VoidCallback>&& successCallback, bool readOnly, const ChangeVersionData* changeVersionData)
+void Database::runTransaction(RefPtr<SQLTransactionCallback>&& callback, RefPtr<SQLTransactionErrorCallback>&& errorCallback, RefPtr<VoidCallback>&& successCallback, RefPtr<SQLTransactionWrapper>&& wrapper, bool readOnly)
 {
-    Ref<SQLTransaction> transaction = SQLTransaction::create(*this, WTFMove(callback), WTFMove(successCallback), errorCallback.copyRef(), readOnly);
-
-    RefPtr<SQLTransactionBackend> transactionBackend = runTransaction(WTFMove(transaction), readOnly, changeVersionData);
-    if (!transactionBackend && errorCallback) {
-        RefPtr<SQLTransactionErrorCallback> errorCallbackProtector = WTFMove(errorCallback);
-        m_scriptExecutionContext->postTask([errorCallbackProtector](ScriptExecutionContext&) {
-            errorCallbackProtector->handleEvent(SQLError::create(SQLError::UNKNOWN_ERR, "database has been closed").ptr());
-        });
+    LockHolder locker(m_transactionInProgressMutex);
+    if (!m_isTransactionQueueEnabled) {
+        if (errorCallback) {
+            RefPtr<SQLTransactionErrorCallback> errorCallbackProtector = WTFMove(errorCallback);
+            m_scriptExecutionContext->postTask([errorCallbackProtector](ScriptExecutionContext&) {
+                errorCallbackProtector->handleEvent(SQLError::create(SQLError::UNKNOWN_ERR, "database has been closed").ptr());
+            });
+        }
+        return;
     }
+
+    auto transaction = SQLTransaction::create(*this, WTFMove(callback), WTFMove(successCallback), errorCallback.copyRef(), WTFMove(wrapper), readOnly);
+    m_transactionQueue.append(&transaction->backend());
+    if (!m_transactionInProgress)
+        scheduleTransaction();
 }
 
 void Database::scheduleTransactionCallback(SQLTransaction* transaction)
index 1e1000f..590c6b3 100644 (file)
@@ -38,7 +38,6 @@
 
 namespace WebCore {
 
-class ChangeVersionData;
 class DatabaseCallback;
 class DatabaseDetails;
 class DatabaseContext;
@@ -50,6 +49,7 @@ class SQLTransactionCallback;
 class SQLTransactionClient;
 class SQLTransactionCoordinator;
 class SQLTransactionErrorCallback;
+class SQLTransactionWrapper;
 class VoidCallback;
 
 class Database final : public ThreadSafeRefCounted<Database> {
@@ -66,8 +66,7 @@ public:
 
     unsigned long long maximumSize() const;
 
-    RefPtr<SQLTransactionBackend> runTransaction(Ref<SQLTransaction>&&, bool readOnly, const ChangeVersionData*);
-    void scheduleTransactionStep(SQLTransactionBackend*);
+    void scheduleTransactionStep(SQLTransactionBackend&);
     void inProgressTransactionCompleted();
 
     bool hasPendingTransaction();
@@ -139,7 +138,7 @@ private:
 
     void scheduleTransaction();
 
-    void runTransaction(RefPtr<SQLTransactionCallback>&&, RefPtr<SQLTransactionErrorCallback>&&, RefPtr<VoidCallback>&& successCallback, bool readOnly, const ChangeVersionData* = nullptr);
+    void runTransaction(RefPtr<SQLTransactionCallback>&&, RefPtr<SQLTransactionErrorCallback>&&, RefPtr<VoidCallback>&& successCallback, RefPtr<SQLTransactionWrapper>&&, bool readOnly);
 
 #if !LOG_DISABLED || !ERROR_DISABLED
     String databaseDebugName() const;
index b871515..e5c42da 100644 (file)
@@ -30,6 +30,7 @@
 
 #include "Database.h"
 #include "Logging.h"
+#include "SQLTransaction.h"
 
 namespace WebCore {
 
index 51fdcde..4b136a5 100644 (file)
 
 namespace WebCore {
 
-Ref<SQLTransaction> SQLTransaction::create(Ref<Database>&& database, RefPtr<SQLTransactionCallback>&& callback, RefPtr<VoidCallback>&& successCallback, RefPtr<SQLTransactionErrorCallback>&& errorCallback, bool readOnly)
+Ref<SQLTransaction> SQLTransaction::create(Ref<Database>&& database, RefPtr<SQLTransactionCallback>&& callback, RefPtr<VoidCallback>&& successCallback, RefPtr<SQLTransactionErrorCallback>&& errorCallback, RefPtr<SQLTransactionWrapper>&& wrapper, bool readOnly)
 {
-    return adoptRef(*new SQLTransaction(WTFMove(database), WTFMove(callback), WTFMove(successCallback), WTFMove(errorCallback), readOnly));
+    return adoptRef(*new SQLTransaction(WTFMove(database), WTFMove(callback), WTFMove(successCallback), WTFMove(errorCallback), WTFMove(wrapper), readOnly));
 }
 
-SQLTransaction::SQLTransaction(Ref<Database>&& database, RefPtr<SQLTransactionCallback>&& callback, RefPtr<VoidCallback>&& successCallback, RefPtr<SQLTransactionErrorCallback>&& errorCallback, bool readOnly)
+SQLTransaction::SQLTransaction(Ref<Database>&& database, RefPtr<SQLTransactionCallback>&& callback, RefPtr<VoidCallback>&& successCallback, RefPtr<SQLTransactionErrorCallback>&& errorCallback, RefPtr<SQLTransactionWrapper>&& wrapper, bool readOnly)
     : m_database(WTFMove(database))
     , m_callbackWrapper(WTFMove(callback), m_database->scriptExecutionContext())
     , m_successCallbackWrapper(WTFMove(successCallback), m_database->scriptExecutionContext())
@@ -61,6 +61,8 @@ SQLTransaction::SQLTransaction(Ref<Database>&& database, RefPtr<SQLTransactionCa
     , m_executeSqlAllowed(false)
     , m_readOnly(readOnly)
 {
+    // This will end up assigning to m_frontend.
+    SQLTransactionBackend::create(m_database.ptr(), this, WTFMove(wrapper), readOnly);
 }
 
 SQLTransaction::~SQLTransaction()
index ddacc8b..b25e8e4 100644 (file)
@@ -47,16 +47,26 @@ class SQLTransactionErrorCallback;
 class SQLValue;
 class VoidCallback;
 
+class SQLTransactionWrapper : public ThreadSafeRefCounted<SQLTransactionWrapper> {
+public:
+    virtual ~SQLTransactionWrapper() { }
+    virtual bool performPreflight(SQLTransactionBackend*) = 0;
+    virtual bool performPostflight(SQLTransactionBackend*) = 0;
+    virtual SQLError* sqlError() const = 0;
+    virtual void handleCommitFailedAfterPostflight(SQLTransactionBackend*) = 0;
+};
+
 class SQLTransaction : public ThreadSafeRefCounted<SQLTransaction>, public SQLTransactionStateMachine<SQLTransaction> {
 public:
-    static Ref<SQLTransaction> create(Ref<Database>&&, RefPtr<SQLTransactionCallback>&&, RefPtr<VoidCallback>&& successCallback, RefPtr<SQLTransactionErrorCallback>&&, bool readOnly);
+    static Ref<SQLTransaction> create(Ref<Database>&&, RefPtr<SQLTransactionCallback>&&, RefPtr<VoidCallback>&& successCallback, RefPtr<SQLTransactionErrorCallback>&&, RefPtr<SQLTransactionWrapper>&&, bool readOnly);
     ~SQLTransaction();
 
-    void performPendingCallback();
-
     void executeSQL(const String& sqlStatement, const Vector<SQLValue>& arguments, RefPtr<SQLStatementCallback>&&, RefPtr<SQLStatementErrorCallback>&&, ExceptionCode&);
 
+    void performPendingCallback();
+
     Database& database() { return m_database; }
+    SQLTransactionBackend& backend() { return *m_backend; }
 
     // APIs called from the backend published via SQLTransaction:
     void requestTransitToState(SQLTransactionState);
@@ -66,7 +76,7 @@ public:
     void setBackend(SQLTransactionBackend*);
 
 private:
-    SQLTransaction(Ref<Database>&&, RefPtr<SQLTransactionCallback>&&, RefPtr<VoidCallback>&& successCallback, RefPtr<SQLTransactionErrorCallback>&&, bool readOnly);
+    SQLTransaction(Ref<Database>&&, RefPtr<SQLTransactionCallback>&&, RefPtr<VoidCallback>&& successCallback, RefPtr<SQLTransactionErrorCallback>&&, RefPtr<SQLTransactionWrapper>&&, bool readOnly);
 
     void clearCallbackWrappers();
 
index 88a1349..a84c83b 100644 (file)
@@ -546,7 +546,7 @@ void SQLTransactionBackend::lockAcquired()
 
     m_requestedState = SQLTransactionState::OpenTransactionAndPreflight;
     ASSERT(m_requestedState != SQLTransactionState::End);
-    m_database->scheduleTransactionStep(this);
+    m_database->scheduleTransactionStep(*this);
 }
 
 void SQLTransactionBackend::openTransactionAndPreflight()
@@ -830,7 +830,7 @@ void SQLTransactionBackend::requestTransitToState(SQLTransactionState nextState)
     LOG(StorageAPI, "Scheduling %s for transaction %p\n", nameForSQLTransactionState(nextState), this);
     m_requestedState = nextState;
     ASSERT(m_requestedState != SQLTransactionState::End);
-    m_database->scheduleTransactionStep(this);
+    m_database->scheduleTransactionStep(*this);
 }
 
 // This state function is used as a stub function to plug unimplemented states
index 5510710..9cdc208 100644 (file)
@@ -44,18 +44,9 @@ class SQLError;
 class SQLiteTransaction;
 class SQLStatement;
 class SQLTransaction;
-class SQLTransactionBackend;
+class SQLTransactionWrapper;
 class SQLValue;
 
-class SQLTransactionWrapper : public ThreadSafeRefCounted<SQLTransactionWrapper> {
-public:
-    virtual ~SQLTransactionWrapper() { }
-    virtual bool performPreflight(SQLTransactionBackend*) = 0;
-    virtual bool performPostflight(SQLTransactionBackend*) = 0;
-    virtual SQLError* sqlError() const = 0;
-    virtual void handleCommitFailedAfterPostflight(SQLTransactionBackend*) = 0;
-};
-
 class SQLTransactionBackend : public ThreadSafeRefCounted<SQLTransactionBackend>, public SQLTransactionStateMachine<SQLTransactionBackend> {
 public:
     static Ref<SQLTransactionBackend> create(Database*, RefPtr<SQLTransaction>&&, RefPtr<SQLTransactionWrapper>&&, bool readOnly);