Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
michael@0 | 2 | // Use of this source code is governed by a BSD-style license that can be |
michael@0 | 3 | // found in the LICENSE file. |
michael@0 | 4 | |
michael@0 | 5 | #ifndef CHROME_COMMON_IPC_CHANNEL_H_ |
michael@0 | 6 | #define CHROME_COMMON_IPC_CHANNEL_H_ |
michael@0 | 7 | |
michael@0 | 8 | #include <queue> |
michael@0 | 9 | #include "chrome/common/ipc_message.h" |
michael@0 | 10 | |
michael@0 | 11 | namespace IPC { |
michael@0 | 12 | |
michael@0 | 13 | //------------------------------------------------------------------------------ |
michael@0 | 14 | |
michael@0 | 15 | class Channel : public Message::Sender { |
michael@0 | 16 | // Security tests need access to the pipe handle. |
michael@0 | 17 | friend class ChannelTest; |
michael@0 | 18 | |
michael@0 | 19 | public: |
michael@0 | 20 | // Implemented by consumers of a Channel to receive messages. |
michael@0 | 21 | class Listener { |
michael@0 | 22 | public: |
michael@0 | 23 | virtual ~Listener() {} |
michael@0 | 24 | |
michael@0 | 25 | // Called when a message is received. |
michael@0 | 26 | virtual void OnMessageReceived(const Message& message) = 0; |
michael@0 | 27 | |
michael@0 | 28 | // Called when the channel is connected and we have received the internal |
michael@0 | 29 | // Hello message from the peer. |
michael@0 | 30 | virtual void OnChannelConnected(int32_t peer_pid) {} |
michael@0 | 31 | |
michael@0 | 32 | // Called when an error is detected that causes the channel to close. |
michael@0 | 33 | // This method is not called when a channel is closed normally. |
michael@0 | 34 | virtual void OnChannelError() {} |
michael@0 | 35 | |
michael@0 | 36 | // If the listener has queued messages, swap them for |queue| like so |
michael@0 | 37 | // swap(impl->my_queued_messages, queue); |
michael@0 | 38 | virtual void GetQueuedMessages(std::queue<Message>& queue) {} |
michael@0 | 39 | }; |
michael@0 | 40 | |
michael@0 | 41 | enum Mode { |
michael@0 | 42 | MODE_SERVER, |
michael@0 | 43 | MODE_CLIENT |
michael@0 | 44 | }; |
michael@0 | 45 | |
michael@0 | 46 | enum { |
michael@0 | 47 | // The maximum message size in bytes. Attempting to receive a |
michael@0 | 48 | // message of this size or bigger results in a channel error. |
michael@0 | 49 | kMaximumMessageSize = 256 * 1024 * 1024, |
michael@0 | 50 | |
michael@0 | 51 | // Ammount of data to read at once from the pipe. |
michael@0 | 52 | kReadBufferSize = 4 * 1024 |
michael@0 | 53 | }; |
michael@0 | 54 | |
michael@0 | 55 | // Initialize a Channel. |
michael@0 | 56 | // |
michael@0 | 57 | // |channel_id| identifies the communication Channel. |
michael@0 | 58 | // |mode| specifies whether this Channel is to operate in server mode or |
michael@0 | 59 | // client mode. In server mode, the Channel is responsible for setting up the |
michael@0 | 60 | // IPC object, whereas in client mode, the Channel merely connects to the |
michael@0 | 61 | // already established IPC object. |
michael@0 | 62 | // |listener| receives a callback on the current thread for each newly |
michael@0 | 63 | // received message. |
michael@0 | 64 | // |
michael@0 | 65 | Channel(const std::wstring& channel_id, Mode mode, Listener* listener); |
michael@0 | 66 | |
michael@0 | 67 | // XXX it would nice not to have yet more platform-specific code in |
michael@0 | 68 | // here but it's just not worth the trouble. |
michael@0 | 69 | # if defined(OS_POSIX) |
michael@0 | 70 | // Connect to a pre-created channel |fd| as |mode|. |
michael@0 | 71 | Channel(int fd, Mode mode, Listener* listener); |
michael@0 | 72 | # elif defined(OS_WIN) |
michael@0 | 73 | // Connect to a pre-created channel as |mode|. Clients connect to |
michael@0 | 74 | // the pre-existing server pipe, and servers take over |server_pipe|. |
michael@0 | 75 | Channel(const std::wstring& channel_id, void* server_pipe, |
michael@0 | 76 | Mode mode, Listener* listener); |
michael@0 | 77 | # endif |
michael@0 | 78 | |
michael@0 | 79 | ~Channel(); |
michael@0 | 80 | |
michael@0 | 81 | // Connect the pipe. On the server side, this will initiate |
michael@0 | 82 | // waiting for connections. On the client, it attempts to |
michael@0 | 83 | // connect to a pre-existing pipe. Note, calling Connect() |
michael@0 | 84 | // will not block the calling thread and may complete |
michael@0 | 85 | // asynchronously. |
michael@0 | 86 | bool Connect(); |
michael@0 | 87 | |
michael@0 | 88 | // Close this Channel explicitly. May be called multiple times. |
michael@0 | 89 | void Close(); |
michael@0 | 90 | |
michael@0 | 91 | // Modify the Channel's listener. |
michael@0 | 92 | Listener* set_listener(Listener* listener); |
michael@0 | 93 | |
michael@0 | 94 | // Send a message over the Channel to the listener on the other end. |
michael@0 | 95 | // |
michael@0 | 96 | // |message| must be allocated using operator new. This object will be |
michael@0 | 97 | // deleted once the contents of the Message have been sent. |
michael@0 | 98 | // |
michael@0 | 99 | // If you Send() a message on a Close()'d channel, we delete the message |
michael@0 | 100 | // immediately. |
michael@0 | 101 | virtual bool Send(Message* message); |
michael@0 | 102 | |
michael@0 | 103 | // Unsound_IsClosed() and Unsound_NumQueuedMessages() are safe to call from |
michael@0 | 104 | // any thread, but the value returned may be out of date, because we don't |
michael@0 | 105 | // use any synchronization when reading or writing it. |
michael@0 | 106 | bool Unsound_IsClosed() const; |
michael@0 | 107 | uint32_t Unsound_NumQueuedMessages() const; |
michael@0 | 108 | |
michael@0 | 109 | #if defined(OS_POSIX) |
michael@0 | 110 | // On POSIX an IPC::Channel wraps a socketpair(), this method returns the |
michael@0 | 111 | // FD # for the client end of the socket and the equivalent FD# to use for |
michael@0 | 112 | // mapping it into the Child process. |
michael@0 | 113 | // This method may only be called on the server side of a channel. |
michael@0 | 114 | // |
michael@0 | 115 | // If the kTestingChannelID flag is specified on the command line then |
michael@0 | 116 | // a named FIFO is used as the channel transport mechanism rather than a |
michael@0 | 117 | // socketpair() in which case this method returns -1 for both parameters. |
michael@0 | 118 | void GetClientFileDescriptorMapping(int *src_fd, int *dest_fd) const; |
michael@0 | 119 | |
michael@0 | 120 | // Return the file descriptor for communication with the peer. |
michael@0 | 121 | int GetFileDescriptor() const; |
michael@0 | 122 | |
michael@0 | 123 | // Reset the file descriptor for communication with the peer. |
michael@0 | 124 | void ResetFileDescriptor(int fd); |
michael@0 | 125 | |
michael@0 | 126 | // Close the client side of the socketpair. |
michael@0 | 127 | void CloseClientFileDescriptor(); |
michael@0 | 128 | |
michael@0 | 129 | #elif defined(OS_WIN) |
michael@0 | 130 | // Return the server pipe handle. |
michael@0 | 131 | void* GetServerPipeHandle() const; |
michael@0 | 132 | #endif // defined(OS_POSIX) |
michael@0 | 133 | |
michael@0 | 134 | private: |
michael@0 | 135 | // PIMPL to which all channel calls are delegated. |
michael@0 | 136 | class ChannelImpl; |
michael@0 | 137 | ChannelImpl *channel_impl_; |
michael@0 | 138 | |
michael@0 | 139 | enum { |
michael@0 | 140 | #if defined(OS_MACOSX) |
michael@0 | 141 | // If the channel receives a message that contains file descriptors, then |
michael@0 | 142 | // it will reply back with this message, indicating that the message has |
michael@0 | 143 | // been received. The sending channel can then close any descriptors that |
michael@0 | 144 | // had been marked as auto_close. This works around a sendmsg() bug on BSD |
michael@0 | 145 | // where the kernel can eagerly close file descriptors that are in message |
michael@0 | 146 | // queues but not yet delivered. |
michael@0 | 147 | RECEIVED_FDS_MESSAGE_TYPE = kuint16max - 1, |
michael@0 | 148 | #endif |
michael@0 | 149 | |
michael@0 | 150 | // The Hello message is internal to the Channel class. It is sent |
michael@0 | 151 | // by the peer when the channel is connected. The message contains |
michael@0 | 152 | // just the process id (pid). The message has a special routing_id |
michael@0 | 153 | // (MSG_ROUTING_NONE) and type (HELLO_MESSAGE_TYPE). |
michael@0 | 154 | HELLO_MESSAGE_TYPE = kuint16max // Maximum value of message type (uint16_t), |
michael@0 | 155 | // to avoid conflicting with normal |
michael@0 | 156 | // message types, which are enumeration |
michael@0 | 157 | // constants starting from 0. |
michael@0 | 158 | }; |
michael@0 | 159 | }; |
michael@0 | 160 | |
michael@0 | 161 | } // namespace IPC |
michael@0 | 162 | |
michael@0 | 163 | #endif // CHROME_COMMON_IPC_CHANNEL_H_ |