2 * Copyright (C) 2012-2018 Apple Inc. All rights reserved.
3 * Copyright (C) 2018 Sony Interactive Entertainment Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
28 #include "NetworkProcess.h"
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"
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>
84 #if ENABLE(SEC_ITEM_SHIM)
85 #include "SecItemShim.h"
88 #include "NetworkCache.h"
89 #include "NetworkCacheCoders.h"
91 #if ENABLE(NETWORK_CAPTURE)
92 #include "NetworkCaptureManager.h"
96 #include "NetworkSessionCocoa.h"
99 using namespace WebCore;
103 NetworkProcess& NetworkProcess::singleton()
105 static NeverDestroyed<NetworkProcess> networkProcess;
106 return networkProcess;
109 NetworkProcess::NetworkProcess()
110 : m_hasSetCacheModel(false)
111 , m_cacheModel(CacheModelDocumentViewer)
112 , m_diskCacheIsDisabledForTesting(false)
113 , m_canHandleHTTPSServerTrustEvaluation(true)
115 , m_clearCacheDispatchGroup(0)
118 NetworkProcessPlatformStrategies::initialize();
120 addSupplement<AuthenticationManager>();
121 addSupplement<WebCookieManager>();
122 #if ENABLE(LEGACY_CUSTOM_PROTOCOL_MANAGER)
123 addSupplement<LegacyCustomProtocolManager>();
126 NetworkStateNotifier::singleton().addListener([this](bool isOnLine) {
127 auto webProcessConnections = m_webProcessConnections;
128 for (auto& webProcessConnection : webProcessConnections)
129 webProcessConnection->setOnLineState(isOnLine);
133 NetworkProcess::~NetworkProcess()
137 AuthenticationManager& NetworkProcess::authenticationManager()
139 return *supplement<AuthenticationManager>();
142 DownloadManager& NetworkProcess::downloadManager()
144 static NeverDestroyed<DownloadManager> downloadManager(*this);
145 return downloadManager;
148 void NetworkProcess::removeNetworkConnectionToWebProcess(NetworkConnectionToWebProcess* connection)
150 size_t vectorIndex = m_webProcessConnections.find(connection);
151 ASSERT(vectorIndex != notFound);
153 m_webProcessConnections.remove(vectorIndex);
156 bool NetworkProcess::shouldTerminate()
158 // Network process keeps session cookies and credentials, so it should never terminate (as long as UI process connection is alive).
162 void NetworkProcess::didReceiveMessage(IPC::Connection& connection, IPC::Decoder& decoder)
164 if (messageReceiverMap().dispatchMessage(connection, decoder))
167 if (decoder.messageReceiverName() == Messages::ChildProcess::messageReceiverName()) {
168 ChildProcess::didReceiveMessage(connection, decoder);
172 #if ENABLE(CONTENT_EXTENSIONS)
173 if (decoder.messageReceiverName() == Messages::NetworkContentRuleListManager::messageReceiverName()) {
174 m_NetworkContentRuleListManager.didReceiveMessage(connection, decoder);
179 didReceiveNetworkProcessMessage(connection, decoder);
182 void NetworkProcess::didReceiveSyncMessage(IPC::Connection& connection, IPC::Decoder& decoder, std::unique_ptr<IPC::Encoder>& replyEncoder)
184 if (messageReceiverMap().dispatchSyncMessage(connection, decoder, replyEncoder))
187 didReceiveSyncNetworkProcessMessage(connection, decoder, replyEncoder);
190 void NetworkProcess::didCreateDownload()
192 disableTermination();
195 void NetworkProcess::didDestroyDownload()
200 IPC::Connection* NetworkProcess::downloadProxyConnection()
202 return parentProcessConnection();
205 AuthenticationManager& NetworkProcess::downloadsAuthenticationManager()
207 return authenticationManager();
210 void NetworkProcess::lowMemoryHandler(Critical critical)
212 if (m_suppressMemoryPressureHandler)
215 WTF::releaseFastMallocFreeMemory();
218 void NetworkProcess::initializeNetworkProcess(NetworkProcessCreationParameters&& parameters)
220 #if HAVE(SEC_KEY_PROXY)
221 WTF::setProcessPrivileges({ ProcessPrivilege::CanAccessRawCookies });
223 WTF::setProcessPrivileges({ ProcessPrivilege::CanAccessRawCookies, ProcessPrivilege::CanAccessCredentials });
225 WebCore::NetworkStorageSession::permitProcessToUseCookieAPI(true);
226 WebCore::setPresentingApplicationPID(parameters.presentingApplicationPID);
227 platformInitializeNetworkProcess(parameters);
229 WTF::Thread::setCurrentThreadIsUserInitiated();
230 AtomicString::init();
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);
239 memoryPressureHandler.install();
242 #if ENABLE(NETWORK_CAPTURE)
243 NetworkCapture::Manager::singleton().initialize(
244 parameters.recordReplayMode,
245 parameters.recordReplayCacheLocation);
248 m_diskCacheIsDisabledForTesting = parameters.shouldUseTestingNetworkSession;
250 m_diskCacheSizeOverride = parameters.diskCacheSizeOverride;
251 setCacheModel(static_cast<uint32_t>(parameters.cacheModel));
253 setCanHandleHTTPSServerTrustEvaluation(parameters.canHandleHTTPSServerTrustEvaluation);
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());
259 if (parameters.shouldUseTestingNetworkSession)
260 NetworkStorageSession::switchToNewTestingSession();
262 #if HAVE(CFNETWORK_STORAGE_PARTITIONING) && !RELEASE_LOG_DISABLED
263 m_logCookieInformation = parameters.logCookieInformation;
266 SessionTracker::setSession(PAL::SessionID::defaultSessionID(), NetworkSession::create(WTFMove(parameters.defaultSessionParameters)));
268 for (auto& supplement : m_supplements.values())
269 supplement->initialize(parameters);
271 for (auto& scheme : parameters.urlSchemesRegisteredAsSecure)
272 registerURLSchemeAsSecure(scheme);
274 for (auto& scheme : parameters.urlSchemesRegisteredAsBypassingContentSecurityPolicy)
275 registerURLSchemeAsBypassingContentSecurityPolicy(scheme);
277 for (auto& scheme : parameters.urlSchemesRegisteredAsLocal)
278 registerURLSchemeAsLocal(scheme);
280 for (auto& scheme : parameters.urlSchemesRegisteredAsNoAccess)
281 registerURLSchemeAsNoAccess(scheme);
283 for (auto& scheme : parameters.urlSchemesRegisteredAsDisplayIsolated)
284 registerURLSchemeAsDisplayIsolated(scheme);
286 for (auto& scheme : parameters.urlSchemesRegisteredAsCORSEnabled)
287 registerURLSchemeAsCORSEnabled(scheme);
289 for (auto& scheme : parameters.urlSchemesRegisteredAsCanDisplayOnlyIfCanRequest)
290 registerURLSchemeAsCanDisplayOnlyIfCanRequest(scheme);
292 m_tracksResourceLoadMilestones = parameters.tracksResourceLoadMilestones;
294 RELEASE_LOG(Process, "%p - NetworkProcess::initializeNetworkProcess: Presenting process = %d", this, WebCore::presentingApplicationPID());
297 void NetworkProcess::initializeConnection(IPC::Connection* connection)
299 ChildProcess::initializeConnection(connection);
301 for (auto& supplement : m_supplements.values())
302 supplement->initializeConnection(connection);
305 void NetworkProcess::createNetworkConnectionToWebProcess()
307 #if USE(UNIX_DOMAIN_SOCKETS)
308 IPC::Connection::SocketPair socketPair = IPC::Connection::createPlatformConnection();
310 auto connection = NetworkConnectionToWebProcess::create(socketPair.server);
311 m_webProcessConnections.append(WTFMove(connection));
313 IPC::Attachment clientSocket(socketPair.client);
314 parentProcessConnection()->send(Messages::NetworkProcessProxy::DidCreateNetworkConnectionToWebProcess(clientSocket), 0);
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);
324 // Create a listening connection.
325 auto connection = NetworkConnectionToWebProcess::create(IPC::Connection::Identifier(listeningPort));
326 m_webProcessConnections.append(WTFMove(connection));
328 IPC::Attachment clientPort(listeningPort, MACH_MSG_TYPE_MAKE_SEND);
329 parentProcessConnection()->send(Messages::NetworkProcessProxy::DidCreateNetworkConnectionToWebProcess(clientPort), 0);
331 IPC::Connection::Identifier serverIdentifier, clientIdentifier;
332 if (!IPC::Connection::createServerAndClientIdentifiers(serverIdentifier, clientIdentifier))
335 auto connection = NetworkConnectionToWebProcess::create(serverIdentifier);
336 m_webProcessConnections.append(WTFMove(connection));
338 IPC::Attachment clientSocket(clientIdentifier);
339 parentProcessConnection()->send(Messages::NetworkProcessProxy::DidCreateNetworkConnectionToWebProcess(clientSocket), 0);
344 if (!m_webProcessConnections.isEmpty())
345 m_webProcessConnections.last()->setOnLineState(NetworkStateNotifier::singleton().onLine());
348 void NetworkProcess::clearCachedCredentials()
350 NetworkStorageSession::defaultStorageSession().credentialStorage().clearCredentials();
351 if (auto* networkSession = SessionTracker::networkSession(PAL::SessionID::defaultSessionID()))
352 networkSession->clearCredentials();
354 ASSERT_NOT_REACHED();
357 void NetworkProcess::addWebsiteDataStore(WebsiteDataStoreParameters&& parameters)
359 RemoteNetworkingContext::ensureWebsiteDataStoreSession(WTFMove(parameters));
362 void NetworkProcess::destroySession(PAL::SessionID sessionID)
364 SessionTracker::destroySession(sessionID);
365 m_sessionsControlledByAutomation.remove(sessionID);
366 CacheStorage::Engine::destroyEngine(sessionID);
369 void NetworkProcess::grantSandboxExtensionsToStorageProcessForBlobs(const Vector<String>& filenames, Function<void ()>&& completionHandler)
371 static uint64_t lastRequestID;
373 uint64_t requestID = ++lastRequestID;
374 m_sandboxExtensionForBlobsCompletionHandlers.set(requestID, WTFMove(completionHandler));
375 parentProcessConnection()->send(Messages::NetworkProcessProxy::GrantSandboxExtensionsToStorageProcessForBlobs(requestID, filenames), 0);
378 void NetworkProcess::didGrantSandboxExtensionsToStorageProcessForBlobs(uint64_t requestID)
380 if (auto handler = m_sandboxExtensionForBlobsCompletionHandlers.take(requestID))
384 void NetworkProcess::writeBlobToFilePath(const WebCore::URL& url, const String& path, SandboxExtension::Handle&& handleForWriting, uint64_t requestID)
386 auto extension = SandboxExtension::create(WTFMove(handleForWriting));
388 parentProcessConnection()->send(Messages::NetworkProcessProxy::DidWriteBlobToFilePath(false, requestID), 0);
392 extension->consume();
393 NetworkBlobRegistry::singleton().writeBlobToFilePath(url, path, [this, extension = WTFMove(extension), requestID] (bool success) {
395 parentProcessConnection()->send(Messages::NetworkProcessProxy::DidWriteBlobToFilePath(success, requestID), 0);
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)
402 if (auto* networkStorageSession = NetworkStorageSession::storageSession(sessionID))
403 networkStorageSession->setPrevalentDomainsToPartitionOrBlockCookies(domainsToPartition, domainsToBlock, domainsToNeitherPartitionNorBlock, shouldClearFirst);
406 void NetworkProcess::hasStorageAccessForFrame(PAL::SessionID sessionID, const String& resourceDomain, const String& firstPartyDomain, uint64_t frameID, uint64_t pageID, uint64_t contextId)
408 if (auto* networkStorageSession = NetworkStorageSession::storageSession(sessionID))
409 parentProcessConnection()->send(Messages::NetworkProcessProxy::StorageAccessRequestResult(networkStorageSession->hasStorageAccess(resourceDomain, firstPartyDomain, frameID, pageID), contextId), 0);
411 ASSERT_NOT_REACHED();
414 void NetworkProcess::getAllStorageAccessEntries(PAL::SessionID sessionID, uint64_t contextId)
416 if (auto* networkStorageSession = NetworkStorageSession::storageSession(sessionID))
417 parentProcessConnection()->send(Messages::NetworkProcessProxy::AllStorageAccessEntriesResult(networkStorageSession->getAllStorageAccessEntries(), contextId), 0);
419 ASSERT_NOT_REACHED();
422 void NetworkProcess::grantStorageAccess(PAL::SessionID sessionID, const String& resourceDomain, const String& firstPartyDomain, std::optional<uint64_t> frameID, uint64_t pageID, uint64_t contextId)
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;
430 ASSERT_NOT_REACHED();
432 parentProcessConnection()->send(Messages::NetworkProcessProxy::StorageAccessRequestResult(isStorageGranted, contextId), 0);
435 void NetworkProcess::removeAllStorageAccess(PAL::SessionID sessionID)
437 if (auto* networkStorageSession = NetworkStorageSession::storageSession(sessionID))
438 networkStorageSession->removeAllStorageAccess();
440 ASSERT_NOT_REACHED();
443 void NetworkProcess::removePrevalentDomains(PAL::SessionID sessionID, const Vector<String>& domains)
445 if (auto* networkStorageSession = NetworkStorageSession::storageSession(sessionID))
446 networkStorageSession->removePrevalentDomains(domains);
450 bool NetworkProcess::sessionIsControlledByAutomation(PAL::SessionID sessionID) const
452 return m_sessionsControlledByAutomation.contains(sessionID);
455 void NetworkProcess::setSessionIsControlledByAutomation(PAL::SessionID sessionID, bool controlled)
458 m_sessionsControlledByAutomation.add(sessionID);
460 m_sessionsControlledByAutomation.remove(sessionID);
463 static void fetchDiskCacheEntries(PAL::SessionID sessionID, OptionSet<WebsiteDataFetchOption> fetchOptions, Function<void (Vector<WebsiteData::Entry>)>&& completionHandler)
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;
471 for (auto& originAndSize : originsAndSizes)
472 entries.append(WebsiteData::Entry { originAndSize.key, WebsiteDataType::DiskCache, originAndSize.value });
474 RunLoop::main().dispatch([completionHandler = WTFMove(completionHandler), entries = WTFMove(entries)] {
475 completionHandler(entries);
481 auto url = traversalEntry->entry.response().url();
482 auto result = originsAndSizes.add({url.protocol().toString(), url.host().toString(), url.port()}, 0);
484 if (fetchOptions.contains(WebsiteDataFetchOption::ComputeSizes))
485 result.iterator->value += traversalEntry->entry.sourceStorageRecord().header.size() + traversalEntry->recordInfo.bodySize;
491 RunLoop::main().dispatch([completionHandler = WTFMove(completionHandler)] {
492 completionHandler({ });
496 void NetworkProcess::fetchWebsiteData(PAL::SessionID sessionID, OptionSet<WebsiteDataType> websiteDataTypes, OptionSet<WebsiteDataFetchOption> fetchOptions, uint64_t callbackID)
498 struct CallbackAggregator final : public RefCounted<CallbackAggregator> {
499 explicit CallbackAggregator(Function<void (WebsiteData)>&& completionHandler)
500 : m_completionHandler(WTFMove(completionHandler))
504 ~CallbackAggregator()
506 ASSERT(RunLoop::isMain());
508 RunLoop::main().dispatch([completionHandler = WTFMove(m_completionHandler), websiteData = WTFMove(m_websiteData)] {
509 completionHandler(websiteData);
513 Function<void (WebsiteData)> m_completionHandler;
514 WebsiteData m_websiteData;
517 auto callbackAggregator = adoptRef(*new CallbackAggregator([this, callbackID] (WebsiteData websiteData) {
518 parentProcessConnection()->send(Messages::NetworkProcessProxy::DidFetchWebsiteData(callbackID, websiteData), 0);
521 if (websiteDataTypes.contains(WebsiteDataType::Cookies)) {
522 if (auto* networkStorageSession = NetworkStorageSession::storageSession(sessionID))
523 getHostnamesWithCookies(*networkStorageSession, callbackAggregator->m_websiteData.hostNamesWithCookies);
526 if (websiteDataTypes.contains(WebsiteDataType::Credentials)) {
527 if (NetworkStorageSession::storageSession(sessionID))
528 callbackAggregator->m_websiteData.originsWithCredentials = NetworkStorageSession::storageSession(sessionID)->credentialStorage().originsWithCredentials();
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);
537 if (websiteDataTypes.contains(WebsiteDataType::DiskCache)) {
538 fetchDiskCacheEntries(sessionID, fetchOptions, [callbackAggregator = WTFMove(callbackAggregator)](auto entries) mutable {
539 callbackAggregator->m_websiteData.entries.appendVector(entries);
544 void NetworkProcess::deleteWebsiteData(PAL::SessionID sessionID, OptionSet<WebsiteDataType> websiteDataTypes, WallTime modifiedSince, uint64_t callbackID)
547 if (websiteDataTypes.contains(WebsiteDataType::HSTSCache)) {
548 if (auto* networkStorageSession = NetworkStorageSession::storageSession(sessionID))
549 clearHSTSCache(*networkStorageSession, modifiedSince);
553 if (websiteDataTypes.contains(WebsiteDataType::Cookies)) {
554 if (auto* networkStorageSession = NetworkStorageSession::storageSession(sessionID))
555 deleteAllCookiesModifiedSince(*networkStorageSession, modifiedSince);
558 if (websiteDataTypes.contains(WebsiteDataType::Credentials)) {
559 if (NetworkStorageSession::storageSession(sessionID))
560 NetworkStorageSession::storageSession(sessionID)->credentialStorage().clearCredentials();
563 auto clearTasksHandler = WTF::CallbackAggregator::create([this, callbackID] {
564 parentProcessConnection()->send(Messages::NetworkProcessProxy::DidDeleteWebsiteData(callbackID), 0);
567 if (websiteDataTypes.contains(WebsiteDataType::DOMCache))
568 CacheStorage::Engine::from(sessionID).clearAllCaches(clearTasksHandler);
570 if (websiteDataTypes.contains(WebsiteDataType::DiskCache) && !sessionID.isEphemeral())
571 clearDiskCache(modifiedSince, [clearTasksHandler = WTFMove(clearTasksHandler)] { });
574 static void clearDiskCacheEntries(const Vector<SecurityOriginData>& origins, Function<void ()>&& completionHandler)
576 if (auto* cache = NetworkProcess::singleton().cache()) {
577 HashSet<RefPtr<SecurityOrigin>> originsToDelete;
578 for (auto& origin : origins)
579 originsToDelete.add(origin.securityOrigin());
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());
589 cache->remove(cacheKeysToDelete, WTFMove(completionHandler));
596 RunLoop::main().dispatch(WTFMove(completionHandler));
599 void NetworkProcess::deleteWebsiteDataForOrigins(PAL::SessionID sessionID, OptionSet<WebsiteDataType> websiteDataTypes, const Vector<SecurityOriginData>& originDatas, const Vector<String>& cookieHostNames, uint64_t callbackID)
601 if (websiteDataTypes.contains(WebsiteDataType::Cookies)) {
602 if (auto* networkStorageSession = NetworkStorageSession::storageSession(sessionID))
603 deleteCookiesForHostnames(*networkStorageSession, cookieHostNames);
606 auto clearTasksHandler = WTF::CallbackAggregator::create([this, callbackID] {
607 parentProcessConnection()->send(Messages::NetworkProcessProxy::DidDeleteWebsiteDataForOrigins(callbackID), 0);
610 if (websiteDataTypes.contains(WebsiteDataType::DOMCache)) {
611 for (auto& originData : originDatas)
612 CacheStorage::Engine::from(sessionID).clearCachesForOrigin(originData, clearTasksHandler);
615 if (websiteDataTypes.contains(WebsiteDataType::DiskCache) && !sessionID.isEphemeral())
616 clearDiskCacheEntries(originDatas, [clearTasksHandler = WTFMove(clearTasksHandler)] { });
619 void NetworkProcess::downloadRequest(PAL::SessionID sessionID, DownloadID downloadID, const ResourceRequest& request, const String& suggestedFilename)
621 downloadManager().startDownload(nullptr, sessionID, downloadID, request, suggestedFilename);
624 void NetworkProcess::resumeDownload(PAL::SessionID sessionID, DownloadID downloadID, const IPC::DataReference& resumeData, const String& path, WebKit::SandboxExtension::Handle&& sandboxExtensionHandle)
626 downloadManager().resumeDownload(sessionID, downloadID, resumeData, path, WTFMove(sandboxExtensionHandle));
629 void NetworkProcess::cancelDownload(DownloadID downloadID)
631 downloadManager().cancelDownload(downloadID);
634 #if USE(PROTECTION_SPACE_AUTH_CALLBACK)
635 static uint64_t generateCanAuthenticateIdentifier()
637 static uint64_t lastLoaderID = 0;
638 return ++lastLoaderID;
641 void NetworkProcess::canAuthenticateAgainstProtectionSpace(NetworkResourceLoader& loader, const WebCore::ProtectionSpace& protectionSpace)
643 uint64_t loaderID = generateCanAuthenticateIdentifier();
644 m_waitingNetworkResourceLoaders.set(loaderID, loader);
645 parentProcessConnection()->send(Messages::NetworkProcessProxy::CanAuthenticateAgainstProtectionSpace(loaderID, loader.pageID(), loader.frameID(), protectionSpace), 0);
648 #if ENABLE(SERVER_PRECONNECT)
649 void NetworkProcess::canAuthenticateAgainstProtectionSpace(PreconnectTask& preconnectTask, const WebCore::ProtectionSpace& protectionSpace)
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);
657 void NetworkProcess::continueCanAuthenticateAgainstProtectionSpace(uint64_t loaderID, bool canAuthenticate)
659 if (auto resourceLoader = m_waitingNetworkResourceLoaders.take(loaderID)) {
660 resourceLoader.value()->continueCanAuthenticateAgainstProtectionSpace(canAuthenticate);
663 #if ENABLE(SERVER_PRECONNECT)
664 if (auto preconnectTask = m_waitingPreconnectTasks.take(loaderID)) {
665 preconnectTask->continueCanAuthenticateAgainstProtectionSpace(canAuthenticate);
673 void NetworkProcess::continueWillSendRequest(DownloadID downloadID, WebCore::ResourceRequest&& request)
675 downloadManager().continueWillSendRequest(downloadID, WTFMove(request));
678 void NetworkProcess::pendingDownloadCanceled(DownloadID downloadID)
680 downloadProxyConnection()->send(Messages::DownloadProxy::DidCancel({ }), downloadID.downloadID());
683 void NetworkProcess::findPendingDownloadLocation(NetworkDataTask& networkDataTask, ResponseCompletionHandler&& completionHandler, const ResourceResponse& response)
685 uint64_t destinationID = networkDataTask.pendingDownloadID().downloadID();
686 downloadProxyConnection()->send(Messages::DownloadProxy::DidReceiveResponse(response), destinationID);
688 downloadManager().willDecidePendingDownloadDestination(networkDataTask, WTFMove(completionHandler));
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());
695 downloadProxyConnection()->send(Messages::DownloadProxy::DecideDestinationWithSuggestedFilenameAsync(networkDataTask.pendingDownloadID(), suggestedFilename), destinationID);
698 void NetworkProcess::continueDecidePendingDownloadDestination(DownloadID downloadID, String destination, SandboxExtension::Handle&& sandboxExtensionHandle, bool allowOverwrite)
700 if (destination.isEmpty())
701 downloadManager().cancelDownload(downloadID);
703 downloadManager().continueDecidePendingDownloadDestination(downloadID, destination, WTFMove(sandboxExtensionHandle), allowOverwrite);
706 void NetworkProcess::setCacheModel(uint32_t cm)
708 CacheModel cacheModel = static_cast<CacheModel>(cm);
710 if (m_hasSetCacheModel && (cacheModel == m_cacheModel))
713 m_hasSetCacheModel = true;
714 m_cacheModel = cacheModel;
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);
726 if (m_diskCacheSizeOverride >= 0)
727 urlCacheDiskCapacity = m_diskCacheSizeOverride;
730 m_cache->setCapacity(urlCacheDiskCapacity);
733 void NetworkProcess::setCanHandleHTTPSServerTrustEvaluation(bool value)
735 m_canHandleHTTPSServerTrustEvaluation = value;
738 void NetworkProcess::getNetworkProcessStatistics(uint64_t callbackID)
742 auto& networkProcess = NetworkProcess::singleton();
743 data.statisticsNumbers.set("DownloadsActiveCount", networkProcess.downloadManager().activeDownloadCount());
744 data.statisticsNumbers.set("OutstandingAuthenticationChallengesCount", networkProcess.authenticationManager().outstandingAuthenticationChallengeCount());
746 parentProcessConnection()->send(Messages::WebProcessPool::DidGetStatistics(data, callbackID), 0);
749 void NetworkProcess::setAllowsAnySSLCertificateForWebSocket(bool allows)
751 DeprecatedGlobalSettings::setAllowsAnySSLCertificate(allows);
754 void NetworkProcess::logDiagnosticMessage(uint64_t webPageID, const String& message, const String& description, ShouldSample shouldSample)
756 if (!DiagnosticLoggingClient::shouldLogAfterSampling(shouldSample))
759 parentProcessConnection()->send(Messages::NetworkProcessProxy::LogDiagnosticMessage(webPageID, message, description, ShouldSample::No), 0);
762 void NetworkProcess::logDiagnosticMessageWithResult(uint64_t webPageID, const String& message, const String& description, DiagnosticLoggingResultType result, ShouldSample shouldSample)
764 if (!DiagnosticLoggingClient::shouldLogAfterSampling(shouldSample))
767 parentProcessConnection()->send(Messages::NetworkProcessProxy::LogDiagnosticMessageWithResult(webPageID, message, description, result, ShouldSample::No), 0);
770 void NetworkProcess::logDiagnosticMessageWithValue(uint64_t webPageID, const String& message, const String& description, double value, unsigned significantFigures, ShouldSample shouldSample)
772 if (!DiagnosticLoggingClient::shouldLogAfterSampling(shouldSample))
775 parentProcessConnection()->send(Messages::NetworkProcessProxy::LogDiagnosticMessageWithValue(webPageID, message, description, value, significantFigures, ShouldSample::No), 0);
778 void NetworkProcess::terminate()
780 #if ENABLE(NETWORK_CAPTURE)
781 NetworkCapture::Manager::singleton().terminate();
785 ChildProcess::terminate();
788 // FIXME: We can remove this one by adapting RefCounter.
789 class TaskCounter : public RefCounted<TaskCounter> {
791 explicit TaskCounter(Function<void()>&& callback) : m_callback(WTFMove(callback)) { }
792 ~TaskCounter() { m_callback(); };
795 Function<void()> m_callback;
798 void NetworkProcess::actualPrepareToSuspend(ShouldAcknowledgeWhenReadyToSuspend shouldAcknowledgeWhenReadyToSuspend)
800 platformPrepareToSuspend();
801 lowMemoryHandler(Critical::Yes);
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);
812 for (auto& connection : m_webProcessConnections)
813 connection->cleanupForSuspension([delayedTaskCounter] { });
816 void NetworkProcess::processWillSuspendImminently(bool& handled)
818 actualPrepareToSuspend(ShouldAcknowledgeWhenReadyToSuspend::No);
822 void NetworkProcess::prepareToSuspend()
824 RELEASE_LOG(ProcessSuspension, "%p - NetworkProcess::prepareToSuspend()", this);
825 actualPrepareToSuspend(ShouldAcknowledgeWhenReadyToSuspend::Yes);
828 void NetworkProcess::cancelPrepareToSuspend()
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 platformProcessDidResume();
836 for (auto& connection : m_webProcessConnections)
837 connection->endSuspension();
840 void NetworkProcess::processDidResume()
842 RELEASE_LOG(ProcessSuspension, "%p - NetworkProcess::processDidResume()", this);
843 platformProcessDidResume();
844 for (auto& connection : m_webProcessConnections)
845 connection->endSuspension();
848 void NetworkProcess::prefetchDNS(const String& hostname)
850 WebCore::prefetchDNS(hostname);
853 String NetworkProcess::cacheStorageDirectory(PAL::SessionID sessionID) const
855 if (sessionID.isEphemeral())
858 if (sessionID == PAL::SessionID::defaultSessionID())
859 return m_cacheStorageDirectory;
861 auto* session = NetworkStorageSession::storageSession(sessionID);
865 return session->cacheStorageDirectory();
868 void NetworkProcess::preconnectTo(const WebCore::URL& url, WebCore::StoredCredentialsPolicy storedCredentialsPolicy)
870 #if ENABLE(SERVER_PRECONNECT)
871 NetworkLoadParameters parameters;
872 parameters.request = ResourceRequest { url };
873 parameters.sessionID = PAL::SessionID::defaultSessionID();
874 parameters.storedCredentialsPolicy = storedCredentialsPolicy;
875 parameters.shouldPreconnectOnly = PreconnectOnly::Yes;
877 new PreconnectTask(WTFMove(parameters));
880 UNUSED_PARAM(storedCredentialsPolicy);
884 uint64_t NetworkProcess::cacheStoragePerOriginQuota() const
886 return m_cacheStoragePerOriginQuota;
889 void NetworkProcess::registerURLSchemeAsSecure(const String& scheme) const
891 SchemeRegistry::registerURLSchemeAsSecure(scheme);
894 void NetworkProcess::registerURLSchemeAsBypassingContentSecurityPolicy(const String& scheme) const
896 SchemeRegistry::registerURLSchemeAsBypassingContentSecurityPolicy(scheme);
899 void NetworkProcess::registerURLSchemeAsLocal(const String& scheme) const
901 SchemeRegistry::registerURLSchemeAsLocal(scheme);
904 void NetworkProcess::registerURLSchemeAsNoAccess(const String& scheme) const
906 SchemeRegistry::registerURLSchemeAsNoAccess(scheme);
909 void NetworkProcess::registerURLSchemeAsDisplayIsolated(const String& scheme) const
911 SchemeRegistry::registerURLSchemeAsDisplayIsolated(scheme);
914 void NetworkProcess::registerURLSchemeAsCORSEnabled(const String& scheme) const
916 SchemeRegistry::registerURLSchemeAsCORSEnabled(scheme);
919 void NetworkProcess::registerURLSchemeAsCanDisplayOnlyIfCanRequest(const String& scheme) const
921 SchemeRegistry::registerAsCanDisplayOnlyIfCanRequest(scheme);
924 void NetworkProcess::didSyncAllCookies()
926 parentProcessConnection()->send(Messages::NetworkProcessProxy::DidSyncAllCookies(), 0);
930 void NetworkProcess::initializeProcess(const ChildProcessInitializationParameters&)
934 void NetworkProcess::initializeProcessName(const ChildProcessInitializationParameters&)
938 void NetworkProcess::initializeSandbox(const ChildProcessInitializationParameters&, SandboxInitializationParameters&)
942 void NetworkProcess::syncAllCookies()
948 } // namespace WebKit