1 // Copyright 2014 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 DEVICE_SERIAL_DATA_SOURCE_SENDER_H_
6 #define DEVICE_SERIAL_DATA_SOURCE_SENDER_H_
10 #include "base/callback.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/weak_ptr.h"
13 #include "device/serial/buffer.h"
14 #include "device/serial/data_stream.mojom.h"
15 #include "third_party/mojo/src/mojo/public/cpp/bindings/binding.h"
16 #include "third_party/mojo/src/mojo/public/cpp/system/data_pipe.h"
20 // A DataSourceSender is an interface between a source of data and a
22 class DataSourceSender
: public base::RefCounted
<DataSourceSender
>,
23 public serial::DataSource
{
25 typedef base::Callback
<void(scoped_ptr
<WritableBuffer
>)> ReadyCallback
;
26 typedef base::Callback
<void()> ErrorCallback
;
28 // Constructs a DataSourceSender. Whenever the pipe is ready for writing, the
29 // |ready_callback| will be called with the WritableBuffer to be filled.
30 // |ready_callback| will not be called again until the previous WritableBuffer
31 // is destroyed. If a connection error occurs, |error_callback| will be
32 // called and the DataSourceSender will act as if ShutDown() had been called.
33 DataSourceSender(mojo::InterfaceRequest
<serial::DataSource
> source
,
34 mojo::InterfacePtr
<serial::DataSourceClient
> client
,
35 const ReadyCallback
& ready_callback
,
36 const ErrorCallback
& error_callback
);
38 // Shuts down this DataSourceSender. After shut down, |ready_callback| and
39 // |error_callback| will never be called.
43 friend class base::RefCounted
<DataSourceSender
>;
46 ~DataSourceSender() override
;
48 // mojo::InterfaceImpl<serial::DataSourceSender> overrides.
49 void Init(uint32_t buffer_size
) override
;
50 void Resume() override
;
51 void ReportBytesReceived(uint32_t bytes_sent
) override
;
52 // mojo error handler. Calls DispatchFatalError().
53 void OnConnectionError();
55 // Gets more data to send to the DataSourceClient.
58 // Invoked to pass |data| obtained in response to |ready_callback_|.
59 void Done(const std::vector
<char>& data
);
61 // Invoked to pass |data| and |error| obtained in response to
63 void DoneWithError(const std::vector
<char>& data
, int32_t error
);
65 // Dispatches |data| to the client.
66 void DoneInternal(const std::vector
<char>& data
);
68 // Reports a fatal error to the client and shuts down.
69 void DispatchFatalError();
71 mojo::Binding
<serial::DataSource
> binding_
;
72 mojo::InterfacePtr
<serial::DataSourceClient
> client_
;
74 // The callback to call when the client is ready for more data.
75 ReadyCallback ready_callback_
;
77 // The callback to call if a fatal error occurs.
78 ErrorCallback error_callback_
;
80 // The current pending send operation if there is one.
81 scoped_ptr
<PendingSend
> pending_send_
;
83 // The number of bytes available for buffering in the client.
84 uint32_t available_buffer_capacity_
;
86 // Whether sending is paused due to an error.
89 // Whether we have encountered a fatal error and shut down.
92 base::WeakPtrFactory
<DataSourceSender
> weak_factory_
;
94 DISALLOW_COPY_AND_ASSIGN(DataSourceSender
);
99 #endif // DEVICE_SERIAL_DATA_SOURCE_SENDER_H_