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_
8 #include "base/callback.h"
9 #include "base/memory/ref_counted.h"
10 #include "device/serial/buffer.h"
11 #include "device/serial/data_stream.mojom.h"
12 #include "mojo/public/cpp/system/data_pipe.h"
18 // A DataSourceSender is an interface between a source of data and a data pipe.
19 class DataSourceSender
: public base::RefCounted
<DataSourceSender
>,
20 public mojo::InterfaceImpl
<serial::DataSource
> {
22 typedef base::Callback
<void(scoped_ptr
<WritableBuffer
>)> ReadyCallback
;
23 typedef base::Callback
<void()> ErrorCallback
;
25 // Constructs a DataSourceSender. Whenever the pipe is ready for writing, the
26 // |ready_callback| will be called with the WritableBuffer to be filled.
27 // |ready_callback| will not be called again until the previous WritableBuffer
28 // is destroyed. If a connection error occurs, |error_callback| will be
29 // called and the DataSourceSender will act as if ShutDown() had been called.
30 DataSourceSender(const ReadyCallback
& ready_callback
,
31 const ErrorCallback
& error_callback
);
33 // Shuts down this DataSourceSender. After shut down, |ready_callback| and
34 // |error_callback| will never be called.
38 friend class base::RefCounted
<DataSourceSender
>;
41 virtual ~DataSourceSender();
43 // mojo::InterfaceImpl<serial::DataSourceSender> overrides.
44 virtual void Init(mojo::ScopedDataPipeProducerHandle handle
) OVERRIDE
;
45 virtual void Resume() OVERRIDE
;
46 // Invoked in the event of a connection error. Calls DispatchFatalError().
47 virtual void OnConnectionError() OVERRIDE
;
49 // Starts waiting for |handle_| to be ready for writes.
52 // Invoked when |handle_| is ready for writes.
53 void OnDoneWaiting(MojoResult result
);
55 // Reports a successful write of |bytes_written|.
56 void Done(uint32_t bytes_written
);
58 // Reports a partially successful or unsuccessful write of |bytes_written|
59 // with an error of |error|.
60 void DoneWithError(uint32_t bytes_written
, int32_t error
);
62 // Finishes the two-phase data pipe write.
63 void DoneInternal(uint32_t bytes_written
);
65 // Reports a fatal error to the client and shuts down.
66 void DispatchFatalError();
68 // The data connection to the data receiver.
69 mojo::ScopedDataPipeProducerHandle handle_
;
71 // The callback to call when |handle_| is ready for more data.
72 ReadyCallback ready_callback_
;
74 // The callback to call if a fatal error occurs.
75 ErrorCallback error_callback_
;
77 // The current pending send operation if there is one.
78 scoped_ptr
<PendingSend
> pending_send_
;
80 // A waiter used to wait until |handle_| is writable if we are waiting.
81 scoped_ptr
<AsyncWaiter
> waiter_
;
83 // The number of bytes sent to the data receiver.
86 // Whether we have encountered a fatal error and shut down.
89 DISALLOW_COPY_AND_ASSIGN(DataSourceSender
);
94 #endif // DEVICE_SERIAL_DATA_SOURCE_SENDER_H_