blob: 8ef9d08949dcbef57cc47104f50ed7a9a31bd3a1 [file] [log] [blame]
Avi Drissman4e1b7bc32022-09-15 14:03:501// Copyright 2012 The Chromium Authors
[email protected]4734d0b2011-12-03 07:10:442// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Will Harriscd57b832023-01-05 20:03:105#ifndef CONTENT_BROWSER_CHILD_PROCESS_HOST_IMPL_H_
6#define CONTENT_BROWSER_CHILD_PROCESS_HOST_IMPL_H_
[email protected]4734d0b2011-12-03 07:10:447
avia9aa7a82015-12-25 03:06:318#include <stddef.h>
9#include <stdint.h>
10
dcheng4ac58c7a2016-04-09 04:51:4811#include <memory>
Arthur Sonzognic686e8f2024-01-11 08:36:3712#include <optional>
[email protected]4734d0b2011-12-03 07:10:4413#include <string>
14#include <vector>
15
Keishi Hattori0e45c022021-11-27 09:25:5216#include "base/memory/raw_ptr.h"
rvargas5779b382014-11-18 20:44:1117#include "base/process/process.h"
dcheng4ac58c7a2016-04-09 04:51:4818#include "build/build_config.h"
Ken Rockot62fb4352019-07-18 16:03:3819#include "content/common/child_process.mojom.h"
Lei Zhang7ab313752021-11-17 01:26:0020#include "content/common/content_export.h"
Will Harriscd57b832023-01-05 20:03:1021#include "content/public/browser/child_process_host.h"
[email protected]30fe1f92013-06-12 16:34:3422#include "ipc/ipc_listener.h"
Ken Rockot62fb4352019-07-18 16:03:3823#include "mojo/public/cpp/bindings/receiver.h"
24#include "mojo/public/cpp/bindings/remote.h"
Ken Rockot692493f2019-11-18 22:36:5725#include "mojo/public/cpp/system/invitation.h"
[email protected]4734d0b2011-12-03 07:10:4426
Takashi Sakamoto9fe220a82022-12-12 16:41:1327#if BUILDFLAG(IS_ANDROID)
28#include "base/memory/memory_pressure_listener.h"
29#endif
30
[email protected]74122042014-04-25 00:07:3031namespace IPC {
Lei Zhang0e004cb2021-12-01 16:43:3432class Channel;
Andrea Orru966ff882022-09-08 03:24:2433} // namespace IPC
[email protected]74122042014-04-25 00:07:3034
[email protected]4734d0b2011-12-03 07:10:4435namespace content {
36class ChildProcessHostDelegate;
37
38// Provides common functionality for hosting a child process and processing IPC
39// messages between the host and the child process. Users are responsible
40// for the actual launching and terminating of the child processes.
Andrea Orru966ff882022-09-08 03:24:2441class CONTENT_EXPORT ChildProcessHostImpl : public ChildProcessHost,
42 public IPC::Listener,
43 public mojom::ChildProcessHost {
[email protected]4734d0b2011-12-03 07:10:4444 public:
Peter Boström828b9022021-09-21 02:28:4345 ChildProcessHostImpl(const ChildProcessHostImpl&) = delete;
46 ChildProcessHostImpl& operator=(const ChildProcessHostImpl&) = delete;
47
dchenge933b3eb2014-10-21 11:44:0948 ~ChildProcessHostImpl() override;
[email protected]4734d0b2011-12-03 07:10:4449
ssid1050d802015-07-28 20:28:1450 // Derives a tracing process id from a child process id. Child process ids
51 // cannot be used directly in child process for tracing due to security
52 // reasons (see: discussion in crrev.com/1173263004). This method is meant to
53 // be used when tracing for identifying cross-process shared memory from a
54 // process which knows the child process id of its endpoints. The value
55 // returned by this method is guaranteed to be equal to the value returned by
56 // MemoryDumpManager::GetTracingProcessId() in the corresponding child
57 // process.
58 //
59 // Never returns MemoryDumpManager::kInvalidTracingProcessId.
chiniforooshan6e4c5072017-03-17 07:56:5660 // Returns only memory_instrumentation::mojom::kServiceTracingProcessId in
61 // single-process mode.
Emily Andrews2eab36a2024-12-03 20:15:4862 static uint64_t ChildProcessIdToTracingProcessId(
63 ChildProcessId child_process_id);
64
65 // TODO(crbug.com/379869738): Deprecated, please use
66 // ChildProcessIdToTracingProcessId above.
avia9aa7a82015-12-25 03:06:3167 static uint64_t ChildProcessUniqueIdToTracingProcessId(int child_process_id);
ssid1050d802015-07-28 20:28:1468
[email protected]4734d0b2011-12-03 07:10:4469 // ChildProcessHost implementation
dchenge933b3eb2014-10-21 11:44:0970 void ForceShutdown() override;
Arthur Sonzognic686e8f2024-01-11 08:36:3771 std::optional<mojo::OutgoingInvitation>& GetMojoInvitation() override;
rockotda9887902016-08-19 20:46:4272 void CreateChannelMojo() override;
dchenge933b3eb2014-10-21 11:44:0973 bool IsChannelOpening() override;
Ken Rockotced31272019-08-02 21:12:1874 void BindReceiver(mojo::GenericPendingReceiver receiver) override;
Etienne Pierre-doraye5313362024-07-18 20:07:2475 void SetBatterySaverMode(bool battery_saver_mode_enabled) override;
Ryan Keane190beb32022-06-16 01:08:1076
Georg Neis35ff854b2024-12-17 02:02:0877#if BUILDFLAG(IS_CHROMEOS)
Andrea Orru966ff882022-09-08 03:24:2478 void ReinitializeLogging(uint32_t logging_dest,
79 base::ScopedFD log_file_descriptor) override;
80#endif
81
Ken Rockot9836cfb72021-06-16 23:16:0282 base::Process& GetPeerProcess();
Ken Rockot62fb4352019-07-18 16:03:3883 mojom::ChildProcess* child_process() { return child_process_.get(); }
John Abd-El-Malekc840aa8d2018-05-21 22:00:3884
Takashi Sakamoto9fe220a82022-12-12 16:41:1385#if BUILDFLAG(IS_ANDROID)
86 // Notifies the child process of memory pressure level.
87 void NotifyMemoryPressureToChildProcess(
88 base::MemoryPressureListener::MemoryPressureLevel level);
89#endif
90
[email protected]4734d0b2011-12-03 07:10:4491 private:
Ken Rockot4f8c3c32019-08-16 16:32:3392 friend class content::ChildProcessHost;
[email protected]4734d0b2011-12-03 07:10:4493
Tom Sepez7bf89002025-07-17 23:44:0494 explicit ChildProcessHostImpl(ChildProcessHostDelegate* delegate);
[email protected]4734d0b2011-12-03 07:10:4495
Ken Rockot4f8c3c32019-08-16 16:32:3396 // mojom::ChildProcessHost implementation:
Ken Rockot9836cfb72021-06-16 23:16:0297 void Ping(PingCallback callback) override;
Ken Rockot4f8c3c32019-08-16 16:32:3398 void BindHostReceiver(mojo::GenericPendingReceiver receiver) override;
99
[email protected]d84effeb2012-06-25 17:03:10100 // IPC::Listener methods:
avia9aa7a82015-12-25 03:06:31101 void OnChannelConnected(int32_t peer_pid) override;
dchenge933b3eb2014-10-21 11:44:09102 void OnChannelError() override;
103 void OnBadMessageReceived(const IPC::Message& message) override;
[email protected]4734d0b2011-12-03 07:10:44104
amistryab52b0c2016-06-07 04:22:57105 // Initializes the IPC channel and returns true on success. |channel_| must be
106 // non-null.
107 bool InitChannel();
108
Ken Rockot9836cfb72021-06-16 23:16:02109 void OnDisconnectedFromChildProcess();
110
Sebastien Marchandf552e982020-09-09 20:19:38111#if BUILDFLAG(CLANG_PROFILING_INSIDE_SANDBOX)
112 void DumpProfilingData(base::OnceClosure callback) override;
Will Harris6f5f50a2021-12-03 17:39:45113 void SetProfilingFile(base::File file) override;
Sebastien Marchandf552e982020-09-09 20:19:38114#endif
115
Ken Rockot692493f2019-11-18 22:36:57116 // The outgoing Mojo invitation which must be consumed to bootstrap Mojo IPC
117 // to the child process.
Arthur Sonzognic686e8f2024-01-11 08:36:37118 std::optional<mojo::OutgoingInvitation> mojo_invitation_{std::in_place};
Ken Rockot692493f2019-11-18 22:36:57119
Keishi Hattori0e45c022021-11-27 09:25:52120 raw_ptr<ChildProcessHostDelegate> delegate_;
rvargas5779b382014-11-18 20:44:11121 base::Process peer_process_;
[email protected]4734d0b2011-12-03 07:10:44122 bool opening_channel_; // True while we're waiting the channel to be opened.
dcheng4ac58c7a2016-04-09 04:51:48123 std::unique_ptr<IPC::Channel> channel_;
Ken Rockot62fb4352019-07-18 16:03:38124 mojo::Remote<mojom::ChildProcess> child_process_;
Ken Rockot4f8c3c32019-08-16 16:32:33125 mojo::Receiver<mojom::ChildProcessHost> receiver_{this};
[email protected]4734d0b2011-12-03 07:10:44126};
127
128} // namespace content
129
Will Harriscd57b832023-01-05 20:03:10130#endif // CONTENT_BROWSER_CHILD_PROCESS_HOST_IMPL_H_