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
35namespace WebKit {
36using namespace WebCore;
37
38WebMessagePortChannelProvider& WebMessagePortChannelProvider::singleton()
39{
40 static WebMessagePortChannelProvider* provider = new WebMessagePortChannelProvider;
41 return *provider;
42}
43
44WebMessagePortChannelProvider::WebMessagePortChannelProvider()
45{
46}
47
48WebMessagePortChannelProvider::~WebMessagePortChannelProvider()
49{
50 ASSERT_NOT_REACHED();
51}
52
53void WebMessagePortChannelProvider::createNewMessagePortChannel(const MessagePortIdentifier& port1, const MessagePortIdentifier& port2)
54{
55 WebProcess::singleton().parentProcessConnection()->send(Messages::WebProcessProxy::CreateNewMessagePortChannel(port1, port2), 0);
56}
57
58void WebMessagePortChannelProvider::entangleLocalPortInThisProcessToRemote(const MessagePortIdentifier& local, const MessagePortIdentifier& remote)
59{
60 WebProcess::singleton().parentProcessConnection()->send(Messages::WebProcessProxy::EntangleLocalPortInThisProcessToRemote(local, remote), 0);
61}
62
63void WebMessagePortChannelProvider::messagePortDisentangled(const MessagePortIdentifier& port)
64{
65 WebProcess::singleton().parentProcessConnection()->send(Messages::WebProcessProxy::MessagePortDisentangled(port), 0);
66}
67
68void WebMessagePortChannelProvider::messagePortClosed(const MessagePortIdentifier& port)
69{
70 WebProcess::singleton().parentProcessConnection()->send(Messages::WebProcessProxy::MessagePortClosed(port), 0);
71}
72
73void 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
89void 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
103void 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
115void WebMessagePortChannelProvider::postMessageToRemote(MessageWithMessagePorts&& message, const MessagePortIdentifier& remoteTarget)
116{
117 WebProcess::singleton().parentProcessConnection()->send(Messages::WebProcessProxy::PostMessageToRemote(WTFMove(message), remoteTarget), 0);
118}
119
120void WebMessagePortChannelProvider::checkProcessLocalPortForActivity(const MessagePortIdentifier& identifier, uint64_t callbackIdentifier)
121{
122 WebProcess::singleton().parentProcessConnection()->send(Messages::WebProcessProxy::DidCheckProcessLocalPortForActivity { callbackIdentifier, MessagePort::isExistingMessagePortLocallyReachable(identifier) }, 0);
123}
124
125void 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
131void 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