wez@chromium.org | 7cce091 | 2011-04-06 21:01:44 +0900 | [diff] [blame] | 1 | // Copyright (c) 2011 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_H_ |
| 6 | #define IPC_IPC_CHANNEL_H_ |
thakis@chromium.org | 01d1452 | 2010-07-27 08:08:24 +0900 | [diff] [blame] | 7 | #pragma once |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 8 | |
evan@chromium.org | 1f3e46b | 2010-10-20 04:11:15 +0900 | [diff] [blame] | 9 | #include "base/compiler_specific.h" |
dmaclach@chromium.org | 058c4a7 | 2010-12-09 04:28:09 +0900 | [diff] [blame] | 10 | #include "ipc/ipc_channel_handle.h" |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 11 | #include "ipc/ipc_message.h" |
| 12 | |
| 13 | namespace IPC { |
| 14 | |
| 15 | //------------------------------------------------------------------------------ |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 16 | // See |
| 17 | // http://www.chromium.org/developers/design-documents/inter-process-communication |
| 18 | // for overview of IPC in Chromium. |
| 19 | |
| 20 | // Channels are implemented using named pipes on Windows, and |
| 21 | // socket pairs (or in some special cases unix domain sockets) on POSIX. |
| 22 | // On Windows we access pipes in various processes by name. |
| 23 | // On POSIX we pass file descriptors to child processes and assign names to them |
| 24 | // in a lookup table. |
| 25 | // In general on POSIX we do not use unix domain sockets due to security |
| 26 | // concerns and the fact that they can leave garbage around the file system |
| 27 | // (MacOS does not support abstract named unix domain sockets). |
| 28 | // You can use unix domain sockets if you like on POSIX by constructing the |
| 29 | // the channel with the mode set to one of the NAMED modes. NAMED modes are |
| 30 | // currently used by automation and service processes. |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 31 | |
| 32 | class Channel : public Message::Sender { |
| 33 | // Security tests need access to the pipe handle. |
| 34 | friend class ChannelTest; |
| 35 | |
| 36 | public: |
| 37 | // Implemented by consumers of a Channel to receive messages. |
| 38 | class Listener { |
| 39 | public: |
| 40 | virtual ~Listener() {} |
| 41 | |
jam@chromium.org | 8a2c784 | 2010-12-24 15:19:28 +0900 | [diff] [blame] | 42 | // Called when a message is received. Returns true iff the message was |
| 43 | // handled. |
| 44 | virtual bool OnMessageReceived(const Message& message) = 0; |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 45 | |
| 46 | // Called when the channel is connected and we have received the internal |
| 47 | // Hello message from the peer. |
| 48 | virtual void OnChannelConnected(int32 peer_pid) {} |
| 49 | |
| 50 | // Called when an error is detected that causes the channel to close. |
| 51 | // This method is not called when a channel is closed normally. |
| 52 | virtual void OnChannelError() {} |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 53 | |
| 54 | #if defined(OS_POSIX) |
| 55 | // Called on the server side when a channel that listens for connections |
| 56 | // denies an attempt to connect. |
| 57 | virtual void OnChannelDenied() {} |
| 58 | |
| 59 | // Called on the server side when a channel that listens for connections |
| 60 | // has an error that causes the listening channel to close. |
| 61 | virtual void OnChannelListenError() {} |
| 62 | #endif // OS_POSIX |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 63 | }; |
| 64 | |
dmaclach@chromium.org | f146c29 | 2011-02-04 05:35:09 +0900 | [diff] [blame] | 65 | // Flags to test modes |
| 66 | enum ModeFlags { |
| 67 | MODE_NO_FLAG = 0x0, |
| 68 | MODE_SERVER_FLAG = 0x1, |
| 69 | MODE_CLIENT_FLAG = 0x2, |
wez@chromium.org | 7cce091 | 2011-04-06 21:01:44 +0900 | [diff] [blame] | 70 | MODE_NAMED_FLAG = 0x4, |
| 71 | #if defined(OS_POSIX) |
| 72 | MODE_OPEN_ACCESS_FLAG = 0x8, // Don't restrict access based on client UID. |
| 73 | #endif |
dmaclach@chromium.org | f146c29 | 2011-02-04 05:35:09 +0900 | [diff] [blame] | 74 | }; |
| 75 | |
| 76 | // Some Standard Modes |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 77 | enum Mode { |
dmaclach@chromium.org | f146c29 | 2011-02-04 05:35:09 +0900 | [diff] [blame] | 78 | MODE_NONE = MODE_NO_FLAG, |
| 79 | MODE_SERVER = MODE_SERVER_FLAG, |
| 80 | MODE_CLIENT = MODE_CLIENT_FLAG, |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 81 | // Channels on Windows are named by default and accessible from other |
| 82 | // processes. On POSIX channels are anonymous by default and not accessible |
| 83 | // from other processes. Named channels work via named unix domain sockets. |
dmaclach@chromium.org | f146c29 | 2011-02-04 05:35:09 +0900 | [diff] [blame] | 84 | // On Windows MODE_NAMED_SERVER is equivalent to MODE_SERVER and |
| 85 | // MODE_NAMED_CLIENT is equivalent to MODE_CLIENT. |
| 86 | MODE_NAMED_SERVER = MODE_SERVER_FLAG | MODE_NAMED_FLAG, |
| 87 | MODE_NAMED_CLIENT = MODE_CLIENT_FLAG | MODE_NAMED_FLAG, |
wez@chromium.org | 7cce091 | 2011-04-06 21:01:44 +0900 | [diff] [blame] | 88 | #if defined(OS_POSIX) |
| 89 | // An "open" named server accepts connections from ANY client. |
| 90 | // The caller must then implement their own access-control based on the |
| 91 | // client process' user Id. |
| 92 | MODE_OPEN_NAMED_SERVER = MODE_OPEN_ACCESS_FLAG | MODE_SERVER_FLAG | |
| 93 | MODE_NAMED_FLAG |
| 94 | #endif |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 95 | }; |
| 96 | |
| 97 | enum { |
| 98 | // The maximum message size in bytes. Attempting to receive a |
| 99 | // message of this size or bigger results in a channel error. |
darin@chromium.org | 6c8db92 | 2010-11-30 08:34:07 +0900 | [diff] [blame] | 100 | kMaximumMessageSize = 128 * 1024 * 1024, |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 101 | |
| 102 | // Ammount of data to read at once from the pipe. |
| 103 | kReadBufferSize = 4 * 1024 |
| 104 | }; |
| 105 | |
| 106 | // Initialize a Channel. |
| 107 | // |
dmaclach@chromium.org | 058c4a7 | 2010-12-09 04:28:09 +0900 | [diff] [blame] | 108 | // |channel_handle| identifies the communication Channel. For POSIX, if |
| 109 | // the file descriptor in the channel handle is != -1, the channel takes |
| 110 | // ownership of the file descriptor and will close it appropriately, otherwise |
| 111 | // it will create a new descriptor internally. |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 112 | // |mode| specifies whether this Channel is to operate in server mode or |
| 113 | // client mode. In server mode, the Channel is responsible for setting up the |
| 114 | // IPC object, whereas in client mode, the Channel merely connects to the |
| 115 | // already established IPC object. |
| 116 | // |listener| receives a callback on the current thread for each newly |
| 117 | // received message. |
| 118 | // |
dmaclach@chromium.org | 058c4a7 | 2010-12-09 04:28:09 +0900 | [diff] [blame] | 119 | Channel(const IPC::ChannelHandle &channel_handle, Mode mode, |
| 120 | Listener* listener); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 121 | |
hans@chromium.org | 78b7593 | 2011-05-25 18:08:19 +0900 | [diff] [blame] | 122 | virtual ~Channel(); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 123 | |
| 124 | // Connect the pipe. On the server side, this will initiate |
| 125 | // waiting for connections. On the client, it attempts to |
| 126 | // connect to a pre-existing pipe. Note, calling Connect() |
| 127 | // will not block the calling thread and may complete |
| 128 | // asynchronously. |
evan@chromium.org | 1f3e46b | 2010-10-20 04:11:15 +0900 | [diff] [blame] | 129 | bool Connect() WARN_UNUSED_RESULT; |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 130 | |
| 131 | // Close this Channel explicitly. May be called multiple times. |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 132 | // On POSIX calling close on an IPC channel that listens for connections will |
| 133 | // cause it to close any accepted connections, and it will stop listening for |
| 134 | // new connections. If you just want to close the currently accepted |
| 135 | // connection and listen for new ones, use ResetToAcceptingConnectionState. |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 136 | void Close(); |
| 137 | |
| 138 | // Modify the Channel's listener. |
| 139 | void set_listener(Listener* listener); |
| 140 | |
| 141 | // Send a message over the Channel to the listener on the other end. |
| 142 | // |
| 143 | // |message| must be allocated using operator new. This object will be |
| 144 | // deleted once the contents of the Message have been sent. |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 145 | virtual bool Send(Message* message); |
| 146 | |
abarth@chromium.org | 07af1a6 | 2010-11-13 03:01:34 +0900 | [diff] [blame] | 147 | #if defined(OS_POSIX) && !defined(OS_NACL) |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 148 | // On POSIX an IPC::Channel wraps a socketpair(), this method returns the |
| 149 | // FD # for the client end of the socket. |
| 150 | // This method may only be called on the server side of a channel. |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 151 | int GetClientFileDescriptor() const; |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 152 | |
| 153 | // On POSIX an IPC::Channel can either wrap an established socket, or it |
| 154 | // can wrap a socket that is listening for connections. Currently an |
| 155 | // IPC::Channel that listens for connections can only accept one connection |
| 156 | // at a time. |
| 157 | |
| 158 | // Returns true if the channel supports listening for connections. |
| 159 | bool AcceptsConnections() const; |
| 160 | |
| 161 | // Returns true if the channel supports listening for connections and is |
| 162 | // currently connected. |
| 163 | bool HasAcceptedConnection() const; |
| 164 | |
wez@chromium.org | 7cce091 | 2011-04-06 21:01:44 +0900 | [diff] [blame] | 165 | // Returns true if the peer process' effective user id can be determined, in |
| 166 | // which case the supplied client_euid is updated with it. |
| 167 | bool GetClientEuid(uid_t* client_euid) const; |
| 168 | |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 169 | // Closes any currently connected socket, and returns to a listening state |
| 170 | // for more connections. |
| 171 | void ResetToAcceptingConnectionState(); |
wez@chromium.org | 7cce091 | 2011-04-06 21:01:44 +0900 | [diff] [blame] | 172 | #endif // defined(OS_POSIX) && !defined(OS_NACL) |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 173 | |
neb@chromium.org | c89aefc | 2010-04-06 02:35:01 +0900 | [diff] [blame] | 174 | protected: |
| 175 | // Used in Chrome by the TestSink to provide a dummy channel implementation |
| 176 | // for testing. TestSink overrides the "interesting" functions in Channel so |
| 177 | // no actual implementation is needed. This will cause un-overridden calls to |
| 178 | // segfault. Do not use outside of test code! |
| 179 | Channel() : channel_impl_(0) { } |
| 180 | |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 181 | private: |
| 182 | // PIMPL to which all channel calls are delegated. |
| 183 | class ChannelImpl; |
| 184 | ChannelImpl *channel_impl_; |
| 185 | |
| 186 | // The Hello message is internal to the Channel class. It is sent |
| 187 | // by the peer when the channel is connected. The message contains |
| 188 | // just the process id (pid). The message has a special routing_id |
| 189 | // (MSG_ROUTING_NONE) and type (HELLO_MESSAGE_TYPE). |
| 190 | enum { |
| 191 | HELLO_MESSAGE_TYPE = kuint16max // Maximum value of message type (uint16), |
| 192 | // to avoid conflicting with normal |
| 193 | // message types, which are enumeration |
| 194 | // constants starting from 0. |
| 195 | }; |
| 196 | }; |
| 197 | |
| 198 | } // namespace IPC |
| 199 | |
| 200 | #endif // IPC_IPC_CHANNEL_H_ |