Avi Drissman | 4e1b7bc3 | 2022-09-15 14:03:50 | [diff] [blame] | 1 | // Copyright 2012 The Chromium Authors |
[email protected] | 4734d0b | 2011-12-03 07:10:44 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
3 | // found in the LICENSE file. | ||||
4 | |||||
Will Harris | cd57b83 | 2023-01-05 20:03:10 | [diff] [blame] | 5 | #ifndef CONTENT_BROWSER_CHILD_PROCESS_HOST_IMPL_H_ |
6 | #define CONTENT_BROWSER_CHILD_PROCESS_HOST_IMPL_H_ | ||||
[email protected] | 4734d0b | 2011-12-03 07:10:44 | [diff] [blame] | 7 | |
avi | a9aa7a8 | 2015-12-25 03:06:31 | [diff] [blame] | 8 | #include <stddef.h> |
9 | #include <stdint.h> | ||||
10 | |||||
dcheng | 4ac58c7a | 2016-04-09 04:51:48 | [diff] [blame] | 11 | #include <memory> |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 12 | #include <optional> |
[email protected] | 4734d0b | 2011-12-03 07:10:44 | [diff] [blame] | 13 | #include <string> |
14 | #include <vector> | ||||
15 | |||||
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 16 | #include "base/memory/raw_ptr.h" |
rvargas | 5779b38 | 2014-11-18 20:44:11 | [diff] [blame] | 17 | #include "base/process/process.h" |
dcheng | 4ac58c7a | 2016-04-09 04:51:48 | [diff] [blame] | 18 | #include "build/build_config.h" |
Ken Rockot | 62fb435 | 2019-07-18 16:03:38 | [diff] [blame] | 19 | #include "content/common/child_process.mojom.h" |
Lei Zhang | 7ab31375 | 2021-11-17 01:26:00 | [diff] [blame] | 20 | #include "content/common/content_export.h" |
Will Harris | cd57b83 | 2023-01-05 20:03:10 | [diff] [blame] | 21 | #include "content/public/browser/child_process_host.h" |
[email protected] | 30fe1f9 | 2013-06-12 16:34:34 | [diff] [blame] | 22 | #include "ipc/ipc_listener.h" |
Ken Rockot | 62fb435 | 2019-07-18 16:03:38 | [diff] [blame] | 23 | #include "mojo/public/cpp/bindings/receiver.h" |
24 | #include "mojo/public/cpp/bindings/remote.h" | ||||
Ken Rockot | 692493f | 2019-11-18 22:36:57 | [diff] [blame] | 25 | #include "mojo/public/cpp/system/invitation.h" |
[email protected] | 4734d0b | 2011-12-03 07:10:44 | [diff] [blame] | 26 | |
Takashi Sakamoto | 9fe220a8 | 2022-12-12 16:41:13 | [diff] [blame] | 27 | #if BUILDFLAG(IS_ANDROID) |
28 | #include "base/memory/memory_pressure_listener.h" | ||||
29 | #endif | ||||
30 | |||||
[email protected] | 7412204 | 2014-04-25 00:07:30 | [diff] [blame] | 31 | namespace IPC { |
Lei Zhang | 0e004cb | 2021-12-01 16:43:34 | [diff] [blame] | 32 | class Channel; |
Andrea Orru | 966ff88 | 2022-09-08 03:24:24 | [diff] [blame] | 33 | } // namespace IPC |
[email protected] | 7412204 | 2014-04-25 00:07:30 | [diff] [blame] | 34 | |
[email protected] | 4734d0b | 2011-12-03 07:10:44 | [diff] [blame] | 35 | namespace content { |
36 | class 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 Orru | 966ff88 | 2022-09-08 03:24:24 | [diff] [blame] | 41 | class CONTENT_EXPORT ChildProcessHostImpl : public ChildProcessHost, |
42 | public IPC::Listener, | ||||
43 | public mojom::ChildProcessHost { | ||||
[email protected] | 4734d0b | 2011-12-03 07:10:44 | [diff] [blame] | 44 | public: |
Peter Boström | 828b902 | 2021-09-21 02:28:43 | [diff] [blame] | 45 | ChildProcessHostImpl(const ChildProcessHostImpl&) = delete; |
46 | ChildProcessHostImpl& operator=(const ChildProcessHostImpl&) = delete; | ||||
47 | |||||
dcheng | e933b3eb | 2014-10-21 11:44:09 | [diff] [blame] | 48 | ~ChildProcessHostImpl() override; |
[email protected] | 4734d0b | 2011-12-03 07:10:44 | [diff] [blame] | 49 | |
ssid | 1050d80 | 2015-07-28 20:28:14 | [diff] [blame] | 50 | // 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. | ||||
chiniforooshan | 6e4c507 | 2017-03-17 07:56:56 | [diff] [blame] | 60 | // Returns only memory_instrumentation::mojom::kServiceTracingProcessId in |
61 | // single-process mode. | ||||
Emily Andrews | 2eab36a | 2024-12-03 20:15:48 | [diff] [blame] | 62 | static uint64_t ChildProcessIdToTracingProcessId( |
63 | ChildProcessId child_process_id); | ||||
64 | |||||
65 | // TODO(crbug.com/379869738): Deprecated, please use | ||||
66 | // ChildProcessIdToTracingProcessId above. | ||||
avi | a9aa7a8 | 2015-12-25 03:06:31 | [diff] [blame] | 67 | static uint64_t ChildProcessUniqueIdToTracingProcessId(int child_process_id); |
ssid | 1050d80 | 2015-07-28 20:28:14 | [diff] [blame] | 68 | |
[email protected] | 4734d0b | 2011-12-03 07:10:44 | [diff] [blame] | 69 | // ChildProcessHost implementation |
dcheng | e933b3eb | 2014-10-21 11:44:09 | [diff] [blame] | 70 | void ForceShutdown() override; |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 71 | std::optional<mojo::OutgoingInvitation>& GetMojoInvitation() override; |
rockot | da988790 | 2016-08-19 20:46:42 | [diff] [blame] | 72 | void CreateChannelMojo() override; |
dcheng | e933b3eb | 2014-10-21 11:44:09 | [diff] [blame] | 73 | bool IsChannelOpening() override; |
Ken Rockot | ced3127 | 2019-08-02 21:12:18 | [diff] [blame] | 74 | void BindReceiver(mojo::GenericPendingReceiver receiver) override; |
Etienne Pierre-doray | e531336 | 2024-07-18 20:07:24 | [diff] [blame] | 75 | void SetBatterySaverMode(bool battery_saver_mode_enabled) override; |
Ryan Keane | 190beb3 | 2022-06-16 01:08:10 | [diff] [blame] | 76 | |
Georg Neis | 35ff854b | 2024-12-17 02:02:08 | [diff] [blame] | 77 | #if BUILDFLAG(IS_CHROMEOS) |
Andrea Orru | 966ff88 | 2022-09-08 03:24:24 | [diff] [blame] | 78 | void ReinitializeLogging(uint32_t logging_dest, |
79 | base::ScopedFD log_file_descriptor) override; | ||||
80 | #endif | ||||
81 | |||||
Ken Rockot | 9836cfb7 | 2021-06-16 23:16:02 | [diff] [blame] | 82 | base::Process& GetPeerProcess(); |
Ken Rockot | 62fb435 | 2019-07-18 16:03:38 | [diff] [blame] | 83 | mojom::ChildProcess* child_process() { return child_process_.get(); } |
John Abd-El-Malek | c840aa8d | 2018-05-21 22:00:38 | [diff] [blame] | 84 | |
Takashi Sakamoto | 9fe220a8 | 2022-12-12 16:41:13 | [diff] [blame] | 85 | #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] | 4734d0b | 2011-12-03 07:10:44 | [diff] [blame] | 91 | private: |
Ken Rockot | 4f8c3c3 | 2019-08-16 16:32:33 | [diff] [blame] | 92 | friend class content::ChildProcessHost; |
[email protected] | 4734d0b | 2011-12-03 07:10:44 | [diff] [blame] | 93 | |
Tom Sepez | 7bf8900 | 2025-07-17 23:44:04 | [diff] [blame] | 94 | explicit ChildProcessHostImpl(ChildProcessHostDelegate* delegate); |
[email protected] | 4734d0b | 2011-12-03 07:10:44 | [diff] [blame] | 95 | |
Ken Rockot | 4f8c3c3 | 2019-08-16 16:32:33 | [diff] [blame] | 96 | // mojom::ChildProcessHost implementation: |
Ken Rockot | 9836cfb7 | 2021-06-16 23:16:02 | [diff] [blame] | 97 | void Ping(PingCallback callback) override; |
Ken Rockot | 4f8c3c3 | 2019-08-16 16:32:33 | [diff] [blame] | 98 | void BindHostReceiver(mojo::GenericPendingReceiver receiver) override; |
99 | |||||
[email protected] | d84effeb | 2012-06-25 17:03:10 | [diff] [blame] | 100 | // IPC::Listener methods: |
avi | a9aa7a8 | 2015-12-25 03:06:31 | [diff] [blame] | 101 | void OnChannelConnected(int32_t peer_pid) override; |
dcheng | e933b3eb | 2014-10-21 11:44:09 | [diff] [blame] | 102 | void OnChannelError() override; |
103 | void OnBadMessageReceived(const IPC::Message& message) override; | ||||
[email protected] | 4734d0b | 2011-12-03 07:10:44 | [diff] [blame] | 104 | |
amistry | ab52b0c | 2016-06-07 04:22:57 | [diff] [blame] | 105 | // Initializes the IPC channel and returns true on success. |channel_| must be |
106 | // non-null. | ||||
107 | bool InitChannel(); | ||||
108 | |||||
Ken Rockot | 9836cfb7 | 2021-06-16 23:16:02 | [diff] [blame] | 109 | void OnDisconnectedFromChildProcess(); |
110 | |||||
Sebastien Marchand | f552e98 | 2020-09-09 20:19:38 | [diff] [blame] | 111 | #if BUILDFLAG(CLANG_PROFILING_INSIDE_SANDBOX) |
112 | void DumpProfilingData(base::OnceClosure callback) override; | ||||
Will Harris | 6f5f50a | 2021-12-03 17:39:45 | [diff] [blame] | 113 | void SetProfilingFile(base::File file) override; |
Sebastien Marchand | f552e98 | 2020-09-09 20:19:38 | [diff] [blame] | 114 | #endif |
115 | |||||
Ken Rockot | 692493f | 2019-11-18 22:36:57 | [diff] [blame] | 116 | // The outgoing Mojo invitation which must be consumed to bootstrap Mojo IPC |
117 | // to the child process. | ||||
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 118 | std::optional<mojo::OutgoingInvitation> mojo_invitation_{std::in_place}; |
Ken Rockot | 692493f | 2019-11-18 22:36:57 | [diff] [blame] | 119 | |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 120 | raw_ptr<ChildProcessHostDelegate> delegate_; |
rvargas | 5779b38 | 2014-11-18 20:44:11 | [diff] [blame] | 121 | base::Process peer_process_; |
[email protected] | 4734d0b | 2011-12-03 07:10:44 | [diff] [blame] | 122 | bool opening_channel_; // True while we're waiting the channel to be opened. |
dcheng | 4ac58c7a | 2016-04-09 04:51:48 | [diff] [blame] | 123 | std::unique_ptr<IPC::Channel> channel_; |
Ken Rockot | 62fb435 | 2019-07-18 16:03:38 | [diff] [blame] | 124 | mojo::Remote<mojom::ChildProcess> child_process_; |
Ken Rockot | 4f8c3c3 | 2019-08-16 16:32:33 | [diff] [blame] | 125 | mojo::Receiver<mojom::ChildProcessHost> receiver_{this}; |
[email protected] | 4734d0b | 2011-12-03 07:10:44 | [diff] [blame] | 126 | }; |
127 | |||||
128 | } // namespace content | ||||
129 | |||||
Will Harris | cd57b83 | 2023-01-05 20:03:10 | [diff] [blame] | 130 | #endif // CONTENT_BROWSER_CHILD_PROCESS_HOST_IMPL_H_ |