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