| 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 "WebRTCMonitor.h" |
| 28 | |
| 29 | #if USE(LIBWEBRTC) |
| 30 | |
| 31 | #include "NetworkConnectionToWebProcessMessages.h" |
| 32 | #include "NetworkProcessConnection.h" |
| 33 | #include "NetworkRTCMonitorMessages.h" |
| 34 | #include "WebProcess.h" |
| 35 | #include <WebCore/LibWebRTCMacros.h> |
| 36 | #include <webrtc/rtc_base/nethelpers.h> |
| 37 | #include <wtf/MainThread.h> |
| 38 | |
| 39 | namespace WebKit { |
| 40 | |
| 41 | void WebRTCMonitor::sendOnMainThread(Function<void(IPC::Connection&)>&& callback) |
| 42 | { |
| 43 | callOnMainThread([callback = WTFMove(callback)]() { |
| 44 | callback(WebProcess::singleton().ensureNetworkProcessConnection().connection()); |
| 45 | }); |
| 46 | } |
| 47 | |
| 48 | void WebRTCMonitor::StartUpdating() |
| 49 | { |
| 50 | if (m_clientCount) { |
| 51 | // Need to signal new client that we already have the network list, let's do it asynchronously |
| 52 | if (m_receivedNetworkList) { |
| 53 | WebCore::LibWebRTCProvider::callOnWebRTCNetworkThread([this] { |
| 54 | SignalNetworksChanged(); |
| 55 | }); |
| 56 | } |
| 57 | } else { |
| 58 | m_receivedNetworkList = false; |
| 59 | sendOnMainThread([](IPC::Connection& connection) { |
| 60 | connection.send(Messages::NetworkRTCMonitor::StartUpdating(), 0); |
| 61 | }); |
| 62 | } |
| 63 | ++m_clientCount; |
| 64 | } |
| 65 | |
| 66 | void WebRTCMonitor::StopUpdating() |
| 67 | { |
| 68 | ASSERT(m_clientCount); |
| 69 | if (--m_clientCount) |
| 70 | return; |
| 71 | |
| 72 | sendOnMainThread([](IPC::Connection& connection) { |
| 73 | connection.send(Messages::NetworkRTCMonitor::StopUpdating(), 0); |
| 74 | }); |
| 75 | } |
| 76 | |
| 77 | void WebRTCMonitor::networksChanged(const Vector<RTCNetwork>& networks, const RTCNetwork::IPAddress& ipv4, const RTCNetwork::IPAddress& ipv6) |
| 78 | { |
| 79 | // No need to protect 'this' as it has the lifetime of LibWebRTC which has the lifetime of the web process. |
| 80 | WebCore::LibWebRTCProvider::callOnWebRTCNetworkThread([this, networks, ipv4, ipv6] { |
| 81 | std::vector<rtc::Network*> networkList(networks.size()); |
| 82 | for (size_t index = 0; index < networks.size(); ++index) |
| 83 | networkList[index] = new rtc::Network(networks[index].value()); |
| 84 | |
| 85 | bool forceSignaling = !m_receivedNetworkList; |
| 86 | m_receivedNetworkList = true; |
| 87 | |
| 88 | bool hasChanged; |
| 89 | set_default_local_addresses(ipv4.value, ipv6.value); |
| 90 | MergeNetworkList(networkList, &hasChanged); |
| 91 | if (hasChanged || forceSignaling) |
| 92 | SignalNetworksChanged(); |
| 93 | |
| 94 | }); |
| 95 | } |
| 96 | |
| 97 | } // namespace WebKit |
| 98 | |
| 99 | #endif // USE(LIBWEBRTC) |
| 100 | |