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