[email protected] | a7c03d4f3 | 2012-01-24 02:36:05 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | d2e884d | 2009-06-22 20:37:52 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
3 | // found in the LICENSE file. | ||||
4 | |||||
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 5 | #ifndef IPC_IPC_CHANNEL_HANDLE_H_ |
6 | #define IPC_IPC_CHANNEL_HANDLE_H_ | ||||
[email protected] | d2e884d | 2009-06-22 20:37:52 | [diff] [blame] | 7 | |
[email protected] | 246a7045 | 2010-03-05 21:53:50 | [diff] [blame] | 8 | #include <string> |
9 | |||||
[email protected] | d2e884d | 2009-06-22 20:37:52 | [diff] [blame] | 10 | #include "build/build_config.h" |
amistry | 3618252 | 2016-06-27 06:34:42 | [diff] [blame] | 11 | #include "mojo/public/cpp/system/message_pipe.h" |
[email protected] | d2e884d | 2009-06-22 20:37:52 | [diff] [blame] | 12 | |
13 | #if defined(OS_POSIX) | ||||
14 | #include "base/file_descriptor_posix.h" | ||||
[email protected] | a7c03d4f3 | 2012-01-24 02:36:05 | [diff] [blame] | 15 | #elif defined(OS_WIN) |
16 | #include <windows.h> | ||||
17 | #endif // defined (OS_WIN) | ||||
[email protected] | d2e884d | 2009-06-22 20:37:52 | [diff] [blame] | 18 | |
19 | // On Windows, any process can create an IPC channel and others can fetch | ||||
20 | // it by name. We pass around the channel names over IPC. | ||||
[email protected] | a7c03d4f3 | 2012-01-24 02:36:05 | [diff] [blame] | 21 | // On Windows the initialization of ChannelHandle with an existing pipe |
22 | // handle is provided for convenience. | ||||
23 | // NOTE: A ChannelHandle with a pipe handle Will NOT be marshalled over IPC. | ||||
24 | |||||
[email protected] | d2e884d | 2009-06-22 20:37:52 | [diff] [blame] | 25 | // On POSIX, we instead pass around handles to channel endpoints via IPC. |
26 | // When it's time to IPC a new channel endpoint around, we send both the | ||||
27 | // channel name as well as a base::FileDescriptor, which is itself a special | ||||
28 | // type that knows how to copy a socket endpoint over IPC. | ||||
29 | // | ||||
[email protected] | a7c03d4f3 | 2012-01-24 02:36:05 | [diff] [blame] | 30 | // In sum, this data structure can be used to pass channel information by name |
31 | // in both Windows and Posix. When passing a handle to a channel over IPC, | ||||
32 | // use this data structure only for POSIX. | ||||
[email protected] | d2e884d | 2009-06-22 20:37:52 | [diff] [blame] | 33 | |
34 | namespace IPC { | ||||
35 | |||||
36 | struct ChannelHandle { | ||||
37 | // Note that serialization for this object is defined in the ParamTraits | ||||
38 | // template specialization in ipc_message_utils.h. | ||||
[email protected] | d2e884d | 2009-06-22 20:37:52 | [diff] [blame] | 39 | ChannelHandle() {} |
[email protected] | 84818e9 | 2011-06-24 18:57:24 | [diff] [blame] | 40 | // The name that is passed in should be an absolute path for Posix. |
41 | // Otherwise there may be a problem in IPC communication between | ||||
42 | // processes with different working directories. | ||||
[email protected] | 42ce94e | 2010-12-08 19:28:09 | [diff] [blame] | 43 | ChannelHandle(const std::string& n) : name(n) {} |
44 | ChannelHandle(const char* n) : name(n) {} | ||||
[email protected] | a7c03d4f3 | 2012-01-24 02:36:05 | [diff] [blame] | 45 | #if defined(OS_WIN) |
46 | explicit ChannelHandle(HANDLE h) : pipe(h) {} | ||||
47 | #elif defined(OS_POSIX) | ||||
[email protected] | d2e884d | 2009-06-22 20:37:52 | [diff] [blame] | 48 | ChannelHandle(const std::string& n, const base::FileDescriptor& s) |
49 | : name(n), socket(s) {} | ||||
[email protected] | 42ce94e | 2010-12-08 19:28:09 | [diff] [blame] | 50 | #endif // defined(OS_POSIX) |
amistry | 3618252 | 2016-06-27 06:34:42 | [diff] [blame] | 51 | ChannelHandle(mojo::MessagePipeHandle h) : mojo_handle(h) {} |
[email protected] | 42ce94e | 2010-12-08 19:28:09 | [diff] [blame] | 52 | |
53 | std::string name; | ||||
54 | #if defined(OS_POSIX) | ||||
55 | base::FileDescriptor socket; | ||||
[email protected] | a7c03d4f3 | 2012-01-24 02:36:05 | [diff] [blame] | 56 | #elif defined(OS_WIN) |
57 | // A simple container to automatically initialize pipe handle | ||||
58 | struct PipeHandle { | ||||
59 | PipeHandle() : handle(NULL) {} | ||||
60 | PipeHandle(HANDLE h) : handle(h) {} | ||||
61 | HANDLE handle; | ||||
62 | }; | ||||
63 | PipeHandle pipe; | ||||
64 | #endif // defined (OS_WIN) | ||||
amistry | 3618252 | 2016-06-27 06:34:42 | [diff] [blame] | 65 | mojo::MessagePipeHandle mojo_handle; |
[email protected] | d2e884d | 2009-06-22 20:37:52 | [diff] [blame] | 66 | }; |
67 | |||||
68 | } // namespace IPC | ||||
69 | |||||
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 70 | #endif // IPC_IPC_CHANNEL_HANDLE_H_ |