2 * Copyright (C) 2012-2016 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
27 #include "NetworkProcessProxy.h"
29 #include "AuthenticationChallengeProxy.h"
30 #include "DownloadProxyMessages.h"
31 #if ENABLE(LEGACY_CUSTOM_PROTOCOL_MANAGER)
32 #include "LegacyCustomProtocolManagerProxyMessages.h"
35 #include "NetworkProcessCreationParameters.h"
36 #include "NetworkProcessMessages.h"
37 #include "SandboxExtension.h"
38 #include "StorageProcessMessages.h"
39 #include "WebPageProxy.h"
40 #include "WebProcessMessages.h"
41 #include "WebProcessPool.h"
42 #include "WebsiteData.h"
43 #include <wtf/CompletionHandler.h>
45 #if ENABLE(SEC_ITEM_SHIM)
46 #include "SecItemShimProxy.h"
50 #include <wtf/spi/darwin/XPCSPI.h>
53 #define MESSAGE_CHECK(assertion) MESSAGE_CHECK_BASE(assertion, connection())
55 using namespace WebCore;
59 static uint64_t generateCallbackID()
61 static uint64_t callbackID;
66 Ref<NetworkProcessProxy> NetworkProcessProxy::create(WebProcessPool& processPool)
68 return adoptRef(*new NetworkProcessProxy(processPool));
71 NetworkProcessProxy::NetworkProcessProxy(WebProcessPool& processPool)
72 : ChildProcessProxy(processPool.alwaysRunsAtBackgroundPriority())
73 , m_processPool(processPool)
74 , m_numPendingConnectionRequests(0)
75 #if ENABLE(LEGACY_CUSTOM_PROTOCOL_MANAGER)
76 , m_customProtocolManagerProxy(*this)
78 , m_throttler(*this, processPool.shouldTakeUIBackgroundAssertion())
83 NetworkProcessProxy::~NetworkProcessProxy()
85 ASSERT(m_pendingFetchWebsiteDataCallbacks.isEmpty());
86 ASSERT(m_pendingDeleteWebsiteDataCallbacks.isEmpty());
87 ASSERT(m_pendingDeleteWebsiteDataForOriginsCallbacks.isEmpty());
90 void NetworkProcessProxy::getLaunchOptions(ProcessLauncher::LaunchOptions& launchOptions)
92 launchOptions.processType = ProcessLauncher::ProcessType::Network;
93 ChildProcessProxy::getLaunchOptions(launchOptions);
96 void NetworkProcessProxy::connectionWillOpen(IPC::Connection& connection)
98 #if ENABLE(SEC_ITEM_SHIM)
99 SecItemShimProxy::singleton().initializeConnection(connection);
101 UNUSED_PARAM(connection);
105 void NetworkProcessProxy::processWillShutDown(IPC::Connection& connection)
107 ASSERT_UNUSED(connection, this->connection() == &connection);
110 void NetworkProcessProxy::getNetworkProcessConnection(Ref<Messages::WebProcessProxy::GetNetworkProcessConnection::DelayedReply>&& reply)
112 m_pendingConnectionReplies.append(WTFMove(reply));
114 if (state() == State::Launching) {
115 m_numPendingConnectionRequests++;
119 connection()->send(Messages::NetworkProcess::CreateNetworkConnectionToWebProcess(), 0, IPC::SendOption::DispatchMessageEvenWhenWaitingForSyncReply);
122 DownloadProxy* NetworkProcessProxy::createDownloadProxy(const ResourceRequest& resourceRequest)
124 if (!m_downloadProxyMap)
125 m_downloadProxyMap = std::make_unique<DownloadProxyMap>(this);
127 return m_downloadProxyMap->createDownloadProxy(m_processPool, resourceRequest);
130 void NetworkProcessProxy::fetchWebsiteData(PAL::SessionID sessionID, OptionSet<WebsiteDataType> dataTypes, OptionSet<WebsiteDataFetchOption> fetchOptions, WTF::Function<void (WebsiteData)>&& completionHandler)
132 ASSERT(canSendMessage());
134 uint64_t callbackID = generateCallbackID();
135 RELEASE_LOG_IF(sessionID.isAlwaysOnLoggingAllowed(), ProcessSuspension, "%p - NetworkProcessProxy is taking a background assertion because the Network process is fetching Website data", this);
137 m_pendingFetchWebsiteDataCallbacks.add(callbackID, [this, token = throttler().backgroundActivityToken(), completionHandler = WTFMove(completionHandler), sessionID](WebsiteData websiteData) {
138 completionHandler(WTFMove(websiteData));
139 RELEASE_LOG_IF(sessionID.isAlwaysOnLoggingAllowed(), ProcessSuspension, "%p - NetworkProcessProxy is releasing a background assertion because the Network process is done fetching Website data", this);
142 send(Messages::NetworkProcess::FetchWebsiteData(sessionID, dataTypes, fetchOptions, callbackID), 0);
145 void NetworkProcessProxy::deleteWebsiteData(PAL::SessionID sessionID, OptionSet<WebsiteDataType> dataTypes, WallTime modifiedSince, WTF::Function<void ()>&& completionHandler)
147 auto callbackID = generateCallbackID();
148 RELEASE_LOG_IF(sessionID.isAlwaysOnLoggingAllowed(), ProcessSuspension, "%p - NetworkProcessProxy is taking a background assertion because the Network process is deleting Website data", this);
150 m_pendingDeleteWebsiteDataCallbacks.add(callbackID, [this, token = throttler().backgroundActivityToken(), completionHandler = WTFMove(completionHandler), sessionID] {
152 RELEASE_LOG_IF(sessionID.isAlwaysOnLoggingAllowed(), ProcessSuspension, "%p - NetworkProcessProxy is releasing a background assertion because the Network process is done deleting Website data", this);
154 send(Messages::NetworkProcess::DeleteWebsiteData(sessionID, dataTypes, modifiedSince, callbackID), 0);
157 void NetworkProcessProxy::deleteWebsiteDataForOrigins(PAL::SessionID sessionID, OptionSet<WebsiteDataType> dataTypes, const Vector<WebCore::SecurityOriginData>& origins, const Vector<String>& cookieHostNames, WTF::Function<void()>&& completionHandler)
159 ASSERT(canSendMessage());
161 uint64_t callbackID = generateCallbackID();
162 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);
164 m_pendingDeleteWebsiteDataForOriginsCallbacks.add(callbackID, [this, token = throttler().backgroundActivityToken(), completionHandler = WTFMove(completionHandler), sessionID] {
166 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);
169 send(Messages::NetworkProcess::DeleteWebsiteDataForOrigins(sessionID, dataTypes, origins, cookieHostNames, callbackID), 0);
172 void NetworkProcessProxy::networkProcessCrashed()
174 clearCallbackStates();
176 Vector<Ref<Messages::WebProcessProxy::GetNetworkProcessConnection::DelayedReply>> pendingReplies;
177 pendingReplies.reserveInitialCapacity(m_pendingConnectionReplies.size());
178 for (auto& reply : m_pendingConnectionReplies)
179 pendingReplies.append(WTFMove(reply));
181 // Tell the network process manager to forget about this network process proxy. This may cause us to be deleted.
182 m_processPool.networkProcessCrashed(*this, WTFMove(pendingReplies));
185 void NetworkProcessProxy::networkProcessFailedToLaunch()
187 // The network process must have crashed or exited, send any pending sync replies we might have.
188 while (!m_pendingConnectionReplies.isEmpty()) {
189 Ref<Messages::WebProcessProxy::GetNetworkProcessConnection::DelayedReply> reply = m_pendingConnectionReplies.takeFirst();
191 #if USE(UNIX_DOMAIN_SOCKETS)
192 reply->send(IPC::Attachment());
194 reply->send(IPC::Attachment(0, MACH_MSG_TYPE_MOVE_SEND));
199 clearCallbackStates();
200 // Tell the network process manager to forget about this network process proxy. This may cause us to be deleted.
201 m_processPool.networkProcessFailedToLaunch(*this);
204 void NetworkProcessProxy::clearCallbackStates()
206 for (const auto& callback : m_pendingFetchWebsiteDataCallbacks.values())
207 callback(WebsiteData());
208 m_pendingFetchWebsiteDataCallbacks.clear();
210 for (const auto& callback : m_pendingDeleteWebsiteDataCallbacks.values())
212 m_pendingDeleteWebsiteDataCallbacks.clear();
214 for (const auto& callback : m_pendingDeleteWebsiteDataForOriginsCallbacks.values())
216 m_pendingDeleteWebsiteDataForOriginsCallbacks.clear();
219 void NetworkProcessProxy::didReceiveMessage(IPC::Connection& connection, IPC::Decoder& decoder)
221 if (dispatchMessage(connection, decoder))
224 if (m_processPool.dispatchMessage(connection, decoder))
227 didReceiveNetworkProcessProxyMessage(connection, decoder);
230 void NetworkProcessProxy::didReceiveSyncMessage(IPC::Connection& connection, IPC::Decoder& decoder, std::unique_ptr<IPC::Encoder>& replyEncoder)
232 if (dispatchSyncMessage(connection, decoder, replyEncoder))
235 ASSERT_NOT_REACHED();
238 void NetworkProcessProxy::didClose(IPC::Connection&)
240 if (m_downloadProxyMap)
241 m_downloadProxyMap->processDidClose();
242 #if ENABLE(LEGACY_CUSTOM_PROTOCOL_MANAGER)
243 m_customProtocolManagerProxy.invalidate();
246 m_tokenForHoldingLockedFiles = nullptr;
248 for (auto& callback : m_writeBlobToFilePathCallbackMap.values())
250 m_writeBlobToFilePathCallbackMap.clear();
252 // This may cause us to be deleted.
253 networkProcessCrashed();
256 void NetworkProcessProxy::didReceiveInvalidMessage(IPC::Connection&, IPC::StringReference, IPC::StringReference)
260 void NetworkProcessProxy::didCreateNetworkConnectionToWebProcess(const IPC::Attachment& connectionIdentifier)
262 ASSERT(!m_pendingConnectionReplies.isEmpty());
264 // Grab the first pending connection reply.
265 RefPtr<Messages::WebProcessProxy::GetNetworkProcessConnection::DelayedReply> reply = m_pendingConnectionReplies.takeFirst();
267 #if USE(UNIX_DOMAIN_SOCKETS)
268 reply->send(connectionIdentifier);
270 reply->send(IPC::Attachment(connectionIdentifier.port(), MACH_MSG_TYPE_MOVE_SEND));
276 void NetworkProcessProxy::didReceiveAuthenticationChallenge(uint64_t pageID, uint64_t frameID, const WebCore::AuthenticationChallenge& coreChallenge, uint64_t challengeID)
278 #if ENABLE(SERVICE_WORKER)
279 if (m_processPool.isServiceWorker(pageID)) {
280 auto authenticationChallenge = AuthenticationChallengeProxy::create(coreChallenge, challengeID, connection());
281 m_processPool.serviceWorkerProxy()->didReceiveAuthenticationChallenge(pageID, frameID, WTFMove(authenticationChallenge));
286 WebPageProxy* page = WebProcessProxy::webPage(pageID);
289 auto authenticationChallenge = AuthenticationChallengeProxy::create(coreChallenge, challengeID, connection());
290 page->didReceiveAuthenticationChallengeProxy(frameID, WTFMove(authenticationChallenge));
293 void NetworkProcessProxy::didFetchWebsiteData(uint64_t callbackID, const WebsiteData& websiteData)
295 auto callback = m_pendingFetchWebsiteDataCallbacks.take(callbackID);
296 callback(websiteData);
299 void NetworkProcessProxy::didDeleteWebsiteData(uint64_t callbackID)
301 auto callback = m_pendingDeleteWebsiteDataCallbacks.take(callbackID);
305 void NetworkProcessProxy::didDeleteWebsiteDataForOrigins(uint64_t callbackID)
307 auto callback = m_pendingDeleteWebsiteDataForOriginsCallbacks.take(callbackID);
311 void NetworkProcessProxy::grantSandboxExtensionsToStorageProcessForBlobs(uint64_t requestID, const Vector<String>& paths)
313 #if ENABLE(SANDBOX_EXTENSIONS)
314 SandboxExtension::HandleArray extensions;
315 extensions.allocate(paths.size());
316 for (size_t i = 0; i < paths.size(); ++i) {
317 // ReadWrite is required for creating hard links as well as deleting the temporary file, which the StorageProcess will do.
318 SandboxExtension::createHandle(paths[i], SandboxExtension::Type::ReadWrite, extensions[i]);
321 m_processPool.sendToStorageProcessRelaunchingIfNecessary(Messages::StorageProcess::GrantSandboxExtensionsForBlobs(paths, extensions));
323 connection()->send(Messages::NetworkProcess::DidGrantSandboxExtensionsToStorageProcessForBlobs(requestID), 0);
326 void NetworkProcessProxy::didFinishLaunching(ProcessLauncher* launcher, IPC::Connection::Identifier connectionIdentifier)
328 ChildProcessProxy::didFinishLaunching(launcher, connectionIdentifier);
330 if (IPC::Connection::identifierIsNull(connectionIdentifier)) {
331 networkProcessFailedToLaunch();
335 for (unsigned i = 0; i < m_numPendingConnectionRequests; ++i)
336 connection()->send(Messages::NetworkProcess::CreateNetworkConnectionToWebProcess(), 0);
338 m_numPendingConnectionRequests = 0;
341 if (m_processPool.processSuppressionEnabled())
342 setProcessSuppressionEnabled(true);
346 if (xpc_connection_t connection = this->connection()->xpcConnection())
347 m_throttler.didConnectToProcess(xpc_connection_get_pid(connection));
351 void NetworkProcessProxy::logDiagnosticMessage(uint64_t pageID, const String& message, const String& description, WebCore::ShouldSample shouldSample)
353 WebPageProxy* page = WebProcessProxy::webPage(pageID);
354 // 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,
355 // but for now we simply drop the message in the rare case this happens.
359 page->logDiagnosticMessage(message, description, shouldSample);
362 void NetworkProcessProxy::logDiagnosticMessageWithResult(uint64_t pageID, const String& message, const String& description, uint32_t result, WebCore::ShouldSample shouldSample)
364 WebPageProxy* page = WebProcessProxy::webPage(pageID);
365 // 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,
366 // but for now we simply drop the message in the rare case this happens.
370 page->logDiagnosticMessageWithResult(message, description, result, shouldSample);
373 void NetworkProcessProxy::logDiagnosticMessageWithValue(uint64_t pageID, const String& message, const String& description, double value, unsigned significantFigures, WebCore::ShouldSample shouldSample)
375 WebPageProxy* page = WebProcessProxy::webPage(pageID);
376 // 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,
377 // but for now we simply drop the message in the rare case this happens.
381 page->logDiagnosticMessageWithValue(message, description, value, significantFigures, shouldSample);
384 #if USE(PROTECTION_SPACE_AUTH_CALLBACK)
385 void NetworkProcessProxy::canAuthenticateAgainstProtectionSpace(uint64_t loaderID, uint64_t pageID, uint64_t frameID, const WebCore::ProtectionSpace& protectionSpace)
387 // NetworkProcess state cannot asynchronously be kept in sync with these objects
388 // like we expect WebProcess <-> UIProcess state to be kept in sync.
389 // So there's no guarantee the messaged WebPageProxy or WebFrameProxy exist here in the UIProcess.
390 // We need to validate both the page and the frame up front.
391 if (auto* page = WebProcessProxy::webPage(pageID)) {
392 if (page->process().webFrame(frameID)) {
393 page->canAuthenticateAgainstProtectionSpace(loaderID, frameID, protectionSpace);
396 #if ENABLE(SERVICE_WORKER)
397 } else if (m_processPool.isServiceWorker(pageID)) {
398 send(Messages::NetworkProcess::ContinueCanAuthenticateAgainstProtectionSpace(loaderID, true), 0);
402 // In the case where we will not be able to reply to this message with a client reply,
403 // we should message back a default to the Networking process.
404 send(Messages::NetworkProcess::ContinueCanAuthenticateAgainstProtectionSpace(loaderID, false), 0);
408 #if HAVE(CFNETWORK_STORAGE_PARTITIONING)
409 static uint64_t nextRequestStorageAccessContextId()
411 static uint64_t nextContextId = 0;
412 return ++nextContextId;
415 void NetworkProcessProxy::hasStorageAccessForPrevalentDomains(PAL::SessionID sessionID, const String& resourceDomain, const String& firstPartyDomain, uint64_t frameID, uint64_t pageID, WTF::CompletionHandler<void(bool)>&& callback)
417 auto contextId = nextRequestStorageAccessContextId();
418 auto addResult = m_storageAccessResponseCallbackMap.add(contextId, WTFMove(callback));
419 ASSERT_UNUSED(addResult, addResult.isNewEntry);
420 send(Messages::NetworkProcess::HasStorageAccessForPrevalentDomains(sessionID, resourceDomain, firstPartyDomain, frameID, pageID, contextId), 0);
423 void NetworkProcessProxy::updateStorageAccessForPrevalentDomains(PAL::SessionID sessionID, const String& resourceDomain, const String& firstPartyDomain, uint64_t frameID, uint64_t pageID, bool value, WTF::CompletionHandler<void(bool)>&& callback)
425 auto contextId = nextRequestStorageAccessContextId();
426 auto addResult = m_storageAccessResponseCallbackMap.add(contextId, WTFMove(callback));
427 ASSERT_UNUSED(addResult, addResult.isNewEntry);
428 send(Messages::NetworkProcess::UpdateStorageAccessForPrevalentDomains(sessionID, resourceDomain, firstPartyDomain, frameID, pageID, value, contextId), 0);
431 void NetworkProcessProxy::storageAccessRequestResult(bool wasGranted, uint64_t contextId)
433 auto callback = m_storageAccessResponseCallbackMap.take(contextId);
434 callback(wasGranted);
438 void NetworkProcessProxy::sendProcessWillSuspendImminently()
440 if (!canSendMessage())
443 bool handled = false;
444 sendSync(Messages::NetworkProcess::ProcessWillSuspendImminently(), Messages::NetworkProcess::ProcessWillSuspendImminently::Reply(handled), 0, 1_s);
447 void NetworkProcessProxy::sendPrepareToSuspend()
449 if (canSendMessage())
450 send(Messages::NetworkProcess::PrepareToSuspend(), 0);
453 void NetworkProcessProxy::sendCancelPrepareToSuspend()
455 if (canSendMessage())
456 send(Messages::NetworkProcess::CancelPrepareToSuspend(), 0);
459 void NetworkProcessProxy::sendProcessDidResume()
461 if (canSendMessage())
462 send(Messages::NetworkProcess::ProcessDidResume(), 0);
465 void NetworkProcessProxy::writeBlobToFilePath(const WebCore::URL& url, const String& path, CompletionHandler<void(bool)>&& callback)
467 if (!canSendMessage()) {
472 static uint64_t writeBlobToFilePathCallbackIdentifiers = 0;
473 uint64_t callbackID = ++writeBlobToFilePathCallbackIdentifiers;
474 m_writeBlobToFilePathCallbackMap.add(callbackID, WTFMove(callback));
476 SandboxExtension::Handle handleForWriting;
477 SandboxExtension::createHandle(path, SandboxExtension::Type::ReadWrite, handleForWriting);
478 send(Messages::NetworkProcess::WriteBlobToFilePath(url, path, handleForWriting, callbackID), 0);
481 void NetworkProcessProxy::didWriteBlobToFilePath(bool success, uint64_t callbackID)
483 if (auto handler = m_writeBlobToFilePathCallbackMap.take(callbackID))
486 ASSERT_NOT_REACHED();
489 void NetworkProcessProxy::processReadyToSuspend()
491 m_throttler.processReadyToSuspend();
494 void NetworkProcessProxy::didSetAssertionState(AssertionState)
498 void NetworkProcessProxy::setIsHoldingLockedFiles(bool isHoldingLockedFiles)
500 if (!isHoldingLockedFiles) {
501 RELEASE_LOG(ProcessSuspension, "UIProcess is releasing a background assertion because the Network process is no longer holding locked files");
502 m_tokenForHoldingLockedFiles = nullptr;
505 if (!m_tokenForHoldingLockedFiles) {
506 RELEASE_LOG(ProcessSuspension, "UIProcess is taking a background assertion because the Network process is holding locked files");
507 m_tokenForHoldingLockedFiles = m_throttler.backgroundActivityToken();
511 } // namespace WebKit