Bug 1897589 - Simplify worker shutdown checks. r=jstutte,dom-worker-reviewers
[gecko.git] / ipc / glue / ForkServiceChild.h
blob9db3dc3828aa84e9cd8fafafd20f909f22bf2964
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #ifndef __FORKSERVICE_CHILD_H_
7 #define __FORKSERVICE_CHILD_H_
9 #include "base/process_util.h"
10 #include "nsIObserver.h"
11 #include "nsString.h"
12 #include "mozilla/ipc/MiniTransceiver.h"
13 #include "mozilla/ipc/LaunchError.h"
14 #include "mozilla/Result.h"
16 #include <sys/types.h>
17 #include <poll.h>
19 namespace mozilla {
20 namespace ipc {
22 class GeckoChildProcessHost;
24 /**
25 * This is the interface to the fork server.
27 * When the chrome process calls |ForkServiceChild| to create a new
28 * process, this class send a message to the fork server through a
29 * pipe and get the PID of the new process from the reply.
31 class ForkServiceChild {
32 public:
33 ForkServiceChild(int aFd, GeckoChildProcessHost* aProcess);
34 virtual ~ForkServiceChild();
36 struct Args {
37 #if defined(XP_LINUX) && defined(MOZ_SANDBOX)
38 int mForkFlags = 0;
39 bool mChroot = false;
40 #endif
41 nsTArray<nsCString> mArgv;
42 nsTArray<EnvVar> mEnv;
43 nsTArray<FdMapping> mFdsRemap;
46 /**
47 * Ask the fork server to create a new process with given parameters.
49 * The fork server uses |base::LaunchApp()| to create a new
50 * content process with the following parameters.
52 * \param aArgv assigns |argv| of the content process.
53 * \param aEnvMap sets |LaunchOptions::env_map|.
54 * \param aFdsRemap sets |LaunchOptions::fd_to_remap|.
55 * \param aPid returns the PID of the content process created.
56 * \return true if success.
58 Result<Ok, LaunchError> SendForkNewSubprocess(const Args& aArgs, pid_t* aPid);
60 /**
61 * Create a fork server process and the singleton of this class.
63 * This function uses |GeckoChildProcessHost| to launch the fork
64 * server, getting the fd of a pipe/socket to the fork server from
65 * it's |IPC::Channel|.
67 static void StartForkServer();
68 static void StopForkServer();
69 /**
70 * Return the singleton.
72 static ForkServiceChild* Get() {
73 auto child = sForkServiceChild.get();
74 return child == nullptr || child->mFailed ? nullptr : child;
77 /**
78 * Returns whether the fork server was ever active. Thread-safe.
80 static bool WasUsed() { return sForkServiceUsed; }
82 private:
83 // Called when a message is received.
84 void OnMessageReceived(UniquePtr<IPC::Message> message);
85 void OnError();
87 UniquePtr<MiniTransceiver> mTcver;
88 static UniquePtr<ForkServiceChild> sForkServiceChild;
89 static Atomic<bool> sForkServiceUsed;
90 pid_t mRecvPid;
91 bool mFailed; // The forkserver has crashed or disconnected.
92 GeckoChildProcessHost* mProcess;
95 /**
96 * Start a fork server at |xpcom-startup| from the chrome process.
98 class ForkServerLauncher : public nsIObserver {
99 public:
100 NS_DECL_ISUPPORTS
101 NS_DECL_NSIOBSERVER
103 ForkServerLauncher();
104 static already_AddRefed<ForkServerLauncher> Create();
106 private:
107 friend class ForkServiceChild;
108 virtual ~ForkServerLauncher();
110 static void RestartForkServer();
112 static bool mHaveStartedClient;
113 static StaticRefPtr<ForkServerLauncher> mSingleton;
116 } // namespace ipc
117 } // namespace mozilla
119 #endif /* __FORKSERVICE_CHILD_H_ */