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 "LibWebRTCResolver.h" |
28 | |
29 | #if USE(LIBWEBRTC) |
30 | |
31 | #include "LibWebRTCNetwork.h" |
32 | #include "NetworkProcessConnection.h" |
33 | #include "NetworkRTCProviderMessages.h" |
34 | #include "WebProcess.h" |
35 | #include <wtf/MainThread.h> |
36 | |
37 | namespace WebKit { |
38 | |
39 | void LibWebRTCResolver::sendOnMainThread(Function<void(IPC::Connection&)>&& callback) |
40 | { |
41 | callOnMainThread([callback = WTFMove(callback)]() { |
42 | callback(WebProcess::singleton().ensureNetworkProcessConnection().connection()); |
43 | }); |
44 | } |
45 | |
46 | void LibWebRTCResolver::Start(const rtc::SocketAddress& address) |
47 | { |
48 | m_isResolving = true; |
49 | m_addressToResolve = address; |
50 | m_port = address.port(); |
51 | auto identifier = m_identifier; |
52 | sendOnMainThread([identifier, address](IPC::Connection& connection) { |
53 | auto addressString = address.HostAsURIString(); |
54 | connection.send(Messages::NetworkRTCProvider::CreateResolver(identifier, String(addressString.data(), addressString.length())), 0); |
55 | }); |
56 | } |
57 | |
58 | bool LibWebRTCResolver::GetResolvedAddress(int family, rtc::SocketAddress* address) const |
59 | { |
60 | ASSERT(address); |
61 | if (m_error || !m_addresses.size()) |
62 | return false; |
63 | |
64 | *address = m_addressToResolve; |
65 | for (auto& ipAddress : m_addresses) { |
66 | if (family == ipAddress.family()) { |
67 | address->SetResolvedIP(ipAddress); |
68 | address->SetPort(m_port); |
69 | return true; |
70 | } |
71 | } |
72 | return false; |
73 | } |
74 | |
75 | void LibWebRTCResolver::Destroy(bool) |
76 | { |
77 | if (!isResolving()) |
78 | return; |
79 | |
80 | if (m_isProvidingResults) { |
81 | m_shouldDestroy = true; |
82 | return; |
83 | } |
84 | |
85 | auto identifier = m_identifier; |
86 | sendOnMainThread([identifier](IPC::Connection& connection) { |
87 | connection.send(Messages::NetworkRTCProvider::StopResolver(identifier), 0); |
88 | }); |
89 | |
90 | doDestroy(); |
91 | } |
92 | |
93 | void LibWebRTCResolver::doDestroy() |
94 | { |
95 | // Let's take the resolver so that it gets destroyed at the end of this function. |
96 | auto resolver = WebProcess::singleton().libWebRTCNetwork().socketFactory().takeResolver(m_identifier); |
97 | ASSERT(resolver); |
98 | } |
99 | |
100 | void LibWebRTCResolver::setResolvedAddress(const Vector<rtc::IPAddress>& addresses) |
101 | { |
102 | m_addresses = addresses; |
103 | m_isProvidingResults = true; |
104 | SignalDone(this); |
105 | m_isProvidingResults = false; |
106 | if (m_shouldDestroy) |
107 | doDestroy(); |
108 | } |
109 | |
110 | void LibWebRTCResolver::setError(int error) |
111 | { |
112 | m_error = error; |
113 | m_isProvidingResults = true; |
114 | SignalDone(this); |
115 | m_isProvidingResults = false; |
116 | if (m_shouldDestroy) |
117 | doDestroy(); |
118 | } |
119 | |
120 | } // namespace WebKit |
121 | |
122 | #endif // USE(LIBWEBRTC) |
123 | |