91ba9a066323108e16796ab646c13bdd482b0ad1
[WebKit-https.git] / Source / WebKit / UIProcess / Network / NetworkProcessProxy.cpp
1 /*
2  * Copyright (C) 2012-2016 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 "AuthenticationChallengeProxy.h"
30 #include "DownloadProxyMessages.h"
31 #include "LegacyCustomProtocolManagerProxyMessages.h"
32 #include "Logging.h"
33 #include "NetworkProcessCreationParameters.h"
34 #include "NetworkProcessMessages.h"
35 #include "SandboxExtension.h"
36 #include "StorageProcessMessages.h"
37 #include "WebPageProxy.h"
38 #include "WebProcessMessages.h"
39 #include "WebProcessPool.h"
40 #include "WebsiteData.h"
41
42 #if ENABLE(SEC_ITEM_SHIM)
43 #include "SecItemShimProxy.h"
44 #endif
45
46 #if PLATFORM(IOS)
47 #include <wtf/spi/darwin/XPCSPI.h>
48 #endif
49
50 #define MESSAGE_CHECK(assertion) MESSAGE_CHECK_BASE(assertion, connection())
51
52 using namespace WebCore;
53
54 namespace WebKit {
55
56 static uint64_t generateCallbackID()
57 {
58     static uint64_t callbackID;
59
60     return ++callbackID;
61 }
62
63 Ref<NetworkProcessProxy> NetworkProcessProxy::create(WebProcessPool& processPool)
64 {
65     return adoptRef(*new NetworkProcessProxy(processPool));
66 }
67
68 NetworkProcessProxy::NetworkProcessProxy(WebProcessPool& processPool)
69     : ChildProcessProxy(processPool.alwaysRunsAtBackgroundPriority())
70     , m_processPool(processPool)
71     , m_numPendingConnectionRequests(0)
72     , m_customProtocolManagerProxy(*this)
73     , m_throttler(*this, processPool.shouldTakeUIBackgroundAssertion())
74 {
75     connect();
76 }
77
78 NetworkProcessProxy::~NetworkProcessProxy()
79 {
80     ASSERT(m_pendingFetchWebsiteDataCallbacks.isEmpty());
81     ASSERT(m_pendingDeleteWebsiteDataCallbacks.isEmpty());
82     ASSERT(m_pendingDeleteWebsiteDataForOriginsCallbacks.isEmpty());
83 }
84
85 void NetworkProcessProxy::getLaunchOptions(ProcessLauncher::LaunchOptions& launchOptions)
86 {
87     launchOptions.processType = ProcessLauncher::ProcessType::Network;
88     ChildProcessProxy::getLaunchOptions(launchOptions);
89 }
90
91 void NetworkProcessProxy::connectionWillOpen(IPC::Connection& connection)
92 {
93 #if ENABLE(SEC_ITEM_SHIM)
94     SecItemShimProxy::singleton().initializeConnection(connection);
95 #else
96     UNUSED_PARAM(connection);
97 #endif
98 }
99
100 void NetworkProcessProxy::processWillShutDown(IPC::Connection& connection)
101 {
102     ASSERT_UNUSED(connection, this->connection() == &connection);
103 }
104
105 void NetworkProcessProxy::getNetworkProcessConnection(Ref<Messages::WebProcessProxy::GetNetworkProcessConnection::DelayedReply>&& reply)
106 {
107     m_pendingConnectionReplies.append(WTFMove(reply));
108
109     if (state() == State::Launching) {
110         m_numPendingConnectionRequests++;
111         return;
112     }
113
114     connection()->send(Messages::NetworkProcess::CreateNetworkConnectionToWebProcess(), 0, IPC::SendOption::DispatchMessageEvenWhenWaitingForSyncReply);
115 }
116
117 DownloadProxy* NetworkProcessProxy::createDownloadProxy(const ResourceRequest& resourceRequest)
118 {
119     if (!m_downloadProxyMap)
120         m_downloadProxyMap = std::make_unique<DownloadProxyMap>(this);
121
122     return m_downloadProxyMap->createDownloadProxy(m_processPool, resourceRequest);
123 }
124
125 void NetworkProcessProxy::fetchWebsiteData(PAL::SessionID sessionID, OptionSet<WebsiteDataType> dataTypes, OptionSet<WebsiteDataFetchOption> fetchOptions, WTF::Function<void (WebsiteData)>&& completionHandler)
126 {
127     ASSERT(canSendMessage());
128
129     uint64_t callbackID = generateCallbackID();
130     RELEASE_LOG_IF(sessionID.isAlwaysOnLoggingAllowed(), ProcessSuspension, "%p - NetworkProcessProxy is taking a background assertion because the Network process is fetching Website data", this);
131
132     m_pendingFetchWebsiteDataCallbacks.add(callbackID, [this, token = throttler().backgroundActivityToken(), completionHandler = WTFMove(completionHandler), sessionID](WebsiteData websiteData) {
133         completionHandler(WTFMove(websiteData));
134         RELEASE_LOG_IF(sessionID.isAlwaysOnLoggingAllowed(), ProcessSuspension, "%p - NetworkProcessProxy is releasing a background assertion because the Network process is done fetching Website data", this);
135     });
136
137     send(Messages::NetworkProcess::FetchWebsiteData(sessionID, dataTypes, fetchOptions, callbackID), 0);
138 }
139
140 void NetworkProcessProxy::deleteWebsiteData(PAL::SessionID sessionID, OptionSet<WebsiteDataType> dataTypes, std::chrono::system_clock::time_point modifiedSince, WTF::Function<void ()>&& completionHandler)
141 {
142     auto callbackID = generateCallbackID();
143     RELEASE_LOG_IF(sessionID.isAlwaysOnLoggingAllowed(), ProcessSuspension, "%p - NetworkProcessProxy is taking a background assertion because the Network process is deleting Website data", this);
144
145     m_pendingDeleteWebsiteDataCallbacks.add(callbackID, [this, token = throttler().backgroundActivityToken(), completionHandler = WTFMove(completionHandler), sessionID] {
146         completionHandler();
147         RELEASE_LOG_IF(sessionID.isAlwaysOnLoggingAllowed(), ProcessSuspension, "%p - NetworkProcessProxy is releasing a background assertion because the Network process is done deleting Website data", this);
148     });
149     send(Messages::NetworkProcess::DeleteWebsiteData(sessionID, dataTypes, modifiedSince, callbackID), 0);
150 }
151
152 void NetworkProcessProxy::deleteWebsiteDataForOrigins(PAL::SessionID sessionID, OptionSet<WebsiteDataType> dataTypes, const Vector<WebCore::SecurityOriginData>& origins, const Vector<String>& cookieHostNames, WTF::Function<void()>&& completionHandler)
153 {
154     ASSERT(canSendMessage());
155
156     uint64_t callbackID = generateCallbackID();
157     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);
158
159     m_pendingDeleteWebsiteDataForOriginsCallbacks.add(callbackID, [this, token = throttler().backgroundActivityToken(), completionHandler = WTFMove(completionHandler), sessionID] {
160         completionHandler();
161         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);
162     });
163
164     send(Messages::NetworkProcess::DeleteWebsiteDataForOrigins(sessionID, dataTypes, origins, cookieHostNames, callbackID), 0);
165 }
166
167 void NetworkProcessProxy::networkProcessCrashed()
168 {
169     clearCallbackStates();
170
171     Vector<Ref<Messages::WebProcessProxy::GetNetworkProcessConnection::DelayedReply>> pendingReplies;
172     pendingReplies.reserveInitialCapacity(m_pendingConnectionReplies.size());
173     for (auto& reply : m_pendingConnectionReplies)
174         pendingReplies.append(WTFMove(reply));
175
176     // Tell the network process manager to forget about this network process proxy. This may cause us to be deleted.
177     m_processPool.networkProcessCrashed(*this, WTFMove(pendingReplies));
178 }
179
180 void NetworkProcessProxy::networkProcessFailedToLaunch()
181 {
182     // The network process must have crashed or exited, send any pending sync replies we might have.
183     while (!m_pendingConnectionReplies.isEmpty()) {
184         Ref<Messages::WebProcessProxy::GetNetworkProcessConnection::DelayedReply> reply = m_pendingConnectionReplies.takeFirst();
185
186 #if USE(UNIX_DOMAIN_SOCKETS)
187         reply->send(IPC::Attachment());
188 #elif OS(DARWIN)
189         reply->send(IPC::Attachment(0, MACH_MSG_TYPE_MOVE_SEND));
190 #else
191         notImplemented();
192 #endif
193     }
194     clearCallbackStates();
195     // Tell the network process manager to forget about this network process proxy. This may cause us to be deleted.
196     m_processPool.networkProcessFailedToLaunch(*this);
197 }
198
199 void NetworkProcessProxy::clearCallbackStates()
200 {
201     for (const auto& callback : m_pendingFetchWebsiteDataCallbacks.values())
202         callback(WebsiteData());
203     m_pendingFetchWebsiteDataCallbacks.clear();
204
205     for (const auto& callback : m_pendingDeleteWebsiteDataCallbacks.values())
206         callback();
207     m_pendingDeleteWebsiteDataCallbacks.clear();
208
209     for (const auto& callback : m_pendingDeleteWebsiteDataForOriginsCallbacks.values())
210         callback();
211     m_pendingDeleteWebsiteDataForOriginsCallbacks.clear();
212 }
213
214 void NetworkProcessProxy::didReceiveMessage(IPC::Connection& connection, IPC::Decoder& decoder)
215 {
216     if (dispatchMessage(connection, decoder))
217         return;
218
219     if (m_processPool.dispatchMessage(connection, decoder))
220         return;
221
222     didReceiveNetworkProcessProxyMessage(connection, decoder);
223 }
224
225 void NetworkProcessProxy::didReceiveSyncMessage(IPC::Connection& connection, IPC::Decoder& decoder, std::unique_ptr<IPC::Encoder>& replyEncoder)
226 {
227     if (dispatchSyncMessage(connection, decoder, replyEncoder))
228         return;
229
230     ASSERT_NOT_REACHED();
231 }
232
233 void NetworkProcessProxy::didClose(IPC::Connection&)
234 {
235     if (m_downloadProxyMap)
236         m_downloadProxyMap->processDidClose();
237     m_customProtocolManagerProxy.invalidate();
238
239     m_tokenForHoldingLockedFiles = nullptr;
240
241     // This may cause us to be deleted.
242     networkProcessCrashed();
243 }
244
245 void NetworkProcessProxy::didReceiveInvalidMessage(IPC::Connection&, IPC::StringReference, IPC::StringReference)
246 {
247 }
248
249 void NetworkProcessProxy::didCreateNetworkConnectionToWebProcess(const IPC::Attachment& connectionIdentifier)
250 {
251     ASSERT(!m_pendingConnectionReplies.isEmpty());
252
253     // Grab the first pending connection reply.
254     RefPtr<Messages::WebProcessProxy::GetNetworkProcessConnection::DelayedReply> reply = m_pendingConnectionReplies.takeFirst();
255
256 #if USE(UNIX_DOMAIN_SOCKETS)
257     reply->send(connectionIdentifier);
258 #elif OS(DARWIN)
259     reply->send(IPC::Attachment(connectionIdentifier.port(), MACH_MSG_TYPE_MOVE_SEND));
260 #else
261     notImplemented();
262 #endif
263 }
264
265 void NetworkProcessProxy::didReceiveAuthenticationChallenge(uint64_t pageID, uint64_t frameID, const WebCore::AuthenticationChallenge& coreChallenge, uint64_t challengeID)
266 {
267     WebPageProxy* page = WebProcessProxy::webPage(pageID);
268     MESSAGE_CHECK(page);
269
270     auto authenticationChallenge = AuthenticationChallengeProxy::create(coreChallenge, challengeID, connection());
271     page->didReceiveAuthenticationChallengeProxy(frameID, WTFMove(authenticationChallenge));
272 }
273
274 void NetworkProcessProxy::didFetchWebsiteData(uint64_t callbackID, const WebsiteData& websiteData)
275 {
276     auto callback = m_pendingFetchWebsiteDataCallbacks.take(callbackID);
277     callback(websiteData);
278 }
279
280 void NetworkProcessProxy::didDeleteWebsiteData(uint64_t callbackID)
281 {
282     auto callback = m_pendingDeleteWebsiteDataCallbacks.take(callbackID);
283     callback();
284 }
285
286 void NetworkProcessProxy::didDeleteWebsiteDataForOrigins(uint64_t callbackID)
287 {
288     auto callback = m_pendingDeleteWebsiteDataForOriginsCallbacks.take(callbackID);
289     callback();
290 }
291
292 void NetworkProcessProxy::grantSandboxExtensionsToStorageProcessForBlobs(uint64_t requestID, const Vector<String>& paths)
293 {
294 #if ENABLE(SANDBOX_EXTENSIONS)
295     SandboxExtension::HandleArray extensions;
296     extensions.allocate(paths.size());
297     for (size_t i = 0; i < paths.size(); ++i) {
298         // ReadWrite is required for creating hard links as well as deleting the temporary file, which the StorageProcess will do.
299         SandboxExtension::createHandle(paths[i], SandboxExtension::ReadWrite, extensions[i]);
300     }
301
302     m_processPool.sendToStorageProcessRelaunchingIfNecessary(Messages::StorageProcess::GrantSandboxExtensionsForBlobs(paths, extensions));
303 #endif
304     connection()->send(Messages::NetworkProcess::DidGrantSandboxExtensionsToStorageProcessForBlobs(requestID), 0);
305 }
306
307 void NetworkProcessProxy::didFinishLaunching(ProcessLauncher* launcher, IPC::Connection::Identifier connectionIdentifier)
308 {
309     ChildProcessProxy::didFinishLaunching(launcher, connectionIdentifier);
310
311     if (IPC::Connection::identifierIsNull(connectionIdentifier)) {
312         networkProcessFailedToLaunch();
313         return;
314     }
315
316     for (unsigned i = 0; i < m_numPendingConnectionRequests; ++i)
317         connection()->send(Messages::NetworkProcess::CreateNetworkConnectionToWebProcess(), 0);
318     
319     m_numPendingConnectionRequests = 0;
320
321 #if PLATFORM(COCOA)
322     if (m_processPool.processSuppressionEnabled())
323         setProcessSuppressionEnabled(true);
324 #endif
325     
326 #if PLATFORM(IOS)
327     if (xpc_connection_t connection = this->connection()->xpcConnection())
328         m_throttler.didConnectToProcess(xpc_connection_get_pid(connection));
329 #endif
330 }
331
332 void NetworkProcessProxy::logDiagnosticMessage(uint64_t pageID, const String& message, const String& description, WebCore::ShouldSample shouldSample)
333 {
334     WebPageProxy* page = WebProcessProxy::webPage(pageID);
335     // 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,
336     // but for now we simply drop the message in the rare case this happens.
337     if (!page)
338         return;
339
340     page->logDiagnosticMessage(message, description, shouldSample);
341 }
342
343 void NetworkProcessProxy::logDiagnosticMessageWithResult(uint64_t pageID, const String& message, const String& description, uint32_t result, WebCore::ShouldSample shouldSample)
344 {
345     WebPageProxy* page = WebProcessProxy::webPage(pageID);
346     // 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,
347     // but for now we simply drop the message in the rare case this happens.
348     if (!page)
349         return;
350
351     page->logDiagnosticMessageWithResult(message, description, result, shouldSample);
352 }
353
354 void NetworkProcessProxy::logDiagnosticMessageWithValue(uint64_t pageID, const String& message, const String& description, double value, unsigned significantFigures, WebCore::ShouldSample shouldSample)
355 {
356     WebPageProxy* page = WebProcessProxy::webPage(pageID);
357     // 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,
358     // but for now we simply drop the message in the rare case this happens.
359     if (!page)
360         return;
361
362     page->logDiagnosticMessageWithValue(message, description, value, significantFigures, shouldSample);
363 }
364
365 #if USE(PROTECTION_SPACE_AUTH_CALLBACK)
366 void NetworkProcessProxy::canAuthenticateAgainstProtectionSpace(uint64_t loaderID, uint64_t pageID, uint64_t frameID, const WebCore::ProtectionSpace& protectionSpace)
367 {
368     // NetworkProcess state cannot asynchronously be kept in sync with these objects
369     // like we expect WebProcess <-> UIProcess state to be kept in sync.
370     // So there's no guarantee the messaged WebPageProxy or WebFrameProxy exist here in the UIProcess.
371     // We need to validate both the page and the frame up front.
372     if (auto* page = WebProcessProxy::webPage(pageID)) {
373         if (page->process().webFrame(frameID)) {
374             page->canAuthenticateAgainstProtectionSpace(loaderID, frameID, protectionSpace);
375             return;
376         }
377     }
378     
379     // In the case where we will not be able to reply to this message with a client reply,
380     // we should message back a default to the Networking process.
381     send(Messages::NetworkProcess::ContinueCanAuthenticateAgainstProtectionSpace(loaderID, false), 0);
382 }
383 #endif
384
385 void NetworkProcessProxy::sendProcessWillSuspendImminently()
386 {
387     if (!canSendMessage())
388         return;
389
390     bool handled = false;
391     sendSync(Messages::NetworkProcess::ProcessWillSuspendImminently(), Messages::NetworkProcess::ProcessWillSuspendImminently::Reply(handled), 0, 1_s);
392 }
393     
394 void NetworkProcessProxy::sendPrepareToSuspend()
395 {
396     if (canSendMessage())
397         send(Messages::NetworkProcess::PrepareToSuspend(), 0);
398 }
399
400 void NetworkProcessProxy::sendCancelPrepareToSuspend()
401 {
402     if (canSendMessage())
403         send(Messages::NetworkProcess::CancelPrepareToSuspend(), 0);
404 }
405
406 void NetworkProcessProxy::sendProcessDidResume()
407 {
408     if (canSendMessage())
409         send(Messages::NetworkProcess::ProcessDidResume(), 0);
410 }
411
412 void NetworkProcessProxy::processReadyToSuspend()
413 {
414     m_throttler.processReadyToSuspend();
415 }
416
417 void NetworkProcessProxy::didSetAssertionState(AssertionState)
418 {
419 }
420     
421 void NetworkProcessProxy::setIsHoldingLockedFiles(bool isHoldingLockedFiles)
422 {
423     if (!isHoldingLockedFiles) {
424         RELEASE_LOG(ProcessSuspension, "UIProcess is releasing a background assertion because the Network process is no longer holding locked files");
425         m_tokenForHoldingLockedFiles = nullptr;
426         return;
427     }
428     if (!m_tokenForHoldingLockedFiles) {
429         RELEASE_LOG(ProcessSuspension, "UIProcess is taking a background assertion because the Network process is holding locked files");
430         m_tokenForHoldingLockedFiles = m_throttler.backgroundActivityToken();
431     }
432 }
433
434 } // namespace WebKit