[email protected] | a7c03d4f3 | 2012-01-24 02:36:05 | [diff] [blame] | 1 | // Copyright (c) 2012 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 | |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 5 | #include "ipc/ipc_channel_win.h" |
[email protected] | c1afbd2c | 2008-10-13 19:19:36 | [diff] [blame] | 6 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 7 | #include <windows.h> |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 8 | |
[email protected] | 5be7da24 | 2009-11-20 23:16:26 | [diff] [blame] | 9 | #include "base/auto_reset.h" |
[email protected] | 72b6f8e2 | 2011-11-12 21:16:41 | [diff] [blame] | 10 | #include "base/bind.h" |
[email protected] | c1afbd2c | 2008-10-13 19:19:36 | [diff] [blame] | 11 | #include "base/compiler_specific.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 12 | #include "base/logging.h" |
[email protected] | c917750 | 2011-01-01 04:48:49 | [diff] [blame] | 13 | #include "base/threading/non_thread_safe.h" |
[email protected] | be1ce6a7 | 2010-08-03 14:35:22 | [diff] [blame] | 14 | #include "base/utf_string_conversions.h" |
[email protected] | b90d7e80 | 2011-01-09 16:32:20 | [diff] [blame] | 15 | #include "base/win/scoped_handle.h" |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 16 | #include "ipc/ipc_logging.h" |
| 17 | #include "ipc/ipc_message_utils.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 18 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 19 | namespace IPC { |
[email protected] | 935aa54 | 2010-10-15 01:59:15 | [diff] [blame] | 20 | |
[email protected] | 514411fc | 2008-12-10 22:28:11 | [diff] [blame] | 21 | Channel::ChannelImpl::State::State(ChannelImpl* channel) : is_pending(false) { |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 22 | memset(&context.overlapped, 0, sizeof(context.overlapped)); |
| 23 | context.handler = channel; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 24 | } |
| 25 | |
[email protected] | 514411fc | 2008-12-10 22:28:11 | [diff] [blame] | 26 | Channel::ChannelImpl::State::~State() { |
| 27 | COMPILE_ASSERT(!offsetof(Channel::ChannelImpl::State, context), |
| 28 | starts_with_io_context); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 29 | } |
| 30 | |
[email protected] | 42ce94e | 2010-12-08 19:28:09 | [diff] [blame] | 31 | Channel::ChannelImpl::ChannelImpl(const IPC::ChannelHandle &channel_handle, |
| 32 | Mode mode, Listener* listener) |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 33 | : ALLOW_THIS_IN_INITIALIZER_LIST(input_state_(this)), |
| 34 | ALLOW_THIS_IN_INITIALIZER_LIST(output_state_(this)), |
| 35 | pipe_(INVALID_HANDLE_VALUE), |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 36 | listener_(listener), |
[email protected] | 1707726c | 2011-02-03 20:35:09 | [diff] [blame] | 37 | waiting_connect_(mode & MODE_SERVER_FLAG), |
[email protected] | c1afbd2c | 2008-10-13 19:19:36 | [diff] [blame] | 38 | processing_incoming_(false), |
[email protected] | 72b6f8e2 | 2011-11-12 21:16:41 | [diff] [blame] | 39 | ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { |
[email protected] | c2391b8 | 2011-05-06 17:39:07 | [diff] [blame] | 40 | CreatePipe(channel_handle, mode); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 41 | } |
| 42 | |
[email protected] | 601858c0 | 2010-09-01 17:08:20 | [diff] [blame] | 43 | Channel::ChannelImpl::~ChannelImpl() { |
| 44 | Close(); |
| 45 | } |
| 46 | |
[email protected] | 514411fc | 2008-12-10 22:28:11 | [diff] [blame] | 47 | void Channel::ChannelImpl::Close() { |
[email protected] | c1e4bff3 | 2009-01-29 00:07:06 | [diff] [blame] | 48 | if (thread_check_.get()) { |
| 49 | DCHECK(thread_check_->CalledOnValidThread()); |
| 50 | } |
| 51 | |
[email protected] | 74f87acf2 | 2009-10-14 22:10:40 | [diff] [blame] | 52 | if (input_state_.is_pending || output_state_.is_pending) |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 53 | CancelIo(pipe_); |
[email protected] | ee78622d | 2008-10-13 21:25:50 | [diff] [blame] | 54 | |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 55 | // Closing the handle at this point prevents us from issuing more requests |
| 56 | // form OnIOCompleted(). |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 57 | if (pipe_ != INVALID_HANDLE_VALUE) { |
| 58 | CloseHandle(pipe_); |
| 59 | pipe_ = INVALID_HANDLE_VALUE; |
| 60 | } |
| 61 | |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 62 | // Make sure all IO has completed. |
| 63 | base::Time start = base::Time::Now(); |
| 64 | while (input_state_.is_pending || output_state_.is_pending) { |
| 65 | MessageLoopForIO::current()->WaitForIOCompletion(INFINITE, this); |
| 66 | } |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 67 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 68 | while (!output_queue_.empty()) { |
| 69 | Message* m = output_queue_.front(); |
| 70 | output_queue_.pop(); |
| 71 | delete m; |
| 72 | } |
| 73 | } |
| 74 | |
[email protected] | 514411fc | 2008-12-10 22:28:11 | [diff] [blame] | 75 | bool Channel::ChannelImpl::Send(Message* message) { |
[email protected] | c1e4bff3 | 2009-01-29 00:07:06 | [diff] [blame] | 76 | DCHECK(thread_check_->CalledOnValidThread()); |
[email protected] | 2a9d601b | 2010-10-19 23:50:00 | [diff] [blame] | 77 | DVLOG(2) << "sending message @" << message << " on channel @" << this |
| 78 | << " with type " << message->type() |
| 79 | << " (" << output_queue_.size() << " in queue)"; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 80 | |
| 81 | #ifdef IPC_MESSAGE_LOG_ENABLED |
[email protected] | 8e8bb6d | 2010-12-13 08:18:55 | [diff] [blame] | 82 | Logging::GetInstance()->OnSendMessage(message, ""); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 83 | #endif |
| 84 | |
| 85 | output_queue_.push(message); |
| 86 | // ensure waiting to write |
| 87 | if (!waiting_connect_) { |
| 88 | if (!output_state_.is_pending) { |
[email protected] | c1afbd2c | 2008-10-13 19:19:36 | [diff] [blame] | 89 | if (!ProcessOutgoingMessages(NULL, 0)) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 90 | return false; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 91 | } |
| 92 | } |
| 93 | |
| 94 | return true; |
| 95 | } |
| 96 | |
[email protected] | 313c00e5 | 2011-08-09 06:46:06 | [diff] [blame] | 97 | // static |
| 98 | bool Channel::ChannelImpl::IsNamedServerInitialized( |
| 99 | const std::string& channel_id) { |
| 100 | if (WaitNamedPipe(PipeName(channel_id).c_str(), 1)) |
| 101 | return true; |
| 102 | // If ERROR_SEM_TIMEOUT occurred, the pipe exists but is handling another |
| 103 | // connection. |
| 104 | return GetLastError() == ERROR_SEM_TIMEOUT; |
| 105 | } |
| 106 | |
| 107 | // static |
[email protected] | d321644 | 2009-03-05 21:07:27 | [diff] [blame] | 108 | const std::wstring Channel::ChannelImpl::PipeName( |
[email protected] | 313c00e5 | 2011-08-09 06:46:06 | [diff] [blame] | 109 | const std::string& channel_id) { |
[email protected] | c2391b8 | 2011-05-06 17:39:07 | [diff] [blame] | 110 | std::string name("\\\\.\\pipe\\chrome."); |
| 111 | return ASCIIToWide(name.append(channel_id)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 112 | } |
| 113 | |
[email protected] | 42ce94e | 2010-12-08 19:28:09 | [diff] [blame] | 114 | bool Channel::ChannelImpl::CreatePipe(const IPC::ChannelHandle &channel_handle, |
[email protected] | 514411fc | 2008-12-10 22:28:11 | [diff] [blame] | 115 | Mode mode) { |
[email protected] | 5e0be64 | 2011-04-28 18:20:09 | [diff] [blame] | 116 | DCHECK_EQ(INVALID_HANDLE_VALUE, pipe_); |
[email protected] | a7c03d4f3 | 2012-01-24 02:36:05 | [diff] [blame] | 117 | string16 pipe_name; |
| 118 | // If we already have a valid pipe for channel just copy it. |
| 119 | if (channel_handle.pipe.handle) { |
| 120 | DCHECK(channel_handle.name.empty()); |
| 121 | pipe_name = L"Not Available"; // Just used for LOG |
| 122 | // Check that the given pipe confirms to the specified mode. We can |
| 123 | // only check for PIPE_TYPE_MESSAGE & PIPE_SERVER_END flags since the |
| 124 | // other flags (PIPE_TYPE_BYTE, and PIPE_CLIENT_END) are defined as 0. |
| 125 | DWORD flags = 0; |
| 126 | GetNamedPipeInfo(channel_handle.pipe.handle, &flags, NULL, NULL, NULL); |
| 127 | DCHECK(!(flags & PIPE_TYPE_MESSAGE)); |
| 128 | if (((mode & MODE_SERVER_FLAG) && !(flags & PIPE_SERVER_END)) || |
| 129 | ((mode & MODE_CLIENT_FLAG) && (flags & PIPE_SERVER_END))) { |
| 130 | LOG(WARNING) << "Inconsistent open mode. Mode :" << mode; |
| 131 | return false; |
| 132 | } |
| 133 | if (!DuplicateHandle(GetCurrentProcess(), |
| 134 | channel_handle.pipe.handle, |
| 135 | GetCurrentProcess(), |
| 136 | &pipe_, |
| 137 | 0, |
| 138 | FALSE, |
| 139 | DUPLICATE_SAME_ACCESS)) { |
| 140 | LOG(WARNING) << "DuplicateHandle failed. Error :" << GetLastError(); |
| 141 | return false; |
| 142 | } |
| 143 | } else if (mode & MODE_SERVER_FLAG) { |
| 144 | DCHECK(!channel_handle.pipe.handle); |
| 145 | const DWORD open_mode = PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED | |
| 146 | FILE_FLAG_FIRST_PIPE_INSTANCE; |
| 147 | pipe_name = PipeName(channel_handle.name); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 148 | pipe_ = CreateNamedPipeW(pipe_name.c_str(), |
[email protected] | a7c03d4f3 | 2012-01-24 02:36:05 | [diff] [blame] | 149 | open_mode, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 150 | PIPE_TYPE_BYTE | PIPE_READMODE_BYTE, |
[email protected] | c2391b8 | 2011-05-06 17:39:07 | [diff] [blame] | 151 | 1, |
[email protected] | 514411fc | 2008-12-10 22:28:11 | [diff] [blame] | 152 | Channel::kReadBufferSize, |
[email protected] | 514411fc | 2008-12-10 22:28:11 | [diff] [blame] | 153 | Channel::kReadBufferSize, |
[email protected] | c2391b8 | 2011-05-06 17:39:07 | [diff] [blame] | 154 | 5000, |
| 155 | NULL); |
[email protected] | 1707726c | 2011-02-03 20:35:09 | [diff] [blame] | 156 | } else if (mode & MODE_CLIENT_FLAG) { |
[email protected] | a7c03d4f3 | 2012-01-24 02:36:05 | [diff] [blame] | 157 | DCHECK(!channel_handle.pipe.handle); |
| 158 | pipe_name = PipeName(channel_handle.name); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 159 | pipe_ = CreateFileW(pipe_name.c_str(), |
| 160 | GENERIC_READ | GENERIC_WRITE, |
| 161 | 0, |
| 162 | NULL, |
| 163 | OPEN_EXISTING, |
| 164 | SECURITY_SQOS_PRESENT | SECURITY_IDENTIFICATION | |
| 165 | FILE_FLAG_OVERLAPPED, |
| 166 | NULL); |
[email protected] | 1707726c | 2011-02-03 20:35:09 | [diff] [blame] | 167 | } else { |
| 168 | NOTREACHED(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 169 | } |
[email protected] | a7c03d4f3 | 2012-01-24 02:36:05 | [diff] [blame] | 170 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 171 | if (pipe_ == INVALID_HANDLE_VALUE) { |
| 172 | // If this process is being closed, the pipe may be gone already. |
[email protected] | c2391b8 | 2011-05-06 17:39:07 | [diff] [blame] | 173 | LOG(WARNING) << "Unable to create pipe \"" << pipe_name << |
| 174 | "\" in " << (mode == 0 ? "server" : "client") |
| 175 | << " mode. Error :" << GetLastError(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 176 | return false; |
| 177 | } |
| 178 | |
| 179 | // Create the Hello message to be sent when Connect is called |
| 180 | scoped_ptr<Message> m(new Message(MSG_ROUTING_NONE, |
| 181 | HELLO_MESSAGE_TYPE, |
| 182 | IPC::Message::PRIORITY_NORMAL)); |
| 183 | if (!m->WriteInt(GetCurrentProcessId())) { |
| 184 | CloseHandle(pipe_); |
| 185 | pipe_ = INVALID_HANDLE_VALUE; |
| 186 | return false; |
| 187 | } |
| 188 | |
| 189 | output_queue_.push(m.release()); |
| 190 | return true; |
| 191 | } |
| 192 | |
[email protected] | 514411fc | 2008-12-10 22:28:11 | [diff] [blame] | 193 | bool Channel::ChannelImpl::Connect() { |
[email protected] | 1934072 | 2009-08-17 19:53:25 | [diff] [blame] | 194 | DLOG_IF(WARNING, thread_check_.get()) << "Connect called more than once"; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 195 | |
[email protected] | c1e4bff3 | 2009-01-29 00:07:06 | [diff] [blame] | 196 | if (!thread_check_.get()) |
[email protected] | c917750 | 2011-01-01 04:48:49 | [diff] [blame] | 197 | thread_check_.reset(new base::NonThreadSafe()); |
[email protected] | c1e4bff3 | 2009-01-29 00:07:06 | [diff] [blame] | 198 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 199 | if (pipe_ == INVALID_HANDLE_VALUE) |
| 200 | return false; |
| 201 | |
[email protected] | c1afbd2c | 2008-10-13 19:19:36 | [diff] [blame] | 202 | MessageLoopForIO::current()->RegisterIOHandler(pipe_, this); |
| 203 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 204 | // Check to see if there is a client connected to our pipe... |
| 205 | if (waiting_connect_) |
| 206 | ProcessConnection(); |
| 207 | |
| 208 | if (!input_state_.is_pending) { |
[email protected] | c1afbd2c | 2008-10-13 19:19:36 | [diff] [blame] | 209 | // Complete setup asynchronously. By not setting input_state_.is_pending |
| 210 | // to true, we indicate to OnIOCompleted that this is the special |
| 211 | // initialization signal. |
[email protected] | 72b6f8e2 | 2011-11-12 21:16:41 | [diff] [blame] | 212 | MessageLoopForIO::current()->PostTask( |
| 213 | FROM_HERE, base::Bind(&Channel::ChannelImpl::OnIOCompleted, |
| 214 | weak_factory_.GetWeakPtr(), &input_state_.context, |
| 215 | 0, 0)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | if (!waiting_connect_) |
[email protected] | c1afbd2c | 2008-10-13 19:19:36 | [diff] [blame] | 219 | ProcessOutgoingMessages(NULL, 0); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 220 | return true; |
| 221 | } |
| 222 | |
[email protected] | 514411fc | 2008-12-10 22:28:11 | [diff] [blame] | 223 | bool Channel::ChannelImpl::ProcessConnection() { |
[email protected] | c1e4bff3 | 2009-01-29 00:07:06 | [diff] [blame] | 224 | DCHECK(thread_check_->CalledOnValidThread()); |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 225 | if (input_state_.is_pending) |
[email protected] | c1afbd2c | 2008-10-13 19:19:36 | [diff] [blame] | 226 | input_state_.is_pending = false; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 227 | |
| 228 | // Do we have a client connected to our pipe? |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 229 | if (INVALID_HANDLE_VALUE == pipe_) |
| 230 | return false; |
| 231 | |
| 232 | BOOL ok = ConnectNamedPipe(pipe_, &input_state_.context.overlapped); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 233 | |
| 234 | DWORD err = GetLastError(); |
| 235 | if (ok) { |
| 236 | // Uhm, the API documentation says that this function should never |
| 237 | // return success when used in overlapped mode. |
| 238 | NOTREACHED(); |
| 239 | return false; |
| 240 | } |
| 241 | |
| 242 | switch (err) { |
| 243 | case ERROR_IO_PENDING: |
| 244 | input_state_.is_pending = true; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 245 | break; |
| 246 | case ERROR_PIPE_CONNECTED: |
| 247 | waiting_connect_ = false; |
| 248 | break; |
[email protected] | 20aa32c | 2009-07-14 22:25:49 | [diff] [blame] | 249 | case ERROR_NO_DATA: |
| 250 | // The pipe is being closed. |
| 251 | return false; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 252 | default: |
| 253 | NOTREACHED(); |
| 254 | return false; |
| 255 | } |
| 256 | |
| 257 | return true; |
| 258 | } |
| 259 | |
[email protected] | 514411fc | 2008-12-10 22:28:11 | [diff] [blame] | 260 | bool Channel::ChannelImpl::ProcessIncomingMessages( |
| 261 | MessageLoopForIO::IOContext* context, |
| 262 | DWORD bytes_read) { |
[email protected] | c1e4bff3 | 2009-01-29 00:07:06 | [diff] [blame] | 263 | DCHECK(thread_check_->CalledOnValidThread()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 264 | if (input_state_.is_pending) { |
| 265 | input_state_.is_pending = false; |
[email protected] | c1afbd2c | 2008-10-13 19:19:36 | [diff] [blame] | 266 | DCHECK(context); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 267 | |
[email protected] | c1afbd2c | 2008-10-13 19:19:36 | [diff] [blame] | 268 | if (!context || !bytes_read) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 269 | return false; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 270 | } else { |
[email protected] | c1afbd2c | 2008-10-13 19:19:36 | [diff] [blame] | 271 | // This happens at channel initialization. |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 272 | DCHECK(!bytes_read && context == &input_state_.context); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | for (;;) { |
| 276 | if (bytes_read == 0) { |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 277 | if (INVALID_HANDLE_VALUE == pipe_) |
| 278 | return false; |
| 279 | |
[email protected] | c1afbd2c | 2008-10-13 19:19:36 | [diff] [blame] | 280 | // Read from pipe... |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 281 | BOOL ok = ReadFile(pipe_, |
| 282 | input_buf_, |
[email protected] | 514411fc | 2008-12-10 22:28:11 | [diff] [blame] | 283 | Channel::kReadBufferSize, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 284 | &bytes_read, |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 285 | &input_state_.context.overlapped); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 286 | if (!ok) { |
| 287 | DWORD err = GetLastError(); |
| 288 | if (err == ERROR_IO_PENDING) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 289 | input_state_.is_pending = true; |
| 290 | return true; |
| 291 | } |
| 292 | LOG(ERROR) << "pipe error: " << err; |
| 293 | return false; |
| 294 | } |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 295 | input_state_.is_pending = true; |
| 296 | return true; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 297 | } |
| 298 | DCHECK(bytes_read); |
| 299 | |
[email protected] | c1afbd2c | 2008-10-13 19:19:36 | [diff] [blame] | 300 | // Process messages from input buffer. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 301 | |
| 302 | const char* p, *end; |
| 303 | if (input_overflow_buf_.empty()) { |
| 304 | p = input_buf_; |
| 305 | end = p + bytes_read; |
| 306 | } else { |
| 307 | if (input_overflow_buf_.size() > (kMaximumMessageSize - bytes_read)) { |
| 308 | input_overflow_buf_.clear(); |
| 309 | LOG(ERROR) << "IPC message is too big"; |
| 310 | return false; |
| 311 | } |
| 312 | input_overflow_buf_.append(input_buf_, bytes_read); |
| 313 | p = input_overflow_buf_.data(); |
| 314 | end = p + input_overflow_buf_.size(); |
| 315 | } |
| 316 | |
| 317 | while (p < end) { |
| 318 | const char* message_tail = Message::FindNext(p, end); |
| 319 | if (message_tail) { |
| 320 | int len = static_cast<int>(message_tail - p); |
| 321 | const Message m(p, len); |
[email protected] | 2a9d601b | 2010-10-19 23:50:00 | [diff] [blame] | 322 | DVLOG(2) << "received message on channel @" << this |
| 323 | << " with type " << m.type(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 324 | if (m.routing_id() == MSG_ROUTING_NONE && |
| 325 | m.type() == HELLO_MESSAGE_TYPE) { |
| 326 | // The Hello message contains only the process id. |
| 327 | listener_->OnChannelConnected(MessageIterator(m).NextInt()); |
| 328 | } else { |
| 329 | listener_->OnMessageReceived(m); |
| 330 | } |
| 331 | p = message_tail; |
| 332 | } else { |
[email protected] | c1afbd2c | 2008-10-13 19:19:36 | [diff] [blame] | 333 | // Last message is partial. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 334 | break; |
| 335 | } |
| 336 | } |
| 337 | input_overflow_buf_.assign(p, end - p); |
| 338 | |
[email protected] | c1afbd2c | 2008-10-13 19:19:36 | [diff] [blame] | 339 | bytes_read = 0; // Get more data. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | return true; |
| 343 | } |
| 344 | |
[email protected] | 514411fc | 2008-12-10 22:28:11 | [diff] [blame] | 345 | bool Channel::ChannelImpl::ProcessOutgoingMessages( |
| 346 | MessageLoopForIO::IOContext* context, |
| 347 | DWORD bytes_written) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 348 | DCHECK(!waiting_connect_); // Why are we trying to send messages if there's |
| 349 | // no connection? |
[email protected] | c1e4bff3 | 2009-01-29 00:07:06 | [diff] [blame] | 350 | DCHECK(thread_check_->CalledOnValidThread()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 351 | |
| 352 | if (output_state_.is_pending) { |
[email protected] | c1afbd2c | 2008-10-13 19:19:36 | [diff] [blame] | 353 | DCHECK(context); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 354 | output_state_.is_pending = false; |
[email protected] | c1afbd2c | 2008-10-13 19:19:36 | [diff] [blame] | 355 | if (!context || bytes_written == 0) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 356 | DWORD err = GetLastError(); |
| 357 | LOG(ERROR) << "pipe error: " << err; |
| 358 | return false; |
| 359 | } |
[email protected] | c1afbd2c | 2008-10-13 19:19:36 | [diff] [blame] | 360 | // Message was sent. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 361 | DCHECK(!output_queue_.empty()); |
| 362 | Message* m = output_queue_.front(); |
| 363 | output_queue_.pop(); |
| 364 | delete m; |
| 365 | } |
| 366 | |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 367 | if (output_queue_.empty()) |
| 368 | return true; |
| 369 | |
| 370 | if (INVALID_HANDLE_VALUE == pipe_) |
| 371 | return false; |
| 372 | |
| 373 | // Write to pipe... |
| 374 | Message* m = output_queue_.front(); |
[email protected] | 8a86140 | 2011-01-28 19:59:11 | [diff] [blame] | 375 | DCHECK(m->size() <= INT_MAX); |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 376 | BOOL ok = WriteFile(pipe_, |
| 377 | m->data(), |
[email protected] | 8a86140 | 2011-01-28 19:59:11 | [diff] [blame] | 378 | static_cast<int>(m->size()), |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 379 | &bytes_written, |
| 380 | &output_state_.context.overlapped); |
| 381 | if (!ok) { |
| 382 | DWORD err = GetLastError(); |
| 383 | if (err == ERROR_IO_PENDING) { |
| 384 | output_state_.is_pending = true; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 385 | |
[email protected] | 2a9d601b | 2010-10-19 23:50:00 | [diff] [blame] | 386 | DVLOG(2) << "sent pending message @" << m << " on channel @" << this |
| 387 | << " with type " << m->type(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 388 | |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 389 | return true; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 390 | } |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 391 | LOG(ERROR) << "pipe error: " << err; |
| 392 | return false; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 393 | } |
| 394 | |
[email protected] | 2a9d601b | 2010-10-19 23:50:00 | [diff] [blame] | 395 | DVLOG(2) << "sent message @" << m << " on channel @" << this |
| 396 | << " with type " << m->type(); |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 397 | |
| 398 | output_state_.is_pending = true; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 399 | return true; |
| 400 | } |
| 401 | |
[email protected] | 514411fc | 2008-12-10 22:28:11 | [diff] [blame] | 402 | void Channel::ChannelImpl::OnIOCompleted(MessageLoopForIO::IOContext* context, |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 403 | DWORD bytes_transfered, DWORD error) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 404 | bool ok; |
[email protected] | c1e4bff3 | 2009-01-29 00:07:06 | [diff] [blame] | 405 | DCHECK(thread_check_->CalledOnValidThread()); |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 406 | if (context == &input_state_.context) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 407 | if (waiting_connect_) { |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 408 | if (!ProcessConnection()) |
| 409 | return; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 410 | // We may have some messages queued up to send... |
| 411 | if (!output_queue_.empty() && !output_state_.is_pending) |
[email protected] | c1afbd2c | 2008-10-13 19:19:36 | [diff] [blame] | 412 | ProcessOutgoingMessages(NULL, 0); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 413 | if (input_state_.is_pending) |
| 414 | return; |
| 415 | // else, fall-through and look for incoming messages... |
| 416 | } |
| 417 | // we don't support recursion through OnMessageReceived yet! |
| 418 | DCHECK(!processing_incoming_); |
[email protected] | 0fbd7033 | 2010-06-01 19:28:34 | [diff] [blame] | 419 | AutoReset<bool> auto_reset_processing_incoming(&processing_incoming_, true); |
[email protected] | c1afbd2c | 2008-10-13 19:19:36 | [diff] [blame] | 420 | ok = ProcessIncomingMessages(context, bytes_transfered); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 421 | } else { |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 422 | DCHECK(context == &output_state_.context); |
[email protected] | c1afbd2c | 2008-10-13 19:19:36 | [diff] [blame] | 423 | ok = ProcessOutgoingMessages(context, bytes_transfered); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 424 | } |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 425 | if (!ok && INVALID_HANDLE_VALUE != pipe_) { |
| 426 | // We don't want to re-enter Close(). |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 427 | Close(); |
| 428 | listener_->OnChannelError(); |
| 429 | } |
| 430 | } |
| 431 | |
[email protected] | 514411fc | 2008-12-10 22:28:11 | [diff] [blame] | 432 | //------------------------------------------------------------------------------ |
| 433 | // Channel's methods simply call through to ChannelImpl. |
[email protected] | 42ce94e | 2010-12-08 19:28:09 | [diff] [blame] | 434 | Channel::Channel(const IPC::ChannelHandle &channel_handle, Mode mode, |
[email protected] | 514411fc | 2008-12-10 22:28:11 | [diff] [blame] | 435 | Listener* listener) |
[email protected] | 42ce94e | 2010-12-08 19:28:09 | [diff] [blame] | 436 | : channel_impl_(new ChannelImpl(channel_handle, mode, listener)) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 437 | } |
[email protected] | 514411fc | 2008-12-10 22:28:11 | [diff] [blame] | 438 | |
| 439 | Channel::~Channel() { |
| 440 | delete channel_impl_; |
| 441 | } |
| 442 | |
| 443 | bool Channel::Connect() { |
| 444 | return channel_impl_->Connect(); |
| 445 | } |
| 446 | |
| 447 | void Channel::Close() { |
| 448 | channel_impl_->Close(); |
| 449 | } |
| 450 | |
| 451 | void Channel::set_listener(Listener* listener) { |
| 452 | channel_impl_->set_listener(listener); |
| 453 | } |
| 454 | |
| 455 | bool Channel::Send(Message* message) { |
| 456 | return channel_impl_->Send(message); |
| 457 | } |
| 458 | |
[email protected] | 313c00e5 | 2011-08-09 06:46:06 | [diff] [blame] | 459 | // static |
| 460 | bool Channel::IsNamedServerInitialized(const std::string& channel_id) { |
| 461 | return ChannelImpl::IsNamedServerInitialized(channel_id); |
| 462 | } |
| 463 | |
[email protected] | 514411fc | 2008-12-10 22:28:11 | [diff] [blame] | 464 | } // namespace IPC |