OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef IPC_IPC_CHANNEL_H_ | 5 #ifndef IPC_IPC_CHANNEL_H_ |
6 #define IPC_IPC_CHANNEL_H_ | 6 #define IPC_IPC_CHANNEL_H_ |
7 | 7 |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 #include <stdint.h> | 9 #include <stdint.h> |
10 | 10 |
(...skipping 21 matching lines...) Expand all Loading... |
32 | 32 |
33 namespace IPC { | 33 namespace IPC { |
34 | 34 |
35 class Listener; | 35 class Listener; |
36 | 36 |
37 //------------------------------------------------------------------------------ | 37 //------------------------------------------------------------------------------ |
38 // See | 38 // See |
39 // http://www.chromium.org/developers/design-documents/inter-process-communicati
on | 39 // http://www.chromium.org/developers/design-documents/inter-process-communicati
on |
40 // for overview of IPC in Chromium. | 40 // for overview of IPC in Chromium. |
41 | 41 |
42 // Channels are implemented using named pipes on Windows, and | 42 // Channels are implemented using mojo message pipes on all platforms other |
43 // socket pairs (or in some special cases unix domain sockets) on POSIX. | 43 // than NaCl. |
44 // On Windows we access pipes in various processes by name. | |
45 // On POSIX we pass file descriptors to child processes and assign names to them | |
46 // in a lookup table. | |
47 // In general on POSIX we do not use unix domain sockets due to security | |
48 // concerns and the fact that they can leave garbage around the file system | |
49 // (MacOS does not support abstract named unix domain sockets). | |
50 // You can use unix domain sockets if you like on POSIX by constructing the | |
51 // the channel with the mode set to one of the NAMED modes. NAMED modes are | |
52 // currently used by automation and service processes. | |
53 | 44 |
54 class IPC_EXPORT Channel : public Endpoint { | 45 class IPC_EXPORT Channel : public Endpoint { |
55 // Security tests need access to the pipe handle. | 46 // Security tests need access to the pipe handle. |
56 friend class ChannelTest; | 47 friend class ChannelTest; |
57 | 48 |
58 public: | 49 public: |
59 // Flags to test modes | 50 // Flags to test modes |
60 enum ModeFlags { | 51 enum ModeFlags { |
61 MODE_NO_FLAG = 0x0, | 52 MODE_NO_FLAG = 0x0, |
62 MODE_SERVER_FLAG = 0x1, | 53 MODE_SERVER_FLAG = 0x1, |
63 MODE_CLIENT_FLAG = 0x2, | 54 MODE_CLIENT_FLAG = 0x2, |
64 MODE_NAMED_FLAG = 0x4, | |
65 }; | 55 }; |
66 | 56 |
67 // Some Standard Modes | 57 // Some Standard Modes |
68 // TODO(morrita): These are under deprecation work. You should use Create*() | 58 // TODO(morrita): These are under deprecation work. You should use Create*() |
69 // functions instead. | 59 // functions instead. |
70 enum Mode { | 60 enum Mode { |
71 MODE_NONE = MODE_NO_FLAG, | 61 MODE_NONE = MODE_NO_FLAG, |
72 MODE_SERVER = MODE_SERVER_FLAG, | 62 MODE_SERVER = MODE_SERVER_FLAG, |
73 MODE_CLIENT = MODE_CLIENT_FLAG, | 63 MODE_CLIENT = MODE_CLIENT_FLAG, |
74 MODE_NAMED_SERVER = MODE_SERVER_FLAG | MODE_NAMED_FLAG, | |
75 MODE_NAMED_CLIENT = MODE_CLIENT_FLAG | MODE_NAMED_FLAG, | |
76 }; | 64 }; |
77 | 65 |
78 // Messages internal to the IPC implementation are defined here. | 66 // Messages internal to the IPC implementation are defined here. |
79 // Uses Maximum value of message type (uint16_t), to avoid conflicting | 67 // Uses Maximum value of message type (uint16_t), to avoid conflicting |
80 // with normal message types, which are enumeration constants starting from 0. | 68 // with normal message types, which are enumeration constants starting from 0. |
81 enum { | 69 enum { |
82 // The Hello message is sent by the peer when the channel is connected. | 70 // The Hello message is sent by the peer when the channel is connected. |
83 // The message contains just the process id (pid). | 71 // The message contains just the process id (pid). |
84 // The message has a special routing_id (MSG_ROUTING_NONE) | 72 // The message has a special routing_id (MSG_ROUTING_NONE) |
85 // and type (HELLO_MESSAGE_TYPE). | 73 // and type (HELLO_MESSAGE_TYPE). |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
186 const IPC::ChannelHandle& channel_handle, | 174 const IPC::ChannelHandle& channel_handle, |
187 Mode mode, | 175 Mode mode, |
188 Listener* listener); | 176 Listener* listener); |
189 | 177 |
190 static std::unique_ptr<Channel> CreateClient( | 178 static std::unique_ptr<Channel> CreateClient( |
191 const IPC::ChannelHandle& channel_handle, | 179 const IPC::ChannelHandle& channel_handle, |
192 Listener* listener, | 180 Listener* listener, |
193 const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner = | 181 const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner = |
194 base::ThreadTaskRunnerHandle::Get()); | 182 base::ThreadTaskRunnerHandle::Get()); |
195 | 183 |
196 // Channels on Windows are named by default and accessible from other | |
197 // processes. On POSIX channels are anonymous by default and not accessible | |
198 // from other processes. Named channels work via named unix domain sockets. | |
199 // On Windows MODE_NAMED_SERVER is equivalent to MODE_SERVER and | |
200 // MODE_NAMED_CLIENT is equivalent to MODE_CLIENT. | |
201 static std::unique_ptr<Channel> CreateNamedServer( | |
202 const IPC::ChannelHandle& channel_handle, | |
203 Listener* listener); | |
204 static std::unique_ptr<Channel> CreateNamedClient( | |
205 const IPC::ChannelHandle& channel_handle, | |
206 Listener* listener); | |
207 static std::unique_ptr<Channel> CreateServer( | 184 static std::unique_ptr<Channel> CreateServer( |
208 const IPC::ChannelHandle& channel_handle, | 185 const IPC::ChannelHandle& channel_handle, |
209 Listener* listener, | 186 Listener* listener, |
210 const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner = | 187 const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner = |
211 base::ThreadTaskRunnerHandle::Get()); | 188 base::ThreadTaskRunnerHandle::Get()); |
212 | 189 |
213 ~Channel() override; | 190 ~Channel() override; |
214 | 191 |
215 // Connect the pipe. On the server side, this will initiate | 192 // Connect the pipe. On the server side, this will initiate |
216 // waiting for connections. On the client, it attempts to | 193 // waiting for connections. On the client, it attempts to |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
275 // This method may only be called on the server side of a channel. | 252 // This method may only be called on the server side of a channel. |
276 // This method can be called on any thread. | 253 // This method can be called on any thread. |
277 virtual int GetClientFileDescriptor() const = 0; | 254 virtual int GetClientFileDescriptor() const = 0; |
278 | 255 |
279 // Same as GetClientFileDescriptor, but transfers the ownership of the | 256 // Same as GetClientFileDescriptor, but transfers the ownership of the |
280 // file descriptor to the caller. | 257 // file descriptor to the caller. |
281 // This method can be called on any thread. | 258 // This method can be called on any thread. |
282 virtual base::ScopedFD TakeClientFileDescriptor() = 0; | 259 virtual base::ScopedFD TakeClientFileDescriptor() = 0; |
283 #endif | 260 #endif |
284 | 261 |
285 // Returns true if a named server channel is initialized on the given channel | |
286 // ID. Even if true, the server may have already accepted a connection. | |
287 static bool IsNamedServerInitialized(const std::string& channel_id); | |
288 | |
289 #if !defined(OS_NACL_SFI) | 262 #if !defined(OS_NACL_SFI) |
290 // Generates a channel ID that's non-predictable and unique. | 263 // Generates a channel ID that's non-predictable and unique. |
291 static std::string GenerateUniqueRandomChannelID(); | 264 static std::string GenerateUniqueRandomChannelID(); |
292 | 265 |
293 // Generates a channel ID that, if passed to the client as a shared secret, | 266 // Generates a channel ID that, if passed to the client as a shared secret, |
294 // will validate that the client's authenticity. On platforms that do not | 267 // will validate that the client's authenticity. On platforms that do not |
295 // require additional this is simply calls GenerateUniqueRandomChannelID(). | 268 // require additional this is simply calls GenerateUniqueRandomChannelID(). |
296 // For portability the prefix should not include the \ character. | 269 // For portability the prefix should not include the \ character. |
297 static std::string GenerateVerifiedChannelID(const std::string& prefix); | 270 static std::string GenerateVerifiedChannelID(const std::string& prefix); |
298 #endif | 271 #endif |
299 | 272 |
300 // Deprecated: Create a mojo::MessagePipe directly and release() its handles | 273 // Deprecated: Create a mojo::MessagePipe directly and release() its handles |
301 // instead. | 274 // instead. |
302 // | 275 // |
303 // Generates a pair of channel handles that can be used as the client and | 276 // Generates a pair of channel handles that can be used as the client and |
304 // server ends of a ChannelMojo. |name_postfix| is ignored. | 277 // server ends of a ChannelMojo. |name_postfix| is ignored. |
305 static void GenerateMojoChannelHandlePair( | 278 static void GenerateMojoChannelHandlePair( |
306 const std::string& name_postfix, | 279 const std::string& name_postfix, |
307 IPC::ChannelHandle* handle0, | 280 IPC::ChannelHandle* handle0, |
308 IPC::ChannelHandle* handle1); | 281 IPC::ChannelHandle* handle1); |
309 | 282 |
310 #if defined(OS_LINUX) | 283 #if defined(OS_LINUX) |
311 // Sandboxed processes live in a PID namespace, so when sending the IPC hello | 284 // Sandboxed processes live in a PID namespace, so when sending the IPC hello |
312 // message from client to server we need to send the PID from the global | 285 // message from client to server we need to send the PID from the global |
313 // PID namespace. | 286 // PID namespace. |
314 static void SetGlobalPid(int pid); | 287 static void SetGlobalPid(int pid); |
315 static int GetGlobalPid(); | 288 static int GetGlobalPid(); |
316 #endif | 289 #endif |
317 | 290 |
318 #if defined(OS_ANDROID) | |
319 // Most tests are single process and work the same on all platforms. However | |
320 // in some cases we want to test multi-process, and Android differs in that it | |
321 // can't 'exec' after forking. This callback resets any data in the forked | |
322 // process such that it acts similar to if it was exec'd, for tests. | |
323 static void NotifyProcessForkedForTesting(); | |
324 #endif | |
325 | |
326 protected: | 291 protected: |
327 // An OutputElement is a wrapper around a Message or raw buffer while it is | 292 // An OutputElement is a wrapper around a Message or raw buffer while it is |
328 // waiting to be passed to the system's underlying IPC mechanism. | 293 // waiting to be passed to the system's underlying IPC mechanism. |
329 class OutputElement { | 294 class OutputElement { |
330 public: | 295 public: |
331 // Takes ownership of message. | 296 // Takes ownership of message. |
332 OutputElement(Message* message); | 297 OutputElement(Message* message); |
333 // Takes ownership of the buffer. |buffer| is freed via free(), so it | 298 // Takes ownership of the buffer. |buffer| is freed via free(), so it |
334 // must be malloced. | 299 // must be malloced. |
335 OutputElement(void* buffer, size_t length); | 300 OutputElement(void* buffer, size_t length); |
(...skipping 12 matching lines...) Expand all Loading... |
348 void OnSetAttachmentBrokerEndpoint() override; | 313 void OnSetAttachmentBrokerEndpoint() override; |
349 | 314 |
350 // Subclasses must call this method at the beginning of their implementation | 315 // Subclasses must call this method at the beginning of their implementation |
351 // of Connect(). | 316 // of Connect(). |
352 void WillConnect(); | 317 void WillConnect(); |
353 | 318 |
354 private: | 319 private: |
355 bool did_start_connect_ = false; | 320 bool did_start_connect_ = false; |
356 }; | 321 }; |
357 | 322 |
358 #if defined(OS_POSIX) | |
359 // SocketPair() creates a pair of socket FDs suitable for using with | |
360 // IPC::Channel. | |
361 IPC_EXPORT bool SocketPair(int* fd1, int* fd2); | |
362 #endif | |
363 | |
364 } // namespace IPC | 323 } // namespace IPC |
365 | 324 |
366 #endif // IPC_IPC_CHANNEL_H_ | 325 #endif // IPC_IPC_CHANNEL_H_ |
OLD | NEW |