| 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_SINK_RECEIVER_H_ | |
| 6 #define DEVICE_SERIAL_DATA_SINK_RECEIVER_H_ | |
| 7 | |
| 8 #include <stdint.h> | |
| 9 | |
| 10 #include <memory> | |
| 11 #include <queue> | |
| 12 | |
| 13 #include "base/callback.h" | |
| 14 #include "base/macros.h" | |
| 15 #include "base/memory/linked_ptr.h" | |
| 16 #include "base/memory/ref_counted.h" | |
| 17 #include "base/memory/weak_ptr.h" | |
| 18 #include "device/serial/buffer.h" | |
| 19 #include "device/serial/data_stream.mojom.h" | |
| 20 #include "mojo/public/cpp/bindings/binding.h" | |
| 21 #include "mojo/public/cpp/system/data_pipe.h" | |
| 22 | |
| 23 namespace device { | |
| 24 | |
| 25 class DataSinkReceiver : public base::RefCounted<DataSinkReceiver>, | |
| 26 public serial::DataSink { | |
| 27 public: | |
| 28 typedef base::Callback<void(std::unique_ptr<ReadOnlyBuffer>)> ReadyCallback; | |
| 29 typedef base::Callback<void(int32_t error)> CancelCallback; | |
| 30 typedef base::Callback<void()> ErrorCallback; | |
| 31 | |
| 32 // Constructs a DataSinkReceiver. Whenever the pipe is ready for reading, the | |
| 33 // |ready_callback| will be called with the ReadOnlyBuffer to read. | |
| 34 // |ready_callback| will not be called again until the previous ReadOnlyBuffer | |
| 35 // is destroyed. If a connection error occurs, |error_callback| will be called | |
| 36 // and the DataSinkReceiver will act as if ShutDown() had been called. If | |
| 37 // |cancel_callback| is valid, it will be called when the DataSink client | |
| 38 // requests cancellation of the in-progress read. | |
| 39 DataSinkReceiver(mojo::InterfaceRequest<serial::DataSink> request, | |
| 40 const ReadyCallback& ready_callback, | |
| 41 const CancelCallback& cancel_callback, | |
| 42 const ErrorCallback& error_callback); | |
| 43 | |
| 44 // Shuts down this DataSinkReceiver. After shut down, |ready_callback|, | |
| 45 // |cancel_callback| and |error_callback| will never be called. | |
| 46 void ShutDown(); | |
| 47 | |
| 48 private: | |
| 49 class Buffer; | |
| 50 class DataFrame; | |
| 51 friend class base::RefCounted<DataSinkReceiver>; | |
| 52 | |
| 53 ~DataSinkReceiver() override; | |
| 54 | |
| 55 // serial::DataSink overrides. | |
| 56 void Cancel(int32_t error) override; | |
| 57 void OnData(mojo::Array<uint8_t> data, | |
| 58 const OnDataCallback& callback) override; | |
| 59 void ClearError() override; | |
| 60 | |
| 61 void OnConnectionError(); | |
| 62 | |
| 63 // Dispatches data to |ready_callback_|. | |
| 64 void RunReadyCallback(); | |
| 65 | |
| 66 // Reports a successful read of |bytes_read|. | |
| 67 void Done(uint32_t bytes_read); | |
| 68 | |
| 69 // Reports a partially successful or unsuccessful read of |bytes_read| | |
| 70 // with an error of |error|. | |
| 71 void DoneWithError(uint32_t bytes_read, int32_t error); | |
| 72 | |
| 73 // Marks |bytes_read| bytes as being read. | |
| 74 bool DoneInternal(uint32_t bytes_read); | |
| 75 | |
| 76 // Reports an error to the client. | |
| 77 void ReportError(uint32_t bytes_read, int32_t error); | |
| 78 | |
| 79 // Reports a fatal error to the client and shuts down. | |
| 80 void DispatchFatalError(); | |
| 81 | |
| 82 mojo::Binding<serial::DataSink> binding_; | |
| 83 | |
| 84 // The callback to call when there is data ready to read. | |
| 85 const ReadyCallback ready_callback_; | |
| 86 | |
| 87 // The callback to call when the client has requested cancellation. | |
| 88 const CancelCallback cancel_callback_; | |
| 89 | |
| 90 // The callback to call if a fatal error occurs. | |
| 91 const ErrorCallback error_callback_; | |
| 92 | |
| 93 // The current error that has not been cleared by a ClearError message.. | |
| 94 int32_t current_error_; | |
| 95 | |
| 96 // The buffer passed to |ready_callback_| if one exists. This is not owned, | |
| 97 // but the Buffer will call Done or DoneWithError before being deleted. | |
| 98 Buffer* buffer_in_use_; | |
| 99 | |
| 100 // The data we have received from the client that has not been passed to | |
| 101 // |ready_callback_|. | |
| 102 std::queue<linked_ptr<DataFrame>> pending_data_buffers_; | |
| 103 | |
| 104 // Whether we have encountered a fatal error and shut down. | |
| 105 bool shut_down_; | |
| 106 | |
| 107 base::WeakPtrFactory<DataSinkReceiver> weak_factory_; | |
| 108 | |
| 109 DISALLOW_COPY_AND_ASSIGN(DataSinkReceiver); | |
| 110 }; | |
| 111 | |
| 112 } // namespace device | |
| 113 | |
| 114 #endif // DEVICE_SERIAL_DATA_SINK_RECEIVER_H_ | |
| OLD | NEW |