ce465ba2711707e7d2a2eb35b1dc90d4aa48725d
[WebKit-https.git] / Source / WebKit / NetworkProcess / NetworkConnectionToWebProcess.cpp
1 /*
2  * Copyright (C) 2012-2016 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 "NetworkConnectionToWebProcess.h"
28
29 #include "BlobDataFileReferenceWithSandboxExtension.h"
30 #include "CacheStorageEngineConnectionMessages.h"
31 #include "DataReference.h"
32 #include "NetworkBlobRegistry.h"
33 #include "NetworkCache.h"
34 #include "NetworkConnectionToWebProcessMessages.h"
35 #include "NetworkLoad.h"
36 #include "NetworkProcess.h"
37 #include "NetworkProcessConnectionMessages.h"
38 #include "NetworkRTCMonitorMessages.h"
39 #include "NetworkRTCProviderMessages.h"
40 #include "NetworkRTCSocketMessages.h"
41 #include "NetworkResourceLoadParameters.h"
42 #include "NetworkResourceLoader.h"
43 #include "NetworkResourceLoaderMessages.h"
44 #include "NetworkSocketStream.h"
45 #include "NetworkSocketStreamMessages.h"
46 #include "RemoteNetworkingContext.h"
47 #include "SessionTracker.h"
48 #include "WebCoreArgumentCoders.h"
49 #include "WebsiteDataStoreParameters.h"
50 #include <WebCore/NetworkStorageSession.h>
51 #include <WebCore/PingHandle.h>
52 #include <WebCore/PlatformCookieJar.h>
53 #include <WebCore/ResourceLoaderOptions.h>
54 #include <WebCore/ResourceRequest.h>
55 #include <pal/SessionID.h>
56
57 #if USE(NETWORK_SESSION)
58 #include "PingLoad.h"
59 #endif
60
61 using namespace WebCore;
62
63 namespace WebKit {
64
65 Ref<NetworkConnectionToWebProcess> NetworkConnectionToWebProcess::create(IPC::Connection::Identifier connectionIdentifier)
66 {
67     return adoptRef(*new NetworkConnectionToWebProcess(connectionIdentifier));
68 }
69
70 NetworkConnectionToWebProcess::NetworkConnectionToWebProcess(IPC::Connection::Identifier connectionIdentifier)
71     : m_connection(IPC::Connection::createServerConnection(connectionIdentifier, *this))
72 {
73     m_connection->open();
74 }
75
76 NetworkConnectionToWebProcess::~NetworkConnectionToWebProcess()
77 {
78 #if USE(LIBWEBRTC)
79     if (m_rtcProvider)
80         m_rtcProvider->close();
81 #endif
82 }
83
84 void NetworkConnectionToWebProcess::didCleanupResourceLoader(NetworkResourceLoader& loader)
85 {
86     ASSERT(m_networkResourceLoaders.get(loader.identifier()) == &loader);
87
88     m_networkResourceLoaders.remove(loader.identifier());
89 }
90
91 void NetworkConnectionToWebProcess::didReceiveMessage(IPC::Connection& connection, IPC::Decoder& decoder)
92 {
93     if (decoder.messageReceiverName() == Messages::NetworkConnectionToWebProcess::messageReceiverName()) {
94         didReceiveNetworkConnectionToWebProcessMessage(connection, decoder);
95         return;
96     }
97
98     if (decoder.messageReceiverName() == Messages::NetworkResourceLoader::messageReceiverName()) {
99         auto loaderIterator = m_networkResourceLoaders.find(decoder.destinationID());
100         if (loaderIterator != m_networkResourceLoaders.end())
101             loaderIterator->value->didReceiveNetworkResourceLoaderMessage(connection, decoder);
102         return;
103     }
104
105     if (decoder.messageReceiverName() == Messages::NetworkSocketStream::messageReceiverName()) {
106         auto socketIterator = m_networkSocketStreams.find(decoder.destinationID());
107         if (socketIterator != m_networkSocketStreams.end()) {
108             socketIterator->value->didReceiveMessage(connection, decoder);
109             if (decoder.messageName() == Messages::NetworkSocketStream::Close::name())
110                 m_networkSocketStreams.remove(socketIterator);
111         }
112         return;
113     }
114
115 #if USE(LIBWEBRTC)
116     if (decoder.messageReceiverName() == Messages::NetworkRTCSocket::messageReceiverName()) {
117         rtcProvider().didReceiveNetworkRTCSocketMessage(connection, decoder);
118         return;
119     }
120     if (decoder.messageReceiverName() == Messages::NetworkRTCMonitor::messageReceiverName()) {
121         rtcProvider().didReceiveNetworkRTCMonitorMessage(connection, decoder);
122         return;
123     }
124     if (decoder.messageReceiverName() == Messages::NetworkRTCProvider::messageReceiverName()) {
125         rtcProvider().didReceiveMessage(connection, decoder);
126         return;
127     }
128 #endif
129
130     if (decoder.messageReceiverName() == Messages::CacheStorageEngineConnection::messageReceiverName()) {
131         cacheStorageConnection().didReceiveMessage(connection, decoder);
132         return;
133     }
134
135     ASSERT_NOT_REACHED();
136 }
137
138 #if USE(LIBWEBRTC)
139 NetworkRTCProvider& NetworkConnectionToWebProcess::rtcProvider()
140 {
141     if (!m_rtcProvider)
142         m_rtcProvider = NetworkRTCProvider::create(*this);
143     return *m_rtcProvider;
144 }
145 #endif
146
147 CacheStorageEngineConnection& NetworkConnectionToWebProcess::cacheStorageConnection()
148 {
149     if (!m_cacheStorageConnection)
150         m_cacheStorageConnection = CacheStorageEngineConnection::create(*this);
151     return *m_cacheStorageConnection;
152 }
153
154 void NetworkConnectionToWebProcess::didReceiveSyncMessage(IPC::Connection& connection, IPC::Decoder& decoder, std::unique_ptr<IPC::Encoder>& reply)
155 {
156     if (decoder.messageReceiverName() == Messages::NetworkConnectionToWebProcess::messageReceiverName()) {
157         didReceiveSyncNetworkConnectionToWebProcessMessage(connection, decoder, reply);
158         return;
159     }
160     ASSERT_NOT_REACHED();
161 }
162
163 void NetworkConnectionToWebProcess::didClose(IPC::Connection&)
164 {
165     // Protect ourself as we might be otherwise be deleted during this function.
166     Ref<NetworkConnectionToWebProcess> protector(*this);
167
168     Vector<RefPtr<NetworkResourceLoader>> loaders;
169     copyValuesToVector(m_networkResourceLoaders, loaders);
170     for (auto& loader : loaders)
171         loader->abort();
172     ASSERT(m_networkResourceLoaders.isEmpty());
173
174     NetworkBlobRegistry::singleton().connectionToWebProcessDidClose(this);
175     NetworkProcess::singleton().removeNetworkConnectionToWebProcess(this);
176
177 #if USE(LIBWEBRTC)
178     if (m_rtcProvider) {
179         m_rtcProvider->close();
180         m_rtcProvider = nullptr;
181     }
182 #endif
183 }
184
185 void NetworkConnectionToWebProcess::didReceiveInvalidMessage(IPC::Connection&, IPC::StringReference, IPC::StringReference)
186 {
187 }
188
189 void NetworkConnectionToWebProcess::createSocketStream(URL&& url, PAL::SessionID sessionID, String cachePartition, uint64_t identifier)
190 {
191     ASSERT(!m_networkSocketStreams.contains(identifier));
192     WebCore::SourceApplicationAuditToken token = { };
193 #if PLATFORM(COCOA)
194     token = { NetworkProcess::singleton().sourceApplicationAuditData() };
195 #endif
196     m_networkSocketStreams.set(identifier, NetworkSocketStream::create(WTFMove(url), sessionID, cachePartition, identifier, m_connection, WTFMove(token)));
197 }
198
199 void NetworkConnectionToWebProcess::destroySocketStream(uint64_t identifier)
200 {
201     ASSERT(m_networkSocketStreams.get(identifier));
202     m_networkSocketStreams.remove(identifier);
203 }
204
205 void NetworkConnectionToWebProcess::cleanupForSuspension(Function<void()>&& completionHandler)
206 {
207 #if USE(LIBWEBRTC)
208     if (m_rtcProvider) {
209         m_rtcProvider->closeListeningSockets(WTFMove(completionHandler));
210         return;
211     }
212 #endif
213     completionHandler();
214 }
215
216 void NetworkConnectionToWebProcess::endSuspension()
217 {
218 #if USE(LIBWEBRTC)
219     if (m_rtcProvider)
220         m_rtcProvider->authorizeListeningSockets();
221 #endif
222 }
223
224 void NetworkConnectionToWebProcess::scheduleResourceLoad(const NetworkResourceLoadParameters& loadParameters)
225 {
226     auto loader = NetworkResourceLoader::create(loadParameters, *this);
227     m_networkResourceLoaders.add(loadParameters.identifier, loader.ptr());
228     loader->start();
229 }
230
231 void NetworkConnectionToWebProcess::performSynchronousLoad(const NetworkResourceLoadParameters& loadParameters, Ref<Messages::NetworkConnectionToWebProcess::PerformSynchronousLoad::DelayedReply>&& reply)
232 {
233     auto loader = NetworkResourceLoader::create(loadParameters, *this, WTFMove(reply));
234     m_networkResourceLoaders.add(loadParameters.identifier, loader.ptr());
235     loader->start();
236 }
237
238 void NetworkConnectionToWebProcess::loadPing(NetworkResourceLoadParameters&& loadParameters, HTTPHeaderMap&& originalRequestHeaders)
239 {
240 #if USE(NETWORK_SESSION)
241     // PingLoad manages its own lifetime, deleting itself when its purpose has been fulfilled.
242     new PingLoad(WTFMove(loadParameters), WTFMove(originalRequestHeaders), *this);
243 #else
244     UNUSED_PARAM(originalRequestHeaders);
245     RefPtr<NetworkingContext> context = RemoteNetworkingContext::create(loadParameters.sessionID, loadParameters.shouldClearReferrerOnHTTPSToHTTPRedirect);
246
247     // PingHandle manages its own lifetime, deleting itself when its purpose has been fulfilled.
248     new PingHandle(context.get(), loadParameters.request, loadParameters.allowStoredCredentials == AllowStoredCredentials, PingHandle::UsesAsyncCallbacks::Yes, loadParameters.shouldFollowRedirects, [this, protectedThis = makeRef(*this), identifier = loadParameters.identifier] (const ResourceError& error) {
249         didFinishPingLoad(identifier, error);
250     });
251 #endif
252 }
253
254 void NetworkConnectionToWebProcess::didFinishPingLoad(uint64_t pingLoadIdentifier, const ResourceError& error)
255 {
256     if (!m_connection->isValid())
257         return;
258
259     m_connection->send(Messages::NetworkProcessConnection::DidFinishPingLoad(pingLoadIdentifier, error), 0);
260 }
261
262 void NetworkConnectionToWebProcess::removeLoadIdentifier(ResourceLoadIdentifier identifier)
263 {
264     RefPtr<NetworkResourceLoader> loader = m_networkResourceLoaders.get(identifier);
265
266     // It's possible we have no loader for this identifier if the NetworkProcess crashed and this was a respawned NetworkProcess.
267     if (!loader)
268         return;
269
270     // Abort the load now, as the WebProcess won't be able to respond to messages any more which might lead
271     // to leaked loader resources (connections, threads, etc).
272     loader->abort();
273     ASSERT(!m_networkResourceLoaders.contains(identifier));
274 }
275
276 void NetworkConnectionToWebProcess::setDefersLoading(ResourceLoadIdentifier identifier, bool defers)
277 {
278     RefPtr<NetworkResourceLoader> loader = m_networkResourceLoaders.get(identifier);
279     if (!loader)
280         return;
281
282     loader->setDefersLoading(defers);
283 }
284
285 void NetworkConnectionToWebProcess::prefetchDNS(const String& hostname)
286 {
287     NetworkProcess::singleton().prefetchDNS(hostname);
288 }
289
290 static NetworkStorageSession& storageSession(PAL::SessionID sessionID)
291 {
292     ASSERT(sessionID.isValid());
293     if (sessionID != PAL::SessionID::defaultSessionID()) {
294         if (auto* storageSession = NetworkStorageSession::storageSession(sessionID))
295             return *storageSession;
296
297         // Some requests with private browsing mode requested may still be coming shortly after NetworkProcess was told to destroy its session.
298         // FIXME: Find a way to track private browsing sessions more rigorously.
299         LOG_ERROR("Non-default storage session was requested, but there was no session for it. Please file a bug unless you just disabled private browsing, in which case it's an expected race.");
300     }
301     return NetworkStorageSession::defaultStorageSession();
302 }
303
304 void NetworkConnectionToWebProcess::startDownload(PAL::SessionID sessionID, DownloadID downloadID, const ResourceRequest& request, const String& suggestedName)
305 {
306     NetworkProcess::singleton().downloadManager().startDownload(this, sessionID, downloadID, request, suggestedName);
307 }
308
309 void NetworkConnectionToWebProcess::convertMainResourceLoadToDownload(PAL::SessionID sessionID, uint64_t mainResourceLoadIdentifier, DownloadID downloadID, const ResourceRequest& request, const ResourceResponse& response)
310 {
311     auto& networkProcess = NetworkProcess::singleton();
312     if (!mainResourceLoadIdentifier) {
313         networkProcess.downloadManager().startDownload(this, sessionID, downloadID, request);
314         return;
315     }
316
317     NetworkResourceLoader* loader = m_networkResourceLoaders.get(mainResourceLoadIdentifier);
318     if (!loader) {
319         // If we're trying to download a blob here loader can be null.
320         return;
321     }
322
323     loader->convertToDownload(downloadID, request, response);
324 }
325
326 void NetworkConnectionToWebProcess::cookiesForDOM(PAL::SessionID sessionID, const URL& firstParty, const URL& url, String& result)
327 {
328     result = WebCore::cookiesForDOM(storageSession(sessionID), firstParty, url);
329 }
330
331 void NetworkConnectionToWebProcess::setCookiesFromDOM(PAL::SessionID sessionID, const URL& firstParty, const URL& url, const String& cookieString)
332 {
333     WebCore::setCookiesFromDOM(storageSession(sessionID), firstParty, url, cookieString);
334 }
335
336 void NetworkConnectionToWebProcess::cookiesEnabled(PAL::SessionID sessionID, const URL& firstParty, const URL& url, bool& result)
337 {
338     result = WebCore::cookiesEnabled(storageSession(sessionID), firstParty, url);
339 }
340
341 void NetworkConnectionToWebProcess::cookieRequestHeaderFieldValue(PAL::SessionID sessionID, const URL& firstParty, const URL& url, String& result)
342 {
343     result = WebCore::cookieRequestHeaderFieldValue(storageSession(sessionID), firstParty, url);
344 }
345
346 void NetworkConnectionToWebProcess::getRawCookies(PAL::SessionID sessionID, const URL& firstParty, const URL& url, Vector<Cookie>& result)
347 {
348     WebCore::getRawCookies(storageSession(sessionID), firstParty, url, result);
349 }
350
351 void NetworkConnectionToWebProcess::deleteCookie(PAL::SessionID sessionID, const URL& url, const String& cookieName)
352 {
353     WebCore::deleteCookie(storageSession(sessionID), url, cookieName);
354 }
355
356 void NetworkConnectionToWebProcess::registerFileBlobURL(const URL& url, const String& path, const SandboxExtension::Handle& extensionHandle, const String& contentType)
357 {
358     RefPtr<SandboxExtension> extension = SandboxExtension::create(extensionHandle);
359
360     NetworkBlobRegistry::singleton().registerFileBlobURL(this, url, path, WTFMove(extension), contentType);
361 }
362
363 void NetworkConnectionToWebProcess::registerBlobURL(const URL& url, Vector<BlobPart>&& blobParts, const String& contentType)
364 {
365     NetworkBlobRegistry::singleton().registerBlobURL(this, url, WTFMove(blobParts), contentType);
366 }
367
368 void NetworkConnectionToWebProcess::registerBlobURLFromURL(const URL& url, const URL& srcURL)
369 {
370     NetworkBlobRegistry::singleton().registerBlobURL(this, url, srcURL);
371 }
372
373 void NetworkConnectionToWebProcess::preregisterSandboxExtensionsForOptionallyFileBackedBlob(const Vector<String>& filePaths, const SandboxExtension::HandleArray& handles)
374 {
375 #if ENABLE(SANDBOX_EXTENSIONS)
376     ASSERT(filePaths.size() == handles.size());
377
378     for (size_t i = 0; i < filePaths.size(); ++i)
379         m_blobDataFileReferences.add(filePaths[i], BlobDataFileReferenceWithSandboxExtension::create(filePaths[i], SandboxExtension::create(handles[i])));
380 #else
381     for (size_t i = 0; i < filePaths.size(); ++i)
382         m_blobDataFileReferences.add(filePaths[i], BlobDataFileReferenceWithSandboxExtension::create(filePaths[i], nullptr));
383 #endif
384 }
385
386 RefPtr<WebCore::BlobDataFileReference> NetworkConnectionToWebProcess::getBlobDataFileReferenceForPath(const String& path)
387 {
388     ASSERT(m_blobDataFileReferences.contains(path));
389     return m_blobDataFileReferences.get(path);
390 }
391
392 void NetworkConnectionToWebProcess::registerBlobURLOptionallyFileBacked(const URL& url, const URL& srcURL, const String& fileBackedPath, const String& contentType)
393 {
394     NetworkBlobRegistry::singleton().registerBlobURLOptionallyFileBacked(this, url, srcURL, fileBackedPath, contentType);
395 }
396
397 void NetworkConnectionToWebProcess::registerBlobURLForSlice(const URL& url, const URL& srcURL, int64_t start, int64_t end)
398 {
399     NetworkBlobRegistry::singleton().registerBlobURLForSlice(this, url, srcURL, start, end);
400 }
401
402 void NetworkConnectionToWebProcess::unregisterBlobURL(const URL& url)
403 {
404     NetworkBlobRegistry::singleton().unregisterBlobURL(this, url);
405 }
406
407 void NetworkConnectionToWebProcess::blobSize(const URL& url, uint64_t& resultSize)
408 {
409     resultSize = NetworkBlobRegistry::singleton().blobSize(this, url);
410 }
411
412 void NetworkConnectionToWebProcess::writeBlobsToTemporaryFiles(const Vector<String>& blobURLs, uint64_t requestIdentifier)
413 {
414     Vector<RefPtr<BlobDataFileReference>> fileReferences;
415     for (auto& url : blobURLs)
416         fileReferences.appendVector(NetworkBlobRegistry::singleton().filesInBlob(*this, { ParsedURLString, url }));
417
418     for (auto& file : fileReferences)
419         file->prepareForFileAccess();
420
421     NetworkBlobRegistry::singleton().writeBlobsToTemporaryFiles(blobURLs, [this, protectedThis = makeRef(*this), requestIdentifier, fileReferences = WTFMove(fileReferences)](auto& fileNames) mutable {
422         for (auto& file : fileReferences)
423             file->revokeFileAccess();
424
425         NetworkProcess::singleton().grantSandboxExtensionsToStorageProcessForBlobs(fileNames, [this, protectedThis = WTFMove(protectedThis), requestIdentifier, fileNames]() {
426             if (!m_connection->isValid())
427                 return;
428
429             m_connection->send(Messages::NetworkProcessConnection::DidWriteBlobsToTemporaryFiles(requestIdentifier, fileNames), 0);
430         });
431     });
432 }
433
434 void NetworkConnectionToWebProcess::storeDerivedDataToCache(const WebKit::NetworkCache::DataKey& dataKey, const IPC::DataReference& data)
435 {
436     if (auto* cache = NetworkProcess::singleton().cache())
437         cache->storeData(dataKey, data.data(), data.size());
438 }
439
440 void NetworkConnectionToWebProcess::setCaptureExtraNetworkLoadMetricsEnabled(bool enabled)
441 {
442     m_captureExtraNetworkLoadMetricsEnabled = enabled;
443 }
444
445 void NetworkConnectionToWebProcess::ensureLegacyPrivateBrowsingSession()
446 {
447     NetworkProcess::singleton().ensurePrivateBrowsingSession({PAL::SessionID::legacyPrivateSessionID(), { }, { }, { }});
448 }
449
450 } // namespace WebKit