1/*
2 * Copyright (C) 2010-2019 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#pragma once
27
28#include "Connection.h"
29#include "MessageReceiverMap.h"
30#include "MessageSender.h"
31#include <WebCore/ProcessIdentifier.h>
32#include <WebCore/UserActivity.h>
33#include <wtf/HashMap.h>
34#include <wtf/RunLoop.h>
35#include <wtf/text/StringHash.h>
36#include <wtf/text/WTFString.h>
37
38namespace WebKit {
39
40class SandboxInitializationParameters;
41struct AuxiliaryProcessInitializationParameters;
42
43class AuxiliaryProcess : protected IPC::Connection::Client, public IPC::MessageSender {
44 WTF_MAKE_NONCOPYABLE(AuxiliaryProcess);
45
46public:
47 enum class ProcessType : uint8_t {
48 WebContent,
49 Network,
50 Plugin
51 };
52
53 void initialize(const AuxiliaryProcessInitializationParameters&);
54
55 // disable and enable termination of the process. when disableTermination is called, the
56 // process won't terminate unless a corresponding disableTermination call is made.
57 void disableTermination();
58 void enableTermination();
59
60 void addMessageReceiver(IPC::StringReference messageReceiverName, IPC::MessageReceiver&);
61 void addMessageReceiver(IPC::StringReference messageReceiverName, uint64_t destinationID, IPC::MessageReceiver&);
62 void removeMessageReceiver(IPC::StringReference messageReceiverName, uint64_t destinationID);
63 void removeMessageReceiver(IPC::StringReference messageReceiverName);
64 void removeMessageReceiver(IPC::MessageReceiver&);
65
66 void setProcessSuppressionEnabled(bool);
67
68#if PLATFORM(COCOA)
69 void setApplicationIsDaemon();
70 void launchServicesCheckIn();
71 void setQOS(int latencyQOS, int throughputQOS);
72#endif
73
74 IPC::Connection* parentProcessConnection() const { return m_connection.get(); }
75
76 IPC::MessageReceiverMap& messageReceiverMap() { return m_messageReceiverMap; }
77
78#if PLATFORM(MAC)
79 static bool isSystemWebKit();
80#endif
81
82#if PLATFORM(COCOA)
83 bool parentProcessHasEntitlement(const char* entitlement);
84#endif
85
86protected:
87 explicit AuxiliaryProcess();
88 virtual ~AuxiliaryProcess();
89
90 void setTerminationTimeout(Seconds seconds) { m_terminationTimeout = seconds; }
91
92 virtual void initializeProcess(const AuxiliaryProcessInitializationParameters&);
93 virtual void initializeProcessName(const AuxiliaryProcessInitializationParameters&);
94 virtual void initializeSandbox(const AuxiliaryProcessInitializationParameters&, SandboxInitializationParameters&);
95 virtual void initializeConnection(IPC::Connection*);
96
97 virtual bool shouldTerminate() = 0;
98 virtual void terminate();
99
100 virtual void stopRunLoop();
101
102#if USE(APPKIT)
103 static void stopNSAppRunLoop();
104#endif
105
106#if PLATFORM(MAC) && ENABLE(WEBPROCESS_NSRUNLOOP)
107 static void stopNSRunLoop();
108#endif
109
110 void didReceiveMessage(IPC::Connection&, IPC::Decoder&) override;
111
112 void registerURLSchemeServiceWorkersCanHandle(const String&) const;
113#if OS(LINUX)
114 void didReceiveMemoryPressureEvent(bool isCritical);
115#endif
116
117private:
118 virtual bool shouldOverrideQuarantine() { return true; }
119
120 // IPC::MessageSender
121 IPC::Connection* messageSenderConnection() const override;
122 uint64_t messageSenderDestinationID() const override;
123
124 // IPC::Connection::Client.
125 void didReceiveInvalidMessage(IPC::Connection&, IPC::StringReference messageReceiverName, IPC::StringReference messageName) final;
126 void didClose(IPC::Connection&) override;
127
128 void shutDown();
129
130 void terminationTimerFired();
131
132 void platformInitialize();
133 void platformStopRunLoop();
134
135 // The timeout, in seconds, before this process will be terminated if termination
136 // has been enabled. If the timeout is 0 seconds, the process will be terminated immediately.
137 Seconds m_terminationTimeout;
138
139 // A termination counter; when the counter reaches zero, the process will be terminated
140 // after a given period of time.
141 unsigned m_terminationCounter;
142
143 RunLoop::Timer<AuxiliaryProcess> m_terminationTimer;
144
145 RefPtr<IPC::Connection> m_connection;
146 IPC::MessageReceiverMap m_messageReceiverMap;
147
148 UserActivity m_processSuppressionDisabled;
149
150#if PLATFORM(COCOA)
151 OSObjectPtr<xpc_object_t> m_priorityBoostMessage;
152#endif
153};
154
155struct AuxiliaryProcessInitializationParameters {
156 String uiProcessName;
157 String clientIdentifier;
158 Optional<WebCore::ProcessIdentifier> processIdentifier;
159 IPC::Connection::Identifier connectionIdentifier;
160 HashMap<String, String> extraInitializationData;
161 AuxiliaryProcess::ProcessType processType;
162#if PLATFORM(COCOA)
163 OSObjectPtr<xpc_object_t> priorityBoostMessage;
164#endif
165};
166
167} // namespace WebKit
168
169