| 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) |
| 11 #include <sys/types.h> | 11 #include <sys/types.h> |
| 12 #endif | 12 #endif |
| 13 | 13 |
| 14 #include "base/compiler_specific.h" | 14 #include "base/compiler_specific.h" |
| 15 #include "base/files/scoped_file.h" | 15 #include "base/files/scoped_file.h" |
| 16 #include "base/process/process.h" | 16 #include "base/process/process.h" |
| 17 #include "ipc/ipc_channel_handle.h" | 17 #include "ipc/ipc_channel_handle.h" |
| 18 #include "ipc/ipc_endpoint.h" |
| 18 #include "ipc/ipc_message.h" | 19 #include "ipc/ipc_message.h" |
| 19 #include "ipc/ipc_sender.h" | |
| 20 | 20 |
| 21 namespace IPC { | 21 namespace IPC { |
| 22 | 22 |
| 23 class AttachmentBroker; | 23 class AttachmentBroker; |
| 24 class Listener; | 24 class Listener; |
| 25 | 25 |
| 26 //------------------------------------------------------------------------------ | 26 //------------------------------------------------------------------------------ |
| 27 // See | 27 // See |
| 28 // http://www.chromium.org/developers/design-documents/inter-process-communicati
on | 28 // http://www.chromium.org/developers/design-documents/inter-process-communicati
on |
| 29 // for overview of IPC in Chromium. | 29 // for overview of IPC in Chromium. |
| 30 | 30 |
| 31 // Channels are implemented using named pipes on Windows, and | 31 // Channels are implemented using named pipes on Windows, and |
| 32 // socket pairs (or in some special cases unix domain sockets) on POSIX. | 32 // socket pairs (or in some special cases unix domain sockets) on POSIX. |
| 33 // On Windows we access pipes in various processes by name. | 33 // On Windows we access pipes in various processes by name. |
| 34 // On POSIX we pass file descriptors to child processes and assign names to them | 34 // On POSIX we pass file descriptors to child processes and assign names to them |
| 35 // in a lookup table. | 35 // in a lookup table. |
| 36 // In general on POSIX we do not use unix domain sockets due to security | 36 // In general on POSIX we do not use unix domain sockets due to security |
| 37 // concerns and the fact that they can leave garbage around the file system | 37 // concerns and the fact that they can leave garbage around the file system |
| 38 // (MacOS does not support abstract named unix domain sockets). | 38 // (MacOS does not support abstract named unix domain sockets). |
| 39 // You can use unix domain sockets if you like on POSIX by constructing the | 39 // You can use unix domain sockets if you like on POSIX by constructing the |
| 40 // the channel with the mode set to one of the NAMED modes. NAMED modes are | 40 // the channel with the mode set to one of the NAMED modes. NAMED modes are |
| 41 // currently used by automation and service processes. | 41 // currently used by automation and service processes. |
| 42 | 42 |
| 43 class IPC_EXPORT Channel : public Sender { | 43 class IPC_EXPORT Channel : public Endpoint { |
| 44 // Security tests need access to the pipe handle. | 44 // Security tests need access to the pipe handle. |
| 45 friend class ChannelTest; | 45 friend class ChannelTest; |
| 46 | 46 |
| 47 public: | 47 public: |
| 48 // Flags to test modes | 48 // Flags to test modes |
| 49 enum ModeFlags { | 49 enum ModeFlags { |
| 50 MODE_NO_FLAG = 0x0, | 50 MODE_NO_FLAG = 0x0, |
| 51 MODE_SERVER_FLAG = 0x1, | 51 MODE_SERVER_FLAG = 0x1, |
| 52 MODE_CLIENT_FLAG = 0x2, | 52 MODE_CLIENT_FLAG = 0x2, |
| 53 MODE_NAMED_FLAG = 0x4, | 53 MODE_NAMED_FLAG = 0x4, |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 158 AttachmentBroker* broker); | 158 AttachmentBroker* broker); |
| 159 #endif | 159 #endif |
| 160 // TODO(erikchen): Remove default parameter for |broker|. It exists only to | 160 // TODO(erikchen): Remove default parameter for |broker|. It exists only to |
| 161 // make the upcoming refactor decomposable into smaller CLs. | 161 // make the upcoming refactor decomposable into smaller CLs. |
| 162 // http://crbug.com/493414. | 162 // http://crbug.com/493414. |
| 163 static scoped_ptr<Channel> CreateServer( | 163 static scoped_ptr<Channel> CreateServer( |
| 164 const IPC::ChannelHandle& channel_handle, | 164 const IPC::ChannelHandle& channel_handle, |
| 165 Listener* listener, | 165 Listener* listener, |
| 166 AttachmentBroker* broker = nullptr); | 166 AttachmentBroker* broker = nullptr); |
| 167 | 167 |
| 168 Channel() : attachment_broker_endpoint_(false) {} | |
| 169 ~Channel() override; | 168 ~Channel() override; |
| 170 | 169 |
| 171 // Connect the pipe. On the server side, this will initiate | 170 // Connect the pipe. On the server side, this will initiate |
| 172 // waiting for connections. On the client, it attempts to | 171 // waiting for connections. On the client, it attempts to |
| 173 // connect to a pre-existing pipe. Note, calling Connect() | 172 // connect to a pre-existing pipe. Note, calling Connect() |
| 174 // will not block the calling thread and may complete | 173 // will not block the calling thread and may complete |
| 175 // asynchronously. | 174 // asynchronously. |
| 176 virtual bool Connect() WARN_UNUSED_RESULT = 0; | 175 virtual bool Connect() WARN_UNUSED_RESULT = 0; |
| 177 | 176 |
| 178 // Close this Channel explicitly. May be called multiple times. | 177 // Close this Channel explicitly. May be called multiple times. |
| 179 // On POSIX calling close on an IPC channel that listens for connections will | 178 // On POSIX calling close on an IPC channel that listens for connections will |
| 180 // cause it to close any accepted connections, and it will stop listening for | 179 // cause it to close any accepted connections, and it will stop listening for |
| 181 // new connections. If you just want to close the currently accepted | 180 // new connections. If you just want to close the currently accepted |
| 182 // connection and listen for new ones, use ResetToAcceptingConnectionState. | 181 // connection and listen for new ones, use ResetToAcceptingConnectionState. |
| 183 virtual void Close() = 0; | 182 virtual void Close() = 0; |
| 184 | 183 |
| 185 // Get the process ID for the connected peer. | |
| 186 // | |
| 187 // Returns base::kNullProcessId if the peer is not connected yet. Watch out | |
| 188 // for race conditions. You can easily get a channel to another process, but | |
| 189 // if your process has not yet processed the "hello" message from the remote | |
| 190 // side, this will fail. You should either make sure calling this is either | |
| 191 // in response to a message from the remote side (which guarantees that it's | |
| 192 // been connected), or you wait for the "connected" notification on the | |
| 193 // listener. | |
| 194 virtual base::ProcessId GetPeerPID() const = 0; | |
| 195 | |
| 196 // Get its own process id. This value is told to the peer. | 184 // Get its own process id. This value is told to the peer. |
| 197 virtual base::ProcessId GetSelfPID() const = 0; | 185 virtual base::ProcessId GetSelfPID() const = 0; |
| 198 | 186 |
| 199 // Overridden from ipc::Sender. | 187 // Overridden from ipc::Sender. |
| 200 // Send a message over the Channel to the listener on the other end. | 188 // Send a message over the Channel to the listener on the other end. |
| 201 // | 189 // |
| 202 // |message| must be allocated using operator new. This object will be | 190 // |message| must be allocated using operator new. This object will be |
| 203 // deleted once the contents of the Message have been sent. | 191 // deleted once the contents of the Message have been sent. |
| 204 bool Send(Message* message) override = 0; | 192 bool Send(Message* message) override = 0; |
| 205 | 193 |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 245 static void SetGlobalPid(int pid); | 233 static void SetGlobalPid(int pid); |
| 246 #endif | 234 #endif |
| 247 | 235 |
| 248 #if defined(OS_ANDROID) | 236 #if defined(OS_ANDROID) |
| 249 // Most tests are single process and work the same on all platforms. However | 237 // Most tests are single process and work the same on all platforms. However |
| 250 // in some cases we want to test multi-process, and Android differs in that it | 238 // in some cases we want to test multi-process, and Android differs in that it |
| 251 // can't 'exec' after forking. This callback resets any data in the forked | 239 // can't 'exec' after forking. This callback resets any data in the forked |
| 252 // process such that it acts similar to if it was exec'd, for tests. | 240 // process such that it acts similar to if it was exec'd, for tests. |
| 253 static void NotifyProcessForkedForTesting(); | 241 static void NotifyProcessForkedForTesting(); |
| 254 #endif | 242 #endif |
| 255 | |
| 256 void set_attachment_broker_endpoint(bool is_endpoint) { | |
| 257 attachment_broker_endpoint_ = is_endpoint; | |
| 258 } | |
| 259 | |
| 260 protected: | |
| 261 bool is_attachment_broker_endpoint() { return attachment_broker_endpoint_; } | |
| 262 | |
| 263 private: | |
| 264 // Whether this channel is used as an endpoint for sending and receiving | |
| 265 // brokerable attachment messages to/from the broker process. | |
| 266 bool attachment_broker_endpoint_; | |
| 267 }; | 243 }; |
| 268 | 244 |
| 269 #if defined(OS_POSIX) | 245 #if defined(OS_POSIX) |
| 270 // SocketPair() creates a pair of socket FDs suitable for using with | 246 // SocketPair() creates a pair of socket FDs suitable for using with |
| 271 // IPC::Channel. | 247 // IPC::Channel. |
| 272 IPC_EXPORT bool SocketPair(int* fd1, int* fd2); | 248 IPC_EXPORT bool SocketPair(int* fd1, int* fd2); |
| 273 #endif | 249 #endif |
| 274 | 250 |
| 275 } // namespace IPC | 251 } // namespace IPC |
| 276 | 252 |
| 277 #endif // IPC_IPC_CHANNEL_H_ | 253 #endif // IPC_IPC_CHANNEL_H_ |
| OLD | NEW |