Resource Load Statistics: Make WebResourceLoadStatisticsStore::updateCookiePartitioni...
[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 "StorageProcessMessages.h"
41 #include "WebCompiledContentRuleList.h"
42 #include "WebPageProxy.h"
43 #include "WebProcessMessages.h"
44 #include "WebProcessPool.h"
45 #include "WebUserContentControllerProxy.h"
46 #include "WebsiteData.h"
47 #include <wtf/CompletionHandler.h>
48
49 #if ENABLE(SEC_ITEM_SHIM)
50 #include "SecItemShimProxy.h"
51 #endif
52
53 #if PLATFORM(IOS)
54 #include <wtf/spi/darwin/XPCSPI.h>
55 #endif
56
57 #define MESSAGE_CHECK(assertion) MESSAGE_CHECK_BASE(assertion, connection())
58
59 using namespace WebCore;
60
61 namespace WebKit {
62
63 static uint64_t generateCallbackID()
64 {
65     static uint64_t callbackID;
66
67     return ++callbackID;
68 }
69
70 Ref<NetworkProcessProxy> NetworkProcessProxy::create(WebProcessPool& processPool)
71 {
72     return adoptRef(*new NetworkProcessProxy(processPool));
73 }
74
75 NetworkProcessProxy::NetworkProcessProxy(WebProcessPool& processPool)
76     : ChildProcessProxy(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
101 void NetworkProcessProxy::getLaunchOptions(ProcessLauncher::LaunchOptions& launchOptions)
102 {
103     launchOptions.processType = ProcessLauncher::ProcessType::Network;
104     ChildProcessProxy::getLaunchOptions(launchOptions);
105 }
106
107 void NetworkProcessProxy::connectionWillOpen(IPC::Connection& connection)
108 {
109 #if ENABLE(SEC_ITEM_SHIM)
110     SecItemShimProxy::singleton().initializeConnection(connection);
111 #else
112     UNUSED_PARAM(connection);
113 #endif
114 }
115
116 void NetworkProcessProxy::processWillShutDown(IPC::Connection& connection)
117 {
118     ASSERT_UNUSED(connection, this->connection() == &connection);
119 }
120
121 void NetworkProcessProxy::getNetworkProcessConnection(Messages::WebProcessProxy::GetNetworkProcessConnection::DelayedReply&& reply)
122 {
123     m_pendingConnectionReplies.append(WTFMove(reply));
124
125     if (state() == State::Launching) {
126         m_numPendingConnectionRequests++;
127         return;
128     }
129
130     connection()->send(Messages::NetworkProcess::CreateNetworkConnectionToWebProcess(), 0, IPC::SendOption::DispatchMessageEvenWhenWaitingForSyncReply);
131 }
132
133 DownloadProxy* NetworkProcessProxy::createDownloadProxy(const ResourceRequest& resourceRequest)
134 {
135     if (!m_downloadProxyMap)
136         m_downloadProxyMap = std::make_unique<DownloadProxyMap>(this);
137
138     return m_downloadProxyMap->createDownloadProxy(m_processPool, resourceRequest);
139 }
140
141 void NetworkProcessProxy::fetchWebsiteData(PAL::SessionID sessionID, OptionSet<WebsiteDataType> dataTypes, OptionSet<WebsiteDataFetchOption> fetchOptions, WTF::Function<void (WebsiteData)>&& completionHandler)
142 {
143     ASSERT(canSendMessage());
144
145     uint64_t callbackID = generateCallbackID();
146     RELEASE_LOG_IF(sessionID.isAlwaysOnLoggingAllowed(), ProcessSuspension, "%p - NetworkProcessProxy is taking a background assertion because the Network process is fetching Website data", this);
147
148     m_pendingFetchWebsiteDataCallbacks.add(callbackID, [this, token = throttler().backgroundActivityToken(), completionHandler = WTFMove(completionHandler), sessionID](WebsiteData websiteData) {
149 #if RELEASE_LOG_DISABLED
150         UNUSED_PARAM(this);
151         UNUSED_PARAM(sessionID);
152 #endif
153         completionHandler(WTFMove(websiteData));
154         RELEASE_LOG_IF(sessionID.isAlwaysOnLoggingAllowed(), ProcessSuspension, "%p - NetworkProcessProxy is releasing a background assertion because the Network process is done fetching Website data", this);
155     });
156
157     send(Messages::NetworkProcess::FetchWebsiteData(sessionID, dataTypes, fetchOptions, callbackID), 0);
158 }
159
160 void NetworkProcessProxy::deleteWebsiteData(PAL::SessionID sessionID, OptionSet<WebsiteDataType> dataTypes, WallTime modifiedSince, WTF::Function<void ()>&& completionHandler)
161 {
162     auto callbackID = generateCallbackID();
163     RELEASE_LOG_IF(sessionID.isAlwaysOnLoggingAllowed(), ProcessSuspension, "%p - NetworkProcessProxy is taking a background assertion because the Network process is deleting Website data", this);
164
165     m_pendingDeleteWebsiteDataCallbacks.add(callbackID, [this, token = throttler().backgroundActivityToken(), completionHandler = WTFMove(completionHandler), sessionID] {
166 #if RELEASE_LOG_DISABLED
167         UNUSED_PARAM(this);
168         UNUSED_PARAM(sessionID);
169 #endif
170         completionHandler();
171         RELEASE_LOG_IF(sessionID.isAlwaysOnLoggingAllowed(), ProcessSuspension, "%p - NetworkProcessProxy is releasing a background assertion because the Network process is done deleting Website data", this);
172     });
173     send(Messages::NetworkProcess::DeleteWebsiteData(sessionID, dataTypes, modifiedSince, callbackID), 0);
174 }
175
176 void NetworkProcessProxy::deleteWebsiteDataForOrigins(PAL::SessionID sessionID, OptionSet<WebsiteDataType> dataTypes, const Vector<WebCore::SecurityOriginData>& origins, const Vector<String>& cookieHostNames, WTF::Function<void()>&& completionHandler)
177 {
178     ASSERT(canSendMessage());
179
180     uint64_t callbackID = generateCallbackID();
181     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);
182
183     m_pendingDeleteWebsiteDataForOriginsCallbacks.add(callbackID, [this, token = throttler().backgroundActivityToken(), completionHandler = WTFMove(completionHandler), sessionID] {
184 #if RELEASE_LOG_DISABLED
185         UNUSED_PARAM(this);
186         UNUSED_PARAM(sessionID);
187 #endif
188         completionHandler();
189         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);
190     });
191
192     send(Messages::NetworkProcess::DeleteWebsiteDataForOrigins(sessionID, dataTypes, origins, cookieHostNames, callbackID), 0);
193 }
194
195 void NetworkProcessProxy::networkProcessCrashed()
196 {
197     clearCallbackStates();
198
199     Vector<Messages::WebProcessProxy::GetNetworkProcessConnection::DelayedReply> pendingReplies;
200     pendingReplies.reserveInitialCapacity(m_pendingConnectionReplies.size());
201     for (auto& reply : m_pendingConnectionReplies)
202         pendingReplies.append(WTFMove(reply));
203
204     // Tell the network process manager to forget about this network process proxy. This may cause us to be deleted.
205     m_processPool.networkProcessCrashed(*this, WTFMove(pendingReplies));
206 }
207
208 void NetworkProcessProxy::networkProcessFailedToLaunch()
209 {
210     // The network process must have crashed or exited, send any pending sync replies we might have.
211     while (!m_pendingConnectionReplies.isEmpty()) {
212         auto reply = m_pendingConnectionReplies.takeFirst();
213
214 #if USE(UNIX_DOMAIN_SOCKETS) || OS(WINDOWS)
215         reply(IPC::Attachment());
216 #elif OS(DARWIN)
217         reply(IPC::Attachment(0, MACH_MSG_TYPE_MOVE_SEND));
218 #else
219         notImplemented();
220 #endif
221     }
222     clearCallbackStates();
223     // Tell the network process manager to forget about this network process proxy. This may cause us to be deleted.
224     m_processPool.networkProcessFailedToLaunch(*this);
225 }
226
227 void NetworkProcessProxy::clearCallbackStates()
228 {
229     while (!m_pendingFetchWebsiteDataCallbacks.isEmpty())
230         m_pendingFetchWebsiteDataCallbacks.take(m_pendingFetchWebsiteDataCallbacks.begin()->key)(WebsiteData { });
231
232     while (!m_pendingDeleteWebsiteDataCallbacks.isEmpty())
233         m_pendingDeleteWebsiteDataCallbacks.take(m_pendingDeleteWebsiteDataCallbacks.begin()->key)();
234
235     while (!m_pendingDeleteWebsiteDataForOriginsCallbacks.isEmpty())
236         m_pendingDeleteWebsiteDataForOriginsCallbacks.take(m_pendingDeleteWebsiteDataForOriginsCallbacks.begin()->key)();
237
238     while (!m_updatePartitionOrBlockCookiesCallbackMap.isEmpty())
239         m_updatePartitionOrBlockCookiesCallbackMap.take(m_updatePartitionOrBlockCookiesCallbackMap.begin()->key)();
240     
241     while (!m_storageAccessResponseCallbackMap.isEmpty())
242         m_storageAccessResponseCallbackMap.take(m_storageAccessResponseCallbackMap.begin()->key)(false);
243 }
244
245 void NetworkProcessProxy::didReceiveMessage(IPC::Connection& connection, IPC::Decoder& decoder)
246 {
247     if (dispatchMessage(connection, decoder))
248         return;
249
250     if (m_processPool.dispatchMessage(connection, decoder))
251         return;
252
253     didReceiveNetworkProcessProxyMessage(connection, decoder);
254 }
255
256 void NetworkProcessProxy::didReceiveSyncMessage(IPC::Connection& connection, IPC::Decoder& decoder, std::unique_ptr<IPC::Encoder>& replyEncoder)
257 {
258     if (dispatchSyncMessage(connection, decoder, replyEncoder))
259         return;
260
261     ASSERT_NOT_REACHED();
262 }
263
264 void NetworkProcessProxy::didClose(IPC::Connection&)
265 {
266     auto protectedProcessPool = makeRef(m_processPool);
267
268     if (m_downloadProxyMap)
269         m_downloadProxyMap->processDidClose();
270 #if ENABLE(LEGACY_CUSTOM_PROTOCOL_MANAGER)
271     m_customProtocolManagerProxy.invalidate();
272 #endif
273
274     m_tokenForHoldingLockedFiles = nullptr;
275     
276     m_syncAllCookiesToken = nullptr;
277     m_syncAllCookiesCounter = 0;
278
279     for (auto& callback : m_writeBlobToFilePathCallbackMap.values())
280         callback(false);
281     m_writeBlobToFilePathCallbackMap.clear();
282
283     // This may 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();
297
298 #if USE(UNIX_DOMAIN_SOCKETS) || OS(WINDOWS)
299     reply(connectionIdentifier);
300 #elif OS(DARWIN)
301     reply(IPC::Attachment(connectionIdentifier.port(), MACH_MSG_TYPE_MOVE_SEND));
302 #else
303     notImplemented();
304 #endif
305 }
306
307 void NetworkProcessProxy::didReceiveAuthenticationChallenge(uint64_t pageID, uint64_t frameID, WebCore::AuthenticationChallenge&& coreChallenge, uint64_t challengeID)
308 {
309 #if ENABLE(SERVICE_WORKER)
310     if (auto* serviceWorkerProcessProxy = m_processPool.serviceWorkerProcessProxyFromPageID(pageID)) {
311         auto authenticationChallenge = AuthenticationChallengeProxy::create(WTFMove(coreChallenge), challengeID, connection());
312         serviceWorkerProcessProxy->didReceiveAuthenticationChallenge(pageID, frameID, WTFMove(authenticationChallenge));
313         return;
314     }
315 #endif
316
317     WebPageProxy* page = WebProcessProxy::webPage(pageID);
318     MESSAGE_CHECK(page);
319
320     auto authenticationChallenge = AuthenticationChallengeProxy::create(WTFMove(coreChallenge), challengeID, connection());
321     page->didReceiveAuthenticationChallengeProxy(frameID, WTFMove(authenticationChallenge));
322 }
323
324 void NetworkProcessProxy::didFetchWebsiteData(uint64_t callbackID, const WebsiteData& websiteData)
325 {
326     auto callback = m_pendingFetchWebsiteDataCallbacks.take(callbackID);
327     callback(websiteData);
328 }
329
330 void NetworkProcessProxy::didDeleteWebsiteData(uint64_t callbackID)
331 {
332     auto callback = m_pendingDeleteWebsiteDataCallbacks.take(callbackID);
333     callback();
334 }
335
336 void NetworkProcessProxy::didDeleteWebsiteDataForOrigins(uint64_t callbackID)
337 {
338     auto callback = m_pendingDeleteWebsiteDataForOriginsCallbacks.take(callbackID);
339     callback();
340 }
341
342 void NetworkProcessProxy::grantSandboxExtensionsToStorageProcessForBlobs(uint64_t requestID, const Vector<String>& paths)
343 {
344 #if ENABLE(SANDBOX_EXTENSIONS)
345     SandboxExtension::HandleArray extensions;
346     extensions.allocate(paths.size());
347     for (size_t i = 0; i < paths.size(); ++i) {
348         // ReadWrite is required for creating hard links as well as deleting the temporary file, which the StorageProcess will do.
349         SandboxExtension::createHandle(paths[i], SandboxExtension::Type::ReadWrite, extensions[i]);
350     }
351
352     m_processPool.sendToStorageProcessRelaunchingIfNecessary(Messages::StorageProcess::GrantSandboxExtensionsForBlobs(paths, extensions));
353 #endif
354     connection()->send(Messages::NetworkProcess::DidGrantSandboxExtensionsToStorageProcessForBlobs(requestID), 0);
355 }
356
357 void NetworkProcessProxy::didFinishLaunching(ProcessLauncher* launcher, IPC::Connection::Identifier connectionIdentifier)
358 {
359     ChildProcessProxy::didFinishLaunching(launcher, connectionIdentifier);
360
361     if (!IPC::Connection::identifierIsValid(connectionIdentifier)) {
362         networkProcessFailedToLaunch();
363         return;
364     }
365
366     for (unsigned i = 0; i < m_numPendingConnectionRequests; ++i)
367         connection()->send(Messages::NetworkProcess::CreateNetworkConnectionToWebProcess(), 0);
368     
369     m_numPendingConnectionRequests = 0;
370
371 #if PLATFORM(COCOA)
372     if (m_processPool.processSuppressionEnabled())
373         setProcessSuppressionEnabled(true);
374 #endif
375     
376 #if PLATFORM(IOS)
377     if (xpc_connection_t connection = this->connection()->xpcConnection())
378         m_throttler.didConnectToProcess(xpc_connection_get_pid(connection));
379 #endif
380 }
381
382 void NetworkProcessProxy::logDiagnosticMessage(uint64_t pageID, const String& message, const String& description, WebCore::ShouldSample shouldSample)
383 {
384     WebPageProxy* page = WebProcessProxy::webPage(pageID);
385     // 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,
386     // but for now we simply drop the message in the rare case this happens.
387     if (!page)
388         return;
389
390     page->logDiagnosticMessage(message, description, shouldSample);
391 }
392
393 void NetworkProcessProxy::logDiagnosticMessageWithResult(uint64_t pageID, const String& message, const String& description, uint32_t result, WebCore::ShouldSample shouldSample)
394 {
395     WebPageProxy* page = WebProcessProxy::webPage(pageID);
396     // 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,
397     // but for now we simply drop the message in the rare case this happens.
398     if (!page)
399         return;
400
401     page->logDiagnosticMessageWithResult(message, description, result, shouldSample);
402 }
403
404 void NetworkProcessProxy::logDiagnosticMessageWithValue(uint64_t pageID, const String& message, const String& description, double value, unsigned significantFigures, WebCore::ShouldSample shouldSample)
405 {
406     WebPageProxy* page = WebProcessProxy::webPage(pageID);
407     // 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,
408     // but for now we simply drop the message in the rare case this happens.
409     if (!page)
410         return;
411
412     page->logDiagnosticMessageWithValue(message, description, value, significantFigures, shouldSample);
413 }
414
415 #if USE(PROTECTION_SPACE_AUTH_CALLBACK)
416 void NetworkProcessProxy::canAuthenticateAgainstProtectionSpace(uint64_t loaderID, uint64_t pageID, uint64_t frameID, const WebCore::ProtectionSpace& protectionSpace)
417 {
418     // NetworkProcess state cannot asynchronously be kept in sync with these objects
419     // like we expect WebProcess <-> UIProcess state to be kept in sync.
420     // So there's no guarantee the messaged WebPageProxy or WebFrameProxy exist here in the UIProcess.
421     // We need to validate both the page and the frame up front.
422     if (auto* page = WebProcessProxy::webPage(pageID)) {
423         if (page->process().webFrame(frameID)) {
424             page->canAuthenticateAgainstProtectionSpace(loaderID, frameID, protectionSpace);
425             return;
426         }
427 #if ENABLE(SERVICE_WORKER)
428     } else if (m_processPool.serviceWorkerProcessProxyFromPageID(pageID)) {
429         send(Messages::NetworkProcess::ContinueCanAuthenticateAgainstProtectionSpace(loaderID, true), 0);
430         return;
431 #endif
432     }
433     // In the case where we will not be able to reply to this message with a client reply,
434     // we should message back a default to the Networking process.
435     send(Messages::NetworkProcess::ContinueCanAuthenticateAgainstProtectionSpace(loaderID, false), 0);
436 }
437 #endif
438
439 #if HAVE(CFNETWORK_STORAGE_PARTITIONING)
440 void NetworkProcessProxy::updatePrevalentDomainsToPartitionOrBlockCookies(PAL::SessionID sessionID, const Vector<String>& domainsToPartition, const Vector<String>& domainsToBlock, const Vector<String>& domainsToNeitherPartitionNorBlock, ShouldClearFirst shouldClearFirst, CompletionHandler<void()>&& callback)
441 {
442     if (!canSendMessage()) {
443         callback();
444         return;
445     }
446     
447     auto callbackId = generateCallbackID();
448     auto addResult = m_updatePartitionOrBlockCookiesCallbackMap.add(callbackId, WTFMove(callback));
449     ASSERT_UNUSED(addResult, addResult.isNewEntry);
450     send(Messages::NetworkProcess::UpdatePrevalentDomainsToPartitionOrBlockCookies(sessionID, domainsToPartition, domainsToBlock, domainsToNeitherPartitionNorBlock, shouldClearFirst == ShouldClearFirst::Yes, callbackId), 0);
451 }
452
453 void NetworkProcessProxy::didUpdatePartitionOrBlockCookies(uint64_t callbackId)
454 {
455     m_updatePartitionOrBlockCookiesCallbackMap.take(callbackId)();
456 }
457
458 static uint64_t nextRequestStorageAccessContextId()
459 {
460     static uint64_t nextContextId = 0;
461     return ++nextContextId;
462 }
463
464 void NetworkProcessProxy::hasStorageAccessForFrame(PAL::SessionID sessionID, const String& resourceDomain, const String& firstPartyDomain, uint64_t frameID, uint64_t pageID, WTF::CompletionHandler<void(bool)>&& callback)
465 {
466     auto contextId = nextRequestStorageAccessContextId();
467     auto addResult = m_storageAccessResponseCallbackMap.add(contextId, WTFMove(callback));
468     ASSERT_UNUSED(addResult, addResult.isNewEntry);
469     send(Messages::NetworkProcess::HasStorageAccessForFrame(sessionID, resourceDomain, firstPartyDomain, frameID, pageID, contextId), 0);
470 }
471
472 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)
473 {
474     auto contextId = nextRequestStorageAccessContextId();
475     auto addResult = m_storageAccessResponseCallbackMap.add(contextId, WTFMove(callback));
476     ASSERT_UNUSED(addResult, addResult.isNewEntry);
477     send(Messages::NetworkProcess::GrantStorageAccess(sessionID, resourceDomain, firstPartyDomain, frameID, pageID, contextId), 0);
478 }
479
480 void NetworkProcessProxy::removeAllStorageAccess(PAL::SessionID sessionID)
481 {
482     if (canSendMessage())
483         send(Messages::NetworkProcess::RemoveAllStorageAccess(sessionID), 0);
484 }
485
486 void NetworkProcessProxy::storageAccessRequestResult(bool wasGranted, uint64_t contextId)
487 {
488     auto callback = m_storageAccessResponseCallbackMap.take(contextId);
489     callback(wasGranted);
490 }
491
492 void NetworkProcessProxy::getAllStorageAccessEntries(PAL::SessionID sessionID, CompletionHandler<void(Vector<String>&& domains)>&& callback)
493 {
494     auto contextId = nextRequestStorageAccessContextId();
495     auto addResult = m_allStorageAccessEntriesCallbackMap.add(contextId, WTFMove(callback));
496     ASSERT_UNUSED(addResult, addResult.isNewEntry);
497     send(Messages::NetworkProcess::GetAllStorageAccessEntries(sessionID, contextId), 0);
498 }
499
500 void NetworkProcessProxy::allStorageAccessEntriesResult(Vector<String>&& domains, uint64_t contextId)
501 {
502     auto callback = m_allStorageAccessEntriesCallbackMap.take(contextId);
503     callback(WTFMove(domains));
504 }
505 #endif
506
507 void NetworkProcessProxy::sendProcessWillSuspendImminently()
508 {
509     if (!canSendMessage())
510         return;
511
512     bool handled = false;
513     sendSync(Messages::NetworkProcess::ProcessWillSuspendImminently(), Messages::NetworkProcess::ProcessWillSuspendImminently::Reply(handled), 0, 1_s);
514 }
515     
516 void NetworkProcessProxy::sendPrepareToSuspend()
517 {
518     if (canSendMessage())
519         send(Messages::NetworkProcess::PrepareToSuspend(), 0);
520 }
521
522 void NetworkProcessProxy::sendCancelPrepareToSuspend()
523 {
524     if (canSendMessage())
525         send(Messages::NetworkProcess::CancelPrepareToSuspend(), 0);
526 }
527
528 void NetworkProcessProxy::sendProcessDidResume()
529 {
530     if (canSendMessage())
531         send(Messages::NetworkProcess::ProcessDidResume(), 0);
532 }
533
534 void NetworkProcessProxy::writeBlobToFilePath(const WebCore::URL& url, const String& path, CompletionHandler<void(bool)>&& callback)
535 {
536     if (!canSendMessage()) {
537         callback(false);
538         return;
539     }
540
541     static uint64_t writeBlobToFilePathCallbackIdentifiers = 0;
542     uint64_t callbackID = ++writeBlobToFilePathCallbackIdentifiers;
543     m_writeBlobToFilePathCallbackMap.add(callbackID, WTFMove(callback));
544
545     SandboxExtension::Handle handleForWriting;
546     SandboxExtension::createHandle(path, SandboxExtension::Type::ReadWrite, handleForWriting);
547     send(Messages::NetworkProcess::WriteBlobToFilePath(url, path, handleForWriting, callbackID), 0);
548 }
549
550 void NetworkProcessProxy::didWriteBlobToFilePath(bool success, uint64_t callbackID)
551 {
552     if (auto handler = m_writeBlobToFilePathCallbackMap.take(callbackID))
553         handler(success);
554     else
555         ASSERT_NOT_REACHED();
556 }
557
558 void NetworkProcessProxy::processReadyToSuspend()
559 {
560     m_throttler.processReadyToSuspend();
561 }
562
563 void NetworkProcessProxy::didSetAssertionState(AssertionState)
564 {
565 }
566     
567 void NetworkProcessProxy::setIsHoldingLockedFiles(bool isHoldingLockedFiles)
568 {
569     if (!isHoldingLockedFiles) {
570         RELEASE_LOG(ProcessSuspension, "UIProcess is releasing a background assertion because the Network process is no longer holding locked files");
571         m_tokenForHoldingLockedFiles = nullptr;
572         return;
573     }
574     if (!m_tokenForHoldingLockedFiles) {
575         RELEASE_LOG(ProcessSuspension, "UIProcess is taking a background assertion because the Network process is holding locked files");
576         m_tokenForHoldingLockedFiles = m_throttler.backgroundActivityToken();
577     }
578 }
579
580 void NetworkProcessProxy::syncAllCookies()
581 {
582     send(Messages::NetworkProcess::SyncAllCookies(), 0);
583     
584     ++m_syncAllCookiesCounter;
585     if (m_syncAllCookiesToken)
586         return;
587     
588     RELEASE_LOG(ProcessSuspension, "%p - NetworkProcessProxy is taking a background assertion because the Network process is syncing cookies", this);
589     m_syncAllCookiesToken = throttler().backgroundActivityToken();
590 }
591     
592 void NetworkProcessProxy::didSyncAllCookies()
593 {
594     ASSERT(m_syncAllCookiesCounter);
595
596     --m_syncAllCookiesCounter;
597     if (!m_syncAllCookiesCounter) {
598         RELEASE_LOG(ProcessSuspension, "%p - NetworkProcessProxy is releasing a background assertion because the Network process is done syncing cookies", this);
599         m_syncAllCookiesToken = nullptr;
600     }
601 }
602
603 void NetworkProcessProxy::addSession(Ref<WebsiteDataStore>&& store)
604 {
605     if (canSendMessage())
606         send(Messages::NetworkProcess::AddWebsiteDataStore { store->parameters() }, 0);
607     auto sessionID = store->sessionID();
608     if (!sessionID.isEphemeral())
609         m_websiteDataStores.set(sessionID, WTFMove(store));
610 }
611
612 void NetworkProcessProxy::removeSession(PAL::SessionID sessionID)
613 {
614     if (canSendMessage())
615         send(Messages::NetworkProcess::DestroySession { sessionID }, 0);
616     if (!sessionID.isEphemeral())
617         m_websiteDataStores.remove(sessionID);
618 }
619
620 void NetworkProcessProxy::retrieveCacheStorageParameters(PAL::SessionID sessionID)
621 {
622     auto iterator = m_websiteDataStores.find(sessionID);
623     if (iterator == m_websiteDataStores.end()) {
624         auto quota = m_processPool.websiteDataStore() ? m_processPool.websiteDataStore()->websiteDataStore().cacheStoragePerOriginQuota() : WebsiteDataStore::defaultCacheStoragePerOriginQuota;
625         send(Messages::NetworkProcess::SetCacheStorageParameters { sessionID, quota, { }, { } }, 0);
626         return;
627     }
628
629     auto& store = *iterator->value;
630     auto& cacheStorageDirectory = store.cacheStorageDirectory();
631     SandboxExtension::Handle cacheStorageDirectoryExtensionHandle;
632     if (!cacheStorageDirectory.isEmpty())
633         SandboxExtension::createHandleForReadWriteDirectory(cacheStorageDirectory, cacheStorageDirectoryExtensionHandle);
634
635     send(Messages::NetworkProcess::SetCacheStorageParameters { sessionID, store.cacheStoragePerOriginQuota(), cacheStorageDirectory, cacheStorageDirectoryExtensionHandle }, 0);
636 }
637
638 #if ENABLE(CONTENT_EXTENSIONS)
639 void NetworkProcessProxy::contentExtensionRules(UserContentControllerIdentifier identifier)
640 {
641     if (auto* webUserContentControllerProxy = WebUserContentControllerProxy::get(identifier)) {
642         m_webUserContentControllerProxies.add(webUserContentControllerProxy);
643         webUserContentControllerProxy->addNetworkProcess(*this);
644
645         auto rules = WTF::map(webUserContentControllerProxy->contentExtensionRules(), [](auto&& keyValue) -> std::pair<String, WebCompiledContentRuleListData> {
646             return std::make_pair(keyValue.value->name(), keyValue.value->compiledRuleList().data());
647         });
648         send(Messages::NetworkContentRuleListManager::AddContentRuleLists { identifier, rules }, 0);
649         return;
650     }
651     send(Messages::NetworkContentRuleListManager::AddContentRuleLists { identifier, { } }, 0);
652 }
653
654 void NetworkProcessProxy::didDestroyWebUserContentControllerProxy(WebUserContentControllerProxy& proxy)
655 {
656     send(Messages::NetworkContentRuleListManager::Remove { proxy.identifier() }, 0);
657     m_webUserContentControllerProxies.remove(&proxy);
658 }
659 #endif
660
661 } // namespace WebKit