| 1 | /* |
| 2 | * Copyright (C) 2018 Apple Inc. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
| 7 | * 1. Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * 2. Redistributions in binary form must reproduce the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer in the |
| 11 | * documentation and/or other materials provided with the distribution. |
| 12 | * |
| 13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' |
| 14 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 15 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS |
| 17 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 18 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 19 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 20 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 21 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 22 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| 23 | * THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | */ |
| 25 | |
| 26 | #include "config.h" |
| 27 | #include "WebMessagePortChannelProvider.h" |
| 28 | |
| 29 | #include "WebProcess.h" |
| 30 | #include "WebProcessProxyMessages.h" |
| 31 | #include <WebCore/MessagePort.h> |
| 32 | #include <WebCore/MessagePortIdentifier.h> |
| 33 | #include <WebCore/MessageWithMessagePorts.h> |
| 34 | |
| 35 | namespace WebKit { |
| 36 | using namespace WebCore; |
| 37 | |
| 38 | WebMessagePortChannelProvider& WebMessagePortChannelProvider::singleton() |
| 39 | { |
| 40 | static WebMessagePortChannelProvider* provider = new WebMessagePortChannelProvider; |
| 41 | return *provider; |
| 42 | } |
| 43 | |
| 44 | WebMessagePortChannelProvider::WebMessagePortChannelProvider() |
| 45 | { |
| 46 | } |
| 47 | |
| 48 | WebMessagePortChannelProvider::~WebMessagePortChannelProvider() |
| 49 | { |
| 50 | ASSERT_NOT_REACHED(); |
| 51 | } |
| 52 | |
| 53 | void WebMessagePortChannelProvider::createNewMessagePortChannel(const MessagePortIdentifier& port1, const MessagePortIdentifier& port2) |
| 54 | { |
| 55 | WebProcess::singleton().parentProcessConnection()->send(Messages::WebProcessProxy::CreateNewMessagePortChannel(port1, port2), 0); |
| 56 | } |
| 57 | |
| 58 | void WebMessagePortChannelProvider::entangleLocalPortInThisProcessToRemote(const MessagePortIdentifier& local, const MessagePortIdentifier& remote) |
| 59 | { |
| 60 | WebProcess::singleton().parentProcessConnection()->send(Messages::WebProcessProxy::EntangleLocalPortInThisProcessToRemote(local, remote), 0); |
| 61 | } |
| 62 | |
| 63 | void WebMessagePortChannelProvider::messagePortDisentangled(const MessagePortIdentifier& port) |
| 64 | { |
| 65 | WebProcess::singleton().parentProcessConnection()->send(Messages::WebProcessProxy::MessagePortDisentangled(port), 0); |
| 66 | } |
| 67 | |
| 68 | void WebMessagePortChannelProvider::messagePortClosed(const MessagePortIdentifier& port) |
| 69 | { |
| 70 | WebProcess::singleton().parentProcessConnection()->send(Messages::WebProcessProxy::MessagePortClosed(port), 0); |
| 71 | } |
| 72 | |
| 73 | void WebMessagePortChannelProvider::takeAllMessagesForPort(const MessagePortIdentifier& port, Function<void(Vector<MessageWithMessagePorts>&&, Function<void()>&&)>&& completionHandler) |
| 74 | { |
| 75 | static std::atomic<uint64_t> currentHandlerIdentifier; |
| 76 | uint64_t identifier = ++currentHandlerIdentifier; |
| 77 | |
| 78 | { |
| 79 | Locker<Lock> locker(m_takeAllMessagesCallbackLock); |
| 80 | auto result = m_takeAllMessagesCallbacks.ensure(identifier, [completionHandler = WTFMove(completionHandler)]() mutable { |
| 81 | return WTFMove(completionHandler); |
| 82 | }); |
| 83 | ASSERT_UNUSED(result, result.isNewEntry); |
| 84 | } |
| 85 | |
| 86 | WebProcess::singleton().parentProcessConnection()->send(Messages::WebProcessProxy::TakeAllMessagesForPort(port, identifier), 0); |
| 87 | } |
| 88 | |
| 89 | void WebMessagePortChannelProvider::didTakeAllMessagesForPort(Vector<MessageWithMessagePorts>&& messages, uint64_t messageCallbackIdentifier, uint64_t messageBatchIdentifier) |
| 90 | { |
| 91 | ASSERT(isMainThread()); |
| 92 | |
| 93 | Locker<Lock> locker(m_takeAllMessagesCallbackLock); |
| 94 | auto callback = m_takeAllMessagesCallbacks.take(messageCallbackIdentifier); |
| 95 | locker.unlockEarly(); |
| 96 | |
| 97 | ASSERT(callback); |
| 98 | callback(WTFMove(messages), [messageBatchIdentifier] { |
| 99 | WebProcess::singleton().parentProcessConnection()->send(Messages::WebProcessProxy::DidDeliverMessagePortMessages(messageBatchIdentifier), 0); |
| 100 | }); |
| 101 | } |
| 102 | |
| 103 | void WebMessagePortChannelProvider::didCheckRemotePortForActivity(uint64_t callbackIdentifier, bool hasActivity) |
| 104 | { |
| 105 | ASSERT(isMainThread()); |
| 106 | |
| 107 | Locker<Lock> locker(m_remoteActivityCallbackLock); |
| 108 | auto callback = m_remoteActivityCallbacks.take(callbackIdentifier); |
| 109 | locker.unlockEarly(); |
| 110 | |
| 111 | ASSERT(callback); |
| 112 | callback(hasActivity ? HasActivity::Yes : HasActivity::No); |
| 113 | } |
| 114 | |
| 115 | void WebMessagePortChannelProvider::postMessageToRemote(MessageWithMessagePorts&& message, const MessagePortIdentifier& remoteTarget) |
| 116 | { |
| 117 | WebProcess::singleton().parentProcessConnection()->send(Messages::WebProcessProxy::PostMessageToRemote(WTFMove(message), remoteTarget), 0); |
| 118 | } |
| 119 | |
| 120 | void WebMessagePortChannelProvider::checkProcessLocalPortForActivity(const MessagePortIdentifier& identifier, uint64_t callbackIdentifier) |
| 121 | { |
| 122 | WebProcess::singleton().parentProcessConnection()->send(Messages::WebProcessProxy::DidCheckProcessLocalPortForActivity { callbackIdentifier, MessagePort::isExistingMessagePortLocallyReachable(identifier) }, 0); |
| 123 | } |
| 124 | |
| 125 | void WebMessagePortChannelProvider::checkProcessLocalPortForActivity(const MessagePortIdentifier&, ProcessIdentifier, CompletionHandler<void(HasActivity)>&&) |
| 126 | { |
| 127 | // To be called only in the UI process provider, not the Web process provider. |
| 128 | ASSERT_NOT_REACHED(); |
| 129 | } |
| 130 | |
| 131 | void WebMessagePortChannelProvider::checkRemotePortForActivity(const MessagePortIdentifier& remoteTarget, CompletionHandler<void(HasActivity)>&& completionHandler) |
| 132 | { |
| 133 | static std::atomic<uint64_t> currentHandlerIdentifier; |
| 134 | uint64_t identifier = ++currentHandlerIdentifier; |
| 135 | |
| 136 | { |
| 137 | Locker<Lock> locker(m_remoteActivityCallbackLock); |
| 138 | auto result = m_remoteActivityCallbacks.ensure(identifier, [completionHandler = WTFMove(completionHandler)]() mutable { |
| 139 | return WTFMove(completionHandler); |
| 140 | }); |
| 141 | ASSERT_UNUSED(result, result.isNewEntry); |
| 142 | } |
| 143 | |
| 144 | WebProcess::singleton().parentProcessConnection()->send(Messages::WebProcessProxy::CheckRemotePortForActivity(remoteTarget, identifier), 0); |
| 145 | } |
| 146 | |
| 147 | } // namespace WebKit |
| 148 | |