mojo_data_pump.h 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. #ifndef COMPONENTS_CAST_CHANNEL_MOJO_DATA_PUMP_H_
  5. #define COMPONENTS_CAST_CHANNEL_MOJO_DATA_PUMP_H_
  6. #include "base/memory/ref_counted.h"
  7. #include "components/cast_channel/cast_transport.h"
  8. #include "mojo/public/cpp/system/data_pipe.h"
  9. #include "mojo/public/cpp/system/simple_watcher.h"
  10. #include "net/base/completion_once_callback.h"
  11. namespace net {
  12. class IOBuffer;
  13. } // namespace net
  14. namespace cast_channel {
  15. // Helper class to read from a mojo consumer handle and write to mojo producer
  16. // handle.
  17. class MojoDataPump : public CastTransportImpl::Channel {
  18. public:
  19. MojoDataPump(mojo::ScopedDataPipeConsumerHandle receive_stream,
  20. mojo::ScopedDataPipeProducerHandle send_stream);
  21. MojoDataPump(const MojoDataPump&) = delete;
  22. MojoDataPump& operator=(const MojoDataPump&) = delete;
  23. ~MojoDataPump() override;
  24. // CastTransportImpl::Channel implementation:
  25. void Read(net::IOBuffer* io_buffer,
  26. int count,
  27. net::CompletionOnceCallback callback) override;
  28. void Write(net::IOBuffer* io_buffer,
  29. int io_buffer_size,
  30. net::CompletionOnceCallback callback) override;
  31. // Returns whether a read is pending.
  32. bool HasPendingRead() const { return !read_callback_.is_null(); }
  33. // Returns whether a write is pending.
  34. bool HasPendingWrite() const { return !write_callback_.is_null(); }
  35. private:
  36. void OnReadComplete(int result);
  37. void StartWatching();
  38. void ReceiveMore(MojoResult result, const mojo::HandleSignalsState& state);
  39. void SendMore(MojoResult result, const mojo::HandleSignalsState& state);
  40. mojo::ScopedDataPipeConsumerHandle receive_stream_;
  41. mojo::SimpleWatcher receive_stream_watcher_;
  42. mojo::ScopedDataPipeProducerHandle send_stream_;
  43. mojo::SimpleWatcher send_stream_watcher_;
  44. net::CompletionOnceCallback read_callback_;
  45. net::CompletionOnceCallback write_callback_;
  46. scoped_refptr<net::IOBuffer> pending_read_buffer_;
  47. scoped_refptr<net::IOBuffer> pending_write_buffer_;
  48. int pending_write_buffer_size_ = 0;
  49. uint32_t read_size_ = 0;
  50. };
  51. } // namespace cast_channel
  52. #endif // COMPONENTS_CAST_CHANNEL_MOJO_DATA_PUMP_H_