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