[WK2] Use C++ lambdas in IPC::Connection
authorzandobersek@gmail.com <zandobersek@gmail.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 17 Mar 2015 10:04:34 +0000 (10:04 +0000)
committerzandobersek@gmail.com <zandobersek@gmail.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 17 Mar 2015 10:04:34 +0000 (10:04 +0000)
https://bugs.webkit.org/show_bug.cgi?id=138018

Reviewed by Anders Carlsson.

Replace uses of WTF::bind() in the IPC::Connection class with C++ lambdas.

* Platform/IPC/Connection.cpp:
(IPC::Connection::dispatchWorkQueueMessageReceiverMessage):
(IPC::Connection::invalidate):
(IPC::Connection::sendMessage):
(IPC::Connection::processIncomingMessage): Simplify the error messages so we
don't have to format strings on-the-fly, removing the issues of cross-thread
string copying altogether.
(IPC::Connection::dispatchDidReceiveInvalidMessage): The parameters are now
of the StringReference type.
(IPC::Connection::enqueueIncomingMessage):
* Platform/IPC/Connection.h:
* Platform/IPC/mac/ConnectionMac.mm:
(IPC::Connection::receiveSourceEventHandler):
* Platform/IPC/unix/ConnectionUnix.cpp:
(IPC::Connection::open):

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

Source/WebKit2/ChangeLog
Source/WebKit2/Platform/IPC/Connection.cpp
Source/WebKit2/Platform/IPC/Connection.h
Source/WebKit2/Platform/IPC/mac/ConnectionMac.mm
Source/WebKit2/Platform/IPC/unix/ConnectionUnix.cpp

index c72bfe9..9eaa675 100644 (file)
@@ -1,5 +1,30 @@
 2015-03-17  Zan Dobersek  <zdobersek@igalia.com>
 
+        [WK2] Use C++ lambdas in IPC::Connection
+        https://bugs.webkit.org/show_bug.cgi?id=138018
+
+        Reviewed by Anders Carlsson.
+
+        Replace uses of WTF::bind() in the IPC::Connection class with C++ lambdas.
+
+        * Platform/IPC/Connection.cpp:
+        (IPC::Connection::dispatchWorkQueueMessageReceiverMessage):
+        (IPC::Connection::invalidate):
+        (IPC::Connection::sendMessage):
+        (IPC::Connection::processIncomingMessage): Simplify the error messages so we
+        don't have to format strings on-the-fly, removing the issues of cross-thread
+        string copying altogether.
+        (IPC::Connection::dispatchDidReceiveInvalidMessage): The parameters are now
+        of the StringReference type.
+        (IPC::Connection::enqueueIncomingMessage):
+        * Platform/IPC/Connection.h:
+        * Platform/IPC/mac/ConnectionMac.mm:
+        (IPC::Connection::receiveSourceEventHandler):
+        * Platform/IPC/unix/ConnectionUnix.cpp:
+        (IPC::Connection::open):
+
+2015-03-17  Zan Dobersek  <zdobersek@igalia.com>
+
         [CMake] Use a forwarding header for ANGLE's ShaderLang.h to avoid picking up ANGLE's EGL headers
         https://bugs.webkit.org/show_bug.cgi?id=142530
 
index e210fe8..57b4c57 100644 (file)
@@ -300,34 +300,32 @@ void Connection::removeWorkQueueMessageReceiver(StringReference messageReceiverN
     });
 }
 
-void Connection::dispatchWorkQueueMessageReceiverMessage(WorkQueueMessageReceiver* workQueueMessageReceiver, MessageDecoder* incomingMessageDecoder)
+void Connection::dispatchWorkQueueMessageReceiverMessage(WorkQueueMessageReceiver& workQueueMessageReceiver, MessageDecoder& decoder)
 {
-    std::unique_ptr<MessageDecoder> decoder(incomingMessageDecoder);
-
-    if (!decoder->isSyncMessage()) {
-        workQueueMessageReceiver->didReceiveMessage(*this, *decoder);
+    if (!decoder.isSyncMessage()) {
+        workQueueMessageReceiver.didReceiveMessage(*this, decoder);
         return;
     }
 
     uint64_t syncRequestID = 0;
-    if (!decoder->decode(syncRequestID) || !syncRequestID) {
+    if (!decoder.decode(syncRequestID) || !syncRequestID) {
         // We received an invalid sync message.
         // FIXME: Handle this.
-        decoder->markInvalid();
+        decoder.markInvalid();
         return;
     }
 
 #if HAVE(DTRACE)
-    auto replyEncoder = std::make_unique<MessageEncoder>("IPC", "SyncMessageReply", syncRequestID, incomingMessageDecoder->UUID());
+    auto replyEncoder = std::make_unique<MessageEncoder>("IPC", "SyncMessageReply", syncRequestID, decoder.UUID());
 #else
     auto replyEncoder = std::make_unique<MessageEncoder>("IPC", "SyncMessageReply", syncRequestID);
 #endif
 
     // Hand off both the decoder and encoder to the work queue message receiver.
-    workQueueMessageReceiver->didReceiveSyncMessage(*this, *decoder, replyEncoder);
+    workQueueMessageReceiver.didReceiveSyncMessage(*this, decoder, replyEncoder);
 
     // FIXME: If the message was invalid, we should send back a SyncMessageError.
-    ASSERT(!decoder->isInvalid());
+    ASSERT(!decoder.isInvalid());
 
     if (replyEncoder)
         sendSyncReply(WTF::move(replyEncoder));
@@ -347,10 +345,12 @@ void Connection::invalidate()
         return;
     }
     
-    // Reset the client.
-    m_client = 0;
+    m_client = nullptr;
 
-    m_connectionQueue->dispatch(WTF::bind(&Connection::platformInvalidate, this));
+    RefPtr<Connection> protectedThis(this);
+    m_connectionQueue->dispatch([protectedThis] {
+        protectedThis->platformInvalidate();
+    });
 }
 
 void Connection::markCurrentlyDispatchedMessageAsInvalid()
@@ -397,7 +397,10 @@ bool Connection::sendMessage(std::unique_ptr<MessageEncoder> encoder, unsigned m
     }
     
     // FIXME: We should add a boolean flag so we don't call this when work has already been scheduled.
-    m_connectionQueue->dispatch(WTF::bind(&Connection::sendOutgoingMessages, this));
+    RefPtr<Connection> protectedThis(this);
+    m_connectionQueue->dispatch([protectedThis] {
+        protectedThis->sendOutgoingMessages();
+    });
     return true;
 }
 
@@ -653,23 +656,27 @@ void Connection::processIncomingMessage(std::unique_ptr<MessageDecoder> message)
     }
 
     if (!m_workQueueMessageReceivers.isValidKey(message->messageReceiverName())) {
-        if (message->messageReceiverName().isEmpty() && message->messageName().isEmpty()) {
-            // Something went wrong when decoding the message. Encode the message length so we can figure out if this
-            // happens for certain message lengths.
-            CString messageReceiverName = "<unknown message>";
-            CString messageName = String::format("<message length: %zu bytes>", message->length()).utf8();
-
-            m_clientRunLoop.dispatch(bind(&Connection::dispatchDidReceiveInvalidMessage, this, messageReceiverName, messageName));
-            return;
-        }
-
-        m_clientRunLoop.dispatch(bind(&Connection::dispatchDidReceiveInvalidMessage, this, message->messageReceiverName().toString(), message->messageName().toString()));
+        RefPtr<Connection> protectedThis(this);
+        StringReference messageReceiverName = message->messageReceiverName();
+        StringCapture capturedMessageReceiverName(messageReceiverName.isEmpty() ? "<unknown message receiver>" : String(messageReceiverName.data(), messageReceiverName.size()));
+        StringReference messageName = message->messageName();
+        StringCapture capturedMessageName(messageName.isEmpty() ? "<unknown message>" : String(messageName.data(), messageName.size()));
+
+        m_clientRunLoop.dispatch([protectedThis, capturedMessageReceiverName, capturedMessageName] {
+            protectedThis->dispatchDidReceiveInvalidMessage(capturedMessageReceiverName.string().utf8(), capturedMessageName.string().utf8());
+        });
         return;
     }
 
     auto it = m_workQueueMessageReceivers.find(message->messageReceiverName());
     if (it != m_workQueueMessageReceivers.end()) {
-        it->value.first->dispatch(bind(&Connection::dispatchWorkQueueMessageReceiverMessage, this, it->value.second, message.release()));
+        RefPtr<Connection> protectedThis(this);
+        RefPtr<WorkQueueMessageReceiver>& workQueueMessageReceiver = it->value.second;
+        MessageDecoder* decoderPtr = message.release();
+        it->value.first->dispatch([protectedThis, workQueueMessageReceiver, decoderPtr] {
+            std::unique_ptr<MessageDecoder> decoder(decoderPtr);
+            protectedThis->dispatchWorkQueueMessageReceiverMessage(*workQueueMessageReceiver, *decoder);
+        });
         return;
     }
 
@@ -829,7 +836,10 @@ void Connection::enqueueIncomingMessage(std::unique_ptr<MessageDecoder> incoming
         m_incomingMessages.append(WTF::move(incomingMessage));
     }
 
-    m_clientRunLoop.dispatch(WTF::bind(&Connection::dispatchOneMessage, this));
+    RefPtr<Connection> protectedThis(this);
+    m_clientRunLoop.dispatch([protectedThis] {
+        protectedThis->dispatchOneMessage();
+    });
 }
 
 void Connection::dispatchMessage(MessageDecoder& decoder)
index 132198b..20bc738 100644 (file)
@@ -213,7 +213,7 @@ private:
     void processIncomingMessage(std::unique_ptr<MessageDecoder>);
     void processIncomingSyncReply(std::unique_ptr<MessageDecoder>);
 
-    void dispatchWorkQueueMessageReceiverMessage(WorkQueueMessageReceiver*, MessageDecoder*);
+    void dispatchWorkQueueMessageReceiverMessage(WorkQueueMessageReceiver&, MessageDecoder&);
 
     bool canSendOutgoingMessages() const;
     bool platformCanSendOutgoingMessages() const;
index 25e2821..4b7e6ee 100644 (file)
@@ -510,7 +510,14 @@ void Connection::receiveSourceEventHandler()
     if (decoder->messageReceiverName() == "IPC" && decoder->messageName() == "SetExceptionPort") {
         if (m_isServer) {
             // Server connections aren't supposed to have their exception ports overriden. Treat this as an invalid message.
-            m_clientRunLoop.dispatch(bind(&Connection::dispatchDidReceiveInvalidMessage, this, decoder->messageReceiverName().toString(), decoder->messageName().toString()));
+            RefPtr<Connection> protectedThis(this);
+            StringReference messageReceiverName = decoder->messageReceiverName();
+            StringCapture capturedMessageReceiverName(String(messageReceiverName.data(), messageReceiverName.size()));
+            StringReference messageName = decoder->messageName();
+            StringCapture capturedMessageName(String(messageName.data(), messageName.size()));
+            m_clientRunLoop.dispatch([protectedThis, capturedMessageReceiverName, capturedMessageName] {
+                protectedThis->dispatchDidReceiveInvalidMessage(capturedMessageReceiverName.string().utf8(), capturedMessageName.string().utf8());
+            });
             return;
         }
         MachPort exceptionPort;
index 16be014..4dd95fd 100644 (file)
@@ -388,27 +388,27 @@ bool Connection::open()
         }
     }
 
+    RefPtr<Connection> protectedThis(this);
     m_isConnected = true;
 #if PLATFORM(GTK)
-    RefPtr<Connection> protector(this);
     m_connectionQueue->registerSocketEventHandler(m_socketDescriptor,
-        [=] {
-            protector->readyReadHandler();
+        [protectedThis] {
+            protectedThis->readyReadHandler();
         },
-        [=] {
-            protector->connectionDidClose();
+        [protectedThis] {
+            protectedThis->connectionDidClose();
         });
 #elif PLATFORM(EFL)
-    RefPtr<Connection> protector(this);
     m_connectionQueue->registerSocketEventHandler(m_socketDescriptor,
-        [protector] {
-            protector->readyReadHandler();
+        [protectedThis] {
+            protectedThis->readyReadHandler();
         });
 #endif
 
-    // Schedule a call to readyReadHandler. Data may have arrived before installation of the signal
-    // handler.
-    m_connectionQueue->dispatch(WTF::bind(&Connection::readyReadHandler, this));
+    // Schedule a call to readyReadHandler. Data may have arrived before installation of the signal handler.
+    m_connectionQueue->dispatch([protectedThis] {
+        protectedThis->readyReadHandler();
+    });
 
     return true;
 }