2d8dbc54ddb17859eb6c655b426a4515d596338c
[WebKit-https.git] / Source / WebKit / UIProcess / Network / NetworkProcessProxy.cpp
1 /*
2  * Copyright (C) 2012-2019 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 "ShouldGrandfatherStatistics.h"
41 #include "StorageAccessStatus.h"
42 #include "WebCompiledContentRuleList.h"
43 #include "WebPageProxy.h"
44 #include "WebProcessMessages.h"
45 #include "WebProcessPool.h"
46 #include "WebProcessProxy.h"
47 #include "WebResourceLoadStatisticsStore.h"
48 #include "WebUserContentControllerProxy.h"
49 #include "WebsiteData.h"
50 #include "WebsiteDataStoreClient.h"
51 #include <WebCore/ClientOrigin.h>
52 #include <WebCore/RegistrableDomain.h>
53 #include <wtf/CompletionHandler.h>
54
55 #if ENABLE(SEC_ITEM_SHIM)
56 #include "SecItemShimProxy.h"
57 #endif
58
59 #if PLATFORM(IOS_FAMILY)
60 #include <wtf/spi/darwin/XPCSPI.h>
61 #endif
62
63 #define MESSAGE_CHECK(assertion) MESSAGE_CHECK_BASE(assertion, connection())
64
65 namespace WebKit {
66 using namespace WebCore;
67
68 static uint64_t generateCallbackID()
69 {
70     static uint64_t callbackID;
71
72     return ++callbackID;
73 }
74
75 NetworkProcessProxy::NetworkProcessProxy(WebProcessPool& processPool)
76     : AuxiliaryProcessProxy(processPool.alwaysRunsAtBackgroundPriority())
77     , m_processPool(processPool)
78     , m_numPendingConnectionRequests(0)
79 #if ENABLE(LEGACY_CUSTOM_PROTOCOL_MANAGER)
80     , m_customProtocolManagerProxy(*this)
81 #endif
82     , m_throttler(*this, processPool.shouldTakeUIBackgroundAssertion())
83 {
84     connect();
85
86     if (auto* websiteDataStore = m_processPool.websiteDataStore())
87         m_websiteDataStores.set(websiteDataStore->websiteDataStore().sessionID(), makeRef(websiteDataStore->websiteDataStore()));
88 }
89
90 NetworkProcessProxy::~NetworkProcessProxy()
91 {
92     ASSERT(m_pendingFetchWebsiteDataCallbacks.isEmpty());
93     ASSERT(m_pendingDeleteWebsiteDataCallbacks.isEmpty());
94     ASSERT(m_pendingDeleteWebsiteDataForOriginsCallbacks.isEmpty());
95 #if ENABLE(CONTENT_EXTENSIONS)
96     for (auto* proxy : m_webUserContentControllerProxies)
97         proxy->removeNetworkProcess(*this);
98 #endif
99
100     for (auto& reply : m_pendingConnectionReplies)
101         reply.second({ });
102 }
103
104 void NetworkProcessProxy::getLaunchOptions(ProcessLauncher::LaunchOptions& launchOptions)
105 {
106     launchOptions.processType = ProcessLauncher::ProcessType::Network;
107     AuxiliaryProcessProxy::getLaunchOptions(launchOptions);
108
109     if (processPool().shouldMakeNextNetworkProcessLaunchFailForTesting()) {
110         processPool().setShouldMakeNextNetworkProcessLaunchFailForTesting(false);
111         launchOptions.shouldMakeProcessLaunchFailForTesting = true;
112     }
113 }
114
115 void NetworkProcessProxy::connectionWillOpen(IPC::Connection& connection)
116 {
117 #if ENABLE(SEC_ITEM_SHIM)
118     SecItemShimProxy::singleton().initializeConnection(connection);
119 #else
120     UNUSED_PARAM(connection);
121 #endif
122 }
123
124 void NetworkProcessProxy::processWillShutDown(IPC::Connection& connection)
125 {
126     ASSERT_UNUSED(connection, this->connection() == &connection);
127 }
128
129 void NetworkProcessProxy::getNetworkProcessConnection(WebProcessProxy& webProcessProxy, Messages::WebProcessProxy::GetNetworkProcessConnection::DelayedReply&& reply)
130 {
131     m_pendingConnectionReplies.append(std::make_pair(makeWeakPtr(webProcessProxy), WTFMove(reply)));
132
133     if (state() == State::Launching) {
134         m_numPendingConnectionRequests++;
135         return;
136     }
137
138     bool isServiceWorkerProcess = false;
139     RegistrableDomain registrableDomain;
140 #if ENABLE(SERVICE_WORKER)
141     if (is<ServiceWorkerProcessProxy>(webProcessProxy)) {
142         isServiceWorkerProcess = true;
143         registrableDomain = downcast<ServiceWorkerProcessProxy>(webProcessProxy).registrableDomain();
144     }
145 #endif
146
147     connection()->send(Messages::NetworkProcess::CreateNetworkConnectionToWebProcess(isServiceWorkerProcess, registrableDomain), 0, IPC::SendOption::DispatchMessageEvenWhenWaitingForSyncReply);
148 }
149
150 void NetworkProcessProxy::synthesizeAppIsBackground(bool background)
151 {
152     if (m_downloadProxyMap) {
153         if (background)
154             m_downloadProxyMap->applicationDidEnterBackground();
155         else
156             m_downloadProxyMap->applicationWillEnterForeground();
157     }
158 }
159
160 DownloadProxy& NetworkProcessProxy::createDownloadProxy(const ResourceRequest& resourceRequest)
161 {
162     if (!m_downloadProxyMap)
163         m_downloadProxyMap = std::make_unique<DownloadProxyMap>(*this);
164
165     return m_downloadProxyMap->createDownloadProxy(m_processPool, resourceRequest);
166 }
167
168 void NetworkProcessProxy::fetchWebsiteData(PAL::SessionID sessionID, OptionSet<WebsiteDataType> dataTypes, OptionSet<WebsiteDataFetchOption> fetchOptions, CompletionHandler<void (WebsiteData)>&& completionHandler)
169 {
170     ASSERT(canSendMessage());
171
172     uint64_t callbackID = generateCallbackID();
173     RELEASE_LOG_IF(sessionID.isAlwaysOnLoggingAllowed(), ProcessSuspension, "%p - NetworkProcessProxy is taking a background assertion because the Network process is fetching Website data", this);
174
175     m_pendingFetchWebsiteDataCallbacks.add(callbackID, [this, token = throttler().backgroundActivityToken(), completionHandler = WTFMove(completionHandler), sessionID] (WebsiteData websiteData) mutable {
176 #if RELEASE_LOG_DISABLED
177         UNUSED_PARAM(this);
178         UNUSED_PARAM(sessionID);
179 #endif
180         completionHandler(WTFMove(websiteData));
181         RELEASE_LOG_IF(sessionID.isAlwaysOnLoggingAllowed(), ProcessSuspension, "%p - NetworkProcessProxy is releasing a background assertion because the Network process is done fetching Website data", this);
182     });
183
184     send(Messages::NetworkProcess::FetchWebsiteData(sessionID, dataTypes, fetchOptions, callbackID), 0);
185 }
186
187 void NetworkProcessProxy::deleteWebsiteData(PAL::SessionID sessionID, OptionSet<WebsiteDataType> dataTypes, WallTime modifiedSince, CompletionHandler<void ()>&& completionHandler)
188 {
189     auto callbackID = generateCallbackID();
190     RELEASE_LOG_IF(sessionID.isAlwaysOnLoggingAllowed(), ProcessSuspension, "%p - NetworkProcessProxy is taking a background assertion because the Network process is deleting Website data", this);
191
192     m_pendingDeleteWebsiteDataCallbacks.add(callbackID, [this, token = throttler().backgroundActivityToken(), completionHandler = WTFMove(completionHandler), sessionID] () mutable {
193 #if RELEASE_LOG_DISABLED
194         UNUSED_PARAM(this);
195         UNUSED_PARAM(sessionID);
196 #endif
197         completionHandler();
198         RELEASE_LOG_IF(sessionID.isAlwaysOnLoggingAllowed(), ProcessSuspension, "%p - NetworkProcessProxy is releasing a background assertion because the Network process is done deleting Website data", this);
199     });
200     send(Messages::NetworkProcess::DeleteWebsiteData(sessionID, dataTypes, modifiedSince, callbackID), 0);
201 }
202
203 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)
204 {
205     ASSERT(canSendMessage());
206
207     uint64_t callbackID = generateCallbackID();
208     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);
209
210     m_pendingDeleteWebsiteDataForOriginsCallbacks.add(callbackID, [this, token = throttler().backgroundActivityToken(), completionHandler = WTFMove(completionHandler), sessionID] () mutable {
211 #if RELEASE_LOG_DISABLED
212         UNUSED_PARAM(this);
213         UNUSED_PARAM(sessionID);
214 #endif
215         completionHandler();
216         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);
217     });
218
219     send(Messages::NetworkProcess::DeleteWebsiteDataForOrigins(sessionID, dataTypes, origins, cookieHostNames, HSTSCacheHostNames, callbackID), 0);
220 }
221
222 void NetworkProcessProxy::networkProcessCrashed()
223 {
224     clearCallbackStates();
225
226     Vector<std::pair<RefPtr<WebProcessProxy>, Messages::WebProcessProxy::GetNetworkProcessConnection::DelayedReply>> pendingReplies;
227     pendingReplies.reserveInitialCapacity(m_pendingConnectionReplies.size());
228     for (auto& reply : m_pendingConnectionReplies) {
229         if (reply.first)
230             pendingReplies.append(std::make_pair(makeRefPtr(reply.first.get()), WTFMove(reply.second)));
231         else
232             reply.second({ });
233     }
234     m_pendingConnectionReplies.clear();
235
236     // Tell the network process manager to forget about this network process proxy. This will cause us to be deleted.
237     m_processPool.networkProcessCrashed(*this, WTFMove(pendingReplies));
238 }
239
240 void NetworkProcessProxy::clearCallbackStates()
241 {
242     while (!m_pendingFetchWebsiteDataCallbacks.isEmpty())
243         m_pendingFetchWebsiteDataCallbacks.take(m_pendingFetchWebsiteDataCallbacks.begin()->key)(WebsiteData { });
244
245     while (!m_pendingDeleteWebsiteDataCallbacks.isEmpty())
246         m_pendingDeleteWebsiteDataCallbacks.take(m_pendingDeleteWebsiteDataCallbacks.begin()->key)();
247
248     while (!m_pendingDeleteWebsiteDataForOriginsCallbacks.isEmpty())
249         m_pendingDeleteWebsiteDataForOriginsCallbacks.take(m_pendingDeleteWebsiteDataForOriginsCallbacks.begin()->key)();
250 }
251
252 void NetworkProcessProxy::didReceiveMessage(IPC::Connection& connection, IPC::Decoder& decoder)
253 {
254     if (dispatchMessage(connection, decoder))
255         return;
256
257     if (m_processPool.dispatchMessage(connection, decoder))
258         return;
259
260     didReceiveNetworkProcessProxyMessage(connection, decoder);
261 }
262
263 void NetworkProcessProxy::didReceiveSyncMessage(IPC::Connection& connection, IPC::Decoder& decoder, std::unique_ptr<IPC::Encoder>& replyEncoder)
264 {
265     if (dispatchSyncMessage(connection, decoder, replyEncoder))
266         return;
267
268     ASSERT_NOT_REACHED();
269 }
270
271 void NetworkProcessProxy::didClose(IPC::Connection&)
272 {
273     auto protectedProcessPool = makeRef(m_processPool);
274
275     if (m_downloadProxyMap)
276         m_downloadProxyMap->processDidClose();
277 #if ENABLE(LEGACY_CUSTOM_PROTOCOL_MANAGER)
278     m_customProtocolManagerProxy.invalidate();
279 #endif
280
281     m_tokenForHoldingLockedFiles = nullptr;
282     m_tokenForIDBDatabaseHoldingLockedFiles = nullptr;
283     
284     m_syncAllCookiesToken = nullptr;
285     m_syncAllCookiesCounter = 0;
286
287     // This will cause us to be deleted.
288     networkProcessCrashed();
289 }
290
291 void NetworkProcessProxy::didReceiveInvalidMessage(IPC::Connection&, IPC::StringReference, IPC::StringReference)
292 {
293 }
294
295 void NetworkProcessProxy::didCreateNetworkConnectionToWebProcess(const IPC::Attachment& connectionIdentifier)
296 {
297     ASSERT(!m_pendingConnectionReplies.isEmpty());
298
299     // Grab the first pending connection reply.
300     auto reply = m_pendingConnectionReplies.takeFirst().second;
301
302 #if USE(UNIX_DOMAIN_SOCKETS) || OS(WINDOWS)
303     reply(connectionIdentifier);
304 #elif OS(DARWIN)
305     MESSAGE_CHECK(MACH_PORT_VALID(connectionIdentifier.port()));
306     reply(IPC::Attachment(connectionIdentifier.port(), MACH_MSG_TYPE_MOVE_SEND));
307 #else
308     notImplemented();
309 #endif
310 }
311
312 void NetworkProcessProxy::didReceiveAuthenticationChallenge(uint64_t pageID, uint64_t frameID, WebCore::AuthenticationChallenge&& coreChallenge, uint64_t challengeID)
313 {
314 #if ENABLE(SERVICE_WORKER)
315     if (auto* serviceWorkerProcessProxy = m_processPool.serviceWorkerProcessProxyFromPageID(pageID)) {
316         auto authenticationChallenge = AuthenticationChallengeProxy::create(WTFMove(coreChallenge), challengeID, makeRef(*connection()), nullptr);
317         serviceWorkerProcessProxy->didReceiveAuthenticationChallenge(pageID, frameID, WTFMove(authenticationChallenge));
318         return;
319     }
320 #endif
321
322     WebPageProxy* page = WebProcessProxy::webPage(pageID);
323     MESSAGE_CHECK(page);
324
325     auto authenticationChallenge = AuthenticationChallengeProxy::create(WTFMove(coreChallenge), challengeID, makeRef(*connection()), page->secKeyProxyStore(coreChallenge));
326     page->didReceiveAuthenticationChallengeProxy(frameID, WTFMove(authenticationChallenge));
327 }
328
329 void NetworkProcessProxy::didFetchWebsiteData(uint64_t callbackID, const WebsiteData& websiteData)
330 {
331     auto callback = m_pendingFetchWebsiteDataCallbacks.take(callbackID);
332     callback(websiteData);
333 }
334
335 void NetworkProcessProxy::didDeleteWebsiteData(uint64_t callbackID)
336 {
337     auto callback = m_pendingDeleteWebsiteDataCallbacks.take(callbackID);
338     callback();
339 }
340
341 void NetworkProcessProxy::didDeleteWebsiteDataForOrigins(uint64_t callbackID)
342 {
343     auto callback = m_pendingDeleteWebsiteDataForOriginsCallbacks.take(callbackID);
344     callback();
345 }
346
347 void NetworkProcessProxy::didFinishLaunching(ProcessLauncher* launcher, IPC::Connection::Identifier connectionIdentifier)
348 {
349     AuxiliaryProcessProxy::didFinishLaunching(launcher, connectionIdentifier);
350
351     if (!IPC::Connection::identifierIsValid(connectionIdentifier)) {
352         networkProcessCrashed();
353         return;
354     }
355
356     for (unsigned i = 0; i < m_numPendingConnectionRequests; ++i)
357         connection()->send(Messages::NetworkProcess::CreateNetworkConnectionToWebProcess(false, { }), 0);
358     
359     m_numPendingConnectionRequests = 0;
360
361 #if PLATFORM(COCOA)
362     if (m_processPool.processSuppressionEnabled())
363         setProcessSuppressionEnabled(true);
364 #endif
365     
366 #if PLATFORM(IOS_FAMILY)
367     if (xpc_connection_t connection = this->connection()->xpcConnection())
368         m_throttler.didConnectToProcess(xpc_connection_get_pid(connection));
369 #endif
370 }
371
372 void NetworkProcessProxy::logDiagnosticMessage(uint64_t pageID, const String& message, const String& description, WebCore::ShouldSample shouldSample)
373 {
374     WebPageProxy* page = WebProcessProxy::webPage(pageID);
375     // 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,
376     // but for now we simply drop the message in the rare case this happens.
377     if (!page)
378         return;
379
380     page->logDiagnosticMessage(message, description, shouldSample);
381 }
382
383 void NetworkProcessProxy::logDiagnosticMessageWithResult(uint64_t pageID, const String& message, const String& description, uint32_t result, WebCore::ShouldSample shouldSample)
384 {
385     WebPageProxy* page = WebProcessProxy::webPage(pageID);
386     // 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,
387     // but for now we simply drop the message in the rare case this happens.
388     if (!page)
389         return;
390
391     page->logDiagnosticMessageWithResult(message, description, result, shouldSample);
392 }
393
394 void NetworkProcessProxy::logDiagnosticMessageWithValue(uint64_t pageID, const String& message, const String& description, double value, unsigned significantFigures, WebCore::ShouldSample shouldSample)
395 {
396     WebPageProxy* page = WebProcessProxy::webPage(pageID);
397     // 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,
398     // but for now we simply drop the message in the rare case this happens.
399     if (!page)
400         return;
401
402     page->logDiagnosticMessageWithValue(message, description, value, significantFigures, shouldSample);
403 }
404
405 void NetworkProcessProxy::logGlobalDiagnosticMessageWithValue(const String& message, const String& description, double value, unsigned significantFigures, WebCore::ShouldSample shouldSample)
406 {
407     if (auto* page = WebPageProxy::nonEphemeralWebPageProxy())
408         page->logDiagnosticMessageWithValue(message, description, value, significantFigures, shouldSample);
409 }
410
411 #if ENABLE(RESOURCE_LOAD_STATISTICS)
412 void NetworkProcessProxy::dumpResourceLoadStatistics(PAL::SessionID sessionID, CompletionHandler<void(String)>&& completionHandler)
413 {
414     if (!canSendMessage()) {
415         completionHandler({ });
416         return;
417     }
418     
419     sendWithAsyncReply(Messages::NetworkProcess::DumpResourceLoadStatistics(sessionID), WTFMove(completionHandler));
420 }
421
422 void NetworkProcessProxy::updatePrevalentDomainsToBlockCookiesFor(PAL::SessionID sessionID, const Vector<RegistrableDomain>& domainsToBlock, CompletionHandler<void()>&& completionHandler)
423 {
424     if (!canSendMessage()) {
425         completionHandler();
426         return;
427     }
428
429     Vector<RegistrableDomain> registrableDomainsToBlock;
430     registrableDomainsToBlock.reserveInitialCapacity(domainsToBlock.size());
431     for (auto& domain : domainsToBlock)
432         registrableDomainsToBlock.uncheckedAppend(domain);
433
434     sendWithAsyncReply(Messages::NetworkProcess::UpdatePrevalentDomainsToBlockCookiesFor(sessionID, registrableDomainsToBlock), WTFMove(completionHandler));
435 }
436
437 void NetworkProcessProxy::isPrevalentResource(PAL::SessionID sessionID, const RegistrableDomain& resourceDomain, CompletionHandler<void(bool)>&& completionHandler)
438 {
439     if (!canSendMessage()) {
440         completionHandler(false);
441         return;
442     }
443
444     sendWithAsyncReply(Messages::NetworkProcess::IsPrevalentResource(sessionID, resourceDomain), WTFMove(completionHandler));
445 }
446
447 void NetworkProcessProxy::isVeryPrevalentResource(PAL::SessionID sessionID, const RegistrableDomain& resourceDomain, CompletionHandler<void(bool)>&& completionHandler)
448 {
449     if (!canSendMessage()) {
450         completionHandler(false);
451         return;
452     }
453
454     sendWithAsyncReply(Messages::NetworkProcess::IsVeryPrevalentResource(sessionID, resourceDomain), WTFMove(completionHandler));
455 }
456
457 void NetworkProcessProxy::setPrevalentResource(PAL::SessionID sessionID, const RegistrableDomain& resourceDomain, CompletionHandler<void()>&& completionHandler)
458 {
459     if (!canSendMessage()) {
460         completionHandler();
461         return;
462     }
463
464     sendWithAsyncReply(Messages::NetworkProcess::SetPrevalentResource(sessionID, resourceDomain), WTFMove(completionHandler));
465 }
466
467 void NetworkProcessProxy::setPrevalentResourceForDebugMode(PAL::SessionID sessionID, const RegistrableDomain& resourceDomain, CompletionHandler<void()>&& completionHandler)
468 {
469     if (!canSendMessage()) {
470         completionHandler();
471         return;
472     }
473
474     sendWithAsyncReply(Messages::NetworkProcess::SetPrevalentResourceForDebugMode(sessionID, resourceDomain), WTFMove(completionHandler));
475 }
476
477 void NetworkProcessProxy::setVeryPrevalentResource(PAL::SessionID sessionID, const RegistrableDomain& resourceDomain, CompletionHandler<void()>&& completionHandler)
478 {
479     if (!canSendMessage()) {
480         completionHandler();
481         return;
482     }
483
484     sendWithAsyncReply(Messages::NetworkProcess::SetVeryPrevalentResource(sessionID, resourceDomain), WTFMove(completionHandler));
485 }
486
487 void NetworkProcessProxy::setLastSeen(PAL::SessionID sessionID, const RegistrableDomain& resourceDomain, Seconds lastSeen, CompletionHandler<void()>&& completionHandler)
488 {
489     if (!canSendMessage()) {
490         completionHandler();
491         return;
492     }
493     
494     sendWithAsyncReply(Messages::NetworkProcess::SetLastSeen(sessionID, resourceDomain, lastSeen), WTFMove(completionHandler));
495 }
496
497 void NetworkProcessProxy::clearPrevalentResource(PAL::SessionID sessionID, const RegistrableDomain& resourceDomain, CompletionHandler<void()>&& completionHandler)
498 {
499     if (!canSendMessage()) {
500         completionHandler();
501         return;
502     }
503     
504     sendWithAsyncReply(Messages::NetworkProcess::ClearPrevalentResource(sessionID, resourceDomain), WTFMove(completionHandler));
505 }
506     
507 void NetworkProcessProxy::scheduleCookieBlockingUpdate(PAL::SessionID sessionID, CompletionHandler<void()>&& completionHandler)
508 {
509     if (!canSendMessage()) {
510         completionHandler();
511         return;
512     }
513     
514     sendWithAsyncReply(Messages::NetworkProcess::ScheduleCookieBlockingUpdate(sessionID), WTFMove(completionHandler));
515 }
516
517 void NetworkProcessProxy::scheduleClearInMemoryAndPersistent(PAL::SessionID sessionID, Optional<WallTime> modifiedSince, ShouldGrandfatherStatistics shouldGrandfather, CompletionHandler<void()>&& completionHandler)
518 {
519     if (!canSendMessage()) {
520         completionHandler();
521         return;
522     }
523
524     sendWithAsyncReply(Messages::NetworkProcess::ScheduleClearInMemoryAndPersistent(sessionID, modifiedSince, shouldGrandfather), WTFMove(completionHandler));
525 }
526
527 void NetworkProcessProxy::scheduleStatisticsAndDataRecordsProcessing(PAL::SessionID sessionID, CompletionHandler<void()>&& completionHandler)
528 {
529     if (!canSendMessage()) {
530         completionHandler();
531         return;
532     }
533     
534     sendWithAsyncReply(Messages::NetworkProcess::ScheduleStatisticsAndDataRecordsProcessing(sessionID), WTFMove(completionHandler));
535 }
536
537 void NetworkProcessProxy::logUserInteraction(PAL::SessionID sessionID, const RegistrableDomain& resourceDomain, CompletionHandler<void()>&& completionHandler)
538 {
539     if (!canSendMessage()) {
540         completionHandler();
541         return;
542     }
543
544     sendWithAsyncReply(Messages::NetworkProcess::LogUserInteraction(sessionID, resourceDomain), WTFMove(completionHandler));
545 }
546
547 void NetworkProcessProxy::hasHadUserInteraction(PAL::SessionID sessionID, const RegistrableDomain& resourceDomain, CompletionHandler<void(bool)>&& completionHandler)
548 {
549     if (!canSendMessage()) {
550         completionHandler(false);
551         return;
552     }
553     
554     sendWithAsyncReply(Messages::NetworkProcess::HadUserInteraction(sessionID, resourceDomain), WTFMove(completionHandler));
555 }
556
557 void NetworkProcessProxy::clearUserInteraction(PAL::SessionID sessionID, const RegistrableDomain& resourceDomain, CompletionHandler<void()>&& completionHandler)
558 {
559     if (!canSendMessage()) {
560         completionHandler();
561         return;
562     }
563     
564     sendWithAsyncReply(Messages::NetworkProcess::ClearUserInteraction(sessionID, resourceDomain), WTFMove(completionHandler));
565 }
566
567 void NetworkProcessProxy::setAgeCapForClientSideCookies(PAL::SessionID sessionID, Optional<Seconds> seconds, CompletionHandler<void()>&& completionHandler)
568 {
569     if (!canSendMessage()) {
570         completionHandler();
571         return;
572     }
573     
574     sendWithAsyncReply(Messages::NetworkProcess::SetAgeCapForClientSideCookies(sessionID, seconds), WTFMove(completionHandler));
575 }
576
577 void NetworkProcessProxy::setTimeToLiveUserInteraction(PAL::SessionID sessionID, Seconds seconds, CompletionHandler<void()>&& completionHandler)
578 {
579     if (!canSendMessage()) {
580         completionHandler();
581         return;
582     }
583     
584     sendWithAsyncReply(Messages::NetworkProcess::SetTimeToLiveUserInteraction(sessionID, seconds), WTFMove(completionHandler));
585 }
586
587 void NetworkProcessProxy::setNotifyPagesWhenTelemetryWasCaptured(PAL::SessionID sessionID, bool value, CompletionHandler<void()>&& completionHandler)
588 {
589     if (!canSendMessage()) {
590         completionHandler();
591         return;
592     }
593     
594     sendWithAsyncReply(Messages::NetworkProcess::SetNotifyPagesWhenTelemetryWasCaptured(sessionID, value), WTFMove(completionHandler));
595 }
596
597 void NetworkProcessProxy::setNotifyPagesWhenDataRecordsWereScanned(PAL::SessionID sessionID, bool value, CompletionHandler<void()>&& completionHandler)
598 {
599     if (!canSendMessage()) {
600         completionHandler();
601         return;
602     }
603     
604     sendWithAsyncReply(Messages::NetworkProcess::SetNotifyPagesWhenDataRecordsWereScanned(sessionID, value), WTFMove(completionHandler));
605 }
606
607 void NetworkProcessProxy::setIsRunningResourceLoadStatisticsTest(PAL::SessionID sessionID, bool value, CompletionHandler<void()>&& completionHandler)
608 {
609     if (!canSendMessage()) {
610         completionHandler();
611         return;
612     }
613     
614     sendWithAsyncReply(Messages::NetworkProcess::SetIsRunningResourceLoadStatisticsTest(sessionID, value), WTFMove(completionHandler));
615 }
616
617 void NetworkProcessProxy::setSubframeUnderTopFrameDomain(PAL::SessionID sessionID, const RegistrableDomain& subFrameDomain, const RegistrableDomain& topFrameDomain, CompletionHandler<void()>&& completionHandler)
618 {
619     if (!canSendMessage()) {
620         completionHandler();
621         return;
622     }
623     
624     sendWithAsyncReply(Messages::NetworkProcess::SetSubframeUnderTopFrameDomain(sessionID, subFrameDomain, topFrameDomain), WTFMove(completionHandler));
625 }
626
627 void NetworkProcessProxy::isRegisteredAsRedirectingTo(PAL::SessionID sessionID, const RegistrableDomain& domainRedirectedFrom, const RegistrableDomain& domainRedirectedTo, CompletionHandler<void(bool)>&& completionHandler)
628 {
629     if (!canSendMessage()) {
630         completionHandler(false);
631         return;
632     }
633     
634     sendWithAsyncReply(Messages::NetworkProcess::IsRegisteredAsRedirectingTo(sessionID, domainRedirectedFrom, domainRedirectedTo), WTFMove(completionHandler));
635 }
636
637 void NetworkProcessProxy::isRegisteredAsSubFrameUnder(PAL::SessionID sessionID, const RegistrableDomain& subFrameDomain, const RegistrableDomain& topFrameDomain, CompletionHandler<void(bool)>&& completionHandler)
638 {
639     if (!canSendMessage()) {
640         completionHandler(false);
641         return;
642     }
643     
644     sendWithAsyncReply(Messages::NetworkProcess::IsRegisteredAsSubFrameUnder(sessionID, subFrameDomain, topFrameDomain), WTFMove(completionHandler));
645 }
646
647 void NetworkProcessProxy::setSubresourceUnderTopFrameDomain(PAL::SessionID sessionID, const RegistrableDomain& subresourceDomain, const RegistrableDomain& topFrameDomain, CompletionHandler<void()>&& completionHandler)
648 {
649     if (!canSendMessage()) {
650         completionHandler();
651         return;
652     }
653     
654     sendWithAsyncReply(Messages::NetworkProcess::SetSubresourceUnderTopFrameDomain(sessionID, subresourceDomain, topFrameDomain), WTFMove(completionHandler));
655 }
656
657 void NetworkProcessProxy::isRegisteredAsSubresourceUnder(PAL::SessionID sessionID, const RegistrableDomain& subresourceDomain, const RegistrableDomain& topFrameDomain, CompletionHandler<void(bool)>&& completionHandler)
658 {
659     if (!canSendMessage()) {
660         completionHandler(false);
661         return;
662     }
663     
664     sendWithAsyncReply(Messages::NetworkProcess::IsRegisteredAsSubresourceUnder(sessionID, subresourceDomain, topFrameDomain), WTFMove(completionHandler));
665 }
666
667 void NetworkProcessProxy::setSubresourceUniqueRedirectTo(PAL::SessionID sessionID, const RegistrableDomain& subresourceDomain, const RegistrableDomain& domainRedirectedTo, CompletionHandler<void()>&& completionHandler)
668 {
669     if (!canSendMessage()) {
670         completionHandler();
671         return;
672     }
673     
674     sendWithAsyncReply(Messages::NetworkProcess::SetSubresourceUniqueRedirectTo(sessionID, subresourceDomain, domainRedirectedTo), WTFMove(completionHandler));
675 }
676
677 void NetworkProcessProxy::setSubresourceUniqueRedirectFrom(PAL::SessionID sessionID, const RegistrableDomain& subresourceDomain, const RegistrableDomain& domainRedirectedFrom, CompletionHandler<void()>&& completionHandler)
678 {
679     if (!canSendMessage()) {
680         completionHandler();
681         return;
682     }
683     
684     sendWithAsyncReply(Messages::NetworkProcess::SetSubresourceUniqueRedirectFrom(sessionID, subresourceDomain, domainRedirectedFrom), WTFMove(completionHandler));
685 }
686
687 void NetworkProcessProxy::setTopFrameUniqueRedirectTo(PAL::SessionID sessionID, const RegistrableDomain& topFrameDomain, const RegistrableDomain& domainRedirectedTo, CompletionHandler<void()>&& completionHandler)
688 {
689     if (!canSendMessage()) {
690         completionHandler();
691         return;
692     }
693     
694     sendWithAsyncReply(Messages::NetworkProcess::SetTopFrameUniqueRedirectTo(sessionID, topFrameDomain, domainRedirectedTo), WTFMove(completionHandler));
695 }
696
697 void NetworkProcessProxy::setTopFrameUniqueRedirectFrom(PAL::SessionID sessionID, const RegistrableDomain& topFrameDomain, const RegistrableDomain& domainRedirectedFrom, CompletionHandler<void()>&& completionHandler)
698 {
699     if (!canSendMessage()) {
700         completionHandler();
701         return;
702     }
703     
704     sendWithAsyncReply(Messages::NetworkProcess::SetTopFrameUniqueRedirectFrom(sessionID, topFrameDomain, domainRedirectedFrom), WTFMove(completionHandler));
705 }
706
707 void NetworkProcessProxy::isGrandfathered(PAL::SessionID sessionID, const RegistrableDomain& resourceDomain, CompletionHandler<void(bool)>&& completionHandler)
708 {
709     if (!canSendMessage()) {
710         completionHandler(false);
711         return;
712     }
713     
714     sendWithAsyncReply(Messages::NetworkProcess::IsGrandfathered(sessionID, resourceDomain), WTFMove(completionHandler));
715 }
716
717 void NetworkProcessProxy::setGrandfathered(PAL::SessionID sessionID, const RegistrableDomain& resourceDomain, bool isGrandfathered, CompletionHandler<void()>&& completionHandler)
718 {
719     if (!canSendMessage()) {
720         completionHandler();
721         return;
722     }
723     
724     sendWithAsyncReply(Messages::NetworkProcess::SetGrandfathered(sessionID, resourceDomain, isGrandfathered), WTFMove(completionHandler));
725 }
726
727 void NetworkProcessProxy::hasStorageAccessForFrame(PAL::SessionID sessionID, const RegistrableDomain& resourceDomain, const RegistrableDomain& topFrameDomain, uint64_t frameID, uint64_t pageID, CompletionHandler<void(bool)>&& completionHandler)
728 {
729     if (!canSendMessage()) {
730         completionHandler(false);
731         return;
732     }
733
734     sendWithAsyncReply(Messages::NetworkProcess::HasStorageAccessForFrame(sessionID, resourceDomain, topFrameDomain, frameID, pageID), WTFMove(completionHandler));
735 }
736
737 void NetworkProcessProxy::hasStorageAccess(PAL::SessionID sessionID, const RegistrableDomain& resourceDomain, const RegistrableDomain& topFrameDomain, Optional<uint64_t> frameID, uint64_t pageID, CompletionHandler<void(bool)>&& completionHandler)
738 {
739     if (!canSendMessage()) {
740         completionHandler(false);
741         return;
742     }
743     
744     sendWithAsyncReply(Messages::NetworkProcess::HasStorageAccess(sessionID, resourceDomain, topFrameDomain, frameID, pageID), WTFMove(completionHandler));
745 }
746
747 void NetworkProcessProxy::requestStorageAccess(PAL::SessionID sessionID, const RegistrableDomain& resourceDomain, const RegistrableDomain& topFrameDomain, Optional<uint64_t> frameID, uint64_t pageID, CompletionHandler<void(StorageAccessStatus)>&& completionHandler)
748 {
749     if (!canSendMessage()) {
750         completionHandler(StorageAccessStatus::CannotRequestAccess);
751         return;
752     }
753
754     sendWithAsyncReply(Messages::NetworkProcess::RequestStorageAccess(sessionID, resourceDomain, topFrameDomain, frameID, pageID), WTFMove(completionHandler));
755 }
756
757 void NetworkProcessProxy::requestStorageAccessConfirm(uint64_t pageID, uint64_t frameID, const RegistrableDomain& subFrameDomain, const RegistrableDomain& topFrameDomain, CompletionHandler<void(bool)>&& completionHandler)
758 {
759     WebPageProxy* page = WebProcessProxy::webPage(pageID);
760     if (!page) {
761         completionHandler(false);
762         return;
763     }
764     
765     page->requestStorageAccessConfirm(subFrameDomain, topFrameDomain, frameID, WTFMove(completionHandler));
766 }
767
768 void NetworkProcessProxy::grantStorageAccess(PAL::SessionID sessionID, const RegistrableDomain& resourceDomain, const RegistrableDomain& topFrameDomain, Optional<uint64_t> frameID, uint64_t pageID, bool userWasPrompted, CompletionHandler<void(bool)>&& completionHandler)
769 {
770     if (!canSendMessage()) {
771         completionHandler(false);
772         return;
773     }
774
775     // FIXME: We should not accept an optional frame ID since we call frameID.value() unconditionally.
776     if (!frameID) {
777         completionHandler(false);
778         return;
779     }
780
781     sendWithAsyncReply(Messages::NetworkProcess::GrantStorageAccess(sessionID, resourceDomain, topFrameDomain, frameID.value(), pageID, userWasPrompted), WTFMove(completionHandler));
782 }
783
784 void NetworkProcessProxy::removeAllStorageAccess(PAL::SessionID sessionID, CompletionHandler<void()>&& completionHandler)
785 {
786     if (!canSendMessage()) {
787         completionHandler();
788         return;
789     }
790
791     sendWithAsyncReply(Messages::NetworkProcess::RemoveAllStorageAccess(sessionID), WTFMove(completionHandler));
792 }
793
794 void NetworkProcessProxy::getAllStorageAccessEntries(PAL::SessionID sessionID, CompletionHandler<void(Vector<String> domains)>&& completionHandler)
795 {
796     if (!canSendMessage()) {
797         completionHandler({ });
798         return;
799     }
800
801     sendWithAsyncReply(Messages::NetworkProcess::GetAllStorageAccessEntries(sessionID), WTFMove(completionHandler));
802 }
803
804 void NetworkProcessProxy::setCacheMaxAgeCapForPrevalentResources(PAL::SessionID sessionID, Seconds seconds, CompletionHandler<void()>&& completionHandler)
805 {
806     if (!canSendMessage()) {
807         completionHandler();
808         return;
809     }
810
811     sendWithAsyncReply(Messages::NetworkProcess::SetCacheMaxAgeCapForPrevalentResources(sessionID, seconds), WTFMove(completionHandler));
812 }
813
814 void NetworkProcessProxy::setCacheMaxAgeCap(PAL::SessionID sessionID, Seconds seconds, CompletionHandler<void()>&& completionHandler)
815 {
816     if (!canSendMessage()) {
817         completionHandler();
818         return;
819     }
820     
821     sendWithAsyncReply(Messages::NetworkProcess::SetCacheMaxAgeCapForPrevalentResources(sessionID, seconds), WTFMove(completionHandler));
822 }
823
824 void NetworkProcessProxy::setGrandfatheringTime(PAL::SessionID sessionID, Seconds seconds, CompletionHandler<void()>&& completionHandler)
825 {
826     if (!canSendMessage()) {
827         completionHandler();
828         return;
829     }
830     
831     sendWithAsyncReply(Messages::NetworkProcess::SetGrandfatheringTime(sessionID, seconds), WTFMove(completionHandler));
832 }
833
834 void NetworkProcessProxy::setMaxStatisticsEntries(PAL::SessionID sessionID, size_t maximumEntryCount, CompletionHandler<void()>&& completionHandler)
835 {
836     if (!canSendMessage()) {
837         completionHandler();
838         return;
839     }
840
841     sendWithAsyncReply(Messages::NetworkProcess::SetMaxStatisticsEntries(sessionID, maximumEntryCount), WTFMove(completionHandler));
842 }
843
844 void NetworkProcessProxy::setMinimumTimeBetweenDataRecordsRemoval(PAL::SessionID sessionID, Seconds seconds, CompletionHandler<void()>&& completionHandler)
845 {
846     if (!canSendMessage()) {
847         completionHandler();
848         return;
849     }
850     
851     sendWithAsyncReply(Messages::NetworkProcess::SetMinimumTimeBetweenDataRecordsRemoval(sessionID, seconds), WTFMove(completionHandler));
852 }
853
854 void NetworkProcessProxy::setPruneEntriesDownTo(PAL::SessionID sessionID, size_t pruneTargetCount, CompletionHandler<void()>&& completionHandler)
855 {
856     if (!canSendMessage()) {
857         completionHandler();
858         return;
859     }
860
861     sendWithAsyncReply(Messages::NetworkProcess::SetPruneEntriesDownTo(sessionID, pruneTargetCount), WTFMove(completionHandler));
862 }
863
864 void NetworkProcessProxy::setShouldClassifyResourcesBeforeDataRecordsRemoval(PAL::SessionID sessionID, bool value, CompletionHandler<void()>&& completionHandler)
865 {
866     if (!canSendMessage()) {
867         completionHandler();
868         return;
869     }
870     
871     sendWithAsyncReply(Messages::NetworkProcess::SetShouldClassifyResourcesBeforeDataRecordsRemoval(sessionID, value), WTFMove(completionHandler));
872 }
873
874 void NetworkProcessProxy::setResourceLoadStatisticsDebugMode(PAL::SessionID sessionID, bool debugMode, CompletionHandler<void()>&& completionHandler)
875 {
876     if (!canSendMessage()) {
877         completionHandler();
878         return;
879     }
880     
881     sendWithAsyncReply(Messages::NetworkProcess::SetResourceLoadStatisticsDebugMode(sessionID, debugMode), WTFMove(completionHandler));
882 }
883
884 void NetworkProcessProxy::resetCacheMaxAgeCapForPrevalentResources(PAL::SessionID sessionID, CompletionHandler<void()>&& completionHandler)
885 {
886     if (!canSendMessage()) {
887         completionHandler();
888         return;
889     }
890     
891     sendWithAsyncReply(Messages::NetworkProcess::ResetCacheMaxAgeCapForPrevalentResources(sessionID), WTFMove(completionHandler));
892 }
893
894 void NetworkProcessProxy::resetParametersToDefaultValues(PAL::SessionID sessionID, CompletionHandler<void()>&& completionHandler)
895 {
896     if (!canSendMessage()) {
897         completionHandler();
898         return;
899     }
900     
901     sendWithAsyncReply(Messages::NetworkProcess::ResetParametersToDefaultValues(sessionID), WTFMove(completionHandler));
902 }
903
904 void NetworkProcessProxy::submitTelemetry(PAL::SessionID sessionID, CompletionHandler<void()>&& completionHandler)
905 {
906     if (!canSendMessage()) {
907         completionHandler();
908         return;
909     }
910     
911     sendWithAsyncReply(Messages::NetworkProcess::SubmitTelemetry(sessionID), WTFMove(completionHandler));
912 }
913
914 void NetworkProcessProxy::scheduleClearInMemoryAndPersistent(PAL::SessionID sessionID, ShouldGrandfatherStatistics shouldGrandfather, CompletionHandler<void()>&& completionHandler)
915 {
916     if (!canSendMessage()) {
917         completionHandler();
918         return;
919     }
920
921     sendWithAsyncReply(Messages::NetworkProcess::ScheduleClearInMemoryAndPersistent(sessionID, { }, shouldGrandfather), WTFMove(completionHandler));
922 }
923
924 void NetworkProcessProxy::logTestingEvent(PAL::SessionID sessionID, const String& event)
925 {
926     if (auto* websiteDataStore = websiteDataStoreFromSessionID(sessionID))
927         websiteDataStore->logTestingEvent(event);
928 }
929
930 void NetworkProcessProxy::notifyResourceLoadStatisticsProcessed()
931 {
932     WebProcessProxy::notifyPageStatisticsAndDataRecordsProcessed();
933 }
934
935 void NetworkProcessProxy::notifyWebsiteDataDeletionForRegistrableDomainsFinished()
936 {
937     WebProcessProxy::notifyWebsiteDataDeletionForRegistrableDomainsFinished();
938 }
939
940 void NetworkProcessProxy::notifyWebsiteDataScanForRegistrableDomainsFinished()
941 {
942     WebProcessProxy::notifyWebsiteDataScanForRegistrableDomainsFinished();
943 }
944
945 void NetworkProcessProxy::notifyResourceLoadStatisticsTelemetryFinished(unsigned totalPrevalentResources, unsigned totalPrevalentResourcesWithUserInteraction, unsigned top3SubframeUnderTopFrameOrigins)
946 {
947     API::Dictionary::MapType messageBody;
948     messageBody.set("TotalPrevalentResources"_s, API::UInt64::create(totalPrevalentResources));
949     messageBody.set("TotalPrevalentResourcesWithUserInteraction"_s, API::UInt64::create(totalPrevalentResourcesWithUserInteraction));
950     messageBody.set("Top3SubframeUnderTopFrameOrigins"_s, API::UInt64::create(top3SubframeUnderTopFrameOrigins));
951
952     WebProcessProxy::notifyPageStatisticsTelemetryFinished(API::Dictionary::create(messageBody).ptr());
953 }
954
955 void NetworkProcessProxy::committedCrossSiteLoadWithLinkDecoration(PAL::SessionID sessionID, const RegistrableDomain& fromDomain, const RegistrableDomain& toDomain, uint64_t pageID, CompletionHandler<void()>&& completionHandler)
956 {
957     if (!canSendMessage()) {
958         completionHandler();
959         return;
960     }
961     
962     sendWithAsyncReply(Messages::NetworkProcess::CommittedCrossSiteLoadWithLinkDecoration(sessionID, fromDomain, toDomain, pageID), WTFMove(completionHandler));
963 }
964
965 void NetworkProcessProxy::setCrossSiteLoadWithLinkDecorationForTesting(PAL::SessionID sessionID, const RegistrableDomain& fromDomain, const RegistrableDomain& toDomain, CompletionHandler<void()>&& completionHandler)
966 {
967     if (!canSendMessage()) {
968         completionHandler();
969         return;
970     }
971     
972     sendWithAsyncReply(Messages::NetworkProcess::SetCrossSiteLoadWithLinkDecorationForTesting(sessionID, fromDomain, toDomain), WTFMove(completionHandler));
973 }
974
975 void NetworkProcessProxy::resetCrossSiteLoadsWithLinkDecorationForTesting(PAL::SessionID sessionID, CompletionHandler<void()>&& completionHandler)
976 {
977     if (!canSendMessage()) {
978         completionHandler();
979         return;
980     }
981     
982     sendWithAsyncReply(Messages::NetworkProcess::ResetCrossSiteLoadsWithLinkDecorationForTesting(sessionID), WTFMove(completionHandler));
983 }
984
985 void NetworkProcessProxy::deleteCookiesForTesting(PAL::SessionID sessionID, const RegistrableDomain& domain, bool includeHttpOnlyCookies, CompletionHandler<void()>&& completionHandler)
986 {
987     if (!canSendMessage()) {
988         completionHandler();
989         return;
990     }
991     
992     sendWithAsyncReply(Messages::NetworkProcess::DeleteCookiesForTesting(sessionID, domain, includeHttpOnlyCookies), WTFMove(completionHandler));
993 }
994
995 void NetworkProcessProxy::deleteWebsiteDataInUIProcessForRegistrableDomains(PAL::SessionID sessionID, OptionSet<WebsiteDataType> dataTypes, OptionSet<WebsiteDataFetchOption> fetchOptions, Vector<RegistrableDomain> domains, CompletionHandler<void(HashSet<WebCore::RegistrableDomain>&&)>&& completionHandler)
996 {
997     auto* websiteDataStore = websiteDataStoreFromSessionID(sessionID);
998     if (!websiteDataStore || dataTypes.isEmpty() || domains.isEmpty()) {
999         completionHandler({ });
1000         return;
1001     }
1002
1003     websiteDataStore->fetchDataForRegistrableDomains(dataTypes, fetchOptions, domains, [dataTypes, websiteDataStore = makeRef(*websiteDataStore), completionHandler = WTFMove(completionHandler)] (Vector<WebsiteDataRecord>&& matchingDataRecords, HashSet<WebCore::RegistrableDomain>&& domainsWithMatchingDataRecords) mutable {
1004         websiteDataStore->removeData(dataTypes, WTFMove(matchingDataRecords), [domainsWithMatchingDataRecords = WTFMove(domainsWithMatchingDataRecords), completionHandler = WTFMove(completionHandler)] () mutable {
1005             completionHandler(WTFMove(domainsWithMatchingDataRecords));
1006         });
1007     });
1008 }
1009
1010 #endif // ENABLE(RESOURCE_LOAD_STATISTICS)
1011
1012 void NetworkProcessProxy::sendProcessWillSuspendImminently()
1013 {
1014     if (!canSendMessage())
1015         return;
1016
1017     bool handled = false;
1018     sendSync(Messages::NetworkProcess::ProcessWillSuspendImminently(), Messages::NetworkProcess::ProcessWillSuspendImminently::Reply(handled), 0, 1_s);
1019 }
1020     
1021 void NetworkProcessProxy::sendPrepareToSuspend()
1022 {
1023     if (canSendMessage())
1024         send(Messages::NetworkProcess::PrepareToSuspend(), 0);
1025 }
1026
1027 void NetworkProcessProxy::sendCancelPrepareToSuspend()
1028 {
1029     if (canSendMessage())
1030         send(Messages::NetworkProcess::CancelPrepareToSuspend(), 0);
1031 }
1032
1033 void NetworkProcessProxy::sendProcessDidResume()
1034 {
1035     if (canSendMessage())
1036         send(Messages::NetworkProcess::ProcessDidResume(), 0);
1037 }
1038
1039 void NetworkProcessProxy::processReadyToSuspend()
1040 {
1041     m_throttler.processReadyToSuspend();
1042 }
1043
1044 void NetworkProcessProxy::didSetAssertionState(AssertionState)
1045 {
1046 }
1047     
1048 void NetworkProcessProxy::setIsHoldingLockedFiles(bool isHoldingLockedFiles)
1049 {
1050     if (!isHoldingLockedFiles) {
1051         RELEASE_LOG(ProcessSuspension, "UIProcess is releasing a background assertion because the Network process is no longer holding locked files");
1052         m_tokenForHoldingLockedFiles = nullptr;
1053         return;
1054     }
1055     if (!m_tokenForHoldingLockedFiles) {
1056         RELEASE_LOG(ProcessSuspension, "UIProcess is taking a background assertion because the Network process is holding locked files");
1057         m_tokenForHoldingLockedFiles = m_throttler.backgroundActivityToken();
1058     }
1059 }
1060
1061 void NetworkProcessProxy::setIsIDBDatabaseHoldingLockedFiles(bool isIDBDatabaseHoldingLockedFiles)
1062 {
1063     if (!isIDBDatabaseHoldingLockedFiles) {
1064         RELEASE_LOG(ProcessSuspension, "UIProcess is releasing a background assertion because the Network process is no longer holding locked files for IDBDatabase");
1065         m_tokenForIDBDatabaseHoldingLockedFiles = nullptr;
1066         return;
1067     }
1068     if (!m_tokenForIDBDatabaseHoldingLockedFiles) {
1069         RELEASE_LOG(ProcessSuspension, "UIProcess is taking a background assertion because the Network process is holding locked files for IDBDatabase");
1070         m_tokenForIDBDatabaseHoldingLockedFiles = m_throttler.backgroundActivityToken();
1071     }
1072 }
1073
1074 void NetworkProcessProxy::syncAllCookies()
1075 {
1076     send(Messages::NetworkProcess::SyncAllCookies(), 0);
1077     
1078     ++m_syncAllCookiesCounter;
1079     if (m_syncAllCookiesToken)
1080         return;
1081     
1082     RELEASE_LOG(ProcessSuspension, "%p - NetworkProcessProxy is taking a background assertion because the Network process is syncing cookies", this);
1083     m_syncAllCookiesToken = throttler().backgroundActivityToken();
1084 }
1085     
1086 void NetworkProcessProxy::didSyncAllCookies()
1087 {
1088     ASSERT(m_syncAllCookiesCounter);
1089
1090     --m_syncAllCookiesCounter;
1091     if (!m_syncAllCookiesCounter) {
1092         RELEASE_LOG(ProcessSuspension, "%p - NetworkProcessProxy is releasing a background assertion because the Network process is done syncing cookies", this);
1093         m_syncAllCookiesToken = nullptr;
1094     }
1095 }
1096
1097 void NetworkProcessProxy::addSession(Ref<WebsiteDataStore>&& store)
1098 {
1099     if (canSendMessage())
1100         send(Messages::NetworkProcess::AddWebsiteDataStore { store->parameters() }, 0);
1101     auto sessionID = store->sessionID();
1102     if (!sessionID.isEphemeral()) {
1103 #if ENABLE(INDEXED_DATABASE)
1104         createSymLinkForFileUpgrade(store->resolvedIndexedDatabaseDirectory());
1105 #endif
1106         m_websiteDataStores.set(sessionID, WTFMove(store));
1107     }
1108 }
1109
1110 void NetworkProcessProxy::removeSession(PAL::SessionID sessionID)
1111 {
1112     if (canSendMessage())
1113         send(Messages::NetworkProcess::DestroySession { sessionID }, 0);
1114     if (!sessionID.isEphemeral())
1115         m_websiteDataStores.remove(sessionID);
1116 }
1117
1118 WebsiteDataStore* NetworkProcessProxy::websiteDataStoreFromSessionID(PAL::SessionID sessionID)
1119 {
1120     auto iterator = m_websiteDataStores.find(sessionID);
1121     if (iterator != m_websiteDataStores.end())
1122         return iterator->value.get();
1123
1124     if (auto* websiteDataStore = m_processPool.websiteDataStore()) {
1125         if (sessionID == websiteDataStore->websiteDataStore().sessionID())
1126             return &websiteDataStore->websiteDataStore();
1127     }
1128
1129     if (sessionID != PAL::SessionID::defaultSessionID())
1130         return nullptr;
1131
1132     return &API::WebsiteDataStore::defaultDataStore()->websiteDataStore();
1133 }
1134
1135 void NetworkProcessProxy::retrieveCacheStorageParameters(PAL::SessionID sessionID)
1136 {
1137     auto* store = websiteDataStoreFromSessionID(sessionID);
1138
1139     if (!store) {
1140         RELEASE_LOG_ERROR(CacheStorage, "%p - NetworkProcessProxy is unable to retrieve CacheStorage parameters from the given session ID %" PRIu64, this, sessionID.sessionID());
1141         send(Messages::NetworkProcess::SetCacheStorageParameters { sessionID, { }, { } }, 0);
1142         return;
1143     }
1144
1145     auto& cacheStorageDirectory = store->cacheStorageDirectory();
1146     SandboxExtension::Handle cacheStorageDirectoryExtensionHandle;
1147     if (!cacheStorageDirectory.isEmpty())
1148         SandboxExtension::createHandleForReadWriteDirectory(cacheStorageDirectory, cacheStorageDirectoryExtensionHandle);
1149
1150     send(Messages::NetworkProcess::SetCacheStorageParameters { sessionID, cacheStorageDirectory, cacheStorageDirectoryExtensionHandle }, 0);
1151 }
1152
1153 #if ENABLE(CONTENT_EXTENSIONS)
1154 void NetworkProcessProxy::contentExtensionRules(UserContentControllerIdentifier identifier)
1155 {
1156     if (auto* webUserContentControllerProxy = WebUserContentControllerProxy::get(identifier)) {
1157         m_webUserContentControllerProxies.add(webUserContentControllerProxy);
1158         webUserContentControllerProxy->addNetworkProcess(*this);
1159
1160         auto rules = WTF::map(webUserContentControllerProxy->contentExtensionRules(), [](auto&& keyValue) -> std::pair<String, WebCompiledContentRuleListData> {
1161             return std::make_pair(keyValue.value->name(), keyValue.value->compiledRuleList().data());
1162         });
1163         send(Messages::NetworkContentRuleListManager::AddContentRuleLists { identifier, rules }, 0);
1164         return;
1165     }
1166     send(Messages::NetworkContentRuleListManager::AddContentRuleLists { identifier, { } }, 0);
1167 }
1168
1169 void NetworkProcessProxy::didDestroyWebUserContentControllerProxy(WebUserContentControllerProxy& proxy)
1170 {
1171     send(Messages::NetworkContentRuleListManager::Remove { proxy.identifier() }, 0);
1172     m_webUserContentControllerProxies.remove(&proxy);
1173 }
1174 #endif
1175
1176 void NetworkProcessProxy::dumpAdClickAttribution(PAL::SessionID sessionID, CompletionHandler<void(const String&)>&& completionHandler)
1177 {
1178     if (!canSendMessage()) {
1179         completionHandler(emptyString());
1180         return;
1181     }
1182
1183     sendWithAsyncReply(Messages::NetworkProcess::DumpAdClickAttribution(sessionID), WTFMove(completionHandler));
1184 }
1185
1186 void NetworkProcessProxy::clearAdClickAttribution(PAL::SessionID sessionID, CompletionHandler<void()>&& completionHandler)
1187 {
1188     if (!canSendMessage()) {
1189         completionHandler();
1190         return;
1191     }
1192     
1193     sendWithAsyncReply(Messages::NetworkProcess::ClearAdClickAttribution(sessionID), WTFMove(completionHandler));
1194 }
1195
1196 void NetworkProcessProxy::sendProcessDidTransitionToForeground()
1197 {
1198     send(Messages::NetworkProcess::ProcessDidTransitionToForeground(), 0);
1199 }
1200
1201 void NetworkProcessProxy::sendProcessDidTransitionToBackground()
1202 {
1203     send(Messages::NetworkProcess::ProcessDidTransitionToBackground(), 0);
1204 }
1205
1206 #if ENABLE(SANDBOX_EXTENSIONS)
1207 void NetworkProcessProxy::getSandboxExtensionsForBlobFiles(const Vector<String>& paths, Messages::NetworkProcessProxy::GetSandboxExtensionsForBlobFiles::AsyncReply&& reply)
1208 {
1209     SandboxExtension::HandleArray extensions;
1210     extensions.allocate(paths.size());
1211     for (size_t i = 0; i < paths.size(); ++i) {
1212         // ReadWrite is required for creating hard links, which is something that might be done with these extensions.
1213         SandboxExtension::createHandle(paths[i], SandboxExtension::Type::ReadWrite, extensions[i]);
1214     }
1215     reply(WTFMove(extensions));
1216 }
1217 #endif
1218
1219 #if ENABLE(SERVICE_WORKER)
1220 void NetworkProcessProxy::establishWorkerContextConnectionToNetworkProcess(RegistrableDomain&& registrableDomain)
1221 {
1222     m_processPool.establishWorkerContextConnectionToNetworkProcess(*this, WTFMove(registrableDomain), WTF::nullopt);
1223 }
1224
1225 void NetworkProcessProxy::establishWorkerContextConnectionToNetworkProcessForExplicitSession(RegistrableDomain&& registrableDomain, PAL::SessionID sessionID)
1226 {
1227     m_processPool.establishWorkerContextConnectionToNetworkProcess(*this, WTFMove(registrableDomain), sessionID);
1228 }
1229 #endif
1230
1231 void NetworkProcessProxy::requestStorageSpace(PAL::SessionID sessionID, const WebCore::ClientOrigin& origin, uint64_t quota, uint64_t currentSize, uint64_t spaceRequired, CompletionHandler<void(Optional<uint64_t> quota)>&& completionHandler)
1232 {
1233     auto* store = websiteDataStoreFromSessionID(sessionID);
1234
1235     if (!store) {
1236         completionHandler({ });
1237         return;
1238     }
1239
1240     store->client().requestStorageSpace(origin.topOrigin, origin.clientOrigin, quota, currentSize, spaceRequired, WTFMove(completionHandler));
1241 }
1242
1243 void NetworkProcessProxy::takeUploadAssertion()
1244 {
1245     ASSERT(!m_uploadAssertion);
1246     m_uploadAssertion = std::make_unique<ProcessAssertion>(processIdentifier(), "WebKit uploads"_s, AssertionState::UnboundedNetworking);
1247 }
1248
1249 void NetworkProcessProxy::clearUploadAssertion()
1250 {
1251     ASSERT(m_uploadAssertion);
1252     m_uploadAssertion = nullptr;
1253 }
1254
1255 #if ENABLE(INDEXED_DATABASE)
1256 void NetworkProcessProxy::createSymLinkForFileUpgrade(const String& indexedDatabaseDirectory)
1257 {
1258     if (indexedDatabaseDirectory.isEmpty())
1259         return;
1260
1261     String oldVersionDirectory = FileSystem::pathByAppendingComponent(indexedDatabaseDirectory, "v0");
1262     FileSystem::deleteEmptyDirectory(oldVersionDirectory);
1263     if (!FileSystem::fileExists(oldVersionDirectory))
1264         FileSystem::createSymbolicLink(indexedDatabaseDirectory, oldVersionDirectory);
1265 }
1266 #endif
1267
1268 } // namespace WebKit
1269
1270 #undef MESSAGE_CHECK
1271 #undef MESSAGE_CHECK_URL