Move URL from WebCore to WTF
[WebKit-https.git] / Source / WebKit / UIProcess / Network / NetworkProcessProxy.cpp
1 /*
2  * Copyright (C) 2012-2018 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 "NetworkProcessProxy.h"
28
29 #include "APIContentRuleList.h"
30 #include "AuthenticationChallengeProxy.h"
31 #include "DownloadProxyMessages.h"
32 #if ENABLE(LEGACY_CUSTOM_PROTOCOL_MANAGER)
33 #include "LegacyCustomProtocolManagerProxyMessages.h"
34 #endif
35 #include "Logging.h"
36 #include "NetworkContentRuleListManagerMessages.h"
37 #include "NetworkProcessCreationParameters.h"
38 #include "NetworkProcessMessages.h"
39 #include "SandboxExtension.h"
40 #include "WebCompiledContentRuleList.h"
41 #include "WebPageProxy.h"
42 #include "WebProcessMessages.h"
43 #include "WebProcessPool.h"
44 #include "WebUserContentControllerProxy.h"
45 #include "WebsiteData.h"
46 #include <wtf/CompletionHandler.h>
47
48 #if ENABLE(SEC_ITEM_SHIM)
49 #include "SecItemShimProxy.h"
50 #endif
51
52 #if PLATFORM(IOS_FAMILY)
53 #include <wtf/spi/darwin/XPCSPI.h>
54 #endif
55
56 #define MESSAGE_CHECK(assertion) MESSAGE_CHECK_BASE(assertion, connection())
57
58 namespace WebKit {
59 using namespace WebCore;
60
61 static uint64_t generateCallbackID()
62 {
63     static uint64_t callbackID;
64
65     return ++callbackID;
66 }
67
68 NetworkProcessProxy::NetworkProcessProxy(WebProcessPool& processPool)
69     : ChildProcessProxy(processPool.alwaysRunsAtBackgroundPriority())
70     , m_processPool(processPool)
71     , m_numPendingConnectionRequests(0)
72 #if ENABLE(LEGACY_CUSTOM_PROTOCOL_MANAGER)
73     , m_customProtocolManagerProxy(*this)
74 #endif
75     , m_throttler(*this, processPool.shouldTakeUIBackgroundAssertion())
76 {
77     connect();
78
79     if (auto* websiteDataStore = m_processPool.websiteDataStore())
80         m_websiteDataStores.set(websiteDataStore->websiteDataStore().sessionID(), makeRef(websiteDataStore->websiteDataStore()));
81 }
82
83 NetworkProcessProxy::~NetworkProcessProxy()
84 {
85     ASSERT(m_pendingFetchWebsiteDataCallbacks.isEmpty());
86     ASSERT(m_pendingDeleteWebsiteDataCallbacks.isEmpty());
87     ASSERT(m_pendingDeleteWebsiteDataForOriginsCallbacks.isEmpty());
88 #if ENABLE(CONTENT_EXTENSIONS)
89     for (auto* proxy : m_webUserContentControllerProxies)
90         proxy->removeNetworkProcess(*this);
91 #endif
92
93     for (auto& reply : m_pendingConnectionReplies)
94         reply.second({ });
95 }
96
97 void NetworkProcessProxy::getLaunchOptions(ProcessLauncher::LaunchOptions& launchOptions)
98 {
99     launchOptions.processType = ProcessLauncher::ProcessType::Network;
100     ChildProcessProxy::getLaunchOptions(launchOptions);
101
102     if (processPool().shouldMakeNextNetworkProcessLaunchFailForTesting()) {
103         processPool().setShouldMakeNextNetworkProcessLaunchFailForTesting(false);
104         launchOptions.shouldMakeProcessLaunchFailForTesting = true;
105     }
106 }
107
108 void NetworkProcessProxy::connectionWillOpen(IPC::Connection& connection)
109 {
110 #if ENABLE(SEC_ITEM_SHIM)
111     SecItemShimProxy::singleton().initializeConnection(connection);
112 #else
113     UNUSED_PARAM(connection);
114 #endif
115 }
116
117 void NetworkProcessProxy::processWillShutDown(IPC::Connection& connection)
118 {
119     ASSERT_UNUSED(connection, this->connection() == &connection);
120 }
121
122 void NetworkProcessProxy::getNetworkProcessConnection(WebProcessProxy& webProcessProxy, Messages::WebProcessProxy::GetNetworkProcessConnection::DelayedReply&& reply)
123 {
124     m_pendingConnectionReplies.append(std::make_pair(makeWeakPtr(webProcessProxy), WTFMove(reply)));
125
126     if (state() == State::Launching) {
127         m_numPendingConnectionRequests++;
128         return;
129     }
130
131     bool isServiceWorkerProcess = false;
132     SecurityOriginData securityOrigin;
133 #if ENABLE(SERVICE_WORKER)
134     if (is<ServiceWorkerProcessProxy>(webProcessProxy)) {
135         isServiceWorkerProcess = true;
136         securityOrigin = downcast<ServiceWorkerProcessProxy>(webProcessProxy).securityOrigin();
137     }
138 #endif
139
140     connection()->send(Messages::NetworkProcess::CreateNetworkConnectionToWebProcess(isServiceWorkerProcess, securityOrigin), 0, IPC::SendOption::DispatchMessageEvenWhenWaitingForSyncReply);
141 }
142
143 DownloadProxy* NetworkProcessProxy::createDownloadProxy(const ResourceRequest& resourceRequest)
144 {
145     if (!m_downloadProxyMap)
146         m_downloadProxyMap = std::make_unique<DownloadProxyMap>(this);
147
148     return m_downloadProxyMap->createDownloadProxy(m_processPool, resourceRequest);
149 }
150
151 void NetworkProcessProxy::fetchWebsiteData(PAL::SessionID sessionID, OptionSet<WebsiteDataType> dataTypes, OptionSet<WebsiteDataFetchOption> fetchOptions, CompletionHandler<void (WebsiteData)>&& completionHandler)
152 {
153     ASSERT(canSendMessage());
154
155     uint64_t callbackID = generateCallbackID();
156     RELEASE_LOG_IF(sessionID.isAlwaysOnLoggingAllowed(), ProcessSuspension, "%p - NetworkProcessProxy is taking a background assertion because the Network process is fetching Website data", this);
157
158     m_pendingFetchWebsiteDataCallbacks.add(callbackID, [this, token = throttler().backgroundActivityToken(), completionHandler = WTFMove(completionHandler), sessionID] (WebsiteData websiteData) mutable {
159 #if RELEASE_LOG_DISABLED
160         UNUSED_PARAM(this);
161         UNUSED_PARAM(sessionID);
162 #endif
163         completionHandler(WTFMove(websiteData));
164         RELEASE_LOG_IF(sessionID.isAlwaysOnLoggingAllowed(), ProcessSuspension, "%p - NetworkProcessProxy is releasing a background assertion because the Network process is done fetching Website data", this);
165     });
166
167     send(Messages::NetworkProcess::FetchWebsiteData(sessionID, dataTypes, fetchOptions, callbackID), 0);
168 }
169
170 void NetworkProcessProxy::deleteWebsiteData(PAL::SessionID sessionID, OptionSet<WebsiteDataType> dataTypes, WallTime modifiedSince, CompletionHandler<void ()>&& completionHandler)
171 {
172     auto callbackID = generateCallbackID();
173     RELEASE_LOG_IF(sessionID.isAlwaysOnLoggingAllowed(), ProcessSuspension, "%p - NetworkProcessProxy is taking a background assertion because the Network process is deleting Website data", this);
174
175     m_pendingDeleteWebsiteDataCallbacks.add(callbackID, [this, token = throttler().backgroundActivityToken(), completionHandler = WTFMove(completionHandler), sessionID] () mutable {
176 #if RELEASE_LOG_DISABLED
177         UNUSED_PARAM(this);
178         UNUSED_PARAM(sessionID);
179 #endif
180         completionHandler();
181         RELEASE_LOG_IF(sessionID.isAlwaysOnLoggingAllowed(), ProcessSuspension, "%p - NetworkProcessProxy is releasing a background assertion because the Network process is done deleting Website data", this);
182     });
183     send(Messages::NetworkProcess::DeleteWebsiteData(sessionID, dataTypes, modifiedSince, callbackID), 0);
184 }
185
186 void NetworkProcessProxy::deleteWebsiteDataForOrigins(PAL::SessionID sessionID, OptionSet<WebsiteDataType> dataTypes, const Vector<WebCore::SecurityOriginData>& origins, const Vector<String>& cookieHostNames, const Vector<String>& HSTSCacheHostNames, CompletionHandler<void()>&& completionHandler)
187 {
188     ASSERT(canSendMessage());
189
190     uint64_t callbackID = generateCallbackID();
191     RELEASE_LOG_IF(sessionID.isAlwaysOnLoggingAllowed(), ProcessSuspension, "%p - NetworkProcessProxy is taking a background assertion because the Network process is deleting Website data for several origins", this);
192
193     m_pendingDeleteWebsiteDataForOriginsCallbacks.add(callbackID, [this, token = throttler().backgroundActivityToken(), completionHandler = WTFMove(completionHandler), sessionID] () mutable {
194 #if RELEASE_LOG_DISABLED
195         UNUSED_PARAM(this);
196         UNUSED_PARAM(sessionID);
197 #endif
198         completionHandler();
199         RELEASE_LOG_IF(sessionID.isAlwaysOnLoggingAllowed(), ProcessSuspension, "%p - NetworkProcessProxy is releasing a background assertion because the Network process is done deleting Website data for several origins", this);
200     });
201
202     send(Messages::NetworkProcess::DeleteWebsiteDataForOrigins(sessionID, dataTypes, origins, cookieHostNames, HSTSCacheHostNames, callbackID), 0);
203 }
204
205 void NetworkProcessProxy::networkProcessCrashed()
206 {
207     clearCallbackStates();
208
209     Vector<std::pair<RefPtr<WebProcessProxy>, Messages::WebProcessProxy::GetNetworkProcessConnection::DelayedReply>> pendingReplies;
210     pendingReplies.reserveInitialCapacity(m_pendingConnectionReplies.size());
211     for (auto& reply : m_pendingConnectionReplies) {
212         if (reply.first)
213             pendingReplies.append(std::make_pair(makeRefPtr(reply.first.get()), WTFMove(reply.second)));
214         else
215             reply.second({ });
216     }
217     m_pendingConnectionReplies.clear();
218
219     // Tell the network process manager to forget about this network process proxy. This will cause us to be deleted.
220     m_processPool.networkProcessCrashed(*this, WTFMove(pendingReplies));
221 }
222
223 void NetworkProcessProxy::clearCallbackStates()
224 {
225     while (!m_pendingFetchWebsiteDataCallbacks.isEmpty())
226         m_pendingFetchWebsiteDataCallbacks.take(m_pendingFetchWebsiteDataCallbacks.begin()->key)(WebsiteData { });
227
228     while (!m_pendingDeleteWebsiteDataCallbacks.isEmpty())
229         m_pendingDeleteWebsiteDataCallbacks.take(m_pendingDeleteWebsiteDataCallbacks.begin()->key)();
230
231     while (!m_pendingDeleteWebsiteDataForOriginsCallbacks.isEmpty())
232         m_pendingDeleteWebsiteDataForOriginsCallbacks.take(m_pendingDeleteWebsiteDataForOriginsCallbacks.begin()->key)();
233
234     while (!m_updateBlockCookiesCallbackMap.isEmpty())
235         m_updateBlockCookiesCallbackMap.take(m_updateBlockCookiesCallbackMap.begin()->key)();
236     
237     while (!m_storageAccessResponseCallbackMap.isEmpty())
238         m_storageAccessResponseCallbackMap.take(m_storageAccessResponseCallbackMap.begin()->key)(false);
239 }
240
241 void NetworkProcessProxy::didReceiveMessage(IPC::Connection& connection, IPC::Decoder& decoder)
242 {
243     if (dispatchMessage(connection, decoder))
244         return;
245
246     if (m_processPool.dispatchMessage(connection, decoder))
247         return;
248
249     didReceiveNetworkProcessProxyMessage(connection, decoder);
250 }
251
252 void NetworkProcessProxy::didReceiveSyncMessage(IPC::Connection& connection, IPC::Decoder& decoder, std::unique_ptr<IPC::Encoder>& replyEncoder)
253 {
254     if (dispatchSyncMessage(connection, decoder, replyEncoder))
255         return;
256
257     ASSERT_NOT_REACHED();
258 }
259
260 void NetworkProcessProxy::didClose(IPC::Connection&)
261 {
262     auto protectedProcessPool = makeRef(m_processPool);
263
264     if (m_downloadProxyMap)
265         m_downloadProxyMap->processDidClose();
266 #if ENABLE(LEGACY_CUSTOM_PROTOCOL_MANAGER)
267     m_customProtocolManagerProxy.invalidate();
268 #endif
269
270     m_tokenForHoldingLockedFiles = nullptr;
271     
272     m_syncAllCookiesToken = nullptr;
273     m_syncAllCookiesCounter = 0;
274
275     for (auto& callback : m_removeAllStorageAccessCallbackMap.values())
276         callback();
277     m_removeAllStorageAccessCallbackMap.clear();
278
279     for (auto& callback : m_updateBlockCookiesCallbackMap.values())
280         callback();
281     m_updateBlockCookiesCallbackMap.clear();
282     
283     // This will cause us to be deleted.
284     networkProcessCrashed();
285 }
286
287 void NetworkProcessProxy::didReceiveInvalidMessage(IPC::Connection&, IPC::StringReference, IPC::StringReference)
288 {
289 }
290
291 void NetworkProcessProxy::didCreateNetworkConnectionToWebProcess(const IPC::Attachment& connectionIdentifier)
292 {
293     ASSERT(!m_pendingConnectionReplies.isEmpty());
294
295     // Grab the first pending connection reply.
296     auto reply = m_pendingConnectionReplies.takeFirst().second;
297
298 #if USE(UNIX_DOMAIN_SOCKETS) || OS(WINDOWS)
299     reply(connectionIdentifier);
300 #elif OS(DARWIN)
301     MESSAGE_CHECK(MACH_PORT_VALID(connectionIdentifier.port()));
302     reply(IPC::Attachment(connectionIdentifier.port(), MACH_MSG_TYPE_MOVE_SEND));
303 #else
304     notImplemented();
305 #endif
306 }
307
308 void NetworkProcessProxy::didReceiveAuthenticationChallenge(uint64_t pageID, uint64_t frameID, WebCore::AuthenticationChallenge&& coreChallenge, uint64_t challengeID)
309 {
310 #if ENABLE(SERVICE_WORKER)
311     if (auto* serviceWorkerProcessProxy = m_processPool.serviceWorkerProcessProxyFromPageID(pageID)) {
312         auto authenticationChallenge = AuthenticationChallengeProxy::create(WTFMove(coreChallenge), challengeID, makeRef(*connection()), nullptr);
313         serviceWorkerProcessProxy->didReceiveAuthenticationChallenge(pageID, frameID, WTFMove(authenticationChallenge));
314         return;
315     }
316 #endif
317
318     WebPageProxy* page = WebProcessProxy::webPage(pageID);
319     MESSAGE_CHECK(page);
320
321     auto authenticationChallenge = AuthenticationChallengeProxy::create(WTFMove(coreChallenge), challengeID, makeRef(*connection()), page->secKeyProxyStore(coreChallenge));
322     page->didReceiveAuthenticationChallengeProxy(frameID, WTFMove(authenticationChallenge));
323 }
324
325 void NetworkProcessProxy::didFetchWebsiteData(uint64_t callbackID, const WebsiteData& websiteData)
326 {
327     auto callback = m_pendingFetchWebsiteDataCallbacks.take(callbackID);
328     callback(websiteData);
329 }
330
331 void NetworkProcessProxy::didDeleteWebsiteData(uint64_t callbackID)
332 {
333     auto callback = m_pendingDeleteWebsiteDataCallbacks.take(callbackID);
334     callback();
335 }
336
337 void NetworkProcessProxy::didDeleteWebsiteDataForOrigins(uint64_t callbackID)
338 {
339     auto callback = m_pendingDeleteWebsiteDataForOriginsCallbacks.take(callbackID);
340     callback();
341 }
342
343 void NetworkProcessProxy::didFinishLaunching(ProcessLauncher* launcher, IPC::Connection::Identifier connectionIdentifier)
344 {
345     ChildProcessProxy::didFinishLaunching(launcher, connectionIdentifier);
346
347     if (!IPC::Connection::identifierIsValid(connectionIdentifier)) {
348         networkProcessCrashed();
349         return;
350     }
351
352     for (unsigned i = 0; i < m_numPendingConnectionRequests; ++i)
353         connection()->send(Messages::NetworkProcess::CreateNetworkConnectionToWebProcess(false, { }), 0);
354     
355     m_numPendingConnectionRequests = 0;
356
357 #if PLATFORM(COCOA)
358     if (m_processPool.processSuppressionEnabled())
359         setProcessSuppressionEnabled(true);
360 #endif
361     
362 #if PLATFORM(IOS_FAMILY)
363     if (xpc_connection_t connection = this->connection()->xpcConnection())
364         m_throttler.didConnectToProcess(xpc_connection_get_pid(connection));
365 #endif
366 }
367
368 void NetworkProcessProxy::logDiagnosticMessage(uint64_t pageID, const String& message, const String& description, WebCore::ShouldSample shouldSample)
369 {
370     WebPageProxy* page = WebProcessProxy::webPage(pageID);
371     // FIXME: We do this null-check because by the time the decision to log is made, the page may be gone. We should refactor to avoid this,
372     // but for now we simply drop the message in the rare case this happens.
373     if (!page)
374         return;
375
376     page->logDiagnosticMessage(message, description, shouldSample);
377 }
378
379 void NetworkProcessProxy::logDiagnosticMessageWithResult(uint64_t pageID, const String& message, const String& description, uint32_t result, WebCore::ShouldSample shouldSample)
380 {
381     WebPageProxy* page = WebProcessProxy::webPage(pageID);
382     // FIXME: We do this null-check because by the time the decision to log is made, the page may be gone. We should refactor to avoid this,
383     // but for now we simply drop the message in the rare case this happens.
384     if (!page)
385         return;
386
387     page->logDiagnosticMessageWithResult(message, description, result, shouldSample);
388 }
389
390 void NetworkProcessProxy::logDiagnosticMessageWithValue(uint64_t pageID, const String& message, const String& description, double value, unsigned significantFigures, WebCore::ShouldSample shouldSample)
391 {
392     WebPageProxy* page = WebProcessProxy::webPage(pageID);
393     // FIXME: We do this null-check because by the time the decision to log is made, the page may be gone. We should refactor to avoid this,
394     // but for now we simply drop the message in the rare case this happens.
395     if (!page)
396         return;
397
398     page->logDiagnosticMessageWithValue(message, description, value, significantFigures, shouldSample);
399 }
400
401 #if ENABLE(RESOURCE_LOAD_STATISTICS)
402 void NetworkProcessProxy::updatePrevalentDomainsToBlockCookiesFor(PAL::SessionID sessionID, const Vector<String>& domainsToBlock, CompletionHandler<void()>&& completionHandler)
403 {
404     if (!canSendMessage()) {
405         completionHandler();
406         return;
407     }
408     
409     auto callbackId = generateCallbackID();
410     auto addResult = m_updateBlockCookiesCallbackMap.add(callbackId, [protectedProcessPool = makeRef(m_processPool), token = throttler().backgroundActivityToken(), completionHandler = WTFMove(completionHandler)]() mutable {
411         completionHandler();
412     });
413     ASSERT_UNUSED(addResult, addResult.isNewEntry);
414     send(Messages::NetworkProcess::UpdatePrevalentDomainsToBlockCookiesFor(sessionID, domainsToBlock, callbackId), 0);
415 }
416
417 void NetworkProcessProxy::didUpdateBlockCookies(uint64_t callbackId)
418 {
419     m_updateBlockCookiesCallbackMap.take(callbackId)();
420 }
421
422 void NetworkProcessProxy::setAgeCapForClientSideCookies(PAL::SessionID sessionID, std::optional<Seconds> seconds, CompletionHandler<void()>&& completionHandler)
423 {
424     if (!canSendMessage()) {
425         completionHandler();
426         return;
427     }
428     
429     auto callbackId = generateCallbackID();
430     auto addResult = m_updateBlockCookiesCallbackMap.add(callbackId, [protectedProcessPool = makeRef(m_processPool), token = throttler().backgroundActivityToken(), completionHandler = WTFMove(completionHandler)]() mutable {
431         completionHandler();
432     });
433     ASSERT_UNUSED(addResult, addResult.isNewEntry);
434     send(Messages::NetworkProcess::SetAgeCapForClientSideCookies(sessionID, seconds, callbackId), 0);
435 }
436
437 void NetworkProcessProxy::didSetAgeCapForClientSideCookies(uint64_t callbackId)
438 {
439     m_updateBlockCookiesCallbackMap.take(callbackId)();
440 }
441
442 void NetworkProcessProxy::hasStorageAccessForFrame(PAL::SessionID sessionID, const String& resourceDomain, const String& firstPartyDomain, uint64_t frameID, uint64_t pageID, WTF::CompletionHandler<void(bool)>&& callback)
443 {
444     auto contextId = generateCallbackID();
445     auto addResult = m_storageAccessResponseCallbackMap.add(contextId, WTFMove(callback));
446     ASSERT_UNUSED(addResult, addResult.isNewEntry);
447     send(Messages::NetworkProcess::HasStorageAccessForFrame(sessionID, resourceDomain, firstPartyDomain, frameID, pageID, contextId), 0);
448 }
449
450 void NetworkProcessProxy::grantStorageAccess(PAL::SessionID sessionID, const String& resourceDomain, const String& firstPartyDomain, std::optional<uint64_t> frameID, uint64_t pageID, WTF::CompletionHandler<void(bool)>&& callback)
451 {
452     auto contextId = generateCallbackID();
453     auto addResult = m_storageAccessResponseCallbackMap.add(contextId, WTFMove(callback));
454     ASSERT_UNUSED(addResult, addResult.isNewEntry);
455     send(Messages::NetworkProcess::GrantStorageAccess(sessionID, resourceDomain, firstPartyDomain, frameID, pageID, contextId), 0);
456 }
457
458 void NetworkProcessProxy::storageAccessRequestResult(bool wasGranted, uint64_t contextId)
459 {
460     auto callback = m_storageAccessResponseCallbackMap.take(contextId);
461     callback(wasGranted);
462 }
463
464 void NetworkProcessProxy::removeAllStorageAccess(PAL::SessionID sessionID, CompletionHandler<void()>&& completionHandler)
465 {
466     if (!canSendMessage()) {
467         completionHandler();
468         return;
469     }
470
471     auto contextId = generateCallbackID();
472     auto addResult = m_removeAllStorageAccessCallbackMap.add(contextId, WTFMove(completionHandler));
473     ASSERT_UNUSED(addResult, addResult.isNewEntry);
474     send(Messages::NetworkProcess::RemoveAllStorageAccess(sessionID, contextId), 0);
475 }
476
477 void NetworkProcessProxy::didRemoveAllStorageAccess(uint64_t contextId)
478 {
479     auto completionHandler = m_removeAllStorageAccessCallbackMap.take(contextId);
480     completionHandler();
481 }
482
483 void NetworkProcessProxy::getAllStorageAccessEntries(PAL::SessionID sessionID, CompletionHandler<void(Vector<String>&& domains)>&& callback)
484 {
485     auto contextId = generateCallbackID();
486     auto addResult = m_allStorageAccessEntriesCallbackMap.add(contextId, WTFMove(callback));
487     ASSERT_UNUSED(addResult, addResult.isNewEntry);
488     send(Messages::NetworkProcess::GetAllStorageAccessEntries(sessionID, contextId), 0);
489 }
490
491 void NetworkProcessProxy::allStorageAccessEntriesResult(Vector<String>&& domains, uint64_t contextId)
492 {
493     auto callback = m_allStorageAccessEntriesCallbackMap.take(contextId);
494     callback(WTFMove(domains));
495 }
496
497 void NetworkProcessProxy::setCacheMaxAgeCapForPrevalentResources(PAL::SessionID sessionID, Seconds seconds, CompletionHandler<void()>&& completionHandler)
498 {
499     if (!canSendMessage()) {
500         completionHandler();
501         return;
502     }
503     
504     auto contextId = generateCallbackID();
505     auto addResult = m_updateRuntimeSettingsCallbackMap.add(contextId, WTFMove(completionHandler));
506     ASSERT_UNUSED(addResult, addResult.isNewEntry);
507     send(Messages::NetworkProcess::SetCacheMaxAgeCapForPrevalentResources(sessionID, seconds, contextId), 0);
508 }
509
510 void NetworkProcessProxy::didSetCacheMaxAgeCapForPrevalentResources(uint64_t contextId)
511 {
512     auto completionHandler = m_updateRuntimeSettingsCallbackMap.take(contextId);
513     completionHandler();
514 }
515
516 void NetworkProcessProxy::resetCacheMaxAgeCapForPrevalentResources(PAL::SessionID sessionID, CompletionHandler<void()>&& completionHandler)
517 {
518     if (!canSendMessage()) {
519         completionHandler();
520         return;
521     }
522     
523     auto contextId = generateCallbackID();
524     auto addResult = m_updateRuntimeSettingsCallbackMap.add(contextId, WTFMove(completionHandler));
525     ASSERT_UNUSED(addResult, addResult.isNewEntry);
526     send(Messages::NetworkProcess::ResetCacheMaxAgeCapForPrevalentResources(sessionID, contextId), 0);
527 }
528
529 void NetworkProcessProxy::didResetCacheMaxAgeCapForPrevalentResources(uint64_t contextId)
530 {
531     auto completionHandler = m_updateRuntimeSettingsCallbackMap.take(contextId);
532     completionHandler();
533 }
534 #endif // ENABLE(RESOURCE_LOAD_STATISTICS)
535
536 void NetworkProcessProxy::sendProcessWillSuspendImminently()
537 {
538     if (!canSendMessage())
539         return;
540
541     bool handled = false;
542     sendSync(Messages::NetworkProcess::ProcessWillSuspendImminently(), Messages::NetworkProcess::ProcessWillSuspendImminently::Reply(handled), 0, 1_s);
543 }
544     
545 void NetworkProcessProxy::sendPrepareToSuspend()
546 {
547     if (canSendMessage())
548         send(Messages::NetworkProcess::PrepareToSuspend(), 0);
549 }
550
551 void NetworkProcessProxy::sendCancelPrepareToSuspend()
552 {
553     if (canSendMessage())
554         send(Messages::NetworkProcess::CancelPrepareToSuspend(), 0);
555 }
556
557 void NetworkProcessProxy::sendProcessDidResume()
558 {
559     if (canSendMessage())
560         send(Messages::NetworkProcess::ProcessDidResume(), 0);
561 }
562
563 void NetworkProcessProxy::writeBlobToFilePath(const URL& url, const String& path, CompletionHandler<void(bool)>&& completionHandler)
564 {
565     if (!canSendMessage()) {
566         completionHandler(false);
567         return;
568     }
569
570     SandboxExtension::Handle handleForWriting;
571     SandboxExtension::createHandle(path, SandboxExtension::Type::ReadWrite, handleForWriting);
572     sendWithAsyncReply(Messages::NetworkProcess::WriteBlobToFilePath(url, path, handleForWriting), WTFMove(completionHandler));
573 }
574
575 void NetworkProcessProxy::processReadyToSuspend()
576 {
577     m_throttler.processReadyToSuspend();
578 }
579
580 void NetworkProcessProxy::didSetAssertionState(AssertionState)
581 {
582 }
583     
584 void NetworkProcessProxy::setIsHoldingLockedFiles(bool isHoldingLockedFiles)
585 {
586     if (!isHoldingLockedFiles) {
587         RELEASE_LOG(ProcessSuspension, "UIProcess is releasing a background assertion because the Network process is no longer holding locked files");
588         m_tokenForHoldingLockedFiles = nullptr;
589         return;
590     }
591     if (!m_tokenForHoldingLockedFiles) {
592         RELEASE_LOG(ProcessSuspension, "UIProcess is taking a background assertion because the Network process is holding locked files");
593         m_tokenForHoldingLockedFiles = m_throttler.backgroundActivityToken();
594     }
595 }
596
597 void NetworkProcessProxy::syncAllCookies()
598 {
599     send(Messages::NetworkProcess::SyncAllCookies(), 0);
600     
601     ++m_syncAllCookiesCounter;
602     if (m_syncAllCookiesToken)
603         return;
604     
605     RELEASE_LOG(ProcessSuspension, "%p - NetworkProcessProxy is taking a background assertion because the Network process is syncing cookies", this);
606     m_syncAllCookiesToken = throttler().backgroundActivityToken();
607 }
608     
609 void NetworkProcessProxy::didSyncAllCookies()
610 {
611     ASSERT(m_syncAllCookiesCounter);
612
613     --m_syncAllCookiesCounter;
614     if (!m_syncAllCookiesCounter) {
615         RELEASE_LOG(ProcessSuspension, "%p - NetworkProcessProxy is releasing a background assertion because the Network process is done syncing cookies", this);
616         m_syncAllCookiesToken = nullptr;
617     }
618 }
619
620 void NetworkProcessProxy::addSession(Ref<WebsiteDataStore>&& store)
621 {
622     if (canSendMessage())
623         send(Messages::NetworkProcess::AddWebsiteDataStore { store->parameters() }, 0);
624     auto sessionID = store->sessionID();
625     if (!sessionID.isEphemeral())
626         m_websiteDataStores.set(sessionID, WTFMove(store));
627 }
628
629 void NetworkProcessProxy::removeSession(PAL::SessionID sessionID)
630 {
631     if (canSendMessage())
632         send(Messages::NetworkProcess::DestroySession { sessionID }, 0);
633     if (!sessionID.isEphemeral())
634         m_websiteDataStores.remove(sessionID);
635 }
636
637 WebsiteDataStore* NetworkProcessProxy::websiteDataStoreFromSessionID(PAL::SessionID sessionID)
638 {
639     auto iterator = m_websiteDataStores.find(sessionID);
640     if (iterator != m_websiteDataStores.end())
641         return iterator->value.get();
642
643     if (auto* websiteDataStore = m_processPool.websiteDataStore()) {
644         if (sessionID == websiteDataStore->websiteDataStore().sessionID())
645             return &websiteDataStore->websiteDataStore();
646     }
647
648     if (sessionID != PAL::SessionID::defaultSessionID())
649         return nullptr;
650
651     return &API::WebsiteDataStore::defaultDataStore()->websiteDataStore();
652 }
653
654 void NetworkProcessProxy::retrieveCacheStorageParameters(PAL::SessionID sessionID)
655 {
656     auto* store = websiteDataStoreFromSessionID(sessionID);
657
658     if (!store) {
659         RELEASE_LOG_ERROR(CacheStorage, "%p - NetworkProcessProxy is unable to retrieve CacheStorage parameters from the given session ID %" PRIu64, this, sessionID.sessionID());
660         auto quota = m_processPool.websiteDataStore() ? m_processPool.websiteDataStore()->websiteDataStore().cacheStoragePerOriginQuota() : WebsiteDataStore::defaultCacheStoragePerOriginQuota;
661         send(Messages::NetworkProcess::SetCacheStorageParameters { sessionID, quota, { }, { } }, 0);
662         return;
663     }
664
665     auto& cacheStorageDirectory = store->cacheStorageDirectory();
666     SandboxExtension::Handle cacheStorageDirectoryExtensionHandle;
667     if (!cacheStorageDirectory.isEmpty())
668         SandboxExtension::createHandleForReadWriteDirectory(cacheStorageDirectory, cacheStorageDirectoryExtensionHandle);
669
670     send(Messages::NetworkProcess::SetCacheStorageParameters { sessionID, store->cacheStoragePerOriginQuota(), cacheStorageDirectory, cacheStorageDirectoryExtensionHandle }, 0);
671 }
672
673 #if ENABLE(CONTENT_EXTENSIONS)
674 void NetworkProcessProxy::contentExtensionRules(UserContentControllerIdentifier identifier)
675 {
676     if (auto* webUserContentControllerProxy = WebUserContentControllerProxy::get(identifier)) {
677         m_webUserContentControllerProxies.add(webUserContentControllerProxy);
678         webUserContentControllerProxy->addNetworkProcess(*this);
679
680         auto rules = WTF::map(webUserContentControllerProxy->contentExtensionRules(), [](auto&& keyValue) -> std::pair<String, WebCompiledContentRuleListData> {
681             return std::make_pair(keyValue.value->name(), keyValue.value->compiledRuleList().data());
682         });
683         send(Messages::NetworkContentRuleListManager::AddContentRuleLists { identifier, rules }, 0);
684         return;
685     }
686     send(Messages::NetworkContentRuleListManager::AddContentRuleLists { identifier, { } }, 0);
687 }
688
689 void NetworkProcessProxy::didDestroyWebUserContentControllerProxy(WebUserContentControllerProxy& proxy)
690 {
691     send(Messages::NetworkContentRuleListManager::Remove { proxy.identifier() }, 0);
692     m_webUserContentControllerProxies.remove(&proxy);
693 }
694 #endif
695     
696 void NetworkProcessProxy::sendProcessDidTransitionToForeground()
697 {
698     send(Messages::NetworkProcess::ProcessDidTransitionToForeground(), 0);
699 }
700
701 void NetworkProcessProxy::sendProcessDidTransitionToBackground()
702 {
703     send(Messages::NetworkProcess::ProcessDidTransitionToBackground(), 0);
704 }
705
706 #if ENABLE(SANDBOX_EXTENSIONS)
707 void NetworkProcessProxy::getSandboxExtensionsForBlobFiles(const Vector<String>& paths, Messages::NetworkProcessProxy::GetSandboxExtensionsForBlobFiles::AsyncReply&& reply)
708 {
709     SandboxExtension::HandleArray extensions;
710     extensions.allocate(paths.size());
711     for (size_t i = 0; i < paths.size(); ++i) {
712         // ReadWrite is required for creating hard links, which is something that might be done with these extensions.
713         SandboxExtension::createHandle(paths[i], SandboxExtension::Type::ReadWrite, extensions[i]);
714     }
715     reply(WTFMove(extensions));
716 }
717 #endif
718
719 #if ENABLE(SERVICE_WORKER)
720 void NetworkProcessProxy::establishWorkerContextConnectionToNetworkProcess(SecurityOriginData&& origin)
721 {
722     m_processPool.establishWorkerContextConnectionToNetworkProcess(*this, WTFMove(origin), std::nullopt);
723 }
724
725 void NetworkProcessProxy::establishWorkerContextConnectionToNetworkProcessForExplicitSession(SecurityOriginData&& origin, PAL::SessionID sessionID)
726 {
727     m_processPool.establishWorkerContextConnectionToNetworkProcess(*this, WTFMove(origin), sessionID);
728 }
729 #endif
730
731 } // namespace WebKit
732
733 #undef MESSAGE_CHECK
734 #undef MESSAGE_CHECK_URL