#ifndef CONTENT_BROWSER_CHILD_PROCESS_HOST_IMPL_H_
#define CONTENT_BROWSER_CHILD_PROCESS_HOST_IMPL_H_
#include <stddef.h>
#include <stdint.h>
#include <memory>
#include <string>
#include <vector>
#include "base/memory/raw_ptr.h"
#include "base/process/process.h"
#include "build/build_config.h"
#include "content/common/child_process.mojom.h"
#include "content/common/content_export.h"
#include "content/public/browser/child_process_host.h"
#include "ipc/ipc_listener.h"
#include "mojo/public/cpp/bindings/receiver.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "mojo/public/cpp/system/invitation.h"
#include "third_party/abseil-cpp/absl/types/optional.h"
#if BUILDFLAG(IS_ANDROID)
#include "base/memory/memory_pressure_listener.h"
#endif
namespace IPC {
class Channel;
class MessageFilter;
}
namespace content {
class ChildProcessHostDelegate;
class CONTENT_EXPORT ChildProcessHostImpl : public ChildProcessHost,
public IPC::Listener,
public mojom::ChildProcessHost {
public:
ChildProcessHostImpl(const ChildProcessHostImpl&) = delete;
ChildProcessHostImpl& operator=(const ChildProcessHostImpl&) = delete;
~ChildProcessHostImpl() override;
static int GenerateChildProcessUniqueId();
static uint64_t ChildProcessUniqueIdToTracingProcessId(int child_process_id);
bool Send(IPC::Message* message) override;
void ForceShutdown() override;
absl::optional<mojo::OutgoingInvitation>& GetMojoInvitation() override;
void CreateChannelMojo() override;
bool IsChannelOpening() override;
void AddFilter(IPC::MessageFilter* filter) override;
void BindReceiver(mojo::GenericPendingReceiver receiver) override;
#if BUILDFLAG(IS_CHROMEOS_ASH)
void ReinitializeLogging(uint32_t logging_dest,
base::ScopedFD log_file_descriptor) override;
#endif
#if BUILDFLAG(IS_CASTOS) || BUILDFLAG(IS_CAST_ANDROID)
void RunServiceDeprecated(
const std::string& service_name,
mojo::ScopedMessagePipeHandle service_pipe) override;
#endif
base::Process& GetPeerProcess();
mojom::ChildProcess* child_process() { return child_process_.get(); }
#if BUILDFLAG(IS_ANDROID)
void NotifyMemoryPressureToChildProcess(
base::MemoryPressureListener::MemoryPressureLevel level);
#endif
private:
friend class content::ChildProcessHost;
ChildProcessHostImpl(ChildProcessHostDelegate* delegate, IpcMode ipc_mode);
void Ping(PingCallback callback) override;
void BindHostReceiver(mojo::GenericPendingReceiver receiver) override;
#if BUILDFLAG(IS_OHOS)
void ReportKeyThread(int32_t status, int32_t process_id, int32_t thread_id, int32_t role) override;
void ReportKeyThreadIds(int32_t status, int32_t process_id,
const std::vector<int32_t>& thread_ids, int32_t role) override {}
#endif
bool OnMessageReceived(const IPC::Message& msg) override;
void OnChannelConnected(int32_t peer_pid) override;
void OnChannelError() override;
void OnBadMessageReceived(const IPC::Message& message) override;
bool InitChannel();
void OnDisconnectedFromChildProcess();
#if BUILDFLAG(CLANG_PROFILING_INSIDE_SANDBOX)
void DumpProfilingData(base::OnceClosure callback) override;
void SetProfilingFile(base::File file) override;
#endif
absl::optional<mojo::OutgoingInvitation> mojo_invitation_{absl::in_place};
const IpcMode ipc_mode_;
raw_ptr<ChildProcessHostDelegate> delegate_;
base::Process peer_process_;
bool opening_channel_;
std::unique_ptr<IPC::Channel> channel_;
mojo::Remote<mojom::ChildProcess> child_process_;
mojo::Receiver<mojom::ChildProcessHost> receiver_{this};
std::vector<scoped_refptr<IPC::MessageFilter>> filters_;
};
}
#endif