REGRESSION (r225789): API tests WKProcessPool.InitialWarmedProcessUsed and WebKit...
[WebKit-https.git] / Source / WebKit / StorageProcess / StorageToWebProcessConnection.cpp
1 /*
2  * Copyright (C) 2013 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 "StorageToWebProcessConnection.h"
28
29 #include "Logging.h"
30 #include "StorageProcess.h"
31 #include "StorageProcessMessages.h"
32 #include "StorageToWebProcessConnectionMessages.h"
33 #include "WebIDBConnectionToClient.h"
34 #include "WebIDBConnectionToClientMessages.h"
35 #include "WebSWServerConnection.h"
36 #include "WebSWServerConnectionMessages.h"
37 #include <wtf/RunLoop.h>
38
39 #if ENABLE(SERVICE_WORKER)
40 #include "WebSWServerToContextConnection.h"
41 #include "WebSWServerToContextConnectionMessages.h"
42 #endif
43
44 using namespace PAL;
45 using namespace WebCore;
46
47 namespace WebKit {
48
49 Ref<StorageToWebProcessConnection> StorageToWebProcessConnection::create(IPC::Connection::Identifier connectionIdentifier)
50 {
51     return adoptRef(*new StorageToWebProcessConnection(connectionIdentifier));
52 }
53
54 StorageToWebProcessConnection::StorageToWebProcessConnection(IPC::Connection::Identifier connectionIdentifier)
55     : m_connection(IPC::Connection::createServerConnection(connectionIdentifier, *this))
56 {
57     m_connection->setOnlySendMessagesAsDispatchWhenWaitingForSyncReplyWhenProcessingSuchAMessage(true);
58     m_connection->open();
59 }
60
61 StorageToWebProcessConnection::~StorageToWebProcessConnection()
62 {
63     m_connection->invalidate();
64 }
65
66 void StorageToWebProcessConnection::didReceiveMessage(IPC::Connection& connection, IPC::Decoder& decoder)
67 {
68     if (decoder.messageReceiverName() == Messages::StorageToWebProcessConnection::messageReceiverName()) {
69         didReceiveStorageToWebProcessConnectionMessage(connection, decoder);
70         return;
71     }
72
73     if (decoder.messageReceiverName() == Messages::StorageProcess::messageReceiverName()) {
74         StorageProcess::singleton().didReceiveStorageProcessMessage(connection, decoder);
75         return;
76     }
77
78 #if ENABLE(INDEXED_DATABASE)
79     if (decoder.messageReceiverName() == Messages::WebIDBConnectionToClient::messageReceiverName()) {
80         auto iterator = m_webIDBConnections.find(decoder.destinationID());
81         if (iterator != m_webIDBConnections.end())
82             iterator->value->didReceiveMessage(connection, decoder);
83         return;
84     }
85 #endif
86
87 #if ENABLE(SERVICE_WORKER)
88     if (decoder.messageReceiverName() == Messages::WebSWServerConnection::messageReceiverName()) {
89         auto iterator = m_swConnections.find(makeObjectIdentifier<SWServerConnectionIdentifierType>(decoder.destinationID()));
90         if (iterator != m_swConnections.end())
91             iterator->value->didReceiveMessage(connection, decoder);
92         return;
93     }
94
95     if (decoder.messageReceiverName() == Messages::WebSWServerToContextConnection::messageReceiverName()) {
96         StorageProcess::singleton().globalServerToContextConnection()->didReceiveMessage(connection, decoder);
97         return;
98     }
99 #endif
100
101     ASSERT_NOT_REACHED();
102 }
103
104 void StorageToWebProcessConnection::didReceiveSyncMessage(IPC::Connection& connection, IPC::Decoder& decoder, std::unique_ptr<IPC::Encoder>& replyEncoder)
105 {
106     if (decoder.messageReceiverName() == Messages::StorageToWebProcessConnection::messageReceiverName()) {
107         didReceiveSyncStorageToWebProcessConnectionMessage(connection, decoder, replyEncoder);
108         return;
109     }
110
111 #if ENABLE(SERVICE_WORKER)
112     if (decoder.messageReceiverName() == Messages::WebSWServerConnection::messageReceiverName()) {
113         auto iterator = m_swConnections.find(makeObjectIdentifier<SWServerConnectionIdentifierType>(decoder.destinationID()));
114         if (iterator != m_swConnections.end())
115             iterator->value->didReceiveSyncMessage(connection, decoder, replyEncoder);
116         return;
117     }
118 #endif
119
120     ASSERT_NOT_REACHED();
121 }
122
123 void StorageToWebProcessConnection::didClose(IPC::Connection& connection)
124 {
125     UNUSED_PARAM(connection);
126
127 #if ENABLE(SERVICE_WORKER)
128     if (StorageProcess::singleton().globalServerToContextConnection() && StorageProcess::singleton().globalServerToContextConnection()->ipcConnection() == &connection) {
129         // Service Worker process exited.
130         StorageProcess::singleton().connectionToContextProcessWasClosed();
131     }
132 #endif
133
134 #if ENABLE(INDEXED_DATABASE)
135     auto idbConnections = m_webIDBConnections;
136     for (auto& connection : idbConnections.values())
137         connection->disconnectedFromWebProcess();
138
139     m_webIDBConnections.clear();
140 #endif
141
142 #if ENABLE(SERVICE_WORKER)
143     Vector<std::unique_ptr<WebSWServerConnection>> connectionVector;
144     connectionVector.reserveInitialCapacity(m_swConnections.size());
145
146     for (auto& connection : m_swConnections.values())
147         connectionVector.uncheckedAppend(WTFMove(connection));
148     for (auto& connection : connectionVector)
149         connection->disconnectedFromWebProcess();
150
151     m_swConnections.clear();
152 #endif
153 }
154
155 void StorageToWebProcessConnection::didReceiveInvalidMessage(IPC::Connection&, IPC::StringReference messageReceiverName, IPC::StringReference messageName)
156 {
157
158 }
159
160 #if ENABLE(SERVICE_WORKER)
161 void StorageToWebProcessConnection::establishSWServerConnection(SessionID sessionID, SWServerConnectionIdentifier& serverConnectionIdentifier)
162 {
163     auto& server = StorageProcess::singleton().swServerForSession(sessionID);
164     auto connection = std::make_unique<WebSWServerConnection>(server, m_connection.get(), sessionID);
165
166     serverConnectionIdentifier = connection->identifier();
167     LOG(ServiceWorker, "StorageToWebProcessConnection::establishSWServerConnection - %s", serverConnectionIdentifier.loggingString().utf8().data());
168     ASSERT(!m_swConnections.contains(serverConnectionIdentifier));
169
170     auto addResult = m_swConnections.add(serverConnectionIdentifier, WTFMove(connection));
171     ASSERT_UNUSED(addResult, addResult.isNewEntry);
172
173     if (!StorageProcess::singleton().globalServerToContextConnection())
174         StorageProcess::singleton().createServerToContextConnection(sessionID);
175 }
176
177 void StorageToWebProcessConnection::removeSWServerConnection(WebCore::SWServer::Connection::Identifier serverConnectionIdentifier)
178 {
179     ASSERT(m_swConnections.contains(serverConnectionIdentifier));
180
181     auto connection = m_swConnections.take(serverConnectionIdentifier);
182     connection->disconnectedFromWebProcess();
183 }
184
185 void StorageToWebProcessConnection::workerContextProcessConnectionCreated()
186 {
187     for (auto* server : SWServer::allServers())
188         server->serverToContextConnectionCreated();
189 }
190 #endif
191
192 #if ENABLE(INDEXED_DATABASE)
193 static uint64_t generateIDBConnectionToServerIdentifier()
194 {
195     ASSERT(RunLoop::isMain());
196     static uint64_t identifier = 0;
197     return ++identifier;
198 }
199
200 void StorageToWebProcessConnection::establishIDBConnectionToServer(SessionID sessionID, uint64_t& serverConnectionIdentifier)
201 {
202     serverConnectionIdentifier = generateIDBConnectionToServerIdentifier();
203     LOG(IndexedDB, "StorageToWebProcessConnection::establishIDBConnectionToServer - %" PRIu64, serverConnectionIdentifier);
204     ASSERT(!m_webIDBConnections.contains(serverConnectionIdentifier));
205
206     m_webIDBConnections.set(serverConnectionIdentifier, WebIDBConnectionToClient::create(*this, serverConnectionIdentifier, sessionID));
207 }
208
209 void StorageToWebProcessConnection::removeIDBConnectionToServer(uint64_t serverConnectionIdentifier)
210 {
211     ASSERT(m_webIDBConnections.contains(serverConnectionIdentifier));
212
213     auto connection = m_webIDBConnections.take(serverConnectionIdentifier);
214     connection->disconnectedFromWebProcess();
215 }
216 #endif
217
218 } // namespace WebKit