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