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 "mojo/public/cpp/system/data_pipe.h" | 16 #include "mojo/public/cpp/system/data_pipe.h" |
17 | 17 |
18 namespace device { | 18 namespace device { |
19 | 19 |
20 class AsyncWaiter; | |
21 | |
22 class DataSinkReceiver : public base::RefCounted<DataSinkReceiver>, | 20 class DataSinkReceiver : public base::RefCounted<DataSinkReceiver>, |
23 public mojo::InterfaceImpl<serial::DataSink> { | 21 public mojo::InterfaceImpl<serial::DataSink> { |
24 public: | 22 public: |
25 typedef base::Callback<void(scoped_ptr<ReadOnlyBuffer>)> ReadyCallback; | 23 typedef base::Callback<void(scoped_ptr<ReadOnlyBuffer>)> ReadyCallback; |
26 typedef base::Callback<void(int32_t error)> CancelCallback; | 24 typedef base::Callback<void(int32_t error)> CancelCallback; |
27 typedef base::Callback<void()> ErrorCallback; | 25 typedef base::Callback<void()> ErrorCallback; |
28 | 26 |
29 // Constructs a DataSinkReceiver. Whenever the pipe is ready for reading, the | 27 // Constructs a DataSinkReceiver. Whenever the pipe is ready for reading, the |
30 // |ready_callback| will be called with the ReadOnlyBuffer to read. | 28 // |ready_callback| will be called with the ReadOnlyBuffer to read. |
31 // |ready_callback| will not be called again until the previous ReadOnlyBuffer | 29 // |ready_callback| will not be called again until the previous ReadOnlyBuffer |
(...skipping 10 matching lines...) Expand all Loading... | |
42 void ShutDown(); | 40 void ShutDown(); |
43 | 41 |
44 private: | 42 private: |
45 class Buffer; | 43 class Buffer; |
46 class PendingFlush; | 44 class PendingFlush; |
47 friend class base::RefCounted<DataSinkReceiver>; | 45 friend class base::RefCounted<DataSinkReceiver>; |
48 | 46 |
49 virtual ~DataSinkReceiver(); | 47 virtual ~DataSinkReceiver(); |
50 | 48 |
51 // mojo::InterfaceImpl<serial::DataSink> overrides. | 49 // mojo::InterfaceImpl<serial::DataSink> overrides. |
52 virtual void Init(mojo::ScopedDataPipeConsumerHandle handle) override; | 50 virtual void Init(uint32_t error) override; |
raymes
2014/10/17 03:10:12
is the parameter meant to be the buffer size?
Sam McNally
2014/10/20 05:12:59
Done.
| |
53 virtual void Cancel(int32_t error) override; | 51 virtual void Cancel(int32_t error) override; |
52 virtual void AcceptData(mojo::Array<uint8_t> data) override; | |
54 virtual void OnConnectionError() override; | 53 virtual void OnConnectionError() override; |
55 | 54 |
56 // Starts waiting for |handle_| to be ready for reads. | 55 // Checks whether there is data ready to dispatch and dispatches it if there |
57 void StartWaiting(); | 56 // is. |
58 | 57 void CheckForData(); |
59 // Invoked when |handle_| is ready for reads. | |
60 void OnDoneWaiting(MojoResult result); | |
61 | 58 |
62 // Reports a successful read of |bytes_read|. | 59 // Reports a successful read of |bytes_read|. |
63 void Done(uint32_t bytes_read); | 60 void Done(uint32_t bytes_read); |
64 | 61 |
65 // Reports a partially successful or unsuccessful read of |bytes_read| | 62 // Reports a partially successful or unsuccessful read of |bytes_read| |
66 // with an error of |error|. | 63 // with an error of |error|. |
67 void DoneWithError(uint32_t bytes_read, int32_t error); | 64 void DoneWithError(uint32_t bytes_read, int32_t error); |
68 | 65 |
69 // Finishes the two-phase data pipe read. | 66 // Finishes the two-phase data pipe read. |
70 bool DoneInternal(uint32_t bytes_read); | 67 bool DoneInternal(uint32_t bytes_read); |
71 | 68 |
72 // Sends an ReportBytesSentAndError message to the client. | 69 // Sends an ReportBytesSentAndError message to the client. |
73 void ReportBytesSentAndError(uint32_t bytes_read, int32_t error); | 70 void ReportBytesSentAndError(uint32_t bytes_read, int32_t error); |
74 | 71 |
75 // Invoked in response to an ReportBytesSentAndError call to the client with | 72 // Invoked in response to an ReportBytesSentAndError call to the client at |
76 // the number of bytes to flush. | 73 // the point in the data stream to flush. |
77 void SetNumBytesToFlush(uint32_t bytes_to_flush); | 74 void DoFlush(); |
78 | 75 |
79 // Reports a fatal error to the client and shuts down. | 76 // Reports a fatal error to the client and shuts down. |
80 void DispatchFatalError(); | 77 void DispatchFatalError(); |
81 | 78 |
82 // The data connection to the data sender. | 79 // The callback to call when there is data ready to read. |
83 mojo::ScopedDataPipeConsumerHandle handle_; | |
84 | |
85 // The callback to call when |handle_| has data ready to read. | |
86 const ReadyCallback ready_callback_; | 80 const ReadyCallback ready_callback_; |
87 | 81 |
88 // The callback to call when the client has requested cancellation. | 82 // The callback to call when the client has requested cancellation. |
89 const CancelCallback cancel_callback_; | 83 const CancelCallback cancel_callback_; |
90 | 84 |
91 // The callback to call if a fatal error occurs. | 85 // The callback to call if a fatal error occurs. |
92 const ErrorCallback error_callback_; | 86 const ErrorCallback error_callback_; |
93 | 87 |
94 // The queue of pending flushes. | 88 // Whether we are waiting for a flush. |
95 std::queue<linked_ptr<PendingFlush> > pending_flushes_; | 89 bool flush_pending_; |
96 | |
97 // A waiter used to wait until |handle_| is readable if we are waiting. | |
98 scoped_ptr<AsyncWaiter> waiter_; | |
99 | 90 |
100 // The buffer passed to |ready_callback_| if one exists. This is not owned, | 91 // The buffer passed to |ready_callback_| if one exists. This is not owned, |
101 // but the Buffer will call Done or DoneWithError before being deleted. | 92 // but the Buffer will call Done or DoneWithError before being deleted. |
102 Buffer* buffer_in_use_; | 93 Buffer* buffer_in_use_; |
103 | 94 |
95 bool waiting_for_data_; | |
96 | |
97 bool initialized_; | |
98 | |
99 uint32_t available_buffer_capacity_; | |
100 | |
101 std::queue<linked_ptr<mojo::Array<uint8_t>>> pending_data_buffers_; | |
102 | |
103 uint32_t pending_data_offset_; | |
raymes
2014/10/17 03:10:12
Please add comments for these.
Sam McNally
2014/10/20 05:12:59
Done.
| |
104 | |
104 // Whether we have encountered a fatal error and shut down. | 105 // Whether we have encountered a fatal error and shut down. |
105 bool shut_down_; | 106 bool shut_down_; |
106 | 107 |
107 base::WeakPtrFactory<DataSinkReceiver> weak_factory_; | 108 base::WeakPtrFactory<DataSinkReceiver> weak_factory_; |
108 | 109 |
109 DISALLOW_COPY_AND_ASSIGN(DataSinkReceiver); | 110 DISALLOW_COPY_AND_ASSIGN(DataSinkReceiver); |
110 }; | 111 }; |
111 | 112 |
112 } // namespace device | 113 } // namespace device |
113 | 114 |
114 #endif // DEVICE_SERIAL_DATA_SINK_RECEIVER_H_ | 115 #endif // DEVICE_SERIAL_DATA_SINK_RECEIVER_H_ |
OLD | NEW |