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 "mozilla/GeckoArgs.h"
11 #include "nsIObserver.h"
13 #include "mozilla/ipc/MiniTransceiver.h"
14 #include "mozilla/ipc/LaunchError.h"
15 #include "mozilla/Result.h"
17 #include <sys/types.h>
23 class GeckoChildProcessHost
;
26 * This is the interface to the fork server.
28 * When the chrome process calls |ForkServiceChild| to create a new
29 * process, this class send a message to the fork server through a
30 * pipe and get the PID of the new process from the reply.
32 class ForkServiceChild
{
34 ForkServiceChild(int aFd
, GeckoChildProcessHost
* aProcess
);
35 virtual ~ForkServiceChild();
38 * Ask the fork server to create a new process with given parameters.
40 * \param aArgs arguments with file attachments used in the content process.
41 * \param aOptions other options which will be used to create the process.
42 * Not all launch options are supported.
43 * \param aPid returns the PID of the content
44 * process created. \return true if success.
46 Result
<Ok
, LaunchError
> SendForkNewSubprocess(
47 geckoargs::ChildProcessArgs
&& aArgs
, base::LaunchOptions
&& aOptions
,
51 * Create a fork server process and the singleton of this class.
53 * This function uses |GeckoChildProcessHost| to launch the fork
54 * server, getting the fd of a pipe/socket to the fork server from
55 * it's |IPC::Channel|.
57 static void StartForkServer();
58 static void StopForkServer();
61 * Return the singleton.
63 static ForkServiceChild
* Get() {
64 auto child
= sForkServiceChild
.get();
65 return child
== nullptr || child
->mFailed
? nullptr : child
;
69 * Returns whether the fork server was ever active. Thread-safe.
71 static bool WasUsed() { return sForkServiceUsed
; }
74 // Called when a message is received.
75 void OnMessageReceived(UniquePtr
<IPC::Message
> message
);
78 UniquePtr
<MiniTransceiver
> mTcver
;
79 static UniquePtr
<ForkServiceChild
> sForkServiceChild
;
80 static Atomic
<bool> sForkServiceUsed
;
82 bool mFailed
; // The forkserver has crashed or disconnected.
83 GeckoChildProcessHost
* mProcess
;
87 * Start a fork server at |xpcom-startup| from the chrome process.
89 class ForkServerLauncher
: public nsIObserver
{
95 static already_AddRefed
<ForkServerLauncher
> Create();
98 friend class ForkServiceChild
;
99 virtual ~ForkServerLauncher();
101 static void RestartForkServer();
103 static bool mHaveStartedClient
;
104 static StaticRefPtr
<ForkServerLauncher
> mSingleton
;
108 } // namespace mozilla
110 #endif /* __FORKSERVICE_CHILD_H_ */