Service Worker Fetch events should time out.
[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 class WebSWServerConnection;
54
55 class WebSWServerToContextConnection : public WebCore::SWServerToContextConnection, public IPC::MessageSender, public IPC::MessageReceiver {
56 public:
57     WebSWServerToContextConnection(NetworkProcess&, WebCore::RegistrableDomain&&, WebCore::SWServer&, Ref<IPC::Connection>&&);
58     ~WebSWServerToContextConnection();
59
60     IPC::Connection* ipcConnection() const { return m_ipcConnection.ptr(); }
61
62     // IPC::MessageReceiver
63     void didReceiveMessage(IPC::Connection&, IPC::Decoder&) final;
64
65     void terminate();
66
67     void startFetch(PAL::SessionID, WebSWServerConnection&, WebCore::FetchIdentifier, WebCore::ServiceWorkerIdentifier, const WebCore::ResourceRequest&, const WebCore::FetchOptions&, const IPC::FormDataReference&, const String&);
68     void cancelFetch(WebCore::SWServerConnectionIdentifier, WebCore::FetchIdentifier, WebCore::ServiceWorkerIdentifier);
69     void continueDidReceiveFetchResponse(WebCore::SWServerConnectionIdentifier, WebCore::FetchIdentifier, WebCore::ServiceWorkerIdentifier);
70
71     void didReceiveFetchTaskMessage(IPC::Connection&, IPC::Decoder&);
72
73     void setThrottleState(bool isThrottleable);
74     bool isThrottleable() const { return m_isThrottleable; }
75
76     void fetchTaskTimedOut(ServiceWorkerFetchTask&);
77
78 private:
79     // IPC::MessageSender
80     IPC::Connection* messageSenderConnection() const final;
81     uint64_t messageSenderDestinationID() const final;
82
83     void postMessageToServiceWorkerClient(const WebCore::ServiceWorkerClientIdentifier& destinationIdentifier, const WebCore::MessageWithMessagePorts&, WebCore::ServiceWorkerIdentifier sourceIdentifier, const String& sourceOrigin);
84
85     // Messages to the SW host WebProcess
86     void installServiceWorkerContext(const WebCore::ServiceWorkerContextData&, const String& userAgent) final;
87     void fireInstallEvent(WebCore::ServiceWorkerIdentifier) final;
88     void fireActivateEvent(WebCore::ServiceWorkerIdentifier) final;
89     void softUpdate(WebCore::ServiceWorkerIdentifier) final;
90     void terminateWorker(WebCore::ServiceWorkerIdentifier) final;
91     void syncTerminateWorker(WebCore::ServiceWorkerIdentifier) final;
92     void findClientByIdentifierCompleted(uint64_t requestIdentifier, const Optional<WebCore::ServiceWorkerClientData>&, bool hasSecurityError) final;
93     void matchAllCompleted(uint64_t requestIdentifier, const Vector<WebCore::ServiceWorkerClientData>&) final;
94     void claimCompleted(uint64_t requestIdentifier) final;
95     void didFinishSkipWaiting(uint64_t callbackID) final;
96
97     void connectionIsNoLongerNeeded() final;
98
99     void connectionClosed();
100
101     Ref<IPC::Connection> m_ipcConnection;
102     Ref<NetworkProcess> m_networkProcess;
103     WeakPtr<WebCore::SWServer> m_server;
104
105     HashMap<ServiceWorkerFetchTask::Identifier, WebCore::FetchIdentifier> m_ongoingFetchIdentifiers;
106     HashMap<WebCore::FetchIdentifier, Ref<ServiceWorkerFetchTask>> m_ongoingFetches;
107     bool m_isThrottleable { true };
108 }; // class WebSWServerToContextConnection
109
110 } // namespace WebKit
111
112 #endif // ENABLE(SERVICE_WORKER)
113