ASSERTION FAILED: !m_processes[i] || *m_processes[i] == process in MessagePortChannel...
[WebKit-https.git] / Source / WebKit / StorageProcess / 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/SWServer.h>
33 #include <pal/SessionID.h>
34 #include <wtf/HashMap.h>
35
36 namespace IPC {
37 class FormDataReference;
38 }
39
40 namespace WebCore {
41 class ServiceWorkerRegistrationKey;
42 struct ClientOrigin;
43 struct ExceptionData;
44 struct MessageWithMessagePorts;
45 struct ServiceWorkerClientData;
46 }
47
48 namespace WebKit {
49
50 class WebSWServerConnection : public WebCore::SWServer::Connection, public IPC::MessageSender, public IPC::MessageReceiver {
51 public:
52     WebSWServerConnection(WebCore::SWServer&, IPC::Connection&, PAL::SessionID);
53     WebSWServerConnection(const WebSWServerConnection&) = delete;
54     ~WebSWServerConnection() final;
55
56     IPC::Connection& ipcConnection() const { return m_contentConnection.get(); }
57
58     void disconnectedFromWebProcess();
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(uint64_t fetchIdentifier, const WebCore::ResourceResponse&);
65     void didReceiveFetchData(uint64_t fetchIdentifier, const IPC::DataReference&, int64_t encodedDataLength);
66     void didReceiveFetchFormData(uint64_t fetchIdentifier, const IPC::FormDataReference&);
67     void didFinishFetch(uint64_t fetchIdentifier);
68     void didFailFetch(uint64_t fetchIdentifier);
69     void didNotHandleFetch(uint64_t 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(uint64_t fetchIdentifier, WebCore::ServiceWorkerRegistrationIdentifier, WebCore::ResourceRequest&&, WebCore::FetchOptions&&, IPC::FormDataReference&&, String&& referrer);
91
92     void matchRegistration(uint64_t registrationMatchRequestIdentifier, const WebCore::SecurityOriginData& topOrigin, const WebCore::URL& clientURL);
93     void getRegistrations(uint64_t registrationMatchRequestIdentifier, const WebCore::SecurityOriginData& topOrigin, const WebCore::URL& clientURL);
94
95     void registerServiceWorkerClient(WebCore::SecurityOriginData&& topOrigin, WebCore::ServiceWorkerClientData&&, const std::optional<WebCore::ServiceWorkerIdentifier>&);
96     void unregisterServiceWorkerClient(const WebCore::ServiceWorkerClientIdentifier&);
97
98     IPC::Connection* messageSenderConnection() final { return m_contentConnection.ptr(); }
99     uint64_t messageSenderDestinationID() final { return identifier().toUInt64(); }
100     
101     template<typename U> void sendToContextProcess(U&& message);
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)