Make MessagePortChannel::takeAllMessagesFromRemote asynchronous.
authorbeidson@apple.com <beidson@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 3 Jan 2018 00:51:01 +0000 (00:51 +0000)
committerbeidson@apple.com <beidson@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 3 Jan 2018 00:51:01 +0000 (00:51 +0000)
https://bugs.webkit.org/show_bug.cgi?id=181205

Reviewed by Alex Christensen.

No new tests (No behavior change)

This is needed for the ongoing WK2 MessagePort work.

For WK1 in-process MessagePorts it is still synchronous; no behavior change.

* dom/InProcessMessagePortChannel.cpp:
(WebCore::InProcessMessagePortChannel::takeAllMessagesFromRemote):
* dom/InProcessMessagePortChannel.h:

* dom/MessagePort.cpp:
(WebCore::MessagePort::dispatchMessages):
* dom/MessagePortChannel.h:

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

Source/WebCore/ChangeLog
Source/WebCore/dom/InProcessMessagePortChannel.cpp
Source/WebCore/dom/InProcessMessagePortChannel.h
Source/WebCore/dom/MessagePort.cpp
Source/WebCore/dom/MessagePortChannel.h

index 8a69657..d3136e8 100644 (file)
@@ -1,3 +1,24 @@
+2018-01-02  Brady Eidson  <beidson@apple.com>
+
+        Make MessagePortChannel::takeAllMessagesFromRemote asynchronous.
+        https://bugs.webkit.org/show_bug.cgi?id=181205
+
+        Reviewed by Alex Christensen.
+
+        No new tests (No behavior change)
+
+        This is needed for the ongoing WK2 MessagePort work.
+
+        For WK1 in-process MessagePorts it is still synchronous; no behavior change.
+
+        * dom/InProcessMessagePortChannel.cpp:
+        (WebCore::InProcessMessagePortChannel::takeAllMessagesFromRemote):
+        * dom/InProcessMessagePortChannel.h:
+
+        * dom/MessagePort.cpp:
+        (WebCore::MessagePort::dispatchMessages):
+        * dom/MessagePortChannel.h:
+
 2018-01-02  Jiewen Tan  <jiewen_tan@apple.com>
 
         Add a WebAuthentication runtime feature flag
index ccf28d8..2467f94 100644 (file)
@@ -27,6 +27,7 @@
 #include "InProcessMessagePortChannel.h"
 
 #include "MessagePort.h"
+#include <wtf/CompletionHandler.h>
 #include <wtf/Locker.h>
 
 namespace WebCore {
@@ -75,10 +76,15 @@ void InProcessMessagePortChannel::postMessageToRemote(Ref<SerializedScriptValue>
         m_remotePort->messageAvailable();
 }
 
-Deque<std::unique_ptr<MessagePortChannel::EventData>> InProcessMessagePortChannel::takeAllMessagesFromRemote()
+void InProcessMessagePortChannel::takeAllMessagesFromRemote(CompletionHandler<void(Deque<std::unique_ptr<EventData>>&&)>&& callback)
 {
-    Locker<Lock> locker(m_lock);
-    return m_incomingQueue->takeAllMessages();
+    Deque<std::unique_ptr<EventData>> messages;
+    {
+        Locker<Lock> locker(m_lock);
+        messages = m_incomingQueue->takeAllMessages();
+    }
+
+    callback(WTFMove(messages));
 }
 
 bool InProcessMessagePortChannel::isConnectedTo(const MessagePortIdentifier& identifier)
index fc35b69..15b01cf 100644 (file)
@@ -37,7 +37,7 @@ public:
     ~InProcessMessagePortChannel() final;
 
     void postMessageToRemote(Ref<SerializedScriptValue>&&, std::unique_ptr<MessagePortChannelArray>&&) final;
-    Deque<std::unique_ptr<EventData>> takeAllMessagesFromRemote() final;
+    void takeAllMessagesFromRemote(CompletionHandler<void(Deque<std::unique_ptr<EventData>>&&)>&&) final;
     bool isConnectedTo(const MessagePortIdentifier&) final;
     bool entangleWithRemoteIfOpen(const MessagePortIdentifier&) final;
     void disentangle() final;
index 5657d75..b3c3b6b 100644 (file)
@@ -203,17 +203,19 @@ void MessagePort::dispatchMessages()
     if (!m_entangledChannel)
         return;
 
-    bool contextIsWorker = is<WorkerGlobalScope>(*m_scriptExecutionContext);
-
-    auto pendingMessages = m_entangledChannel->takeAllMessagesFromRemote();
-    for (auto& message : pendingMessages) {
-        // close() in Worker onmessage handler should prevent next message from dispatching.
-        if (contextIsWorker && downcast<WorkerGlobalScope>(*m_scriptExecutionContext).isClosing())
+    m_entangledChannel->takeAllMessagesFromRemote([this, protectedThis = makeRef(*this)](Deque<std::unique_ptr<MessagePortChannel::EventData>>&& messages) {
+        if (!m_scriptExecutionContext)
             return;
 
-        auto ports = MessagePort::entanglePorts(*m_scriptExecutionContext, WTFMove(message->channels));
-        dispatchEvent(MessageEvent::create(WTFMove(ports), WTFMove(message->message)));
-    }
+        bool contextIsWorker = is<WorkerGlobalScope>(*m_scriptExecutionContext);
+        for (auto& message : messages) {
+            // close() in Worker onmessage handler should prevent next message from dispatching.
+            if (contextIsWorker && downcast<WorkerGlobalScope>(*m_scriptExecutionContext).isClosing())
+                return;
+            auto ports = MessagePort::entanglePorts(*m_scriptExecutionContext, WTFMove(message->channels));
+            dispatchEvent(MessageEvent::create(WTFMove(ports), WTFMove(message->message)));
+        }
+    });
 }
 
 bool MessagePort::hasPendingActivity() const
index cdf03f9..08fa0cb 100644 (file)
@@ -67,7 +67,7 @@ public:
     static void createChannelBetweenPorts(MessagePort&, MessagePort&);
 
     virtual void postMessageToRemote(Ref<SerializedScriptValue>&&, std::unique_ptr<MessagePortChannelArray>&&) = 0;
-    virtual Deque<std::unique_ptr<EventData>> takeAllMessagesFromRemote() = 0;
+    virtual void takeAllMessagesFromRemote(CompletionHandler<void(Deque<std::unique_ptr<EventData>>&&)>&&) = 0;
     virtual bool isConnectedTo(const MessagePortIdentifier&) = 0;
     virtual bool entangleWithRemoteIfOpen(const MessagePortIdentifier&) = 0;
     virtual void disentangle() = 0;