[go: nahoru, domu]

blob: 8d9a0993cc73d9b02967009dcd90403cfe3008b6 [file] [log] [blame]
Avi Drissmanea1be232022-09-14 23:29:061// Copyright 2012 The Chromium Authors
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
Ken Rockot3044d212018-01-23 02:44:3914#include "base/component_export.h"
morritaa409ccc2014-10-20 23:53:2515#include "base/files/scoped_file.h"
Avi Drissmancac43f22023-01-12 00:58:4116#include "base/functional/bind.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"
avi246998d2015-12-22 02:39:0420#include "build/build_config.h"
Takuto Ikutaaa3b796c2019-02-06 02:54:5621#include "ipc/ipc.mojom-forward.h"
dmaclach@chromium.org42ce94e2010-12-08 19:28:0922#include "ipc/ipc_channel_handle.h"
agl@chromium.org946d1b22009-07-22 23:57:2123#include "ipc/ipc_message.h"
sammcf810f07f2016-11-10 22:34:0724#include "ipc/ipc_sender.h"
Ken Rockot493a59f32021-06-04 22:16:5025#include "mojo/public/cpp/bindings/generic_pending_associated_receiver.h"
Julie Jeongeun Kima6c8e672019-12-07 03:27:0526#include "mojo/public/cpp/bindings/pending_associated_receiver.h"
rockot7c6bf952016-07-14 00:34:1127#include "mojo/public/cpp/bindings/scoped_interface_endpoint_handle.h"
Daniel Chengeefad6e32021-02-08 17:56:3428#include "mojo/public/cpp/bindings/shared_remote.h"
brettw@chromium.org57319ce2012-06-11 22:35:2629
Xiaohan Wangab909b32022-01-12 17:57:3930#if BUILDFLAG(IS_POSIX)
avi246998d2015-12-22 02:39:0431#include <sys/types.h>
32#endif
33
initial.commit09911bf2008-07-26 23:55:2934namespace IPC {
35
brettw@chromium.org57319ce2012-06-11 22:35:2636class Listener;
Scott Haseleycf0f1722023-11-03 21:39:0137class UrgentMessageObserver;
brettw@chromium.org57319ce2012-06-11 22:35:2638
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
Peter Kasting7b798b752022-05-06 21:09:5553 using ModeFlags = int;
54 static constexpr ModeFlags MODE_NO_FLAG = 0x0;
55 static constexpr ModeFlags MODE_SERVER_FLAG = 0x1;
56 static constexpr ModeFlags MODE_CLIENT_FLAG = 0x2;
dmaclach@chromium.org1707726c2011-02-03 20:35:0957
58 // Some Standard Modes
morrita@chromium.orge482111a82014-05-30 03:58:5959 // TODO(morrita): These are under deprecation work. You should use Create*()
60 // functions instead.
initial.commit09911bf2008-07-26 23:55:2961 enum Mode {
dmaclach@chromium.org1707726c2011-02-03 20:35:0962 MODE_NONE = MODE_NO_FLAG,
63 MODE_SERVER = MODE_SERVER_FLAG,
64 MODE_CLIENT = MODE_CLIENT_FLAG,
initial.commit09911bf2008-07-26 23:55:2965 };
66
hubbe@chromium.orgdc875dc2013-10-15 00:07:0067 // Messages internal to the IPC implementation are defined here.
avi246998d2015-12-22 02:39:0468 // Uses Maximum value of message type (uint16_t), to avoid conflicting
hubbe@chromium.orgdc875dc2013-10-15 00:07:0069 // with normal message types, which are enumeration constants starting from 0.
brettw@chromium.orgd805c6a2012-03-08 12:30:2870 enum {
hubbe@chromium.orgdc875dc2013-10-15 00:07:0071 // The Hello message is sent by the peer when the channel is connected.
72 // The message contains just the process id (pid).
73 // The message has a special routing_id (MSG_ROUTING_NONE)
74 // and type (HELLO_MESSAGE_TYPE).
tfarina7023f522015-09-11 19:58:4875 HELLO_MESSAGE_TYPE = UINT16_MAX,
hubbe@chromium.orgdc875dc2013-10-15 00:07:0076 // The CLOSE_FD_MESSAGE_TYPE is used in the IPC class to
77 // work around a bug in sendmsg() on Mac. When an FD is sent
78 // over the socket, a CLOSE_FD_MESSAGE is sent with hops = 2.
79 // The client will return the message with hops = 1, *after* it
80 // has received the message that contains the FD. When we
81 // receive it again on the sender side, we close the FD.
82 CLOSE_FD_MESSAGE_TYPE = HELLO_MESSAGE_TYPE - 1
brettw@chromium.orgd805c6a2012-03-08 12:30:2883 };
84
rockot7c6bf952016-07-14 00:34:1185 // Helper interface a Channel may implement to expose support for associated
86 // Mojo interfaces.
Ken Rockot3044d212018-01-23 02:44:3987 class COMPONENT_EXPORT(IPC) AssociatedInterfaceSupport {
rockot7c6bf952016-07-14 00:34:1188 public:
89 using GenericAssociatedInterfaceFactory =
Matt Falkenhagen6140bb12019-11-19 22:52:3690 base::RepeatingCallback<void(mojo::ScopedInterfaceEndpointHandle)>;
rockot7c6bf952016-07-14 00:34:1191
92 virtual ~AssociatedInterfaceSupport() {}
93
rockota628d0b2017-02-09 08:40:1594 // Returns a ThreadSafeForwarded for this channel which can be used to
95 // safely send mojom::Channel requests from arbitrary threads.
96 virtual std::unique_ptr<mojo::ThreadSafeForwarder<mojom::Channel>>
97 CreateThreadSafeChannel() = 0;
98
rockot0e4de5f2016-07-22 21:18:0799 // Adds an interface factory to this channel for interface |name|. Must be
100 // safe to call from any thread.
rockot7c6bf952016-07-14 00:34:11101 virtual void AddGenericAssociatedInterface(
102 const std::string& name,
103 const GenericAssociatedInterfaceFactory& factory) = 0;
104
105 // Requests an associated interface from the remote endpoint.
Ken Rockot493a59f32021-06-04 22:16:50106 virtual void GetRemoteAssociatedInterface(
107 mojo::GenericPendingAssociatedReceiver receiver) = 0;
rockot7c6bf952016-07-14 00:34:11108
Julie Jeongeun Kim3f9e09e2019-09-26 04:56:15109 // Template helper to add an interface factory to this channel.
110 template <typename Interface>
Matt Falkenhagen6140bb12019-11-19 22:52:36111 using AssociatedReceiverFactory = base::RepeatingCallback<void(
112 mojo::PendingAssociatedReceiver<Interface>)>;
Julie Jeongeun Kim3f9e09e2019-09-26 04:56:15113 template <typename Interface>
114 void AddAssociatedInterface(
115 const AssociatedReceiverFactory<Interface>& factory) {
116 AddGenericAssociatedInterface(
117 Interface::Name_,
Matt Falkenhagen6140bb12019-11-19 22:52:36118 base::BindRepeating(&BindPendingAssociatedReceiver<Interface>,
119 factory));
Julie Jeongeun Kim3f9e09e2019-09-26 04:56:15120 }
121
rockot7c6bf952016-07-14 00:34:11122 private:
Julie Jeongeun Kim3f9e09e2019-09-26 04:56:15123 template <typename Interface>
124 static void BindPendingAssociatedReceiver(
125 const AssociatedReceiverFactory<Interface>& factory,
126 mojo::ScopedInterfaceEndpointHandle handle) {
127 factory.Run(
128 mojo::PendingAssociatedReceiver<Interface>(std::move(handle)));
129 }
rockot7c6bf952016-07-14 00:34:11130 };
131
pkasting@chromium.org05094a32011-09-01 00:50:13132 // The maximum message size in bytes. Attempting to receive a message of this
133 // size or bigger results in a channel error.
Ken Rockot8b8c9062017-09-18 19:36:21134 static constexpr size_t kMaximumMessageSize = 128 * 1024 * 1024;
jeremy@chromium.org514411fc2008-12-10 22:28:11135
viettrungluu@chromium.org4c4c0dc2013-01-05 02:13:04136 // Amount of data to read at once from the pipe.
pkasting@chromium.org05094a32011-09-01 00:50:13137 static const size_t kReadBufferSize = 4 * 1024;
initial.commit09911bf2008-07-26 23:55:29138
dskiba06a2e652015-11-04 01:24:59139 // Maximum persistent read buffer size. Read buffer can grow larger to
140 // accommodate large messages, but it's recommended to shrink back to this
141 // value because it fits 99.9% of all messages (see issue 529940 for data).
142 static const size_t kMaximumReadBufferSize = 64 * 1024;
143
initial.commit09911bf2008-07-26 23:55:29144 // Initialize a Channel.
145 //
dmaclach@chromium.org42ce94e2010-12-08 19:28:09146 // |channel_handle| identifies the communication Channel. For POSIX, if
147 // the file descriptor in the channel handle is != -1, the channel takes
148 // ownership of the file descriptor and will close it appropriately, otherwise
149 // it will create a new descriptor internally.
rvargas@google.comc1afbd2c2008-10-13 19:19:36150 // |listener| receives a callback on the current thread for each newly
151 // received message.
initial.commit09911bf2008-07-26 23:55:29152 //
morrita@chromium.orge482111a82014-05-30 03:58:59153 // There are four type of modes how channels operate:
154 //
155 // - Server and named server: In these modes, the Channel is
Joe Downing69ed47f2021-12-13 19:13:00156 // responsible for setting up the IPC object.
morrita@chromium.orge482111a82014-05-30 03:58:59157 // - An "open" named server: It accepts connections from ANY client.
158 // The caller must then implement their own access-control based on the
159 // client process' user Id.
160 // - Client and named client: In these mode, the Channel merely
161 // connects to the already established IPC object.
162 //
163 // Each mode has its own Create*() API to create the Channel object.
danakj03de39b2016-04-23 04:21:09164 static std::unique_ptr<Channel> Create(
165 const IPC::ChannelHandle& channel_handle,
166 Mode mode,
167 Listener* listener);
morrita@chromium.org2f60c9b2014-06-06 20:13:51168
danakj03de39b2016-04-23 04:21:09169 static std::unique_ptr<Channel> CreateClient(
erikchen27aa7d82015-06-16 21:21:04170 const IPC::ChannelHandle& channel_handle,
rockota34707ca2016-07-20 04:28:32171 Listener* listener,
Hajime Hoshife3ecdc2017-11-28 03:34:13172 const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner);
morrita@chromium.orge482111a82014-05-30 03:58:59173
danakj03de39b2016-04-23 04:21:09174 static std::unique_ptr<Channel> CreateServer(
erikchen27aa7d82015-06-16 21:21:04175 const IPC::ChannelHandle& channel_handle,
rockota34707ca2016-07-20 04:28:32176 Listener* listener,
Hajime Hoshife3ecdc2017-11-28 03:34:13177 const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner);
morrita@chromium.orge482111a82014-05-30 03:58:59178
dchengfe61fca2014-10-22 02:29:52179 ~Channel() override;
initial.commit09911bf2008-07-26 23:55:29180
181 // Connect the pipe. On the server side, this will initiate
182 // waiting for connections. On the client, it attempts to
183 // connect to a pre-existing pipe. Note, calling Connect()
184 // will not block the calling thread and may complete
185 // asynchronously.
erikchen90971902016-04-25 23:45:31186 //
187 // The subclass implementation must call WillConnect() at the beginning of its
188 // implementation.
Daniel Chenge618e802022-01-14 18:56:52189 [[nodiscard]] virtual bool Connect() = 0;
initial.commit09911bf2008-07-26 23:55:29190
rockot10188752016-09-08 18:24:56191 // Pause the channel. Subsequent sends will be queued internally until
192 // Unpause() is called and the channel is flushed either by Unpause() or a
193 // subsequent call to Flush().
194 virtual void Pause();
rockot401fb2c2016-09-06 18:35:57195
rockot10188752016-09-08 18:24:56196 // Unpause the channel. This allows subsequent Send() calls to transmit
197 // messages immediately, without queueing. If |flush| is true, any messages
198 // queued while paused will be flushed immediately upon unpausing. Otherwise
199 // you must call Flush() explicitly.
rockot401fb2c2016-09-06 18:35:57200 //
201 // Not all implementations support Unpause(). See ConnectPaused() above for
202 // details.
203 virtual void Unpause(bool flush);
204
205 // Manually flush the pipe. This is only useful exactly once, and only after
206 // a call to Unpause(false), in order to explicitly flush out any
207 // messages which were queued prior to unpausing.
208 //
209 // Not all implementations support Flush(). See ConnectPaused() above for
210 // details.
211 virtual void Flush();
212
initial.commit09911bf2008-07-26 23:55:29213 // Close this Channel explicitly. May be called multiple times.
dmaclach@chromium.org22b42c52010-12-20 06:59:23214 // On POSIX calling close on an IPC channel that listens for connections will
215 // cause it to close any accepted connections, and it will stop listening for
216 // new connections. If you just want to close the currently accepted
217 // connection and listen for new ones, use ResetToAcceptingConnectionState.
morrita@chromium.org2f60c9b2014-06-06 20:13:51218 virtual void Close() = 0;
initial.commit09911bf2008-07-26 23:55:29219
rockot7c6bf952016-07-14 00:34:11220 // Gets a helper for associating Mojo interfaces with this Channel.
221 //
222 // NOTE: Not all implementations support this.
223 virtual AssociatedInterfaceSupport* GetAssociatedInterfaceSupport();
224
thakis6d1bd472014-10-29 00:30:41225 // Overridden from ipc::Sender.
initial.commit09911bf2008-07-26 23:55:29226 // Send a message over the Channel to the listener on the other end.
227 //
rvargas@google.comc1afbd2c2008-10-13 19:19:36228 // |message| must be allocated using operator new. This object will be
229 // deleted once the contents of the Message have been sent.
dcheng1c3d9ac2014-12-23 19:59:59230 bool Send(Message* message) override = 0;
initial.commit09911bf2008-07-26 23:55:29231
Scott Haseleycf0f1722023-11-03 21:39:01232 // Sets the UrgentMessageObserver for this channel. `observer` must outlive
233 // the channel.
234 //
235 // Only channel associated mojo interfaces support urgent messages.
236 virtual void SetUrgentMessageObserver(UrgentMessageObserver* observer);
237
Xiaohan Wang53a511632022-01-21 18:14:58238#if !BUILDFLAG(IS_NACL)
jschuh@chromium.org5c41e6e12012-03-17 02:20:46239 // Generates a channel ID that's non-predictable and unique.
240 static std::string GenerateUniqueRandomChannelID();
bbudge@chromium.orgbccbaf22012-09-28 21:46:01241#endif
jschuh@chromium.org5c41e6e12012-03-17 02:20:46242
Xiaohan Wangab909b32022-01-12 17:57:39243#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
perkjdbcac352014-12-11 17:27:58244 // Sandboxed processes live in a PID namespace, so when sending the IPC hello
245 // message from client to server we need to send the PID from the global
246 // PID namespace.
247 static void SetGlobalPid(int pid);
sammce4d0abd2016-03-07 22:38:04248 static int GetGlobalPid();
perkjdbcac352014-12-11 17:27:58249#endif
250
erikchen5142dc72015-09-10 21:00:18251 protected:
erikchen90971902016-04-25 23:45:31252 // Subclasses must call this method at the beginning of their implementation
253 // of Connect().
254 void WillConnect();
255
256 private:
257 bool did_start_connect_ = false;
initial.commit09911bf2008-07-26 23:55:29258};
259
jeremy@chromium.org514411fc2008-12-10 22:28:11260} // namespace IPC
initial.commit09911bf2008-07-26 23:55:29261
agl@chromium.org946d1b22009-07-22 23:57:21262#endif // IPC_IPC_CHANNEL_H_