1/*
2 * Copyright (C) 2010 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 "AuxiliaryProcess.h"
28
29#include "Logging.h"
30#include "SandboxInitializationParameters.h"
31#include <WebCore/SchemeRegistry.h>
32#include <pal/SessionID.h>
33
34#if !OS(WINDOWS)
35#include <unistd.h>
36#endif
37
38#if OS(LINUX)
39#include <wtf/MemoryPressureHandler.h>
40#endif
41
42namespace WebKit {
43using namespace WebCore;
44
45AuxiliaryProcess::AuxiliaryProcess()
46 : m_terminationCounter(0)
47 , m_terminationTimer(RunLoop::main(), this, &AuxiliaryProcess::terminationTimerFired)
48 , m_processSuppressionDisabled("Process Suppression Disabled by UIProcess")
49{
50}
51
52AuxiliaryProcess::~AuxiliaryProcess()
53{
54}
55
56void AuxiliaryProcess::didClose(IPC::Connection&)
57{
58 _exit(EXIT_SUCCESS);
59}
60
61void AuxiliaryProcess::initialize(const AuxiliaryProcessInitializationParameters& parameters)
62{
63 RELEASE_ASSERT_WITH_MESSAGE(parameters.processIdentifier, "Unable to initialize child process without a WebCore process identifier");
64 Process::setIdentifier(*parameters.processIdentifier);
65
66 platformInitialize();
67
68#if PLATFORM(COCOA)
69 m_priorityBoostMessage = parameters.priorityBoostMessage;
70#endif
71
72 initializeProcess(parameters);
73
74 SandboxInitializationParameters sandboxParameters;
75 initializeSandbox(parameters, sandboxParameters);
76
77 initializeProcessName(parameters);
78
79 // In WebKit2, only the UI process should ever be generating non-default PAL::SessionIDs.
80 PAL::SessionID::enableGenerationProtection();
81
82 m_connection = IPC::Connection::createClientConnection(parameters.connectionIdentifier, *this);
83 initializeConnection(m_connection.get());
84 m_connection->open();
85}
86
87void AuxiliaryProcess::setProcessSuppressionEnabled(bool enabled)
88{
89 if (enabled)
90 m_processSuppressionDisabled.stop();
91 else
92 m_processSuppressionDisabled.start();
93}
94
95void AuxiliaryProcess::initializeProcess(const AuxiliaryProcessInitializationParameters&)
96{
97}
98
99void AuxiliaryProcess::initializeProcessName(const AuxiliaryProcessInitializationParameters&)
100{
101}
102
103void AuxiliaryProcess::initializeConnection(IPC::Connection*)
104{
105}
106
107void AuxiliaryProcess::addMessageReceiver(IPC::StringReference messageReceiverName, IPC::MessageReceiver& messageReceiver)
108{
109 m_messageReceiverMap.addMessageReceiver(messageReceiverName, messageReceiver);
110}
111
112void AuxiliaryProcess::addMessageReceiver(IPC::StringReference messageReceiverName, uint64_t destinationID, IPC::MessageReceiver& messageReceiver)
113{
114 m_messageReceiverMap.addMessageReceiver(messageReceiverName, destinationID, messageReceiver);
115}
116
117void AuxiliaryProcess::removeMessageReceiver(IPC::StringReference messageReceiverName, uint64_t destinationID)
118{
119 m_messageReceiverMap.removeMessageReceiver(messageReceiverName, destinationID);
120}
121
122void AuxiliaryProcess::removeMessageReceiver(IPC::StringReference messageReceiverName)
123{
124 m_messageReceiverMap.removeMessageReceiver(messageReceiverName);
125}
126
127void AuxiliaryProcess::removeMessageReceiver(IPC::MessageReceiver& messageReceiver)
128{
129 m_messageReceiverMap.removeMessageReceiver(messageReceiver);
130}
131
132void AuxiliaryProcess::disableTermination()
133{
134 m_terminationCounter++;
135 m_terminationTimer.stop();
136}
137
138void AuxiliaryProcess::enableTermination()
139{
140 ASSERT(m_terminationCounter > 0);
141 m_terminationCounter--;
142
143 if (m_terminationCounter)
144 return;
145
146 if (!m_terminationTimeout) {
147 terminationTimerFired();
148 return;
149 }
150
151 m_terminationTimer.startOneShot(m_terminationTimeout);
152}
153
154IPC::Connection* AuxiliaryProcess::messageSenderConnection() const
155{
156 return m_connection.get();
157}
158
159uint64_t AuxiliaryProcess::messageSenderDestinationID() const
160{
161 return 0;
162}
163
164void AuxiliaryProcess::terminationTimerFired()
165{
166 if (!shouldTerminate())
167 return;
168
169 terminate();
170}
171
172void AuxiliaryProcess::stopRunLoop()
173{
174 platformStopRunLoop();
175}
176
177#if !PLATFORM(COCOA)
178void AuxiliaryProcess::platformStopRunLoop()
179{
180 RunLoop::main().stop();
181}
182#endif
183
184void AuxiliaryProcess::terminate()
185{
186 m_connection->invalidate();
187
188 stopRunLoop();
189}
190
191void AuxiliaryProcess::shutDown()
192{
193 terminate();
194}
195
196void AuxiliaryProcess::registerURLSchemeServiceWorkersCanHandle(const String& urlScheme) const
197{
198 WebCore::SchemeRegistry::registerURLSchemeServiceWorkersCanHandle(urlScheme);
199}
200
201#if !PLATFORM(COCOA)
202void AuxiliaryProcess::platformInitialize()
203{
204}
205
206void AuxiliaryProcess::initializeSandbox(const AuxiliaryProcessInitializationParameters&, SandboxInitializationParameters&)
207{
208}
209
210void AuxiliaryProcess::didReceiveInvalidMessage(IPC::Connection&, IPC::StringReference messageReceiverName, IPC::StringReference messageName)
211{
212 WTFLogAlways("Received invalid message: '%s::%s'", messageReceiverName.toString().data(), messageName.toString().data());
213 CRASH();
214}
215
216#if OS(LINUX)
217void AuxiliaryProcess::didReceiveMemoryPressureEvent(bool isCritical)
218{
219 MemoryPressureHandler::singleton().triggerMemoryPressureEvent(isCritical);
220}
221#endif
222
223#endif // !PLATFORM(COCOA)
224
225} // namespace WebKit
226