58094d628cb08305b975a81192dccb3cbc944e1a
[WebKit-https.git] / Source / WebKit / NetworkProcess / ServiceWorker / WebSWServerToContextConnection.cpp
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 #include "config.h"
27 #include "WebSWServerToContextConnection.h"
28
29 #if ENABLE(SERVICE_WORKER)
30
31 #include "FormDataReference.h"
32 #include "Logging.h"
33 #include "NetworkProcess.h"
34 #include "ServiceWorkerFetchTask.h"
35 #include "ServiceWorkerFetchTaskMessages.h"
36 #include "WebCoreArgumentCoders.h"
37 #include "WebSWContextManagerConnectionMessages.h"
38 #include <WebCore/SWServer.h>
39 #include <WebCore/ServiceWorkerContextData.h>
40
41 namespace WebKit {
42 using namespace WebCore;
43
44 WebSWServerToContextConnection::WebSWServerToContextConnection(NetworkProcess& networkProcess, RegistrableDomain&& registrableDomain, SWServer& server, Ref<IPC::Connection>&& connection)
45     : SWServerToContextConnection(WTFMove(registrableDomain))
46     , m_ipcConnection(WTFMove(connection))
47     , m_networkProcess(networkProcess)
48     , m_server(makeWeakPtr(server))
49 {
50     server.addContextConnection(*this);
51 }
52
53 WebSWServerToContextConnection::~WebSWServerToContextConnection()
54 {
55     connectionClosed();
56     if (m_server && m_server->contextConnectionForRegistrableDomain(registrableDomain()) == this)
57         m_server->removeContextConnection(*this);
58 }
59
60 IPC::Connection* WebSWServerToContextConnection::messageSenderConnection() const
61 {
62     return m_ipcConnection.ptr();
63 }
64
65 uint64_t WebSWServerToContextConnection::messageSenderDestinationID() const
66 {
67     return 0;
68 }
69
70 void WebSWServerToContextConnection::connectionClosed()
71 {
72     auto fetches = WTFMove(m_ongoingFetches);
73     for (auto& fetch : fetches.values())
74         fetch->fail(ResourceError { errorDomainWebKitInternal, 0, { }, "Service Worker context closed"_s });
75 }
76
77 void WebSWServerToContextConnection::postMessageToServiceWorkerClient(const ServiceWorkerClientIdentifier& destinationIdentifier, const MessageWithMessagePorts& message, ServiceWorkerIdentifier sourceIdentifier, const String& sourceOrigin)
78 {
79     if (!m_server)
80         return;
81
82     if (auto* connection = m_server->connection(destinationIdentifier.serverConnectionIdentifier))
83         connection->postMessageToServiceWorkerClient(destinationIdentifier.contextIdentifier, message, sourceIdentifier, sourceOrigin);
84 }
85
86 void WebSWServerToContextConnection::installServiceWorkerContext(const ServiceWorkerContextData& data, const String& userAgent)
87 {
88     send(Messages::WebSWContextManagerConnection::InstallServiceWorker { data, userAgent });
89 }
90
91 void WebSWServerToContextConnection::fireInstallEvent(ServiceWorkerIdentifier serviceWorkerIdentifier)
92 {
93     send(Messages::WebSWContextManagerConnection::FireInstallEvent(serviceWorkerIdentifier));
94 }
95
96 void WebSWServerToContextConnection::fireActivateEvent(ServiceWorkerIdentifier serviceWorkerIdentifier)
97 {
98     send(Messages::WebSWContextManagerConnection::FireActivateEvent(serviceWorkerIdentifier));
99 }
100
101 void WebSWServerToContextConnection::softUpdate(ServiceWorkerIdentifier serviceWorkerIdentifier)
102 {
103     send(Messages::WebSWContextManagerConnection::SoftUpdate(serviceWorkerIdentifier));
104 }
105
106 void WebSWServerToContextConnection::terminateWorker(ServiceWorkerIdentifier serviceWorkerIdentifier)
107 {
108     send(Messages::WebSWContextManagerConnection::TerminateWorker(serviceWorkerIdentifier));
109 }
110
111 void WebSWServerToContextConnection::syncTerminateWorker(ServiceWorkerIdentifier serviceWorkerIdentifier)
112 {
113     sendSync(Messages::WebSWContextManagerConnection::SyncTerminateWorker(serviceWorkerIdentifier), Messages::WebSWContextManagerConnection::SyncTerminateWorker::Reply());
114 }
115
116 void WebSWServerToContextConnection::findClientByIdentifierCompleted(uint64_t requestIdentifier, const Optional<ServiceWorkerClientData>& data, bool hasSecurityError)
117 {
118     send(Messages::WebSWContextManagerConnection::FindClientByIdentifierCompleted { requestIdentifier, data, hasSecurityError });
119 }
120
121 void WebSWServerToContextConnection::matchAllCompleted(uint64_t requestIdentifier, const Vector<ServiceWorkerClientData>& clientsData)
122 {
123     send(Messages::WebSWContextManagerConnection::MatchAllCompleted { requestIdentifier, clientsData });
124 }
125
126 void WebSWServerToContextConnection::claimCompleted(uint64_t requestIdentifier)
127 {
128     send(Messages::WebSWContextManagerConnection::ClaimCompleted { requestIdentifier });
129 }
130
131 void WebSWServerToContextConnection::didFinishSkipWaiting(uint64_t callbackID)
132 {
133     send(Messages::WebSWContextManagerConnection::DidFinishSkipWaiting { callbackID });
134 }
135
136 void WebSWServerToContextConnection::connectionIsNoLongerNeeded()
137 {
138     RELEASE_LOG(ServiceWorker, "Service worker process is no longer needed, terminating it");
139     terminate();
140     connectionClosed();
141 }
142
143 void WebSWServerToContextConnection::setThrottleState(bool isThrottleable)
144 {
145     m_isThrottleable = isThrottleable;
146     send(Messages::WebSWContextManagerConnection::SetThrottleState { isThrottleable });
147 }
148
149 void WebSWServerToContextConnection::terminate()
150 {
151     send(Messages::WebSWContextManagerConnection::TerminateProcess());
152 }
153
154 void WebSWServerToContextConnection::startFetch(PAL::SessionID sessionID, Ref<IPC::Connection>&& contentConnection, WebCore::SWServerConnectionIdentifier serverConnectionIdentifier, FetchIdentifier contentFetchIdentifier, ServiceWorkerIdentifier serviceWorkerIdentifier, const ResourceRequest& request, const FetchOptions& options, const IPC::FormDataReference& data, const String& referrer)
155 {
156     auto fetchIdentifier = FetchIdentifier::generate();
157     
158     m_ongoingFetches.add(fetchIdentifier, ServiceWorkerFetchTask::create(sessionID, WTFMove(contentConnection), serverConnectionIdentifier, contentFetchIdentifier));
159
160     ASSERT(!m_ongoingFetchIdentifiers.contains({serverConnectionIdentifier, contentFetchIdentifier}));
161     m_ongoingFetchIdentifiers.add({serverConnectionIdentifier, contentFetchIdentifier}, fetchIdentifier);
162
163     send(Messages::WebSWContextManagerConnection::StartFetch { serverConnectionIdentifier, serviceWorkerIdentifier, fetchIdentifier, request, options, data, referrer });
164 }
165
166 void WebSWServerToContextConnection::cancelFetch(WebCore::SWServerConnectionIdentifier serverConnectionIdentifier, FetchIdentifier contentFetchIdentifier, ServiceWorkerIdentifier serviceWorkerIdentifier)
167 {
168     auto iterator = m_ongoingFetchIdentifiers.find({ serverConnectionIdentifier, contentFetchIdentifier });
169     if (iterator == m_ongoingFetchIdentifiers.end())
170         return;
171
172     send(Messages::WebSWContextManagerConnection::CancelFetch { serverConnectionIdentifier, serviceWorkerIdentifier, iterator->value });
173
174     m_ongoingFetches.remove(iterator->value);
175     m_ongoingFetchIdentifiers.remove(iterator);
176 }
177
178 void WebSWServerToContextConnection::continueDidReceiveFetchResponse(WebCore::SWServerConnectionIdentifier serverConnectionIdentifier, FetchIdentifier contentFetchIdentifier, ServiceWorkerIdentifier serviceWorkerIdentifier)
179 {
180     auto iterator = m_ongoingFetchIdentifiers.find({ serverConnectionIdentifier, contentFetchIdentifier });
181     if (iterator == m_ongoingFetchIdentifiers.end())
182         return;
183     
184     send(Messages::WebSWContextManagerConnection::ContinueDidReceiveFetchResponse { serverConnectionIdentifier, serviceWorkerIdentifier, iterator->value });
185 }
186
187 void WebSWServerToContextConnection::didReceiveFetchTaskMessage(IPC::Connection& connection, IPC::Decoder& decoder)
188 {
189     auto iterator = m_ongoingFetches.find(makeObjectIdentifier<FetchIdentifierType>(decoder.destinationID()));
190     if (iterator == m_ongoingFetches.end())
191         return;
192     
193     bool shouldRemove = decoder.messageName() == Messages::ServiceWorkerFetchTask::DidFail::name()
194         || decoder.messageName() == Messages::ServiceWorkerFetchTask::DidNotHandle::name()
195         || decoder.messageName() == Messages::ServiceWorkerFetchTask::DidFinish::name()
196         || decoder.messageName() == Messages::ServiceWorkerFetchTask::DidReceiveRedirectResponse::name();
197
198     iterator->value->didReceiveMessage(connection, decoder);
199
200     if (shouldRemove) {
201         ASSERT(m_ongoingFetchIdentifiers.contains(iterator->value->identifier()));
202         m_ongoingFetchIdentifiers.remove(iterator->value->identifier());
203         m_ongoingFetches.remove(iterator);
204     }
205 }
206
207 } // namespace WebKit
208
209 #endif // ENABLE(SERVICE_WORKER)