a3c64ed1278df67a99e00597a4420ea2176492ad
[WebKit-https.git] / Source / WebKit / NetworkProcess / ServiceWorker / WebSWServerToContextConnection.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 "ServiceWorkerFetchTask.h"
33 #include <WebCore/SWServerToContextConnection.h>
34 #include <wtf/WeakPtr.h>
35
36 namespace WebCore {
37 struct FetchOptions;
38 struct MessageWithMessagePorts;
39 class ResourceRequest;
40 }
41
42 namespace IPC {
43 class FormDataReference;
44 }
45
46 namespace PAL {
47 class SessionID;
48 }
49
50 namespace WebKit {
51
52 class NetworkProcess;
53
54 class WebSWServerToContextConnection : public WebCore::SWServerToContextConnection, public IPC::MessageSender, public IPC::MessageReceiver {
55 public:
56     WebSWServerToContextConnection(NetworkProcess&, WebCore::RegistrableDomain&&, WebCore::SWServer&, Ref<IPC::Connection>&&);
57     ~WebSWServerToContextConnection();
58
59     IPC::Connection* ipcConnection() const { return m_ipcConnection.ptr(); }
60
61     // IPC::MessageReceiver
62     void didReceiveMessage(IPC::Connection&, IPC::Decoder&) final;
63
64     void terminate();
65
66     void startFetch(PAL::SessionID, Ref<IPC::Connection>&&, WebCore::SWServerConnectionIdentifier, WebCore::FetchIdentifier, WebCore::ServiceWorkerIdentifier, const WebCore::ResourceRequest&, const WebCore::FetchOptions&, const IPC::FormDataReference&, const String&);
67     void cancelFetch(WebCore::SWServerConnectionIdentifier, WebCore::FetchIdentifier, WebCore::ServiceWorkerIdentifier);
68     void continueDidReceiveFetchResponse(WebCore::SWServerConnectionIdentifier, WebCore::FetchIdentifier, WebCore::ServiceWorkerIdentifier);
69
70     void didReceiveFetchTaskMessage(IPC::Connection&, IPC::Decoder&);
71
72     void setThrottleState(bool isThrottleable);
73     bool isThrottleable() const { return m_isThrottleable; }
74
75 private:
76     // IPC::MessageSender
77     IPC::Connection* messageSenderConnection() const final;
78     uint64_t messageSenderDestinationID() const final;
79
80     void postMessageToServiceWorkerClient(const WebCore::ServiceWorkerClientIdentifier& destinationIdentifier, const WebCore::MessageWithMessagePorts&, WebCore::ServiceWorkerIdentifier sourceIdentifier, const String& sourceOrigin);
81
82     // Messages to the SW host WebProcess
83     void installServiceWorkerContext(const WebCore::ServiceWorkerContextData&, const String& userAgent) final;
84     void fireInstallEvent(WebCore::ServiceWorkerIdentifier) final;
85     void fireActivateEvent(WebCore::ServiceWorkerIdentifier) final;
86     void softUpdate(WebCore::ServiceWorkerIdentifier) final;
87     void terminateWorker(WebCore::ServiceWorkerIdentifier) final;
88     void syncTerminateWorker(WebCore::ServiceWorkerIdentifier) final;
89     void findClientByIdentifierCompleted(uint64_t requestIdentifier, const Optional<WebCore::ServiceWorkerClientData>&, bool hasSecurityError) final;
90     void matchAllCompleted(uint64_t requestIdentifier, const Vector<WebCore::ServiceWorkerClientData>&) final;
91     void claimCompleted(uint64_t requestIdentifier) final;
92     void didFinishSkipWaiting(uint64_t callbackID) final;
93
94     void connectionIsNoLongerNeeded() final;
95
96     void connectionClosed();
97
98     Ref<IPC::Connection> m_ipcConnection;
99     Ref<NetworkProcess> m_networkProcess;
100     WeakPtr<WebCore::SWServer> m_server;
101
102     HashMap<ServiceWorkerFetchTask::Identifier, WebCore::FetchIdentifier> m_ongoingFetchIdentifiers;
103     HashMap<WebCore::FetchIdentifier, Ref<ServiceWorkerFetchTask>> m_ongoingFetches;
104     bool m_isThrottleable { true };
105 }; // class WebSWServerToContextConnection
106
107 } // namespace WebKit
108
109 #endif // ENABLE(SERVICE_WORKER)
110