9a56e2fb3dba6e12a99395c19b43791ef00c34d7
[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 "Logging.h"
33 #include "ServiceWorkerClientFetch.h"
34 #include "StorageToWebProcessConnectionMessages.h"
35 #include "WebCoreArgumentCoders.h"
36 #include "WebSWOriginTable.h"
37 #include "WebSWServerConnectionMessages.h"
38 #include <WebCore/Document.h>
39 #include <WebCore/SerializedScriptValue.h>
40 #include <WebCore/ServiceWorkerFetchResult.h>
41 #include <WebCore/ServiceWorkerJobData.h>
42 #include <WebCore/ServiceWorkerRegistrationData.h>
43
44 using namespace PAL;
45 using namespace WebCore;
46
47 namespace WebKit {
48
49 WebSWClientConnection::WebSWClientConnection(IPC::Connection& connection, SessionID sessionID)
50     : m_sessionID(sessionID)
51     , m_connection(connection)
52     , m_swOriginTable(makeUniqueRef<WebSWOriginTable>())
53 {
54     bool result = sendSync(Messages::StorageToWebProcessConnection::EstablishSWServerConnection(sessionID), Messages::StorageToWebProcessConnection::EstablishSWServerConnection::Reply(m_identifier));
55
56     ASSERT_UNUSED(result, result);
57 }
58
59 WebSWClientConnection::~WebSWClientConnection()
60 {
61 }
62
63 void WebSWClientConnection::scheduleJobInServer(const ServiceWorkerJobData& jobData)
64 {
65     send(Messages::WebSWServerConnection::ScheduleJobInServer(jobData));
66 }
67
68 void WebSWClientConnection::finishFetchingScriptInServer(const ServiceWorkerFetchResult& result)
69 {
70     send(Messages::WebSWServerConnection::FinishFetchingScriptInServer(result));
71 }
72
73 void WebSWClientConnection::addServiceWorkerRegistrationInServer(const ServiceWorkerRegistrationKey& key, uint64_t registrationIdentifier)
74 {
75     send(Messages::WebSWServerConnection::AddServiceWorkerRegistrationInServer(key, registrationIdentifier));
76 }
77
78 void WebSWClientConnection::removeServiceWorkerRegistrationInServer(const ServiceWorkerRegistrationKey& key, uint64_t registrationIdentifier)
79 {
80     send(Messages::WebSWServerConnection::RemoveServiceWorkerRegistrationInServer(key, registrationIdentifier));
81 }
82
83 void WebSWClientConnection::postMessageToServiceWorkerGlobalScope(ServiceWorkerIdentifier destinationIdentifier, Ref<SerializedScriptValue>&& scriptValue, ScriptExecutionContext& source)
84 {
85     // FIXME: Add support for posting messages from workers.
86     if (!is<Document>(source))
87         return;
88
89     send(Messages::WebSWServerConnection::PostMessageToServiceWorkerGlobalScope(destinationIdentifier, IPC::DataReference { scriptValue->data() }, downcast<Document>(source).identifier(), source.origin()));
90 }
91
92 bool WebSWClientConnection::hasServiceWorkerRegisteredForOrigin(const SecurityOrigin& origin) const
93 {
94     return m_swOriginTable->contains(origin);
95 }
96
97 void WebSWClientConnection::setSWOriginTableSharedMemory(const SharedMemory::Handle& handle)
98 {
99     m_swOriginTable->setSharedMemory(handle);
100 }
101
102 void WebSWClientConnection::didMatchRegistration(uint64_t matchingRequest, std::optional<ServiceWorkerRegistrationData>&& result)
103 {
104     if (auto completionHandler = m_ongoingMatchRegistrationTasks.take(matchingRequest))
105         completionHandler(WTFMove(result));
106 }
107
108 void WebSWClientConnection::matchRegistration(const SecurityOrigin& topOrigin, const URL& clientURL, RegistrationCallback&& callback)
109 {
110     uint64_t requestIdentifier = ++m_previousMatchRegistrationTaskIdentifier;
111     m_ongoingMatchRegistrationTasks.add(requestIdentifier, WTFMove(callback));
112     send(Messages::WebSWServerConnection::MatchRegistration(requestIdentifier, SecurityOriginData::fromSecurityOrigin(topOrigin), clientURL));
113 }
114
115 Ref<ServiceWorkerClientFetch> WebSWClientConnection::startFetch(WebServiceWorkerProvider& provider, Ref<WebCore::ResourceLoader>&& loader, uint64_t identifier, ServiceWorkerClientFetch::Callback&& callback)
116 {
117     ASSERT(loader->options().serviceWorkersMode == ServiceWorkersMode::All);
118     // FIXME: Decide whether to assert for loader->options().serviceWorkerIdentifier once we have a story for navigation loads.
119
120     send(Messages::WebSWServerConnection::StartFetch(identifier, loader->options().serviceWorkerIdentifier, loader->originalRequest(), loader->options()));
121     return ServiceWorkerClientFetch::create(provider, WTFMove(loader), identifier, m_connection.get(), WTFMove(callback));
122 }
123
124 void WebSWClientConnection::postMessageToServiceWorkerClient(uint64_t destinationScriptExecutionContextIdentifier, const IPC::DataReference& message, ServiceWorkerIdentifier sourceIdentifier, const String& sourceOrigin)
125 {
126     SWClientConnection::postMessageToServiceWorkerClient(destinationScriptExecutionContextIdentifier, SerializedScriptValue::adopt(message.vector()), sourceIdentifier, sourceOrigin);
127 }
128
129 } // namespace WebKit
130
131 #endif // ENABLE(SERVICE_WORKER)