brettw@chromium.org | d805c6a | 2012-03-08 12:30:28 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
agl@chromium.org | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 5 | #ifndef IPC_IPC_CHANNEL_H_ |
| 6 | #define IPC_IPC_CHANNEL_H_ |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 7 | |
avi | 246998d | 2015-12-22 02:39:04 | [diff] [blame] | 8 | #include <stddef.h> |
tfarina | 7023f52 | 2015-09-11 19:58:48 | [diff] [blame] | 9 | #include <stdint.h> |
| 10 | |
danakj | 03de39b | 2016-04-23 04:21:09 | [diff] [blame] | 11 | #include <memory> |
jschuh@chromium.org | 5c41e6e1 | 2012-03-17 02:20:46 | [diff] [blame] | 12 | #include <string> |
| 13 | |
Sebastien Marchand | 6d0558fd | 2019-01-25 16:49:37 | [diff] [blame] | 14 | #include "base/bind.h" |
Ken Rockot | 3044d21 | 2018-01-23 02:44:39 | [diff] [blame] | 15 | #include "base/component_export.h" |
morrita | a409ccc | 2014-10-20 23:53:25 | [diff] [blame] | 16 | #include "base/files/scoped_file.h" |
rockot | 8d890f6 | 2016-07-14 16:37:14 | [diff] [blame] | 17 | #include "base/memory/ref_counted.h" |
rsesek@chromium.org | e66ef60 | 2013-07-24 05:15:24 | [diff] [blame] | 18 | #include "base/process/process.h" |
Patrick Monette | 643cdf6 | 2021-10-15 19:13:42 | [diff] [blame] | 19 | #include "base/task/single_thread_task_runner.h" |
rockot | a34707ca | 2016-07-20 04:28:32 | [diff] [blame] | 20 | #include "base/threading/thread_task_runner_handle.h" |
avi | 246998d | 2015-12-22 02:39:04 | [diff] [blame] | 21 | #include "build/build_config.h" |
Takuto Ikuta | aa3b796c | 2019-02-06 02:54:56 | [diff] [blame] | 22 | #include "ipc/ipc.mojom-forward.h" |
dmaclach@chromium.org | 42ce94e | 2010-12-08 19:28:09 | [diff] [blame] | 23 | #include "ipc/ipc_channel_handle.h" |
agl@chromium.org | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 24 | #include "ipc/ipc_message.h" |
sammc | f810f07f | 2016-11-10 22:34:07 | [diff] [blame] | 25 | #include "ipc/ipc_sender.h" |
Ken Rockot | 493a59f3 | 2021-06-04 22:16:50 | [diff] [blame] | 26 | #include "mojo/public/cpp/bindings/generic_pending_associated_receiver.h" |
Julie Jeongeun Kim | a6c8e67 | 2019-12-07 03:27:05 | [diff] [blame] | 27 | #include "mojo/public/cpp/bindings/pending_associated_receiver.h" |
rockot | 7c6bf95 | 2016-07-14 00:34:11 | [diff] [blame] | 28 | #include "mojo/public/cpp/bindings/scoped_interface_endpoint_handle.h" |
Daniel Cheng | eefad6e3 | 2021-02-08 17:56:34 | [diff] [blame] | 29 | #include "mojo/public/cpp/bindings/shared_remote.h" |
brettw@chromium.org | 57319ce | 2012-06-11 22:35:26 | [diff] [blame] | 30 | |
Xiaohan Wang | ab909b3 | 2022-01-12 17:57:39 | [diff] [blame] | 31 | #if BUILDFLAG(IS_POSIX) |
avi | 246998d | 2015-12-22 02:39:04 | [diff] [blame] | 32 | #include <sys/types.h> |
| 33 | #endif |
| 34 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 35 | namespace IPC { |
| 36 | |
brettw@chromium.org | 57319ce | 2012-06-11 22:35:26 | [diff] [blame] | 37 | class Listener; |
| 38 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 39 | //------------------------------------------------------------------------------ |
dmaclach@chromium.org | 22b42c5 | 2010-12-20 06:59:23 | [diff] [blame] | 40 | // See |
| 41 | // http://www.chromium.org/developers/design-documents/inter-process-communication |
| 42 | // for overview of IPC in Chromium. |
| 43 | |
sammc | beeed368 | 2016-11-08 23:24:35 | [diff] [blame] | 44 | // Channels are implemented using mojo message pipes on all platforms other |
| 45 | // than NaCl. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 46 | |
Ken Rockot | 3044d21 | 2018-01-23 02:44:39 | [diff] [blame] | 47 | class COMPONENT_EXPORT(IPC) Channel : public Sender { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 48 | // Security tests need access to the pipe handle. |
| 49 | friend class ChannelTest; |
| 50 | |
| 51 | public: |
dmaclach@chromium.org | 1707726c | 2011-02-03 20:35:09 | [diff] [blame] | 52 | // Flags to test modes |
| 53 | enum ModeFlags { |
| 54 | MODE_NO_FLAG = 0x0, |
| 55 | MODE_SERVER_FLAG = 0x1, |
| 56 | MODE_CLIENT_FLAG = 0x2, |
dmaclach@chromium.org | 1707726c | 2011-02-03 20:35:09 | [diff] [blame] | 57 | }; |
| 58 | |
| 59 | // Some Standard Modes |
morrita@chromium.org | e482111a8 | 2014-05-30 03:58:59 | [diff] [blame] | 60 | // TODO(morrita): These are under deprecation work. You should use Create*() |
| 61 | // functions instead. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 62 | enum Mode { |
dmaclach@chromium.org | 1707726c | 2011-02-03 20:35:09 | [diff] [blame] | 63 | MODE_NONE = MODE_NO_FLAG, |
| 64 | MODE_SERVER = MODE_SERVER_FLAG, |
| 65 | MODE_CLIENT = MODE_CLIENT_FLAG, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 66 | }; |
| 67 | |
hubbe@chromium.org | dc875dc | 2013-10-15 00:07:00 | [diff] [blame] | 68 | // Messages internal to the IPC implementation are defined here. |
avi | 246998d | 2015-12-22 02:39:04 | [diff] [blame] | 69 | // Uses Maximum value of message type (uint16_t), to avoid conflicting |
hubbe@chromium.org | dc875dc | 2013-10-15 00:07:00 | [diff] [blame] | 70 | // with normal message types, which are enumeration constants starting from 0. |
brettw@chromium.org | d805c6a | 2012-03-08 12:30:28 | [diff] [blame] | 71 | enum { |
hubbe@chromium.org | dc875dc | 2013-10-15 00:07:00 | [diff] [blame] | 72 | // The Hello message is sent by the peer when the channel is connected. |
| 73 | // The message contains just the process id (pid). |
| 74 | // The message has a special routing_id (MSG_ROUTING_NONE) |
| 75 | // and type (HELLO_MESSAGE_TYPE). |
tfarina | 7023f52 | 2015-09-11 19:58:48 | [diff] [blame] | 76 | HELLO_MESSAGE_TYPE = UINT16_MAX, |
hubbe@chromium.org | dc875dc | 2013-10-15 00:07:00 | [diff] [blame] | 77 | // The CLOSE_FD_MESSAGE_TYPE is used in the IPC class to |
| 78 | // work around a bug in sendmsg() on Mac. When an FD is sent |
| 79 | // over the socket, a CLOSE_FD_MESSAGE is sent with hops = 2. |
| 80 | // The client will return the message with hops = 1, *after* it |
| 81 | // has received the message that contains the FD. When we |
| 82 | // receive it again on the sender side, we close the FD. |
| 83 | CLOSE_FD_MESSAGE_TYPE = HELLO_MESSAGE_TYPE - 1 |
brettw@chromium.org | d805c6a | 2012-03-08 12:30:28 | [diff] [blame] | 84 | }; |
| 85 | |
rockot | 7c6bf95 | 2016-07-14 00:34:11 | [diff] [blame] | 86 | // Helper interface a Channel may implement to expose support for associated |
| 87 | // Mojo interfaces. |
Ken Rockot | 3044d21 | 2018-01-23 02:44:39 | [diff] [blame] | 88 | class COMPONENT_EXPORT(IPC) AssociatedInterfaceSupport { |
rockot | 7c6bf95 | 2016-07-14 00:34:11 | [diff] [blame] | 89 | public: |
| 90 | using GenericAssociatedInterfaceFactory = |
Matt Falkenhagen | 6140bb1 | 2019-11-19 22:52:36 | [diff] [blame] | 91 | base::RepeatingCallback<void(mojo::ScopedInterfaceEndpointHandle)>; |
rockot | 7c6bf95 | 2016-07-14 00:34:11 | [diff] [blame] | 92 | |
| 93 | virtual ~AssociatedInterfaceSupport() {} |
| 94 | |
rockot | a628d0b | 2017-02-09 08:40:15 | [diff] [blame] | 95 | // Returns a ThreadSafeForwarded for this channel which can be used to |
| 96 | // safely send mojom::Channel requests from arbitrary threads. |
| 97 | virtual std::unique_ptr<mojo::ThreadSafeForwarder<mojom::Channel>> |
| 98 | CreateThreadSafeChannel() = 0; |
| 99 | |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 100 | // Adds an interface factory to this channel for interface |name|. Must be |
| 101 | // safe to call from any thread. |
rockot | 7c6bf95 | 2016-07-14 00:34:11 | [diff] [blame] | 102 | virtual void AddGenericAssociatedInterface( |
| 103 | const std::string& name, |
| 104 | const GenericAssociatedInterfaceFactory& factory) = 0; |
| 105 | |
| 106 | // Requests an associated interface from the remote endpoint. |
Ken Rockot | 493a59f3 | 2021-06-04 22:16:50 | [diff] [blame] | 107 | virtual void GetRemoteAssociatedInterface( |
| 108 | mojo::GenericPendingAssociatedReceiver receiver) = 0; |
rockot | 7c6bf95 | 2016-07-14 00:34:11 | [diff] [blame] | 109 | |
Julie Jeongeun Kim | 3f9e09e | 2019-09-26 04:56:15 | [diff] [blame] | 110 | // Template helper to add an interface factory to this channel. |
| 111 | template <typename Interface> |
Matt Falkenhagen | 6140bb1 | 2019-11-19 22:52:36 | [diff] [blame] | 112 | using AssociatedReceiverFactory = base::RepeatingCallback<void( |
| 113 | mojo::PendingAssociatedReceiver<Interface>)>; |
Julie Jeongeun Kim | 3f9e09e | 2019-09-26 04:56:15 | [diff] [blame] | 114 | template <typename Interface> |
| 115 | void AddAssociatedInterface( |
| 116 | const AssociatedReceiverFactory<Interface>& factory) { |
| 117 | AddGenericAssociatedInterface( |
| 118 | Interface::Name_, |
Matt Falkenhagen | 6140bb1 | 2019-11-19 22:52:36 | [diff] [blame] | 119 | base::BindRepeating(&BindPendingAssociatedReceiver<Interface>, |
| 120 | factory)); |
Julie Jeongeun Kim | 3f9e09e | 2019-09-26 04:56:15 | [diff] [blame] | 121 | } |
| 122 | |
rockot | 7c6bf95 | 2016-07-14 00:34:11 | [diff] [blame] | 123 | private: |
Julie Jeongeun Kim | 3f9e09e | 2019-09-26 04:56:15 | [diff] [blame] | 124 | template <typename Interface> |
| 125 | static void BindPendingAssociatedReceiver( |
| 126 | const AssociatedReceiverFactory<Interface>& factory, |
| 127 | mojo::ScopedInterfaceEndpointHandle handle) { |
| 128 | factory.Run( |
| 129 | mojo::PendingAssociatedReceiver<Interface>(std::move(handle))); |
| 130 | } |
rockot | 7c6bf95 | 2016-07-14 00:34:11 | [diff] [blame] | 131 | }; |
| 132 | |
pkasting@chromium.org | 05094a3 | 2011-09-01 00:50:13 | [diff] [blame] | 133 | // The maximum message size in bytes. Attempting to receive a message of this |
| 134 | // size or bigger results in a channel error. |
Ken Rockot | 8b8c906 | 2017-09-18 19:36:21 | [diff] [blame] | 135 | static constexpr size_t kMaximumMessageSize = 128 * 1024 * 1024; |
jeremy@chromium.org | 514411fc | 2008-12-10 22:28:11 | [diff] [blame] | 136 | |
viettrungluu@chromium.org | 4c4c0dc | 2013-01-05 02:13:04 | [diff] [blame] | 137 | // Amount of data to read at once from the pipe. |
pkasting@chromium.org | 05094a3 | 2011-09-01 00:50:13 | [diff] [blame] | 138 | static const size_t kReadBufferSize = 4 * 1024; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 139 | |
dskiba | 06a2e65 | 2015-11-04 01:24:59 | [diff] [blame] | 140 | // Maximum persistent read buffer size. Read buffer can grow larger to |
| 141 | // accommodate large messages, but it's recommended to shrink back to this |
| 142 | // value because it fits 99.9% of all messages (see issue 529940 for data). |
| 143 | static const size_t kMaximumReadBufferSize = 64 * 1024; |
| 144 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 145 | // Initialize a Channel. |
| 146 | // |
dmaclach@chromium.org | 42ce94e | 2010-12-08 19:28:09 | [diff] [blame] | 147 | // |channel_handle| identifies the communication Channel. For POSIX, if |
| 148 | // the file descriptor in the channel handle is != -1, the channel takes |
| 149 | // ownership of the file descriptor and will close it appropriately, otherwise |
| 150 | // it will create a new descriptor internally. |
rvargas@google.com | c1afbd2c | 2008-10-13 19:19:36 | [diff] [blame] | 151 | // |listener| receives a callback on the current thread for each newly |
| 152 | // received message. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 153 | // |
morrita@chromium.org | e482111a8 | 2014-05-30 03:58:59 | [diff] [blame] | 154 | // There are four type of modes how channels operate: |
| 155 | // |
| 156 | // - Server and named server: In these modes, the Channel is |
Joe Downing | 69ed47f | 2021-12-13 19:13:00 | [diff] [blame] | 157 | // responsible for setting up the IPC object. |
morrita@chromium.org | e482111a8 | 2014-05-30 03:58:59 | [diff] [blame] | 158 | // - An "open" named server: It accepts connections from ANY client. |
| 159 | // The caller must then implement their own access-control based on the |
| 160 | // client process' user Id. |
| 161 | // - Client and named client: In these mode, the Channel merely |
| 162 | // connects to the already established IPC object. |
| 163 | // |
| 164 | // Each mode has its own Create*() API to create the Channel object. |
danakj | 03de39b | 2016-04-23 04:21:09 | [diff] [blame] | 165 | static std::unique_ptr<Channel> Create( |
| 166 | const IPC::ChannelHandle& channel_handle, |
| 167 | Mode mode, |
| 168 | Listener* listener); |
morrita@chromium.org | 2f60c9b | 2014-06-06 20:13:51 | [diff] [blame] | 169 | |
danakj | 03de39b | 2016-04-23 04:21:09 | [diff] [blame] | 170 | static std::unique_ptr<Channel> CreateClient( |
erikchen | 27aa7d8 | 2015-06-16 21:21:04 | [diff] [blame] | 171 | const IPC::ChannelHandle& channel_handle, |
rockot | a34707ca | 2016-07-20 04:28:32 | [diff] [blame] | 172 | Listener* listener, |
Hajime Hoshi | fe3ecdc | 2017-11-28 03:34:13 | [diff] [blame] | 173 | const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner); |
morrita@chromium.org | e482111a8 | 2014-05-30 03:58:59 | [diff] [blame] | 174 | |
danakj | 03de39b | 2016-04-23 04:21:09 | [diff] [blame] | 175 | static std::unique_ptr<Channel> CreateServer( |
erikchen | 27aa7d8 | 2015-06-16 21:21:04 | [diff] [blame] | 176 | const IPC::ChannelHandle& channel_handle, |
rockot | a34707ca | 2016-07-20 04:28:32 | [diff] [blame] | 177 | Listener* listener, |
Hajime Hoshi | fe3ecdc | 2017-11-28 03:34:13 | [diff] [blame] | 178 | const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner); |
morrita@chromium.org | e482111a8 | 2014-05-30 03:58:59 | [diff] [blame] | 179 | |
dcheng | fe61fca | 2014-10-22 02:29:52 | [diff] [blame] | 180 | ~Channel() override; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 181 | |
| 182 | // Connect the pipe. On the server side, this will initiate |
| 183 | // waiting for connections. On the client, it attempts to |
| 184 | // connect to a pre-existing pipe. Note, calling Connect() |
| 185 | // will not block the calling thread and may complete |
| 186 | // asynchronously. |
erikchen | 9097190 | 2016-04-25 23:45:31 | [diff] [blame] | 187 | // |
| 188 | // The subclass implementation must call WillConnect() at the beginning of its |
| 189 | // implementation. |
Daniel Cheng | e618e80 | 2022-01-14 18:56:52 | [diff] [blame] | 190 | [[nodiscard]] virtual bool Connect() = 0; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 191 | |
rockot | 1018875 | 2016-09-08 18:24:56 | [diff] [blame] | 192 | // Pause the channel. Subsequent sends will be queued internally until |
| 193 | // Unpause() is called and the channel is flushed either by Unpause() or a |
| 194 | // subsequent call to Flush(). |
| 195 | virtual void Pause(); |
rockot | 401fb2c | 2016-09-06 18:35:57 | [diff] [blame] | 196 | |
rockot | 1018875 | 2016-09-08 18:24:56 | [diff] [blame] | 197 | // Unpause the channel. This allows subsequent Send() calls to transmit |
| 198 | // messages immediately, without queueing. If |flush| is true, any messages |
| 199 | // queued while paused will be flushed immediately upon unpausing. Otherwise |
| 200 | // you must call Flush() explicitly. |
rockot | 401fb2c | 2016-09-06 18:35:57 | [diff] [blame] | 201 | // |
| 202 | // Not all implementations support Unpause(). See ConnectPaused() above for |
| 203 | // details. |
| 204 | virtual void Unpause(bool flush); |
| 205 | |
| 206 | // Manually flush the pipe. This is only useful exactly once, and only after |
| 207 | // a call to Unpause(false), in order to explicitly flush out any |
| 208 | // messages which were queued prior to unpausing. |
| 209 | // |
| 210 | // Not all implementations support Flush(). See ConnectPaused() above for |
| 211 | // details. |
| 212 | virtual void Flush(); |
| 213 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 214 | // Close this Channel explicitly. May be called multiple times. |
dmaclach@chromium.org | 22b42c5 | 2010-12-20 06:59:23 | [diff] [blame] | 215 | // On POSIX calling close on an IPC channel that listens for connections will |
| 216 | // cause it to close any accepted connections, and it will stop listening for |
| 217 | // new connections. If you just want to close the currently accepted |
| 218 | // connection and listen for new ones, use ResetToAcceptingConnectionState. |
morrita@chromium.org | 2f60c9b | 2014-06-06 20:13:51 | [diff] [blame] | 219 | virtual void Close() = 0; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 220 | |
rockot | 7c6bf95 | 2016-07-14 00:34:11 | [diff] [blame] | 221 | // Gets a helper for associating Mojo interfaces with this Channel. |
| 222 | // |
| 223 | // NOTE: Not all implementations support this. |
| 224 | virtual AssociatedInterfaceSupport* GetAssociatedInterfaceSupport(); |
| 225 | |
thakis | 6d1bd47 | 2014-10-29 00:30:41 | [diff] [blame] | 226 | // Overridden from ipc::Sender. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 227 | // Send a message over the Channel to the listener on the other end. |
| 228 | // |
rvargas@google.com | c1afbd2c | 2008-10-13 19:19:36 | [diff] [blame] | 229 | // |message| must be allocated using operator new. This object will be |
| 230 | // deleted once the contents of the Message have been sent. |
dcheng | 1c3d9ac | 2014-12-23 19:59:59 | [diff] [blame] | 231 | bool Send(Message* message) override = 0; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 232 | |
Xiaohan Wang | 53a51163 | 2022-01-21 18:14:58 | [diff] [blame^] | 233 | #if !BUILDFLAG(IS_NACL) |
jschuh@chromium.org | 5c41e6e1 | 2012-03-17 02:20:46 | [diff] [blame] | 234 | // Generates a channel ID that's non-predictable and unique. |
| 235 | static std::string GenerateUniqueRandomChannelID(); |
bbudge@chromium.org | bccbaf2 | 2012-09-28 21:46:01 | [diff] [blame] | 236 | #endif |
jschuh@chromium.org | 5c41e6e1 | 2012-03-17 02:20:46 | [diff] [blame] | 237 | |
Xiaohan Wang | ab909b3 | 2022-01-12 17:57:39 | [diff] [blame] | 238 | #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) |
perkj | dbcac35 | 2014-12-11 17:27:58 | [diff] [blame] | 239 | // Sandboxed processes live in a PID namespace, so when sending the IPC hello |
| 240 | // message from client to server we need to send the PID from the global |
| 241 | // PID namespace. |
| 242 | static void SetGlobalPid(int pid); |
sammc | e4d0abd | 2016-03-07 22:38:04 | [diff] [blame] | 243 | static int GetGlobalPid(); |
perkj | dbcac35 | 2014-12-11 17:27:58 | [diff] [blame] | 244 | #endif |
| 245 | |
erikchen | 5142dc7 | 2015-09-10 21:00:18 | [diff] [blame] | 246 | protected: |
erikchen | 9097190 | 2016-04-25 23:45:31 | [diff] [blame] | 247 | // Subclasses must call this method at the beginning of their implementation |
| 248 | // of Connect(). |
| 249 | void WillConnect(); |
| 250 | |
| 251 | private: |
| 252 | bool did_start_connect_ = false; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 253 | }; |
| 254 | |
jeremy@chromium.org | 514411fc | 2008-12-10 22:28:11 | [diff] [blame] | 255 | } // namespace IPC |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 256 | |
agl@chromium.org | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 257 | #endif // IPC_IPC_CHANNEL_H_ |