diff options
author | Lorry Tar Creator <lorry-tar-importer@lorry> | 2017-06-27 06:07:23 +0000 |
---|---|---|
committer | Lorry Tar Creator <lorry-tar-importer@lorry> | 2017-06-27 06:07:23 +0000 |
commit | 1bf1084f2b10c3b47fd1a588d85d21ed0eb41d0c (patch) | |
tree | 46dcd36c86e7fbc6e5df36deb463b33e9967a6f7 /Source/WebKit2/WebProcess/Network/webrtc | |
parent | 32761a6cee1d0dee366b885b7b9c777e67885688 (diff) | |
download | WebKitGtk-tarball-master.tar.gz |
webkitgtk-2.16.5HEADwebkitgtk-2.16.5master
Diffstat (limited to 'Source/WebKit2/WebProcess/Network/webrtc')
18 files changed, 1312 insertions, 0 deletions
diff --git a/Source/WebKit2/WebProcess/Network/webrtc/LibWebRTCNetwork.h b/Source/WebKit2/WebProcess/Network/webrtc/LibWebRTCNetwork.h new file mode 100644 index 000000000..2a9c5ba73 --- /dev/null +++ b/Source/WebKit2/WebProcess/Network/webrtc/LibWebRTCNetwork.h @@ -0,0 +1,54 @@ +/* + * Copyright (C) 2017 Apple Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF + * THE POSSIBILITY OF SUCH DAMAGE. + */ + +#pragma once + +#if USE(LIBWEBRTC) + +#include "LibWebRTCSocketFactory.h" +#include "WebRTCMonitor.h" +#include "WebRTCResolver.h" +#include "WebRTCSocket.h" + +namespace WebKit { + +class LibWebRTCNetwork { +public: + LibWebRTCNetwork() = default; + + WebRTCMonitor& monitor() { return m_webNetworkMonitor; } + LibWebRTCSocketFactory& socketFactory() { return m_socketFactory; } + + WebRTCSocket socket(uint64_t identifier) { return WebRTCSocket(socketFactory(), identifier); } + WebRTCResolver resolver(uint64_t identifier) { return WebRTCResolver(socketFactory(), identifier); } + +private: + LibWebRTCSocketFactory m_socketFactory; + WebRTCMonitor m_webNetworkMonitor; +}; + +} // namespace WebKit + +#endif // USE(LIBWEBRTC) diff --git a/Source/WebKit2/WebProcess/Network/webrtc/LibWebRTCProvider.cpp b/Source/WebKit2/WebProcess/Network/webrtc/LibWebRTCProvider.cpp new file mode 100644 index 000000000..d3b15151b --- /dev/null +++ b/Source/WebKit2/WebProcess/Network/webrtc/LibWebRTCProvider.cpp @@ -0,0 +1,43 @@ +/* + * Copyright (C) 2017 Apple Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF + * THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "config.h" +#include "LibWebRTCProvider.h" + +#if USE(LIBWEBRTC) + +#include "WebProcess.h" +#include <webrtc/api/peerconnectionfactory.h> + +namespace WebKit { + +rtc::scoped_refptr<webrtc::PeerConnectionInterface> LibWebRTCProvider::createPeerConnection(webrtc::PeerConnectionObserver& observer) +{ + return WebCore::LibWebRTCProvider::createPeerConnection(observer, WebProcess::singleton().libWebRTCNetwork().monitor(), WebProcess::singleton().libWebRTCNetwork().socketFactory()); +} + +} // namespace WebKit + +#endif // USE(LIBWEBRTC) diff --git a/Source/WebKit2/WebProcess/Network/webrtc/LibWebRTCProvider.h b/Source/WebKit2/WebProcess/Network/webrtc/LibWebRTCProvider.h new file mode 100644 index 000000000..87dc23b90 --- /dev/null +++ b/Source/WebKit2/WebProcess/Network/webrtc/LibWebRTCProvider.h @@ -0,0 +1,45 @@ +/* + * Copyright (C) 2017 Apple Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF + * THE POSSIBILITY OF SUCH DAMAGE. + */ + +#pragma once + +#include <WebCore/LibWebRTCProvider.h> + + +namespace WebKit { + +#if USE(LIBWEBRTC) +class LibWebRTCProvider final : public WebCore::LibWebRTCProvider { +public: + LibWebRTCProvider() = default; + +private: + rtc::scoped_refptr<webrtc::PeerConnectionInterface> createPeerConnection(webrtc::PeerConnectionObserver&) final; +}; +#else +using LibWebRTCProvider = WebCore::LibWebRTCProvider; +#endif // USE(LIBWEBRTC) + +} // namespace WebKit diff --git a/Source/WebKit2/WebProcess/Network/webrtc/LibWebRTCResolver.cpp b/Source/WebKit2/WebProcess/Network/webrtc/LibWebRTCResolver.cpp new file mode 100644 index 000000000..9c718790f --- /dev/null +++ b/Source/WebKit2/WebProcess/Network/webrtc/LibWebRTCResolver.cpp @@ -0,0 +1,103 @@ +/* + * Copyright (C) 2017 Apple Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF + * THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "config.h" +#include "LibWebRTCResolver.h" + +#if USE(LIBWEBRTC) + +#include "NetworkProcessConnection.h" +#include "NetworkRTCProviderMessages.h" +#include "WebProcess.h" +#include <wtf/MainThread.h> + +namespace WebKit { + +static inline void sendOnMainThread(Function<void(IPC::Connection&)>&& callback) +{ + callOnMainThread([callback = WTFMove(callback)]() { + callback(WebProcess::singleton().networkConnection().connection()); + }); +} + +void LibWebRTCResolver::Start(const rtc::SocketAddress& address) +{ + m_isResolving = true; + m_addressToResolve = address; + m_port = address.port(); + auto identifier = m_identifier; + sendOnMainThread([identifier, address](IPC::Connection& connection) { + auto addressString = address.HostAsURIString(); + connection.send(Messages::NetworkRTCProvider::CreateResolver(identifier, String(addressString.data(), addressString.length())), 0); + }); +} + +bool LibWebRTCResolver::GetResolvedAddress(int family, rtc::SocketAddress* address) const +{ + ASSERT(address); + if (m_error || !m_addresses.size()) + return false; + + *address = m_addressToResolve; + for (auto& ipAddress : m_addresses) { + if (family == ipAddress.family()) { + address->SetResolvedIP(ipAddress); + address->SetPort(m_port); + return true; + } + } + return false; +} + +void LibWebRTCResolver::Destroy(bool) +{ + if (!isResolving()) + return; + + auto identifier = m_identifier; + sendOnMainThread([identifier](IPC::Connection& connection) { + connection.send(Messages::NetworkRTCProvider::StopResolver(identifier), 0); + }); + + // Let's take the resolver so that it gets destroyed at the end of this function. + auto resolver = WebProcess::singleton().libWebRTCNetwork().socketFactory().takeResolver(m_identifier); + ASSERT(resolver); +} + +void LibWebRTCResolver::setResolvedAddress(const Vector<rtc::IPAddress>& addresses) +{ + m_addresses = addresses; + SignalDone(this); +} + +void LibWebRTCResolver::setError(int error) +{ + m_error = error; + SignalDone(this); +} + +} // namespace WebKit + +#endif // USE(LIBWEBRTC) diff --git a/Source/WebKit2/WebProcess/Network/webrtc/LibWebRTCResolver.h b/Source/WebKit2/WebProcess/Network/webrtc/LibWebRTCResolver.h new file mode 100644 index 000000000..935b62975 --- /dev/null +++ b/Source/WebKit2/WebProcess/Network/webrtc/LibWebRTCResolver.h @@ -0,0 +1,67 @@ +/* + * Copyright (C) 2017 Apple Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF + * THE POSSIBILITY OF SUCH DAMAGE. + */ + +#pragma once + +#if USE(LIBWEBRTC) + +#include <WebCore/LibWebRTCMacros.h> +#include <webrtc/base/nethelpers.h> +#include <webrtc/p2p/base/packetsocketfactory.h> +#include <wtf/Vector.h> + +namespace WebKit { +class LibWebRTCSocketFactory; + +class LibWebRTCResolver final : public rtc::AsyncResolverInterface { +public: + LibWebRTCResolver(uint64_t identifier) : m_identifier(identifier) { } + + bool isResolving() const { return m_isResolving; } + uint64_t identifier() const { return m_identifier; } + +private: + friend class WebRTCResolver; + + // AsyncResolverInterface API. + void Start(const rtc::SocketAddress&) final; + bool GetResolvedAddress(int, rtc::SocketAddress*) const final; + int GetError() const final { return m_error; } + void Destroy(bool) final; + + void setError(int); + void setResolvedAddress(const Vector<rtc::IPAddress>&); + + uint64_t m_identifier; + Vector<rtc::IPAddress> m_addresses; + rtc::SocketAddress m_addressToResolve; + int m_error { 0 }; + uint16_t m_port { 0 }; + bool m_isResolving { false }; +}; + +} // namespace WebKit + +#endif // USE(LIBWEBRTC) diff --git a/Source/WebKit2/WebProcess/Network/webrtc/LibWebRTCSocket.cpp b/Source/WebKit2/WebProcess/Network/webrtc/LibWebRTCSocket.cpp new file mode 100644 index 000000000..20277d577 --- /dev/null +++ b/Source/WebKit2/WebProcess/Network/webrtc/LibWebRTCSocket.cpp @@ -0,0 +1,178 @@ +/* + * Copyright (C) 2017 Apple Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF + * THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "config.h" +#include "LibWebRTCSocket.h" + +#if USE(LIBWEBRTC) + +#include "DataReference.h" +#include "LibWebRTCSocketFactory.h" +#include "NetworkProcessConnection.h" +#include "NetworkRTCSocketMessages.h" +#include "WebProcess.h" +#include <WebCore/SharedBuffer.h> +#include <wtf/Function.h> +#include <wtf/MainThread.h> + +namespace WebKit { + +static inline void sendOnMainThread(Function<void(IPC::Connection&)>&& callback) +{ + callOnMainThread([callback = WTFMove(callback)]() { + callback(WebProcess::singleton().networkConnection().connection()); + }); +} + +LibWebRTCSocket::LibWebRTCSocket(LibWebRTCSocketFactory& factory, uint64_t identifier, Type type, const rtc::SocketAddress& localAddress, const rtc::SocketAddress& remoteAddress) + : m_factory(factory) + , m_identifier(identifier) + , m_type(type) + , m_localAddress(localAddress) + , m_remoteAddress(remoteAddress) +{ + memset(&m_options, 1, MAX_SOCKET_OPTION); +} + +LibWebRTCSocket::~LibWebRTCSocket() +{ + m_factory.detach(*this); +} + +rtc::SocketAddress LibWebRTCSocket::GetLocalAddress() const +{ + return m_localAddress; +} + +rtc::SocketAddress LibWebRTCSocket::GetRemoteAddress() const +{ + return m_remoteAddress; +} + +void LibWebRTCSocket::signalAddressReady(const rtc::SocketAddress& address) +{ + m_localAddress = address; + m_state = (m_type == Type::ClientTCP) ? STATE_CONNECTED : STATE_BOUND; + SignalAddressReady(this, m_localAddress); +} + +void LibWebRTCSocket::signalReadPacket(const WebCore::SharedBuffer& buffer, rtc::SocketAddress&& address, int64_t timestamp) +{ + m_remoteAddress = WTFMove(address); + SignalReadPacket(this, buffer.data(), buffer.size(), m_remoteAddress, rtc::PacketTime(timestamp, 0)); +} + +void LibWebRTCSocket::signalSentPacket(int rtcPacketID, int64_t sendTimeMs) +{ + m_availableSendingBytes += m_beingSentPacketSizes.takeFirst(); + SignalSentPacket(this, rtc::SentPacket(rtcPacketID, sendTimeMs)); + if (m_shouldSignalReadyToSend) { + m_shouldSignalReadyToSend = false; + SignalReadyToSend(this); + } +} + +void LibWebRTCSocket::signalConnect() +{ + m_state = STATE_CONNECTED; + SignalConnect(this); +} + +void LibWebRTCSocket::signalClose(int error) +{ + m_state = STATE_CLOSED; + SignalClose(this, error); +} + +static inline String authKey(const rtc::PacketOptions& options) +{ + if (options.packet_time_params.srtp_auth_key.size() <= 0) + return { }; + return String(options.packet_time_params.srtp_auth_key.data(), options.packet_time_params.srtp_auth_key.size()); +} + +bool LibWebRTCSocket::willSend(size_t size) +{ + if (size > m_availableSendingBytes) { + m_shouldSignalReadyToSend = true; + setError(EWOULDBLOCK); + return false; + } + m_availableSendingBytes -= size; + m_beingSentPacketSizes.append(size); + return true; +} + +int LibWebRTCSocket::SendTo(const void *value, size_t size, const rtc::SocketAddress& address, const rtc::PacketOptions& options) +{ + if (!willSend(size)) + return -1; + + auto buffer = WebCore::SharedBuffer::create(static_cast<const uint8_t*>(value), size); + auto identifier = this->identifier(); + + sendOnMainThread([identifier, buffer = WTFMove(buffer), address, options](IPC::Connection& connection) { + IPC::DataReference data(reinterpret_cast<const uint8_t*>(buffer->data()), buffer->size()); + String srtpAuthKey = authKey(options); + Messages::NetworkRTCSocket::SendTo message(data, RTCNetwork::SocketAddress(address), options.packet_id, options.packet_time_params.rtp_sendtime_extension_id, srtpAuthKey, options.packet_time_params.srtp_packet_index, options.dscp); + connection.send(WTFMove(message), identifier); + }); + return size; +} + +int LibWebRTCSocket::Close() +{ + auto identifier = this->identifier(); + sendOnMainThread([identifier](IPC::Connection& connection) { + connection.send(Messages::NetworkRTCSocket::Close(), identifier); + }); + return 0; +} + +int LibWebRTCSocket::GetOption(rtc::Socket::Option option, int* value) +{ + ASSERT(option < MAX_SOCKET_OPTION); + int storedValue = m_options[option]; + if (storedValue != -1) + *value = m_options[option]; + return 0; +} + +int LibWebRTCSocket::SetOption(rtc::Socket::Option option, int value) +{ + ASSERT(option < MAX_SOCKET_OPTION); + + m_options[option] = value; + + auto identifier = this->identifier(); + sendOnMainThread([identifier, option, value](IPC::Connection& connection) { + connection.send(Messages::NetworkRTCSocket::SetOption(option, value), identifier); + }); + return 0; +} + +} // namespace WebKit + +#endif // USE(LIBWEBRTC) diff --git a/Source/WebKit2/WebProcess/Network/webrtc/LibWebRTCSocket.h b/Source/WebKit2/WebProcess/Network/webrtc/LibWebRTCSocket.h new file mode 100644 index 000000000..957437f8d --- /dev/null +++ b/Source/WebKit2/WebProcess/Network/webrtc/LibWebRTCSocket.h @@ -0,0 +1,104 @@ +/* + * Copyright (C) 2017 Apple Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF + * THE POSSIBILITY OF SUCH DAMAGE. + */ + +#pragma once + +#if USE(LIBWEBRTC) + +#include <WebCore/LibWebRTCProvider.h> +#include <webrtc/base/asyncpacketsocket.h> +#include <wtf/Deque.h> +#include <wtf/Forward.h> + +namespace IPC { +class Connection; +class DataReference; +class Decoder; +} + +namespace WebCore { +class SharedBuffer; +} + +namespace WebKit { + +class LibWebRTCSocketFactory; + +class LibWebRTCSocket final : public rtc::AsyncPacketSocket { +public: + enum class Type { UDP, ServerTCP, ClientTCP }; + + LibWebRTCSocket(LibWebRTCSocketFactory&, uint64_t identifier, Type, const rtc::SocketAddress& localAddress, const rtc::SocketAddress& remoteAddress); + ~LibWebRTCSocket(); + + uint64_t identifier() const { return m_identifier; } + const rtc::SocketAddress& localAddress() const { return m_localAddress; } + const rtc::SocketAddress& remoteAddress() const { return m_remoteAddress; } + + void setError(int error) { m_error = error; } + void setState(State state) { m_state = state; } + +private: + bool willSend(size_t); + + friend class WebRTCSocket; + void signalReadPacket(const WebCore::SharedBuffer&, rtc::SocketAddress&&, int64_t); + void signalSentPacket(int, int64_t); + void signalAddressReady(const rtc::SocketAddress&); + void signalConnect(); + void signalClose(int); + + // AsyncPacketSocket API + int GetError() const final { return m_error; } + void SetError(int error) final { setError(error); } + rtc::SocketAddress GetLocalAddress() const final; + rtc::SocketAddress GetRemoteAddress() const final; + int Send(const void *pv, size_t cb, const rtc::PacketOptions& options) final { return SendTo(pv, cb, m_remoteAddress, options); } + int SendTo(const void *, size_t, const rtc::SocketAddress&, const rtc::PacketOptions&) final; + int Close() final; + State GetState() const final { return m_state; } + int GetOption(rtc::Socket::Option, int*) final; + int SetOption(rtc::Socket::Option, int) final; + + LibWebRTCSocketFactory& m_factory; + uint64_t m_identifier { 0 }; + Type m_type; + rtc::SocketAddress m_localAddress; + rtc::SocketAddress m_remoteAddress; + + int m_error { 0 }; + State m_state { STATE_BINDING }; + + static const unsigned MAX_SOCKET_OPTION { rtc::Socket::OPT_RTP_SENDTIME_EXTN_ID + 1 }; + int m_options[MAX_SOCKET_OPTION]; + + Deque<size_t> m_beingSentPacketSizes; + size_t m_availableSendingBytes { 65536 }; + bool m_shouldSignalReadyToSend { false }; +}; + +} // namespace WebKit + +#endif // USE(LIBWEBRTC) diff --git a/Source/WebKit2/WebProcess/Network/webrtc/LibWebRTCSocketFactory.cpp b/Source/WebKit2/WebProcess/Network/webrtc/LibWebRTCSocketFactory.cpp new file mode 100644 index 000000000..ccd8d60d1 --- /dev/null +++ b/Source/WebKit2/WebProcess/Network/webrtc/LibWebRTCSocketFactory.cpp @@ -0,0 +1,105 @@ +/* + * Copyright (C) 2017 Apple Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF + * THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "config.h" +#include "LibWebRTCSocketFactory.h" + +#if USE(LIBWEBRTC) + +#include "NetworkProcessConnection.h" +#include "NetworkRTCMonitorMessages.h" +#include "NetworkRTCProviderMessages.h" +#include "WebProcess.h" +#include "WebRTCSocket.h" +#include <wtf/MainThread.h> + +namespace WebKit { + +uint64_t LibWebRTCSocketFactory::s_uniqueSocketIdentifier = 0; +uint64_t LibWebRTCSocketFactory::s_uniqueResolverIdentifier = 0; + +rtc::AsyncPacketSocket* LibWebRTCSocketFactory::CreateServerTcpSocket(const rtc::SocketAddress& address, uint16_t minPort, uint16_t maxPort, int options) +{ + auto socket = std::make_unique<LibWebRTCSocket>(*this, ++s_uniqueSocketIdentifier, LibWebRTCSocket::Type::ServerTCP, address, rtc::SocketAddress()); + m_sockets.set(socket->identifier(), socket.get()); + + callOnMainThread([identifier = socket->identifier(), address = RTCNetwork::isolatedCopy(address), minPort, maxPort, options]() { + if (!WebProcess::singleton().networkConnection().connection().send(Messages::NetworkRTCProvider::CreateServerTCPSocket(identifier, RTCNetwork::SocketAddress(address), minPort, maxPort, options), 0)) { + // FIXME: Set error back to socket + return; + } + + }); + + return socket.release(); +} + +rtc::AsyncPacketSocket* LibWebRTCSocketFactory::CreateUdpSocket(const rtc::SocketAddress& address, uint16_t minPort, uint16_t maxPort) +{ + auto socket = std::make_unique<LibWebRTCSocket>(*this, ++s_uniqueSocketIdentifier, LibWebRTCSocket::Type::UDP, address, rtc::SocketAddress()); + m_sockets.set(socket->identifier(), socket.get()); + + callOnMainThread([identifier = socket->identifier(), address = RTCNetwork::isolatedCopy(address), minPort, maxPort]() { + if (!WebProcess::singleton().networkConnection().connection().send(Messages::NetworkRTCProvider::CreateUDPSocket(identifier, RTCNetwork::SocketAddress(address), minPort, maxPort), 0)) { + // FIXME: Set error back to socket + return; + } + }); + return socket.release(); +} + +rtc::AsyncPacketSocket* LibWebRTCSocketFactory::CreateClientTcpSocket(const rtc::SocketAddress& localAddress, const rtc::SocketAddress& remoteAddress, const rtc::ProxyInfo&, const std::string&, int options) +{ + auto socket = std::make_unique<LibWebRTCSocket>(*this, ++s_uniqueSocketIdentifier, LibWebRTCSocket::Type::ClientTCP, localAddress, remoteAddress); + socket->setState(LibWebRTCSocket::STATE_CONNECTING); + m_sockets.set(socket->identifier(), socket.get()); + + callOnMainThread([identifier = socket->identifier(), localAddress = RTCNetwork::isolatedCopy(localAddress), remoteAddress = RTCNetwork::isolatedCopy(remoteAddress), options]() { + if (!WebProcess::singleton().networkConnection().connection().send(Messages::NetworkRTCProvider::CreateClientTCPSocket(identifier, RTCNetwork::SocketAddress(localAddress), RTCNetwork::SocketAddress(remoteAddress), options), 0)) { + // FIXME: Set error back to socket + return; + } + }); + + return socket.release(); +} + +void LibWebRTCSocketFactory::detach(LibWebRTCSocket& socket) +{ + ASSERT(m_sockets.contains(socket.identifier())); + m_sockets.remove(socket.identifier()); +} + +rtc::AsyncResolverInterface* LibWebRTCSocketFactory::CreateAsyncResolver() +{ + auto resolver = std::make_unique<LibWebRTCResolver>(++s_uniqueResolverIdentifier); + auto* resolverPointer = resolver.get(); + m_resolvers.set(resolverPointer->identifier(), WTFMove(resolver)); + return resolverPointer; +} + +} // namespace WebKit + +#endif // USE(LIBWEBRTC) diff --git a/Source/WebKit2/WebProcess/Network/webrtc/LibWebRTCSocketFactory.h b/Source/WebKit2/WebProcess/Network/webrtc/LibWebRTCSocketFactory.h new file mode 100644 index 000000000..023f17561 --- /dev/null +++ b/Source/WebKit2/WebProcess/Network/webrtc/LibWebRTCSocketFactory.h @@ -0,0 +1,67 @@ +/* + * Copyright (C) 2017 Apple Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF + * THE POSSIBILITY OF SUCH DAMAGE. + */ + +#pragma once + +#if USE(LIBWEBRTC) + +#include "LibWebRTCResolver.h" +#include "LibWebRTCSocket.h" +#include <WebCore/LibWebRTCMacros.h> +#include <webrtc/base/nethelpers.h> +#include <webrtc/p2p/base/packetsocketfactory.h> +#include <wtf/HashMap.h> + +namespace WebKit { + +class LibWebRTCSocketFactory final : public rtc::PacketSocketFactory { +public: + LibWebRTCSocketFactory() { } + + void detach(LibWebRTCSocket&); + + LibWebRTCSocket* socket(uint64_t identifier) { return m_sockets.get(identifier); } + LibWebRTCResolver* resolver(uint64_t identifier) { return m_resolvers.get(identifier); } + + std::unique_ptr<LibWebRTCResolver> takeResolver(uint64_t identifier) { return m_resolvers.take(identifier); } + +private: + rtc::AsyncPacketSocket* CreateUdpSocket(const rtc::SocketAddress&, uint16_t minPort, uint16_t maxPort) final; + rtc::AsyncPacketSocket* CreateServerTcpSocket(const rtc::SocketAddress&, uint16_t min_port, uint16_t max_port, int options) final; + rtc::AsyncPacketSocket* CreateClientTcpSocket(const rtc::SocketAddress& localAddress, const rtc::SocketAddress& remoteAddress, const rtc::ProxyInfo&, const std::string&, int options); + rtc::AsyncResolverInterface* CreateAsyncResolver() final; + + // We cannot own sockets, clients of the factory are responsible to free them. + HashMap<uint64_t, LibWebRTCSocket*> m_sockets; + static uint64_t s_uniqueSocketIdentifier; + + // We can own resolvers as we control their Destroy method. + HashMap<uint64_t, std::unique_ptr<LibWebRTCResolver>> m_resolvers; + static uint64_t s_uniqueResolverIdentifier; +}; + +} // namespace WebKit + +#endif // USE(LIBWEBRTC) diff --git a/Source/WebKit2/WebProcess/Network/webrtc/WebRTCMonitor.cpp b/Source/WebKit2/WebProcess/Network/webrtc/WebRTCMonitor.cpp new file mode 100644 index 000000000..9eac494bb --- /dev/null +++ b/Source/WebKit2/WebProcess/Network/webrtc/WebRTCMonitor.cpp @@ -0,0 +1,99 @@ +/* + * Copyright (C) 2017 Apple Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF + * THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "config.h" +#include "WebRTCMonitor.h" + +#if USE(LIBWEBRTC) + +#include "NetworkConnectionToWebProcessMessages.h" +#include "NetworkProcessConnection.h" +#include "NetworkRTCMonitorMessages.h" +#include "WebProcess.h" +#include <WebCore/LibWebRTCMacros.h> +#include <webrtc/base/nethelpers.h> +#include <wtf/MainThread.h> + +namespace WebKit { + +static inline void sendOnMainThread(Function<void(IPC::Connection&)>&& callback) +{ + callOnMainThread([callback = WTFMove(callback)]() { + callback(WebProcess::singleton().networkConnection().connection()); + }); +} + +void WebRTCMonitor::StartUpdating() +{ + if (m_clientCount) { + // Need to signal new client that we already have the network list, let's do it asynchronously + if (m_receivedNetworkList) { + WebCore::LibWebRTCProvider::callOnWebRTCNetworkThread([this] { + SignalNetworksChanged(); + }); + } + } else { + m_receivedNetworkList = false; + sendOnMainThread([](IPC::Connection& connection) { + connection.send(Messages::NetworkRTCMonitor::StartUpdating(), 0); + }); + } + ++m_clientCount; +} + +void WebRTCMonitor::StopUpdating() +{ + ASSERT(m_clientCount); + if (--m_clientCount) + return; + + sendOnMainThread([](IPC::Connection& connection) { + connection.send(Messages::NetworkRTCMonitor::StopUpdating(), 0); + }); +} + +void WebRTCMonitor::networksChanged(const Vector<RTCNetwork>& networks, const RTCNetwork::IPAddress& ipv4, const RTCNetwork::IPAddress& ipv6) +{ + // No need to protect 'this' as it has the lifetime of LibWebRTC which has the lifetime of the web process. + WebCore::LibWebRTCProvider::callOnWebRTCNetworkThread([this, networks, ipv4, ipv6] { + std::vector<rtc::Network*> networkList(networks.size()); + for (size_t index = 0; index < networks.size(); ++index) + networkList[index] = new rtc::Network(networks[index].value()); + + bool forceSignaling = !m_receivedNetworkList; + m_receivedNetworkList = true; + + bool hasChanged; + set_default_local_addresses(ipv4.value, ipv6.value); + MergeNetworkList(networkList, &hasChanged); + if (hasChanged || forceSignaling) + SignalNetworksChanged(); + + }); +} + +} // namespace WebKit + +#endif // USE(LIBWEBRTC) diff --git a/Source/WebKit2/WebProcess/Network/webrtc/WebRTCMonitor.h b/Source/WebKit2/WebProcess/Network/webrtc/WebRTCMonitor.h new file mode 100644 index 000000000..dc1524911 --- /dev/null +++ b/Source/WebKit2/WebProcess/Network/webrtc/WebRTCMonitor.h @@ -0,0 +1,61 @@ +/* + * Copyright (C) 2017 Apple Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF + * THE POSSIBILITY OF SUCH DAMAGE. + */ + +#pragma once + +#if USE(LIBWEBRTC) + +#include "RTCNetwork.h" +#include <WebCore/LibWebRTCProvider.h> +#include <wtf/Vector.h> + +namespace IPC { +class Connection; +class Decoder; +} + +namespace WebKit { + +struct NetworksChangedData; + +class WebRTCMonitor final : public rtc::NetworkManagerBase { +public: + WebRTCMonitor() = default; + + void didReceiveMessage(IPC::Connection&, IPC::Decoder&); + +private: + void networksChanged(const Vector<RTCNetwork>& networkList, const RTCNetwork::IPAddress&, const RTCNetwork::IPAddress&); + + void StartUpdating() final; + void StopUpdating() final; + + unsigned m_clientCount { 0 }; + bool m_receivedNetworkList { false }; +}; + +} // namespace WebKit + +#endif // USE(LIBWEBRTC) diff --git a/Source/WebKit2/WebProcess/Network/webrtc/WebRTCMonitor.messages.in b/Source/WebKit2/WebProcess/Network/webrtc/WebRTCMonitor.messages.in new file mode 100644 index 000000000..eae177047 --- /dev/null +++ b/Source/WebKit2/WebProcess/Network/webrtc/WebRTCMonitor.messages.in @@ -0,0 +1,29 @@ +# Copyright (C) 2017 Apple Inc. All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# 1. Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# 2. Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# +# THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND +# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +# DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR +# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +#if USE(LIBWEBRTC) + +messages -> WebRTCMonitor { + void NetworksChanged(Vector<WebKit::RTCNetwork> networks, WebKit::RTCNetwork::IPAddress defaultIPV4Address, WebKit::RTCNetwork::IPAddress defaultIPV6Address) +} + +#endif // USE(LIBWEBRTC) diff --git a/Source/WebKit2/WebProcess/Network/webrtc/WebRTCResolver.cpp b/Source/WebKit2/WebProcess/Network/webrtc/WebRTCResolver.cpp new file mode 100644 index 000000000..b4bd8711e --- /dev/null +++ b/Source/WebKit2/WebProcess/Network/webrtc/WebRTCResolver.cpp @@ -0,0 +1,76 @@ +/* + * Copyright (C) 2017 Apple Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF + * THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "config.h" +#include "WebRTCResolver.h" + +#if USE(LIBWEBRTC) + +#include "LibWebRTCResolver.h" +#include "LibWebRTCSocketFactory.h" +#include <WebCore/LibWebRTCProvider.h> +#include <wtf/Function.h> + +namespace WebKit { + +WebRTCResolver::WebRTCResolver(LibWebRTCSocketFactory& socketFactory, uint64_t identifier) + : m_socketFactory(socketFactory) + , m_identifier(identifier) +{ +} + +void WebRTCResolver::setResolvedAddress(const Vector<RTCNetwork::IPAddress>& addresses) +{ + auto identifier = m_identifier; + auto& factory = m_socketFactory; + + Vector<rtc::IPAddress> rtcAddresses; + rtcAddresses.reserveInitialCapacity(addresses.size()); + for (auto& address : addresses) + rtcAddresses.uncheckedAppend(address.value); + + WebCore::LibWebRTCProvider::callOnWebRTCNetworkThread([&factory, identifier, rtcAddresses = WTFMove(rtcAddresses)]() { + auto* resolver = factory.resolver(identifier); + if (!resolver) + return; + resolver->setResolvedAddress(rtcAddresses); + }); +} + +void WebRTCResolver::resolvedAddressError(int error) +{ + auto identifier = m_identifier; + auto& factory = m_socketFactory; + WebCore::LibWebRTCProvider::callOnWebRTCNetworkThread([&factory, identifier, error]() { + auto* resolver = factory.resolver(identifier); + if (!resolver) + return; + resolver->setError(error); + }); +} + +} // namespace WebKit + +#endif // USE(LIBWEBRTC) diff --git a/Source/WebKit2/WebProcess/Network/webrtc/WebRTCResolver.h b/Source/WebKit2/WebProcess/Network/webrtc/WebRTCResolver.h new file mode 100644 index 000000000..1b82eec0a --- /dev/null +++ b/Source/WebKit2/WebProcess/Network/webrtc/WebRTCResolver.h @@ -0,0 +1,58 @@ +/* + * Copyright (C) 2017 Apple Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF + * THE POSSIBILITY OF SUCH DAMAGE. + */ + +#pragma once + +#if USE(LIBWEBRTC) + +#include "RTCNetwork.h" +#include <wtf/Vector.h> + +namespace IPC { +class Connection; +class Decoder; +} + +namespace WebKit { + +class LibWebRTCSocketFactory; + +class WebRTCResolver { +public: + WebRTCResolver(LibWebRTCSocketFactory&, uint64_t); + + void didReceiveMessage(IPC::Connection&, IPC::Decoder&); + +private: + void setResolvedAddress(const Vector<RTCNetwork::IPAddress>&); + void resolvedAddressError(int); + + LibWebRTCSocketFactory& m_socketFactory; + uint64_t m_identifier { 0 }; +}; + +} // namespace WebKit + +#endif // USE(LIBWEBRTC) diff --git a/Source/WebKit2/WebProcess/Network/webrtc/WebRTCResolver.messages.in b/Source/WebKit2/WebProcess/Network/webrtc/WebRTCResolver.messages.in new file mode 100644 index 000000000..430c8d6b8 --- /dev/null +++ b/Source/WebKit2/WebProcess/Network/webrtc/WebRTCResolver.messages.in @@ -0,0 +1,30 @@ +# Copyright (C) 2017 Apple Inc. All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# 1. Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# 2. Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# +# THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND +# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +# DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR +# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +#if USE(LIBWEBRTC) + +messages -> WebRTCResolver { + void SetResolvedAddress(Vector<WebKit::RTCNetwork::IPAddress> addresses) + void ResolvedAddressError(int error) +} + +#endif // USE(LIBWEBRTC) diff --git a/Source/WebKit2/WebProcess/Network/webrtc/WebRTCSocket.cpp b/Source/WebKit2/WebProcess/Network/webrtc/WebRTCSocket.cpp new file mode 100644 index 000000000..d6dce48aa --- /dev/null +++ b/Source/WebKit2/WebProcess/Network/webrtc/WebRTCSocket.cpp @@ -0,0 +1,95 @@ +/* + * Copyright (C) 2017 Apple Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF + * THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "config.h" +#include "WebRTCSocket.h" + +#if USE(LIBWEBRTC) + +#include "DataReference.h" +#include "LibWebRTCSocket.h" +#include "LibWebRTCSocketFactory.h" +#include "NetworkRTCSocketMessages.h" +#include <WebCore/SharedBuffer.h> +#include <wtf/Function.h> + +namespace WebKit { + +void WebRTCSocket::signalOnNetworkThread(LibWebRTCSocketFactory& factory, uint64_t identifier, Function<void(LibWebRTCSocket&)>&& callback) +{ + // factory is staying valid during the process lifetime. + WebCore::LibWebRTCProvider::callOnWebRTCNetworkThread([&factory, identifier, callback = WTFMove(callback)]() { + auto* socket = factory.socket(identifier); + if (!socket) + return; + callback(*socket); + }); +} + +WebRTCSocket::WebRTCSocket(LibWebRTCSocketFactory& factory, uint64_t identifier) + : m_factory(factory) + , m_identifier(identifier) +{ +} + +void WebRTCSocket::signalAddressReady(const RTCNetwork::SocketAddress& address) +{ + signalOnNetworkThread(m_factory, m_identifier, [address](LibWebRTCSocket& socket) { + socket.signalAddressReady(address.value); + }); +} + +void WebRTCSocket::signalReadPacket(const IPC::DataReference& data, const RTCNetwork::IPAddress& address, uint16_t port, int64_t timestamp) +{ + auto buffer = WebCore::SharedBuffer::create(data.data(), data.size()); + signalOnNetworkThread(m_factory, m_identifier, [buffer = WTFMove(buffer), address, port, timestamp](LibWebRTCSocket& socket) { + socket.signalReadPacket(buffer.get(), rtc::SocketAddress(address.value, port), timestamp); + }); +} + +void WebRTCSocket::signalSentPacket(int rtcPacketID, int64_t sendTimeMs) +{ + signalOnNetworkThread(m_factory, m_identifier, [rtcPacketID, sendTimeMs](LibWebRTCSocket& socket) { + socket.signalSentPacket(rtcPacketID, sendTimeMs); + }); +} + +void WebRTCSocket::signalConnect() +{ + signalOnNetworkThread(m_factory, m_identifier, [](LibWebRTCSocket& socket) { + socket.signalConnect(); + }); +} + +void WebRTCSocket::signalClose(int error) +{ + signalOnNetworkThread(m_factory, m_identifier, [error](LibWebRTCSocket& socket) { + socket.signalClose(error); + }); +} + +} // namespace WebKit + +#endif // USE(LIBWEBRTC) diff --git a/Source/WebKit2/WebProcess/Network/webrtc/WebRTCSocket.h b/Source/WebKit2/WebProcess/Network/webrtc/WebRTCSocket.h new file mode 100644 index 000000000..d26d60f24 --- /dev/null +++ b/Source/WebKit2/WebProcess/Network/webrtc/WebRTCSocket.h @@ -0,0 +1,65 @@ +/* + * Copyright (C) 2017 Apple Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF + * THE POSSIBILITY OF SUCH DAMAGE. + */ + +#pragma once + +#if USE(LIBWEBRTC) + +#include "RTCNetwork.h" +#include <wtf/Function.h> + +namespace IPC { +class Connection; +class DataReference; +class Decoder; +} + +namespace WebKit { + +class LibWebRTCSocket; +class LibWebRTCSocketFactory; + +class WebRTCSocket { +public: + WebRTCSocket(LibWebRTCSocketFactory&, uint64_t); + + void didReceiveMessage(IPC::Connection&, IPC::Decoder&); + + static void signalOnNetworkThread(LibWebRTCSocketFactory&, uint64_t, Function<void(LibWebRTCSocket&)>&&); + +private: + void signalReadPacket(const IPC::DataReference&, const RTCNetwork::IPAddress&, uint16_t port, int64_t); + void signalSentPacket(int, int64_t); + void signalAddressReady(const RTCNetwork::SocketAddress&); + void signalConnect(); + void signalClose(int); + + LibWebRTCSocketFactory& m_factory; + uint64_t m_identifier { 0 }; +}; + +} // namespace WebKit + +#endif // USE(LIBWEBRTC) diff --git a/Source/WebKit2/WebProcess/Network/webrtc/WebRTCSocket.messages.in b/Source/WebKit2/WebProcess/Network/webrtc/WebRTCSocket.messages.in new file mode 100644 index 000000000..426d6d192 --- /dev/null +++ b/Source/WebKit2/WebProcess/Network/webrtc/WebRTCSocket.messages.in @@ -0,0 +1,33 @@ +# Copyright (C) 2017 Apple Inc. All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# 1. Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# 2. Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# +# THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND +# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +# DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR +# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +#if USE(LIBWEBRTC) + +messages -> WebRTCSocket { + SignalReadPacket(IPC::DataReference data, WebKit::RTCNetwork::IPAddress address, uint16_t port, int64_t timestamp) + SignalSentPacket(int packetSize, int64_t timestamp) + SignalAddressReady(WebKit::RTCNetwork::SocketAddress address) + SignalConnect() + SignalClose(int error) +} + +#endif // USE(LIBWEBRTC) |