1 | /* |
2 | * Copyright (C) 2017 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 "LibWebRTCSocket.h" |
28 | |
29 | #if USE(LIBWEBRTC) |
30 | |
31 | #include "DataReference.h" |
32 | #include "LibWebRTCSocketFactory.h" |
33 | #include "NetworkProcessConnection.h" |
34 | #include "NetworkRTCSocketMessages.h" |
35 | #include "RTCPacketOptions.h" |
36 | #include "WebProcess.h" |
37 | #include <WebCore/SharedBuffer.h> |
38 | #include <wtf/Function.h> |
39 | #include <wtf/MainThread.h> |
40 | |
41 | namespace WebKit { |
42 | |
43 | LibWebRTCSocket::LibWebRTCSocket(LibWebRTCSocketFactory& factory, uint64_t identifier, Type type, const rtc::SocketAddress& localAddress, const rtc::SocketAddress& remoteAddress) |
44 | : m_factory(factory) |
45 | , m_identifier(identifier) |
46 | , m_type(type) |
47 | , m_localAddress(localAddress) |
48 | , m_remoteAddress(remoteAddress) |
49 | { |
50 | } |
51 | |
52 | LibWebRTCSocket::~LibWebRTCSocket() |
53 | { |
54 | Close(); |
55 | m_factory.detach(*this); |
56 | } |
57 | |
58 | void LibWebRTCSocket::sendOnMainThread(Function<void(IPC::Connection&)>&& callback) |
59 | { |
60 | callOnMainThread([callback = WTFMove(callback)]() { |
61 | callback(WebProcess::singleton().ensureNetworkProcessConnection().connection()); |
62 | }); |
63 | } |
64 | |
65 | rtc::SocketAddress LibWebRTCSocket::GetLocalAddress() const |
66 | { |
67 | return m_localAddress; |
68 | } |
69 | |
70 | rtc::SocketAddress LibWebRTCSocket::GetRemoteAddress() const |
71 | { |
72 | return m_remoteAddress; |
73 | } |
74 | |
75 | void LibWebRTCSocket::signalAddressReady(const rtc::SocketAddress& address) |
76 | { |
77 | m_localAddress = address; |
78 | m_state = (m_type == Type::ClientTCP) ? STATE_CONNECTED : STATE_BOUND; |
79 | SignalAddressReady(this, m_localAddress); |
80 | } |
81 | |
82 | void LibWebRTCSocket::signalReadPacket(const WebCore::SharedBuffer& buffer, rtc::SocketAddress&& address, int64_t timestamp) |
83 | { |
84 | m_remoteAddress = WTFMove(address); |
85 | SignalReadPacket(this, buffer.data(), buffer.size(), m_remoteAddress, timestamp); |
86 | } |
87 | |
88 | void LibWebRTCSocket::signalSentPacket(int rtcPacketID, int64_t sendTimeMs) |
89 | { |
90 | m_availableSendingBytes += m_beingSentPacketSizes.takeFirst(); |
91 | SignalSentPacket(this, rtc::SentPacket(rtcPacketID, sendTimeMs)); |
92 | if (m_shouldSignalReadyToSend) { |
93 | m_shouldSignalReadyToSend = false; |
94 | SignalReadyToSend(this); |
95 | } |
96 | } |
97 | |
98 | void LibWebRTCSocket::signalConnect() |
99 | { |
100 | m_state = STATE_CONNECTED; |
101 | SignalConnect(this); |
102 | } |
103 | |
104 | void LibWebRTCSocket::signalClose(int error) |
105 | { |
106 | m_state = STATE_CLOSED; |
107 | SignalClose(this, error); |
108 | } |
109 | |
110 | void LibWebRTCSocket::signalNewConnection(rtc::AsyncPacketSocket* newConnectionSocket) |
111 | { |
112 | ASSERT(m_type == Type::ServerTCP); |
113 | SignalNewConnection(this, newConnectionSocket); |
114 | } |
115 | |
116 | bool LibWebRTCSocket::willSend(size_t size) |
117 | { |
118 | if (size > m_availableSendingBytes) { |
119 | m_shouldSignalReadyToSend = true; |
120 | setError(EWOULDBLOCK); |
121 | return false; |
122 | } |
123 | m_availableSendingBytes -= size; |
124 | m_beingSentPacketSizes.append(size); |
125 | return true; |
126 | } |
127 | |
128 | int LibWebRTCSocket::SendTo(const void *value, size_t size, const rtc::SocketAddress& address, const rtc::PacketOptions& options) |
129 | { |
130 | if (!willSend(size)) |
131 | return -1; |
132 | |
133 | auto buffer = WebCore::SharedBuffer::create(static_cast<const uint8_t*>(value), size); |
134 | auto identifier = this->identifier(); |
135 | |
136 | sendOnMainThread([identifier, buffer = WTFMove(buffer), address, options](IPC::Connection& connection) { |
137 | IPC::DataReference data(reinterpret_cast<const uint8_t*>(buffer->data()), buffer->size()); |
138 | connection.send(Messages::NetworkRTCSocket::SendTo { data, RTCNetwork::SocketAddress { address }, RTCPacketOptions { options } }, identifier); |
139 | }); |
140 | return size; |
141 | } |
142 | |
143 | int LibWebRTCSocket::Close() |
144 | { |
145 | if (m_state == STATE_CLOSED) |
146 | return 0; |
147 | |
148 | m_state = STATE_CLOSED; |
149 | auto identifier = this->identifier(); |
150 | sendOnMainThread([identifier](IPC::Connection& connection) { |
151 | connection.send(Messages::NetworkRTCSocket::Close(), identifier); |
152 | }); |
153 | return 0; |
154 | } |
155 | |
156 | int LibWebRTCSocket::GetOption(rtc::Socket::Option option, int* value) |
157 | { |
158 | ASSERT(option < MAX_SOCKET_OPTION); |
159 | if (auto storedValue = m_options[option]) { |
160 | *value = *storedValue; |
161 | return 0; |
162 | } |
163 | return -1; |
164 | } |
165 | |
166 | int LibWebRTCSocket::SetOption(rtc::Socket::Option option, int value) |
167 | { |
168 | ASSERT(option < MAX_SOCKET_OPTION); |
169 | |
170 | m_options[option] = value; |
171 | |
172 | auto identifier = this->identifier(); |
173 | sendOnMainThread([identifier, option, value](IPC::Connection& connection) { |
174 | connection.send(Messages::NetworkRTCSocket::SetOption(option, value), identifier); |
175 | }); |
176 | return 0; |
177 | } |
178 | |
179 | } // namespace WebKit |
180 | |
181 | #endif // USE(LIBWEBRTC) |
182 | |