Move URL from WebCore to WTF
[WebKit-https.git] / Source / WebKit / NetworkProcess / ServiceWorker / WebSWServerConnection.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 "MessageReceiver.h"
31 #include "MessageSender.h"
32 #include <WebCore/FetchIdentifier.h>
33 #include <WebCore/SWServer.h>
34 #include <pal/SessionID.h>
35 #include <wtf/HashMap.h>
36
37 namespace IPC {
38 class FormDataReference;
39 }
40
41 namespace WebCore {
42 class ServiceWorkerRegistrationKey;
43 struct ClientOrigin;
44 struct ExceptionData;
45 struct MessageWithMessagePorts;
46 struct ServiceWorkerClientData;
47 }
48
49 namespace WebKit {
50
51 class WebSWServerConnection : public WebCore::SWServer::Connection, public IPC::MessageSender, public IPC::MessageReceiver {
52 public:
53     WebSWServerConnection(WebCore::SWServer&, IPC::Connection&, PAL::SessionID);
54     WebSWServerConnection(const WebSWServerConnection&) = delete;
55     ~WebSWServerConnection() final;
56
57     IPC::Connection& ipcConnection() const { return m_contentConnection.get(); }
58
59     void didReceiveMessage(IPC::Connection&, IPC::Decoder&) final;
60     void didReceiveSyncMessage(IPC::Connection&, IPC::Decoder&, std::unique_ptr<IPC::Encoder>&);
61
62     PAL::SessionID sessionID() const { return m_sessionID; }
63
64     void didReceiveFetchResponse(WebCore::FetchIdentifier, const WebCore::ResourceResponse&);
65     void didReceiveFetchData(WebCore::FetchIdentifier, const IPC::DataReference&, int64_t encodedDataLength);
66     void didReceiveFetchFormData(WebCore::FetchIdentifier, const IPC::FormDataReference&);
67     void didFinishFetch(WebCore::FetchIdentifier);
68     void didFailFetch(WebCore::FetchIdentifier, const WebCore::ResourceError&);
69     void didNotHandleFetch(WebCore::FetchIdentifier);
70
71     void postMessageToServiceWorkerClient(WebCore::DocumentIdentifier destinationContextIdentifier, WebCore::MessageWithMessagePorts&&, WebCore::ServiceWorkerIdentifier sourceServiceWorkerIdentifier, const String& sourceOrigin);
72     void postMessageToServiceWorker(WebCore::ServiceWorkerIdentifier destination, WebCore::MessageWithMessagePorts&&, const WebCore::ServiceWorkerOrClientIdentifier& source);
73
74 private:
75     // Implement SWServer::Connection (Messages to the client WebProcess)
76     void rejectJobInClient(WebCore::ServiceWorkerJobIdentifier, const WebCore::ExceptionData&) final;
77     void resolveRegistrationJobInClient(WebCore::ServiceWorkerJobIdentifier, const WebCore::ServiceWorkerRegistrationData&, WebCore::ShouldNotifyWhenResolved) final;
78     void resolveUnregistrationJobInClient(WebCore::ServiceWorkerJobIdentifier, const WebCore::ServiceWorkerRegistrationKey&, bool unregistrationResult) final;
79     void startScriptFetchInClient(WebCore::ServiceWorkerJobIdentifier, const WebCore::ServiceWorkerRegistrationKey&, WebCore::FetchOptions::Cache) final;
80     void updateRegistrationStateInClient(WebCore::ServiceWorkerRegistrationIdentifier, WebCore::ServiceWorkerRegistrationState, const std::optional<WebCore::ServiceWorkerData>&) final;
81     void updateWorkerStateInClient(WebCore::ServiceWorkerIdentifier, WebCore::ServiceWorkerState) final;
82     void fireUpdateFoundEvent(WebCore::ServiceWorkerRegistrationIdentifier) final;
83     void setRegistrationLastUpdateTime(WebCore::ServiceWorkerRegistrationIdentifier, WallTime) final;
84     void setRegistrationUpdateViaCache(WebCore::ServiceWorkerRegistrationIdentifier, WebCore::ServiceWorkerUpdateViaCache) final;
85     void notifyClientsOfControllerChange(const HashSet<WebCore::DocumentIdentifier>& contextIdentifiers, const WebCore::ServiceWorkerData& newController);
86     void registrationReady(uint64_t registrationReadyRequestIdentifier, WebCore::ServiceWorkerRegistrationData&&) final;
87
88     void scheduleJobInServer(WebCore::ServiceWorkerJobData&&);
89
90     void startFetch(WebCore::ServiceWorkerRegistrationIdentifier, WebCore::FetchIdentifier, WebCore::ResourceRequest&&, WebCore::FetchOptions&&, IPC::FormDataReference&&, String&& referrer);
91     void cancelFetch(WebCore::ServiceWorkerRegistrationIdentifier, WebCore::FetchIdentifier);
92
93     void matchRegistration(uint64_t registrationMatchRequestIdentifier, const WebCore::SecurityOriginData& topOrigin, const URL& clientURL);
94     void getRegistrations(uint64_t registrationMatchRequestIdentifier, const WebCore::SecurityOriginData& topOrigin, const URL& clientURL);
95
96     void registerServiceWorkerClient(WebCore::SecurityOriginData&& topOrigin, WebCore::ServiceWorkerClientData&&, const std::optional<WebCore::ServiceWorkerRegistrationIdentifier>&);
97     void unregisterServiceWorkerClient(const WebCore::ServiceWorkerClientIdentifier&);
98
99     IPC::Connection* messageSenderConnection() final { return m_contentConnection.ptr(); }
100     uint64_t messageSenderDestinationID() final { return identifier().toUInt64(); }
101     
102     template<typename U> static void sendToContextProcess(WebCore::SWServerToContextConnection&, U&& message);
103
104     PAL::SessionID m_sessionID;
105     Ref<IPC::Connection> m_contentConnection;
106     HashMap<WebCore::ServiceWorkerClientIdentifier, WebCore::ClientOrigin> m_clientOrigins;
107 };
108
109 } // namespace WebKit
110
111 #endif // ENABLE(SERVICE_WORKER)