| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 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 | 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 CHROME_COMMON_IPC_CHANNEL_H_ | 5 #ifndef CHROME_COMMON_IPC_CHANNEL_H_ |
| 6 #define CHROME_COMMON_IPC_CHANNEL_H_ | 6 #define CHROME_COMMON_IPC_CHANNEL_H_ |
| 7 | 7 |
| 8 #include <queue> | 8 #include <queue> |
| 9 | 9 |
| 10 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
| 11 #include "chrome/common/ipc_message.h" | 11 #include "chrome/common/ipc_message.h" |
| 12 | 12 |
| 13 namespace IPC { | 13 namespace IPC { |
| 14 | 14 |
| 15 //------------------------------------------------------------------------------ | 15 //------------------------------------------------------------------------------ |
| 16 | 16 |
| 17 class Channel : public Message::Sender | 17 class Channel : public Message::Sender, |
| 18 #if defined(OS_WIN) | 18 #if defined(OS_WIN) |
| 19 , public MessageLoopForIO::IOHandler | 19 public MessageLoopForIO::IOHandler |
| 20 #elif defined(OS_POSIX) |
| 21 public MessageLoopForIO::FileWatcher |
| 20 #endif | 22 #endif |
| 21 { | 23 { |
| 22 // Security tests need access to the pipe handle. | 24 // Security tests need access to the pipe handle. |
| 23 friend class ChannelTest; | 25 friend class ChannelTest; |
| 24 | 26 |
| 25 public: | 27 public: |
| 26 // Implemented by consumers of a Channel to receive messages. | 28 // Implemented by consumers of a Channel to receive messages. |
| 27 class Listener { | 29 class Listener { |
| 28 public: | 30 public: |
| 29 virtual ~Listener() {} | 31 virtual ~Listener() {} |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 83 // |message| must be allocated using operator new. This object will be | 85 // |message| must be allocated using operator new. This object will be |
| 84 // deleted once the contents of the Message have been sent. | 86 // deleted once the contents of the Message have been sent. |
| 85 // | 87 // |
| 86 // FIXME bug 551500: the channel does not notice failures, so if the | 88 // FIXME bug 551500: the channel does not notice failures, so if the |
| 87 // renderer crashes, it will silently succeed, leaking the parameter. | 89 // renderer crashes, it will silently succeed, leaking the parameter. |
| 88 // At least the leak will be fixed by... | 90 // At least the leak will be fixed by... |
| 89 // | 91 // |
| 90 virtual bool Send(Message* message); | 92 virtual bool Send(Message* message); |
| 91 | 93 |
| 92 private: | 94 private: |
| 93 #if defined(OS_WIN) | |
| 94 const std::wstring PipeName(const std::wstring& channel_id) const; | 95 const std::wstring PipeName(const std::wstring& channel_id) const; |
| 95 bool CreatePipe(const std::wstring& channel_id, Mode mode); | 96 bool CreatePipe(const std::wstring& channel_id, Mode mode); |
| 97 #if defined(OS_WIN) |
| 96 bool ProcessConnection(); | 98 bool ProcessConnection(); |
| 97 bool ProcessIncomingMessages(MessageLoopForIO::IOContext* context, | 99 bool ProcessIncomingMessages(MessageLoopForIO::IOContext* context, |
| 98 DWORD bytes_read); | 100 DWORD bytes_read); |
| 99 bool ProcessOutgoingMessages(MessageLoopForIO::IOContext* context, | 101 bool ProcessOutgoingMessages(MessageLoopForIO::IOContext* context, |
| 100 DWORD bytes_written); | 102 DWORD bytes_written); |
| 101 | 103 |
| 102 // MessageLoop::IOHandler implementation. | 104 // MessageLoop::IOHandler implementation. |
| 103 virtual void OnIOCompleted(MessageLoopForIO::IOContext* context, | 105 virtual void OnIOCompleted(MessageLoopForIO::IOContext* context, |
| 104 DWORD bytes_transfered, DWORD error); | 106 DWORD bytes_transfered, DWORD error); |
| 105 #endif | |
| 106 | |
| 107 private: | 107 private: |
| 108 enum { | 108 enum { |
| 109 BUF_SIZE = 4096 | 109 BUF_SIZE = 4096 |
| 110 }; | 110 }; |
| 111 | 111 |
| 112 #if defined(OS_WIN) | |
| 113 struct State { | 112 struct State { |
| 114 explicit State(Channel* channel); | 113 explicit State(Channel* channel); |
| 115 ~State(); | 114 ~State(); |
| 116 MessageLoopForIO::IOContext context; | 115 MessageLoopForIO::IOContext context; |
| 117 bool is_pending; | 116 bool is_pending; |
| 118 }; | 117 }; |
| 119 | 118 |
| 120 State input_state_; | 119 State input_state_; |
| 121 State output_state_; | 120 State output_state_; |
| 122 | 121 |
| 123 HANDLE pipe_; | 122 HANDLE pipe_; |
| 124 #endif | 123 #elif defined(OS_POSIX) |
| 124 bool ProcessIncomingMessages(); |
| 125 bool ProcessOutgoingMessages(); |
| 126 |
| 127 void OnFileReadReady(int fd); |
| 128 void OnFileWriteReady(int fd); |
| 129 |
| 130 |
| 131 Mode mode_; |
| 132 |
| 133 // TODO(playmobil): do we need to change BUF_SIZE ? |
| 134 private: |
| 135 enum { |
| 136 BUF_SIZE = 4096 |
| 137 }; |
| 138 |
| 139 // PIMPL to encapsulate libevent structures. |
| 140 struct EventHolder; |
| 141 EventHolder *server_listen_connection_event_; |
| 142 EventHolder *read_event_; |
| 143 EventHolder *write_event_; |
| 144 |
| 145 // If sending a message blocks then we use this variable |
| 146 // to keep track of where we are. |
| 147 size_t message_send_bytes_written_; |
| 148 |
| 149 int server_listen_pipe_; |
| 150 int pipe_; |
| 151 std::string pipe_name_; |
| 152 #endif // defined(OS_POSIX) |
| 125 Listener* listener_; | 153 Listener* listener_; |
| 126 | 154 |
| 127 // Messages to be sent are queued here. | 155 // Messages to be sent are queued here. |
| 128 std::queue<Message*> output_queue_; | 156 std::queue<Message*> output_queue_; |
| 129 | 157 |
| 130 // We read from the pipe into this buffer | 158 // We read from the pipe into this buffer |
| 131 char input_buf_[BUF_SIZE]; | 159 char input_buf_[BUF_SIZE]; |
| 132 | 160 |
| 133 // Large messages that span multiple pipe buffers, get built-up using | 161 // Large messages that span multiple pipe buffers, get built-up using |
| 134 // this buffer. | 162 // this buffer. |
| (...skipping 20 matching lines...) Expand all Loading... |
| 155 // to avoid conflicting with normal | 183 // to avoid conflicting with normal |
| 156 // message types, which are enumeration | 184 // message types, which are enumeration |
| 157 // constants starting from 0. | 185 // constants starting from 0. |
| 158 }; | 186 }; |
| 159 }; | 187 }; |
| 160 | 188 |
| 161 } | 189 } |
| 162 | 190 |
| 163 #endif // CHROME_COMMON_IPC_CHANNEL_H_ | 191 #endif // CHROME_COMMON_IPC_CHANNEL_H_ |
| 164 | 192 |
| OLD | NEW |