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 serial::DataSink, | 21 public mojo::InterfaceImpl<serial::DataSink> { |
22 public mojo::ErrorHandler { | |
23 public: | 22 public: |
24 typedef base::Callback<void(scoped_ptr<ReadOnlyBuffer>)> ReadyCallback; | 23 typedef base::Callback<void(scoped_ptr<ReadOnlyBuffer>)> ReadyCallback; |
25 typedef base::Callback<void(int32_t error)> CancelCallback; | 24 typedef base::Callback<void(int32_t error)> CancelCallback; |
26 typedef base::Callback<void()> ErrorCallback; | 25 typedef base::Callback<void()> ErrorCallback; |
27 | 26 |
28 // Constructs a DataSinkReceiver. Whenever the pipe is ready for reading, the | 27 // Constructs a DataSinkReceiver. Whenever the pipe is ready for reading, the |
29 // |ready_callback| will be called with the ReadOnlyBuffer to read. | 28 // |ready_callback| will be called with the ReadOnlyBuffer to read. |
30 // |ready_callback| will not be called again until the previous ReadOnlyBuffer | 29 // |ready_callback| will not be called again until the previous ReadOnlyBuffer |
31 // is destroyed. If a connection error occurs, |error_callback| will be called | 30 // is destroyed. If a connection error occurs, |error_callback| will be called |
32 // and the DataSinkReceiver will act as if ShutDown() had been called. If | 31 // and the DataSinkReceiver will act as if ShutDown() had been called. If |
33 // |cancel_callback| is valid, it will be called when the DataSinkClient | 32 // |cancel_callback| is valid, it will be called when the DataSinkClient |
34 // requests cancellation of the in-progress read. | 33 // requests cancellation of the in-progress read. |
35 DataSinkReceiver(mojo::InterfaceRequest<serial::DataSink> request, | 34 DataSinkReceiver(const ReadyCallback& ready_callback, |
36 mojo::InterfacePtr<serial::DataSinkClient> client, | |
37 const ReadyCallback& ready_callback, | |
38 const CancelCallback& cancel_callback, | 35 const CancelCallback& cancel_callback, |
39 const ErrorCallback& error_callback); | 36 const ErrorCallback& error_callback); |
40 | 37 |
41 // Shuts down this DataSinkReceiver. After shut down, |ready_callback|, | 38 // Shuts down this DataSinkReceiver. After shut down, |ready_callback|, |
42 // |cancel_callback| and |error_callback| will never be called. | 39 // |cancel_callback| and |error_callback| will never be called. |
43 void ShutDown(); | 40 void ShutDown(); |
44 | 41 |
45 private: | 42 private: |
46 class Buffer; | 43 class Buffer; |
47 class DataFrame; | 44 class DataFrame; |
(...skipping 23 matching lines...) Expand all Loading... |
71 // Sends an ReportBytesSentAndError message to the client. | 68 // Sends an ReportBytesSentAndError message to the client. |
72 void ReportBytesSentAndError(uint32_t bytes_read, int32_t error); | 69 void ReportBytesSentAndError(uint32_t bytes_read, int32_t error); |
73 | 70 |
74 // Invoked in response to an ReportBytesSentAndError call to the client at | 71 // Invoked in response to an ReportBytesSentAndError call to the client at |
75 // the point in the data stream to flush. | 72 // the point in the data stream to flush. |
76 void DoFlush(); | 73 void DoFlush(); |
77 | 74 |
78 // Reports a fatal error to the client and shuts down. | 75 // Reports a fatal error to the client and shuts down. |
79 void DispatchFatalError(); | 76 void DispatchFatalError(); |
80 | 77 |
81 mojo::Binding<serial::DataSink> binding_; | |
82 mojo::InterfacePtr<serial::DataSinkClient> client_; | |
83 | |
84 // The callback to call when there is data ready to read. | 78 // The callback to call when there is data ready to read. |
85 const ReadyCallback ready_callback_; | 79 const ReadyCallback ready_callback_; |
86 | 80 |
87 // The callback to call when the client has requested cancellation. | 81 // The callback to call when the client has requested cancellation. |
88 const CancelCallback cancel_callback_; | 82 const CancelCallback cancel_callback_; |
89 | 83 |
90 // The callback to call if a fatal error occurs. | 84 // The callback to call if a fatal error occurs. |
91 const ErrorCallback error_callback_; | 85 const ErrorCallback error_callback_; |
92 | 86 |
93 // Whether we are waiting for a flush. | 87 // Whether we are waiting for a flush. |
(...skipping 17 matching lines...) Expand all Loading... |
111 bool shut_down_; | 105 bool shut_down_; |
112 | 106 |
113 base::WeakPtrFactory<DataSinkReceiver> weak_factory_; | 107 base::WeakPtrFactory<DataSinkReceiver> weak_factory_; |
114 | 108 |
115 DISALLOW_COPY_AND_ASSIGN(DataSinkReceiver); | 109 DISALLOW_COPY_AND_ASSIGN(DataSinkReceiver); |
116 }; | 110 }; |
117 | 111 |
118 } // namespace device | 112 } // namespace device |
119 | 113 |
120 #endif // DEVICE_SERIAL_DATA_SINK_RECEIVER_H_ | 114 #endif // DEVICE_SERIAL_DATA_SINK_RECEIVER_H_ |
OLD | NEW |