OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "mojo/edk/system/raw_channel.h" | 5 #include "mojo/edk/system/raw_channel.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 353 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
364 | 364 |
365 RawChannel::IOResult RawChannelWin::Read(size_t* bytes_read) { | 365 RawChannel::IOResult RawChannelWin::Read(size_t* bytes_read) { |
366 DCHECK_EQ(base::MessageLoop::current(), message_loop_for_io()); | 366 DCHECK_EQ(base::MessageLoop::current(), message_loop_for_io()); |
367 DCHECK(io_handler_); | 367 DCHECK(io_handler_); |
368 DCHECK(!io_handler_->pending_read()); | 368 DCHECK(!io_handler_->pending_read()); |
369 | 369 |
370 char* buffer = nullptr; | 370 char* buffer = nullptr; |
371 size_t bytes_to_read = 0; | 371 size_t bytes_to_read = 0; |
372 read_buffer()->GetBuffer(&buffer, &bytes_to_read); | 372 read_buffer()->GetBuffer(&buffer, &bytes_to_read); |
373 | 373 |
374 BOOL result = ReadFile(io_handler_->handle(), | 374 BOOL result = |
375 buffer, | 375 ReadFile(io_handler_->handle(), buffer, static_cast<DWORD>(bytes_to_read), |
376 static_cast<DWORD>(bytes_to_read), | 376 nullptr, &io_handler_->read_context()->overlapped); |
377 nullptr, | |
378 &io_handler_->read_context()->overlapped); | |
379 if (!result) { | 377 if (!result) { |
380 DWORD error = GetLastError(); | 378 DWORD error = GetLastError(); |
381 if (error == ERROR_BROKEN_PIPE) | 379 if (error == ERROR_BROKEN_PIPE) |
382 return IO_FAILED_SHUTDOWN; | 380 return IO_FAILED_SHUTDOWN; |
383 if (error != ERROR_IO_PENDING) { | 381 if (error != ERROR_IO_PENDING) { |
384 LOG(WARNING) << "ReadFile: " << logging::SystemErrorCodeToString(error); | 382 LOG(WARNING) << "ReadFile: " << logging::SystemErrorCodeToString(error); |
385 return IO_FAILED_UNKNOWN; | 383 return IO_FAILED_UNKNOWN; |
386 } | 384 } |
387 } | 385 } |
388 | 386 |
389 if (result && skip_completion_port_on_success_) { | 387 if (result && skip_completion_port_on_success_) { |
390 DWORD bytes_read_dword = 0; | 388 DWORD bytes_read_dword = 0; |
391 BOOL get_size_result = | 389 BOOL get_size_result = GetOverlappedResult( |
392 GetOverlappedResult(io_handler_->handle(), | 390 io_handler_->handle(), &io_handler_->read_context()->overlapped, |
393 &io_handler_->read_context()->overlapped, | 391 &bytes_read_dword, FALSE); |
394 &bytes_read_dword, | |
395 FALSE); | |
396 DPCHECK(get_size_result); | 392 DPCHECK(get_size_result); |
397 *bytes_read = bytes_read_dword; | 393 *bytes_read = bytes_read_dword; |
398 return IO_SUCCEEDED; | 394 return IO_SUCCEEDED; |
399 } | 395 } |
400 | 396 |
401 // If the read is pending or the read has succeeded but we don't skip | 397 // If the read is pending or the read has succeeded but we don't skip |
402 // completion port on success, instruct |io_handler_| to wait for the | 398 // completion port on success, instruct |io_handler_| to wait for the |
403 // completion packet. | 399 // completion packet. |
404 // | 400 // |
405 // TODO(yzshen): It seems there isn't document saying that all error cases | 401 // TODO(yzshen): It seems there isn't document saying that all error cases |
(...skipping 13 matching lines...) Expand all Loading... |
419 size_t bytes_read = 0; | 415 size_t bytes_read = 0; |
420 IOResult io_result = Read(&bytes_read); | 416 IOResult io_result = Read(&bytes_read); |
421 if (io_result == IO_SUCCEEDED) { | 417 if (io_result == IO_SUCCEEDED) { |
422 DCHECK(skip_completion_port_on_success_); | 418 DCHECK(skip_completion_port_on_success_); |
423 | 419 |
424 // We have finished reading successfully. Queue a notification manually. | 420 // We have finished reading successfully. Queue a notification manually. |
425 io_handler_->OnPendingReadStarted(); | 421 io_handler_->OnPendingReadStarted(); |
426 // |io_handler_| won't go away before the task is run, so it is safe to use | 422 // |io_handler_| won't go away before the task is run, so it is safe to use |
427 // |base::Unretained()|. | 423 // |base::Unretained()|. |
428 message_loop_for_io()->PostTask( | 424 message_loop_for_io()->PostTask( |
429 FROM_HERE, | 425 FROM_HERE, base::Bind(&RawChannelIOHandler::OnIOCompleted, |
430 base::Bind(&RawChannelIOHandler::OnIOCompleted, | 426 base::Unretained(io_handler_), |
431 base::Unretained(io_handler_), | 427 base::Unretained(io_handler_->read_context()), |
432 base::Unretained(io_handler_->read_context()), | 428 static_cast<DWORD>(bytes_read), ERROR_SUCCESS)); |
433 static_cast<DWORD>(bytes_read), | |
434 ERROR_SUCCESS)); | |
435 return IO_PENDING; | 429 return IO_PENDING; |
436 } | 430 } |
437 | 431 |
438 return io_result; | 432 return io_result; |
439 } | 433 } |
440 | 434 |
441 embedder::ScopedPlatformHandleVectorPtr RawChannelWin::GetReadPlatformHandles( | 435 embedder::ScopedPlatformHandleVectorPtr RawChannelWin::GetReadPlatformHandles( |
442 size_t num_platform_handles, | 436 size_t num_platform_handles, |
443 const void* platform_handle_table) { | 437 const void* platform_handle_table) { |
444 // TODO(vtl): Implement. | 438 // TODO(vtl): Implement. |
(...skipping 13 matching lines...) Expand all Loading... |
458 // TODO(vtl): Implement. | 452 // TODO(vtl): Implement. |
459 NOTIMPLEMENTED(); | 453 NOTIMPLEMENTED(); |
460 } | 454 } |
461 | 455 |
462 std::vector<WriteBuffer::Buffer> buffers; | 456 std::vector<WriteBuffer::Buffer> buffers; |
463 write_buffer_no_lock()->GetBuffers(&buffers); | 457 write_buffer_no_lock()->GetBuffers(&buffers); |
464 DCHECK(!buffers.empty()); | 458 DCHECK(!buffers.empty()); |
465 | 459 |
466 // TODO(yzshen): Handle multi-segment writes more efficiently. | 460 // TODO(yzshen): Handle multi-segment writes more efficiently. |
467 DWORD bytes_written_dword = 0; | 461 DWORD bytes_written_dword = 0; |
468 BOOL result = WriteFile(io_handler_->handle(), | 462 BOOL result = |
469 buffers[0].addr, | 463 WriteFile(io_handler_->handle(), buffers[0].addr, |
470 static_cast<DWORD>(buffers[0].size), | 464 static_cast<DWORD>(buffers[0].size), &bytes_written_dword, |
471 &bytes_written_dword, | 465 &io_handler_->write_context_no_lock()->overlapped); |
472 &io_handler_->write_context_no_lock()->overlapped); | |
473 if (!result) { | 466 if (!result) { |
474 DWORD error = GetLastError(); | 467 DWORD error = GetLastError(); |
475 if (error == ERROR_BROKEN_PIPE) | 468 if (error == ERROR_BROKEN_PIPE) |
476 return IO_FAILED_SHUTDOWN; | 469 return IO_FAILED_SHUTDOWN; |
477 if (error != ERROR_IO_PENDING) { | 470 if (error != ERROR_IO_PENDING) { |
478 LOG(WARNING) << "WriteFile: " << logging::SystemErrorCodeToString(error); | 471 LOG(WARNING) << "WriteFile: " << logging::SystemErrorCodeToString(error); |
479 return IO_FAILED_UNKNOWN; | 472 return IO_FAILED_UNKNOWN; |
480 } | 473 } |
481 } | 474 } |
482 | 475 |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
514 | 507 |
515 // We have finished writing successfully. Queue a notification manually. | 508 // We have finished writing successfully. Queue a notification manually. |
516 io_handler_->OnPendingWriteStartedNoLock(); | 509 io_handler_->OnPendingWriteStartedNoLock(); |
517 // |io_handler_| won't go away before that task is run, so it is safe to use | 510 // |io_handler_| won't go away before that task is run, so it is safe to use |
518 // |base::Unretained()|. | 511 // |base::Unretained()|. |
519 message_loop_for_io()->PostTask( | 512 message_loop_for_io()->PostTask( |
520 FROM_HERE, | 513 FROM_HERE, |
521 base::Bind(&RawChannelIOHandler::OnIOCompleted, | 514 base::Bind(&RawChannelIOHandler::OnIOCompleted, |
522 base::Unretained(io_handler_), | 515 base::Unretained(io_handler_), |
523 base::Unretained(io_handler_->write_context_no_lock()), | 516 base::Unretained(io_handler_->write_context_no_lock()), |
524 static_cast<DWORD>(bytes_written), | 517 static_cast<DWORD>(bytes_written), ERROR_SUCCESS)); |
525 ERROR_SUCCESS)); | |
526 return IO_PENDING; | 518 return IO_PENDING; |
527 } | 519 } |
528 | 520 |
529 return io_result; | 521 return io_result; |
530 } | 522 } |
531 | 523 |
532 bool RawChannelWin::OnInit() { | 524 bool RawChannelWin::OnInit() { |
533 DCHECK_EQ(base::MessageLoop::current(), message_loop_for_io()); | 525 DCHECK_EQ(base::MessageLoop::current(), message_loop_for_io()); |
534 | 526 |
535 DCHECK(handle_.is_valid()); | 527 DCHECK(handle_.is_valid()); |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
576 | 568 |
577 // Static factory method declared in raw_channel.h. | 569 // Static factory method declared in raw_channel.h. |
578 // static | 570 // static |
579 scoped_ptr<RawChannel> RawChannel::Create( | 571 scoped_ptr<RawChannel> RawChannel::Create( |
580 embedder::ScopedPlatformHandle handle) { | 572 embedder::ScopedPlatformHandle handle) { |
581 return make_scoped_ptr(new RawChannelWin(handle.Pass())); | 573 return make_scoped_ptr(new RawChannelWin(handle.Pass())); |
582 } | 574 } |
583 | 575 |
584 } // namespace system | 576 } // namespace system |
585 } // namespace mojo | 577 } // namespace mojo |
OLD | NEW |