[Attachment Support] Add a way to write blob data to a file URL from the UI process
[WebKit-https.git] / Source / WebKit / NetworkProcess / NetworkProcess.cpp
1 /*
2  * Copyright (C) 2012-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 "NetworkProcess.h"
28
29 #include "ArgumentCoders.h"
30 #include "Attachment.h"
31 #include "AuthenticationManager.h"
32 #include "ChildProcessMessages.h"
33 #include "DataReference.h"
34 #include "DownloadProxyMessages.h"
35 #if ENABLE(LEGACY_CUSTOM_PROTOCOL_MANAGER)
36 #include "LegacyCustomProtocolManager.h"
37 #endif
38 #include "Logging.h"
39 #include "NetworkBlobRegistry.h"
40 #include "NetworkConnectionToWebProcess.h"
41 #include "NetworkProcessCreationParameters.h"
42 #include "NetworkProcessPlatformStrategies.h"
43 #include "NetworkProcessProxyMessages.h"
44 #include "NetworkResourceLoader.h"
45 #include "NetworkSession.h"
46 #include "PreconnectTask.h"
47 #include "RemoteNetworkingContext.h"
48 #include "SessionTracker.h"
49 #include "StatisticsData.h"
50 #include "WebCookieManager.h"
51 #include "WebCoreArgumentCoders.h"
52 #include "WebPageProxyMessages.h"
53 #include "WebProcessPoolMessages.h"
54 #include "WebsiteData.h"
55 #include "WebsiteDataFetchOption.h"
56 #include "WebsiteDataStore.h"
57 #include "WebsiteDataStoreParameters.h"
58 #include "WebsiteDataType.h"
59 #include <WebCore/DNS.h>
60 #include <WebCore/DeprecatedGlobalSettings.h>
61 #include <WebCore/DiagnosticLoggingClient.h>
62 #include <WebCore/LogInitialization.h>
63 #include <WebCore/MIMETypeRegistry.h>
64 #include <WebCore/NetworkStorageSession.h>
65 #include <WebCore/PlatformCookieJar.h>
66 #include <WebCore/ResourceRequest.h>
67 #include <WebCore/RuntimeApplicationChecks.h>
68 #include <WebCore/SecurityOriginData.h>
69 #include <WebCore/SecurityOriginHash.h>
70 #include <WebCore/Settings.h>
71 #include <WebCore/URLParser.h>
72 #include <pal/SessionID.h>
73 #include <wtf/CallbackAggregator.h>
74 #include <wtf/OptionSet.h>
75 #include <wtf/RunLoop.h>
76 #include <wtf/text/AtomicString.h>
77 #include <wtf/text/CString.h>
78
79 #if ENABLE(SEC_ITEM_SHIM)
80 #include "SecItemShim.h"
81 #endif
82
83 #include "NetworkCache.h"
84 #include "NetworkCacheCoders.h"
85
86 #if ENABLE(NETWORK_CAPTURE)
87 #include "NetworkCaptureManager.h"
88 #endif
89
90 #if PLATFORM(COCOA)
91 #include "NetworkSessionCocoa.h"
92 #endif
93
94 using namespace WebCore;
95
96 namespace WebKit {
97
98 NetworkProcess& NetworkProcess::singleton()
99 {
100     static NeverDestroyed<NetworkProcess> networkProcess;
101     return networkProcess;
102 }
103
104 NetworkProcess::NetworkProcess()
105     : m_hasSetCacheModel(false)
106     , m_cacheModel(CacheModelDocumentViewer)
107     , m_diskCacheIsDisabledForTesting(false)
108     , m_canHandleHTTPSServerTrustEvaluation(true)
109 #if PLATFORM(COCOA)
110     , m_clearCacheDispatchGroup(0)
111 #endif
112 #if PLATFORM(IOS)
113     , m_webSQLiteDatabaseTracker(*this)
114 #endif
115 {
116     NetworkProcessPlatformStrategies::initialize();
117
118     addSupplement<AuthenticationManager>();
119     addSupplement<WebCookieManager>();
120 #if ENABLE(LEGACY_CUSTOM_PROTOCOL_MANAGER)
121     addSupplement<LegacyCustomProtocolManager>();
122 #endif
123 }
124
125 NetworkProcess::~NetworkProcess()
126 {
127 }
128
129 AuthenticationManager& NetworkProcess::authenticationManager()
130 {
131     return *supplement<AuthenticationManager>();
132 }
133
134 DownloadManager& NetworkProcess::downloadManager()
135 {
136     static NeverDestroyed<DownloadManager> downloadManager(*this);
137     return downloadManager;
138 }
139
140 void NetworkProcess::removeNetworkConnectionToWebProcess(NetworkConnectionToWebProcess* connection)
141 {
142     size_t vectorIndex = m_webProcessConnections.find(connection);
143     ASSERT(vectorIndex != notFound);
144
145     m_webProcessConnections.remove(vectorIndex);
146 }
147
148 bool NetworkProcess::shouldTerminate()
149 {
150     // Network process keeps session cookies and credentials, so it should never terminate (as long as UI process connection is alive).
151     return false;
152 }
153
154 void NetworkProcess::didReceiveMessage(IPC::Connection& connection, IPC::Decoder& decoder)
155 {
156     if (messageReceiverMap().dispatchMessage(connection, decoder))
157         return;
158
159     if (decoder.messageReceiverName() == Messages::ChildProcess::messageReceiverName()) {
160         ChildProcess::didReceiveMessage(connection, decoder);
161         return;
162     }
163
164     didReceiveNetworkProcessMessage(connection, decoder);
165 }
166
167 void NetworkProcess::didReceiveSyncMessage(IPC::Connection& connection, IPC::Decoder& decoder, std::unique_ptr<IPC::Encoder>& replyEncoder)
168 {
169     if (messageReceiverMap().dispatchSyncMessage(connection, decoder, replyEncoder))
170         return;
171
172     didReceiveSyncNetworkProcessMessage(connection, decoder, replyEncoder);
173 }
174
175 void NetworkProcess::didClose(IPC::Connection&)
176 {
177     // The UIProcess just exited.
178     stopRunLoop();
179 }
180
181 void NetworkProcess::didCreateDownload()
182 {
183     disableTermination();
184 }
185
186 void NetworkProcess::didDestroyDownload()
187 {
188     enableTermination();
189 }
190
191 IPC::Connection* NetworkProcess::downloadProxyConnection()
192 {
193     return parentProcessConnection();
194 }
195
196 AuthenticationManager& NetworkProcess::downloadsAuthenticationManager()
197 {
198     return authenticationManager();
199 }
200
201 void NetworkProcess::lowMemoryHandler(Critical critical)
202 {
203     if (m_suppressMemoryPressureHandler)
204         return;
205
206     WTF::releaseFastMallocFreeMemory();
207 }
208
209 void NetworkProcess::initializeNetworkProcess(NetworkProcessCreationParameters&& parameters)
210 {
211     WebCore::setPresentingApplicationPID(parameters.presentingApplicationPID);
212     platformInitializeNetworkProcess(parameters);
213
214     WTF::Thread::setCurrentThreadIsUserInitiated();
215     AtomicString::init();
216
217     m_suppressMemoryPressureHandler = parameters.shouldSuppressMemoryPressureHandler;
218     m_loadThrottleLatency = parameters.loadThrottleLatency;
219     if (!m_suppressMemoryPressureHandler) {
220         auto& memoryPressureHandler = MemoryPressureHandler::singleton();
221 #if OS(LINUX)
222         if (parameters.memoryPressureMonitorHandle.fileDescriptor() != -1)
223             memoryPressureHandler.setMemoryPressureMonitorHandle(parameters.memoryPressureMonitorHandle.releaseFileDescriptor());
224 #endif
225         memoryPressureHandler.setLowMemoryHandler([this] (Critical critical, Synchronous) {
226             lowMemoryHandler(critical);
227         });
228         memoryPressureHandler.install();
229     }
230
231 #if ENABLE(NETWORK_CAPTURE)
232     NetworkCapture::Manager::singleton().initialize(
233         parameters.recordReplayMode,
234         parameters.recordReplayCacheLocation);
235 #endif
236
237     m_diskCacheIsDisabledForTesting = parameters.shouldUseTestingNetworkSession;
238
239     m_diskCacheSizeOverride = parameters.diskCacheSizeOverride;
240     setCacheModel(static_cast<uint32_t>(parameters.cacheModel));
241
242     setCanHandleHTTPSServerTrustEvaluation(parameters.canHandleHTTPSServerTrustEvaluation);
243
244     // FIXME: instead of handling this here, a message should be sent later (scales to multiple sessions)
245     if (parameters.privateBrowsingEnabled)
246         RemoteNetworkingContext::ensureWebsiteDataStoreSession(WebsiteDataStoreParameters::legacyPrivateSessionParameters());
247
248     if (parameters.shouldUseTestingNetworkSession)
249         NetworkStorageSession::switchToNewTestingSession();
250
251 #if HAVE(CFNETWORK_STORAGE_PARTITIONING) && !RELEASE_LOG_DISABLED
252     m_logCookieInformation = parameters.logCookieInformation;
253 #endif
254
255 #if USE(NETWORK_SESSION)
256 #if ENABLE(LEGACY_CUSTOM_PROTOCOL_MANAGER)
257     parameters.defaultSessionParameters.legacyCustomProtocolManager = supplement<LegacyCustomProtocolManager>();
258 #endif
259     SessionTracker::setSession(PAL::SessionID::defaultSessionID(), NetworkSession::create(WTFMove(parameters.defaultSessionParameters)));
260 #endif
261
262     for (auto& supplement : m_supplements.values())
263         supplement->initialize(parameters);
264 }
265
266 void NetworkProcess::initializeConnection(IPC::Connection* connection)
267 {
268     ChildProcess::initializeConnection(connection);
269
270     for (auto& supplement : m_supplements.values())
271         supplement->initializeConnection(connection);
272 }
273
274 void NetworkProcess::createNetworkConnectionToWebProcess()
275 {
276 #if USE(UNIX_DOMAIN_SOCKETS)
277     IPC::Connection::SocketPair socketPair = IPC::Connection::createPlatformConnection();
278
279     auto connection = NetworkConnectionToWebProcess::create(socketPair.server);
280     m_webProcessConnections.append(WTFMove(connection));
281
282     IPC::Attachment clientSocket(socketPair.client);
283     parentProcessConnection()->send(Messages::NetworkProcessProxy::DidCreateNetworkConnectionToWebProcess(clientSocket), 0);
284 #elif OS(DARWIN)
285     // Create the listening port.
286     mach_port_t listeningPort;
287     mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &listeningPort);
288
289     // Create a listening connection.
290     auto connection = NetworkConnectionToWebProcess::create(IPC::Connection::Identifier(listeningPort));
291     m_webProcessConnections.append(WTFMove(connection));
292
293     IPC::Attachment clientPort(listeningPort, MACH_MSG_TYPE_MAKE_SEND);
294     parentProcessConnection()->send(Messages::NetworkProcessProxy::DidCreateNetworkConnectionToWebProcess(clientPort), 0);
295 #else
296     notImplemented();
297 #endif
298 }
299
300 void NetworkProcess::clearCachedCredentials()
301 {
302     NetworkStorageSession::defaultStorageSession().credentialStorage().clearCredentials();
303 #if USE(NETWORK_SESSION)
304     if (auto* networkSession = SessionTracker::networkSession(PAL::SessionID::defaultSessionID()))
305         networkSession->clearCredentials();
306     else
307         ASSERT_NOT_REACHED();
308 #endif
309 }
310
311 void NetworkProcess::addWebsiteDataStore(WebsiteDataStoreParameters&& parameters)
312 {
313     RemoteNetworkingContext::ensureWebsiteDataStoreSession(WTFMove(parameters));
314 }
315
316 void NetworkProcess::destroySession(PAL::SessionID sessionID)
317 {
318     SessionTracker::destroySession(sessionID);
319 }
320
321 void NetworkProcess::grantSandboxExtensionsToStorageProcessForBlobs(const Vector<String>& filenames, Function<void ()>&& completionHandler)
322 {
323     static uint64_t lastRequestID;
324
325     uint64_t requestID = ++lastRequestID;
326     m_sandboxExtensionForBlobsCompletionHandlers.set(requestID, WTFMove(completionHandler));
327     parentProcessConnection()->send(Messages::NetworkProcessProxy::GrantSandboxExtensionsToStorageProcessForBlobs(requestID, filenames), 0);
328 }
329
330 void NetworkProcess::didGrantSandboxExtensionsToStorageProcessForBlobs(uint64_t requestID)
331 {
332     if (auto handler = m_sandboxExtensionForBlobsCompletionHandlers.take(requestID))
333         handler();
334 }
335
336 void NetworkProcess::writeBlobToFilePath(const WebCore::URL& url, const String& path, SandboxExtension::Handle&& handleForWriting, uint64_t requestID)
337 {
338     auto extension = SandboxExtension::create(WTFMove(handleForWriting));
339     if (!extension) {
340         parentProcessConnection()->send(Messages::NetworkProcessProxy::DidWriteBlobToFilePath(false, requestID), 0);
341         return;
342     }
343
344     extension->consume();
345     NetworkBlobRegistry::singleton().writeBlobToFilePath(url, path, [this, extension = WTFMove(extension), requestID] (bool success) {
346         extension->revoke();
347         parentProcessConnection()->send(Messages::NetworkProcessProxy::DidWriteBlobToFilePath(success, requestID), 0);
348     });
349 }
350
351 #if HAVE(CFNETWORK_STORAGE_PARTITIONING)
352 void NetworkProcess::updatePrevalentDomainsToPartitionOrBlockCookies(PAL::SessionID sessionID, const Vector<String>& domainsToPartition, const Vector<String>& domainsToBlock, const Vector<String>& domainsToNeitherPartitionNorBlock, bool shouldClearFirst)
353 {
354     if (auto* networkStorageSession = NetworkStorageSession::storageSession(sessionID))
355         networkStorageSession->setPrevalentDomainsToPartitionOrBlockCookies(domainsToPartition, domainsToBlock, domainsToNeitherPartitionNorBlock, shouldClearFirst);
356 }
357
358 void NetworkProcess::hasStorageAccessForPrevalentDomains(PAL::SessionID sessionID, const String& resourceDomain, const String& firstPartyDomain, uint64_t frameID, uint64_t pageID, uint64_t contextId)
359 {
360     if (auto* networkStorageSession = NetworkStorageSession::storageSession(sessionID))
361         parentProcessConnection()->send(Messages::NetworkProcessProxy::StorageAccessRequestResult(networkStorageSession->isStorageAccessGranted(resourceDomain, firstPartyDomain, frameID, pageID), contextId), 0);
362     else
363         ASSERT_NOT_REACHED();
364 }
365
366 void NetworkProcess::updateStorageAccessForPrevalentDomains(PAL::SessionID sessionID, const String& resourceDomain, const String& firstPartyDomain, uint64_t frameID, uint64_t pageID, bool shouldGrantStorage, uint64_t contextId)
367 {
368     bool isStorageGranted = false;
369     if (auto* networkStorageSession = NetworkStorageSession::storageSession(sessionID)) {
370         networkStorageSession->setStorageAccessGranted(resourceDomain, firstPartyDomain, frameID, pageID, shouldGrantStorage);
371         ASSERT(networkStorageSession->isStorageAccessGranted(resourceDomain, firstPartyDomain, frameID, pageID) == shouldGrantStorage);
372         isStorageGranted = shouldGrantStorage;
373     } else
374         ASSERT_NOT_REACHED();
375
376     parentProcessConnection()->send(Messages::NetworkProcessProxy::StorageAccessRequestResult(isStorageGranted, contextId), 0);
377 }
378
379 void NetworkProcess::removePrevalentDomains(PAL::SessionID sessionID, const Vector<String>& domains)
380 {
381     if (auto* networkStorageSession = NetworkStorageSession::storageSession(sessionID))
382         networkStorageSession->removePrevalentDomains(domains);
383 }
384 #endif
385
386 static void fetchDiskCacheEntries(PAL::SessionID sessionID, OptionSet<WebsiteDataFetchOption> fetchOptions, Function<void (Vector<WebsiteData::Entry>)>&& completionHandler)
387 {
388     if (auto* cache = NetworkProcess::singleton().cache()) {
389         HashMap<SecurityOriginData, uint64_t> originsAndSizes;
390         cache->traverse([fetchOptions, completionHandler = WTFMove(completionHandler), originsAndSizes = WTFMove(originsAndSizes)](auto* traversalEntry) mutable {
391             if (!traversalEntry) {
392                 Vector<WebsiteData::Entry> entries;
393
394                 for (auto& originAndSize : originsAndSizes)
395                     entries.append(WebsiteData::Entry { originAndSize.key, WebsiteDataType::DiskCache, originAndSize.value });
396
397                 RunLoop::main().dispatch([completionHandler = WTFMove(completionHandler), entries = WTFMove(entries)] {
398                     completionHandler(entries);
399                 });
400
401                 return;
402             }
403
404             auto url = traversalEntry->entry.response().url();
405             auto result = originsAndSizes.add({url.protocol().toString(), url.host(), url.port()}, 0);
406
407             if (fetchOptions.contains(WebsiteDataFetchOption::ComputeSizes))
408                 result.iterator->value += traversalEntry->entry.sourceStorageRecord().header.size() + traversalEntry->recordInfo.bodySize;
409         });
410
411         return;
412     }
413
414     RunLoop::main().dispatch([completionHandler = WTFMove(completionHandler)] {
415         completionHandler({ });
416     });
417 }
418
419 void NetworkProcess::fetchWebsiteData(PAL::SessionID sessionID, OptionSet<WebsiteDataType> websiteDataTypes, OptionSet<WebsiteDataFetchOption> fetchOptions, uint64_t callbackID)
420 {
421     struct CallbackAggregator final : public RefCounted<CallbackAggregator> {
422         explicit CallbackAggregator(Function<void (WebsiteData)>&& completionHandler)
423             : m_completionHandler(WTFMove(completionHandler))
424         {
425         }
426
427         ~CallbackAggregator()
428         {
429             ASSERT(RunLoop::isMain());
430
431             RunLoop::main().dispatch([completionHandler = WTFMove(m_completionHandler), websiteData = WTFMove(m_websiteData)] {
432                 completionHandler(websiteData);
433             });
434         }
435
436         Function<void (WebsiteData)> m_completionHandler;
437         WebsiteData m_websiteData;
438     };
439
440     auto callbackAggregator = adoptRef(*new CallbackAggregator([this, callbackID] (WebsiteData websiteData) {
441         parentProcessConnection()->send(Messages::NetworkProcessProxy::DidFetchWebsiteData(callbackID, websiteData), 0);
442     }));
443
444     if (websiteDataTypes.contains(WebsiteDataType::Cookies)) {
445         if (auto* networkStorageSession = NetworkStorageSession::storageSession(sessionID))
446             getHostnamesWithCookies(*networkStorageSession, callbackAggregator->m_websiteData.hostNamesWithCookies);
447     }
448
449     if (websiteDataTypes.contains(WebsiteDataType::Credentials)) {
450         if (NetworkStorageSession::storageSession(sessionID))
451             callbackAggregator->m_websiteData.originsWithCredentials = NetworkStorageSession::storageSession(sessionID)->credentialStorage().originsWithCredentials();
452     }
453
454     if (websiteDataTypes.contains(WebsiteDataType::DOMCache)) {
455         CacheStorage::Engine::fetchEntries(sessionID, fetchOptions.contains(WebsiteDataFetchOption::ComputeSizes), [callbackAggregator = callbackAggregator.copyRef()](auto entries) mutable {
456             callbackAggregator->m_websiteData.entries.appendVector(entries);
457         });
458     }
459
460     if (websiteDataTypes.contains(WebsiteDataType::DiskCache)) {
461         fetchDiskCacheEntries(sessionID, fetchOptions, [callbackAggregator = WTFMove(callbackAggregator)](auto entries) mutable {
462             callbackAggregator->m_websiteData.entries.appendVector(entries);
463         });
464     }
465 }
466
467 void NetworkProcess::deleteWebsiteData(PAL::SessionID sessionID, OptionSet<WebsiteDataType> websiteDataTypes, WallTime modifiedSince, uint64_t callbackID)
468 {
469 #if PLATFORM(COCOA)
470     if (websiteDataTypes.contains(WebsiteDataType::HSTSCache)) {
471         if (auto* networkStorageSession = NetworkStorageSession::storageSession(sessionID))
472             clearHSTSCache(*networkStorageSession, modifiedSince);
473     }
474 #endif
475
476     if (websiteDataTypes.contains(WebsiteDataType::Cookies)) {
477         if (auto* networkStorageSession = NetworkStorageSession::storageSession(sessionID))
478             deleteAllCookiesModifiedSince(*networkStorageSession, modifiedSince);
479     }
480
481     if (websiteDataTypes.contains(WebsiteDataType::Credentials)) {
482         if (NetworkStorageSession::storageSession(sessionID))
483             NetworkStorageSession::storageSession(sessionID)->credentialStorage().clearCredentials();
484     }
485
486     auto clearTasksHandler = WTF::CallbackAggregator::create([this, callbackID] {
487         parentProcessConnection()->send(Messages::NetworkProcessProxy::DidDeleteWebsiteData(callbackID), 0);
488     });
489
490     if (websiteDataTypes.contains(WebsiteDataType::DOMCache))
491         CacheStorage::Engine::from(sessionID).clearAllCaches(clearTasksHandler);
492
493     if (websiteDataTypes.contains(WebsiteDataType::DiskCache) && !sessionID.isEphemeral())
494         clearDiskCache(modifiedSince, [clearTasksHandler = WTFMove(clearTasksHandler)] { });
495 }
496
497 static void clearDiskCacheEntries(const Vector<SecurityOriginData>& origins, Function<void ()>&& completionHandler)
498 {
499     if (auto* cache = NetworkProcess::singleton().cache()) {
500         HashSet<RefPtr<SecurityOrigin>> originsToDelete;
501         for (auto& origin : origins)
502             originsToDelete.add(origin.securityOrigin());
503
504         Vector<NetworkCache::Key> cacheKeysToDelete;
505         cache->traverse([cache, completionHandler = WTFMove(completionHandler), originsToDelete = WTFMove(originsToDelete), cacheKeysToDelete = WTFMove(cacheKeysToDelete)](auto* traversalEntry) mutable {
506             if (traversalEntry) {
507                 if (originsToDelete.contains(SecurityOrigin::create(traversalEntry->entry.response().url())))
508                     cacheKeysToDelete.append(traversalEntry->entry.key());
509                 return;
510             }
511
512             cache->remove(cacheKeysToDelete, WTFMove(completionHandler));
513             return;
514         });
515
516         return;
517     }
518
519     RunLoop::main().dispatch(WTFMove(completionHandler));
520 }
521
522 void NetworkProcess::deleteWebsiteDataForOrigins(PAL::SessionID sessionID, OptionSet<WebsiteDataType> websiteDataTypes, const Vector<SecurityOriginData>& originDatas, const Vector<String>& cookieHostNames, uint64_t callbackID)
523 {
524     if (websiteDataTypes.contains(WebsiteDataType::Cookies)) {
525         if (auto* networkStorageSession = NetworkStorageSession::storageSession(sessionID))
526             deleteCookiesForHostnames(*networkStorageSession, cookieHostNames);
527     }
528
529     auto clearTasksHandler = WTF::CallbackAggregator::create([this, callbackID] {
530         parentProcessConnection()->send(Messages::NetworkProcessProxy::DidDeleteWebsiteDataForOrigins(callbackID), 0);
531     });
532
533     if (websiteDataTypes.contains(WebsiteDataType::DOMCache)) {
534         for (auto& originData : originDatas) {
535             auto origin = originData.securityOrigin()->toString();
536             CacheStorage::Engine::from(sessionID).clearCachesForOrigin(origin, clearTasksHandler);
537         }
538     }
539
540     if (websiteDataTypes.contains(WebsiteDataType::DiskCache) && !sessionID.isEphemeral())
541         clearDiskCacheEntries(originDatas, [clearTasksHandler = WTFMove(clearTasksHandler)] { });
542 }
543
544 void NetworkProcess::downloadRequest(PAL::SessionID sessionID, DownloadID downloadID, const ResourceRequest& request, const String& suggestedFilename)
545 {
546     downloadManager().startDownload(nullptr, sessionID, downloadID, request, suggestedFilename);
547 }
548
549 void NetworkProcess::resumeDownload(PAL::SessionID sessionID, DownloadID downloadID, const IPC::DataReference& resumeData, const String& path, WebKit::SandboxExtension::Handle&& sandboxExtensionHandle)
550 {
551     downloadManager().resumeDownload(sessionID, downloadID, resumeData, path, WTFMove(sandboxExtensionHandle));
552 }
553
554 void NetworkProcess::cancelDownload(DownloadID downloadID)
555 {
556     downloadManager().cancelDownload(downloadID);
557 }
558     
559 #if USE(PROTECTION_SPACE_AUTH_CALLBACK)
560 static uint64_t generateCanAuthenticateIdentifier()
561 {
562     static uint64_t lastLoaderID = 0;
563     return ++lastLoaderID;
564 }
565
566 void NetworkProcess::canAuthenticateAgainstProtectionSpace(NetworkResourceLoader& loader, const WebCore::ProtectionSpace& protectionSpace)
567 {
568     uint64_t loaderID = generateCanAuthenticateIdentifier();
569     m_waitingNetworkResourceLoaders.set(loaderID, loader);
570     parentProcessConnection()->send(Messages::NetworkProcessProxy::CanAuthenticateAgainstProtectionSpace(loaderID, loader.pageID(), loader.frameID(), protectionSpace), 0);
571 }
572
573 #if ENABLE(SERVER_PRECONNECT)
574 void NetworkProcess::canAuthenticateAgainstProtectionSpace(PreconnectTask& preconnectTask, const WebCore::ProtectionSpace& protectionSpace)
575 {
576     uint64_t loaderID = generateCanAuthenticateIdentifier();
577     m_waitingPreconnectTasks.set(loaderID, preconnectTask.createWeakPtr());
578     parentProcessConnection()->send(Messages::NetworkProcessProxy::CanAuthenticateAgainstProtectionSpace(loaderID, preconnectTask.pageID(), preconnectTask.frameID(), protectionSpace), 0);
579 }
580 #endif
581
582 void NetworkProcess::continueCanAuthenticateAgainstProtectionSpace(uint64_t loaderID, bool canAuthenticate)
583 {
584     if (auto resourceLoader = m_waitingNetworkResourceLoaders.take(loaderID)) {
585         resourceLoader.value()->continueCanAuthenticateAgainstProtectionSpace(canAuthenticate);
586         return;
587     }
588 #if ENABLE(SERVER_PRECONNECT)
589     if (auto preconnectTask = m_waitingPreconnectTasks.take(loaderID)) {
590         preconnectTask->continueCanAuthenticateAgainstProtectionSpace(canAuthenticate);
591         return;
592     }
593 #endif
594 }
595
596 #endif
597
598 #if USE(NETWORK_SESSION)
599 void NetworkProcess::continueWillSendRequest(DownloadID downloadID, WebCore::ResourceRequest&& request)
600 {
601     downloadManager().continueWillSendRequest(downloadID, WTFMove(request));
602 }
603
604 void NetworkProcess::pendingDownloadCanceled(DownloadID downloadID)
605 {
606     downloadProxyConnection()->send(Messages::DownloadProxy::DidCancel({ }), downloadID.downloadID());
607 }
608
609 void NetworkProcess::findPendingDownloadLocation(NetworkDataTask& networkDataTask, ResponseCompletionHandler&& completionHandler, const ResourceResponse& response)
610 {
611     uint64_t destinationID = networkDataTask.pendingDownloadID().downloadID();
612     downloadProxyConnection()->send(Messages::DownloadProxy::DidReceiveResponse(response), destinationID);
613
614     downloadManager().willDecidePendingDownloadDestination(networkDataTask, WTFMove(completionHandler));
615
616     // As per https://html.spec.whatwg.org/#as-a-download (step 2), the filename from the Content-Disposition header
617     // should override the suggested filename from the download attribute.
618     String suggestedFilename = response.isAttachmentWithFilename() ? response.suggestedFilename() : networkDataTask.suggestedFilename();
619     suggestedFilename = MIMETypeRegistry::appendFileExtensionIfNecessary(suggestedFilename, response.mimeType());
620
621     downloadProxyConnection()->send(Messages::DownloadProxy::DecideDestinationWithSuggestedFilenameAsync(networkDataTask.pendingDownloadID(), suggestedFilename), destinationID);
622 }
623 #endif
624
625 void NetworkProcess::continueDecidePendingDownloadDestination(DownloadID downloadID, String destination, SandboxExtension::Handle&& sandboxExtensionHandle, bool allowOverwrite)
626 {
627     if (destination.isEmpty())
628         downloadManager().cancelDownload(downloadID);
629     else
630         downloadManager().continueDecidePendingDownloadDestination(downloadID, destination, WTFMove(sandboxExtensionHandle), allowOverwrite);
631 }
632
633 void NetworkProcess::setCacheModel(uint32_t cm)
634 {
635     CacheModel cacheModel = static_cast<CacheModel>(cm);
636
637     if (m_hasSetCacheModel && (cacheModel == m_cacheModel))
638         return;
639
640     m_hasSetCacheModel = true;
641     m_cacheModel = cacheModel;
642
643     unsigned urlCacheMemoryCapacity = 0;
644     uint64_t urlCacheDiskCapacity = 0;
645     uint64_t diskFreeSize = 0;
646     if (WebCore::FileSystem::getVolumeFreeSpace(m_diskCacheDirectory, diskFreeSize)) {
647         // As a fudge factor, use 1000 instead of 1024, in case the reported byte
648         // count doesn't align exactly to a megabyte boundary.
649         diskFreeSize /= KB * 1000;
650         calculateURLCacheSizes(cacheModel, diskFreeSize, urlCacheMemoryCapacity, urlCacheDiskCapacity);
651     }
652
653     if (m_diskCacheSizeOverride >= 0)
654         urlCacheDiskCapacity = m_diskCacheSizeOverride;
655
656     if (m_cache) {
657         m_cache->setCapacity(urlCacheDiskCapacity);
658         return;
659     }
660
661     platformSetURLCacheSize(urlCacheMemoryCapacity, urlCacheDiskCapacity);
662 }
663
664 void NetworkProcess::setCanHandleHTTPSServerTrustEvaluation(bool value)
665 {
666     m_canHandleHTTPSServerTrustEvaluation = value;
667 }
668
669 void NetworkProcess::getNetworkProcessStatistics(uint64_t callbackID)
670 {
671     StatisticsData data;
672
673     auto& networkProcess = NetworkProcess::singleton();
674     data.statisticsNumbers.set("DownloadsActiveCount", networkProcess.downloadManager().activeDownloadCount());
675     data.statisticsNumbers.set("OutstandingAuthenticationChallengesCount", networkProcess.authenticationManager().outstandingAuthenticationChallengeCount());
676
677     parentProcessConnection()->send(Messages::WebProcessPool::DidGetStatistics(data, callbackID), 0);
678 }
679
680 void NetworkProcess::setAllowsAnySSLCertificateForWebSocket(bool allows)
681 {
682     DeprecatedGlobalSettings::setAllowsAnySSLCertificate(allows);
683 }
684
685 void NetworkProcess::logDiagnosticMessage(uint64_t webPageID, const String& message, const String& description, ShouldSample shouldSample)
686 {
687     if (!DiagnosticLoggingClient::shouldLogAfterSampling(shouldSample))
688         return;
689
690     parentProcessConnection()->send(Messages::NetworkProcessProxy::LogDiagnosticMessage(webPageID, message, description, ShouldSample::No), 0);
691 }
692
693 void NetworkProcess::logDiagnosticMessageWithResult(uint64_t webPageID, const String& message, const String& description, DiagnosticLoggingResultType result, ShouldSample shouldSample)
694 {
695     if (!DiagnosticLoggingClient::shouldLogAfterSampling(shouldSample))
696         return;
697
698     parentProcessConnection()->send(Messages::NetworkProcessProxy::LogDiagnosticMessageWithResult(webPageID, message, description, result, ShouldSample::No), 0);
699 }
700
701 void NetworkProcess::logDiagnosticMessageWithValue(uint64_t webPageID, const String& message, const String& description, double value, unsigned significantFigures, ShouldSample shouldSample)
702 {
703     if (!DiagnosticLoggingClient::shouldLogAfterSampling(shouldSample))
704         return;
705
706     parentProcessConnection()->send(Messages::NetworkProcessProxy::LogDiagnosticMessageWithValue(webPageID, message, description, value, significantFigures, ShouldSample::No), 0);
707 }
708
709 void NetworkProcess::terminate()
710 {
711 #if ENABLE(NETWORK_CAPTURE)
712     NetworkCapture::Manager::singleton().terminate();
713 #endif
714
715     platformTerminate();
716     ChildProcess::terminate();
717 }
718
719 // FIXME: We can remove this one by adapting RefCounter.
720 class TaskCounter : public RefCounted<TaskCounter> {
721 public:
722     explicit TaskCounter(Function<void()>&& callback) : m_callback(WTFMove(callback)) { }
723     ~TaskCounter() { m_callback(); };
724
725 private:
726     Function<void()> m_callback;
727 };
728
729 void NetworkProcess::actualPrepareToSuspend(ShouldAcknowledgeWhenReadyToSuspend shouldAcknowledgeWhenReadyToSuspend)
730 {
731     lowMemoryHandler(Critical::Yes);
732
733     RefPtr<TaskCounter> delayedTaskCounter;
734     if (shouldAcknowledgeWhenReadyToSuspend == ShouldAcknowledgeWhenReadyToSuspend::Yes) {
735         delayedTaskCounter = adoptRef(new TaskCounter([this] {
736             RELEASE_LOG(ProcessSuspension, "%p - NetworkProcess::notifyProcessReadyToSuspend() Sending ProcessReadyToSuspend IPC message", this);
737             if (parentProcessConnection())
738                 parentProcessConnection()->send(Messages::NetworkProcessProxy::ProcessReadyToSuspend(), 0);
739         }));
740     }
741
742     for (auto& connection : m_webProcessConnections)
743         connection->cleanupForSuspension([delayedTaskCounter] { });
744 }
745
746 void NetworkProcess::processWillSuspendImminently(bool& handled)
747 {
748     actualPrepareToSuspend(ShouldAcknowledgeWhenReadyToSuspend::No);
749     handled = true;
750 }
751
752 void NetworkProcess::prepareToSuspend()
753 {
754     RELEASE_LOG(ProcessSuspension, "%p - NetworkProcess::prepareToSuspend()", this);
755     actualPrepareToSuspend(ShouldAcknowledgeWhenReadyToSuspend::Yes);
756 }
757
758 void NetworkProcess::cancelPrepareToSuspend()
759 {
760     // Although it is tempting to send a NetworkProcessProxy::DidCancelProcessSuspension message from here
761     // we do not because prepareToSuspend() already replied with a NetworkProcessProxy::ProcessReadyToSuspend
762     // message. And NetworkProcessProxy expects to receive either a NetworkProcessProxy::ProcessReadyToSuspend-
763     // or NetworkProcessProxy::DidCancelProcessSuspension- message, but not both.
764     RELEASE_LOG(ProcessSuspension, "%p - NetworkProcess::cancelPrepareToSuspend()", this);
765     for (auto& connection : m_webProcessConnections)
766         connection->endSuspension();
767 }
768
769 void NetworkProcess::processDidResume()
770 {
771     RELEASE_LOG(ProcessSuspension, "%p - NetworkProcess::processDidResume()", this);
772     for (auto& connection : m_webProcessConnections)
773         connection->endSuspension();
774 }
775
776 void NetworkProcess::prefetchDNS(const String& hostname)
777 {
778     WebCore::prefetchDNS(hostname);
779 }
780
781 String NetworkProcess::cacheStorageDirectory(PAL::SessionID sessionID) const
782 {
783     if (sessionID.isEphemeral())
784         return { };
785
786     if (sessionID == PAL::SessionID::defaultSessionID())
787         return m_cacheStorageDirectory;
788
789     auto* session = NetworkStorageSession::storageSession(sessionID);
790     if (!session)
791         return { };
792
793     return session->cacheStorageDirectory();
794 }
795
796 void NetworkProcess::preconnectTo(const WebCore::URL& url, WebCore::StoredCredentialsPolicy storedCredentialsPolicy)
797 {
798 #if ENABLE(SERVER_PRECONNECT)
799     NetworkLoadParameters parameters;
800     parameters.request = ResourceRequest { url };
801     parameters.sessionID = PAL::SessionID::defaultSessionID();
802     parameters.storedCredentialsPolicy = storedCredentialsPolicy;
803     parameters.shouldPreconnectOnly = PreconnectOnly::Yes;
804
805     new PreconnectTask(WTFMove(parameters));
806 #else
807     UNUSED_PARAM(url);
808     UNUSED_PARAM(storedCredentialsPolicy);
809 #endif
810 }
811
812 uint64_t NetworkProcess::cacheStoragePerOriginQuota() const
813 {
814     return m_cacheStoragePerOriginQuota;
815 }
816
817 #if !PLATFORM(COCOA)
818 void NetworkProcess::initializeProcess(const ChildProcessInitializationParameters&)
819 {
820 }
821
822 void NetworkProcess::initializeProcessName(const ChildProcessInitializationParameters&)
823 {
824 }
825
826 void NetworkProcess::initializeSandbox(const ChildProcessInitializationParameters&, SandboxInitializationParameters&)
827 {
828 }
829
830 void NetworkProcess::syncAllCookies()
831 {
832 }
833
834 #endif
835
836 } // namespace WebKit