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"
12 #include "mozilla/ipc/MiniTransceiver.h"
13 #include "mozilla/ipc/LaunchError.h"
14 #include "mozilla/Result.h"
16 #include <sys/types.h>
22 class GeckoChildProcessHost
;
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
{
33 ForkServiceChild(int aFd
, GeckoChildProcessHost
* aProcess
);
34 virtual ~ForkServiceChild();
37 * Ask the fork server to create a new process with given parameters.
39 * The fork server uses |base::LaunchApp()| to create a new
40 * content process with the following parameters.
42 * \param aArgv assigns |argv| of the content process.
43 * \param aEnvMap sets |LaunchOptions::env_map|.
44 * \param aFdsRemap sets |LaunchOptions::fd_to_remap|.
45 * \param aPid returns the PID of the content process created.
46 * \return true if success.
48 Result
<Ok
, LaunchError
> SendForkNewSubprocess(
49 const nsTArray
<nsCString
>& aArgv
, const nsTArray
<EnvVar
>& aEnvMap
,
50 const nsTArray
<FdMapping
>& aFdsRemap
, pid_t
* aPid
);
53 * Create a fork server process and the singleton of this class.
55 * This function uses |GeckoChildProcessHost| to launch the fork
56 * server, getting the fd of a pipe/socket to the fork server from
57 * it's |IPC::Channel|.
59 static void StartForkServer();
60 static void StopForkServer();
62 * Return the singleton.
64 static ForkServiceChild
* Get() {
65 auto child
= sForkServiceChild
.get();
66 return child
== nullptr || child
->mFailed
? nullptr : child
;
70 // Called when a message is received.
71 void OnMessageReceived(UniquePtr
<IPC::Message
> message
);
74 UniquePtr
<MiniTransceiver
> mTcver
;
75 static UniquePtr
<ForkServiceChild
> sForkServiceChild
;
77 bool mFailed
; // The forkserver has crashed or disconnected.
78 GeckoChildProcessHost
* mProcess
;
82 * Start a fork server at |xpcom-startup| from the chrome process.
84 class ForkServerLauncher
: public nsIObserver
{
90 static already_AddRefed
<ForkServerLauncher
> Create();
93 friend class ForkServiceChild
;
94 virtual ~ForkServerLauncher();
96 static void RestartForkServer();
98 static bool mHaveStartedClient
;
99 static StaticRefPtr
<ForkServerLauncher
> mSingleton
;
103 } // namespace mozilla
105 #endif /* __FORKSERVICE_CHILD_H_ */