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