| 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 <stdint.h> | |
| 9 | |
| 10 #include <memory> | |
| 11 #include <vector> | |
| 12 | |
| 13 #include "base/callback.h" | |
| 14 #include "base/macros.h" | |
| 15 #include "base/memory/ref_counted.h" | |
| 16 #include "base/memory/weak_ptr.h" | |
| 17 #include "device/serial/buffer.h" | |
| 18 #include "device/serial/data_stream.mojom.h" | |
| 19 #include "mojo/public/cpp/bindings/binding.h" | |
| 20 #include "mojo/public/cpp/system/data_pipe.h" | |
| 21 | |
| 22 namespace device { | |
| 23 | |
| 24 // A DataSourceSender is an interface between a source of data and a | |
| 25 // DataSourceClient. | |
| 26 class DataSourceSender : public base::RefCounted<DataSourceSender>, | |
| 27 public serial::DataSource { | |
| 28 public: | |
| 29 typedef base::Callback<void(std::unique_ptr<WritableBuffer>)> ReadyCallback; | |
| 30 typedef base::Callback<void()> ErrorCallback; | |
| 31 | |
| 32 // Constructs a DataSourceSender. Whenever the pipe is ready for writing, the | |
| 33 // |ready_callback| will be called with the WritableBuffer to be filled. | |
| 34 // |ready_callback| will not be called again until the previous WritableBuffer | |
| 35 // is destroyed. If a connection error occurs, |error_callback| will be | |
| 36 // called and the DataSourceSender will act as if ShutDown() had been called. | |
| 37 DataSourceSender(mojo::InterfaceRequest<serial::DataSource> source, | |
| 38 mojo::InterfacePtr<serial::DataSourceClient> client, | |
| 39 const ReadyCallback& ready_callback, | |
| 40 const ErrorCallback& error_callback); | |
| 41 | |
| 42 // Shuts down this DataSourceSender. After shut down, |ready_callback| and | |
| 43 // |error_callback| will never be called. | |
| 44 void ShutDown(); | |
| 45 | |
| 46 private: | |
| 47 friend class base::RefCounted<DataSourceSender>; | |
| 48 class PendingSend; | |
| 49 | |
| 50 ~DataSourceSender() override; | |
| 51 | |
| 52 // mojo::InterfaceImpl<serial::DataSourceSender> overrides. | |
| 53 void Init(uint32_t buffer_size) override; | |
| 54 void Resume() override; | |
| 55 void ReportBytesReceived(uint32_t bytes_sent) override; | |
| 56 // mojo error handler. Calls DispatchFatalError(). | |
| 57 void OnConnectionError(); | |
| 58 | |
| 59 // Gets more data to send to the DataSourceClient. | |
| 60 void GetMoreData(); | |
| 61 | |
| 62 // Invoked to pass |data| obtained in response to |ready_callback_|. | |
| 63 void Done(const std::vector<char>& data); | |
| 64 | |
| 65 // Invoked to pass |data| and |error| obtained in response to | |
| 66 // |ready_callback_|. | |
| 67 void DoneWithError(const std::vector<char>& data, int32_t error); | |
| 68 | |
| 69 // Dispatches |data| to the client. | |
| 70 void DoneInternal(const std::vector<char>& data); | |
| 71 | |
| 72 // Reports a fatal error to the client and shuts down. | |
| 73 void DispatchFatalError(); | |
| 74 | |
| 75 mojo::Binding<serial::DataSource> binding_; | |
| 76 mojo::InterfacePtr<serial::DataSourceClient> client_; | |
| 77 | |
| 78 // The callback to call when the client is ready for more data. | |
| 79 ReadyCallback ready_callback_; | |
| 80 | |
| 81 // The callback to call if a fatal error occurs. | |
| 82 ErrorCallback error_callback_; | |
| 83 | |
| 84 // The current pending send operation if there is one. | |
| 85 std::unique_ptr<PendingSend> pending_send_; | |
| 86 | |
| 87 // The number of bytes available for buffering in the client. | |
| 88 uint32_t available_buffer_capacity_; | |
| 89 | |
| 90 // Whether sending is paused due to an error. | |
| 91 bool paused_; | |
| 92 | |
| 93 // Whether we have encountered a fatal error and shut down. | |
| 94 bool shut_down_; | |
| 95 | |
| 96 base::WeakPtrFactory<DataSourceSender> weak_factory_; | |
| 97 | |
| 98 DISALLOW_COPY_AND_ASSIGN(DataSourceSender); | |
| 99 }; | |
| 100 | |
| 101 } // namespace device | |
| 102 | |
| 103 #endif // DEVICE_SERIAL_DATA_SOURCE_SENDER_H_ | |
| OLD | NEW |