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