| 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 <string> | 8 #include <string> |
| 9 | 9 |
| 10 #if defined(OS_POSIX) | 10 #if defined(OS_POSIX) |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 110 // - An "open" named server: It accepts connections from ANY client. | 110 // - An "open" named server: It accepts connections from ANY client. |
| 111 // The caller must then implement their own access-control based on the | 111 // The caller must then implement their own access-control based on the |
| 112 // client process' user Id. | 112 // client process' user Id. |
| 113 // - Client and named client: In these mode, the Channel merely | 113 // - Client and named client: In these mode, the Channel merely |
| 114 // connects to the already established IPC object. | 114 // connects to the already established IPC object. |
| 115 // | 115 // |
| 116 // Each mode has its own Create*() API to create the Channel object. | 116 // Each mode has its own Create*() API to create the Channel object. |
| 117 // | 117 // |
| 118 // TODO(morrita): Replace CreateByModeForProxy() with one of above Create*(). | 118 // TODO(morrita): Replace CreateByModeForProxy() with one of above Create*(). |
| 119 // | 119 // |
| 120 static scoped_ptr<Channel> CreateByModeForProxy( | 120 static scoped_ptr<Channel> Create( |
| 121 const IPC::ChannelHandle &channel_handle, Mode mode,Listener* listener); | 121 const IPC::ChannelHandle &channel_handle, Mode mode,Listener* listener); |
| 122 |
| 122 static scoped_ptr<Channel> CreateClient( | 123 static scoped_ptr<Channel> CreateClient( |
| 123 const IPC::ChannelHandle &channel_handle, Listener* listener); | 124 const IPC::ChannelHandle &channel_handle, Listener* listener); |
| 124 | 125 |
| 125 // Channels on Windows are named by default and accessible from other | 126 // Channels on Windows are named by default and accessible from other |
| 126 // processes. On POSIX channels are anonymous by default and not accessible | 127 // processes. On POSIX channels are anonymous by default and not accessible |
| 127 // from other processes. Named channels work via named unix domain sockets. | 128 // from other processes. Named channels work via named unix domain sockets. |
| 128 // On Windows MODE_NAMED_SERVER is equivalent to MODE_SERVER and | 129 // On Windows MODE_NAMED_SERVER is equivalent to MODE_SERVER and |
| 129 // MODE_NAMED_CLIENT is equivalent to MODE_CLIENT. | 130 // MODE_NAMED_CLIENT is equivalent to MODE_CLIENT. |
| 130 static scoped_ptr<Channel> CreateNamedServer( | 131 static scoped_ptr<Channel> CreateNamedServer( |
| 131 const IPC::ChannelHandle &channel_handle, Listener* listener); | 132 const IPC::ChannelHandle &channel_handle, Listener* listener); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 142 const IPC::ChannelHandle &channel_handle, Listener* listener); | 143 const IPC::ChannelHandle &channel_handle, Listener* listener); |
| 143 | 144 |
| 144 | 145 |
| 145 virtual ~Channel(); | 146 virtual ~Channel(); |
| 146 | 147 |
| 147 // Connect the pipe. On the server side, this will initiate | 148 // Connect the pipe. On the server side, this will initiate |
| 148 // waiting for connections. On the client, it attempts to | 149 // waiting for connections. On the client, it attempts to |
| 149 // connect to a pre-existing pipe. Note, calling Connect() | 150 // connect to a pre-existing pipe. Note, calling Connect() |
| 150 // will not block the calling thread and may complete | 151 // will not block the calling thread and may complete |
| 151 // asynchronously. | 152 // asynchronously. |
| 152 bool Connect() WARN_UNUSED_RESULT; | 153 virtual bool Connect() WARN_UNUSED_RESULT = 0; |
| 153 | 154 |
| 154 // Close this Channel explicitly. May be called multiple times. | 155 // Close this Channel explicitly. May be called multiple times. |
| 155 // On POSIX calling close on an IPC channel that listens for connections will | 156 // On POSIX calling close on an IPC channel that listens for connections will |
| 156 // cause it to close any accepted connections, and it will stop listening for | 157 // cause it to close any accepted connections, and it will stop listening for |
| 157 // new connections. If you just want to close the currently accepted | 158 // new connections. If you just want to close the currently accepted |
| 158 // connection and listen for new ones, use ResetToAcceptingConnectionState. | 159 // connection and listen for new ones, use ResetToAcceptingConnectionState. |
| 159 void Close(); | 160 virtual void Close() = 0; |
| 160 | 161 |
| 161 // Get the process ID for the connected peer. | 162 // Get the process ID for the connected peer. |
| 162 // | 163 // |
| 163 // Returns base::kNullProcessId if the peer is not connected yet. Watch out | 164 // Returns base::kNullProcessId if the peer is not connected yet. Watch out |
| 164 // for race conditions. You can easily get a channel to another process, but | 165 // for race conditions. You can easily get a channel to another process, but |
| 165 // if your process has not yet processed the "hello" message from the remote | 166 // if your process has not yet processed the "hello" message from the remote |
| 166 // side, this will fail. You should either make sure calling this is either | 167 // side, this will fail. You should either make sure calling this is either |
| 167 // in response to a message from the remote side (which guarantees that it's | 168 // in response to a message from the remote side (which guarantees that it's |
| 168 // been connected), or you wait for the "connected" notification on the | 169 // been connected), or you wait for the "connected" notification on the |
| 169 // listener. | 170 // listener. |
| 170 base::ProcessId peer_pid() const; | 171 virtual base::ProcessId GetPeerPID() const = 0; |
| 171 | 172 |
| 172 // Send a message over the Channel to the listener on the other end. | 173 // Send a message over the Channel to the listener on the other end. |
| 173 // | 174 // |
| 174 // |message| must be allocated using operator new. This object will be | 175 // |message| must be allocated using operator new. This object will be |
| 175 // deleted once the contents of the Message have been sent. | 176 // deleted once the contents of the Message have been sent. |
| 176 virtual bool Send(Message* message) OVERRIDE; | 177 virtual bool Send(Message* message) = 0; |
| 177 | 178 |
| 178 #if defined(OS_POSIX) | 179 #if defined(OS_POSIX) && !defined(OS_NACL) |
| 179 // On POSIX an IPC::Channel wraps a socketpair(), this method returns the | 180 // On POSIX an IPC::Channel wraps a socketpair(), this method returns the |
| 180 // FD # for the client end of the socket. | 181 // FD # for the client end of the socket. |
| 181 // This method may only be called on the server side of a channel. | 182 // This method may only be called on the server side of a channel. |
| 182 // This method can be called on any thread. | 183 // This method can be called on any thread. |
| 183 int GetClientFileDescriptor() const; | 184 virtual int GetClientFileDescriptor() const = 0; |
| 184 | 185 |
| 185 // Same as GetClientFileDescriptor, but transfers the ownership of the | 186 // Same as GetClientFileDescriptor, but transfers the ownership of the |
| 186 // file descriptor to the caller. | 187 // file descriptor to the caller. |
| 187 // This method can be called on any thread. | 188 // This method can be called on any thread. |
| 188 int TakeClientFileDescriptor(); | 189 virtual int TakeClientFileDescriptor() = 0; |
| 189 | 190 |
| 190 // On POSIX an IPC::Channel can either wrap an established socket, or it | 191 // On POSIX an IPC::Channel can either wrap an established socket, or it |
| 191 // can wrap a socket that is listening for connections. Currently an | 192 // can wrap a socket that is listening for connections. Currently an |
| 192 // IPC::Channel that listens for connections can only accept one connection | 193 // IPC::Channel that listens for connections can only accept one connection |
| 193 // at a time. | 194 // at a time. |
| 194 | 195 |
| 195 // Returns true if the channel supports listening for connections. | 196 // Returns true if the channel supports listening for connections. |
| 196 bool AcceptsConnections() const; | 197 virtual bool AcceptsConnections() const = 0; |
| 197 | 198 |
| 198 // Returns true if the channel supports listening for connections and is | 199 // Returns true if the channel supports listening for connections and is |
| 199 // currently connected. | 200 // currently connected. |
| 200 bool HasAcceptedConnection() const; | 201 virtual bool HasAcceptedConnection() const = 0; |
| 201 | 202 |
| 202 // Returns true if the peer process' effective user id can be determined, in | 203 // Returns true if the peer process' effective user id can be determined, in |
| 203 // which case the supplied peer_euid is updated with it. | 204 // which case the supplied peer_euid is updated with it. |
| 204 bool GetPeerEuid(uid_t* peer_euid) const; | 205 virtual bool GetPeerEuid(uid_t* peer_euid) const = 0; |
| 205 | 206 |
| 206 // Closes any currently connected socket, and returns to a listening state | 207 // Closes any currently connected socket, and returns to a listening state |
| 207 // for more connections. | 208 // for more connections. |
| 208 void ResetToAcceptingConnectionState(); | 209 virtual void ResetToAcceptingConnectionState() = 0; |
| 209 #endif // defined(OS_POSIX) && !defined(OS_NACL) | 210 #endif // defined(OS_POSIX) && !defined(OS_NACL) |
| 210 | 211 |
| 211 // Returns true if a named server channel is initialized on the given channel | 212 // Returns true if a named server channel is initialized on the given channel |
| 212 // ID. Even if true, the server may have already accepted a connection. | 213 // ID. Even if true, the server may have already accepted a connection. |
| 213 static bool IsNamedServerInitialized(const std::string& channel_id); | 214 static bool IsNamedServerInitialized(const std::string& channel_id); |
| 214 | 215 |
| 215 #if !defined(OS_NACL) | 216 #if !defined(OS_NACL) |
| 216 // Generates a channel ID that's non-predictable and unique. | 217 // Generates a channel ID that's non-predictable and unique. |
| 217 static std::string GenerateUniqueRandomChannelID(); | 218 static std::string GenerateUniqueRandomChannelID(); |
| 218 | 219 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 231 #endif | 232 #endif |
| 232 | 233 |
| 233 #if defined(OS_ANDROID) | 234 #if defined(OS_ANDROID) |
| 234 // Most tests are single process and work the same on all platforms. However | 235 // Most tests are single process and work the same on all platforms. However |
| 235 // in some cases we want to test multi-process, and Android differs in that it | 236 // in some cases we want to test multi-process, and Android differs in that it |
| 236 // can't 'exec' after forking. This callback resets any data in the forked | 237 // can't 'exec' after forking. This callback resets any data in the forked |
| 237 // process such that it acts similar to if it was exec'd, for tests. | 238 // process such that it acts similar to if it was exec'd, for tests. |
| 238 static void NotifyProcessForkedForTesting(); | 239 static void NotifyProcessForkedForTesting(); |
| 239 #endif | 240 #endif |
| 240 | 241 |
| 241 protected: | |
| 242 // Used in Chrome by the TestSink to provide a dummy channel implementation | |
| 243 // for testing. TestSink overrides the "interesting" functions in Channel so | |
| 244 // no actual implementation is needed. This will cause un-overridden calls to | |
| 245 // segfault. Do not use outside of test code! | |
| 246 Channel() : channel_impl_(0) { } | |
| 247 | |
| 248 private: | |
| 249 Channel(const IPC::ChannelHandle &channel_handle, Mode mode, | |
| 250 Listener* listener); | |
| 251 | |
| 252 // PIMPL to which all channel calls are delegated. | |
| 253 class ChannelImpl; | |
| 254 ChannelImpl *channel_impl_; | |
| 255 }; | 242 }; |
| 256 | 243 |
| 257 #if defined(OS_POSIX) | 244 #if defined(OS_POSIX) |
| 258 // SocketPair() creates a pair of socket FDs suitable for using with | 245 // SocketPair() creates a pair of socket FDs suitable for using with |
| 259 // IPC::Channel. | 246 // IPC::Channel. |
| 260 IPC_EXPORT bool SocketPair(int* fd1, int* fd2); | 247 IPC_EXPORT bool SocketPair(int* fd1, int* fd2); |
| 261 #endif | 248 #endif |
| 262 | 249 |
| 263 } // namespace IPC | 250 } // namespace IPC |
| 264 | 251 |
| 265 #endif // IPC_IPC_CHANNEL_H_ | 252 #endif // IPC_IPC_CHANNEL_H_ |
| OLD | NEW |