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 "ChildProcess.h"
28
29#include "SandboxInitializationParameters.h"
30#include <unistd.h>
31
32namespace WebKit {
33
34ChildProcess::ChildProcess()
35    : m_terminationTimeout(0)
36    , m_terminationCounter(0)
37    , m_terminationTimer(RunLoop::main(), this, &ChildProcess::terminationTimerFired)
38    , m_processSuppressionDisabled("Process Suppression Disabled by UIProcess")
39{
40}
41
42ChildProcess::~ChildProcess()
43{
44}
45
46static void didCloseOnConnectionWorkQueue(IPC::Connection*)
47{
48    // If the connection has been closed and we haven't responded in the main thread for 10 seconds
49    // the process will exit forcibly.
50    auto watchdogDelay = std::chrono::seconds(10);
51
52    WorkQueue::create("com.apple.WebKit.ChildProcess.WatchDogQueue")->dispatchAfter(watchdogDelay, []{
53        // We use _exit here since the watchdog callback is called from another thread and we don't want
54        // global destructors or atexit handlers to be called from this thread while the main thread is busy
55        // doing its thing.
56        _exit(EXIT_FAILURE);
57    });
58}
59
60void ChildProcess::initialize(const ChildProcessInitializationParameters& parameters)
61{
62    platformInitialize();
63
64    initializeProcess(parameters);
65    initializeProcessName(parameters);
66
67    SandboxInitializationParameters sandboxParameters;
68    initializeSandbox(parameters, sandboxParameters);
69
70    m_connection = IPC::Connection::createClientConnection(parameters.connectionIdentifier, this, RunLoop::main());
71    m_connection->setDidCloseOnConnectionWorkQueueCallback(didCloseOnConnectionWorkQueue);
72    initializeConnection(m_connection.get());
73    m_connection->open();
74}
75
76void ChildProcess::setProcessSuppressionEnabled(bool enabled)
77{
78    if (enabled)
79        m_processSuppressionDisabled.stop();
80    else
81        m_processSuppressionDisabled.start();
82}
83
84void ChildProcess::initializeProcess(const ChildProcessInitializationParameters&)
85{
86}
87
88void ChildProcess::initializeProcessName(const ChildProcessInitializationParameters&)
89{
90}
91
92void ChildProcess::initializeConnection(IPC::Connection*)
93{
94}
95
96void ChildProcess::addMessageReceiver(IPC::StringReference messageReceiverName, IPC::MessageReceiver& messageReceiver)
97{
98    m_messageReceiverMap.addMessageReceiver(messageReceiverName, messageReceiver);
99}
100
101void ChildProcess::addMessageReceiver(IPC::StringReference messageReceiverName, uint64_t destinationID, IPC::MessageReceiver& messageReceiver)
102{
103    m_messageReceiverMap.addMessageReceiver(messageReceiverName, destinationID, messageReceiver);
104}
105
106void ChildProcess::removeMessageReceiver(IPC::StringReference messageReceiverName, uint64_t destinationID)
107{
108    m_messageReceiverMap.removeMessageReceiver(messageReceiverName, destinationID);
109}
110
111void ChildProcess::disableTermination()
112{
113    m_terminationCounter++;
114    m_terminationTimer.stop();
115}
116
117void ChildProcess::enableTermination()
118{
119    ASSERT(m_terminationCounter > 0);
120    m_terminationCounter--;
121
122    if (m_terminationCounter)
123        return;
124
125    if (!m_terminationTimeout) {
126        terminationTimerFired();
127        return;
128    }
129
130    m_terminationTimer.startOneShot(m_terminationTimeout);
131}
132
133IPC::Connection* ChildProcess::messageSenderConnection()
134{
135    return m_connection.get();
136}
137
138uint64_t ChildProcess::messageSenderDestinationID()
139{
140    return 0;
141}
142
143void ChildProcess::terminationTimerFired()
144{
145    if (!shouldTerminate())
146        return;
147
148    terminate();
149}
150
151void ChildProcess::stopRunLoop()
152{
153    RunLoop::main().stop();
154}
155
156void ChildProcess::terminate()
157{
158    m_connection->invalidate();
159
160    stopRunLoop();
161}
162
163#if !PLATFORM(COCOA)
164void ChildProcess::platformInitialize()
165{
166}
167
168void ChildProcess::initializeSandbox(const ChildProcessInitializationParameters&, SandboxInitializationParameters&)
169{
170}
171#endif
172
173} // namespace WebKit
174