mojo_data_pump.cc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // Copyright 2018 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "components/cast_channel/mojo_data_pump.h"
  5. #include <utility>
  6. #include "base/bind.h"
  7. #include "base/memory/ptr_util.h"
  8. #include "net/base/completion_once_callback.h"
  9. #include "net/base/io_buffer.h"
  10. #include "net/base/net_errors.h"
  11. namespace cast_channel {
  12. MojoDataPump::MojoDataPump(mojo::ScopedDataPipeConsumerHandle receive_stream,
  13. mojo::ScopedDataPipeProducerHandle send_stream)
  14. : receive_stream_(std::move(receive_stream)),
  15. receive_stream_watcher_(FROM_HERE,
  16. mojo::SimpleWatcher::ArmingPolicy::MANUAL),
  17. send_stream_(std::move(send_stream)),
  18. send_stream_watcher_(FROM_HERE,
  19. mojo::SimpleWatcher::ArmingPolicy::MANUAL) {
  20. DCHECK(receive_stream_.is_valid());
  21. DCHECK(send_stream_.is_valid());
  22. receive_stream_watcher_.Watch(
  23. receive_stream_.get(),
  24. MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED,
  25. MOJO_TRIGGER_CONDITION_SIGNALS_SATISFIED,
  26. base::BindRepeating(&MojoDataPump::ReceiveMore, base::Unretained(this)));
  27. send_stream_watcher_.Watch(
  28. send_stream_.get(),
  29. MOJO_HANDLE_SIGNAL_WRITABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED,
  30. MOJO_TRIGGER_CONDITION_SIGNALS_SATISFIED,
  31. base::BindRepeating(&MojoDataPump::SendMore, base::Unretained(this)));
  32. }
  33. MojoDataPump::~MojoDataPump() {}
  34. void MojoDataPump::Read(net::IOBuffer* io_buffer,
  35. int count,
  36. net::CompletionOnceCallback callback) {
  37. DCHECK(callback);
  38. DCHECK(!read_callback_);
  39. if (count <= 0) {
  40. std::move(callback).Run(net::ERR_INVALID_ARGUMENT);
  41. return;
  42. }
  43. pending_read_buffer_ = io_buffer;
  44. read_size_ = count;
  45. read_callback_ = std::move(callback);
  46. receive_stream_watcher_.ArmOrNotify();
  47. }
  48. void MojoDataPump::Write(net::IOBuffer* io_buffer,
  49. int io_buffer_size,
  50. net::CompletionOnceCallback callback) {
  51. DCHECK(callback);
  52. DCHECK(!write_callback_);
  53. write_callback_ = std::move(callback);
  54. pending_write_buffer_ = io_buffer;
  55. pending_write_buffer_size_ = io_buffer_size;
  56. send_stream_watcher_.ArmOrNotify();
  57. }
  58. void MojoDataPump::ReceiveMore(MojoResult result,
  59. const mojo::HandleSignalsState& state) {
  60. DCHECK(read_callback_);
  61. DCHECK_NE(0u, read_size_);
  62. uint32_t num_bytes = read_size_;
  63. if (result == MOJO_RESULT_OK) {
  64. result = receive_stream_->ReadData(pending_read_buffer_->data(), &num_bytes,
  65. MOJO_READ_DATA_FLAG_NONE);
  66. }
  67. if (result == MOJO_RESULT_SHOULD_WAIT) {
  68. receive_stream_watcher_.ArmOrNotify();
  69. return;
  70. }
  71. read_size_ = 0;
  72. if (result != MOJO_RESULT_OK) {
  73. std::move(read_callback_).Run(net::ERR_FAILED);
  74. return;
  75. }
  76. std::move(read_callback_).Run(num_bytes);
  77. }
  78. void MojoDataPump::SendMore(MojoResult result,
  79. const mojo::HandleSignalsState& state) {
  80. DCHECK(write_callback_);
  81. uint32_t num_bytes = pending_write_buffer_size_;
  82. if (result == MOJO_RESULT_OK) {
  83. result = send_stream_->WriteData(pending_write_buffer_->data(), &num_bytes,
  84. MOJO_WRITE_DATA_FLAG_NONE);
  85. }
  86. if (result == MOJO_RESULT_SHOULD_WAIT) {
  87. send_stream_watcher_.ArmOrNotify();
  88. return;
  89. }
  90. pending_write_buffer_ = nullptr;
  91. pending_write_buffer_size_ = 0;
  92. if (result != MOJO_RESULT_OK) {
  93. std::move(write_callback_).Run(net::ERR_FAILED);
  94. return;
  95. }
  96. std::move(write_callback_).Run(num_bytes);
  97. }
  98. } // namespace cast_channel