amit@chromium.org | 3729074 | 2012-01-24 11:36:05 +0900 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [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 | |
| 5 | #ifndef IPC_IPC_CHANNEL_HANDLE_H_ |
| 6 | #define IPC_IPC_CHANNEL_HANDLE_H_ |
| 7 | |
apatrick@chromium.org | bcd2b35 | 2010-03-06 06:53:50 +0900 | [diff] [blame] | 8 | #include <string> |
| 9 | |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 10 | #include "build/build_config.h" |
| 11 | |
| 12 | #if defined(OS_POSIX) |
| 13 | #include "base/file_descriptor_posix.h" |
amit@chromium.org | 3729074 | 2012-01-24 11:36:05 +0900 | [diff] [blame] | 14 | #elif defined(OS_WIN) |
| 15 | #include <windows.h> |
| 16 | #endif // defined (OS_WIN) |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 17 | |
| 18 | // On Windows, any process can create an IPC channel and others can fetch |
| 19 | // it by name. We pass around the channel names over IPC. |
amit@chromium.org | 3729074 | 2012-01-24 11:36:05 +0900 | [diff] [blame] | 20 | // On Windows the initialization of ChannelHandle with an existing pipe |
| 21 | // handle is provided for convenience. |
| 22 | // NOTE: A ChannelHandle with a pipe handle Will NOT be marshalled over IPC. |
| 23 | |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 24 | // On POSIX, we instead pass around handles to channel endpoints via IPC. |
| 25 | // When it's time to IPC a new channel endpoint around, we send both the |
| 26 | // channel name as well as a base::FileDescriptor, which is itself a special |
| 27 | // type that knows how to copy a socket endpoint over IPC. |
| 28 | // |
amit@chromium.org | 3729074 | 2012-01-24 11:36:05 +0900 | [diff] [blame] | 29 | // In sum, this data structure can be used to pass channel information by name |
| 30 | // in both Windows and Posix. When passing a handle to a channel over IPC, |
| 31 | // use this data structure only for POSIX. |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 32 | |
| 33 | namespace IPC { |
| 34 | |
| 35 | struct ChannelHandle { |
| 36 | // Note that serialization for this object is defined in the ParamTraits |
| 37 | // template specialization in ipc_message_utils.h. |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 38 | ChannelHandle() {} |
abeera@google.com | b107425 | 2011-06-25 03:57:24 +0900 | [diff] [blame] | 39 | // The name that is passed in should be an absolute path for Posix. |
| 40 | // Otherwise there may be a problem in IPC communication between |
| 41 | // processes with different working directories. |
dmaclach@chromium.org | 058c4a7 | 2010-12-09 04:28:09 +0900 | [diff] [blame] | 42 | ChannelHandle(const std::string& n) : name(n) {} |
| 43 | ChannelHandle(const char* n) : name(n) {} |
amit@chromium.org | 3729074 | 2012-01-24 11:36:05 +0900 | [diff] [blame] | 44 | #if defined(OS_WIN) |
| 45 | explicit ChannelHandle(HANDLE h) : pipe(h) {} |
| 46 | #elif defined(OS_POSIX) |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 47 | ChannelHandle(const std::string& n, const base::FileDescriptor& s) |
| 48 | : name(n), socket(s) {} |
dmaclach@chromium.org | 058c4a7 | 2010-12-09 04:28:09 +0900 | [diff] [blame] | 49 | #endif // defined(OS_POSIX) |
| 50 | |
| 51 | std::string name; |
| 52 | #if defined(OS_POSIX) |
| 53 | base::FileDescriptor socket; |
amit@chromium.org | 3729074 | 2012-01-24 11:36:05 +0900 | [diff] [blame] | 54 | #elif defined(OS_WIN) |
| 55 | // A simple container to automatically initialize pipe handle |
| 56 | struct PipeHandle { |
| 57 | PipeHandle() : handle(NULL) {} |
| 58 | PipeHandle(HANDLE h) : handle(h) {} |
| 59 | HANDLE handle; |
| 60 | }; |
| 61 | PipeHandle pipe; |
| 62 | #endif // defined (OS_WIN) |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 63 | }; |
| 64 | |
| 65 | } // namespace IPC |
| 66 | |
| 67 | #endif // IPC_IPC_CHANNEL_HANDLE_H_ |