REGRESSION (r225789): API tests WKProcessPool.InitialWarmedProcessUsed and WebKit...
[WebKit-https.git] / Source / WebKit / StorageProcess / StorageProcess.cpp
1 /*
2  * Copyright (C) 2013, 2014, 2015, 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 "StorageProcess.h"
28
29 #include "StorageProcessCreationParameters.h"
30 #include "StorageProcessMessages.h"
31 #include "StorageProcessProxyMessages.h"
32 #include "StorageToWebProcessConnection.h"
33 #include "WebCoreArgumentCoders.h"
34 #include "WebSWOriginStore.h"
35 #include "WebSWServerConnection.h"
36 #include "WebSWServerToContextConnection.h"
37 #include "WebsiteData.h"
38 #include <WebCore/FileSystem.h>
39 #include <WebCore/IDBKeyData.h>
40 #include <WebCore/NotImplemented.h>
41 #include <WebCore/SWServerWorker.h>
42 #include <WebCore/SecurityOrigin.h>
43 #include <WebCore/ServiceWorkerClientIdentifier.h>
44 #include <WebCore/TextEncoding.h>
45 #include <pal/SessionID.h>
46 #include <wtf/CallbackAggregator.h>
47 #include <wtf/CrossThreadTask.h>
48 #include <wtf/MainThread.h>
49
50 #if ENABLE(SERVICE_WORKER)
51 #include "WebSWServerToContextConnectionMessages.h"
52 #endif
53
54 using namespace WebCore;
55
56 namespace WebKit {
57
58 StorageProcess& StorageProcess::singleton()
59 {
60     static NeverDestroyed<StorageProcess> process;
61     return process;
62 }
63
64 StorageProcess::StorageProcess()
65     : m_queue(WorkQueue::create("com.apple.WebKit.StorageProcess"))
66 {
67     // Make sure the UTF8Encoding encoding and the text encoding maps have been built on the main thread before a background thread needs it.
68     // FIXME: https://bugs.webkit.org/show_bug.cgi?id=135365 - Need a more explicit way of doing this besides accessing the UTF8Encoding.
69     UTF8Encoding();
70 }
71
72 StorageProcess::~StorageProcess()
73 {
74 }
75
76 void StorageProcess::initializeConnection(IPC::Connection* connection)
77 {
78     ChildProcess::initializeConnection(connection);
79 }
80
81 bool StorageProcess::shouldTerminate()
82 {
83     return true;
84 }
85
86 void StorageProcess::didClose(IPC::Connection& connection)
87 {
88 #if ENABLE(SERVICE_WORKER)
89     if (m_serverToContextConnection && m_serverToContextConnection->ipcConnection() == &connection) {
90         connectionToContextProcessWasClosed();
91         return;
92     }
93 #else
94     UNUSED_PARAM(connection);
95 #endif
96     stopRunLoop();
97 }
98
99 #if ENABLE(SERVICE_WORKER)
100 void StorageProcess::connectionToContextProcessWasClosed()
101 {
102     if (!m_serverToContextConnection)
103         return;
104
105     bool shouldRelaunch = needsServerToContextConnection();
106
107     m_serverToContextConnection->connectionClosed();
108     m_serverToContextConnection = nullptr;
109
110     for (auto& swServer : m_swServers.values())
111         swServer->markAllWorkersAsTerminated();
112
113     if (shouldRelaunch)
114         createServerToContextConnection(std::nullopt);
115 }
116
117 // The rule is that we need a context process (and a connection to it) as long as we have SWServerConnections to regular WebProcesses.
118 bool StorageProcess::needsServerToContextConnection() const
119 {
120     if (m_swServerConnections.isEmpty())
121         return false;
122
123     // If the last SWServerConnection is to the context process, then we no longer need the context connection.
124     if (m_swServerConnections.size() == 1 && m_serverToContextConnection && &m_swServerConnections.begin()->value->ipcConnection() == m_serverToContextConnection->ipcConnection())
125         return false;
126
127     return true;
128 }
129 #endif
130
131 void StorageProcess::didReceiveMessage(IPC::Connection& connection, IPC::Decoder& decoder)
132 {
133     if (messageReceiverMap().dispatchMessage(connection, decoder))
134         return;
135
136     if (decoder.messageReceiverName() == Messages::StorageProcess::messageReceiverName()) {
137         didReceiveStorageProcessMessage(connection, decoder);
138         return;
139     }
140
141 #if ENABLE(SERVICE_WORKER)
142     if (decoder.messageReceiverName() == Messages::WebSWServerToContextConnection::messageReceiverName()) {
143         if (auto* swConnection = SWServerToContextConnection::globalServerToContextConnection()) {
144             auto* webSWConnection = static_cast<WebSWServerToContextConnection*>(swConnection);
145             webSWConnection->didReceiveMessage(connection, decoder);
146             return;        
147         }
148     }
149 #endif
150 }
151
152 #if ENABLE(INDEXED_DATABASE)
153 IDBServer::IDBServer& StorageProcess::idbServer(PAL::SessionID sessionID)
154 {
155     auto addResult = m_idbServers.add(sessionID, nullptr);
156     if (!addResult.isNewEntry) {
157         ASSERT(addResult.iterator->value);
158         return *addResult.iterator->value;
159     }
160
161     auto path = m_idbDatabasePaths.get(sessionID);
162     // There should already be a registered path for this PAL::SessionID.
163     // If there's not, then where did this PAL::SessionID come from?
164     ASSERT(!path.isEmpty());
165
166     addResult.iterator->value = IDBServer::IDBServer::create(path, StorageProcess::singleton());
167     return *addResult.iterator->value;
168 }
169 #endif
170
171 void StorageProcess::initializeWebsiteDataStore(const StorageProcessCreationParameters& parameters)
172 {
173 #if ENABLE(INDEXED_DATABASE)
174     // *********
175     // IMPORTANT: Do not change the directory structure for indexed databases on disk without first consulting a reviewer from Apple (<rdar://problem/17454712>)
176     // *********
177
178     auto addResult = m_idbDatabasePaths.ensure(parameters.sessionID, [path = parameters.indexedDatabaseDirectory] {
179         return path;
180     });
181     if (addResult.isNewEntry) {
182         SandboxExtension::consumePermanently(parameters.indexedDatabaseDirectoryExtensionHandle);
183         postStorageTask(createCrossThreadTask(*this, &StorageProcess::ensurePathExists, parameters.indexedDatabaseDirectory));
184     }
185 #endif
186 #if ENABLE(SERVICE_WORKER)
187     addResult = m_swDatabasePaths.ensure(parameters.sessionID, [path = parameters.serviceWorkerRegistrationDirectory] {
188         return path;
189     });
190     if (addResult.isNewEntry) {
191         SandboxExtension::consumePermanently(parameters.serviceWorkerRegistrationDirectoryExtensionHandle);
192         postStorageTask(createCrossThreadTask(*this, &StorageProcess::ensurePathExists, parameters.serviceWorkerRegistrationDirectory));
193     }
194 #endif
195 }
196
197 void StorageProcess::ensurePathExists(const String& path)
198 {
199     ASSERT(!RunLoop::isMain());
200
201     if (!FileSystem::makeAllDirectories(path))
202         LOG_ERROR("Failed to make all directories for path '%s'", path.utf8().data());
203 }
204
205 void StorageProcess::postStorageTask(CrossThreadTask&& task)
206 {
207     ASSERT(RunLoop::isMain());
208
209     LockHolder locker(m_storageTaskMutex);
210
211     m_storageTasks.append(WTFMove(task));
212
213     m_queue->dispatch([this] {
214         performNextStorageTask();
215     });
216 }
217
218 void StorageProcess::performNextStorageTask()
219 {
220     ASSERT(!RunLoop::isMain());
221
222     CrossThreadTask task;
223     {
224         LockHolder locker(m_storageTaskMutex);
225         ASSERT(!m_storageTasks.isEmpty());
226         task = m_storageTasks.takeFirst();
227     }
228
229     task.performTask();
230 }
231
232 void StorageProcess::createStorageToWebProcessConnection(bool isServiceWorkerProcess)
233 {
234 #if USE(UNIX_DOMAIN_SOCKETS)
235     IPC::Connection::SocketPair socketPair = IPC::Connection::createPlatformConnection();
236     m_storageToWebProcessConnections.append(StorageToWebProcessConnection::create(socketPair.server));
237     parentProcessConnection()->send(Messages::StorageProcessProxy::DidCreateStorageToWebProcessConnection(IPC::Attachment(socketPair.client)), 0);
238 #elif OS(DARWIN)
239     // Create the listening port.
240     mach_port_t listeningPort;
241     mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &listeningPort);
242
243     // Create a listening connection.
244     m_storageToWebProcessConnections.append(StorageToWebProcessConnection::create(IPC::Connection::Identifier(listeningPort)));
245
246     IPC::Attachment clientPort(listeningPort, MACH_MSG_TYPE_MAKE_SEND);
247     parentProcessConnection()->send(Messages::StorageProcessProxy::DidCreateStorageToWebProcessConnection(clientPort), 0);
248 #else
249     notImplemented();
250 #endif
251
252 #if ENABLE(SERVICE_WORKER)
253     if (isServiceWorkerProcess && !m_storageToWebProcessConnections.isEmpty()) {
254         ASSERT(m_waitingForServerToContextProcessConnection);
255         m_serverToContextConnection = WebSWServerToContextConnection::create(m_storageToWebProcessConnections.last()->connection());
256         m_waitingForServerToContextProcessConnection = false;
257
258         for (auto& connection : m_storageToWebProcessConnections)
259             connection->workerContextProcessConnectionCreated();
260     }
261 #else
262     UNUSED_PARAM(isServiceWorkerProcess);
263 #endif
264 }
265
266 void StorageProcess::fetchWebsiteData(PAL::SessionID sessionID, OptionSet<WebsiteDataType> websiteDataTypes, uint64_t callbackID)
267 {
268     auto completionHandler = [this, callbackID](const WebsiteData& websiteData) {
269         parentProcessConnection()->send(Messages::StorageProcessProxy::DidFetchWebsiteData(callbackID, websiteData), 0);
270     };
271
272 #if ENABLE(SERVICE_WORKER)
273     if (websiteDataTypes.contains(WebsiteDataType::ServiceWorkerRegistrations))
274         notImplemented();
275 #endif
276
277 #if ENABLE(INDEXED_DATABASE)
278     String path = m_idbDatabasePaths.get(sessionID);
279     if (!path.isEmpty() && websiteDataTypes.contains(WebsiteDataType::IndexedDBDatabases)) {
280         // FIXME: Pick the right database store based on the session ID.
281         postStorageTask(CrossThreadTask([this, completionHandler = WTFMove(completionHandler), path = WTFMove(path)]() mutable {
282             RunLoop::main().dispatch([completionHandler = WTFMove(completionHandler), securityOrigins = indexedDatabaseOrigins(path)] {
283                 WebsiteData websiteData;
284                 for (const auto& securityOrigin : securityOrigins)
285                     websiteData.entries.append({ securityOrigin, WebsiteDataType::IndexedDBDatabases, 0 });
286
287                 completionHandler(websiteData);
288             });
289         }));
290         return;
291     }
292 #endif
293
294     completionHandler({ });
295 }
296
297 void StorageProcess::deleteWebsiteData(PAL::SessionID sessionID, OptionSet<WebsiteDataType> websiteDataTypes, std::chrono::system_clock::time_point modifiedSince, uint64_t callbackID)
298 {
299     auto callbackAggregator = CallbackAggregator::create([this, callbackID] {
300         parentProcessConnection()->send(Messages::StorageProcessProxy::DidDeleteWebsiteData(callbackID), 0);
301     });
302
303 #if ENABLE(SERVICE_WORKER)
304     if (websiteDataTypes.contains(WebsiteDataType::ServiceWorkerRegistrations))
305         swServerForSession(sessionID).clearAll([callbackAggregator = callbackAggregator.copyRef()] { });
306 #endif
307
308 #if ENABLE(INDEXED_DATABASE)
309     if (websiteDataTypes.contains(WebsiteDataType::IndexedDBDatabases))
310         idbServer(sessionID).closeAndDeleteDatabasesModifiedSince(modifiedSince, [callbackAggregator = WTFMove(callbackAggregator)] { });
311 #endif
312 }
313
314 void StorageProcess::deleteWebsiteDataForOrigins(PAL::SessionID sessionID, OptionSet<WebsiteDataType> websiteDataTypes, const Vector<SecurityOriginData>& securityOriginDatas, uint64_t callbackID)
315 {
316     auto callbackAggregator = CallbackAggregator::create([this, callbackID]() {
317         parentProcessConnection()->send(Messages::StorageProcessProxy::DidDeleteWebsiteDataForOrigins(callbackID), 0);
318     });
319
320 #if ENABLE(SERVICE_WORKER)
321     if (websiteDataTypes.contains(WebsiteDataType::ServiceWorkerRegistrations)) {
322         auto& server = swServerForSession(sessionID);
323         for (auto& originData : securityOriginDatas)
324             server.clear(originData.securityOrigin(), [callbackAggregator = callbackAggregator.copyRef()] { });
325     }
326 #endif
327
328 #if ENABLE(INDEXED_DATABASE)
329     if (!websiteDataTypes.contains(WebsiteDataType::IndexedDBDatabases))
330         idbServer(sessionID).closeAndDeleteDatabasesForOrigins(securityOriginDatas, [callbackAggregator = WTFMove(callbackAggregator)] { });
331 #endif
332 }
333
334 #if ENABLE(SANDBOX_EXTENSIONS)
335 void StorageProcess::grantSandboxExtensionsForBlobs(const Vector<String>& paths, SandboxExtension::HandleArray&& handles)
336 {
337     ASSERT(paths.size() == handles.size());
338
339     for (size_t i = 0; i < paths.size(); ++i) {
340         auto result = m_blobTemporaryFileSandboxExtensions.add(paths[i], SandboxExtension::create(WTFMove(handles[i])));
341         ASSERT_UNUSED(result, result.isNewEntry);
342     }
343 }
344 #endif
345
346 #if ENABLE(INDEXED_DATABASE)
347 void StorageProcess::prepareForAccessToTemporaryFile(const String& path)
348 {
349     if (auto extension = m_blobTemporaryFileSandboxExtensions.get(path))
350         extension->consume();
351 }
352
353 void StorageProcess::accessToTemporaryFileComplete(const String& path)
354 {
355     // We've either hard linked the temporary blob file to the database directory, copied it there,
356     // or the transaction is being aborted.
357     // In any of those cases, we can delete the temporary blob file now.
358     FileSystem::deleteFile(path);
359
360     if (auto extension = m_blobTemporaryFileSandboxExtensions.take(path))
361         extension->revoke();
362 }
363
364 Vector<WebCore::SecurityOriginData> StorageProcess::indexedDatabaseOrigins(const String& path)
365 {
366     if (path.isEmpty())
367         return { };
368
369     Vector<WebCore::SecurityOriginData> securityOrigins;
370     for (auto& originPath : FileSystem::listDirectory(path, "*")) {
371         String databaseIdentifier = FileSystem::pathGetFileName(originPath);
372
373         if (auto securityOrigin = SecurityOriginData::fromDatabaseIdentifier(databaseIdentifier))
374             securityOrigins.append(WTFMove(*securityOrigin));
375     }
376
377     return securityOrigins;
378 }
379
380 #endif
381
382 #if ENABLE(SANDBOX_EXTENSIONS)
383 void StorageProcess::getSandboxExtensionsForBlobFiles(const Vector<String>& filenames, WTF::Function<void (SandboxExtension::HandleArray&&)>&& completionHandler)
384 {
385     static uint64_t lastRequestID;
386
387     uint64_t requestID = ++lastRequestID;
388     m_sandboxExtensionForBlobsCompletionHandlers.set(requestID, WTFMove(completionHandler));
389     parentProcessConnection()->send(Messages::StorageProcessProxy::GetSandboxExtensionsForBlobFiles(requestID, filenames), 0);
390 }
391
392 void StorageProcess::didGetSandboxExtensionsForBlobFiles(uint64_t requestID, SandboxExtension::HandleArray&& handles)
393 {
394     if (auto handler = m_sandboxExtensionForBlobsCompletionHandlers.take(requestID))
395         handler(WTFMove(handles));
396 }
397 #endif
398
399 #if ENABLE(SERVICE_WORKER)
400 SWServer& StorageProcess::swServerForSession(PAL::SessionID sessionID)
401 {
402     auto result = m_swServers.add(sessionID, nullptr);
403     if (!result.isNewEntry) {
404         ASSERT(result.iterator->value);
405         return *result.iterator->value;
406     }
407
408     auto path = m_swDatabasePaths.get(sessionID);
409     // There should already be a registered path for this PAL::SessionID.
410     // If there's not, then where did this PAL::SessionID come from?
411     ASSERT(sessionID.isEphemeral() || !path.isEmpty());
412
413     result.iterator->value = std::make_unique<SWServer>(makeUniqueRef<WebSWOriginStore>(), WTFMove(path), sessionID);
414     return *result.iterator->value;
415 }
416
417 WebSWOriginStore& StorageProcess::swOriginStoreForSession(PAL::SessionID sessionID)
418 {
419     return static_cast<WebSWOriginStore&>(swServerForSession(sessionID).originStore());
420 }
421
422 WebSWServerToContextConnection* StorageProcess::globalServerToContextConnection()
423 {
424     return m_serverToContextConnection.get();
425 }
426
427 void StorageProcess::createServerToContextConnection(std::optional<PAL::SessionID> sessionID)
428 {
429     if (m_waitingForServerToContextProcessConnection)
430         return;
431     
432     m_waitingForServerToContextProcessConnection = true;
433     if (sessionID)
434         parentProcessConnection()->send(Messages::StorageProcessProxy::EstablishWorkerContextConnectionToStorageProcessForExplicitSession(*sessionID), 0);
435     else
436         parentProcessConnection()->send(Messages::StorageProcessProxy::EstablishWorkerContextConnectionToStorageProcess(), 0);
437 }
438
439 void StorageProcess::didFailFetch(SWServerConnectionIdentifier serverConnectionIdentifier, uint64_t fetchIdentifier)
440 {
441     if (auto* connection = m_swServerConnections.get(serverConnectionIdentifier))
442         connection->didFailFetch(fetchIdentifier);
443 }
444
445 void StorageProcess::didNotHandleFetch(SWServerConnectionIdentifier serverConnectionIdentifier, uint64_t fetchIdentifier)
446 {
447     if (auto* connection = m_swServerConnections.get(serverConnectionIdentifier))
448         connection->didNotHandleFetch(fetchIdentifier);
449 }
450
451 void StorageProcess::didReceiveFetchResponse(SWServerConnectionIdentifier serverConnectionIdentifier, uint64_t fetchIdentifier, const WebCore::ResourceResponse& response)
452 {
453     if (auto* connection = m_swServerConnections.get(serverConnectionIdentifier))
454         connection->didReceiveFetchResponse(fetchIdentifier, response);
455 }
456
457 void StorageProcess::didReceiveFetchData(SWServerConnectionIdentifier serverConnectionIdentifier, uint64_t fetchIdentifier, const IPC::DataReference& data, int64_t encodedDataLength)
458 {
459     if (auto* connection = m_swServerConnections.get(serverConnectionIdentifier))
460         connection->didReceiveFetchData(fetchIdentifier, data, encodedDataLength);
461 }
462
463 void StorageProcess::didReceiveFetchFormData(SWServerConnectionIdentifier serverConnectionIdentifier, uint64_t fetchIdentifier, const IPC::FormDataReference& formData)
464 {
465     if (auto* connection = m_swServerConnections.get(serverConnectionIdentifier))
466         connection->didReceiveFetchFormData(fetchIdentifier, formData);
467 }
468
469 void StorageProcess::didFinishFetch(SWServerConnectionIdentifier serverConnectionIdentifier, uint64_t fetchIdentifier)
470 {
471     if (auto* connection = m_swServerConnections.get(serverConnectionIdentifier))
472         connection->didFinishFetch(fetchIdentifier);
473 }
474
475 void StorageProcess::postMessageToServiceWorkerClient(const ServiceWorkerClientIdentifier& destinationIdentifier, const IPC::DataReference& message, ServiceWorkerIdentifier sourceIdentifier, const String& sourceOrigin)
476 {
477     if (auto* connection = m_swServerConnections.get(destinationIdentifier.serverConnectionIdentifier))
478         connection->postMessageToServiceWorkerClient(destinationIdentifier.contextIdentifier, message, sourceIdentifier, sourceOrigin);
479 }
480
481 void StorageProcess::registerSWServerConnection(WebSWServerConnection& connection)
482 {
483     ASSERT(!m_swServerConnections.contains(connection.identifier()));
484     m_swServerConnections.add(connection.identifier(), &connection);
485     swOriginStoreForSession(connection.sessionID()).registerSWServerConnection(connection);
486 }
487
488 void StorageProcess::unregisterSWServerConnection(WebSWServerConnection& connection)
489 {
490     ASSERT(m_swServerConnections.get(connection.identifier()) == &connection);
491     m_swServerConnections.remove(connection.identifier());
492     swOriginStoreForSession(connection.sessionID()).unregisterSWServerConnection(connection);
493 }
494 #endif
495
496 #if !PLATFORM(COCOA)
497 void StorageProcess::initializeProcess(const ChildProcessInitializationParameters&)
498 {
499 }
500
501 void StorageProcess::initializeProcessName(const ChildProcessInitializationParameters&)
502 {
503 }
504
505 void StorageProcess::initializeSandbox(const ChildProcessInitializationParameters&, SandboxInitializationParameters&)
506 {
507 }
508 #endif
509
510 } // namespace WebKit