#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/rtc_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().ensureNetworkProcessConnection().connection());
});
}
void WebRTCMonitor::StartUpdating()
{
if (m_clientCount) {
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)
{
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();
});
}
}
#endif // USE(LIBWEBRTC)