Backed out changeset b71c8c052463 (bug 1943846) for causing mass failures. CLOSED...
[gecko.git] / ipc / glue / ForkServiceChild.h
blob4b7961efde3fbb5431c6212662a35cf1b83182b1
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"
12 #include "nsString.h"
13 #include "mozilla/ipc/MiniTransceiver.h"
14 #include "mozilla/ipc/LaunchError.h"
15 #include "mozilla/Result.h"
17 #include <sys/types.h>
18 #include <poll.h>
20 namespace mozilla {
21 namespace ipc {
23 class GeckoChildProcessHost;
25 /**
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 {
33 public:
34 ForkServiceChild(int aFd, GeckoChildProcessHost* aProcess);
35 virtual ~ForkServiceChild();
37 /**
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,
48 pid_t* aPid);
50 /**
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();
60 /**
61 * Return the singleton.
63 static ForkServiceChild* Get() {
64 auto child = sForkServiceChild.get();
65 return child == nullptr || child->mFailed ? nullptr : child;
68 /**
69 * Returns whether the fork server was ever active. Thread-safe.
71 static bool WasUsed() { return sForkServiceUsed; }
73 private:
74 // Called when a message is received.
75 void OnMessageReceived(UniquePtr<IPC::Message> message);
76 void OnError();
78 UniquePtr<MiniTransceiver> mTcver;
79 static UniquePtr<ForkServiceChild> sForkServiceChild;
80 static Atomic<bool> sForkServiceUsed;
81 pid_t mRecvPid;
82 bool mFailed; // The forkserver has crashed or disconnected.
83 GeckoChildProcessHost* mProcess;
86 /**
87 * Start a fork server at |xpcom-startup| from the chrome process.
89 class ForkServerLauncher : public nsIObserver {
90 public:
91 NS_DECL_ISUPPORTS
92 NS_DECL_NSIOBSERVER
94 ForkServerLauncher();
95 static already_AddRefed<ForkServerLauncher> Create();
97 private:
98 friend class ForkServiceChild;
99 virtual ~ForkServerLauncher();
101 static void RestartForkServer();
103 static bool mHaveStartedClient;
104 static StaticRefPtr<ForkServerLauncher> mSingleton;
107 } // namespace ipc
108 } // namespace mozilla
110 #endif /* __FORKSERVICE_CHILD_H_ */