Make in-process MessagePorts be (mostly) asynchronous
authorbeidson@apple.com <beidson@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 19 Jan 2018 06:33:11 +0000 (06:33 +0000)
committerbeidson@apple.com <beidson@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 19 Jan 2018 06:33:11 +0000 (06:33 +0000)
commitd9117138d6832a5880ed196d9c54067d44b4fe48
tree6a32afd07912fc02bf83cfb3c773ee79803487a4
parent44217dd4c160b76d8249cf455a53b38dc45b1a7b
Make in-process MessagePorts be (mostly) asynchronous
https://bugs.webkit.org/show_bug.cgi?id=181454

Reviewed by Alex Christensen.

No new tests (Covered *brutally* by existing tests)

Part of making MessagePorts be a thing we can pass across processes is making them work async.

The existing "MessagePortChannel" method of abstraction was not cut out for this.
This patch gets rid of MessagePortChannel and adds a new MessagePortChannelProvider abstraction.
It then gets the new machinery working in-process (with some pieces of out-of-process in place)

One synchronous behavior this patch maintains is the hasPendingActivity() check used to support GC.
That will (creatively) be made async in the next followup.

More generally from MessagePorts, this patch also adds a "MessageWithMessagePorts" object to be used
with all forms of postMessage(). Much better.

* CMakeLists.txt:
* Sources.txt:
* WebCore.xcodeproj/project.pbxproj:

* dom/InProcessMessagePortChannel.cpp: Removed.
* dom/InProcessMessagePortChannel.h: Removed.
* dom/MessagePortChannel.cpp: Removed.
* dom/MessagePortChannel.h: Removed.

* dom/MessageChannel.cpp:
(WebCore::MessageChannel::create):
(WebCore::MessageChannel::MessageChannel):
(WebCore::m_port2): Deleted.
* dom/MessageChannel.h:
(WebCore::MessageChannel::create): Deleted.

* dom/MessagePort.cpp:
(WebCore::MessagePort::create):
(WebCore::MessagePort::MessagePort):
(WebCore::MessagePort::~MessagePort):
(WebCore::MessagePort::entangle):
(WebCore::MessagePort::postMessage):
(WebCore::MessagePort::disentangle):
(WebCore::MessagePort::messageAvailable):
(WebCore::MessagePort::start):
(WebCore::MessagePort::close):
(WebCore::MessagePort::contextDestroyed):
(WebCore::MessagePort::dispatchMessages):
(WebCore::MessagePort::hasPendingActivity const):
(WebCore::MessagePort::locallyEntangledPort const):
(WebCore::MessagePort::disentanglePorts):
(WebCore::MessagePort::entanglePorts):
(WebCore::MessagePort::entangleWithRemote): Deleted.
* dom/MessagePort.h:

* dom/MessagePortIdentifier.h:
(WebCore::MessagePortIdentifier::logString const):

* dom/ScriptExecutionContext.cpp:
(WebCore::ScriptExecutionContext::processMessageWithMessagePortsSoon):
(WebCore::ScriptExecutionContext::dispatchMessagePortEvents):
(WebCore::ScriptExecutionContext::processMessagePortMessagesSoon): Deleted.
* dom/ScriptExecutionContext.h:

Add a single object that represents two intertwined ports, tracks their pending
messages, tracks which process they're in, etc etc:
* dom/messageports/MessagePortChannel.cpp: Added.
(WebCore::MessagePortChannel::create):
(WebCore::MessagePortChannel::MessagePortChannel):
(WebCore::MessagePortChannel::~MessagePortChannel):
(WebCore::MessagePortChannel::includesPort):
(WebCore::MessagePortChannel::entanglePortWithProcess):
(WebCore::MessagePortChannel::disentanglePort):
(WebCore::MessagePortChannel::closePort):
(WebCore::MessagePortChannel::postMessageToRemote):
(WebCore::MessagePortChannel::takeAllMessagesForPort):
(WebCore::MessagePortChannel::hasAnyMessagesPendingOrInFlight const):
* dom/messageports/MessagePortChannel.h: Added.
(WebCore::MessagePortChannel::port1 const):
(WebCore::MessagePortChannel::port2 const):
(WebCore::MessagePortChannel::logString const):

Abstraction for creating and operating on MessagePorts in a potentially cross-process way:
* dom/messageports/MessagePortChannelProvider.cpp: Added.
(WebCore::MessagePortChannelProvider::singleton):
(WebCore::MessagePortChannelProvider::setSharedProvider):
* dom/messageports/MessagePortChannelProvider.h: Added.
(WebCore::MessagePortChannelProvider::~MessagePortChannelProvider):

Adds a concrete implementation of that provider to be used in-process (e.g. WK1):
* dom/messageports/MessagePortChannelProviderImpl.cpp: Added.
(WebCore::MessagePortChannelProviderImpl::~MessagePortChannelProviderImpl):
(WebCore::MessagePortChannelProviderImpl::performActionOnAppropriateThread):
(WebCore::MessagePortChannelProviderImpl::createNewMessagePortChannel):
(WebCore::MessagePortChannelProviderImpl::entangleLocalPortInThisProcessToRemote):
(WebCore::MessagePortChannelProviderImpl::messagePortDisentangled):
(WebCore::MessagePortChannelProviderImpl::messagePortClosed):
(WebCore::MessagePortChannelProviderImpl::postMessageToRemote):
(WebCore::MessagePortChannelProviderImpl::takeAllMessagesForPort):
(WebCore::MessagePortChannelProviderImpl::hasMessagesForPorts_temporarySync):
* dom/messageports/MessagePortChannelProviderImpl.h: Added.

Adds a main thread object to handle the set of all MessagePortChannels that are open.
For now it lives in the WebProcess, but for out-of-process it will live in the UIProcess:
* dom/messageports/MessagePortChannelRegistry.cpp: Added.
(WebCore::MessagePortChannelRegistry::~MessagePortChannelRegistry):
(WebCore::MessagePortChannelRegistry::didCreateMessagePortChannel):
(WebCore::MessagePortChannelRegistry::messagePortChannelCreated):
(WebCore::MessagePortChannelRegistry::messagePortChannelDestroyed):
(WebCore::MessagePortChannelRegistry::didEntangleLocalToRemote):
(WebCore::MessagePortChannelRegistry::didDisentangleMessagePort):
(WebCore::MessagePortChannelRegistry::didCloseMessagePort):
(WebCore::MessagePortChannelRegistry::didPostMessageToRemote):
(WebCore::MessagePortChannelRegistry::takeAllMessagesForPort):
(WebCore::MessagePortChannelRegistry::hasMessagesForPorts_temporarySync): This is named against style
  and weird on purpose - to call attention to how bad it is and how it's temporary.
(WebCore::MessagePortChannelRegistry::existingChannelContainingPort):
* dom/messageports/MessagePortChannelRegistry.h: Added.

Add an object that represents a "SerializedScriptValue for the message payload and the ports
that are being transferred along with that payload". This is used in all forms of postMessage():
* dom/messageports/MessageWithMessagePorts.cpp: Added.
* dom/messageports/MessageWithMessagePorts.h: Added.

* page/DOMWindow.cpp:
(WebCore::PostMessageTimer::PostMessageTimer):
(WebCore::PostMessageTimer::event):
(WebCore::DOMWindow::postMessage):

* platform/Logging.h:

* workers/DedicatedWorkerGlobalScope.cpp:
(WebCore::DedicatedWorkerGlobalScope::postMessage):

* workers/Worker.cpp:
(WebCore::Worker::postMessage):

* workers/WorkerGlobalScopeProxy.h:

* workers/WorkerMessagingProxy.cpp:
(WebCore::WorkerMessagingProxy::postMessageToWorkerObject):
(WebCore::WorkerMessagingProxy::postMessageToWorkerGlobalScope):
* workers/WorkerMessagingProxy.h:

* workers/WorkerObjectProxy.h:

* workers/service/ServiceWorker.cpp:
(WebCore::ServiceWorker::postMessage):

* workers/service/ServiceWorkerClient.cpp:
(WebCore::ServiceWorkerClient::postMessage):

* workers/service/context/SWContextManager.cpp:
(WebCore::SWContextManager::postMessageToServiceWorker):

* workers/service/context/ServiceWorkerThread.cpp:
(WebCore::fireMessageEvent):
(WebCore::ServiceWorkerThread::postMessageToServiceWorker):
* workers/service/context/ServiceWorkerThread.h:

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@227190 268f45cc-cd09-0410-ab3c-d52691b4dbfc
38 files changed:
Source/WebCore/CMakeLists.txt
Source/WebCore/ChangeLog
Source/WebCore/Sources.txt
Source/WebCore/WebCore.xcodeproj/project.pbxproj
Source/WebCore/dom/InProcessMessagePortChannel.cpp [deleted file]
Source/WebCore/dom/InProcessMessagePortChannel.h [deleted file]
Source/WebCore/dom/MessageChannel.cpp
Source/WebCore/dom/MessageChannel.h
Source/WebCore/dom/MessagePort.cpp
Source/WebCore/dom/MessagePort.h
Source/WebCore/dom/MessagePortChannel.cpp [deleted file]
Source/WebCore/dom/MessagePortChannel.h [deleted file]
Source/WebCore/dom/MessagePortIdentifier.h
Source/WebCore/dom/ScriptExecutionContext.cpp
Source/WebCore/dom/ScriptExecutionContext.h
Source/WebCore/dom/messageports/MessagePortChannel.cpp [new file with mode: 0644]
Source/WebCore/dom/messageports/MessagePortChannel.h [new file with mode: 0644]
Source/WebCore/dom/messageports/MessagePortChannelProvider.cpp [new file with mode: 0644]
Source/WebCore/dom/messageports/MessagePortChannelProvider.h [new file with mode: 0644]
Source/WebCore/dom/messageports/MessagePortChannelProviderImpl.cpp [new file with mode: 0644]
Source/WebCore/dom/messageports/MessagePortChannelProviderImpl.h [new file with mode: 0644]
Source/WebCore/dom/messageports/MessagePortChannelRegistry.cpp [new file with mode: 0644]
Source/WebCore/dom/messageports/MessagePortChannelRegistry.h [new file with mode: 0644]
Source/WebCore/dom/messageports/MessageWithMessagePorts.cpp [new file with mode: 0644]
Source/WebCore/dom/messageports/MessageWithMessagePorts.h [new file with mode: 0644]
Source/WebCore/page/DOMWindow.cpp
Source/WebCore/platform/Logging.h
Source/WebCore/workers/DedicatedWorkerGlobalScope.cpp
Source/WebCore/workers/Worker.cpp
Source/WebCore/workers/WorkerGlobalScopeProxy.h
Source/WebCore/workers/WorkerMessagingProxy.cpp
Source/WebCore/workers/WorkerMessagingProxy.h
Source/WebCore/workers/WorkerObjectProxy.h
Source/WebCore/workers/service/ServiceWorker.cpp
Source/WebCore/workers/service/ServiceWorkerClient.cpp
Source/WebCore/workers/service/context/SWContextManager.cpp
Source/WebCore/workers/service/context/ServiceWorkerThread.cpp
Source/WebCore/workers/service/context/ServiceWorkerThread.h