2 * Copyright (C) 2017 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
28 #if ENABLE(SERVICE_WORKER)
30 #include "Connection.h"
31 #include "MessageReceiver.h"
32 #include "MessageSender.h"
33 #include "ServiceWorkerClientFetch.h"
34 #include "SharedMemory.h"
35 #include <WebCore/SWClientConnection.h>
36 #include <pal/SessionID.h>
37 #include <wtf/UniqueRef.h>
46 class WebSWOriginTable;
47 class WebServiceWorkerProvider;
49 class WebSWClientConnection final : public WebCore::SWClientConnection, public IPC::MessageSender, public IPC::MessageReceiver {
51 static Ref<WebSWClientConnection> create(IPC::Connection& connection, PAL::SessionID sessionID) { return adoptRef(*new WebSWClientConnection { connection, sessionID }); }
52 ~WebSWClientConnection();
54 WebCore::SWServerConnectionIdentifier serverConnectionIdentifier() const final { return m_identifier; }
56 void addServiceWorkerRegistrationInServer(WebCore::ServiceWorkerRegistrationIdentifier) final;
57 void removeServiceWorkerRegistrationInServer(WebCore::ServiceWorkerRegistrationIdentifier) final;
59 void disconnectedFromWebProcess();
60 void didReceiveMessage(IPC::Connection&, IPC::Decoder&) final;
62 bool mayHaveServiceWorkerRegisteredForOrigin(const WebCore::SecurityOrigin&) const final;
63 void startFetch(const WebCore::ResourceLoader&, uint64_t identifier);
65 void postMessageToServiceWorkerClient(WebCore::DocumentIdentifier destinationContextIdentifier, const IPC::DataReference& message, WebCore::ServiceWorkerData&& source, const String& sourceOrigin);
67 void connectionToServerLost();
69 void syncTerminateWorker(WebCore::ServiceWorkerIdentifier) final;
72 WebSWClientConnection(IPC::Connection&, PAL::SessionID);
74 void scheduleJobInServer(const WebCore::ServiceWorkerJobData&) final;
75 void finishFetchingScriptInServer(const WebCore::ServiceWorkerFetchResult&) final;
76 void postMessageToServiceWorker(WebCore::ServiceWorkerIdentifier destinationIdentifier, Ref<WebCore::SerializedScriptValue>&&, WebCore::ServiceWorkerClientIdentifier sourceIdentifier, WebCore::ServiceWorkerClientData&& source) final;
77 void postMessageToServiceWorker(WebCore::ServiceWorkerIdentifier destinationIdentifier, Ref<WebCore::SerializedScriptValue>&&, WebCore::ServiceWorkerIdentifier sourceWorkerIdentifier) final;
78 void registerServiceWorkerClient(const WebCore::SecurityOrigin& topOrigin, WebCore::DocumentIdentifier, const WebCore::ServiceWorkerClientData&, const std::optional<WebCore::ServiceWorkerIdentifier>&) final;
79 void unregisterServiceWorkerClient(WebCore::DocumentIdentifier) final;
81 void matchRegistration(const WebCore::SecurityOrigin& topOrigin, const WebCore::URL& clientURL, RegistrationCallback&&) final;
82 void didMatchRegistration(uint64_t matchRequestIdentifier, std::optional<WebCore::ServiceWorkerRegistrationData>&&);
83 void didGetRegistrations(uint64_t matchRequestIdentifier, Vector<WebCore::ServiceWorkerRegistrationData>&&);
84 void whenRegistrationReady(const WebCore::SecurityOrigin& topOrigin, const WebCore::URL& clientURL, WhenRegistrationReadyCallback&&) final;
85 void registrationReady(uint64_t callbackID, WebCore::ServiceWorkerRegistrationData&&);
87 void getRegistrations(const WebCore::SecurityOrigin& topOrigin, const WebCore::URL& clientURL, GetRegistrationsCallback&&) final;
89 void didResolveRegistrationPromise(const WebCore::ServiceWorkerRegistrationKey&) final;
91 void scheduleStorageJob(const WebCore::ServiceWorkerJobData&);
93 void runOrDelayTaskForImport(WTF::Function<void()>&& task);
95 IPC::Connection* messageSenderConnection() final { return m_connection.ptr(); }
96 uint64_t messageSenderDestinationID() final { return m_identifier.toUInt64(); }
98 void setSWOriginTableSharedMemory(const SharedMemory::Handle&);
99 void setSWOriginTableIsImported();
101 PAL::SessionID m_sessionID;
102 WebCore::SWServerConnectionIdentifier m_identifier;
104 Ref<IPC::Connection> m_connection;
105 UniqueRef<WebSWOriginTable> m_swOriginTable;
107 uint64_t m_previousCallbackIdentifier { 0 };
108 HashMap<uint64_t, RegistrationCallback> m_ongoingMatchRegistrationTasks;
109 HashMap<uint64_t, GetRegistrationsCallback> m_ongoingGetRegistrationsTasks;
110 HashMap<uint64_t, WhenRegistrationReadyCallback> m_ongoingRegistrationReadyTasks;
111 Deque<WTF::Function<void()>> m_tasksPendingOriginImport;
113 }; // class WebSWServerConnection
115 } // namespace WebKit
117 #endif // ENABLE(SERVICE_WORKER)