blob: a34d6d7829de64702efb19c1fa693ab9b84d7625 [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]c9177502011-01-01 04:48:4916#include "base/threading/non_thread_safe.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]946d1b22009-07-22 23:57:2119#include "ipc/ipc_logging.h"
20#include "ipc/ipc_message_utils.h"
initial.commit09911bf2008-07-26 23:55:2921
initial.commit09911bf2008-07-26 23:55:2922namespace IPC {
[email protected]935aa542010-10-15 01:59:1523
[email protected]514411fc2008-12-10 22:28:1124Channel::ChannelImpl::State::State(ChannelImpl* channel) : is_pending(false) {
[email protected]17b89142008-11-07 21:52:1525 memset(&context.overlapped, 0, sizeof(context.overlapped));
26 context.handler = channel;
initial.commit09911bf2008-07-26 23:55:2927}
28
[email protected]514411fc2008-12-10 22:28:1129Channel::ChannelImpl::State::~State() {
30 COMPILE_ASSERT(!offsetof(Channel::ChannelImpl::State, context),
31 starts_with_io_context);
initial.commit09911bf2008-07-26 23:55:2932}
33
[email protected]42ce94e2010-12-08 19:28:0934Channel::ChannelImpl::ChannelImpl(const IPC::ChannelHandle &channel_handle,
35 Mode mode, Listener* listener)
[email protected]d805c6a2012-03-08 12:30:2836 : ChannelReader(listener),
37 ALLOW_THIS_IN_INITIALIZER_LIST(input_state_(this)),
[email protected]17b89142008-11-07 21:52:1538 ALLOW_THIS_IN_INITIALIZER_LIST(output_state_(this)),
39 pipe_(INVALID_HANDLE_VALUE),
[email protected]0a6fc4b2012-04-05 02:38:3440 peer_pid_(base::kNullProcessId),
[email protected]1707726c2011-02-03 20:35:0941 waiting_connect_(mode & MODE_SERVER_FLAG),
[email protected]c1afbd2c2008-10-13 19:19:3642 processing_incoming_(false),
[email protected]5c41e6e12012-03-17 02:20:4643 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)),
44 client_secret_(0),
45 validate_client_(false) {
[email protected]c2391b82011-05-06 17:39:0746 CreatePipe(channel_handle, mode);
initial.commit09911bf2008-07-26 23:55:2947}
48
[email protected]601858c02010-09-01 17:08:2049Channel::ChannelImpl::~ChannelImpl() {
50 Close();
51}
52
[email protected]514411fc2008-12-10 22:28:1153void Channel::ChannelImpl::Close() {
[email protected]c1e4bff32009-01-29 00:07:0654 if (thread_check_.get()) {
55 DCHECK(thread_check_->CalledOnValidThread());
56 }
57
[email protected]74f87acf22009-10-14 22:10:4058 if (input_state_.is_pending || output_state_.is_pending)
[email protected]17b89142008-11-07 21:52:1559 CancelIo(pipe_);
[email protected]ee78622d2008-10-13 21:25:5060
[email protected]17b89142008-11-07 21:52:1561 // Closing the handle at this point prevents us from issuing more requests
62 // form OnIOCompleted().
initial.commit09911bf2008-07-26 23:55:2963 if (pipe_ != INVALID_HANDLE_VALUE) {
64 CloseHandle(pipe_);
65 pipe_ = INVALID_HANDLE_VALUE;
66 }
67
[email protected]17b89142008-11-07 21:52:1568 // Make sure all IO has completed.
69 base::Time start = base::Time::Now();
70 while (input_state_.is_pending || output_state_.is_pending) {
71 MessageLoopForIO::current()->WaitForIOCompletion(INFINITE, this);
72 }
[email protected]17b89142008-11-07 21:52:1573
initial.commit09911bf2008-07-26 23:55:2974 while (!output_queue_.empty()) {
75 Message* m = output_queue_.front();
76 output_queue_.pop();
77 delete m;
78 }
79}
80
[email protected]514411fc2008-12-10 22:28:1181bool Channel::ChannelImpl::Send(Message* message) {
[email protected]c1e4bff32009-01-29 00:07:0682 DCHECK(thread_check_->CalledOnValidThread());
[email protected]2a9d601b2010-10-19 23:50:0083 DVLOG(2) << "sending message @" << message << " on channel @" << this
84 << " with type " << message->type()
85 << " (" << output_queue_.size() << " in queue)";
initial.commit09911bf2008-07-26 23:55:2986
87#ifdef IPC_MESSAGE_LOG_ENABLED
[email protected]8e8bb6d2010-12-13 08:18:5588 Logging::GetInstance()->OnSendMessage(message, "");
initial.commit09911bf2008-07-26 23:55:2989#endif
90
91 output_queue_.push(message);
92 // ensure waiting to write
93 if (!waiting_connect_) {
94 if (!output_state_.is_pending) {
[email protected]c1afbd2c2008-10-13 19:19:3695 if (!ProcessOutgoingMessages(NULL, 0))
initial.commit09911bf2008-07-26 23:55:2996 return false;
initial.commit09911bf2008-07-26 23:55:2997 }
98 }
99
100 return true;
101}
102
[email protected]313c00e52011-08-09 06:46:06103// static
104bool Channel::ChannelImpl::IsNamedServerInitialized(
105 const std::string& channel_id) {
[email protected]5c41e6e12012-03-17 02:20:46106 if (WaitNamedPipe(PipeName(channel_id, NULL).c_str(), 1))
[email protected]313c00e52011-08-09 06:46:06107 return true;
108 // If ERROR_SEM_TIMEOUT occurred, the pipe exists but is handling another
109 // connection.
110 return GetLastError() == ERROR_SEM_TIMEOUT;
111}
112
[email protected]215f6fd2012-03-03 08:55:45113Channel::ChannelImpl::ReadState Channel::ChannelImpl::ReadData(
114 char* buffer,
115 int buffer_len,
116 int* /* bytes_read */) {
117 if (INVALID_HANDLE_VALUE == pipe_)
118 return READ_FAILED;
119
120 DWORD bytes_read = 0;
[email protected]d805c6a2012-03-08 12:30:28121 BOOL ok = ReadFile(pipe_, buffer, buffer_len,
[email protected]215f6fd2012-03-03 08:55:45122 &bytes_read, &input_state_.context.overlapped);
123 if (!ok) {
124 DWORD err = GetLastError();
125 if (err == ERROR_IO_PENDING) {
126 input_state_.is_pending = true;
127 return READ_PENDING;
128 }
129 LOG(ERROR) << "pipe error: " << err;
130 return READ_FAILED;
131 }
132
133 // We could return READ_SUCCEEDED here. But the way that this code is
134 // structured we instead go back to the message loop. Our completion port
135 // will be signalled even in the "synchronously completed" state.
136 //
137 // This allows us to potentially process some outgoing messages and
138 // interleave other work on this thread when we're getting hammered with
139 // input messages. Potentially, this could be tuned to be more efficient
140 // with some testing.
141 input_state_.is_pending = true;
142 return READ_PENDING;
143}
144
145bool Channel::ChannelImpl::WillDispatchInputMessage(Message* msg) {
[email protected]05908782012-04-03 08:49:43146 // Make sure we get a hello when client validation is required.
147 if (validate_client_)
148 return IsHelloMessage(*msg);
[email protected]215f6fd2012-03-03 08:55:45149 return true;
150}
151
152void Channel::ChannelImpl::HandleHelloMessage(const Message& msg) {
153 // The hello message contains one parameter containing the PID.
[email protected]5c41e6e12012-03-17 02:20:46154 MessageIterator it = MessageIterator(msg);
155 int32 claimed_pid = it.NextInt();
156 if (validate_client_ && (it.NextInt() != client_secret_)) {
157 NOTREACHED();
158 // Something went wrong. Abort connection.
159 Close();
160 listener()->OnChannelError();
161 return;
162 }
[email protected]0a6fc4b2012-04-05 02:38:34163 peer_pid_ = claimed_pid;
[email protected]05908782012-04-03 08:49:43164 // validation completed.
165 validate_client_ = false;
[email protected]5c41e6e12012-03-17 02:20:46166 listener()->OnChannelConnected(claimed_pid);
[email protected]215f6fd2012-03-03 08:55:45167}
168
169bool Channel::ChannelImpl::DidEmptyInputBuffers() {
[email protected]d805c6a2012-03-08 12:30:28170 // We don't need to do anything here.
[email protected]215f6fd2012-03-03 08:55:45171 return true;
172}
173
[email protected]313c00e52011-08-09 06:46:06174// static
[email protected]5c41e6e12012-03-17 02:20:46175const string16 Channel::ChannelImpl::PipeName(
176 const std::string& channel_id, int32* secret) {
[email protected]c2391b82011-05-06 17:39:07177 std::string name("\\\\.\\pipe\\chrome.");
[email protected]5c41e6e12012-03-17 02:20:46178
179 // Prevent the shared secret from ending up in the pipe name.
180 size_t index = channel_id.find_first_of('\\');
181 if (index != std::string::npos) {
182 if (secret) // Retrieve the secret if asked for.
183 base::StringToInt(channel_id.substr(index + 1), secret);
184 return ASCIIToWide(name.append(channel_id.substr(0, index - 1)));
185 }
186
187 // This case is here to support predictable named pipes in tests.
188 if (secret)
189 *secret = 0;
[email protected]c2391b82011-05-06 17:39:07190 return ASCIIToWide(name.append(channel_id));
initial.commit09911bf2008-07-26 23:55:29191}
192
[email protected]42ce94e2010-12-08 19:28:09193bool Channel::ChannelImpl::CreatePipe(const IPC::ChannelHandle &channel_handle,
[email protected]514411fc2008-12-10 22:28:11194 Mode mode) {
[email protected]5e0be642011-04-28 18:20:09195 DCHECK_EQ(INVALID_HANDLE_VALUE, pipe_);
[email protected]a7c03d4f32012-01-24 02:36:05196 string16 pipe_name;
197 // If we already have a valid pipe for channel just copy it.
198 if (channel_handle.pipe.handle) {
199 DCHECK(channel_handle.name.empty());
200 pipe_name = L"Not Available"; // Just used for LOG
201 // Check that the given pipe confirms to the specified mode. We can
202 // only check for PIPE_TYPE_MESSAGE & PIPE_SERVER_END flags since the
203 // other flags (PIPE_TYPE_BYTE, and PIPE_CLIENT_END) are defined as 0.
204 DWORD flags = 0;
205 GetNamedPipeInfo(channel_handle.pipe.handle, &flags, NULL, NULL, NULL);
206 DCHECK(!(flags & PIPE_TYPE_MESSAGE));
207 if (((mode & MODE_SERVER_FLAG) && !(flags & PIPE_SERVER_END)) ||
208 ((mode & MODE_CLIENT_FLAG) && (flags & PIPE_SERVER_END))) {
209 LOG(WARNING) << "Inconsistent open mode. Mode :" << mode;
210 return false;
211 }
212 if (!DuplicateHandle(GetCurrentProcess(),
213 channel_handle.pipe.handle,
214 GetCurrentProcess(),
215 &pipe_,
216 0,
217 FALSE,
218 DUPLICATE_SAME_ACCESS)) {
219 LOG(WARNING) << "DuplicateHandle failed. Error :" << GetLastError();
220 return false;
221 }
222 } else if (mode & MODE_SERVER_FLAG) {
223 DCHECK(!channel_handle.pipe.handle);
224 const DWORD open_mode = PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED |
225 FILE_FLAG_FIRST_PIPE_INSTANCE;
[email protected]5c41e6e12012-03-17 02:20:46226 pipe_name = PipeName(channel_handle.name, &client_secret_);
227 validate_client_ = !!client_secret_;
initial.commit09911bf2008-07-26 23:55:29228 pipe_ = CreateNamedPipeW(pipe_name.c_str(),
[email protected]a7c03d4f32012-01-24 02:36:05229 open_mode,
initial.commit09911bf2008-07-26 23:55:29230 PIPE_TYPE_BYTE | PIPE_READMODE_BYTE,
[email protected]c2391b82011-05-06 17:39:07231 1,
[email protected]514411fc2008-12-10 22:28:11232 Channel::kReadBufferSize,
[email protected]514411fc2008-12-10 22:28:11233 Channel::kReadBufferSize,
[email protected]c2391b82011-05-06 17:39:07234 5000,
235 NULL);
[email protected]1707726c2011-02-03 20:35:09236 } else if (mode & MODE_CLIENT_FLAG) {
[email protected]a7c03d4f32012-01-24 02:36:05237 DCHECK(!channel_handle.pipe.handle);
[email protected]5c41e6e12012-03-17 02:20:46238 pipe_name = PipeName(channel_handle.name, &client_secret_);
initial.commit09911bf2008-07-26 23:55:29239 pipe_ = CreateFileW(pipe_name.c_str(),
240 GENERIC_READ | GENERIC_WRITE,
241 0,
242 NULL,
243 OPEN_EXISTING,
244 SECURITY_SQOS_PRESENT | SECURITY_IDENTIFICATION |
245 FILE_FLAG_OVERLAPPED,
246 NULL);
[email protected]1707726c2011-02-03 20:35:09247 } else {
248 NOTREACHED();
initial.commit09911bf2008-07-26 23:55:29249 }
[email protected]a7c03d4f32012-01-24 02:36:05250
initial.commit09911bf2008-07-26 23:55:29251 if (pipe_ == INVALID_HANDLE_VALUE) {
252 // If this process is being closed, the pipe may be gone already.
[email protected]c2391b82011-05-06 17:39:07253 LOG(WARNING) << "Unable to create pipe \"" << pipe_name <<
254 "\" in " << (mode == 0 ? "server" : "client")
255 << " mode. Error :" << GetLastError();
initial.commit09911bf2008-07-26 23:55:29256 return false;
257 }
258
259 // Create the Hello message to be sent when Connect is called
260 scoped_ptr<Message> m(new Message(MSG_ROUTING_NONE,
261 HELLO_MESSAGE_TYPE,
262 IPC::Message::PRIORITY_NORMAL));
[email protected]5c41e6e12012-03-17 02:20:46263
264 // Don't send the secret to the untrusted process, and don't send a secret
265 // if the value is zero (for IPC backwards compatability).
266 int32 secret = validate_client_ ? 0 : client_secret_;
267 if (!m->WriteInt(GetCurrentProcessId()) ||
268 (secret && !m->WriteUInt32(secret))) {
initial.commit09911bf2008-07-26 23:55:29269 CloseHandle(pipe_);
270 pipe_ = INVALID_HANDLE_VALUE;
271 return false;
272 }
273
274 output_queue_.push(m.release());
275 return true;
276}
277
[email protected]514411fc2008-12-10 22:28:11278bool Channel::ChannelImpl::Connect() {
[email protected]19340722009-08-17 19:53:25279 DLOG_IF(WARNING, thread_check_.get()) << "Connect called more than once";
initial.commit09911bf2008-07-26 23:55:29280
[email protected]c1e4bff32009-01-29 00:07:06281 if (!thread_check_.get())
[email protected]c9177502011-01-01 04:48:49282 thread_check_.reset(new base::NonThreadSafe());
[email protected]c1e4bff32009-01-29 00:07:06283
initial.commit09911bf2008-07-26 23:55:29284 if (pipe_ == INVALID_HANDLE_VALUE)
285 return false;
286
[email protected]c1afbd2c2008-10-13 19:19:36287 MessageLoopForIO::current()->RegisterIOHandler(pipe_, this);
288
initial.commit09911bf2008-07-26 23:55:29289 // Check to see if there is a client connected to our pipe...
290 if (waiting_connect_)
291 ProcessConnection();
292
293 if (!input_state_.is_pending) {
[email protected]c1afbd2c2008-10-13 19:19:36294 // Complete setup asynchronously. By not setting input_state_.is_pending
295 // to true, we indicate to OnIOCompleted that this is the special
296 // initialization signal.
[email protected]72b6f8e22011-11-12 21:16:41297 MessageLoopForIO::current()->PostTask(
298 FROM_HERE, base::Bind(&Channel::ChannelImpl::OnIOCompleted,
299 weak_factory_.GetWeakPtr(), &input_state_.context,
300 0, 0));
initial.commit09911bf2008-07-26 23:55:29301 }
302
303 if (!waiting_connect_)
[email protected]c1afbd2c2008-10-13 19:19:36304 ProcessOutgoingMessages(NULL, 0);
initial.commit09911bf2008-07-26 23:55:29305 return true;
306}
307
[email protected]514411fc2008-12-10 22:28:11308bool Channel::ChannelImpl::ProcessConnection() {
[email protected]c1e4bff32009-01-29 00:07:06309 DCHECK(thread_check_->CalledOnValidThread());
[email protected]17b89142008-11-07 21:52:15310 if (input_state_.is_pending)
[email protected]c1afbd2c2008-10-13 19:19:36311 input_state_.is_pending = false;
initial.commit09911bf2008-07-26 23:55:29312
313 // Do we have a client connected to our pipe?
[email protected]17b89142008-11-07 21:52:15314 if (INVALID_HANDLE_VALUE == pipe_)
315 return false;
316
317 BOOL ok = ConnectNamedPipe(pipe_, &input_state_.context.overlapped);
initial.commit09911bf2008-07-26 23:55:29318
319 DWORD err = GetLastError();
320 if (ok) {
321 // Uhm, the API documentation says that this function should never
322 // return success when used in overlapped mode.
323 NOTREACHED();
324 return false;
325 }
326
327 switch (err) {
328 case ERROR_IO_PENDING:
329 input_state_.is_pending = true;
initial.commit09911bf2008-07-26 23:55:29330 break;
331 case ERROR_PIPE_CONNECTED:
332 waiting_connect_ = false;
333 break;
[email protected]20aa32c2009-07-14 22:25:49334 case ERROR_NO_DATA:
335 // The pipe is being closed.
336 return false;
initial.commit09911bf2008-07-26 23:55:29337 default:
338 NOTREACHED();
339 return false;
340 }
341
342 return true;
343}
344
[email protected]514411fc2008-12-10 22:28:11345bool Channel::ChannelImpl::ProcessOutgoingMessages(
346 MessageLoopForIO::IOContext* context,
347 DWORD bytes_written) {
initial.commit09911bf2008-07-26 23:55:29348 DCHECK(!waiting_connect_); // Why are we trying to send messages if there's
349 // no connection?
[email protected]c1e4bff32009-01-29 00:07:06350 DCHECK(thread_check_->CalledOnValidThread());
initial.commit09911bf2008-07-26 23:55:29351
352 if (output_state_.is_pending) {
[email protected]c1afbd2c2008-10-13 19:19:36353 DCHECK(context);
initial.commit09911bf2008-07-26 23:55:29354 output_state_.is_pending = false;
[email protected]c1afbd2c2008-10-13 19:19:36355 if (!context || bytes_written == 0) {
initial.commit09911bf2008-07-26 23:55:29356 DWORD err = GetLastError();
357 LOG(ERROR) << "pipe error: " << err;
358 return false;
359 }
[email protected]c1afbd2c2008-10-13 19:19:36360 // Message was sent.
initial.commit09911bf2008-07-26 23:55:29361 DCHECK(!output_queue_.empty());
362 Message* m = output_queue_.front();
363 output_queue_.pop();
364 delete m;
365 }
366
[email protected]17b89142008-11-07 21:52:15367 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]8a861402011-01-28 19:59:11375 DCHECK(m->size() <= INT_MAX);
[email protected]17b89142008-11-07 21:52:15376 BOOL ok = WriteFile(pipe_,
377 m->data(),
[email protected]8a861402011-01-28 19:59:11378 static_cast<int>(m->size()),
[email protected]17b89142008-11-07 21:52:15379 &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.commit09911bf2008-07-26 23:55:29385
[email protected]2a9d601b2010-10-19 23:50:00386 DVLOG(2) << "sent pending message @" << m << " on channel @" << this
387 << " with type " << m->type();
initial.commit09911bf2008-07-26 23:55:29388
[email protected]17b89142008-11-07 21:52:15389 return true;
initial.commit09911bf2008-07-26 23:55:29390 }
[email protected]17b89142008-11-07 21:52:15391 LOG(ERROR) << "pipe error: " << err;
392 return false;
initial.commit09911bf2008-07-26 23:55:29393 }
394
[email protected]2a9d601b2010-10-19 23:50:00395 DVLOG(2) << "sent message @" << m << " on channel @" << this
396 << " with type " << m->type();
[email protected]17b89142008-11-07 21:52:15397
398 output_state_.is_pending = true;
initial.commit09911bf2008-07-26 23:55:29399 return true;
400}
401
[email protected]514411fc2008-12-10 22:28:11402void Channel::ChannelImpl::OnIOCompleted(MessageLoopForIO::IOContext* context,
[email protected]215f6fd2012-03-03 08:55:45403 DWORD bytes_transfered,
404 DWORD error) {
405 bool ok = true;
[email protected]c1e4bff32009-01-29 00:07:06406 DCHECK(thread_check_->CalledOnValidThread());
[email protected]17b89142008-11-07 21:52:15407 if (context == &input_state_.context) {
initial.commit09911bf2008-07-26 23:55:29408 if (waiting_connect_) {
[email protected]17b89142008-11-07 21:52:15409 if (!ProcessConnection())
410 return;
initial.commit09911bf2008-07-26 23:55:29411 // We may have some messages queued up to send...
412 if (!output_queue_.empty() && !output_state_.is_pending)
[email protected]c1afbd2c2008-10-13 19:19:36413 ProcessOutgoingMessages(NULL, 0);
initial.commit09911bf2008-07-26 23:55:29414 if (input_state_.is_pending)
415 return;
416 // else, fall-through and look for incoming messages...
417 }
[email protected]215f6fd2012-03-03 08:55:45418
419 // We don't support recursion through OnMessageReceived yet!
initial.commit09911bf2008-07-26 23:55:29420 DCHECK(!processing_incoming_);
[email protected]0fbd70332010-06-01 19:28:34421 AutoReset<bool> auto_reset_processing_incoming(&processing_incoming_, true);
[email protected]215f6fd2012-03-03 08:55:45422
423 // Process the new data.
424 if (input_state_.is_pending) {
425 // This is the normal case for everything except the initialization step.
426 input_state_.is_pending = false;
427 if (!bytes_transfered)
428 ok = false;
429 else
430 ok = AsyncReadComplete(bytes_transfered);
431 } else {
432 DCHECK(!bytes_transfered);
433 }
434
435 // Request more data.
436 if (ok)
437 ok = ProcessIncomingMessages();
initial.commit09911bf2008-07-26 23:55:29438 } else {
[email protected]17b89142008-11-07 21:52:15439 DCHECK(context == &output_state_.context);
[email protected]c1afbd2c2008-10-13 19:19:36440 ok = ProcessOutgoingMessages(context, bytes_transfered);
initial.commit09911bf2008-07-26 23:55:29441 }
[email protected]17b89142008-11-07 21:52:15442 if (!ok && INVALID_HANDLE_VALUE != pipe_) {
443 // We don't want to re-enter Close().
initial.commit09911bf2008-07-26 23:55:29444 Close();
[email protected]d805c6a2012-03-08 12:30:28445 listener()->OnChannelError();
initial.commit09911bf2008-07-26 23:55:29446 }
447}
448
[email protected]514411fc2008-12-10 22:28:11449//------------------------------------------------------------------------------
450// Channel's methods simply call through to ChannelImpl.
[email protected]42ce94e2010-12-08 19:28:09451Channel::Channel(const IPC::ChannelHandle &channel_handle, Mode mode,
[email protected]514411fc2008-12-10 22:28:11452 Listener* listener)
[email protected]42ce94e2010-12-08 19:28:09453 : channel_impl_(new ChannelImpl(channel_handle, mode, listener)) {
initial.commit09911bf2008-07-26 23:55:29454}
[email protected]514411fc2008-12-10 22:28:11455
456Channel::~Channel() {
457 delete channel_impl_;
458}
459
460bool Channel::Connect() {
461 return channel_impl_->Connect();
462}
463
464void Channel::Close() {
465 channel_impl_->Close();
466}
467
468void Channel::set_listener(Listener* listener) {
469 channel_impl_->set_listener(listener);
470}
471
[email protected]0a6fc4b2012-04-05 02:38:34472base::ProcessId Channel::peer_pid() const {
473 return channel_impl_->peer_pid();
474}
475
[email protected]514411fc2008-12-10 22:28:11476bool Channel::Send(Message* message) {
477 return channel_impl_->Send(message);
478}
479
[email protected]313c00e52011-08-09 06:46:06480// static
481bool Channel::IsNamedServerInitialized(const std::string& channel_id) {
482 return ChannelImpl::IsNamedServerInitialized(channel_id);
483}
484
[email protected]5c41e6e12012-03-17 02:20:46485// static
486std::string Channel::GenerateVerifiedChannelID(const std::string& prefix) {
487 // Windows pipes can be enumerated by low-privileged processes. So, we
488 // append a strong random value after the \ character. This value is not
489 // included in the pipe name, but sent as part of the client hello, to
490 // hijacking the pipe name to spoof the client.
491
492 std::string id = prefix;
493 if (!id.empty())
494 id.append(".");
495
496 int secret;
497 do { // Guarantee we get a non-zero value.
498 secret = base::RandInt(0, std::numeric_limits<int>::max());
499 } while (secret == 0);
500
501 id.append(GenerateUniqueRandomChannelID());
502 return id.append(base::StringPrintf("\\%d", secret));
503}
504
[email protected]514411fc2008-12-10 22:28:11505} // namespace IPC