| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ipc/ipc_channel_win.h" | 5 #include "ipc/ipc_channel_win.h" |
| 6 | 6 |
| 7 #include <windows.h> | 7 #include <windows.h> |
| 8 | 8 |
| 9 #include "base/auto_reset.h" | 9 #include "base/auto_reset.h" |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 46 waiting_connect_(mode & MODE_SERVER_FLAG), | 46 waiting_connect_(mode & MODE_SERVER_FLAG), |
| 47 processing_incoming_(false), | 47 processing_incoming_(false), |
| 48 validate_client_(false), | 48 validate_client_(false), |
| 49 client_secret_(0), | 49 client_secret_(0), |
| 50 broker_(broker), | 50 broker_(broker), |
| 51 weak_factory_(this) { | 51 weak_factory_(this) { |
| 52 CreatePipe(channel_handle, mode); | 52 CreatePipe(channel_handle, mode); |
| 53 } | 53 } |
| 54 | 54 |
| 55 ChannelWin::~ChannelWin() { | 55 ChannelWin::~ChannelWin() { |
| 56 CleanUp(); |
| 56 Close(); | 57 Close(); |
| 57 } | 58 } |
| 58 | 59 |
| 59 void ChannelWin::Close() { | 60 void ChannelWin::Close() { |
| 60 if (thread_check_.get()) | 61 if (thread_check_.get()) |
| 61 DCHECK(thread_check_->CalledOnValidThread()); | 62 DCHECK(thread_check_->CalledOnValidThread()); |
| 62 | 63 |
| 63 if (input_state_.is_pending || output_state_.is_pending) | 64 if (input_state_.is_pending || output_state_.is_pending) |
| 64 CancelIo(pipe_.Get()); | 65 CancelIo(pipe_.Get()); |
| 65 | 66 |
| 66 // Closing the handle at this point prevents us from issuing more requests | 67 // Closing the handle at this point prevents us from issuing more requests |
| 67 // form OnIOCompleted(). | 68 // form OnIOCompleted(). |
| 68 if (pipe_.IsValid()) | 69 if (pipe_.IsValid()) |
| 69 pipe_.Close(); | 70 pipe_.Close(); |
| 70 | 71 |
| 71 // Make sure all IO has completed. | 72 // Make sure all IO has completed. |
| 72 while (input_state_.is_pending || output_state_.is_pending) { | 73 while (input_state_.is_pending || output_state_.is_pending) { |
| 73 base::MessageLoopForIO::current()->WaitForIOCompletion(INFINITE, this); | 74 base::MessageLoopForIO::current()->WaitForIOCompletion(INFINITE, this); |
| 74 } | 75 } |
| 75 | 76 |
| 76 while (!output_queue_.empty()) { | 77 while (!output_queue_.empty()) { |
| 77 Message* m = output_queue_.front(); | 78 OutputElement* element = output_queue_.front(); |
| 78 output_queue_.pop(); | 79 output_queue_.pop(); |
| 79 delete m; | 80 delete element; |
| 80 } | 81 } |
| 81 } | 82 } |
| 82 | 83 |
| 83 bool ChannelWin::Send(Message* message) { | 84 bool ChannelWin::Send(Message* message) { |
| 84 DCHECK(thread_check_->CalledOnValidThread()); | 85 DCHECK(thread_check_->CalledOnValidThread()); |
| 85 DVLOG(2) << "sending message @" << message << " on channel @" << this | 86 DVLOG(2) << "sending message @" << message << " on channel @" << this |
| 86 << " with type " << message->type() | 87 << " with type " << message->type() |
| 87 << " (" << output_queue_.size() << " in queue)"; | 88 << " (" << output_queue_.size() << " in queue)"; |
| 88 | 89 |
| 89 if (!prelim_queue_.empty()) { | 90 if (!prelim_queue_.empty()) { |
| (...skipping 29 matching lines...) Expand all Loading... |
| 119 #ifdef IPC_MESSAGE_LOG_ENABLED | 120 #ifdef IPC_MESSAGE_LOG_ENABLED |
| 120 Logging::GetInstance()->OnSendMessage(message, ""); | 121 Logging::GetInstance()->OnSendMessage(message, ""); |
| 121 #endif | 122 #endif |
| 122 | 123 |
| 123 TRACE_EVENT_WITH_FLOW0(TRACE_DISABLED_BY_DEFAULT("ipc.flow"), | 124 TRACE_EVENT_WITH_FLOW0(TRACE_DISABLED_BY_DEFAULT("ipc.flow"), |
| 124 "ChannelWin::ProcessMessageForDelivery", | 125 "ChannelWin::ProcessMessageForDelivery", |
| 125 message->flags(), | 126 message->flags(), |
| 126 TRACE_EVENT_FLAG_FLOW_OUT); | 127 TRACE_EVENT_FLAG_FLOW_OUT); |
| 127 | 128 |
| 128 // |output_queue_| takes ownership of |message|. | 129 // |output_queue_| takes ownership of |message|. |
| 129 output_queue_.push(message); | 130 OutputElement* element = new OutputElement(message); |
| 131 output_queue_.push(element); |
| 132 |
| 133 #if USE_ATTACHMENT_BROKER |
| 134 if (message->HasBrokerableAttachments()) { |
| 135 // |output_queue_| takes ownership of |ids.buffer|. |
| 136 Message::SerializedAttachmentIds ids = |
| 137 message->SerializedIdsOfBrokerableAttachments(); |
| 138 OutputElement* new_element = new OutputElement(ids.buffer, ids.size); |
| 139 output_queue_.push(new_element); |
| 140 } |
| 141 #endif |
| 142 |
| 130 // ensure waiting to write | 143 // ensure waiting to write |
| 131 if (!waiting_connect_) { | 144 if (!waiting_connect_) { |
| 132 if (!output_state_.is_pending) { | 145 if (!output_state_.is_pending) { |
| 133 if (!ProcessOutgoingMessages(NULL, 0)) | 146 if (!ProcessOutgoingMessages(NULL, 0)) |
| 134 return false; | 147 return false; |
| 135 } | 148 } |
| 136 } | 149 } |
| 137 | 150 |
| 138 return true; | 151 return true; |
| 139 } | 152 } |
| (...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 356 | 369 |
| 357 // Don't send the secret to the untrusted process, and don't send a secret | 370 // Don't send the secret to the untrusted process, and don't send a secret |
| 358 // if the value is zero (for IPC backwards compatability). | 371 // if the value is zero (for IPC backwards compatability). |
| 359 int32 secret = validate_client_ ? 0 : client_secret_; | 372 int32 secret = validate_client_ ? 0 : client_secret_; |
| 360 if (!m->WriteInt(GetCurrentProcessId()) || | 373 if (!m->WriteInt(GetCurrentProcessId()) || |
| 361 (secret && !m->WriteUInt32(secret))) { | 374 (secret && !m->WriteUInt32(secret))) { |
| 362 pipe_.Close(); | 375 pipe_.Close(); |
| 363 return false; | 376 return false; |
| 364 } | 377 } |
| 365 | 378 |
| 366 output_queue_.push(m.release()); | 379 OutputElement* element = new OutputElement(m.release()); |
| 380 output_queue_.push(element); |
| 367 return true; | 381 return true; |
| 368 } | 382 } |
| 369 | 383 |
| 370 bool ChannelWin::Connect() { | 384 bool ChannelWin::Connect() { |
| 371 DLOG_IF(WARNING, thread_check_.get()) << "Connect called more than once"; | 385 DLOG_IF(WARNING, thread_check_.get()) << "Connect called more than once"; |
| 372 | 386 |
| 373 if (!thread_check_.get()) | 387 if (!thread_check_.get()) |
| 374 thread_check_.reset(new base::ThreadChecker()); | 388 thread_check_.reset(new base::ThreadChecker()); |
| 375 | 389 |
| 376 if (!pipe_.IsValid()) | 390 if (!pipe_.IsValid()) |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 446 if (output_state_.is_pending) { | 460 if (output_state_.is_pending) { |
| 447 DCHECK(context); | 461 DCHECK(context); |
| 448 output_state_.is_pending = false; | 462 output_state_.is_pending = false; |
| 449 if (!context || bytes_written == 0) { | 463 if (!context || bytes_written == 0) { |
| 450 DWORD err = GetLastError(); | 464 DWORD err = GetLastError(); |
| 451 LOG(ERROR) << "pipe error: " << err; | 465 LOG(ERROR) << "pipe error: " << err; |
| 452 return false; | 466 return false; |
| 453 } | 467 } |
| 454 // Message was sent. | 468 // Message was sent. |
| 455 CHECK(!output_queue_.empty()); | 469 CHECK(!output_queue_.empty()); |
| 456 Message* m = output_queue_.front(); | 470 OutputElement* element = output_queue_.front(); |
| 457 output_queue_.pop(); | 471 output_queue_.pop(); |
| 458 delete m; | 472 delete element; |
| 459 } | 473 } |
| 460 | 474 |
| 461 if (output_queue_.empty()) | 475 if (output_queue_.empty()) |
| 462 return true; | 476 return true; |
| 463 | 477 |
| 464 if (!pipe_.IsValid()) | 478 if (!pipe_.IsValid()) |
| 465 return false; | 479 return false; |
| 466 | 480 |
| 467 // Write to pipe... | 481 // Write to pipe... |
| 468 Message* m = output_queue_.front(); | 482 OutputElement* element = output_queue_.front(); |
| 469 DCHECK(m->size() <= INT_MAX); | 483 DCHECK(element->size() <= INT_MAX); |
| 470 BOOL ok = WriteFile(pipe_.Get(), | 484 BOOL ok = WriteFile(pipe_.Get(), element->data(), |
| 471 m->data(), | 485 static_cast<uint32>(element->size()), NULL, |
| 472 static_cast<uint32>(m->size()), | |
| 473 NULL, | |
| 474 &output_state_.context.overlapped); | 486 &output_state_.context.overlapped); |
| 475 if (!ok) { | 487 if (!ok) { |
| 476 DWORD write_error = GetLastError(); | 488 DWORD write_error = GetLastError(); |
| 477 if (write_error == ERROR_IO_PENDING) { | 489 if (write_error == ERROR_IO_PENDING) { |
| 478 output_state_.is_pending = true; | 490 output_state_.is_pending = true; |
| 479 | 491 |
| 480 DVLOG(2) << "sent pending message @" << m << " on channel @" << this | 492 const Message* m = element->get_message(); |
| 481 << " with type " << m->type(); | 493 if (m) { |
| 494 DVLOG(2) << "sent pending message @" << m << " on channel @" << this |
| 495 << " with type " << m->type(); |
| 496 } |
| 482 | 497 |
| 483 return true; | 498 return true; |
| 484 } | 499 } |
| 485 LOG(ERROR) << "pipe error: " << write_error; | 500 LOG(ERROR) << "pipe error: " << write_error; |
| 486 return false; | 501 return false; |
| 487 } | 502 } |
| 488 | 503 |
| 489 DVLOG(2) << "sent message @" << m << " on channel @" << this | 504 const Message* m = element->get_message(); |
| 490 << " with type " << m->type(); | 505 if (m) { |
| 506 DVLOG(2) << "sent message @" << m << " on channel @" << this |
| 507 << " with type " << m->type(); |
| 508 } |
| 491 | 509 |
| 492 output_state_.is_pending = true; | 510 output_state_.is_pending = true; |
| 493 return true; | 511 return true; |
| 494 } | 512 } |
| 495 | 513 |
| 496 void ChannelWin::OnIOCompleted( | 514 void ChannelWin::OnIOCompleted( |
| 497 base::MessageLoopForIO::IOContext* context, | 515 base::MessageLoopForIO::IOContext* context, |
| 498 DWORD bytes_transfered, | 516 DWORD bytes_transfered, |
| 499 DWORD error) { | 517 DWORD error) { |
| 500 bool ok = true; | 518 bool ok = true; |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 575 int secret; | 593 int secret; |
| 576 do { // Guarantee we get a non-zero value. | 594 do { // Guarantee we get a non-zero value. |
| 577 secret = base::RandInt(0, std::numeric_limits<int>::max()); | 595 secret = base::RandInt(0, std::numeric_limits<int>::max()); |
| 578 } while (secret == 0); | 596 } while (secret == 0); |
| 579 | 597 |
| 580 id.append(GenerateUniqueRandomChannelID()); | 598 id.append(GenerateUniqueRandomChannelID()); |
| 581 return id.append(base::StringPrintf("\\%d", secret)); | 599 return id.append(base::StringPrintf("\\%d", secret)); |
| 582 } | 600 } |
| 583 | 601 |
| 584 } // namespace IPC | 602 } // namespace IPC |
| OLD | NEW |