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