Support container.getRegistration() / getRegistrations() inside service workers
[WebKit-https.git] / Source / WebKit / WebProcess / Storage / WebSWClientConnection.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 "WebSWClientConnection.h"
28
29 #if ENABLE(SERVICE_WORKER)
30
31 #include "DataReference.h"
32 #include "FormDataReference.h"
33 #include "Logging.h"
34 #include "ServiceWorkerClientFetch.h"
35 #include "StorageToWebProcessConnectionMessages.h"
36 #include "WebCoreArgumentCoders.h"
37 #include "WebSWOriginTable.h"
38 #include "WebSWServerConnectionMessages.h"
39 #include <WebCore/Document.h>
40 #include <WebCore/SerializedScriptValue.h>
41 #include <WebCore/ServiceWorkerClientData.h>
42 #include <WebCore/ServiceWorkerFetchResult.h>
43 #include <WebCore/ServiceWorkerJobData.h>
44 #include <WebCore/ServiceWorkerRegistrationData.h>
45
46 using namespace PAL;
47 using namespace WebCore;
48
49 namespace WebKit {
50
51 WebSWClientConnection::WebSWClientConnection(IPC::Connection& connection, SessionID sessionID)
52     : m_sessionID(sessionID)
53     , m_connection(connection)
54     , m_swOriginTable(makeUniqueRef<WebSWOriginTable>())
55 {
56     bool result = sendSync(Messages::StorageToWebProcessConnection::EstablishSWServerConnection(sessionID), Messages::StorageToWebProcessConnection::EstablishSWServerConnection::Reply(m_identifier));
57
58     ASSERT_UNUSED(result, result);
59 }
60
61 WebSWClientConnection::~WebSWClientConnection()
62 {
63 }
64
65 void WebSWClientConnection::scheduleJobInServer(const ServiceWorkerJobData& jobData)
66 {
67     send(Messages::WebSWServerConnection::ScheduleJobInServer(jobData));
68 }
69
70 void WebSWClientConnection::finishFetchingScriptInServer(const ServiceWorkerFetchResult& result)
71 {
72     send(Messages::WebSWServerConnection::FinishFetchingScriptInServer(result));
73 }
74
75 void WebSWClientConnection::addServiceWorkerRegistrationInServer(ServiceWorkerRegistrationIdentifier identifier)
76 {
77     send(Messages::WebSWServerConnection::AddServiceWorkerRegistrationInServer(identifier));
78 }
79
80 void WebSWClientConnection::removeServiceWorkerRegistrationInServer(ServiceWorkerRegistrationIdentifier identifier)
81 {
82     send(Messages::WebSWServerConnection::RemoveServiceWorkerRegistrationInServer(identifier));
83 }
84
85 void WebSWClientConnection::postMessageToServiceWorker(ServiceWorkerIdentifier destinationIdentifier, Ref<SerializedScriptValue>&& scriptValue, ServiceWorkerClientIdentifier sourceIdentifier, ServiceWorkerClientData&& source)
86 {
87     send(Messages::WebSWServerConnection::PostMessageToServiceWorkerFromClient(destinationIdentifier, IPC::DataReference { scriptValue->data() }, sourceIdentifier, WTFMove(source)));
88 }
89
90 void WebSWClientConnection::postMessageToServiceWorker(ServiceWorkerIdentifier destinationIdentifier, Ref<SerializedScriptValue>&& scriptValue, ServiceWorkerIdentifier sourceWorkerIdentifier)
91 {
92     send(Messages::WebSWServerConnection::PostMessageToServiceWorkerFromServiceWorker(destinationIdentifier, IPC::DataReference { scriptValue->data() }, sourceWorkerIdentifier));
93 }
94
95 void WebSWClientConnection::serviceWorkerStartedControllingClient(ServiceWorkerIdentifier serviceWorkerIdentifier, ServiceWorkerRegistrationIdentifier registrationIdentifier, DocumentIdentifier contextIdentifier)
96 {
97     send(Messages::WebSWServerConnection::ServiceWorkerStartedControllingClient(serviceWorkerIdentifier, registrationIdentifier, contextIdentifier));
98 }
99
100 void WebSWClientConnection::serviceWorkerStoppedControllingClient(ServiceWorkerIdentifier serviceWorkerIdentifier, ServiceWorkerRegistrationIdentifier registrationIdentifier, DocumentIdentifier contextIdentifier)
101 {
102     send(Messages::WebSWServerConnection::ServiceWorkerStoppedControllingClient(serviceWorkerIdentifier, registrationIdentifier, contextIdentifier));
103 }
104
105 void WebSWClientConnection::registerServiceWorkerClient(const SecurityOrigin& topOrigin, WebCore::DocumentIdentifier contextIdentifier, const WebCore::ServiceWorkerClientData& data)
106 {
107     send(Messages::WebSWServerConnection::RegisterServiceWorkerClient { SecurityOriginData::fromSecurityOrigin(topOrigin), contextIdentifier, data });
108 }
109
110 void WebSWClientConnection::unregisterServiceWorkerClient(WebCore::DocumentIdentifier contextIdentifier)
111 {
112     send(Messages::WebSWServerConnection::UnregisterServiceWorkerClient { contextIdentifier });
113 }
114
115 void WebSWClientConnection::didResolveRegistrationPromise(const ServiceWorkerRegistrationKey& key)
116 {
117     send(Messages::WebSWServerConnection::DidResolveRegistrationPromise(key));
118 }
119
120 bool WebSWClientConnection::mayHaveServiceWorkerRegisteredForOrigin(const SecurityOrigin& origin) const
121 {
122     if (!m_swOriginTable->isInitialized())
123         return true;
124
125     return m_swOriginTable->contains(origin);
126 }
127
128 void WebSWClientConnection::setSWOriginTableSharedMemory(const SharedMemory::Handle& handle)
129 {
130     m_swOriginTable->setSharedMemory(handle);
131 }
132
133 void WebSWClientConnection::initializeSWOriginTableAsEmpty()
134 {
135     m_swOriginTable->initializeAsEmpty();
136 }
137
138 void WebSWClientConnection::didMatchRegistration(uint64_t matchingRequest, std::optional<ServiceWorkerRegistrationData>&& result)
139 {
140     ASSERT(isMainThread());
141
142     if (auto completionHandler = m_ongoingMatchRegistrationTasks.take(matchingRequest))
143         completionHandler(WTFMove(result));
144 }
145
146 void WebSWClientConnection::didGetRegistrations(uint64_t matchingRequest, Vector<ServiceWorkerRegistrationData>&& registrations)
147 {
148     ASSERT(isMainThread());
149
150     if (auto completionHandler = m_ongoingGetRegistrationsTasks.take(matchingRequest))
151         completionHandler(WTFMove(registrations));
152 }
153
154 void WebSWClientConnection::matchRegistration(const SecurityOrigin& topOrigin, const URL& clientURL, RegistrationCallback&& callback)
155 {
156     ASSERT(isMainThread());
157
158     if (!mayHaveServiceWorkerRegisteredForOrigin(topOrigin)) {
159         callback(std::nullopt);
160         return;
161     }
162
163     uint64_t requestIdentifier = ++m_previousMatchRegistrationTaskIdentifier;
164     m_ongoingMatchRegistrationTasks.add(requestIdentifier, WTFMove(callback));
165     send(Messages::WebSWServerConnection::MatchRegistration(requestIdentifier, SecurityOriginData::fromSecurityOrigin(topOrigin), clientURL));
166 }
167
168 void WebSWClientConnection::getRegistrations(const SecurityOrigin& topOrigin, const URL& clientURL, GetRegistrationsCallback&& callback)
169 {
170     ASSERT(isMainThread());
171
172     if (!mayHaveServiceWorkerRegisteredForOrigin(topOrigin)) {
173         callback({ });
174         return;
175     }
176
177     uint64_t requestIdentifier = ++m_previousGetRegistrationsTaskIdentifier;
178     m_ongoingGetRegistrationsTasks.add(requestIdentifier, WTFMove(callback));
179     send(Messages::WebSWServerConnection::GetRegistrations(requestIdentifier, SecurityOriginData::fromSecurityOrigin(topOrigin), clientURL));
180 }
181
182 void WebSWClientConnection::startFetch(const ResourceLoader& loader, uint64_t identifier)
183 {
184     ASSERT(loader.options().serviceWorkersMode != ServiceWorkersMode::None && loader.options().serviceWorkerIdentifier);
185     send(Messages::WebSWServerConnection::StartFetch { identifier, loader.options().serviceWorkerIdentifier, loader.request(), loader.options(), IPC::FormDataReference { loader.request().httpBody() } });
186 }
187
188 void WebSWClientConnection::postMessageToServiceWorkerClient(DocumentIdentifier destinationContextIdentifier, const IPC::DataReference& message, ServiceWorkerData&& source, const String& sourceOrigin)
189 {
190     SWClientConnection::postMessageToServiceWorkerClient(destinationContextIdentifier, SerializedScriptValue::adopt(message.vector()), WTFMove(source), sourceOrigin);
191 }
192
193 void WebSWClientConnection::connectionToServerLost()
194 {
195     auto registrationTasks = WTFMove(m_ongoingMatchRegistrationTasks);
196     for (auto& callback : registrationTasks.values())
197         callback(std::nullopt);
198
199     auto getRegistrationTasks = WTFMove(m_ongoingGetRegistrationsTasks);
200     for (auto& callback : getRegistrationTasks.values())
201         callback({ });
202
203     clearPendingJobs();
204 }
205
206 void WebSWClientConnection::syncTerminateWorker(ServiceWorkerIdentifier identifier)
207 {
208     sendSync(Messages::WebSWServerConnection::SyncTerminateWorker(identifier), Messages::WebSWServerConnection::SyncTerminateWorker::Reply());
209 }
210
211 } // namespace WebKit
212
213 #endif // ENABLE(SERVICE_WORKER)