neb@chromium.org | 74f2754 | 2010-03-31 17:44:57 | [diff] [blame^] | 1 | // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
agl@chromium.org | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 5 | #ifndef IPC_IPC_CHANNEL_H_ |
| 6 | #define IPC_IPC_CHANNEL_H_ |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 7 | |
agl@chromium.org | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 8 | #include "ipc/ipc_message.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 9 | |
| 10 | namespace IPC { |
| 11 | |
| 12 | //------------------------------------------------------------------------------ |
| 13 | |
jeremy@chromium.org | 514411fc | 2008-12-10 22:28:11 | [diff] [blame] | 14 | class Channel : public Message::Sender { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 15 | // Security tests need access to the pipe handle. |
| 16 | friend class ChannelTest; |
| 17 | |
| 18 | public: |
| 19 | // Implemented by consumers of a Channel to receive messages. |
| 20 | class Listener { |
| 21 | public: |
darin@google.com | c8ea1ef | 2008-09-06 18:07:10 | [diff] [blame] | 22 | virtual ~Listener() {} |
| 23 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 24 | // Called when a message is received. |
| 25 | virtual void OnMessageReceived(const Message& message) = 0; |
| 26 | |
| 27 | // Called when the channel is connected and we have received the internal |
| 28 | // Hello message from the peer. |
| 29 | virtual void OnChannelConnected(int32 peer_pid) {} |
| 30 | |
| 31 | // Called when an error is detected that causes the channel to close. |
| 32 | // This method is not called when a channel is closed normally. |
| 33 | virtual void OnChannelError() {} |
| 34 | }; |
| 35 | |
| 36 | enum Mode { |
| 37 | MODE_SERVER, |
| 38 | MODE_CLIENT |
| 39 | }; |
| 40 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 41 | enum { |
jeremy@chromium.org | 514411fc | 2008-12-10 22:28:11 | [diff] [blame] | 42 | // The maximum message size in bytes. Attempting to receive a |
| 43 | // message of this size or bigger results in a channel error. |
| 44 | kMaximumMessageSize = 256 * 1024 * 1024, |
| 45 | |
| 46 | // Ammount of data to read at once from the pipe. |
| 47 | kReadBufferSize = 4 * 1024 |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 48 | }; |
| 49 | |
| 50 | // Initialize a Channel. |
| 51 | // |
rvargas@google.com | c1afbd2c | 2008-10-13 19:19:36 | [diff] [blame] | 52 | // |channel_id| identifies the communication Channel. |
| 53 | // |mode| specifies whether this Channel is to operate in server mode or |
| 54 | // client mode. In server mode, the Channel is responsible for setting up the |
| 55 | // IPC object, whereas in client mode, the Channel merely connects to the |
| 56 | // already established IPC object. |
| 57 | // |listener| receives a callback on the current thread for each newly |
| 58 | // received message. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 59 | // |
evan@chromium.org | 9a3a293b | 2009-06-04 22:28:16 | [diff] [blame] | 60 | Channel(const std::string& channel_id, Mode mode, Listener* listener); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 61 | |
jeremy@chromium.org | 514411fc | 2008-12-10 22:28:11 | [diff] [blame] | 62 | ~Channel(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 63 | |
| 64 | // Connect the pipe. On the server side, this will initiate |
| 65 | // waiting for connections. On the client, it attempts to |
| 66 | // connect to a pre-existing pipe. Note, calling Connect() |
| 67 | // will not block the calling thread and may complete |
| 68 | // asynchronously. |
| 69 | bool Connect(); |
| 70 | |
| 71 | // Close this Channel explicitly. May be called multiple times. |
| 72 | void Close(); |
| 73 | |
| 74 | // Modify the Channel's listener. |
jeremy@chromium.org | 514411fc | 2008-12-10 22:28:11 | [diff] [blame] | 75 | void set_listener(Listener* listener); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 76 | |
| 77 | // Send a message over the Channel to the listener on the other end. |
| 78 | // |
rvargas@google.com | c1afbd2c | 2008-10-13 19:19:36 | [diff] [blame] | 79 | // |message| must be allocated using operator new. This object will be |
| 80 | // deleted once the contents of the Message have been sent. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 81 | // |
| 82 | // FIXME bug 551500: the channel does not notice failures, so if the |
| 83 | // renderer crashes, it will silently succeed, leaking the parameter. |
| 84 | // At least the leak will be fixed by... |
| 85 | // |
| 86 | virtual bool Send(Message* message); |
| 87 | |
jeremy@chromium.org | df3c1ca1 | 2008-12-19 21:37:01 | [diff] [blame] | 88 | #if defined(OS_POSIX) |
| 89 | // On POSIX an IPC::Channel wraps a socketpair(), this method returns the |
agl@chromium.org | cc8f146 | 2009-06-12 17:36:55 | [diff] [blame] | 90 | // FD # for the client end of the socket. |
jeremy@chromium.org | df3c1ca1 | 2008-12-19 21:37:01 | [diff] [blame] | 91 | // This method may only be called on the server side of a channel. |
| 92 | // |
| 93 | // If the kTestingChannelID flag is specified on the command line then |
| 94 | // a named FIFO is used as the channel transport mechanism rather than a |
agl@chromium.org | cc8f146 | 2009-06-12 17:36:55 | [diff] [blame] | 95 | // socketpair() in which case this method returns -1. |
| 96 | int GetClientFileDescriptor() const; |
jeremy@chromium.org | df3c1ca1 | 2008-12-19 21:37:01 | [diff] [blame] | 97 | #endif // defined(OS_POSIX) |
| 98 | |
neb@chromium.org | 74f2754 | 2010-03-31 17:44:57 | [diff] [blame^] | 99 | protected: |
| 100 | // Used in Chrome by the TestSink to provide a dummy channel implementation |
| 101 | // for testing. TestSink overrides the "interesting" functions in Channel so |
| 102 | // no actual implementation is needed. This will cause un-overridden calls to |
| 103 | // segfault. Do not use outside of test code! |
| 104 | Channel() : channel_impl_(0) { } |
| 105 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 106 | private: |
jeremy@chromium.org | 514411fc | 2008-12-10 22:28:11 | [diff] [blame] | 107 | // PIMPL to which all channel calls are delegated. |
| 108 | class ChannelImpl; |
| 109 | ChannelImpl *channel_impl_; |
rvargas@google.com | c1afbd2c | 2008-10-13 19:19:36 | [diff] [blame] | 110 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 111 | // The Hello message is internal to the Channel class. It is sent |
| 112 | // by the peer when the channel is connected. The message contains |
| 113 | // just the process id (pid). The message has a special routing_id |
| 114 | // (MSG_ROUTING_NONE) and type (HELLO_MESSAGE_TYPE). |
| 115 | enum { |
nsylvain@chromium.org | d4651ff | 2008-12-02 16:51:58 | [diff] [blame] | 116 | HELLO_MESSAGE_TYPE = kuint16max // Maximum value of message type (uint16), |
| 117 | // to avoid conflicting with normal |
| 118 | // message types, which are enumeration |
| 119 | // constants starting from 0. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 120 | }; |
| 121 | }; |
| 122 | |
jeremy@chromium.org | 514411fc | 2008-12-10 22:28:11 | [diff] [blame] | 123 | } // namespace IPC |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 124 | |
agl@chromium.org | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 125 | #endif // IPC_IPC_CHANNEL_H_ |