1 : // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
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 CHROME_COMMON_IPC_CHANNEL_H_
6 : #define CHROME_COMMON_IPC_CHANNEL_H_
7 :
8 : #include "chrome/common/ipc_message.h"
9 :
10 : namespace IPC {
11 :
12 : //------------------------------------------------------------------------------
13 :
14 : class Channel : public Message::Sender {
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 2 : class Listener {
21 : public:
22 0 : virtual ~Listener() {}
23 :
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 0 : 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 0 : virtual void OnChannelError() {}
34 : };
35 :
36 : enum Mode {
37 : MODE_SERVER,
38 : MODE_CLIENT
39 : };
40 :
41 : enum {
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
48 : };
49 :
50 : // Initialize a Channel.
51 : //
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.
59 : //
60 : Channel(const std::wstring& channel_id, Mode mode, Listener* listener);
61 :
62 : // XXX it would nice not to have yet more platform-specific code in
63 : // here but it's just not worth the trouble.
64 : # if defined(OS_POSIX)
65 : // Connect to a pre-created channel |fd| as |mode|.
66 : Channel(int fd, Mode mode, Listener* listener);
67 : # elif defined(OS_WIN)
68 : // Connect to a pre-created channel as |mode|. Clients connect to
69 : // the pre-existing server pipe, and servers take over |server_pipe|.
70 : Channel(const std::wstring& channel_id, void* server_pipe,
71 : Mode mode, Listener* listener);
72 : # endif
73 :
74 : ~Channel();
75 :
76 : // Connect the pipe. On the server side, this will initiate
77 : // waiting for connections. On the client, it attempts to
78 : // connect to a pre-existing pipe. Note, calling Connect()
79 : // will not block the calling thread and may complete
80 : // asynchronously.
81 : bool Connect();
82 :
83 : // Close this Channel explicitly. May be called multiple times.
84 : void Close();
85 :
86 : // Modify the Channel's listener.
87 : Listener* set_listener(Listener* listener);
88 :
89 : // Send a message over the Channel to the listener on the other end.
90 : //
91 : // |message| must be allocated using operator new. This object will be
92 : // deleted once the contents of the Message have been sent.
93 : //
94 : // FIXME bug 551500: the channel does not notice failures, so if the
95 : // renderer crashes, it will silently succeed, leaking the parameter.
96 : // At least the leak will be fixed by...
97 : //
98 : virtual bool Send(Message* message);
99 :
100 : #if defined(OS_POSIX)
101 : // On POSIX an IPC::Channel wraps a socketpair(), this method returns the
102 : // FD # for the client end of the socket and the equivalent FD# to use for
103 : // mapping it into the Child process.
104 : // This method may only be called on the server side of a channel.
105 : //
106 : // If the kTestingChannelID flag is specified on the command line then
107 : // a named FIFO is used as the channel transport mechanism rather than a
108 : // socketpair() in which case this method returns -1 for both parameters.
109 : void GetClientFileDescriptorMapping(int *src_fd, int *dest_fd) const;
110 :
111 : // Return the server side of the socketpair.
112 : int GetServerFileDescriptor() const;
113 : #elif defined(OS_WIN)
114 : // Return the server pipe handle.
115 : void* GetServerPipeHandle() const;
116 : #endif // defined(OS_POSIX)
117 :
118 : private:
119 : // PIMPL to which all channel calls are delegated.
120 : class ChannelImpl;
121 : ChannelImpl *channel_impl_;
122 :
123 : // The Hello message is internal to the Channel class. It is sent
124 : // by the peer when the channel is connected. The message contains
125 : // just the process id (pid). The message has a special routing_id
126 : // (MSG_ROUTING_NONE) and type (HELLO_MESSAGE_TYPE).
127 : enum {
128 : HELLO_MESSAGE_TYPE = kuint16max // Maximum value of message type (uint16),
129 : // to avoid conflicting with normal
130 : // message types, which are enumeration
131 : // constants starting from 0.
132 : };
133 : };
134 :
135 : } // namespace IPC
136 :
137 : #endif // CHROME_COMMON_IPC_CHANNEL_H_
|