| 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 | #pragma once |
| 27 | |
| 28 | #if USE(LIBWEBRTC) |
| 29 | |
| 30 | #include "LibWebRTCSocketClient.h" |
| 31 | #include "NetworkRTCMonitor.h" |
| 32 | #include "RTCNetwork.h" |
| 33 | #include <WebCore/LibWebRTCMacros.h> |
| 34 | #include <webrtc/p2p/base/basicpacketsocketfactory.h> |
| 35 | #include <webrtc/rtc_base/third_party/sigslot/sigslot.h> |
| 36 | #include <wtf/HashMap.h> |
| 37 | #include <wtf/ThreadSafeRefCounted.h> |
| 38 | #include <wtf/UniqueRef.h> |
| 39 | #include <wtf/text/WTFString.h> |
| 40 | |
| 41 | namespace IPC { |
| 42 | class Connection; |
| 43 | class Decoder; |
| 44 | } |
| 45 | |
| 46 | namespace WebKit { |
| 47 | class NetworkConnectionToWebProcess; |
| 48 | class NetworkRTCResolver; |
| 49 | class NetworkRTCSocket; |
| 50 | |
| 51 | class NetworkRTCProvider : public ThreadSafeRefCounted<NetworkRTCProvider>, public rtc::MessageHandler { |
| 52 | public: |
| 53 | static Ref<NetworkRTCProvider> create(NetworkConnectionToWebProcess& connection) { return adoptRef(*new NetworkRTCProvider(connection)); } |
| 54 | ~NetworkRTCProvider(); |
| 55 | |
| 56 | void didReceiveMessage(IPC::Connection&, IPC::Decoder&); |
| 57 | void didReceiveNetworkRTCMonitorMessage(IPC::Connection& connection, IPC::Decoder& decoder) { m_rtcMonitor.didReceiveMessage(connection, decoder); } |
| 58 | void didReceiveNetworkRTCSocketMessage(IPC::Connection&, IPC::Decoder&); |
| 59 | |
| 60 | std::unique_ptr<LibWebRTCSocketClient> takeSocket(uint64_t); |
| 61 | void resolverDone(uint64_t); |
| 62 | |
| 63 | void close(); |
| 64 | |
| 65 | void callSocket(uint64_t, Function<void(LibWebRTCSocketClient&)>&&); |
| 66 | void callOnRTCNetworkThread(Function<void()>&&); |
| 67 | void sendFromMainThread(Function<void(IPC::Connection&)>&&); |
| 68 | |
| 69 | void newConnection(LibWebRTCSocketClient&, std::unique_ptr<rtc::AsyncPacketSocket>&&); |
| 70 | |
| 71 | void closeListeningSockets(Function<void()>&&); |
| 72 | void authorizeListeningSockets() { m_isListeningSocketAuthorized = true; } |
| 73 | |
| 74 | private: |
| 75 | explicit NetworkRTCProvider(NetworkConnectionToWebProcess&); |
| 76 | |
| 77 | void createUDPSocket(uint64_t, const RTCNetwork::SocketAddress&, uint16_t, uint16_t); |
| 78 | void createClientTCPSocket(uint64_t, const RTCNetwork::SocketAddress&, const RTCNetwork::SocketAddress&, int); |
| 79 | void createServerTCPSocket(uint64_t, const RTCNetwork::SocketAddress&, uint16_t minPort, uint16_t maxPort, int); |
| 80 | void wrapNewTCPConnection(uint64_t identifier, uint64_t newConnectionSocketIdentifier); |
| 81 | |
| 82 | void createResolver(uint64_t, const String&); |
| 83 | void stopResolver(uint64_t); |
| 84 | |
| 85 | void addSocket(uint64_t, std::unique_ptr<LibWebRTCSocketClient>&&); |
| 86 | |
| 87 | void createSocket(uint64_t identifier, std::unique_ptr<rtc::AsyncPacketSocket>&&, LibWebRTCSocketClient::Type); |
| 88 | |
| 89 | void OnMessage(rtc::Message*); |
| 90 | |
| 91 | HashMap<uint64_t, std::unique_ptr<NetworkRTCResolver>> m_resolvers; |
| 92 | HashMap<uint64_t, std::unique_ptr<LibWebRTCSocketClient>> m_sockets; |
| 93 | NetworkConnectionToWebProcess* m_connection; |
| 94 | bool m_isStarted { true }; |
| 95 | |
| 96 | NetworkRTCMonitor m_rtcMonitor; |
| 97 | |
| 98 | std::unique_ptr<rtc::Thread> m_rtcNetworkThread; |
| 99 | UniqueRef<rtc::BasicPacketSocketFactory> m_packetSocketFactory; |
| 100 | |
| 101 | HashMap<uint64_t, std::unique_ptr<rtc::AsyncPacketSocket>> m_pendingIncomingSockets; |
| 102 | uint64_t m_incomingSocketIdentifier { 0 }; |
| 103 | bool m_isListeningSocketAuthorized { true }; |
| 104 | }; |
| 105 | |
| 106 | } // namespace WebKit |
| 107 | |
| 108 | #endif // USE(LIBWEBRTC) |
| 109 | |