1 | /* |
2 | * Copyright (C) 2010-2019 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 "NetworkProcessConnection.h" |
28 | |
29 | #include "DataReference.h" |
30 | #include "LibWebRTCNetwork.h" |
31 | #include "NetworkConnectionToWebProcessMessages.h" |
32 | #include "ServiceWorkerClientFetchMessages.h" |
33 | #include "StorageAreaMap.h" |
34 | #include "StorageAreaMapMessages.h" |
35 | #include "WebCacheStorageProvider.h" |
36 | #include "WebCoreArgumentCoders.h" |
37 | #include "WebIDBConnectionToServerMessages.h" |
38 | #include "WebLoaderStrategy.h" |
39 | #include "WebMDNSRegisterMessages.h" |
40 | #include "WebPage.h" |
41 | #include "WebPageMessages.h" |
42 | #include "WebPaymentCoordinator.h" |
43 | #include "WebProcess.h" |
44 | #include "WebRTCMonitor.h" |
45 | #include "WebRTCMonitorMessages.h" |
46 | #include "WebRTCResolverMessages.h" |
47 | #include "WebRTCSocketMessages.h" |
48 | #include "WebResourceLoaderMessages.h" |
49 | #include "WebSWClientConnection.h" |
50 | #include "WebSWClientConnectionMessages.h" |
51 | #include "WebSWContextManagerConnection.h" |
52 | #include "WebSWContextManagerConnectionMessages.h" |
53 | #include "WebServiceWorkerProvider.h" |
54 | #include "WebSocketChannel.h" |
55 | #include "WebSocketChannelMessages.h" |
56 | #include "WebSocketStream.h" |
57 | #include "WebSocketStreamMessages.h" |
58 | #include <WebCore/CachedResource.h> |
59 | #include <WebCore/MemoryCache.h> |
60 | #include <WebCore/SharedBuffer.h> |
61 | #include <pal/SessionID.h> |
62 | |
63 | #if ENABLE(APPLE_PAY_REMOTE_UI) |
64 | #include "WebPaymentCoordinatorMessages.h" |
65 | #endif |
66 | |
67 | namespace WebKit { |
68 | using namespace WebCore; |
69 | |
70 | NetworkProcessConnection::NetworkProcessConnection(IPC::Connection::Identifier connectionIdentifier) |
71 | : m_connection(IPC::Connection::createClientConnection(connectionIdentifier, *this)) |
72 | { |
73 | m_connection->open(); |
74 | } |
75 | |
76 | NetworkProcessConnection::~NetworkProcessConnection() |
77 | { |
78 | m_connection->invalidate(); |
79 | } |
80 | |
81 | void NetworkProcessConnection::didReceiveMessage(IPC::Connection& connection, IPC::Decoder& decoder) |
82 | { |
83 | if (decoder.messageReceiverName() == Messages::WebResourceLoader::messageReceiverName()) { |
84 | if (auto* webResourceLoader = WebProcess::singleton().webLoaderStrategy().webResourceLoaderForIdentifier(decoder.destinationID())) |
85 | webResourceLoader->didReceiveWebResourceLoaderMessage(connection, decoder); |
86 | return; |
87 | } |
88 | if (decoder.messageReceiverName() == Messages::WebSocketStream::messageReceiverName()) { |
89 | if (auto* stream = WebSocketStream::streamWithIdentifier(decoder.destinationID())) |
90 | stream->didReceiveMessage(connection, decoder); |
91 | return; |
92 | } |
93 | if (decoder.messageReceiverName() == Messages::WebSocketChannel::messageReceiverName()) { |
94 | WebProcess::singleton().webSocketChannelManager().didReceiveMessage(connection, decoder); |
95 | return; |
96 | } |
97 | if (decoder.messageReceiverName() == Messages::WebPage::messageReceiverName()) { |
98 | if (auto* webPage = WebProcess::singleton().webPage(makeObjectIdentifier<PageIdentifierType>(decoder.destinationID()))) |
99 | webPage->didReceiveWebPageMessage(connection, decoder); |
100 | return; |
101 | } |
102 | if (decoder.messageReceiverName() == Messages::StorageAreaMap::messageReceiverName()) { |
103 | if (auto* stoargeAreaMap = WebProcess::singleton().storageAreaMap(decoder.destinationID())) |
104 | stoargeAreaMap->didReceiveMessage(connection, decoder); |
105 | return; |
106 | } |
107 | |
108 | #if USE(LIBWEBRTC) |
109 | if (decoder.messageReceiverName() == Messages::WebRTCSocket::messageReceiverName()) { |
110 | WebProcess::singleton().libWebRTCNetwork().socket(decoder.destinationID()).didReceiveMessage(connection, decoder); |
111 | return; |
112 | } |
113 | if (decoder.messageReceiverName() == Messages::WebRTCMonitor::messageReceiverName()) { |
114 | WebProcess::singleton().libWebRTCNetwork().monitor().didReceiveMessage(connection, decoder); |
115 | return; |
116 | } |
117 | if (decoder.messageReceiverName() == Messages::WebRTCResolver::messageReceiverName()) { |
118 | WebProcess::singleton().libWebRTCNetwork().resolver(decoder.destinationID()).didReceiveMessage(connection, decoder); |
119 | return; |
120 | } |
121 | #endif |
122 | #if ENABLE(WEB_RTC) |
123 | if (decoder.messageReceiverName() == Messages::WebMDNSRegister::messageReceiverName()) { |
124 | WebProcess::singleton().libWebRTCNetwork().mdnsRegister().didReceiveMessage(connection, decoder); |
125 | return; |
126 | } |
127 | #endif |
128 | |
129 | #if ENABLE(INDEXED_DATABASE) |
130 | if (decoder.messageReceiverName() == Messages::WebIDBConnectionToServer::messageReceiverName()) { |
131 | if (auto idbConnection = m_webIDBConnectionsByIdentifier.get(decoder.destinationID())) |
132 | idbConnection->didReceiveMessage(connection, decoder); |
133 | return; |
134 | } |
135 | #endif |
136 | |
137 | #if ENABLE(SERVICE_WORKER) |
138 | if (decoder.messageReceiverName() == Messages::WebSWClientConnection::messageReceiverName()) { |
139 | auto serviceWorkerConnection = m_swConnectionsByIdentifier.get(makeObjectIdentifier<SWServerConnectionIdentifierType>(decoder.destinationID())); |
140 | if (serviceWorkerConnection) |
141 | serviceWorkerConnection->didReceiveMessage(connection, decoder); |
142 | return; |
143 | } |
144 | if (decoder.messageReceiverName() == Messages::ServiceWorkerClientFetch::messageReceiverName()) { |
145 | WebServiceWorkerProvider::singleton().didReceiveServiceWorkerClientFetchMessage(connection, decoder); |
146 | return; |
147 | } |
148 | if (decoder.messageReceiverName() == Messages::WebSWContextManagerConnection::messageReceiverName()) { |
149 | ASSERT(SWContextManager::singleton().connection()); |
150 | if (auto* contextManagerConnection = SWContextManager::singleton().connection()) |
151 | static_cast<WebSWContextManagerConnection&>(*contextManagerConnection).didReceiveMessage(connection, decoder); |
152 | return; |
153 | } |
154 | #endif |
155 | |
156 | #if ENABLE(APPLE_PAY_REMOTE_UI) |
157 | if (decoder.messageReceiverName() == Messages::WebPaymentCoordinator::messageReceiverName()) { |
158 | if (auto webPage = WebProcess::singleton().webPage(makeObjectIdentifier<PageIdentifierType>(decoder.destinationID()))) |
159 | webPage->paymentCoordinator()->didReceiveMessage(connection, decoder); |
160 | return; |
161 | } |
162 | #endif |
163 | |
164 | didReceiveNetworkProcessConnectionMessage(connection, decoder); |
165 | } |
166 | |
167 | void NetworkProcessConnection::didReceiveSyncMessage(IPC::Connection& connection, IPC::Decoder& decoder, std::unique_ptr<IPC::Encoder>& replyEncoder) |
168 | { |
169 | #if ENABLE(SERVICE_WORKER) |
170 | if (decoder.messageReceiverName() == Messages::WebSWContextManagerConnection::messageReceiverName()) { |
171 | ASSERT(SWContextManager::singleton().connection()); |
172 | if (auto* contextManagerConnection = SWContextManager::singleton().connection()) |
173 | static_cast<WebSWContextManagerConnection&>(*contextManagerConnection).didReceiveSyncMessage(connection, decoder, replyEncoder); |
174 | return; |
175 | } |
176 | #endif |
177 | |
178 | #if ENABLE(APPLE_PAY_REMOTE_UI) |
179 | if (decoder.messageReceiverName() == Messages::WebPaymentCoordinator::messageReceiverName()) { |
180 | if (auto webPage = WebProcess::singleton().webPage(makeObjectIdentifier<PageIdentifierType>(decoder.destinationID()))) |
181 | webPage->paymentCoordinator()->didReceiveSyncMessage(connection, decoder, replyEncoder); |
182 | return; |
183 | } |
184 | #endif |
185 | |
186 | ASSERT_NOT_REACHED(); |
187 | } |
188 | |
189 | void NetworkProcessConnection::didClose(IPC::Connection&) |
190 | { |
191 | // The NetworkProcess probably crashed. |
192 | Ref<NetworkProcessConnection> protector(*this); |
193 | WebProcess::singleton().networkProcessConnectionClosed(this); |
194 | |
195 | #if ENABLE(INDEXED_DATABASE) |
196 | for (auto& connection : m_webIDBConnectionsByIdentifier.values()) |
197 | connection->connectionToServerLost(); |
198 | |
199 | m_webIDBConnectionsByIdentifier.clear(); |
200 | m_webIDBConnectionsBySession.clear(); |
201 | #endif |
202 | |
203 | #if ENABLE(SERVICE_WORKER) |
204 | auto connections = std::exchange(m_swConnectionsByIdentifier, { }); |
205 | for (auto& connection : connections.values()) |
206 | connection->connectionToServerLost(); |
207 | #endif |
208 | } |
209 | |
210 | void NetworkProcessConnection::didReceiveInvalidMessage(IPC::Connection&, IPC::StringReference, IPC::StringReference) |
211 | { |
212 | } |
213 | |
214 | void NetworkProcessConnection::writeBlobsToTemporaryFiles(const Vector<String>& blobURLs, CompletionHandler<void(Vector<String>&& filePaths)>&& completionHandler) |
215 | { |
216 | WebProcess::singleton().ensureNetworkProcessConnection().connection().sendWithAsyncReply(Messages::NetworkConnectionToWebProcess::WriteBlobsToTemporaryFiles(blobURLs), WTFMove(completionHandler)); |
217 | } |
218 | |
219 | void NetworkProcessConnection::didFinishPingLoad(uint64_t pingLoadIdentifier, ResourceError&& error, ResourceResponse&& response) |
220 | { |
221 | WebProcess::singleton().webLoaderStrategy().didFinishPingLoad(pingLoadIdentifier, WTFMove(error), WTFMove(response)); |
222 | } |
223 | |
224 | void NetworkProcessConnection::didFinishPreconnection(uint64_t preconnectionIdentifier, ResourceError&& error) |
225 | { |
226 | WebProcess::singleton().webLoaderStrategy().didFinishPreconnection(preconnectionIdentifier, WTFMove(error)); |
227 | } |
228 | |
229 | void NetworkProcessConnection::setOnLineState(bool isOnLine) |
230 | { |
231 | WebProcess::singleton().webLoaderStrategy().setOnLineState(isOnLine); |
232 | } |
233 | |
234 | #if ENABLE(SHAREABLE_RESOURCE) |
235 | void NetworkProcessConnection::didCacheResource(const ResourceRequest& request, const ShareableResource::Handle& handle, PAL::SessionID sessionID) |
236 | { |
237 | CachedResource* resource = MemoryCache::singleton().resourceForRequest(request, sessionID); |
238 | if (!resource) |
239 | return; |
240 | |
241 | RefPtr<SharedBuffer> buffer = handle.tryWrapInSharedBuffer(); |
242 | if (!buffer) { |
243 | LOG_ERROR("Unable to create SharedBuffer from ShareableResource handle for resource url %s" , request.url().string().utf8().data()); |
244 | return; |
245 | } |
246 | |
247 | resource->tryReplaceEncodedData(*buffer); |
248 | } |
249 | #endif |
250 | |
251 | #if ENABLE(INDEXED_DATABASE) |
252 | WebIDBConnectionToServer& NetworkProcessConnection::idbConnectionToServerForSession(PAL::SessionID sessionID) |
253 | { |
254 | return *m_webIDBConnectionsBySession.ensure(sessionID, [&] { |
255 | auto connection = WebIDBConnectionToServer::create(sessionID); |
256 | |
257 | auto result = m_webIDBConnectionsByIdentifier.add(connection->identifier(), connection.copyRef()); |
258 | ASSERT_UNUSED(result, result.isNewEntry); |
259 | |
260 | return connection; |
261 | }).iterator->value; |
262 | } |
263 | #endif |
264 | |
265 | #if ENABLE(SERVICE_WORKER) |
266 | WebSWClientConnection& NetworkProcessConnection::serviceWorkerConnectionForSession(PAL::SessionID sessionID) |
267 | { |
268 | ASSERT(sessionID.isValid()); |
269 | return *m_swConnectionsBySession.ensure(sessionID, [sessionID] { |
270 | return WebSWClientConnection::create(sessionID); |
271 | }).iterator->value; |
272 | } |
273 | |
274 | void NetworkProcessConnection::removeSWClientConnection(WebSWClientConnection& connection) |
275 | { |
276 | ASSERT(m_swConnectionsByIdentifier.contains(connection.serverConnectionIdentifier())); |
277 | m_swConnectionsByIdentifier.remove(connection.serverConnectionIdentifier()); |
278 | } |
279 | |
280 | SWServerConnectionIdentifier NetworkProcessConnection::initializeSWClientConnection(WebSWClientConnection& connection) |
281 | { |
282 | SWServerConnectionIdentifier identifier; |
283 | bool result = m_connection->sendSync(Messages::NetworkConnectionToWebProcess::EstablishSWServerConnection(connection.sessionID()), Messages::NetworkConnectionToWebProcess::EstablishSWServerConnection::Reply(identifier), 0); |
284 | ASSERT_UNUSED(result, result); |
285 | |
286 | ASSERT(!m_swConnectionsByIdentifier.contains(identifier)); |
287 | m_swConnectionsByIdentifier.add(identifier, &connection); |
288 | |
289 | return identifier; |
290 | } |
291 | |
292 | #endif |
293 | } // namespace WebKit |
294 | |