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.
5 #ifndef COMPONENTS_CAST_CHANNEL_MOJO_DATA_PUMP_H_
6 #define COMPONENTS_CAST_CHANNEL_MOJO_DATA_PUMP_H_
8 #include "base/macros.h"
9 #include "base/memory/ref_counted.h"
10 #include "components/cast_channel/cast_transport.h"
11 #include "mojo/public/cpp/system/data_pipe.h"
12 #include "mojo/public/cpp/system/simple_watcher.h"
13 #include "net/base/completion_once_callback.h"
19 namespace cast_channel {
21 // Helper class to read from a mojo consumer handle and write to mojo producer
23 class MojoDataPump : public CastTransportImpl::Channel {
25 MojoDataPump(mojo::ScopedDataPipeConsumerHandle receive_stream,
26 mojo::ScopedDataPipeProducerHandle send_stream);
28 ~MojoDataPump() override;
30 // CastTransportImpl::Channel implementation:
31 void Read(net::IOBuffer* io_buffer,
33 net::CompletionOnceCallback callback) override;
34 void Write(net::IOBuffer* io_buffer,
36 net::CompletionOnceCallback callback) override;
38 // Returns whether a read is pending.
39 bool HasPendingRead() const { return !read_callback_.is_null(); }
41 // Returns whether a write is pending.
42 bool HasPendingWrite() const { return !write_callback_.is_null(); }
45 void OnReadComplete(int result);
47 void ReceiveMore(MojoResult result, const mojo::HandleSignalsState& state);
48 void SendMore(MojoResult result, const mojo::HandleSignalsState& state);
50 mojo::ScopedDataPipeConsumerHandle receive_stream_;
51 mojo::SimpleWatcher receive_stream_watcher_;
52 mojo::ScopedDataPipeProducerHandle send_stream_;
53 mojo::SimpleWatcher send_stream_watcher_;
55 net::CompletionOnceCallback read_callback_;
56 net::CompletionOnceCallback write_callback_;
57 scoped_refptr<net::IOBuffer> pending_read_buffer_;
58 scoped_refptr<net::IOBuffer> pending_write_buffer_;
59 int pending_write_buffer_size_ = 0;
60 uint32_t read_size_ = 0;
62 DISALLOW_COPY_AND_ASSIGN(MojoDataPump);
65 } // namespace cast_channel
67 #endif // COMPONENTS_CAST_CHANNEL_MOJO_DATA_PUMP_H_