#ifndef CONTENT_BROWSER_UTILITY_PROCESS_HOST_H_
#define CONTENT_BROWSER_UTILITY_PROCESS_HOST_H_
#include <memory>
#include <string>
#include <vector>
#include "base/environment.h"
#include "base/memory/weak_ptr.h"
#include "base/process/launch.h"
#include "build/build_config.h"
#include "build/chromecast_buildflags.h"
#include "content/browser/child_process_launcher.h"
#include "content/common/child_process.mojom.h"
#include "content/common/content_export.h"
#include "content/public/browser/browser_child_process_host_delegate.h"
#include "content/public/common/zygote/zygote_buildflags.h"
#include "mojo/public/cpp/bindings/generic_pending_receiver.h"
#include "mojo/public/cpp/bindings/pending_receiver.h"
#include "sandbox/policy/mojom/sandbox.mojom.h"
#include "third_party/abseil-cpp/absl/types/optional.h"
#include "third_party/abseil-cpp/absl/types/variant.h"
#if BUILDFLAG(USE_ZYGOTE)
#include "content/public/common/zygote/zygote_handle.h"
#endif
#if BUILDFLAG(IS_CASTOS) || BUILDFLAG(IS_CAST_ANDROID)
#include "base/functional/callback.h"
#include "mojo/public/cpp/system/message_pipe.h"
#endif
namespace base {
class Thread;
}
#if BUILDFLAG(IS_LINUX)
namespace viz {
class GpuClient;
}
#endif
namespace content {
class BrowserChildProcessHostImpl;
class InProcessChildThreadParams;
struct ChildProcessData;
typedef base::Thread* (*UtilityMainThreadFactoryFunction)(
const InProcessChildThreadParams&);
class CONTENT_EXPORT UtilityProcessHost
: public BrowserChildProcessHostDelegate {
public:
static void RegisterUtilityMainThreadFactory(
UtilityMainThreadFactoryFunction create);
class Client {
public:
virtual ~Client() {}
virtual void OnProcessLaunched(const base::Process& process) {}
virtual void OnProcessTerminatedNormally() {}
virtual void OnProcessCrashed() {}
};
UtilityProcessHost();
explicit UtilityProcessHost(std::unique_ptr<Client> client);
UtilityProcessHost(const UtilityProcessHost&) = delete;
UtilityProcessHost& operator=(const UtilityProcessHost&) = delete;
~UtilityProcessHost() override;
base::WeakPtr<UtilityProcessHost> AsWeakPtr();
void SetSandboxType(sandbox::mojom::Sandbox sandbox_type);
const ChildProcessData& GetData();
#if BUILDFLAG(IS_POSIX)
void SetEnv(const base::EnvironmentMap& env);
#endif
bool Start();
#if BUILDFLAG(IS_CASTOS) || BUILDFLAG(IS_CAST_ANDROID)
using RunServiceDeprecatedCallback =
base::OnceCallback<void(absl::optional<base::ProcessId>)>;
void RunServiceDeprecated(const std::string& service_name,
mojo::ScopedMessagePipeHandle service_pipe,
RunServiceDeprecatedCallback callback);
#endif
void SetName(const std::u16string& name);
void SetMetricsName(const std::string& metrics_name);
void set_child_flags(int flags) { child_flags_ = flags; }
void SetExtraCommandLineSwitches(std::vector<std::string> switches);
#if BUILDFLAG(IS_POSIX) && !BUILDFLAG(IS_MAC)
void AddFileToPreload(std::string key,
absl::variant<base::FilePath, base::ScopedFD> file);
#endif
#if BUILDFLAG(USE_ZYGOTE)
void SetZygoteForTesting(ZygoteCommunication* handle);
#endif
mojom::ChildProcess* GetChildProcess();
private:
bool StartProcess();
void OnProcessLaunched() override;
void OnProcessLaunchFailed(int error_code) override;
void OnProcessCrashed(int exit_code) override;
absl::optional<std::string> GetServiceName() override;
void BindHostReceiver(mojo::GenericPendingReceiver receiver) override;
sandbox::mojom::Sandbox sandbox_type_;
int child_flags_;
base::EnvironmentMap env_;
bool started_;
std::u16string name_;
std::string metrics_name_;
std::unique_ptr<BrowserChildProcessHostImpl> process_;
std::unique_ptr<base::Thread> in_process_thread_;
std::vector<std::string> extra_switches_;
std::unique_ptr<ChildProcessLauncherFileData> file_data_;
#if BUILDFLAG(USE_ZYGOTE)
absl::optional<raw_ptr<ZygoteCommunication>> zygote_for_testing_;
#endif
enum class LaunchState {
kLaunchInProgress,
kLaunchComplete,
kLaunchFailed,
};
LaunchState launch_state_ = LaunchState::kLaunchInProgress;
#if BUILDFLAG(IS_CASTOS) || BUILDFLAG(IS_CAST_ANDROID)
std::vector<RunServiceDeprecatedCallback> pending_run_service_callbacks_;
#endif
#if BUILDFLAG(IS_LINUX)
std::unique_ptr<viz::GpuClient, base::OnTaskRunnerDeleter> gpu_client_;
#endif
std::unique_ptr<Client> client_;
base::WeakPtrFactory<UtilityProcessHost> weak_ptr_factory_{this};
};
}
#endif