Delay some service worker operations until after the database import completes.
[WebKit-https.git] / Source / WebKit / WebProcess / Storage / WebSWClientConnection.h
1 /*
2  * Copyright (C) 2017 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 #pragma once
27
28 #if ENABLE(SERVICE_WORKER)
29
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>
38
39 namespace WebCore {
40 struct ExceptionData;
41 class ResourceLoader;
42 }
43
44 namespace WebKit {
45
46 class WebSWOriginTable;
47 class WebServiceWorkerProvider;
48
49 class WebSWClientConnection final : public WebCore::SWClientConnection, public IPC::MessageSender, public IPC::MessageReceiver {
50 public:
51     static Ref<WebSWClientConnection> create(IPC::Connection& connection, PAL::SessionID sessionID) { return adoptRef(*new WebSWClientConnection { connection, sessionID }); }
52     ~WebSWClientConnection();
53
54     WebCore::SWServerConnectionIdentifier serverConnectionIdentifier() const final { return m_identifier; }
55
56     void addServiceWorkerRegistrationInServer(WebCore::ServiceWorkerRegistrationIdentifier) final;
57     void removeServiceWorkerRegistrationInServer(WebCore::ServiceWorkerRegistrationIdentifier) final;
58
59     void disconnectedFromWebProcess();
60     void didReceiveMessage(IPC::Connection&, IPC::Decoder&) final;
61
62     bool mayHaveServiceWorkerRegisteredForOrigin(const WebCore::SecurityOrigin&) const final;
63     void startFetch(const WebCore::ResourceLoader&, uint64_t identifier);
64
65     void postMessageToServiceWorkerClient(WebCore::DocumentIdentifier destinationContextIdentifier, const IPC::DataReference& message, WebCore::ServiceWorkerData&& source, const String& sourceOrigin);
66
67     void connectionToServerLost();
68
69     void syncTerminateWorker(WebCore::ServiceWorkerIdentifier) final;
70
71 private:
72     WebSWClientConnection(IPC::Connection&, PAL::SessionID);
73
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;
80
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&&);
86
87     void getRegistrations(const WebCore::SecurityOrigin& topOrigin, const WebCore::URL& clientURL, GetRegistrationsCallback&&) final;
88
89     void didResolveRegistrationPromise(const WebCore::ServiceWorkerRegistrationKey&) final;
90
91     void scheduleStorageJob(const WebCore::ServiceWorkerJobData&);
92
93     void runOrDelayTaskForImport(WTF::Function<void()>&& task);
94
95     IPC::Connection* messageSenderConnection() final { return m_connection.ptr(); }
96     uint64_t messageSenderDestinationID() final { return m_identifier.toUInt64(); }
97
98     void setSWOriginTableSharedMemory(const SharedMemory::Handle&);
99     void setSWOriginTableIsImported();
100
101     PAL::SessionID m_sessionID;
102     WebCore::SWServerConnectionIdentifier m_identifier;
103
104     Ref<IPC::Connection> m_connection;
105     UniqueRef<WebSWOriginTable> m_swOriginTable;
106
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;
112
113 }; // class WebSWServerConnection
114
115 } // namespace WebKit
116
117 #endif // ENABLE(SERVICE_WORKER)