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