Bring back SWContextManager abstraction in WebCore
[WebKit-https.git] / Source / WebKit / StorageProcess / ServiceWorker / WebSWServerConnection.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 "WebSWServerConnection.h"
28
29 #if ENABLE(SERVICE_WORKER)
30
31 #include "DataReference.h"
32 #include "Logging.h"
33 #include "ServiceWorkerClientFetchMessages.h"
34 #include "StorageProcess.h"
35 #include "StorageToWebProcessConnectionMessages.h"
36 #include "WebCoreArgumentCoders.h"
37 #include "WebProcess.h"
38 #include "WebProcessMessages.h"
39 #include "WebSWClientConnectionMessages.h"
40 #include "WebSWContextManagerConnectionMessages.h"
41 #include "WebSWOriginStore.h"
42 #include "WebSWServerConnectionMessages.h"
43 #include "WebToStorageProcessConnection.h"
44 #include <WebCore/ExceptionData.h>
45 #include <WebCore/NotImplemented.h>
46 #include <WebCore/SecurityOrigin.h>
47 #include <WebCore/ServiceWorkerContextData.h>
48 #include <WebCore/ServiceWorkerJobData.h>
49 #include <WebCore/ServiceWorkerRegistrationData.h>
50 #include <wtf/MainThread.h>
51
52 using namespace PAL;
53 using namespace WebCore;
54
55 namespace WebKit {
56
57 WebSWServerConnection::WebSWServerConnection(SWServer& server, IPC::Connection& connection, uint64_t connectionIdentifier, SessionID sessionID)
58     : SWServer::Connection(server, connectionIdentifier)
59     , m_sessionID(sessionID)
60     , m_contentConnection(connection)
61 {
62 }
63
64 WebSWServerConnection::~WebSWServerConnection()
65 {
66 }
67
68 void WebSWServerConnection::disconnectedFromWebProcess()
69 {
70     notImplemented();
71 }
72
73 void WebSWServerConnection::rejectJobInClient(uint64_t jobIdentifier, const ExceptionData& exceptionData)
74 {
75     send(Messages::WebSWClientConnection::JobRejectedInServer(jobIdentifier, exceptionData));
76 }
77
78 void WebSWServerConnection::resolveRegistrationJobInClient(uint64_t jobIdentifier, const ServiceWorkerRegistrationData& registrationData)
79 {
80     auto origin = registrationData.key.topOrigin.securityOrigin();
81     StorageProcess::singleton().ensureSWOriginStoreForSession(m_sessionID).add(origin);
82     send(Messages::WebSWClientConnection::RegistrationJobResolvedInServer(jobIdentifier, registrationData));
83 }
84
85 void WebSWServerConnection::resolveUnregistrationJobInClient(uint64_t jobIdentifier, const ServiceWorkerRegistrationKey& registrationKey, bool unregistrationResult)
86 {
87     auto origin = registrationKey.topOrigin.securityOrigin();
88     if (auto* store = StorageProcess::singleton().swOriginStoreForSession(m_sessionID))
89         store->remove(origin);
90     send(Messages::WebSWClientConnection::UnregistrationJobResolvedInServer(jobIdentifier, unregistrationResult));
91 }
92
93 void WebSWServerConnection::startScriptFetchInClient(uint64_t jobIdentifier)
94 {
95     send(Messages::WebSWClientConnection::StartScriptFetchForServer(jobIdentifier));
96 }
97
98 void WebSWServerConnection::startServiceWorkerContext(const ServiceWorkerContextData& data)
99 {
100     if (sendToContextProcess(Messages::WebSWContextManagerConnection::StartServiceWorker(identifier(), data)))
101         return;
102
103     m_pendingContextDatas.append(data);
104 }
105
106 void WebSWServerConnection::startFetch(uint64_t fetchIdentifier, uint64_t serviceWorkerIdentifier, const ResourceRequest& request, const FetchOptions& options)
107 {
108     sendToContextProcess(Messages::WebSWContextManagerConnection::StartFetch(identifier(), fetchIdentifier, serviceWorkerIdentifier, request, options));
109 }
110
111 void WebSWServerConnection::postMessageToServiceWorkerGlobalScope(uint64_t serviceWorkerIdentifier, const IPC::DataReference& message, const String& sourceOrigin)
112 {
113     sendToContextProcess(Messages::WebSWContextManagerConnection::PostMessageToServiceWorkerGlobalScope(serviceWorkerIdentifier, message, sourceOrigin));
114 }
115
116 void WebSWServerConnection::didReceiveFetchResponse(uint64_t fetchIdentifier, const ResourceResponse& response)
117 {
118     m_contentConnection->send(Messages::ServiceWorkerClientFetch::DidReceiveResponse { response }, fetchIdentifier);
119 }
120
121 void WebSWServerConnection::didReceiveFetchData(uint64_t fetchIdentifier, const IPC::DataReference& data, int64_t encodedDataLength)
122 {
123     m_contentConnection->send(Messages::ServiceWorkerClientFetch::DidReceiveData { data, encodedDataLength }, fetchIdentifier);
124 }
125
126 void WebSWServerConnection::didFinishFetch(uint64_t fetchIdentifier)
127 {
128     m_contentConnection->send(Messages::ServiceWorkerClientFetch::DidFinish { }, fetchIdentifier);
129 }
130
131 void WebSWServerConnection::didFailFetch(uint64_t fetchIdentifier)
132 {
133     m_contentConnection->send(Messages::ServiceWorkerClientFetch::DidFail { }, fetchIdentifier);
134 }
135
136 void WebSWServerConnection::didNotHandleFetch(uint64_t fetchIdentifier)
137 {
138     m_contentConnection->send(Messages::ServiceWorkerClientFetch::DidNotHandle { }, fetchIdentifier);
139 }
140
141 template<typename U> bool WebSWServerConnection::sendToContextProcess(U&& message)
142 {
143     if (!m_contextConnection)
144         return false;
145
146     return m_contextConnection->send<U>(WTFMove(message), 0);
147 }
148
149 void WebSWServerConnection::setContextConnection(IPC::Connection* connection)
150 {
151     m_contextConnection = connection;
152
153     // We can now start any pending service worker contexts.
154     for (auto& pendingContextData : m_pendingContextDatas)
155         startServiceWorkerContext(pendingContextData);
156     
157     m_pendingContextDatas.clear();
158 }
159     
160 } // namespace WebKit
161
162 #endif // ENABLE(SERVICE_WORKER)