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