Replace uses of ArgumentEncoder/ArgumentDecoder with MessageEncoder/MessageDecoder
[WebKit-https.git] / Source / WebKit2 / SharedWorkerProcess / SharedWorkerProcess.cpp
1 /*
2  * Copyright (C) 2010, 2012 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 "SharedWorkerProcess.h"
28
29 #include "ArgumentCoders.h"
30 #include "Attachment.h"
31 #include "SharedWorkerProcessCreationParameters.h"
32 #include "SharedWorkerProcessProxyMessages.h"
33 #include "WebProcessConnection.h"
34 #include <WebCore/NotImplemented.h>
35 #include <WebCore/RunLoop.h>
36
37 #if PLATFORM(MAC)
38 #include <crt_externs.h>
39 #endif
40
41 #if USE(UNIX_DOMAIN_SOCKETS)
42 #include <errno.h>
43 #include <fcntl.h>
44 #include <sys/resource.h>
45 #include <sys/socket.h>
46 #include <unistd.h>
47
48 #ifdef SOCK_SEQPACKET
49 #define SOCKET_TYPE SOCK_SEQPACKET
50 #else
51 #if PLATFORM(GTK)
52 #define SOCKET_TYPE SOCK_STREAM
53 #else
54 #define SOCKET_TYPE SOCK_DGRAM
55 #endif
56 #endif // SOCK_SEQPACKET
57 #endif // USE(UNIX_DOMAIN_SOCKETS)
58
59 using namespace WebCore;
60
61 namespace WebKit {
62
63 SharedWorkerProcess& SharedWorkerProcess::shared()
64 {
65     DEFINE_STATIC_LOCAL(SharedWorkerProcess, process, ());
66     return process;
67 }
68
69 SharedWorkerProcess::SharedWorkerProcess()
70     : m_minimumLifetimeTimer(RunLoop::main(), this, &SharedWorkerProcess::minimumLifetimeTimerFired)
71 {
72 }
73
74 SharedWorkerProcess::~SharedWorkerProcess()
75 {
76 }
77
78 void SharedWorkerProcess::initialize(CoreIPC::Connection::Identifier serverIdentifier, RunLoop* runLoop)
79 {
80     ASSERT(!m_connection);
81
82     m_connection = CoreIPC::Connection::createClientConnection(serverIdentifier, this, runLoop);
83     m_connection->setDidCloseOnConnectionWorkQueueCallback(didCloseOnConnectionWorkQueue);
84     m_connection->open();
85 }
86
87 void SharedWorkerProcess::removeWebProcessConnection(WebProcessConnection* webProcessConnection)
88 {
89     size_t vectorIndex = m_webProcessConnections.find(webProcessConnection);
90     ASSERT(vectorIndex != notFound);
91
92     m_webProcessConnections.remove(vectorIndex);
93     
94     enableTermination();
95 }
96
97 bool SharedWorkerProcess::shouldTerminate()
98 {
99     ASSERT(m_webProcessConnections.isEmpty());
100
101     return true;
102 }
103
104 void SharedWorkerProcess::didReceiveMessage(CoreIPC::Connection* connection, CoreIPC::MessageID messageID, CoreIPC::MessageDecoder& decoder)
105 {
106     didReceiveSharedWorkerProcessMessage(connection, messageID, decoder);
107 }
108
109 void SharedWorkerProcess::didClose(CoreIPC::Connection*)
110 {
111     // The UI process has crashed, just go ahead and quit.
112     RunLoop::current()->stop();
113 }
114
115 void SharedWorkerProcess::didReceiveInvalidMessage(CoreIPC::Connection*, CoreIPC::MessageID)
116 {
117 }
118
119 void SharedWorkerProcess::initializeSharedWorkerProcess(const SharedWorkerProcessCreationParameters& parameters)
120 {
121     setMinimumLifetime(parameters.minimumLifetime);
122     setTerminationTimeout(parameters.terminationTimeout);
123
124     platformInitialize(parameters);
125 }
126
127 void SharedWorkerProcess::createWebProcessConnection()
128 {
129 #if PLATFORM(MAC)
130     // Create the listening port.
131     mach_port_t listeningPort;
132     mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &listeningPort);
133
134     // Create a listening connection.
135     RefPtr<WebProcessConnection> connection = WebProcessConnection::create(CoreIPC::Connection::Identifier(listeningPort));
136     m_webProcessConnections.append(connection.release());
137
138     CoreIPC::Attachment clientPort(listeningPort, MACH_MSG_TYPE_MAKE_SEND);
139     m_connection->send(Messages::SharedWorkerProcessProxy::DidCreateWebProcessConnection(clientPort), 0);
140 #elif USE(UNIX_DOMAIN_SOCKETS)
141     int sockets[2];
142     if (socketpair(AF_UNIX, SOCKET_TYPE, 0, sockets) == -1) {
143         ASSERT_NOT_REACHED();
144         return;
145     }
146
147     // Don't expose the shared worker process socket to the web process.
148     while (fcntl(sockets[1], F_SETFD, FD_CLOEXEC)  == -1) {
149         if (errno != EINTR) {
150             ASSERT_NOT_REACHED();
151             while (close(sockets[0]) == -1 && errno == EINTR) { }
152             while (close(sockets[1]) == -1 && errno == EINTR) { }
153             return;
154         }
155     }
156
157     // Don't expose the shared worker process socket to possible future web processes.
158     while (fcntl(sockets[0], F_SETFD, FD_CLOEXEC) == -1) {
159         if (errno != EINTR) {
160             ASSERT_NOT_REACHED();
161             while (close(sockets[0]) == -1 && errno == EINTR) { }
162             while (close(sockets[1]) == -1 && errno == EINTR) { }
163             return;
164         }
165     }
166
167     RefPtr<WebProcessConnection> connection = WebProcessConnection::create(sockets[1]);
168     m_webProcessConnections.append(connection.release());
169
170     CoreIPC::Attachment clientSocket(sockets[0]);
171     m_connection->send(Messages::SharedWorkerProcessProxy::DidCreateWebProcessConnection(clientSocket), 0);
172 #else
173     notImplemented();
174 #endif
175
176     disableTermination();
177 }
178
179 void SharedWorkerProcess::setMinimumLifetime(double lifetime)
180 {
181     if (lifetime <= 0.0)
182         return;
183     
184     disableTermination();
185     
186     m_minimumLifetimeTimer.startOneShot(lifetime);
187 }
188
189 void SharedWorkerProcess::minimumLifetimeTimerFired()
190 {
191     enableTermination();
192 }
193
194 } // namespace WebKit