| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef DEVICE_SERIAL_DATA_SOURCE_SENDER_H_ | 5 #ifndef DEVICE_SERIAL_DATA_SOURCE_SENDER_H_ |
| 6 #define DEVICE_SERIAL_DATA_SOURCE_SENDER_H_ | 6 #define DEVICE_SERIAL_DATA_SOURCE_SENDER_H_ |
| 7 | 7 |
| 8 #include <vector> |
| 9 |
| 8 #include "base/callback.h" | 10 #include "base/callback.h" |
| 9 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
| 12 #include "base/memory/weak_ptr.h" |
| 10 #include "device/serial/buffer.h" | 13 #include "device/serial/buffer.h" |
| 11 #include "device/serial/data_stream.mojom.h" | 14 #include "device/serial/data_stream.mojom.h" |
| 12 #include "mojo/public/cpp/system/data_pipe.h" | 15 #include "mojo/public/cpp/system/data_pipe.h" |
| 13 | 16 |
| 14 namespace device { | 17 namespace device { |
| 15 | 18 |
| 16 class AsyncWaiter; | 19 // A DataSourceSender is an interface between a source of data and a |
| 17 | 20 // DataSourceClient. |
| 18 // A DataSourceSender is an interface between a source of data and a data pipe. | |
| 19 class DataSourceSender : public base::RefCounted<DataSourceSender>, | 21 class DataSourceSender : public base::RefCounted<DataSourceSender>, |
| 20 public mojo::InterfaceImpl<serial::DataSource> { | 22 public mojo::InterfaceImpl<serial::DataSource> { |
| 21 public: | 23 public: |
| 22 typedef base::Callback<void(scoped_ptr<WritableBuffer>)> ReadyCallback; | 24 typedef base::Callback<void(scoped_ptr<WritableBuffer>)> ReadyCallback; |
| 23 typedef base::Callback<void()> ErrorCallback; | 25 typedef base::Callback<void()> ErrorCallback; |
| 24 | 26 |
| 25 // Constructs a DataSourceSender. Whenever the pipe is ready for writing, the | 27 // Constructs a DataSourceSender. Whenever the pipe is ready for writing, the |
| 26 // |ready_callback| will be called with the WritableBuffer to be filled. | 28 // |ready_callback| will be called with the WritableBuffer to be filled. |
| 27 // |ready_callback| will not be called again until the previous WritableBuffer | 29 // |ready_callback| will not be called again until the previous WritableBuffer |
| 28 // is destroyed. If a connection error occurs, |error_callback| will be | 30 // is destroyed. If a connection error occurs, |error_callback| will be |
| 29 // called and the DataSourceSender will act as if ShutDown() had been called. | 31 // called and the DataSourceSender will act as if ShutDown() had been called. |
| 30 DataSourceSender(const ReadyCallback& ready_callback, | 32 DataSourceSender(const ReadyCallback& ready_callback, |
| 31 const ErrorCallback& error_callback); | 33 const ErrorCallback& error_callback); |
| 32 | 34 |
| 33 // Shuts down this DataSourceSender. After shut down, |ready_callback| and | 35 // Shuts down this DataSourceSender. After shut down, |ready_callback| and |
| 34 // |error_callback| will never be called. | 36 // |error_callback| will never be called. |
| 35 void ShutDown(); | 37 void ShutDown(); |
| 36 | 38 |
| 37 private: | 39 private: |
| 38 friend class base::RefCounted<DataSourceSender>; | 40 friend class base::RefCounted<DataSourceSender>; |
| 39 class PendingSend; | 41 class PendingSend; |
| 40 | 42 |
| 41 ~DataSourceSender() override; | 43 ~DataSourceSender() override; |
| 42 | 44 |
| 43 // mojo::InterfaceImpl<serial::DataSourceSender> overrides. | 45 // mojo::InterfaceImpl<serial::DataSourceSender> overrides. |
| 44 void Init(mojo::ScopedDataPipeProducerHandle handle) override; | 46 void Init(uint32_t buffer_size) override; |
| 45 void Resume() override; | 47 void Resume() override; |
| 48 void ReportBytesReceived(uint32_t bytes_sent) override; |
| 46 // Invoked in the event of a connection error. Calls DispatchFatalError(). | 49 // Invoked in the event of a connection error. Calls DispatchFatalError(). |
| 47 void OnConnectionError() override; | 50 void OnConnectionError() override; |
| 48 | 51 |
| 49 // Starts waiting for |handle_| to be ready for writes. | 52 // Gets more data to send to the DataSourceClient. |
| 50 void StartWaiting(); | 53 void GetMoreData(); |
| 51 | 54 |
| 52 // Invoked when |handle_| is ready for writes. | 55 // Invoked to pass |data| obtained in response to |ready_callback_|. |
| 53 void OnDoneWaiting(MojoResult result); | 56 void Done(const std::vector<char>& data); |
| 54 | 57 |
| 55 // Reports a successful write of |bytes_written|. | 58 // Invoked to pass |data| and |error| obtained in response to |
| 56 void Done(uint32_t bytes_written); | 59 // |ready_callback_|. |
| 60 void DoneWithError(const std::vector<char>& data, int32_t error); |
| 57 | 61 |
| 58 // Reports a partially successful or unsuccessful write of |bytes_written| | 62 // Dispatches |data| to the client. |
| 59 // with an error of |error|. | 63 void DoneInternal(const std::vector<char>& data); |
| 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 | 64 |
| 65 // Reports a fatal error to the client and shuts down. | 65 // Reports a fatal error to the client and shuts down. |
| 66 void DispatchFatalError(); | 66 void DispatchFatalError(); |
| 67 | 67 |
| 68 // The data connection to the data receiver. | 68 // The callback to call when the client is ready for more data. |
| 69 mojo::ScopedDataPipeProducerHandle handle_; | |
| 70 | |
| 71 // The callback to call when |handle_| is ready for more data. | |
| 72 ReadyCallback ready_callback_; | 69 ReadyCallback ready_callback_; |
| 73 | 70 |
| 74 // The callback to call if a fatal error occurs. | 71 // The callback to call if a fatal error occurs. |
| 75 ErrorCallback error_callback_; | 72 ErrorCallback error_callback_; |
| 76 | 73 |
| 77 // The current pending send operation if there is one. | 74 // The current pending send operation if there is one. |
| 78 scoped_ptr<PendingSend> pending_send_; | 75 scoped_ptr<PendingSend> pending_send_; |
| 79 | 76 |
| 80 // A waiter used to wait until |handle_| is writable if we are waiting. | 77 // The number of bytes available for buffering in the client. |
| 81 scoped_ptr<AsyncWaiter> waiter_; | 78 uint32_t available_buffer_capacity_; |
| 82 | 79 |
| 83 // The number of bytes sent to the data receiver. | 80 // Whether sending is paused due to an error. |
| 84 uint32_t bytes_sent_; | 81 bool paused_; |
| 85 | 82 |
| 86 // Whether we have encountered a fatal error and shut down. | 83 // Whether we have encountered a fatal error and shut down. |
| 87 bool shut_down_; | 84 bool shut_down_; |
| 88 | 85 |
| 86 base::WeakPtrFactory<DataSourceSender> weak_factory_; |
| 87 |
| 89 DISALLOW_COPY_AND_ASSIGN(DataSourceSender); | 88 DISALLOW_COPY_AND_ASSIGN(DataSourceSender); |
| 90 }; | 89 }; |
| 91 | 90 |
| 92 } // namespace device | 91 } // namespace device |
| 93 | 92 |
| 94 #endif // DEVICE_SERIAL_DATA_SOURCE_SENDER_H_ | 93 #endif // DEVICE_SERIAL_DATA_SOURCE_SENDER_H_ |
| OLD | NEW |