Create a WebResourceLoadStatisticsStore attached to the NetworkSession
[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::didLogUserInteraction(uint64_t contextId)
423 {
424     // FIXME(193297): Implement when activating automated test cases.
425     UNUSED_PARAM(contextId);
426 }
427
428 void NetworkProcessProxy::setAgeCapForClientSideCookies(PAL::SessionID sessionID, Optional<Seconds> seconds, CompletionHandler<void()>&& completionHandler)
429 {
430     if (!canSendMessage()) {
431         completionHandler();
432         return;
433     }
434     
435     auto callbackId = generateCallbackID();
436     auto addResult = m_updateBlockCookiesCallbackMap.add(callbackId, [protectedProcessPool = makeRef(m_processPool), token = throttler().backgroundActivityToken(), completionHandler = WTFMove(completionHandler)]() mutable {
437         completionHandler();
438     });
439     ASSERT_UNUSED(addResult, addResult.isNewEntry);
440     send(Messages::NetworkProcess::SetAgeCapForClientSideCookies(sessionID, seconds, callbackId), 0);
441 }
442
443 void NetworkProcessProxy::didSetAgeCapForClientSideCookies(uint64_t callbackId)
444 {
445     m_updateBlockCookiesCallbackMap.take(callbackId)();
446 }
447
448 void NetworkProcessProxy::hasStorageAccessForFrame(PAL::SessionID sessionID, const String& resourceDomain, const String& firstPartyDomain, uint64_t frameID, uint64_t pageID, WTF::CompletionHandler<void(bool)>&& callback)
449 {
450     auto contextId = generateCallbackID();
451     auto addResult = m_storageAccessResponseCallbackMap.add(contextId, WTFMove(callback));
452     ASSERT_UNUSED(addResult, addResult.isNewEntry);
453     send(Messages::NetworkProcess::HasStorageAccessForFrame(sessionID, resourceDomain, firstPartyDomain, frameID, pageID, contextId), 0);
454 }
455
456 void NetworkProcessProxy::grantStorageAccess(PAL::SessionID sessionID, const String& resourceDomain, const String& firstPartyDomain, Optional<uint64_t> frameID, uint64_t pageID, WTF::CompletionHandler<void(bool)>&& callback)
457 {
458     auto contextId = generateCallbackID();
459     auto addResult = m_storageAccessResponseCallbackMap.add(contextId, WTFMove(callback));
460     ASSERT_UNUSED(addResult, addResult.isNewEntry);
461     send(Messages::NetworkProcess::GrantStorageAccess(sessionID, resourceDomain, firstPartyDomain, frameID, pageID, contextId), 0);
462 }
463
464 void NetworkProcessProxy::storageAccessRequestResult(bool wasGranted, uint64_t contextId)
465 {
466     auto callback = m_storageAccessResponseCallbackMap.take(contextId);
467     callback(wasGranted);
468 }
469
470 void NetworkProcessProxy::removeAllStorageAccess(PAL::SessionID sessionID, CompletionHandler<void()>&& completionHandler)
471 {
472     if (!canSendMessage()) {
473         completionHandler();
474         return;
475     }
476
477     auto contextId = generateCallbackID();
478     auto addResult = m_removeAllStorageAccessCallbackMap.add(contextId, WTFMove(completionHandler));
479     ASSERT_UNUSED(addResult, addResult.isNewEntry);
480     send(Messages::NetworkProcess::RemoveAllStorageAccess(sessionID, contextId), 0);
481 }
482
483 void NetworkProcessProxy::didRemoveAllStorageAccess(uint64_t contextId)
484 {
485     auto completionHandler = m_removeAllStorageAccessCallbackMap.take(contextId);
486     completionHandler();
487 }
488
489 void NetworkProcessProxy::getAllStorageAccessEntries(PAL::SessionID sessionID, CompletionHandler<void(Vector<String>&& domains)>&& callback)
490 {
491     auto contextId = generateCallbackID();
492     auto addResult = m_allStorageAccessEntriesCallbackMap.add(contextId, WTFMove(callback));
493     ASSERT_UNUSED(addResult, addResult.isNewEntry);
494     send(Messages::NetworkProcess::GetAllStorageAccessEntries(sessionID, contextId), 0);
495 }
496
497 void NetworkProcessProxy::allStorageAccessEntriesResult(Vector<String>&& domains, uint64_t contextId)
498 {
499     auto callback = m_allStorageAccessEntriesCallbackMap.take(contextId);
500     callback(WTFMove(domains));
501 }
502
503 void NetworkProcessProxy::setCacheMaxAgeCapForPrevalentResources(PAL::SessionID sessionID, Seconds seconds, CompletionHandler<void()>&& completionHandler)
504 {
505     if (!canSendMessage()) {
506         completionHandler();
507         return;
508     }
509     
510     auto contextId = generateCallbackID();
511     auto addResult = m_updateRuntimeSettingsCallbackMap.add(contextId, WTFMove(completionHandler));
512     ASSERT_UNUSED(addResult, addResult.isNewEntry);
513     send(Messages::NetworkProcess::SetCacheMaxAgeCapForPrevalentResources(sessionID, seconds, contextId), 0);
514 }
515
516 void NetworkProcessProxy::didSetCacheMaxAgeCapForPrevalentResources(uint64_t contextId)
517 {
518     auto completionHandler = m_updateRuntimeSettingsCallbackMap.take(contextId);
519     completionHandler();
520 }
521
522 void NetworkProcessProxy::resetCacheMaxAgeCapForPrevalentResources(PAL::SessionID sessionID, CompletionHandler<void()>&& completionHandler)
523 {
524     if (!canSendMessage()) {
525         completionHandler();
526         return;
527     }
528     
529     auto contextId = generateCallbackID();
530     auto addResult = m_updateRuntimeSettingsCallbackMap.add(contextId, WTFMove(completionHandler));
531     ASSERT_UNUSED(addResult, addResult.isNewEntry);
532     send(Messages::NetworkProcess::ResetCacheMaxAgeCapForPrevalentResources(sessionID, contextId), 0);
533 }
534
535 void NetworkProcessProxy::didResetCacheMaxAgeCapForPrevalentResources(uint64_t contextId)
536 {
537     auto completionHandler = m_updateRuntimeSettingsCallbackMap.take(contextId);
538     completionHandler();
539 }
540 #endif // ENABLE(RESOURCE_LOAD_STATISTICS)
541
542 void NetworkProcessProxy::sendProcessWillSuspendImminently()
543 {
544     if (!canSendMessage())
545         return;
546
547     bool handled = false;
548     sendSync(Messages::NetworkProcess::ProcessWillSuspendImminently(), Messages::NetworkProcess::ProcessWillSuspendImminently::Reply(handled), 0, 1_s);
549 }
550     
551 void NetworkProcessProxy::sendPrepareToSuspend()
552 {
553     if (canSendMessage())
554         send(Messages::NetworkProcess::PrepareToSuspend(), 0);
555 }
556
557 void NetworkProcessProxy::sendCancelPrepareToSuspend()
558 {
559     if (canSendMessage())
560         send(Messages::NetworkProcess::CancelPrepareToSuspend(), 0);
561 }
562
563 void NetworkProcessProxy::sendProcessDidResume()
564 {
565     if (canSendMessage())
566         send(Messages::NetworkProcess::ProcessDidResume(), 0);
567 }
568
569 void NetworkProcessProxy::writeBlobToFilePath(const URL& url, const String& path, CompletionHandler<void(bool)>&& completionHandler)
570 {
571     if (!canSendMessage()) {
572         completionHandler(false);
573         return;
574     }
575
576     SandboxExtension::Handle handleForWriting;
577     SandboxExtension::createHandle(path, SandboxExtension::Type::ReadWrite, handleForWriting);
578     sendWithAsyncReply(Messages::NetworkProcess::WriteBlobToFilePath(url, path, handleForWriting), WTFMove(completionHandler));
579 }
580
581 void NetworkProcessProxy::processReadyToSuspend()
582 {
583     m_throttler.processReadyToSuspend();
584 }
585
586 void NetworkProcessProxy::didSetAssertionState(AssertionState)
587 {
588 }
589     
590 void NetworkProcessProxy::setIsHoldingLockedFiles(bool isHoldingLockedFiles)
591 {
592     if (!isHoldingLockedFiles) {
593         RELEASE_LOG(ProcessSuspension, "UIProcess is releasing a background assertion because the Network process is no longer holding locked files");
594         m_tokenForHoldingLockedFiles = nullptr;
595         return;
596     }
597     if (!m_tokenForHoldingLockedFiles) {
598         RELEASE_LOG(ProcessSuspension, "UIProcess is taking a background assertion because the Network process is holding locked files");
599         m_tokenForHoldingLockedFiles = m_throttler.backgroundActivityToken();
600     }
601 }
602
603 void NetworkProcessProxy::syncAllCookies()
604 {
605     send(Messages::NetworkProcess::SyncAllCookies(), 0);
606     
607     ++m_syncAllCookiesCounter;
608     if (m_syncAllCookiesToken)
609         return;
610     
611     RELEASE_LOG(ProcessSuspension, "%p - NetworkProcessProxy is taking a background assertion because the Network process is syncing cookies", this);
612     m_syncAllCookiesToken = throttler().backgroundActivityToken();
613 }
614     
615 void NetworkProcessProxy::didSyncAllCookies()
616 {
617     ASSERT(m_syncAllCookiesCounter);
618
619     --m_syncAllCookiesCounter;
620     if (!m_syncAllCookiesCounter) {
621         RELEASE_LOG(ProcessSuspension, "%p - NetworkProcessProxy is releasing a background assertion because the Network process is done syncing cookies", this);
622         m_syncAllCookiesToken = nullptr;
623     }
624 }
625
626 void NetworkProcessProxy::addSession(Ref<WebsiteDataStore>&& store)
627 {
628     if (canSendMessage())
629         send(Messages::NetworkProcess::AddWebsiteDataStore { store->parameters() }, 0);
630     auto sessionID = store->sessionID();
631     if (!sessionID.isEphemeral())
632         m_websiteDataStores.set(sessionID, WTFMove(store));
633 }
634
635 void NetworkProcessProxy::removeSession(PAL::SessionID sessionID)
636 {
637     if (canSendMessage())
638         send(Messages::NetworkProcess::DestroySession { sessionID }, 0);
639     if (!sessionID.isEphemeral())
640         m_websiteDataStores.remove(sessionID);
641 }
642
643 WebsiteDataStore* NetworkProcessProxy::websiteDataStoreFromSessionID(PAL::SessionID sessionID)
644 {
645     auto iterator = m_websiteDataStores.find(sessionID);
646     if (iterator != m_websiteDataStores.end())
647         return iterator->value.get();
648
649     if (auto* websiteDataStore = m_processPool.websiteDataStore()) {
650         if (sessionID == websiteDataStore->websiteDataStore().sessionID())
651             return &websiteDataStore->websiteDataStore();
652     }
653
654     if (sessionID != PAL::SessionID::defaultSessionID())
655         return nullptr;
656
657     return &API::WebsiteDataStore::defaultDataStore()->websiteDataStore();
658 }
659
660 void NetworkProcessProxy::retrieveCacheStorageParameters(PAL::SessionID sessionID)
661 {
662     auto* store = websiteDataStoreFromSessionID(sessionID);
663
664     if (!store) {
665         RELEASE_LOG_ERROR(CacheStorage, "%p - NetworkProcessProxy is unable to retrieve CacheStorage parameters from the given session ID %" PRIu64, this, sessionID.sessionID());
666         auto quota = m_processPool.websiteDataStore() ? m_processPool.websiteDataStore()->websiteDataStore().cacheStoragePerOriginQuota() : WebsiteDataStoreConfiguration::defaultCacheStoragePerOriginQuota;
667         send(Messages::NetworkProcess::SetCacheStorageParameters { sessionID, quota, { }, { } }, 0);
668         return;
669     }
670
671     auto& cacheStorageDirectory = store->cacheStorageDirectory();
672     SandboxExtension::Handle cacheStorageDirectoryExtensionHandle;
673     if (!cacheStorageDirectory.isEmpty())
674         SandboxExtension::createHandleForReadWriteDirectory(cacheStorageDirectory, cacheStorageDirectoryExtensionHandle);
675
676     send(Messages::NetworkProcess::SetCacheStorageParameters { sessionID, store->cacheStoragePerOriginQuota(), cacheStorageDirectory, cacheStorageDirectoryExtensionHandle }, 0);
677 }
678
679 #if ENABLE(CONTENT_EXTENSIONS)
680 void NetworkProcessProxy::contentExtensionRules(UserContentControllerIdentifier identifier)
681 {
682     if (auto* webUserContentControllerProxy = WebUserContentControllerProxy::get(identifier)) {
683         m_webUserContentControllerProxies.add(webUserContentControllerProxy);
684         webUserContentControllerProxy->addNetworkProcess(*this);
685
686         auto rules = WTF::map(webUserContentControllerProxy->contentExtensionRules(), [](auto&& keyValue) -> std::pair<String, WebCompiledContentRuleListData> {
687             return std::make_pair(keyValue.value->name(), keyValue.value->compiledRuleList().data());
688         });
689         send(Messages::NetworkContentRuleListManager::AddContentRuleLists { identifier, rules }, 0);
690         return;
691     }
692     send(Messages::NetworkContentRuleListManager::AddContentRuleLists { identifier, { } }, 0);
693 }
694
695 void NetworkProcessProxy::didDestroyWebUserContentControllerProxy(WebUserContentControllerProxy& proxy)
696 {
697     send(Messages::NetworkContentRuleListManager::Remove { proxy.identifier() }, 0);
698     m_webUserContentControllerProxies.remove(&proxy);
699 }
700 #endif
701     
702 void NetworkProcessProxy::sendProcessDidTransitionToForeground()
703 {
704     send(Messages::NetworkProcess::ProcessDidTransitionToForeground(), 0);
705 }
706
707 void NetworkProcessProxy::sendProcessDidTransitionToBackground()
708 {
709     send(Messages::NetworkProcess::ProcessDidTransitionToBackground(), 0);
710 }
711
712 #if ENABLE(SANDBOX_EXTENSIONS)
713 void NetworkProcessProxy::getSandboxExtensionsForBlobFiles(const Vector<String>& paths, Messages::NetworkProcessProxy::GetSandboxExtensionsForBlobFiles::AsyncReply&& reply)
714 {
715     SandboxExtension::HandleArray extensions;
716     extensions.allocate(paths.size());
717     for (size_t i = 0; i < paths.size(); ++i) {
718         // ReadWrite is required for creating hard links, which is something that might be done with these extensions.
719         SandboxExtension::createHandle(paths[i], SandboxExtension::Type::ReadWrite, extensions[i]);
720     }
721     reply(WTFMove(extensions));
722 }
723 #endif
724
725 #if ENABLE(SERVICE_WORKER)
726 void NetworkProcessProxy::establishWorkerContextConnectionToNetworkProcess(SecurityOriginData&& origin)
727 {
728     m_processPool.establishWorkerContextConnectionToNetworkProcess(*this, WTFMove(origin), WTF::nullopt);
729 }
730
731 void NetworkProcessProxy::establishWorkerContextConnectionToNetworkProcessForExplicitSession(SecurityOriginData&& origin, PAL::SessionID sessionID)
732 {
733     m_processPool.establishWorkerContextConnectionToNetworkProcess(*this, WTFMove(origin), sessionID);
734 }
735 #endif
736
737 } // namespace WebKit
738
739 #undef MESSAGE_CHECK
740 #undef MESSAGE_CHECK_URL