blob: 9e32667492f1fd9f3f5f1bf73357b02bc2b6a5ae [file] [log] [blame]
beidson@apple.com21dd3ed2012-10-12 17:13:13 +00001/*
2 * Copyright (C) 2012 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
ap@apple.com9fb96e12013-01-08 17:50:51 +000029#if ENABLE(NETWORK_PROCESS)
30
beidson@apple.comdaf91062013-01-26 00:03:48 +000031#include "AuthenticationChallengeProxy.h"
andersca@apple.com172fb562013-01-30 00:00:39 +000032#include "CustomProtocolManagerProxyMessages.h"
andersca@apple.com93b4c362012-12-16 01:26:05 +000033#include "DownloadProxyMessages.h"
beidson@apple.comc9a08bc2012-10-12 23:37:30 +000034#include "NetworkProcessCreationParameters.h"
35#include "NetworkProcessMessages.h"
beidson@apple.comdfbdf942012-10-19 23:26:16 +000036#include "WebProcessMessages.h"
weinig@apple.com9c5a3ea2014-12-23 20:29:26 +000037#include "WebProcessPool.h"
andersca@apple.comce927682015-02-27 20:14:22 +000038#include "WebsiteData.h"
paroga@webkit.orgf8108ef2013-11-09 20:59:28 +000039#include <wtf/RunLoop.h>
beidson@apple.com21dd3ed2012-10-12 17:13:13 +000040
commit-queue@webkit.orgb48afca2013-12-08 18:26:15 +000041#if ENABLE(SEC_ITEM_SHIM)
42#include "SecItemShimProxy.h"
43#endif
44
bdakin@apple.com1539c6c2015-01-28 22:22:16 +000045#if PLATFORM(IOS)
dbates@webkit.orgdcec3832014-11-21 16:40:55 +000046#include <wtf/spi/darwin/XPCSPI.h>
47#endif
48
beidson@apple.comdaf91062013-01-26 00:03:48 +000049#define MESSAGE_CHECK(assertion) MESSAGE_CHECK_BASE(assertion, connection())
50
beidson@apple.com21dd3ed2012-10-12 17:13:13 +000051using namespace WebCore;
52
53namespace WebKit {
54
andersca@apple.com5ebf4582014-12-09 20:29:59 +000055static uint64_t generateCallbackID()
56{
57 static uint64_t callbackID;
58
59 return ++callbackID;
60}
61
weinig@apple.com9c5a3ea2014-12-23 20:29:26 +000062PassRefPtr<NetworkProcessProxy> NetworkProcessProxy::create(WebProcessPool& processPool)
beidson@apple.com21dd3ed2012-10-12 17:13:13 +000063{
weinig@apple.com9c5a3ea2014-12-23 20:29:26 +000064 return adoptRef(new NetworkProcessProxy(processPool));
beidson@apple.com21dd3ed2012-10-12 17:13:13 +000065}
66
weinig@apple.com9c5a3ea2014-12-23 20:29:26 +000067NetworkProcessProxy::NetworkProcessProxy(WebProcessPool& processPool)
68 : m_processPool(processPool)
beidson@apple.comdfbdf942012-10-19 23:26:16 +000069 , m_numPendingConnectionRequests(0)
weinig@apple.com9c5a3ea2014-12-23 20:29:26 +000070 , m_customProtocolManagerProxy(this, processPool)
beidson@apple.com21dd3ed2012-10-12 17:13:13 +000071{
ap@apple.com10a670e2012-12-05 23:28:31 +000072 connect();
73}
74
weinig@apple.com444fbd692013-01-27 05:32:53 +000075NetworkProcessProxy::~NetworkProcessProxy()
76{
andersca@apple.comce927682015-02-27 20:14:22 +000077 ASSERT(m_pendingFetchWebsiteDataCallbacks.isEmpty());
andersca@apple.com5ebf4582014-12-09 20:29:59 +000078 ASSERT(m_pendingDeleteWebsiteDataCallbacks.isEmpty());
andersca@apple.comce927682015-02-27 20:14:22 +000079 ASSERT(m_pendingDeleteWebsiteDataForOriginsCallbacks.isEmpty());
weinig@apple.com444fbd692013-01-27 05:32:53 +000080}
81
ap@apple.com10a670e2012-12-05 23:28:31 +000082void NetworkProcessProxy::getLaunchOptions(ProcessLauncher::LaunchOptions& launchOptions)
83{
beidson@apple.com21dd3ed2012-10-12 17:13:13 +000084 launchOptions.processType = ProcessLauncher::NetworkProcess;
weinig@apple.com444fbd692013-01-27 05:32:53 +000085 platformGetLaunchOptions(launchOptions);
beidson@apple.com21dd3ed2012-10-12 17:13:13 +000086}
87
andersca@apple.com15bc0862015-01-16 21:37:42 +000088void NetworkProcessProxy::connectionWillOpen(IPC::Connection& connection)
andersca@apple.com296139a2013-02-12 20:56:57 +000089{
mitz@apple.comda402b72013-12-04 21:04:31 +000090#if ENABLE(SEC_ITEM_SHIM)
cdumez@apple.com4faf1ef2015-01-30 20:49:33 +000091 SecItemShimProxy::singleton().initializeConnection(connection);
mrobinson@webkit.orgab8540d2014-03-20 17:20:58 +000092#else
93 UNUSED_PARAM(connection);
andersca@apple.com296139a2013-02-12 20:56:57 +000094#endif
95}
96
beidson@apple.comdfbdf942012-10-19 23:26:16 +000097void NetworkProcessProxy::getNetworkProcessConnection(PassRefPtr<Messages::WebProcessProxy::GetNetworkProcessConnection::DelayedReply> reply)
beidson@apple.com21dd3ed2012-10-12 17:13:13 +000098{
beidson@apple.comdfbdf942012-10-19 23:26:16 +000099 m_pendingConnectionReplies.append(reply);
beidson@apple.com21dd3ed2012-10-12 17:13:13 +0000100
andersca@apple.com90ab0cb2014-02-27 23:16:41 +0000101 if (state() == State::Launching) {
beidson@apple.comdfbdf942012-10-19 23:26:16 +0000102 m_numPendingConnectionRequests++;
103 return;
104 }
105
andersca@apple.com69388f02013-12-30 20:20:02 +0000106 connection()->send(Messages::NetworkProcess::CreateNetworkConnectionToWebProcess(), 0, IPC::DispatchMessageEvenWhenWaitingForSyncReply);
beidson@apple.comdfbdf942012-10-19 23:26:16 +0000107}
108
jeffm@apple.com76858a12014-10-02 21:01:08 +0000109DownloadProxy* NetworkProcessProxy::createDownloadProxy(const ResourceRequest& resourceRequest)
andersca@apple.com12e74922012-12-15 23:11:52 +0000110{
111 if (!m_downloadProxyMap)
zandobersek@gmail.comda3be5e32014-04-27 13:40:14 +0000112 m_downloadProxyMap = std::make_unique<DownloadProxyMap>(this);
andersca@apple.com12e74922012-12-15 23:11:52 +0000113
weinig@apple.com9c5a3ea2014-12-23 20:29:26 +0000114 return m_downloadProxyMap->createDownloadProxy(m_processPool, resourceRequest);
andersca@apple.com12e74922012-12-15 23:11:52 +0000115}
116
andersca@apple.comce927682015-02-27 20:14:22 +0000117void NetworkProcessProxy::fetchWebsiteData(SessionID sessionID, WebsiteDataTypes dataTypes, std::function<void (WebsiteData)> completionHandler)
118{
119 ASSERT(canSendMessage());
120
121 uint64_t callbackID = generateCallbackID();
122 m_pendingFetchWebsiteDataCallbacks.add(callbackID, WTF::move(completionHandler));
123
124 send(Messages::WebProcess::FetchWebsiteData(sessionID, dataTypes, callbackID), 0);
125}
126
andersca@apple.com5ebf4582014-12-09 20:29:59 +0000127void NetworkProcessProxy::deleteWebsiteData(WebCore::SessionID sessionID, WebsiteDataTypes dataTypes, std::chrono::system_clock::time_point modifiedSince, std::function<void ()> completionHandler)
128{
129 auto callbackID = generateCallbackID();
130
131 m_pendingDeleteWebsiteDataCallbacks.add(callbackID, WTF::move(completionHandler));
132 send(Messages::NetworkProcess::DeleteWebsiteData(sessionID, dataTypes, modifiedSince, callbackID), 0);
133}
134
andersca@apple.comce927682015-02-27 20:14:22 +0000135void NetworkProcessProxy::deleteWebsiteDataForOrigins(SessionID sessionID, WebsiteDataTypes dataTypes, const Vector<RefPtr<WebCore::SecurityOrigin>>& origins, std::function<void ()> completionHandler)
136{
137 ASSERT(canSendMessage());
138
139 uint64_t callbackID = generateCallbackID();
140 m_pendingDeleteWebsiteDataForOriginsCallbacks.add(callbackID, WTF::move(completionHandler));
141
142 Vector<SecurityOriginData> originData;
143 for (auto& origin : origins)
144 originData.append(SecurityOriginData::fromSecurityOrigin(*origin));
145
146 send(Messages::WebProcess::DeleteWebsiteDataForOrigins(sessionID, dataTypes, originData, callbackID), 0);
147}
148
beidson@apple.comdfbdf942012-10-19 23:26:16 +0000149void NetworkProcessProxy::networkProcessCrashedOrFailedToLaunch()
150{
151 // The network process must have crashed or exited, send any pending sync replies we might have.
152 while (!m_pendingConnectionReplies.isEmpty()) {
153 RefPtr<Messages::WebProcessProxy::GetNetworkProcessConnection::DelayedReply> reply = m_pendingConnectionReplies.takeFirst();
154
mitz@apple.com1b970882014-02-15 17:06:17 +0000155#if OS(DARWIN)
andersca@apple.com69388f02013-12-30 20:20:02 +0000156 reply->send(IPC::Attachment(0, MACH_MSG_TYPE_MOVE_SEND));
svillar@igalia.come78749f2013-12-09 12:28:44 +0000157#elif USE(UNIX_DOMAIN_SOCKETS)
andersca@apple.com69388f02013-12-30 20:20:02 +0000158 reply->send(IPC::Attachment());
beidson@apple.comdfbdf942012-10-19 23:26:16 +0000159#else
160 notImplemented();
161#endif
162 }
163
andersca@apple.comce927682015-02-27 20:14:22 +0000164 for (const auto& callback : m_pendingFetchWebsiteDataCallbacks.values())
165 callback(WebsiteData());
166 m_pendingFetchWebsiteDataCallbacks.clear();
167
andersca@apple.com5ebf4582014-12-09 20:29:59 +0000168 for (const auto& callback : m_pendingDeleteWebsiteDataCallbacks.values())
169 callback();
170 m_pendingDeleteWebsiteDataCallbacks.clear();
171
andersca@apple.comce927682015-02-27 20:14:22 +0000172 for (const auto& callback : m_pendingDeleteWebsiteDataForOriginsCallbacks.values())
173 callback();
174 m_pendingDeleteWebsiteDataForOriginsCallbacks.clear();
175
beidson@apple.comdfbdf942012-10-19 23:26:16 +0000176 // Tell the network process manager to forget about this network process proxy. This may cause us to be deleted.
weinig@apple.com9c5a3ea2014-12-23 20:29:26 +0000177 m_processPool.networkProcessCrashed(this);
beidson@apple.comdfbdf942012-10-19 23:26:16 +0000178}
179
andersca@apple.com6a186852015-01-05 18:38:15 +0000180void NetworkProcessProxy::didReceiveMessage(IPC::Connection& connection, IPC::MessageDecoder& decoder)
beidson@apple.comdfbdf942012-10-19 23:26:16 +0000181{
weinig@apple.comaa382042013-02-07 04:40:29 +0000182 if (dispatchMessage(connection, decoder))
andersca@apple.com93b4c362012-12-16 01:26:05 +0000183 return;
184
weinig@apple.com9c5a3ea2014-12-23 20:29:26 +0000185 if (m_processPool.dispatchMessage(connection, decoder))
ap@apple.coma6354cb2012-12-24 01:29:47 +0000186 return;
187
andersca@apple.com93914d82013-01-30 22:48:09 +0000188 didReceiveNetworkProcessProxyMessage(connection, decoder);
beidson@apple.com21dd3ed2012-10-12 17:13:13 +0000189}
190
andersca@apple.com6a186852015-01-05 18:38:15 +0000191void NetworkProcessProxy::didReceiveSyncMessage(IPC::Connection& connection, IPC::MessageDecoder& decoder, std::unique_ptr<IPC::MessageEncoder>& replyEncoder)
andersca@apple.com93b4c362012-12-16 01:26:05 +0000192{
weinig@apple.comaa382042013-02-07 04:40:29 +0000193 if (dispatchSyncMessage(connection, decoder, replyEncoder))
andersca@apple.com93b4c362012-12-16 01:26:05 +0000194 return;
195
196 ASSERT_NOT_REACHED();
197}
198
andersca@apple.com30e768b2015-01-04 20:14:45 +0000199void NetworkProcessProxy::didClose(IPC::Connection&)
beidson@apple.com21dd3ed2012-10-12 17:13:13 +0000200{
andersca@apple.com12e74922012-12-15 23:11:52 +0000201 if (m_downloadProxyMap)
202 m_downloadProxyMap->processDidClose();
203
beidson@apple.comdfbdf942012-10-19 23:26:16 +0000204 // This may cause us to be deleted.
205 networkProcessCrashedOrFailedToLaunch();
beidson@apple.com21dd3ed2012-10-12 17:13:13 +0000206}
207
andersca@apple.com30e768b2015-01-04 20:14:45 +0000208void NetworkProcessProxy::didReceiveInvalidMessage(IPC::Connection&, IPC::StringReference, IPC::StringReference)
beidson@apple.com21dd3ed2012-10-12 17:13:13 +0000209{
beidson@apple.com21dd3ed2012-10-12 17:13:13 +0000210}
211
andersca@apple.com69388f02013-12-30 20:20:02 +0000212void NetworkProcessProxy::didCreateNetworkConnectionToWebProcess(const IPC::Attachment& connectionIdentifier)
beidson@apple.comdfbdf942012-10-19 23:26:16 +0000213{
214 ASSERT(!m_pendingConnectionReplies.isEmpty());
215
216 // Grab the first pending connection reply.
217 RefPtr<Messages::WebProcessProxy::GetNetworkProcessConnection::DelayedReply> reply = m_pendingConnectionReplies.takeFirst();
218
mitz@apple.com1b970882014-02-15 17:06:17 +0000219#if OS(DARWIN)
andersca@apple.com69388f02013-12-30 20:20:02 +0000220 reply->send(IPC::Attachment(connectionIdentifier.port(), MACH_MSG_TYPE_MOVE_SEND));
svillar@igalia.come78749f2013-12-09 12:28:44 +0000221#elif USE(UNIX_DOMAIN_SOCKETS)
222 reply->send(connectionIdentifier);
beidson@apple.comdfbdf942012-10-19 23:26:16 +0000223#else
224 notImplemented();
225#endif
226}
227
beidson@apple.comdaf91062013-01-26 00:03:48 +0000228void NetworkProcessProxy::didReceiveAuthenticationChallenge(uint64_t pageID, uint64_t frameID, const WebCore::AuthenticationChallenge& coreChallenge, uint64_t challengeID)
229{
230 WebPageProxy* page = WebProcessProxy::webPage(pageID);
231 MESSAGE_CHECK(page);
232
233 RefPtr<AuthenticationChallengeProxy> authenticationChallenge = AuthenticationChallengeProxy::create(coreChallenge, challengeID, connection());
234 page->didReceiveAuthenticationChallengeProxy(frameID, authenticationChallenge.release());
235}
236
andersca@apple.comce927682015-02-27 20:14:22 +0000237void NetworkProcessProxy::didFetchWebsiteData(uint64_t callbackID, const WebsiteData& websiteData)
238{
239 auto callback = m_pendingFetchWebsiteDataCallbacks.take(callbackID);
240 callback(websiteData);
241}
242
andersca@apple.com5ebf4582014-12-09 20:29:59 +0000243void NetworkProcessProxy::didDeleteWebsiteData(uint64_t callbackID)
244{
245 auto callback = m_pendingDeleteWebsiteDataCallbacks.take(callbackID);
246 callback();
247}
248
andersca@apple.comce927682015-02-27 20:14:22 +0000249void NetworkProcessProxy::didDeleteWebsiteDataForOrigins(uint64_t callbackID)
250{
251 auto callback = m_pendingDeleteWebsiteDataForOriginsCallbacks.take(callbackID);
252 callback();
253}
254
andersca@apple.com69388f02013-12-30 20:20:02 +0000255void NetworkProcessProxy::didFinishLaunching(ProcessLauncher* launcher, IPC::Connection::Identifier connectionIdentifier)
beidson@apple.com21dd3ed2012-10-12 17:13:13 +0000256{
ap@apple.com10a670e2012-12-05 23:28:31 +0000257 ChildProcessProxy::didFinishLaunching(launcher, connectionIdentifier);
beidson@apple.com21dd3ed2012-10-12 17:13:13 +0000258
andersca@apple.com69388f02013-12-30 20:20:02 +0000259 if (IPC::Connection::identifierIsNull(connectionIdentifier)) {
beidson@apple.com21dd3ed2012-10-12 17:13:13 +0000260 // FIXME: Do better cleanup here.
261 return;
262 }
263
beidson@apple.comdfbdf942012-10-19 23:26:16 +0000264 for (unsigned i = 0; i < m_numPendingConnectionRequests; ++i)
ap@apple.com10a670e2012-12-05 23:28:31 +0000265 connection()->send(Messages::NetworkProcess::CreateNetworkConnectionToWebProcess(), 0);
beidson@apple.comdfbdf942012-10-19 23:26:16 +0000266
267 m_numPendingConnectionRequests = 0;
commit-queue@webkit.org948d5842012-11-07 01:17:53 +0000268
mitz@apple.com1b970882014-02-15 17:06:17 +0000269#if PLATFORM(COCOA)
weinig@apple.com9c5a3ea2014-12-23 20:29:26 +0000270 if (m_processPool.processSuppressionEnabled())
commit-queue@webkit.orgb1ebaef2013-01-29 22:52:19 +0000271 setProcessSuppressionEnabled(true);
commit-queue@webkit.org948d5842012-11-07 01:17:53 +0000272#endif
barraclough@apple.comd9151342014-04-17 01:40:04 +0000273
bdakin@apple.com1539c6c2015-01-28 22:22:16 +0000274#if PLATFORM(IOS)
simon.fraser@apple.comdd0dee12014-04-22 00:23:04 +0000275 if (xpc_connection_t connection = this->connection()->xpcConnection())
276 m_assertion = std::make_unique<ProcessAssertion>(xpc_connection_get_pid(connection), AssertionState::Foreground);
barraclough@apple.comd9151342014-04-17 01:40:04 +0000277#endif
beidson@apple.com21dd3ed2012-10-12 17:13:13 +0000278}
279
cdumez@apple.com216d2572015-02-23 22:08:45 +0000280void NetworkProcessProxy::logDiagnosticMessage(uint64_t pageID, const String& message, const String& description, bool shouldSample)
cdumez@apple.com35cab1d2015-02-11 23:57:31 +0000281{
282 WebPageProxy* page = WebProcessProxy::webPage(pageID);
283 // 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,
284 // but for now we simply drop the message in the rare case this happens.
285 if (!page)
286 return;
287
cdumez@apple.com216d2572015-02-23 22:08:45 +0000288 page->logDiagnosticMessage(message, description, shouldSample);
cdumez@apple.com35cab1d2015-02-11 23:57:31 +0000289}
290
cdumez@apple.com216d2572015-02-23 22:08:45 +0000291void NetworkProcessProxy::logDiagnosticMessageWithResult(uint64_t pageID, const String& message, const String& description, uint32_t result, bool shouldSample)
cdumez@apple.com35cab1d2015-02-11 23:57:31 +0000292{
293 WebPageProxy* page = WebProcessProxy::webPage(pageID);
294 // 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,
295 // but for now we simply drop the message in the rare case this happens.
296 if (!page)
297 return;
298
cdumez@apple.com216d2572015-02-23 22:08:45 +0000299 page->logDiagnosticMessageWithResult(message, description, result, shouldSample);
cdumez@apple.com35cab1d2015-02-11 23:57:31 +0000300}
301
cdumez@apple.com216d2572015-02-23 22:08:45 +0000302void NetworkProcessProxy::logDiagnosticMessageWithValue(uint64_t pageID, const String& message, const String& description, const String& value, bool shouldSample)
cdumez@apple.com35cab1d2015-02-11 23:57:31 +0000303{
304 WebPageProxy* page = WebProcessProxy::webPage(pageID);
305 // 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,
306 // but for now we simply drop the message in the rare case this happens.
307 if (!page)
308 return;
309
cdumez@apple.com216d2572015-02-23 22:08:45 +0000310 page->logDiagnosticMessageWithValue(message, description, value, shouldSample);
cdumez@apple.com35cab1d2015-02-11 23:57:31 +0000311}
312
beidson@apple.com21dd3ed2012-10-12 17:13:13 +0000313} // namespace WebKit
314
315#endif // ENABLE(NETWORK_PROCESS)