Move URL from WebCore to WTF
[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/MessageWithMessagePorts.h>
36 #include <WebCore/SWClientConnection.h>
37 #include <pal/SessionID.h>
38 #include <wtf/UniqueRef.h>
39
40 namespace WebCore {
41 struct ExceptionData;
42 class ResourceLoader;
43 }
44
45 namespace WebKit {
46
47 class WebSWOriginTable;
48 class WebServiceWorkerProvider;
49
50 class WebSWClientConnection final : public WebCore::SWClientConnection, public IPC::MessageSender, public IPC::MessageReceiver {
51 public:
52     static Ref<WebSWClientConnection> create(IPC::Connection& connection, PAL::SessionID sessionID) { return adoptRef(*new WebSWClientConnection { connection, sessionID }); }
53     ~WebSWClientConnection();
54
55     WebCore::SWServerConnectionIdentifier serverConnectionIdentifier() const final { return m_identifier; }
56
57     void addServiceWorkerRegistrationInServer(WebCore::ServiceWorkerRegistrationIdentifier) final;
58     void removeServiceWorkerRegistrationInServer(WebCore::ServiceWorkerRegistrationIdentifier) final;
59
60     void disconnectedFromWebProcess();
61     void didReceiveMessage(IPC::Connection&, IPC::Decoder&) final;
62
63     bool mayHaveServiceWorkerRegisteredForOrigin(const WebCore::SecurityOriginData&) const final;
64     void startFetch(WebCore::FetchIdentifier, WebCore::ServiceWorkerRegistrationIdentifier, const WebCore::ResourceRequest&, const WebCore::FetchOptions&, const String& referrer);
65     void cancelFetch(WebCore::FetchIdentifier, WebCore::ServiceWorkerRegistrationIdentifier);
66
67     void postMessageToServiceWorkerClient(WebCore::DocumentIdentifier destinationContextIdentifier, WebCore::MessageWithMessagePorts&&, WebCore::ServiceWorkerData&& source, const String& sourceOrigin);
68
69     void connectionToServerLost();
70
71     void syncTerminateWorker(WebCore::ServiceWorkerIdentifier) final;
72
73 private:
74     WebSWClientConnection(IPC::Connection&, PAL::SessionID);
75
76     void scheduleJobInServer(const WebCore::ServiceWorkerJobData&) final;
77     void finishFetchingScriptInServer(const WebCore::ServiceWorkerFetchResult&) final;
78     void postMessageToServiceWorker(WebCore::ServiceWorkerIdentifier destinationIdentifier, WebCore::MessageWithMessagePorts&&, const WebCore::ServiceWorkerOrClientIdentifier& source) final;
79     void registerServiceWorkerClient(const WebCore::SecurityOrigin& topOrigin, const WebCore::ServiceWorkerClientData&, const std::optional<WebCore::ServiceWorkerRegistrationIdentifier>&) final;
80     void unregisterServiceWorkerClient(WebCore::DocumentIdentifier) final;
81
82     void matchRegistration(WebCore::SecurityOriginData&& topOrigin, const URL& clientURL, RegistrationCallback&&) final;
83     void didMatchRegistration(uint64_t matchRequestIdentifier, std::optional<WebCore::ServiceWorkerRegistrationData>&&);
84     void didGetRegistrations(uint64_t matchRequestIdentifier, Vector<WebCore::ServiceWorkerRegistrationData>&&);
85     void whenRegistrationReady(const WebCore::SecurityOrigin& topOrigin, const URL& clientURL, WhenRegistrationReadyCallback&&) final;
86     void registrationReady(uint64_t callbackID, WebCore::ServiceWorkerRegistrationData&&);
87
88     void getRegistrations(WebCore::SecurityOriginData&& topOrigin, const URL& clientURL, GetRegistrationsCallback&&) final;
89
90     void didResolveRegistrationPromise(const WebCore::ServiceWorkerRegistrationKey&) final;
91
92     void scheduleStorageJob(const WebCore::ServiceWorkerJobData&);
93
94     void runOrDelayTaskForImport(WTF::Function<void()>&& task);
95
96     IPC::Connection* messageSenderConnection() final { return m_connection.ptr(); }
97     uint64_t messageSenderDestinationID() final { return m_identifier.toUInt64(); }
98
99     void setSWOriginTableSharedMemory(const SharedMemory::Handle&);
100     void setSWOriginTableIsImported();
101
102     PAL::SessionID m_sessionID;
103     WebCore::SWServerConnectionIdentifier m_identifier;
104
105     Ref<IPC::Connection> m_connection;
106     UniqueRef<WebSWOriginTable> m_swOriginTable;
107
108     uint64_t m_previousCallbackIdentifier { 0 };
109     HashMap<uint64_t, RegistrationCallback> m_ongoingMatchRegistrationTasks;
110     HashMap<uint64_t, GetRegistrationsCallback> m_ongoingGetRegistrationsTasks;
111     HashMap<uint64_t, WhenRegistrationReadyCallback> m_ongoingRegistrationReadyTasks;
112     Deque<WTF::Function<void()>> m_tasksPendingOriginImport;
113
114 }; // class WebSWServerConnection
115
116 } // namespace WebKit
117
118 #endif // ENABLE(SERVICE_WORKER)