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