blob: 10af1fecdb34a4317c3a4588f9788ba6dbff4f17 [file] [log] [blame]
[email protected]a7c03d4f32012-01-24 02:36:051// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
[email protected]946d1b22009-07-22 23:57:215#include "ipc/ipc_channel_win.h"
[email protected]c1afbd2c2008-10-13 19:19:366
initial.commit09911bf2008-07-26 23:55:297#include <windows.h>
initial.commit09911bf2008-07-26 23:55:298
[email protected]5be7da242009-11-20 23:16:269#include "base/auto_reset.h"
[email protected]72b6f8e22011-11-12 21:16:4110#include "base/bind.h"
[email protected]c1afbd2c2008-10-13 19:19:3611#include "base/compiler_specific.h"
initial.commit09911bf2008-07-26 23:55:2912#include "base/logging.h"
[email protected]5c41e6e12012-03-17 02:20:4613#include "base/process_util.h"
14#include "base/rand_util.h"
15#include "base/string_number_conversions.h"
[email protected]327e52b2012-06-25 21:11:3616#include "base/threading/thread_checker.h"
[email protected]be1ce6a72010-08-03 14:35:2217#include "base/utf_string_conversions.h"
[email protected]b90d7e802011-01-09 16:32:2018#include "base/win/scoped_handle.h"
[email protected]0ee990682012-11-17 19:20:0519#include "ipc/ipc_listener.h"
[email protected]946d1b22009-07-22 23:57:2120#include "ipc/ipc_logging.h"
21#include "ipc/ipc_message_utils.h"
initial.commit09911bf2008-07-26 23:55:2922
initial.commit09911bf2008-07-26 23:55:2923namespace IPC {
[email protected]935aa542010-10-15 01:59:1524
[email protected]514411fc2008-12-10 22:28:1125Channel::ChannelImpl::State::State(ChannelImpl* channel) : is_pending(false) {
[email protected]17b89142008-11-07 21:52:1526 memset(&context.overlapped, 0, sizeof(context.overlapped));
27 context.handler = channel;
initial.commit09911bf2008-07-26 23:55:2928}
29
[email protected]514411fc2008-12-10 22:28:1130Channel::ChannelImpl::State::~State() {
31 COMPILE_ASSERT(!offsetof(Channel::ChannelImpl::State, context),
32 starts_with_io_context);
initial.commit09911bf2008-07-26 23:55:2933}
34
[email protected]42ce94e2010-12-08 19:28:0935Channel::ChannelImpl::ChannelImpl(const IPC::ChannelHandle &channel_handle,
36 Mode mode, Listener* listener)
[email protected]d805c6a2012-03-08 12:30:2837 : ChannelReader(listener),
38 ALLOW_THIS_IN_INITIALIZER_LIST(input_state_(this)),
[email protected]17b89142008-11-07 21:52:1539 ALLOW_THIS_IN_INITIALIZER_LIST(output_state_(this)),
40 pipe_(INVALID_HANDLE_VALUE),
[email protected]0a6fc4b2012-04-05 02:38:3441 peer_pid_(base::kNullProcessId),
[email protected]1707726c2011-02-03 20:35:0942 waiting_connect_(mode & MODE_SERVER_FLAG),
[email protected]c1afbd2c2008-10-13 19:19:3643 processing_incoming_(false),
[email protected]5c41e6e12012-03-17 02:20:4644 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)),
45 client_secret_(0),
46 validate_client_(false) {
[email protected]c2391b82011-05-06 17:39:0747 CreatePipe(channel_handle, mode);
initial.commit09911bf2008-07-26 23:55:2948}
49
[email protected]601858c02010-09-01 17:08:2050Channel::ChannelImpl::~ChannelImpl() {
51 Close();
52}
53
[email protected]514411fc2008-12-10 22:28:1154void Channel::ChannelImpl::Close() {
[email protected]c1e4bff32009-01-29 00:07:0655 if (thread_check_.get()) {
56 DCHECK(thread_check_->CalledOnValidThread());
57 }
58
[email protected]74f87acf22009-10-14 22:10:4059 if (input_state_.is_pending || output_state_.is_pending)
[email protected]17b89142008-11-07 21:52:1560 CancelIo(pipe_);
[email protected]ee78622d2008-10-13 21:25:5061
[email protected]17b89142008-11-07 21:52:1562 // Closing the handle at this point prevents us from issuing more requests
63 // form OnIOCompleted().
initial.commit09911bf2008-07-26 23:55:2964 if (pipe_ != INVALID_HANDLE_VALUE) {
65 CloseHandle(pipe_);
66 pipe_ = INVALID_HANDLE_VALUE;
67 }
68
[email protected]17b89142008-11-07 21:52:1569 // Make sure all IO has completed.
70 base::Time start = base::Time::Now();
71 while (input_state_.is_pending || output_state_.is_pending) {
72 MessageLoopForIO::current()->WaitForIOCompletion(INFINITE, this);
73 }
[email protected]17b89142008-11-07 21:52:1574
initial.commit09911bf2008-07-26 23:55:2975 while (!output_queue_.empty()) {
76 Message* m = output_queue_.front();
77 output_queue_.pop();
78 delete m;
79 }
80}
81
[email protected]514411fc2008-12-10 22:28:1182bool Channel::ChannelImpl::Send(Message* message) {
[email protected]c1e4bff32009-01-29 00:07:0683 DCHECK(thread_check_->CalledOnValidThread());
[email protected]2a9d601b2010-10-19 23:50:0084 DVLOG(2) << "sending message @" << message << " on channel @" << this
85 << " with type " << message->type()
86 << " (" << output_queue_.size() << " in queue)";
initial.commit09911bf2008-07-26 23:55:2987
88#ifdef IPC_MESSAGE_LOG_ENABLED
[email protected]8e8bb6d2010-12-13 08:18:5589 Logging::GetInstance()->OnSendMessage(message, "");
initial.commit09911bf2008-07-26 23:55:2990#endif
91
[email protected]2c391df2012-09-18 03:41:2992 message->TraceMessageBegin();
initial.commit09911bf2008-07-26 23:55:2993 output_queue_.push(message);
94 // ensure waiting to write
95 if (!waiting_connect_) {
96 if (!output_state_.is_pending) {
[email protected]c1afbd2c2008-10-13 19:19:3697 if (!ProcessOutgoingMessages(NULL, 0))
initial.commit09911bf2008-07-26 23:55:2998 return false;
initial.commit09911bf2008-07-26 23:55:2999 }
100 }
101
102 return true;
103}
104
[email protected]313c00e52011-08-09 06:46:06105// static
106bool Channel::ChannelImpl::IsNamedServerInitialized(
107 const std::string& channel_id) {
[email protected]5c41e6e12012-03-17 02:20:46108 if (WaitNamedPipe(PipeName(channel_id, NULL).c_str(), 1))
[email protected]313c00e52011-08-09 06:46:06109 return true;
110 // If ERROR_SEM_TIMEOUT occurred, the pipe exists but is handling another
111 // connection.
112 return GetLastError() == ERROR_SEM_TIMEOUT;
113}
114
[email protected]215f6fd2012-03-03 08:55:45115Channel::ChannelImpl::ReadState Channel::ChannelImpl::ReadData(
116 char* buffer,
117 int buffer_len,
118 int* /* bytes_read */) {
119 if (INVALID_HANDLE_VALUE == pipe_)
120 return READ_FAILED;
121
122 DWORD bytes_read = 0;
[email protected]d805c6a2012-03-08 12:30:28123 BOOL ok = ReadFile(pipe_, buffer, buffer_len,
[email protected]215f6fd2012-03-03 08:55:45124 &bytes_read, &input_state_.context.overlapped);
125 if (!ok) {
126 DWORD err = GetLastError();
127 if (err == ERROR_IO_PENDING) {
128 input_state_.is_pending = true;
129 return READ_PENDING;
130 }
131 LOG(ERROR) << "pipe error: " << err;
132 return READ_FAILED;
133 }
134
135 // We could return READ_SUCCEEDED here. But the way that this code is
136 // structured we instead go back to the message loop. Our completion port
137 // will be signalled even in the "synchronously completed" state.
138 //
139 // This allows us to potentially process some outgoing messages and
140 // interleave other work on this thread when we're getting hammered with
141 // input messages. Potentially, this could be tuned to be more efficient
142 // with some testing.
143 input_state_.is_pending = true;
144 return READ_PENDING;
145}
146
147bool Channel::ChannelImpl::WillDispatchInputMessage(Message* msg) {
[email protected]05908782012-04-03 08:49:43148 // Make sure we get a hello when client validation is required.
149 if (validate_client_)
150 return IsHelloMessage(*msg);
[email protected]215f6fd2012-03-03 08:55:45151 return true;
152}
153
154void Channel::ChannelImpl::HandleHelloMessage(const Message& msg) {
155 // The hello message contains one parameter containing the PID.
[email protected]5c41e6e12012-03-17 02:20:46156 MessageIterator it = MessageIterator(msg);
157 int32 claimed_pid = it.NextInt();
158 if (validate_client_ && (it.NextInt() != client_secret_)) {
159 NOTREACHED();
160 // Something went wrong. Abort connection.
161 Close();
162 listener()->OnChannelError();
163 return;
164 }
[email protected]0a6fc4b2012-04-05 02:38:34165 peer_pid_ = claimed_pid;
[email protected]05908782012-04-03 08:49:43166 // validation completed.
167 validate_client_ = false;
[email protected]5c41e6e12012-03-17 02:20:46168 listener()->OnChannelConnected(claimed_pid);
[email protected]215f6fd2012-03-03 08:55:45169}
170
171bool Channel::ChannelImpl::DidEmptyInputBuffers() {
[email protected]d805c6a2012-03-08 12:30:28172 // We don't need to do anything here.
[email protected]215f6fd2012-03-03 08:55:45173 return true;
174}
175
[email protected]313c00e52011-08-09 06:46:06176// static
[email protected]5c41e6e12012-03-17 02:20:46177const string16 Channel::ChannelImpl::PipeName(
178 const std::string& channel_id, int32* secret) {
[email protected]c2391b82011-05-06 17:39:07179 std::string name("\\\\.\\pipe\\chrome.");
[email protected]5c41e6e12012-03-17 02:20:46180
181 // Prevent the shared secret from ending up in the pipe name.
182 size_t index = channel_id.find_first_of('\\');
183 if (index != std::string::npos) {
184 if (secret) // Retrieve the secret if asked for.
185 base::StringToInt(channel_id.substr(index + 1), secret);
186 return ASCIIToWide(name.append(channel_id.substr(0, index - 1)));
187 }
188
189 // This case is here to support predictable named pipes in tests.
190 if (secret)
191 *secret = 0;
[email protected]c2391b82011-05-06 17:39:07192 return ASCIIToWide(name.append(channel_id));
initial.commit09911bf2008-07-26 23:55:29193}
194
[email protected]42ce94e2010-12-08 19:28:09195bool Channel::ChannelImpl::CreatePipe(const IPC::ChannelHandle &channel_handle,
[email protected]514411fc2008-12-10 22:28:11196 Mode mode) {
[email protected]5e0be642011-04-28 18:20:09197 DCHECK_EQ(INVALID_HANDLE_VALUE, pipe_);
[email protected]a7c03d4f32012-01-24 02:36:05198 string16 pipe_name;
199 // If we already have a valid pipe for channel just copy it.
200 if (channel_handle.pipe.handle) {
201 DCHECK(channel_handle.name.empty());
202 pipe_name = L"Not Available"; // Just used for LOG
203 // Check that the given pipe confirms to the specified mode. We can
204 // only check for PIPE_TYPE_MESSAGE & PIPE_SERVER_END flags since the
205 // other flags (PIPE_TYPE_BYTE, and PIPE_CLIENT_END) are defined as 0.
206 DWORD flags = 0;
207 GetNamedPipeInfo(channel_handle.pipe.handle, &flags, NULL, NULL, NULL);
208 DCHECK(!(flags & PIPE_TYPE_MESSAGE));
209 if (((mode & MODE_SERVER_FLAG) && !(flags & PIPE_SERVER_END)) ||
210 ((mode & MODE_CLIENT_FLAG) && (flags & PIPE_SERVER_END))) {
211 LOG(WARNING) << "Inconsistent open mode. Mode :" << mode;
212 return false;
213 }
214 if (!DuplicateHandle(GetCurrentProcess(),
215 channel_handle.pipe.handle,
216 GetCurrentProcess(),
217 &pipe_,
218 0,
219 FALSE,
220 DUPLICATE_SAME_ACCESS)) {
221 LOG(WARNING) << "DuplicateHandle failed. Error :" << GetLastError();
222 return false;
223 }
224 } else if (mode & MODE_SERVER_FLAG) {
225 DCHECK(!channel_handle.pipe.handle);
226 const DWORD open_mode = PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED |
227 FILE_FLAG_FIRST_PIPE_INSTANCE;
[email protected]5c41e6e12012-03-17 02:20:46228 pipe_name = PipeName(channel_handle.name, &client_secret_);
229 validate_client_ = !!client_secret_;
initial.commit09911bf2008-07-26 23:55:29230 pipe_ = CreateNamedPipeW(pipe_name.c_str(),
[email protected]a7c03d4f32012-01-24 02:36:05231 open_mode,
initial.commit09911bf2008-07-26 23:55:29232 PIPE_TYPE_BYTE | PIPE_READMODE_BYTE,
[email protected]c2391b82011-05-06 17:39:07233 1,
[email protected]514411fc2008-12-10 22:28:11234 Channel::kReadBufferSize,
[email protected]514411fc2008-12-10 22:28:11235 Channel::kReadBufferSize,
[email protected]c2391b82011-05-06 17:39:07236 5000,
237 NULL);
[email protected]1707726c2011-02-03 20:35:09238 } else if (mode & MODE_CLIENT_FLAG) {
[email protected]a7c03d4f32012-01-24 02:36:05239 DCHECK(!channel_handle.pipe.handle);
[email protected]5c41e6e12012-03-17 02:20:46240 pipe_name = PipeName(channel_handle.name, &client_secret_);
initial.commit09911bf2008-07-26 23:55:29241 pipe_ = CreateFileW(pipe_name.c_str(),
242 GENERIC_READ | GENERIC_WRITE,
243 0,
244 NULL,
245 OPEN_EXISTING,
246 SECURITY_SQOS_PRESENT | SECURITY_IDENTIFICATION |
247 FILE_FLAG_OVERLAPPED,
248 NULL);
[email protected]1707726c2011-02-03 20:35:09249 } else {
250 NOTREACHED();
initial.commit09911bf2008-07-26 23:55:29251 }
[email protected]a7c03d4f32012-01-24 02:36:05252
initial.commit09911bf2008-07-26 23:55:29253 if (pipe_ == INVALID_HANDLE_VALUE) {
254 // If this process is being closed, the pipe may be gone already.
[email protected]c2391b82011-05-06 17:39:07255 LOG(WARNING) << "Unable to create pipe \"" << pipe_name <<
[email protected]34088fb2012-05-06 23:21:55256 "\" in " << (mode & MODE_SERVER_FLAG ? "server" : "client")
[email protected]c2391b82011-05-06 17:39:07257 << " mode. Error :" << GetLastError();
initial.commit09911bf2008-07-26 23:55:29258 return false;
259 }
260
261 // Create the Hello message to be sent when Connect is called
262 scoped_ptr<Message> m(new Message(MSG_ROUTING_NONE,
263 HELLO_MESSAGE_TYPE,
264 IPC::Message::PRIORITY_NORMAL));
[email protected]5c41e6e12012-03-17 02:20:46265
266 // Don't send the secret to the untrusted process, and don't send a secret
267 // if the value is zero (for IPC backwards compatability).
268 int32 secret = validate_client_ ? 0 : client_secret_;
269 if (!m->WriteInt(GetCurrentProcessId()) ||
270 (secret && !m->WriteUInt32(secret))) {
initial.commit09911bf2008-07-26 23:55:29271 CloseHandle(pipe_);
272 pipe_ = INVALID_HANDLE_VALUE;
273 return false;
274 }
275
276 output_queue_.push(m.release());
277 return true;
278}
279
[email protected]514411fc2008-12-10 22:28:11280bool Channel::ChannelImpl::Connect() {
[email protected]19340722009-08-17 19:53:25281 DLOG_IF(WARNING, thread_check_.get()) << "Connect called more than once";
initial.commit09911bf2008-07-26 23:55:29282
[email protected]c1e4bff32009-01-29 00:07:06283 if (!thread_check_.get())
[email protected]327e52b2012-06-25 21:11:36284 thread_check_.reset(new base::ThreadChecker());
[email protected]c1e4bff32009-01-29 00:07:06285
initial.commit09911bf2008-07-26 23:55:29286 if (pipe_ == INVALID_HANDLE_VALUE)
287 return false;
288
[email protected]c1afbd2c2008-10-13 19:19:36289 MessageLoopForIO::current()->RegisterIOHandler(pipe_, this);
290
initial.commit09911bf2008-07-26 23:55:29291 // Check to see if there is a client connected to our pipe...
292 if (waiting_connect_)
293 ProcessConnection();
294
295 if (!input_state_.is_pending) {
[email protected]c1afbd2c2008-10-13 19:19:36296 // Complete setup asynchronously. By not setting input_state_.is_pending
297 // to true, we indicate to OnIOCompleted that this is the special
298 // initialization signal.
[email protected]72b6f8e22011-11-12 21:16:41299 MessageLoopForIO::current()->PostTask(
300 FROM_HERE, base::Bind(&Channel::ChannelImpl::OnIOCompleted,
301 weak_factory_.GetWeakPtr(), &input_state_.context,
302 0, 0));
initial.commit09911bf2008-07-26 23:55:29303 }
304
305 if (!waiting_connect_)
[email protected]c1afbd2c2008-10-13 19:19:36306 ProcessOutgoingMessages(NULL, 0);
initial.commit09911bf2008-07-26 23:55:29307 return true;
308}
309
[email protected]514411fc2008-12-10 22:28:11310bool Channel::ChannelImpl::ProcessConnection() {
[email protected]c1e4bff32009-01-29 00:07:06311 DCHECK(thread_check_->CalledOnValidThread());
[email protected]17b89142008-11-07 21:52:15312 if (input_state_.is_pending)
[email protected]c1afbd2c2008-10-13 19:19:36313 input_state_.is_pending = false;
initial.commit09911bf2008-07-26 23:55:29314
315 // Do we have a client connected to our pipe?
[email protected]17b89142008-11-07 21:52:15316 if (INVALID_HANDLE_VALUE == pipe_)
317 return false;
318
319 BOOL ok = ConnectNamedPipe(pipe_, &input_state_.context.overlapped);
initial.commit09911bf2008-07-26 23:55:29320
321 DWORD err = GetLastError();
322 if (ok) {
323 // Uhm, the API documentation says that this function should never
324 // return success when used in overlapped mode.
325 NOTREACHED();
326 return false;
327 }
328
329 switch (err) {
330 case ERROR_IO_PENDING:
331 input_state_.is_pending = true;
initial.commit09911bf2008-07-26 23:55:29332 break;
333 case ERROR_PIPE_CONNECTED:
334 waiting_connect_ = false;
335 break;
[email protected]20aa32c2009-07-14 22:25:49336 case ERROR_NO_DATA:
337 // The pipe is being closed.
338 return false;
initial.commit09911bf2008-07-26 23:55:29339 default:
340 NOTREACHED();
341 return false;
342 }
343
344 return true;
345}
346
[email protected]514411fc2008-12-10 22:28:11347bool Channel::ChannelImpl::ProcessOutgoingMessages(
348 MessageLoopForIO::IOContext* context,
349 DWORD bytes_written) {
initial.commit09911bf2008-07-26 23:55:29350 DCHECK(!waiting_connect_); // Why are we trying to send messages if there's
351 // no connection?
[email protected]c1e4bff32009-01-29 00:07:06352 DCHECK(thread_check_->CalledOnValidThread());
initial.commit09911bf2008-07-26 23:55:29353
354 if (output_state_.is_pending) {
[email protected]c1afbd2c2008-10-13 19:19:36355 DCHECK(context);
initial.commit09911bf2008-07-26 23:55:29356 output_state_.is_pending = false;
[email protected]c1afbd2c2008-10-13 19:19:36357 if (!context || bytes_written == 0) {
initial.commit09911bf2008-07-26 23:55:29358 DWORD err = GetLastError();
359 LOG(ERROR) << "pipe error: " << err;
360 return false;
361 }
[email protected]c1afbd2c2008-10-13 19:19:36362 // Message was sent.
initial.commit09911bf2008-07-26 23:55:29363 DCHECK(!output_queue_.empty());
364 Message* m = output_queue_.front();
365 output_queue_.pop();
366 delete m;
367 }
368
[email protected]17b89142008-11-07 21:52:15369 if (output_queue_.empty())
370 return true;
371
372 if (INVALID_HANDLE_VALUE == pipe_)
373 return false;
374
375 // Write to pipe...
376 Message* m = output_queue_.front();
[email protected]8a861402011-01-28 19:59:11377 DCHECK(m->size() <= INT_MAX);
[email protected]17b89142008-11-07 21:52:15378 BOOL ok = WriteFile(pipe_,
379 m->data(),
[email protected]8a861402011-01-28 19:59:11380 static_cast<int>(m->size()),
[email protected]17b89142008-11-07 21:52:15381 &bytes_written,
382 &output_state_.context.overlapped);
383 if (!ok) {
384 DWORD err = GetLastError();
385 if (err == ERROR_IO_PENDING) {
386 output_state_.is_pending = true;
initial.commit09911bf2008-07-26 23:55:29387
[email protected]2a9d601b2010-10-19 23:50:00388 DVLOG(2) << "sent pending message @" << m << " on channel @" << this
389 << " with type " << m->type();
initial.commit09911bf2008-07-26 23:55:29390
[email protected]17b89142008-11-07 21:52:15391 return true;
initial.commit09911bf2008-07-26 23:55:29392 }
[email protected]17b89142008-11-07 21:52:15393 LOG(ERROR) << "pipe error: " << err;
394 return false;
initial.commit09911bf2008-07-26 23:55:29395 }
396
[email protected]2a9d601b2010-10-19 23:50:00397 DVLOG(2) << "sent message @" << m << " on channel @" << this
398 << " with type " << m->type();
[email protected]17b89142008-11-07 21:52:15399
400 output_state_.is_pending = true;
initial.commit09911bf2008-07-26 23:55:29401 return true;
402}
403
[email protected]514411fc2008-12-10 22:28:11404void Channel::ChannelImpl::OnIOCompleted(MessageLoopForIO::IOContext* context,
[email protected]215f6fd2012-03-03 08:55:45405 DWORD bytes_transfered,
406 DWORD error) {
407 bool ok = true;
[email protected]c1e4bff32009-01-29 00:07:06408 DCHECK(thread_check_->CalledOnValidThread());
[email protected]17b89142008-11-07 21:52:15409 if (context == &input_state_.context) {
initial.commit09911bf2008-07-26 23:55:29410 if (waiting_connect_) {
[email protected]17b89142008-11-07 21:52:15411 if (!ProcessConnection())
412 return;
initial.commit09911bf2008-07-26 23:55:29413 // We may have some messages queued up to send...
414 if (!output_queue_.empty() && !output_state_.is_pending)
[email protected]c1afbd2c2008-10-13 19:19:36415 ProcessOutgoingMessages(NULL, 0);
initial.commit09911bf2008-07-26 23:55:29416 if (input_state_.is_pending)
417 return;
418 // else, fall-through and look for incoming messages...
419 }
[email protected]215f6fd2012-03-03 08:55:45420
421 // We don't support recursion through OnMessageReceived yet!
initial.commit09911bf2008-07-26 23:55:29422 DCHECK(!processing_incoming_);
[email protected]997ec9f2012-11-21 04:44:14423 base::AutoReset<bool> auto_reset_processing_incoming(
424 &processing_incoming_, true);
[email protected]215f6fd2012-03-03 08:55:45425
426 // Process the new data.
427 if (input_state_.is_pending) {
428 // This is the normal case for everything except the initialization step.
429 input_state_.is_pending = false;
430 if (!bytes_transfered)
431 ok = false;
432 else
433 ok = AsyncReadComplete(bytes_transfered);
434 } else {
435 DCHECK(!bytes_transfered);
436 }
437
438 // Request more data.
439 if (ok)
440 ok = ProcessIncomingMessages();
initial.commit09911bf2008-07-26 23:55:29441 } else {
[email protected]17b89142008-11-07 21:52:15442 DCHECK(context == &output_state_.context);
[email protected]c1afbd2c2008-10-13 19:19:36443 ok = ProcessOutgoingMessages(context, bytes_transfered);
initial.commit09911bf2008-07-26 23:55:29444 }
[email protected]17b89142008-11-07 21:52:15445 if (!ok && INVALID_HANDLE_VALUE != pipe_) {
446 // We don't want to re-enter Close().
initial.commit09911bf2008-07-26 23:55:29447 Close();
[email protected]d805c6a2012-03-08 12:30:28448 listener()->OnChannelError();
initial.commit09911bf2008-07-26 23:55:29449 }
450}
451
[email protected]514411fc2008-12-10 22:28:11452//------------------------------------------------------------------------------
453// Channel's methods simply call through to ChannelImpl.
[email protected]42ce94e2010-12-08 19:28:09454Channel::Channel(const IPC::ChannelHandle &channel_handle, Mode mode,
[email protected]514411fc2008-12-10 22:28:11455 Listener* listener)
[email protected]42ce94e2010-12-08 19:28:09456 : channel_impl_(new ChannelImpl(channel_handle, mode, listener)) {
initial.commit09911bf2008-07-26 23:55:29457}
[email protected]514411fc2008-12-10 22:28:11458
459Channel::~Channel() {
460 delete channel_impl_;
461}
462
463bool Channel::Connect() {
464 return channel_impl_->Connect();
465}
466
467void Channel::Close() {
[email protected]5fc7ec592012-05-16 18:58:32468 if (channel_impl_)
469 channel_impl_->Close();
[email protected]514411fc2008-12-10 22:28:11470}
471
472void Channel::set_listener(Listener* listener) {
473 channel_impl_->set_listener(listener);
474}
475
[email protected]0a6fc4b2012-04-05 02:38:34476base::ProcessId Channel::peer_pid() const {
477 return channel_impl_->peer_pid();
478}
479
[email protected]514411fc2008-12-10 22:28:11480bool Channel::Send(Message* message) {
481 return channel_impl_->Send(message);
482}
483
[email protected]313c00e52011-08-09 06:46:06484// static
485bool Channel::IsNamedServerInitialized(const std::string& channel_id) {
486 return ChannelImpl::IsNamedServerInitialized(channel_id);
487}
488
[email protected]5c41e6e12012-03-17 02:20:46489// static
490std::string Channel::GenerateVerifiedChannelID(const std::string& prefix) {
491 // Windows pipes can be enumerated by low-privileged processes. So, we
492 // append a strong random value after the \ character. This value is not
493 // included in the pipe name, but sent as part of the client hello, to
494 // hijacking the pipe name to spoof the client.
495
496 std::string id = prefix;
497 if (!id.empty())
498 id.append(".");
499
500 int secret;
501 do { // Guarantee we get a non-zero value.
502 secret = base::RandInt(0, std::numeric_limits<int>::max());
503 } while (secret == 0);
504
505 id.append(GenerateUniqueRandomChannelID());
506 return id.append(base::StringPrintf("\\%d", secret));
507}
508
[email protected]514411fc2008-12-10 22:28:11509} // namespace IPC