| OLD | NEW |
| (Empty) | |
| 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. |
| 4 |
| 5 #ifndef DEVICE_SERIAL_DATA_SOURCE_SENDER_H_ |
| 6 #define DEVICE_SERIAL_DATA_SOURCE_SENDER_H_ |
| 7 |
| 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" |
| 13 |
| 14 namespace device { |
| 15 |
| 16 class AsyncWaiter; |
| 17 |
| 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> { |
| 21 public: |
| 22 typedef base::Callback<void(scoped_ptr<WritableBuffer>)> ReadyCallback; |
| 23 typedef base::Callback<void()> ErrorCallback; |
| 24 |
| 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); |
| 32 |
| 33 // Shuts down this DataSourceSender. After shut down, |ready_callback| and |
| 34 // |error_callback| will never be called. |
| 35 void ShutDown(); |
| 36 |
| 37 private: |
| 38 friend class base::RefCounted<DataSourceSender>; |
| 39 class PendingSend; |
| 40 |
| 41 virtual ~DataSourceSender(); |
| 42 |
| 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; |
| 48 |
| 49 // Starts waiting for |handle_| to be ready for writes. |
| 50 void StartWaiting(); |
| 51 |
| 52 // Invoked when |handle_| is ready for writes. |
| 53 void OnDoneWaiting(MojoResult result); |
| 54 |
| 55 // Reports a successful write of |bytes_written|. |
| 56 void Done(uint32_t bytes_written); |
| 57 |
| 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); |
| 61 |
| 62 // Finishes the two-phase data pipe write. |
| 63 void DoneInternal(uint32_t bytes_written); |
| 64 |
| 65 // Reports a fatal error to the client and shuts down. |
| 66 void DispatchFatalError(); |
| 67 |
| 68 // The data connection to the data receiver. |
| 69 mojo::ScopedDataPipeProducerHandle handle_; |
| 70 |
| 71 // The callback to call when |handle_| is ready for more data. |
| 72 ReadyCallback ready_callback_; |
| 73 |
| 74 // The callback to call if a fatal error occurs. |
| 75 ErrorCallback error_callback_; |
| 76 |
| 77 // The current pending send operation if there is one. |
| 78 scoped_ptr<PendingSend> pending_send_; |
| 79 |
| 80 // A waiter used to wait until |handle_| is writable if we are waiting. |
| 81 scoped_ptr<AsyncWaiter> waiter_; |
| 82 |
| 83 // The number of bytes sent to the data receiver. |
| 84 uint32_t bytes_sent_; |
| 85 |
| 86 // Whether we have encountered a fatal error and shut down. |
| 87 bool shut_down_; |
| 88 |
| 89 DISALLOW_COPY_AND_ASSIGN(DataSourceSender); |
| 90 }; |
| 91 |
| 92 } // namespace device |
| 93 |
| 94 #endif // DEVICE_SERIAL_DATA_SOURCE_SENDER_H_ |
| OLD | NEW |