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