| 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 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| 11 #if defined(OS_POSIX) | 11 #if defined(OS_POSIX) |
| 12 #include <sys/types.h> | 12 #include <sys/types.h> |
| 13 #endif | 13 #endif |
| 14 | 14 |
| 15 #include "base/compiler_specific.h" | 15 #include "base/compiler_specific.h" |
| 16 #include "base/process.h" | 16 #include "base/process.h" |
| 17 #include "ipc/ipc_channel_handle.h" | 17 #include "ipc/ipc_channel_handle.h" |
| 18 #include "ipc/ipc_message.h" | 18 #include "ipc/ipc_message.h" |
| 19 #include "ipc/ipc_message_sender.h" |
| 20 |
| 21 // TODO(brettw) remove this when the "typedef Sender" is removed below. |
| 22 #include "ipc/ipc_channel_listener.h" |
| 19 | 23 |
| 20 namespace IPC { | 24 namespace IPC { |
| 21 | 25 |
| 26 class ChannelListener; |
| 27 |
| 22 //------------------------------------------------------------------------------ | 28 //------------------------------------------------------------------------------ |
| 23 // See | 29 // See |
| 24 // http://www.chromium.org/developers/design-documents/inter-process-communicati
on | 30 // http://www.chromium.org/developers/design-documents/inter-process-communicati
on |
| 25 // for overview of IPC in Chromium. | 31 // for overview of IPC in Chromium. |
| 26 | 32 |
| 27 // Channels are implemented using named pipes on Windows, and | 33 // Channels are implemented using named pipes on Windows, and |
| 28 // socket pairs (or in some special cases unix domain sockets) on POSIX. | 34 // socket pairs (or in some special cases unix domain sockets) on POSIX. |
| 29 // On Windows we access pipes in various processes by name. | 35 // On Windows we access pipes in various processes by name. |
| 30 // On POSIX we pass file descriptors to child processes and assign names to them | 36 // On POSIX we pass file descriptors to child processes and assign names to them |
| 31 // in a lookup table. | 37 // in a lookup table. |
| 32 // In general on POSIX we do not use unix domain sockets due to security | 38 // In general on POSIX we do not use unix domain sockets due to security |
| 33 // concerns and the fact that they can leave garbage around the file system | 39 // concerns and the fact that they can leave garbage around the file system |
| 34 // (MacOS does not support abstract named unix domain sockets). | 40 // (MacOS does not support abstract named unix domain sockets). |
| 35 // You can use unix domain sockets if you like on POSIX by constructing the | 41 // You can use unix domain sockets if you like on POSIX by constructing the |
| 36 // the channel with the mode set to one of the NAMED modes. NAMED modes are | 42 // the channel with the mode set to one of the NAMED modes. NAMED modes are |
| 37 // currently used by automation and service processes. | 43 // currently used by automation and service processes. |
| 38 | 44 |
| 39 class IPC_EXPORT Channel : public Message::Sender { | 45 class IPC_EXPORT Channel : public MessageSender { |
| 40 // Security tests need access to the pipe handle. | 46 // Security tests need access to the pipe handle. |
| 41 friend class ChannelTest; | 47 friend class ChannelTest; |
| 42 | 48 |
| 43 public: | 49 public: |
| 44 // Implemented by consumers of a Channel to receive messages. | 50 // IPC::ChannelListener used to be IPC::Channel::Listener which prevented |
| 45 class IPC_EXPORT Listener { | 51 // forward declarations. To keep existing code compiling, we provide this |
| 46 public: | 52 // backwards-compatible definition. New code should use IPC::ChannelListener. |
| 47 virtual ~Listener() {} | 53 // TODO(brettw) converto users of this and delete. |
| 48 | 54 typedef ChannelListener Listener; |
| 49 // Called when a message is received. Returns true iff the message was | |
| 50 // handled. | |
| 51 virtual bool OnMessageReceived(const Message& message) = 0; | |
| 52 | |
| 53 // Called when the channel is connected and we have received the internal | |
| 54 // Hello message from the peer. | |
| 55 virtual void OnChannelConnected(int32 peer_pid) {} | |
| 56 | |
| 57 // Called when an error is detected that causes the channel to close. | |
| 58 // This method is not called when a channel is closed normally. | |
| 59 virtual void OnChannelError() {} | |
| 60 | |
| 61 #if defined(OS_POSIX) | |
| 62 // Called on the server side when a channel that listens for connections | |
| 63 // denies an attempt to connect. | |
| 64 virtual void OnChannelDenied() {} | |
| 65 | |
| 66 // Called on the server side when a channel that listens for connections | |
| 67 // has an error that causes the listening channel to close. | |
| 68 virtual void OnChannelListenError() {} | |
| 69 #endif // OS_POSIX | |
| 70 }; | |
| 71 | 55 |
| 72 // Flags to test modes | 56 // Flags to test modes |
| 73 enum ModeFlags { | 57 enum ModeFlags { |
| 74 MODE_NO_FLAG = 0x0, | 58 MODE_NO_FLAG = 0x0, |
| 75 MODE_SERVER_FLAG = 0x1, | 59 MODE_SERVER_FLAG = 0x1, |
| 76 MODE_CLIENT_FLAG = 0x2, | 60 MODE_CLIENT_FLAG = 0x2, |
| 77 MODE_NAMED_FLAG = 0x4, | 61 MODE_NAMED_FLAG = 0x4, |
| 78 #if defined(OS_POSIX) | 62 #if defined(OS_POSIX) |
| 79 MODE_OPEN_ACCESS_FLAG = 0x8, // Don't restrict access based on client UID. | 63 MODE_OPEN_ACCESS_FLAG = 0x8, // Don't restrict access based on client UID. |
| 80 #endif | 64 #endif |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 126 // ownership of the file descriptor and will close it appropriately, otherwise | 110 // ownership of the file descriptor and will close it appropriately, otherwise |
| 127 // it will create a new descriptor internally. | 111 // it will create a new descriptor internally. |
| 128 // |mode| specifies whether this Channel is to operate in server mode or | 112 // |mode| specifies whether this Channel is to operate in server mode or |
| 129 // client mode. In server mode, the Channel is responsible for setting up the | 113 // client mode. In server mode, the Channel is responsible for setting up the |
| 130 // IPC object, whereas in client mode, the Channel merely connects to the | 114 // IPC object, whereas in client mode, the Channel merely connects to the |
| 131 // already established IPC object. | 115 // already established IPC object. |
| 132 // |listener| receives a callback on the current thread for each newly | 116 // |listener| receives a callback on the current thread for each newly |
| 133 // received message. | 117 // received message. |
| 134 // | 118 // |
| 135 Channel(const IPC::ChannelHandle &channel_handle, Mode mode, | 119 Channel(const IPC::ChannelHandle &channel_handle, Mode mode, |
| 136 Listener* listener); | 120 ChannelListener* listener); |
| 137 | 121 |
| 138 virtual ~Channel(); | 122 virtual ~Channel(); |
| 139 | 123 |
| 140 // Connect the pipe. On the server side, this will initiate | 124 // Connect the pipe. On the server side, this will initiate |
| 141 // waiting for connections. On the client, it attempts to | 125 // waiting for connections. On the client, it attempts to |
| 142 // connect to a pre-existing pipe. Note, calling Connect() | 126 // connect to a pre-existing pipe. Note, calling Connect() |
| 143 // will not block the calling thread and may complete | 127 // will not block the calling thread and may complete |
| 144 // asynchronously. | 128 // asynchronously. |
| 145 bool Connect() WARN_UNUSED_RESULT; | 129 bool Connect() WARN_UNUSED_RESULT; |
| 146 | 130 |
| 147 // Close this Channel explicitly. May be called multiple times. | 131 // Close this Channel explicitly. May be called multiple times. |
| 148 // On POSIX calling close on an IPC channel that listens for connections will | 132 // On POSIX calling close on an IPC channel that listens for connections will |
| 149 // cause it to close any accepted connections, and it will stop listening for | 133 // cause it to close any accepted connections, and it will stop listening for |
| 150 // new connections. If you just want to close the currently accepted | 134 // new connections. If you just want to close the currently accepted |
| 151 // connection and listen for new ones, use ResetToAcceptingConnectionState. | 135 // connection and listen for new ones, use ResetToAcceptingConnectionState. |
| 152 void Close(); | 136 void Close(); |
| 153 | 137 |
| 154 // Modify the Channel's listener. | 138 // Modify the Channel's listener. |
| 155 void set_listener(Listener* listener); | 139 void set_listener(ChannelListener* listener); |
| 156 | 140 |
| 157 // Get the process ID for the connected peer. | 141 // Get the process ID for the connected peer. |
| 158 // Returns base::kNullProcessId if the peer is not connected yet. | 142 // Returns base::kNullProcessId if the peer is not connected yet. |
| 159 base::ProcessId peer_pid() const; | 143 base::ProcessId peer_pid() const; |
| 160 | 144 |
| 161 // Send a message over the Channel to the listener on the other end. | 145 // Send a message over the Channel to the listener on the other end. |
| 162 // | 146 // |
| 163 // |message| must be allocated using operator new. This object will be | 147 // |message| must be allocated using operator new. This object will be |
| 164 // deleted once the contents of the Message have been sent. | 148 // deleted once the contents of the Message have been sent. |
| 165 virtual bool Send(Message* message) OVERRIDE; | 149 virtual bool Send(Message* message) OVERRIDE; |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 226 | 210 |
| 227 private: | 211 private: |
| 228 // PIMPL to which all channel calls are delegated. | 212 // PIMPL to which all channel calls are delegated. |
| 229 class ChannelImpl; | 213 class ChannelImpl; |
| 230 ChannelImpl *channel_impl_; | 214 ChannelImpl *channel_impl_; |
| 231 }; | 215 }; |
| 232 | 216 |
| 233 } // namespace IPC | 217 } // namespace IPC |
| 234 | 218 |
| 235 #endif // IPC_IPC_CHANNEL_H_ | 219 #endif // IPC_IPC_CHANNEL_H_ |
| OLD | NEW |