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