[webkit-changes] [WebKit/WebKit] f997a9: Give ability for IPC calls to work with NativePromise

Jean-Yves Avenard noreply at github.com
Thu Sep 21 00:53:35 PDT 2023


  Branch: refs/heads/main
  Home:   https://github.com/WebKit/WebKit
  Commit: f997a9edb80cd79caeb1d0a1b87610ffd7a56e88
      https://github.com/WebKit/WebKit/commit/f997a9edb80cd79caeb1d0a1b87610ffd7a56e88
  Author: Jean-Yves Avenard <jya at apple.com>
  Date:   2023-09-21 (Thu, 21 Sep 2023)

  Changed paths:
    M Source/WTF/wtf/NativePromise.h
    M Source/WebKit/Platform/IPC/Connection.h
    M Source/WebKit/Scripts/webkit/messages.py
    M Source/WebKit/Scripts/webkit/tests/TestWithCVPixelBufferMessages.h
    M Source/WebKit/Scripts/webkit/tests/TestWithEnabledIfMessages.h
    M Source/WebKit/Scripts/webkit/tests/TestWithIfMessageMessages.h
    M Source/WebKit/Scripts/webkit/tests/TestWithImageDataMessages.h
    M Source/WebKit/Scripts/webkit/tests/TestWithLegacyReceiverMessages.h
    M Source/WebKit/Scripts/webkit/tests/TestWithSemaphoreMessages.h
    M Source/WebKit/Scripts/webkit/tests/TestWithStreamBatchedMessages.h
    M Source/WebKit/Scripts/webkit/tests/TestWithStreamBufferMessages.h
    M Source/WebKit/Scripts/webkit/tests/TestWithStreamMessages.h
    M Source/WebKit/Scripts/webkit/tests/TestWithStreamServerConnectionHandleMessages.h
    M Source/WebKit/Scripts/webkit/tests/TestWithSuperclassMessages.h
    M Source/WebKit/Scripts/webkit/tests/TestWithoutAttributesMessages.h
    M Source/WebKit/Scripts/webkit/tests/TestWithoutUsingIPCConnectionMessages.h
    M Tools/TestWebKitAPI/Tests/IPC/ConnectionTests.cpp
    M Tools/TestWebKitAPI/Tests/IPC/IPCTestUtilities.h
    M Tools/TestWebKitAPI/Tests/IPC/MessageSenderTests.cpp
    M Tools/TestWebKitAPI/Tests/WTF/NativePromise.cpp

  Log Message:
  -----------
  Give ability for IPC calls to work with NativePromise
https://bugs.webkit.org/show_bug.cgi?id=261720
rdar://115704438

Reviewed by Kimmo Kinnunen.

Add the ability to send an asynchronous IPC message where the response
will be returned in the form of a NativePromise.
The NativePromise will be rejected with the IPC error if an error occurs.
Otherwise, the value returned is the value of the IPC response. Unlike the
CompletionHandler version sendWithAsyncReply, the returned value is only
wrapped in a std::tuple if more than 1 element is returned.

The Promise is defined for each asynchronous message generated like so:
```
class WaitForTarget {
public:
    using Arguments = std::tuple<WebCore::SeekTarget>;

    static IPC::MessageName name() { return IPC::MessageName::MediaSourcePrivateRemote_WaitForTarget; }
    static constexpr bool isSync = false;
    static constexpr bool canDispatchOutOfOrder = false;
    static constexpr bool replyCanDispatchOutOfOrder = false;

    static IPC::MessageName asyncMessageReplyName() { return IPC::MessageName::MediaSourcePrivateRemote_WaitForTargetReply; }
    static constexpr auto callbackThread = WTF::CompletionHandlerCallThread::ConstructionThread;
    using ReplyArguments = std::tuple<MediaTime>;
    using Promise = WTF::NativePromise<MediaTime, IPC::Error, true>;
    explicit WaitForTarget(const WebCore::SeekTarget& target)
        : m_arguments(target)
    {
    }

    auto&& arguments()
    {
        return WTFMove(m_arguments);
    }

private:
    std::tuple<const WebCore::SeekTarget&> m_arguments;
};
```

We need to introduce the concept of NativePromise::runSynchronouslyOnTarget
in order to ensure that processing of asynchronous results is kept in order
when mixing with the sendWithAsyncReply/CompletionHandler API.
Consider the following:
```
    m_connectionToWebProcess->connection().sendWithPromisedReply(Messages::Message1, m_identifier)
    ->whenSettled(RunLoop::main(), __func__, WTFMove(completionHandler1));
    m_connectionToWebProcess->connection().sendWithAsyncReply(Messages::Message1, WTFMove(completionHandler2), m_identifier);
```

In this example, it is expected that completionHandler1 is run before
completionHandler2 (in the same order the IPC results were received)
By default a NativePromise will always dispatch a task to run the resolve/reject
callback, and so in the example above completionHandler1 would be run after
completionHandler2 as sendWithAsyncReply immediately calls the completionHandler.

By setting the NativePromise::Producer in RunSynchronouslyOnTarget mode,
it will immediately run the callback if already on the target thread.
Preserving the order of operations.
For the time being, RunSynchronouslyOnTarget is a response only to the
problem described above.
In the future we could imagine that it is up to the NativePromise consumer
to decide how the callbacks is to be run. But this is another problem for
another day.

API tests added for both IPC and NativePromise.

* Source/WTF/wtf/NativePromise.h:
* Source/WebKit/Platform/IPC/Connection.h:
(IPC::Connection::sendWithPromisedReply):
(IPC::Connection::makeAsyncReplyHandler):
* Source/WebKit/Scripts/webkit/messages.py:
(message_to_struct_declaration):
(forward_declarations_and_headers):
* Source/WebKit/Scripts/webkit/tests/TestWithCVPixelBufferMessages.h:
* Source/WebKit/Scripts/webkit/tests/TestWithEnabledIfMessages.h:
* Source/WebKit/Scripts/webkit/tests/TestWithIfMessageMessages.h:
* Source/WebKit/Scripts/webkit/tests/TestWithImageDataMessages.h:
* Source/WebKit/Scripts/webkit/tests/TestWithLegacyReceiverMessages.h:
* Source/WebKit/Scripts/webkit/tests/TestWithSemaphoreMessages.h:
* Source/WebKit/Scripts/webkit/tests/TestWithStreamBatchedMessages.h:
* Source/WebKit/Scripts/webkit/tests/TestWithStreamBufferMessages.h:
* Source/WebKit/Scripts/webkit/tests/TestWithStreamMessages.h:
* Source/WebKit/Scripts/webkit/tests/TestWithStreamServerConnectionHandleMessages.h:
* Source/WebKit/Scripts/webkit/tests/TestWithSuperclassMessages.h:
* Source/WebKit/Scripts/webkit/tests/TestWithoutAttributesMessages.h:
* Source/WebKit/Scripts/webkit/tests/TestWithoutUsingIPCConnectionMessages.h:
* Tools/TestWebKitAPI/Tests/IPC/ConnectionTests.cpp:
(TestWebKitAPI::TEST_P):
* Tools/TestWebKitAPI/Tests/IPC/IPCTestUtilities.h:
* Tools/TestWebKitAPI/Tests/IPC/MessageSenderTests.cpp:
(TestWebKitAPI::TEST_P):
* Tools/TestWebKitAPI/Tests/WTF/NativePromise.cpp:
(TestWebKitAPI::TEST):

Canonical link: https://commits.webkit.org/268240@main




More information about the webkit-changes mailing list