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#ifndef ChildProcess_h
27#define ChildProcess_h
28
29#include "Connection.h"
30#include "MessageReceiverMap.h"
31#include "MessageSender.h"
32#include <WebCore/UserActivity.h>
33#include <wtf/HashMap.h>
34#include <wtf/RetainPtr.h>
35#include <wtf/RunLoop.h>
36#include <wtf/text/StringHash.h>
37#include <wtf/text/WTFString.h>
38
39namespace WebKit {
40
41class SandboxInitializationParameters;
42
43struct ChildProcessInitializationParameters {
44    String uiProcessName;
45    String clientIdentifier;
46    IPC::Connection::Identifier connectionIdentifier;
47    HashMap<String, String> extraInitializationData;
48};
49
50class ChildProcess : protected IPC::Connection::Client, public IPC::MessageSender {
51    WTF_MAKE_NONCOPYABLE(ChildProcess);
52
53public:
54    void initialize(const ChildProcessInitializationParameters&);
55
56    // disable and enable termination of the process. when disableTermination is called, the
57    // process won't terminate unless a corresponding disableTermination call is made.
58    void disableTermination();
59    void enableTermination();
60
61    void addMessageReceiver(IPC::StringReference messageReceiverName, IPC::MessageReceiver&);
62    void addMessageReceiver(IPC::StringReference messageReceiverName, uint64_t destinationID, IPC::MessageReceiver&);
63    void removeMessageReceiver(IPC::StringReference messageReceiverName, uint64_t destinationID);
64
65    void setProcessSuppressionEnabled(bool);
66
67#if PLATFORM(COCOA)
68    void setApplicationIsDaemon();
69    void setQOS(int latencyQOS, int throughputQOS);
70#endif
71
72    IPC::Connection* parentProcessConnection() const { return m_connection.get(); }
73
74    IPC::MessageReceiverMap& messageReceiverMap() { return m_messageReceiverMap; }
75
76protected:
77    explicit ChildProcess();
78    virtual ~ChildProcess();
79
80    void setTerminationTimeout(double seconds) { m_terminationTimeout = seconds; }
81
82    virtual void initializeProcess(const ChildProcessInitializationParameters&);
83    virtual void initializeProcessName(const ChildProcessInitializationParameters&);
84    virtual void initializeSandbox(const ChildProcessInitializationParameters&, SandboxInitializationParameters&);
85    virtual void initializeConnection(IPC::Connection*);
86
87    virtual bool shouldTerminate() = 0;
88    virtual void terminate();
89
90    virtual void stopRunLoop();
91
92#if USE(APPKIT)
93    static void stopNSAppRunLoop();
94#endif
95
96private:
97    // IPC::MessageSender
98    virtual IPC::Connection* messageSenderConnection() override;
99    virtual uint64_t messageSenderDestinationID() override;
100
101    void terminationTimerFired();
102
103    void platformInitialize();
104
105    // The timeout, in seconds, before this process will be terminated if termination
106    // has been enabled. If the timeout is 0 seconds, the process will be terminated immediately.
107    double m_terminationTimeout;
108
109    // A termination counter; when the counter reaches zero, the process will be terminated
110    // after a given period of time.
111    unsigned m_terminationCounter;
112
113    RunLoop::Timer<ChildProcess> m_terminationTimer;
114
115    RefPtr<IPC::Connection> m_connection;
116    IPC::MessageReceiverMap m_messageReceiverMap;
117
118    UserActivity m_processSuppressionDisabled;
119};
120
121} // namespace WebKit
122
123#endif // ChildProcess_h
124