[go: nahoru, domu]

blob: 91b184cec8c8e220ba272466c1d6b838b6309e51 [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"
evan@chromium.org39703fb2010-10-19 19:11:1515#include "base/compiler_specific.h"
Ken Rockot3044d212018-01-23 02:44:3916#include "base/component_export.h"
morritaa409ccc2014-10-20 23:53:2517#include "base/files/scoped_file.h"
rockot8d890f62016-07-14 16:37:1418#include "base/memory/ref_counted.h"
rsesek@chromium.orge66ef602013-07-24 05:15:2419#include "base/process/process.h"
rockot8d890f62016-07-14 16:37:1420#include "base/single_thread_task_runner.h"
rockota34707ca2016-07-20 04:28:3221#include "base/threading/thread_task_runner_handle.h"
avi246998d2015-12-22 02:39:0422#include "build/build_config.h"
rockota628d0b2017-02-09 08:40:1523#include "ipc/ipc.mojom.h"
dmaclach@chromium.org42ce94e2010-12-08 19:28:0924#include "ipc/ipc_channel_handle.h"
agl@chromium.org946d1b22009-07-22 23:57:2125#include "ipc/ipc_message.h"
sammcf810f07f2016-11-10 22:34:0726#include "ipc/ipc_sender.h"
rockot7c6bf952016-07-14 00:34:1127#include "mojo/public/cpp/bindings/associated_interface_ptr.h"
28#include "mojo/public/cpp/bindings/associated_interface_request.h"
29#include "mojo/public/cpp/bindings/scoped_interface_endpoint_handle.h"
rockota628d0b2017-02-09 08:40:1530#include "mojo/public/cpp/bindings/thread_safe_interface_ptr.h"
brettw@chromium.org57319ce2012-06-11 22:35:2631
avi246998d2015-12-22 02:39:0432#if defined(OS_POSIX)
33#include <sys/types.h>
34#endif
35
initial.commit09911bf2008-07-26 23:55:2936namespace IPC {
37
brettw@chromium.org57319ce2012-06-11 22:35:2638class Listener;
39
initial.commit09911bf2008-07-26 23:55:2940//------------------------------------------------------------------------------
dmaclach@chromium.org22b42c52010-12-20 06:59:2341// See
42// http://www.chromium.org/developers/design-documents/inter-process-communication
43// for overview of IPC in Chromium.
44
sammcbeeed3682016-11-08 23:24:3545// Channels are implemented using mojo message pipes on all platforms other
46// than NaCl.
initial.commit09911bf2008-07-26 23:55:2947
Ken Rockot3044d212018-01-23 02:44:3948class COMPONENT_EXPORT(IPC) Channel : public Sender {
initial.commit09911bf2008-07-26 23:55:2949 // Security tests need access to the pipe handle.
50 friend class ChannelTest;
51
52 public:
dmaclach@chromium.org1707726c2011-02-03 20:35:0953 // Flags to test modes
54 enum ModeFlags {
55 MODE_NO_FLAG = 0x0,
56 MODE_SERVER_FLAG = 0x1,
57 MODE_CLIENT_FLAG = 0x2,
dmaclach@chromium.org1707726c2011-02-03 20:35:0958 };
59
60 // Some Standard Modes
morrita@chromium.orge482111a82014-05-30 03:58:5961 // TODO(morrita): These are under deprecation work. You should use Create*()
62 // functions instead.
initial.commit09911bf2008-07-26 23:55:2963 enum Mode {
dmaclach@chromium.org1707726c2011-02-03 20:35:0964 MODE_NONE = MODE_NO_FLAG,
65 MODE_SERVER = MODE_SERVER_FLAG,
66 MODE_CLIENT = MODE_CLIENT_FLAG,
initial.commit09911bf2008-07-26 23:55:2967 };
68
hubbe@chromium.orgdc875dc2013-10-15 00:07:0069 // Messages internal to the IPC implementation are defined here.
avi246998d2015-12-22 02:39:0470 // Uses Maximum value of message type (uint16_t), to avoid conflicting
hubbe@chromium.orgdc875dc2013-10-15 00:07:0071 // with normal message types, which are enumeration constants starting from 0.
brettw@chromium.orgd805c6a2012-03-08 12:30:2872 enum {
hubbe@chromium.orgdc875dc2013-10-15 00:07:0073 // The Hello message is sent by the peer when the channel is connected.
74 // The message contains just the process id (pid).
75 // The message has a special routing_id (MSG_ROUTING_NONE)
76 // and type (HELLO_MESSAGE_TYPE).
tfarina7023f522015-09-11 19:58:4877 HELLO_MESSAGE_TYPE = UINT16_MAX,
hubbe@chromium.orgdc875dc2013-10-15 00:07:0078 // The CLOSE_FD_MESSAGE_TYPE is used in the IPC class to
79 // work around a bug in sendmsg() on Mac. When an FD is sent
80 // over the socket, a CLOSE_FD_MESSAGE is sent with hops = 2.
81 // The client will return the message with hops = 1, *after* it
82 // has received the message that contains the FD. When we
83 // receive it again on the sender side, we close the FD.
84 CLOSE_FD_MESSAGE_TYPE = HELLO_MESSAGE_TYPE - 1
brettw@chromium.orgd805c6a2012-03-08 12:30:2885 };
86
rockot7c6bf952016-07-14 00:34:1187 // Helper interface a Channel may implement to expose support for associated
88 // Mojo interfaces.
Ken Rockot3044d212018-01-23 02:44:3989 class COMPONENT_EXPORT(IPC) AssociatedInterfaceSupport {
rockot7c6bf952016-07-14 00:34:1190 public:
91 using GenericAssociatedInterfaceFactory =
92 base::Callback<void(mojo::ScopedInterfaceEndpointHandle)>;
93
94 virtual ~AssociatedInterfaceSupport() {}
95
rockota628d0b2017-02-09 08:40:1596 // Returns a ThreadSafeForwarded for this channel which can be used to
97 // safely send mojom::Channel requests from arbitrary threads.
98 virtual std::unique_ptr<mojo::ThreadSafeForwarder<mojom::Channel>>
99 CreateThreadSafeChannel() = 0;
100
rockot0e4de5f2016-07-22 21:18:07101 // Adds an interface factory to this channel for interface |name|. Must be
102 // safe to call from any thread.
rockot7c6bf952016-07-14 00:34:11103 virtual void AddGenericAssociatedInterface(
104 const std::string& name,
105 const GenericAssociatedInterfaceFactory& factory) = 0;
106
107 // Requests an associated interface from the remote endpoint.
108 virtual void GetGenericRemoteAssociatedInterface(
109 const std::string& name,
110 mojo::ScopedInterfaceEndpointHandle handle) = 0;
111
112 // Template helper to add an interface factory to this channel.
113 template <typename Interface>
114 using AssociatedInterfaceFactory =
115 base::Callback<void(mojo::AssociatedInterfaceRequest<Interface>)>;
116 template <typename Interface>
117 void AddAssociatedInterface(
118 const AssociatedInterfaceFactory<Interface>& factory) {
119 AddGenericAssociatedInterface(
120 Interface::Name_,
121 base::Bind(&BindAssociatedInterfaceRequest<Interface>, factory));
122 }
123
124 // Template helper to request a remote associated interface.
125 template <typename Interface>
126 void GetRemoteAssociatedInterface(
127 mojo::AssociatedInterfacePtr<Interface>* proxy) {
yzshen1aa8e56c2017-02-16 23:15:45128 auto request = mojo::MakeRequest(proxy);
rockot7c6bf952016-07-14 00:34:11129 GetGenericRemoteAssociatedInterface(
130 Interface::Name_, request.PassHandle());
131 }
132
133 private:
134 template <typename Interface>
135 static void BindAssociatedInterfaceRequest(
136 const AssociatedInterfaceFactory<Interface>& factory,
137 mojo::ScopedInterfaceEndpointHandle handle) {
Ken Rockot96d1b7b52017-05-13 00:29:21138 factory.Run(
139 mojo::AssociatedInterfaceRequest<Interface>(std::move(handle)));
rockot7c6bf952016-07-14 00:34:11140 }
141 };
142
pkasting@chromium.org05094a32011-09-01 00:50:13143 // The maximum message size in bytes. Attempting to receive a message of this
144 // size or bigger results in a channel error.
Ken Rockot8b8c9062017-09-18 19:36:21145 static constexpr size_t kMaximumMessageSize = 128 * 1024 * 1024;
jeremy@chromium.org514411fc2008-12-10 22:28:11146
viettrungluu@chromium.org4c4c0dc2013-01-05 02:13:04147 // Amount of data to read at once from the pipe.
pkasting@chromium.org05094a32011-09-01 00:50:13148 static const size_t kReadBufferSize = 4 * 1024;
initial.commit09911bf2008-07-26 23:55:29149
dskiba06a2e652015-11-04 01:24:59150 // Maximum persistent read buffer size. Read buffer can grow larger to
151 // accommodate large messages, but it's recommended to shrink back to this
152 // value because it fits 99.9% of all messages (see issue 529940 for data).
153 static const size_t kMaximumReadBufferSize = 64 * 1024;
154
initial.commit09911bf2008-07-26 23:55:29155 // Initialize a Channel.
156 //
dmaclach@chromium.org42ce94e2010-12-08 19:28:09157 // |channel_handle| identifies the communication Channel. For POSIX, if
158 // the file descriptor in the channel handle is != -1, the channel takes
159 // ownership of the file descriptor and will close it appropriately, otherwise
160 // it will create a new descriptor internally.
rvargas@google.comc1afbd2c2008-10-13 19:19:36161 // |listener| receives a callback on the current thread for each newly
162 // received message.
initial.commit09911bf2008-07-26 23:55:29163 //
morrita@chromium.orge482111a82014-05-30 03:58:59164 // There are four type of modes how channels operate:
165 //
166 // - Server and named server: In these modes, the Channel is
167 // responsible for settingb up the IPC object
168 // - An "open" named server: It accepts connections from ANY client.
169 // The caller must then implement their own access-control based on the
170 // client process' user Id.
171 // - Client and named client: In these mode, the Channel merely
172 // connects to the already established IPC object.
173 //
174 // Each mode has its own Create*() API to create the Channel object.
danakj03de39b2016-04-23 04:21:09175 static std::unique_ptr<Channel> Create(
176 const IPC::ChannelHandle& channel_handle,
177 Mode mode,
178 Listener* listener);
morrita@chromium.org2f60c9b2014-06-06 20:13:51179
danakj03de39b2016-04-23 04:21:09180 static std::unique_ptr<Channel> CreateClient(
erikchen27aa7d82015-06-16 21:21:04181 const IPC::ChannelHandle& channel_handle,
rockota34707ca2016-07-20 04:28:32182 Listener* listener,
Hajime Hoshife3ecdc2017-11-28 03:34:13183 const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner);
morrita@chromium.orge482111a82014-05-30 03:58:59184
danakj03de39b2016-04-23 04:21:09185 static std::unique_ptr<Channel> CreateServer(
erikchen27aa7d82015-06-16 21:21:04186 const IPC::ChannelHandle& channel_handle,
rockota34707ca2016-07-20 04:28:32187 Listener* listener,
Hajime Hoshife3ecdc2017-11-28 03:34:13188 const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner);
morrita@chromium.orge482111a82014-05-30 03:58:59189
dchengfe61fca2014-10-22 02:29:52190 ~Channel() override;
initial.commit09911bf2008-07-26 23:55:29191
192 // Connect the pipe. On the server side, this will initiate
193 // waiting for connections. On the client, it attempts to
194 // connect to a pre-existing pipe. Note, calling Connect()
195 // will not block the calling thread and may complete
196 // asynchronously.
erikchen90971902016-04-25 23:45:31197 //
198 // The subclass implementation must call WillConnect() at the beginning of its
199 // implementation.
morrita@chromium.org2f60c9b2014-06-06 20:13:51200 virtual bool Connect() WARN_UNUSED_RESULT = 0;
initial.commit09911bf2008-07-26 23:55:29201
rockot10188752016-09-08 18:24:56202 // Pause the channel. Subsequent sends will be queued internally until
203 // Unpause() is called and the channel is flushed either by Unpause() or a
204 // subsequent call to Flush().
205 virtual void Pause();
rockot401fb2c2016-09-06 18:35:57206
rockot10188752016-09-08 18:24:56207 // Unpause the channel. This allows subsequent Send() calls to transmit
208 // messages immediately, without queueing. If |flush| is true, any messages
209 // queued while paused will be flushed immediately upon unpausing. Otherwise
210 // you must call Flush() explicitly.
rockot401fb2c2016-09-06 18:35:57211 //
212 // Not all implementations support Unpause(). See ConnectPaused() above for
213 // details.
214 virtual void Unpause(bool flush);
215
216 // Manually flush the pipe. This is only useful exactly once, and only after
217 // a call to Unpause(false), in order to explicitly flush out any
218 // messages which were queued prior to unpausing.
219 //
220 // Not all implementations support Flush(). See ConnectPaused() above for
221 // details.
222 virtual void Flush();
223
initial.commit09911bf2008-07-26 23:55:29224 // Close this Channel explicitly. May be called multiple times.
dmaclach@chromium.org22b42c52010-12-20 06:59:23225 // On POSIX calling close on an IPC channel that listens for connections will
226 // cause it to close any accepted connections, and it will stop listening for
227 // new connections. If you just want to close the currently accepted
228 // connection and listen for new ones, use ResetToAcceptingConnectionState.
morrita@chromium.org2f60c9b2014-06-06 20:13:51229 virtual void Close() = 0;
initial.commit09911bf2008-07-26 23:55:29230
rockot7c6bf952016-07-14 00:34:11231 // Gets a helper for associating Mojo interfaces with this Channel.
232 //
233 // NOTE: Not all implementations support this.
234 virtual AssociatedInterfaceSupport* GetAssociatedInterfaceSupport();
235
thakis6d1bd472014-10-29 00:30:41236 // Overridden from ipc::Sender.
initial.commit09911bf2008-07-26 23:55:29237 // Send a message over the Channel to the listener on the other end.
238 //
rvargas@google.comc1afbd2c2008-10-13 19:19:36239 // |message| must be allocated using operator new. This object will be
240 // deleted once the contents of the Message have been sent.
dcheng1c3d9ac2014-12-23 19:59:59241 bool Send(Message* message) override = 0;
initial.commit09911bf2008-07-26 23:55:29242
hidehikof7295f22014-10-28 11:57:21243#if !defined(OS_NACL_SFI)
jschuh@chromium.org5c41e6e12012-03-17 02:20:46244 // Generates a channel ID that's non-predictable and unique.
245 static std::string GenerateUniqueRandomChannelID();
bbudge@chromium.orgbccbaf22012-09-28 21:46:01246#endif
jschuh@chromium.org5c41e6e12012-03-17 02:20:46247
perkjdbcac352014-12-11 17:27:58248#if defined(OS_LINUX)
249 // Sandboxed processes live in a PID namespace, so when sending the IPC hello
250 // message from client to server we need to send the PID from the global
251 // PID namespace.
252 static void SetGlobalPid(int pid);
sammce4d0abd2016-03-07 22:38:04253 static int GetGlobalPid();
perkjdbcac352014-12-11 17:27:58254#endif
255
erikchen5142dc72015-09-10 21:00:18256 protected:
erikchen90971902016-04-25 23:45:31257 // Subclasses must call this method at the beginning of their implementation
258 // of Connect().
259 void WillConnect();
260
261 private:
262 bool did_start_connect_ = false;
initial.commit09911bf2008-07-26 23:55:29263};
264
jeremy@chromium.org514411fc2008-12-10 22:28:11265} // namespace IPC
initial.commit09911bf2008-07-26 23:55:29266
agl@chromium.org946d1b22009-07-22 23:57:21267#endif // IPC_IPC_CHANNEL_H_