[go: nahoru, domu]

blob: 16463847980bf0c3bac14eb38514bfacd765a406 [file] [log] [blame]
brettw@chromium.orgd805c6a2012-03-08 12:30:281// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
agl@chromium.org946d1b22009-07-22 23:57:215#ifndef IPC_IPC_CHANNEL_H_
6#define IPC_IPC_CHANNEL_H_
initial.commit09911bf2008-07-26 23:55:297
avi246998d2015-12-22 02:39:048#include <stddef.h>
tfarina7023f522015-09-11 19:58:489#include <stdint.h>
10
danakj03de39b2016-04-23 04:21:0911#include <memory>
jschuh@chromium.org5c41e6e12012-03-17 02:20:4612#include <string>
13
Sebastien Marchand6d0558fd2019-01-25 16:49:3714#include "base/bind.h"
Ken Rockot3044d212018-01-23 02:44:3915#include "base/component_export.h"
morritaa409ccc2014-10-20 23:53:2516#include "base/files/scoped_file.h"
rockot8d890f62016-07-14 16:37:1417#include "base/memory/ref_counted.h"
rsesek@chromium.orge66ef602013-07-24 05:15:2418#include "base/process/process.h"
Patrick Monette643cdf62021-10-15 19:13:4219#include "base/task/single_thread_task_runner.h"
rockota34707ca2016-07-20 04:28:3220#include "base/threading/thread_task_runner_handle.h"
avi246998d2015-12-22 02:39:0421#include "build/build_config.h"
Takuto Ikutaaa3b796c2019-02-06 02:54:5622#include "ipc/ipc.mojom-forward.h"
dmaclach@chromium.org42ce94e2010-12-08 19:28:0923#include "ipc/ipc_channel_handle.h"
agl@chromium.org946d1b22009-07-22 23:57:2124#include "ipc/ipc_message.h"
sammcf810f07f2016-11-10 22:34:0725#include "ipc/ipc_sender.h"
Ken Rockot493a59f32021-06-04 22:16:5026#include "mojo/public/cpp/bindings/generic_pending_associated_receiver.h"
Julie Jeongeun Kima6c8e672019-12-07 03:27:0527#include "mojo/public/cpp/bindings/pending_associated_receiver.h"
rockot7c6bf952016-07-14 00:34:1128#include "mojo/public/cpp/bindings/scoped_interface_endpoint_handle.h"
Daniel Chengeefad6e32021-02-08 17:56:3429#include "mojo/public/cpp/bindings/shared_remote.h"
brettw@chromium.org57319ce2012-06-11 22:35:2630
Xiaohan Wangab909b32022-01-12 17:57:3931#if BUILDFLAG(IS_POSIX)
avi246998d2015-12-22 02:39:0432#include <sys/types.h>
33#endif
34
initial.commit09911bf2008-07-26 23:55:2935namespace IPC {
36
brettw@chromium.org57319ce2012-06-11 22:35:2637class Listener;
38
initial.commit09911bf2008-07-26 23:55:2939//------------------------------------------------------------------------------
dmaclach@chromium.org22b42c52010-12-20 06:59:2340// See
41// http://www.chromium.org/developers/design-documents/inter-process-communication
42// for overview of IPC in Chromium.
43
sammcbeeed3682016-11-08 23:24:3544// Channels are implemented using mojo message pipes on all platforms other
45// than NaCl.
initial.commit09911bf2008-07-26 23:55:2946
Ken Rockot3044d212018-01-23 02:44:3947class COMPONENT_EXPORT(IPC) Channel : public Sender {
initial.commit09911bf2008-07-26 23:55:2948 // Security tests need access to the pipe handle.
49 friend class ChannelTest;
50
51 public:
dmaclach@chromium.org1707726c2011-02-03 20:35:0952 // Flags to test modes
53 enum ModeFlags {
54 MODE_NO_FLAG = 0x0,
55 MODE_SERVER_FLAG = 0x1,
56 MODE_CLIENT_FLAG = 0x2,
dmaclach@chromium.org1707726c2011-02-03 20:35:0957 };
58
59 // Some Standard Modes
morrita@chromium.orge482111a82014-05-30 03:58:5960 // TODO(morrita): These are under deprecation work. You should use Create*()
61 // functions instead.
initial.commit09911bf2008-07-26 23:55:2962 enum Mode {
dmaclach@chromium.org1707726c2011-02-03 20:35:0963 MODE_NONE = MODE_NO_FLAG,
64 MODE_SERVER = MODE_SERVER_FLAG,
65 MODE_CLIENT = MODE_CLIENT_FLAG,
initial.commit09911bf2008-07-26 23:55:2966 };
67
hubbe@chromium.orgdc875dc2013-10-15 00:07:0068 // Messages internal to the IPC implementation are defined here.
avi246998d2015-12-22 02:39:0469 // Uses Maximum value of message type (uint16_t), to avoid conflicting
hubbe@chromium.orgdc875dc2013-10-15 00:07:0070 // with normal message types, which are enumeration constants starting from 0.
brettw@chromium.orgd805c6a2012-03-08 12:30:2871 enum {
hubbe@chromium.orgdc875dc2013-10-15 00:07:0072 // 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).
tfarina7023f522015-09-11 19:58:4876 HELLO_MESSAGE_TYPE = UINT16_MAX,
hubbe@chromium.orgdc875dc2013-10-15 00:07:0077 // 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.orgd805c6a2012-03-08 12:30:2884 };
85
rockot7c6bf952016-07-14 00:34:1186 // Helper interface a Channel may implement to expose support for associated
87 // Mojo interfaces.
Ken Rockot3044d212018-01-23 02:44:3988 class COMPONENT_EXPORT(IPC) AssociatedInterfaceSupport {
rockot7c6bf952016-07-14 00:34:1189 public:
90 using GenericAssociatedInterfaceFactory =
Matt Falkenhagen6140bb12019-11-19 22:52:3691 base::RepeatingCallback<void(mojo::ScopedInterfaceEndpointHandle)>;
rockot7c6bf952016-07-14 00:34:1192
93 virtual ~AssociatedInterfaceSupport() {}
94
rockota628d0b2017-02-09 08:40:1595 // 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
rockot0e4de5f2016-07-22 21:18:07100 // Adds an interface factory to this channel for interface |name|. Must be
101 // safe to call from any thread.
rockot7c6bf952016-07-14 00:34:11102 virtual void AddGenericAssociatedInterface(
103 const std::string& name,
104 const GenericAssociatedInterfaceFactory& factory) = 0;
105
106 // Requests an associated interface from the remote endpoint.
Ken Rockot493a59f32021-06-04 22:16:50107 virtual void GetRemoteAssociatedInterface(
108 mojo::GenericPendingAssociatedReceiver receiver) = 0;
rockot7c6bf952016-07-14 00:34:11109
Julie Jeongeun Kim3f9e09e2019-09-26 04:56:15110 // Template helper to add an interface factory to this channel.
111 template <typename Interface>
Matt Falkenhagen6140bb12019-11-19 22:52:36112 using AssociatedReceiverFactory = base::RepeatingCallback<void(
113 mojo::PendingAssociatedReceiver<Interface>)>;
Julie Jeongeun Kim3f9e09e2019-09-26 04:56:15114 template <typename Interface>
115 void AddAssociatedInterface(
116 const AssociatedReceiverFactory<Interface>& factory) {
117 AddGenericAssociatedInterface(
118 Interface::Name_,
Matt Falkenhagen6140bb12019-11-19 22:52:36119 base::BindRepeating(&BindPendingAssociatedReceiver<Interface>,
120 factory));
Julie Jeongeun Kim3f9e09e2019-09-26 04:56:15121 }
122
rockot7c6bf952016-07-14 00:34:11123 private:
Julie Jeongeun Kim3f9e09e2019-09-26 04:56:15124 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 }
rockot7c6bf952016-07-14 00:34:11131 };
132
pkasting@chromium.org05094a32011-09-01 00:50:13133 // The maximum message size in bytes. Attempting to receive a message of this
134 // size or bigger results in a channel error.
Ken Rockot8b8c9062017-09-18 19:36:21135 static constexpr size_t kMaximumMessageSize = 128 * 1024 * 1024;
jeremy@chromium.org514411fc2008-12-10 22:28:11136
viettrungluu@chromium.org4c4c0dc2013-01-05 02:13:04137 // Amount of data to read at once from the pipe.
pkasting@chromium.org05094a32011-09-01 00:50:13138 static const size_t kReadBufferSize = 4 * 1024;
initial.commit09911bf2008-07-26 23:55:29139
dskiba06a2e652015-11-04 01:24:59140 // 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.commit09911bf2008-07-26 23:55:29145 // Initialize a Channel.
146 //
dmaclach@chromium.org42ce94e2010-12-08 19:28:09147 // |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.comc1afbd2c2008-10-13 19:19:36151 // |listener| receives a callback on the current thread for each newly
152 // received message.
initial.commit09911bf2008-07-26 23:55:29153 //
morrita@chromium.orge482111a82014-05-30 03:58:59154 // There are four type of modes how channels operate:
155 //
156 // - Server and named server: In these modes, the Channel is
Joe Downing69ed47f2021-12-13 19:13:00157 // responsible for setting up the IPC object.
morrita@chromium.orge482111a82014-05-30 03:58:59158 // - 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.
danakj03de39b2016-04-23 04:21:09165 static std::unique_ptr<Channel> Create(
166 const IPC::ChannelHandle& channel_handle,
167 Mode mode,
168 Listener* listener);
morrita@chromium.org2f60c9b2014-06-06 20:13:51169
danakj03de39b2016-04-23 04:21:09170 static std::unique_ptr<Channel> CreateClient(
erikchen27aa7d82015-06-16 21:21:04171 const IPC::ChannelHandle& channel_handle,
rockota34707ca2016-07-20 04:28:32172 Listener* listener,
Hajime Hoshife3ecdc2017-11-28 03:34:13173 const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner);
morrita@chromium.orge482111a82014-05-30 03:58:59174
danakj03de39b2016-04-23 04:21:09175 static std::unique_ptr<Channel> CreateServer(
erikchen27aa7d82015-06-16 21:21:04176 const IPC::ChannelHandle& channel_handle,
rockota34707ca2016-07-20 04:28:32177 Listener* listener,
Hajime Hoshife3ecdc2017-11-28 03:34:13178 const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner);
morrita@chromium.orge482111a82014-05-30 03:58:59179
dchengfe61fca2014-10-22 02:29:52180 ~Channel() override;
initial.commit09911bf2008-07-26 23:55:29181
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.
erikchen90971902016-04-25 23:45:31187 //
188 // The subclass implementation must call WillConnect() at the beginning of its
189 // implementation.
Daniel Chenge618e802022-01-14 18:56:52190 [[nodiscard]] virtual bool Connect() = 0;
initial.commit09911bf2008-07-26 23:55:29191
rockot10188752016-09-08 18:24:56192 // 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();
rockot401fb2c2016-09-06 18:35:57196
rockot10188752016-09-08 18:24:56197 // 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.
rockot401fb2c2016-09-06 18:35:57201 //
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.commit09911bf2008-07-26 23:55:29214 // Close this Channel explicitly. May be called multiple times.
dmaclach@chromium.org22b42c52010-12-20 06:59:23215 // 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.org2f60c9b2014-06-06 20:13:51219 virtual void Close() = 0;
initial.commit09911bf2008-07-26 23:55:29220
rockot7c6bf952016-07-14 00:34:11221 // Gets a helper for associating Mojo interfaces with this Channel.
222 //
223 // NOTE: Not all implementations support this.
224 virtual AssociatedInterfaceSupport* GetAssociatedInterfaceSupport();
225
thakis6d1bd472014-10-29 00:30:41226 // Overridden from ipc::Sender.
initial.commit09911bf2008-07-26 23:55:29227 // Send a message over the Channel to the listener on the other end.
228 //
rvargas@google.comc1afbd2c2008-10-13 19:19:36229 // |message| must be allocated using operator new. This object will be
230 // deleted once the contents of the Message have been sent.
dcheng1c3d9ac2014-12-23 19:59:59231 bool Send(Message* message) override = 0;
initial.commit09911bf2008-07-26 23:55:29232
Xiaohan Wang53a511632022-01-21 18:14:58233#if !BUILDFLAG(IS_NACL)
jschuh@chromium.org5c41e6e12012-03-17 02:20:46234 // Generates a channel ID that's non-predictable and unique.
235 static std::string GenerateUniqueRandomChannelID();
bbudge@chromium.orgbccbaf22012-09-28 21:46:01236#endif
jschuh@chromium.org5c41e6e12012-03-17 02:20:46237
Xiaohan Wangab909b32022-01-12 17:57:39238#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
perkjdbcac352014-12-11 17:27:58239 // 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);
sammce4d0abd2016-03-07 22:38:04243 static int GetGlobalPid();
perkjdbcac352014-12-11 17:27:58244#endif
245
erikchen5142dc72015-09-10 21:00:18246 protected:
erikchen90971902016-04-25 23:45:31247 // 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.commit09911bf2008-07-26 23:55:29253};
254
jeremy@chromium.org514411fc2008-12-10 22:28:11255} // namespace IPC
initial.commit09911bf2008-07-26 23:55:29256
agl@chromium.org946d1b22009-07-22 23:57:21257#endif // IPC_IPC_CHANNEL_H_