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 #include "device/serial/data_receiver.h" | 5 #include "device/serial/data_receiver.h" |
6 | 6 |
7 #include <limits> | 7 #include <limits> |
| 8 #include <utility> |
8 | 9 |
9 #include "base/bind.h" | 10 #include "base/bind.h" |
10 #include "base/message_loop/message_loop.h" | 11 #include "base/message_loop/message_loop.h" |
11 | 12 |
12 namespace device { | 13 namespace device { |
13 | 14 |
14 // Represents a receive that is not yet fulfilled. | 15 // Represents a receive that is not yet fulfilled. |
15 class DataReceiver::PendingReceive { | 16 class DataReceiver::PendingReceive { |
16 public: | 17 public: |
17 PendingReceive(DataReceiver* receiver, | 18 PendingReceive(DataReceiver* receiver, |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
76 PendingReceive* pending_receive_; | 77 PendingReceive* pending_receive_; |
77 | 78 |
78 const char* buffer_; | 79 const char* buffer_; |
79 uint32_t buffer_size_; | 80 uint32_t buffer_size_; |
80 }; | 81 }; |
81 | 82 |
82 // A buffer of data or an error received from the DataSource. | 83 // A buffer of data or an error received from the DataSource. |
83 struct DataReceiver::DataFrame { | 84 struct DataReceiver::DataFrame { |
84 explicit DataFrame(mojo::Array<uint8_t> data) | 85 explicit DataFrame(mojo::Array<uint8_t> data) |
85 : is_error(false), | 86 : is_error(false), |
86 data(data.Pass()), | 87 data(std::move(data)), |
87 offset(0), | 88 offset(0), |
88 error(0), | 89 error(0), |
89 dispatched(false) {} | 90 dispatched(false) {} |
90 | 91 |
91 explicit DataFrame(int32_t error) | 92 explicit DataFrame(int32_t error) |
92 : is_error(true), offset(0), error(error), dispatched(false) {} | 93 : is_error(true), offset(0), error(error), dispatched(false) {} |
93 | 94 |
94 // Whether this DataFrame represents an error. | 95 // Whether this DataFrame represents an error. |
95 bool is_error; | 96 bool is_error; |
96 | 97 |
97 // The data received from the DataSource. | 98 // The data received from the DataSource. |
98 mojo::Array<uint8_t> data; | 99 mojo::Array<uint8_t> data; |
99 | 100 |
100 // The offset within |data| at which the next read should begin. | 101 // The offset within |data| at which the next read should begin. |
101 uint32_t offset; | 102 uint32_t offset; |
102 | 103 |
103 // The value of the error that occurred. | 104 // The value of the error that occurred. |
104 const int32_t error; | 105 const int32_t error; |
105 | 106 |
106 // Whether the error has been dispatched to the user. | 107 // Whether the error has been dispatched to the user. |
107 bool dispatched; | 108 bool dispatched; |
108 }; | 109 }; |
109 | 110 |
110 DataReceiver::DataReceiver( | 111 DataReceiver::DataReceiver( |
111 mojo::InterfacePtr<serial::DataSource> source, | 112 mojo::InterfacePtr<serial::DataSource> source, |
112 mojo::InterfaceRequest<serial::DataSourceClient> client, | 113 mojo::InterfaceRequest<serial::DataSourceClient> client, |
113 uint32_t buffer_size, | 114 uint32_t buffer_size, |
114 int32_t fatal_error_value) | 115 int32_t fatal_error_value) |
115 : source_(source.Pass()), | 116 : source_(std::move(source)), |
116 client_(this, client.Pass()), | 117 client_(this, std::move(client)), |
117 fatal_error_value_(fatal_error_value), | 118 fatal_error_value_(fatal_error_value), |
118 shut_down_(false), | 119 shut_down_(false), |
119 weak_factory_(this) { | 120 weak_factory_(this) { |
120 source_.set_connection_error_handler( | 121 source_.set_connection_error_handler( |
121 base::Bind(&DataReceiver::OnConnectionError, base::Unretained(this))); | 122 base::Bind(&DataReceiver::OnConnectionError, base::Unretained(this))); |
122 source_->Init(buffer_size); | 123 source_->Init(buffer_size); |
123 client_.set_connection_error_handler( | 124 client_.set_connection_error_handler( |
124 base::Bind(&DataReceiver::OnConnectionError, base::Unretained(this))); | 125 base::Bind(&DataReceiver::OnConnectionError, base::Unretained(this))); |
125 } | 126 } |
126 | 127 |
(...skipping 25 matching lines...) Expand all Loading... |
152 void DataReceiver::OnError(int32_t error) { | 153 void DataReceiver::OnError(int32_t error) { |
153 if (shut_down_) | 154 if (shut_down_) |
154 return; | 155 return; |
155 | 156 |
156 pending_data_frames_.push(linked_ptr<DataFrame>(new DataFrame(error))); | 157 pending_data_frames_.push(linked_ptr<DataFrame>(new DataFrame(error))); |
157 if (pending_receive_) | 158 if (pending_receive_) |
158 ReceiveInternal(); | 159 ReceiveInternal(); |
159 } | 160 } |
160 | 161 |
161 void DataReceiver::OnData(mojo::Array<uint8_t> data) { | 162 void DataReceiver::OnData(mojo::Array<uint8_t> data) { |
162 pending_data_frames_.push(linked_ptr<DataFrame>(new DataFrame(data.Pass()))); | 163 pending_data_frames_.push( |
| 164 linked_ptr<DataFrame>(new DataFrame(std::move(data)))); |
163 if (pending_receive_) | 165 if (pending_receive_) |
164 ReceiveInternal(); | 166 ReceiveInternal(); |
165 } | 167 } |
166 | 168 |
167 void DataReceiver::OnConnectionError() { | 169 void DataReceiver::OnConnectionError() { |
168 ShutDown(); | 170 ShutDown(); |
169 } | 171 } |
170 | 172 |
171 void DataReceiver::Done(uint32_t bytes_consumed) { | 173 void DataReceiver::Done(uint32_t bytes_consumed) { |
172 if (shut_down_) | 174 if (shut_down_) |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
281 buffer_size_ = 0; | 283 buffer_size_ = 0; |
282 } | 284 } |
283 | 285 |
284 void DataReceiver::PendingReceive::Buffer::DoneWithError( | 286 void DataReceiver::PendingReceive::Buffer::DoneWithError( |
285 uint32_t bytes_consumed, | 287 uint32_t bytes_consumed, |
286 int32_t error) { | 288 int32_t error) { |
287 Done(bytes_consumed); | 289 Done(bytes_consumed); |
288 } | 290 } |
289 | 291 |
290 } // namespace device | 292 } // namespace device |
OLD | NEW |