Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(160)

Side by Side Diff: device/serial/data_receiver.cc

Issue 646063003: Change data pipe wrappers used by SerialConnection to use message pipe. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/message_loop/message_loop.h" 10 #include "base/message_loop/message_loop.h"
11 #include "device/serial/async_waiter.h"
12 11
13 namespace device { 12 namespace device {
14 13
15 // Represents a receive that is not yet fulfilled. 14 // Represents a receive that is not yet fulfilled.
16 class DataReceiver::PendingReceive { 15 class DataReceiver::PendingReceive {
17 public: 16 public:
18 PendingReceive(DataReceiver* receiver, 17 PendingReceive(DataReceiver* receiver,
19 const ReceiveDataCallback& callback, 18 const ReceiveDataCallback& callback,
20 const ReceiveErrorCallback& error_callback, 19 const ReceiveErrorCallback& error_callback,
21 int32_t fatal_error_value); 20 int32_t fatal_error_value);
22 21
23 // Dispatches |data| to |receive_callback_|. 22 // Dispatches |data| to |receive_callback_|.
24 void DispatchData(const void* data, uint32_t num_bytes); 23 void DispatchData(DataReceiver::PendingData* data);
25 24
26 // Reports |error| to |receive_error_callback_| if it is an appropriate time. 25 // Reports |error| to |receive_error_callback_| if it is an appropriate time.
27 // Returns whether it dispatched |error|. 26 // Returns whether it dispatched |error|.
28 bool DispatchError(DataReceiver::PendingError* error, 27 bool DispatchError(DataReceiver::PendingError* error);
29 uint32_t bytes_received);
30 28
31 // Reports |fatal_error_value_| to |receive_error_callback_|. 29 // Reports |fatal_error_value_| to |receive_error_callback_|.
32 void DispatchFatalError(); 30 void DispatchFatalError();
33 31
32 bool dispatched() { return buffer_in_use_; }
raymes 2014/10/17 01:55:42 why not just call this function "buffer_in_use()"
Sam McNally 2014/10/20 05:12:58 Done.
33
34 private: 34 private:
35 class Buffer; 35 class Buffer;
36 36
37 // Invoked when the user is finished with the ReadOnlyBuffer provided to 37 // Invoked when the user is finished with the ReadOnlyBuffer provided to
38 // |receive_callback_|. 38 // |receive_callback_|.
39 void Done(uint32_t num_bytes); 39 void Done(uint32_t num_bytes);
40 40
41 // The DataReceiver that owns this. 41 // The DataReceiver that owns this.
42 DataReceiver* receiver_; 42 DataReceiver* receiver_;
43 43
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
75 // The DataReceiver whose data pipe we are providing a view. 75 // The DataReceiver whose data pipe we are providing a view.
76 scoped_refptr<DataReceiver> receiver_; 76 scoped_refptr<DataReceiver> receiver_;
77 77
78 // The PendingReceive to which this buffer has been created in response. 78 // The PendingReceive to which this buffer has been created in response.
79 PendingReceive* pending_receive_; 79 PendingReceive* pending_receive_;
80 80
81 const char* buffer_; 81 const char* buffer_;
82 uint32_t buffer_size_; 82 uint32_t buffer_size_;
83 }; 83 };
84 84
85 // A buffer of data received from the DataSource.
86 struct DataReceiver::PendingData {
87 explicit PendingData(mojo::Array<uint8_t> data)
88 : data(data.Pass()), offset(0) {}
89
90 mojo::Array<uint8_t> data;
91
92 // The offset within |data| at which the next read should begin.
93 uint32_t offset;
94 };
95
85 // Represents an error received from the DataSource. 96 // Represents an error received from the DataSource.
86 struct DataReceiver::PendingError { 97 struct DataReceiver::PendingError {
raymes 2014/10/17 01:55:42 Could we combine PendingData and PendingError? It
Sam McNally 2014/10/20 05:12:58 Done.
87 PendingError(uint32_t offset, int32_t error) 98 PendingError(int32_t error, size_t queue_position)
88 : offset(offset), error(error), dispatched(false) {} 99 : queue_position(queue_position), error(error), dispatched(false) {}
89 100
90 // The location within the data stream where the error occurred. 101 // The number of pending data buffers to be dispatched before this error.
91 const uint32_t offset; 102 size_t queue_position;
92 103
93 // The value of the error that occurred. 104 // The value of the error that occurred.
94 const int32_t error; 105 const int32_t error;
95 106
96 // Whether the error has been dispatched to the user. 107 // Whether the error has been dispatched to the user.
97 bool dispatched; 108 bool dispatched;
98 }; 109 };
99 110
100 DataReceiver::DataReceiver(mojo::InterfacePtr<serial::DataSource> source, 111 DataReceiver::DataReceiver(mojo::InterfacePtr<serial::DataSource> source,
101 uint32_t buffer_size, 112 uint32_t buffer_size,
102 int32_t fatal_error_value) 113 int32_t fatal_error_value)
103 : source_(source.Pass()), 114 : source_(source.Pass()),
104 fatal_error_value_(fatal_error_value), 115 fatal_error_value_(fatal_error_value),
105 bytes_received_(0),
106 shut_down_(false), 116 shut_down_(false),
107 weak_factory_(this) { 117 weak_factory_(this) {
108 MojoCreateDataPipeOptions options = {
109 sizeof(options), MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, 1, buffer_size,
110 };
111 mojo::ScopedDataPipeProducerHandle remote_handle;
112 MojoResult result = mojo::CreateDataPipe(&options, &remote_handle, &handle_);
113 DCHECK_EQ(MOJO_RESULT_OK, result);
114 source_->Init(remote_handle.Pass());
115 source_.set_client(this); 118 source_.set_client(this);
119 source_.set_error_handler(this);
120 source_->Init(buffer_size);
116 } 121 }
117 122
118 bool DataReceiver::Receive(const ReceiveDataCallback& callback, 123 bool DataReceiver::Receive(const ReceiveDataCallback& callback,
119 const ReceiveErrorCallback& error_callback) { 124 const ReceiveErrorCallback& error_callback) {
120 DCHECK(!callback.is_null() && !error_callback.is_null()); 125 DCHECK(!callback.is_null() && !error_callback.is_null());
121 if (pending_receive_ || shut_down_) 126 if (pending_receive_ || shut_down_)
122 return false; 127 return false;
123 // When the DataSource encounters an error, it pauses transmission. When the 128 // When the DataSource encounters an error, it pauses transmission. When the
124 // user starts a new receive following notification of the error (via 129 // user starts a new receive following notification of the error (via
125 // |error_callback| of the previous Receive call) of the error we can tell the 130 // |error_callback| of the previous Receive call) of the error we can tell the
126 // DataSource to resume transmission of data. 131 // DataSource to resume transmission of data.
127 if (pending_error_ && pending_error_->dispatched) { 132 if (pending_error_ && pending_error_->dispatched) {
128 source_->Resume(); 133 source_->Resume();
129 pending_error_.reset(); 134 pending_error_.reset();
130 } 135 }
131 136
132 pending_receive_.reset( 137 pending_receive_.reset(
133 new PendingReceive(this, callback, error_callback, fatal_error_value_)); 138 new PendingReceive(this, callback, error_callback, fatal_error_value_));
134 base::MessageLoop::current()->PostTask( 139 ReceiveInternal();
135 FROM_HERE,
136 base::Bind(&DataReceiver::ReceiveInternal, weak_factory_.GetWeakPtr()));
137 return true; 140 return true;
138 } 141 }
139 142
140 DataReceiver::~DataReceiver() { 143 DataReceiver::~DataReceiver() {
141 ShutDown(); 144 ShutDown();
142 } 145 }
143 146
144 void DataReceiver::OnError(uint32_t offset, int32_t error) { 147 void DataReceiver::OnError(int32_t error) {
145 if (shut_down_) 148 if (shut_down_)
146 return; 149 return;
147 150
148 if (pending_error_) { 151 if (pending_error_) {
149 // When OnError is called by the DataSource, transmission of data is 152 // When OnError is called by the DataSource, transmission of data is
150 // suspended. Thus we shouldn't receive another call to OnError until we 153 // suspended. Thus we shouldn't receive another call to OnError until we
151 // have fully dealt with the error and called Resume to resume transmission 154 // have fully dealt with the error and called Resume to resume transmission
152 // (see Receive()). Under normal operation we should never get here, but if 155 // (see Receive()). Under normal operation we should never get here, but if
153 // we do (e.g. in the case of a hijacked service process) just shut down. 156 // we do (e.g. in the case of a hijacked service process) just shut down.
154 ShutDown(); 157 ShutDown();
155 return; 158 return;
156 } 159 }
157 pending_error_.reset(new PendingError(offset, error)); 160 pending_error_.reset(new PendingError(error, pending_data_buffers_.size()));
158 if (pending_receive_ && 161 if (pending_receive_)
159 pending_receive_->DispatchError(pending_error_.get(), bytes_received_)) { 162 ReceiveInternal();
160 pending_receive_.reset(); 163 }
161 waiter_.reset(); 164
162 } 165 void DataReceiver::OnData(mojo::Array<uint8_t> data) {
166 pending_data_buffers_.push(
167 linked_ptr<PendingData>(new PendingData(data.Pass())));
168 if (pending_receive_)
169 ReceiveInternal();
163 } 170 }
164 171
165 void DataReceiver::OnConnectionError() { 172 void DataReceiver::OnConnectionError() {
166 ShutDown(); 173 ShutDown();
167 } 174 }
168 175
169 void DataReceiver::Done(uint32_t bytes_consumed) { 176 void DataReceiver::Done(uint32_t bytes_consumed) {
170 if (shut_down_) 177 if (shut_down_)
171 return; 178 return;
172 179
173 DCHECK(pending_receive_); 180 DCHECK(pending_receive_);
174 MojoResult result = mojo::EndReadDataRaw(handle_.get(), bytes_consumed); 181 PendingData& pending_data = *pending_data_buffers_.front();
175 DCHECK_EQ(MOJO_RESULT_OK, result); 182 pending_data.offset += bytes_consumed;
183 if (pending_data.offset == pending_data.data.size()) {
raymes 2014/10/17 01:55:42 Maybe we should at least DCHECK here that pending_
Sam McNally 2014/10/20 05:12:58 Done.
184 if (pending_error_)
185 pending_error_->queue_position--;
186 pending_data_buffers_.pop();
187 }
176 pending_receive_.reset(); 188 pending_receive_.reset();
177 bytes_received_ += bytes_consumed;
178 }
179
180 void DataReceiver::OnDoneWaiting(MojoResult result) {
181 DCHECK(pending_receive_ && !shut_down_ && waiter_);
182 waiter_.reset();
183 if (result != MOJO_RESULT_OK) {
184 ShutDown();
185 return;
186 }
187 ReceiveInternal();
188 } 189 }
189 190
190 void DataReceiver::ReceiveInternal() { 191 void DataReceiver::ReceiveInternal() {
191 if (shut_down_) 192 if (shut_down_)
192 return; 193 return;
193 DCHECK(pending_receive_); 194 DCHECK(pending_receive_);
194 if (pending_error_ && 195 if (pending_receive_->dispatched())
195 pending_receive_->DispatchError(pending_error_.get(), bytes_received_)) { 196 return;
197
198 if (pending_error_ && pending_receive_->DispatchError(pending_error_.get())) {
196 pending_receive_.reset(); 199 pending_receive_.reset();
197 waiter_.reset();
198 return; 200 return;
199 } 201 }
200 202 if (!pending_data_buffers_.empty())
201 const void* data; 203 pending_receive_->DispatchData(pending_data_buffers_.front().get());
202 uint32_t num_bytes = std::numeric_limits<uint32_t>::max();
203 MojoResult result = mojo::BeginReadDataRaw(
204 handle_.get(), &data, &num_bytes, MOJO_READ_DATA_FLAG_NONE);
205 if (result == MOJO_RESULT_OK) {
206 if (!CheckErrorNotInReadRange(num_bytes)) {
207 ShutDown();
208 return;
209 }
210
211 pending_receive_->DispatchData(data, num_bytes);
212 return;
213 }
214 if (result == MOJO_RESULT_SHOULD_WAIT) {
215 waiter_.reset(new AsyncWaiter(
216 handle_.get(),
217 MOJO_HANDLE_SIGNAL_READABLE,
218 base::Bind(&DataReceiver::OnDoneWaiting, weak_factory_.GetWeakPtr())));
219 return;
220 }
221 ShutDown();
222 }
223
224 bool DataReceiver::CheckErrorNotInReadRange(uint32_t num_bytes) {
225 DCHECK(pending_receive_);
226 if (!pending_error_)
227 return true;
228
229 DCHECK_NE(bytes_received_, pending_error_->offset);
230 DCHECK_NE(num_bytes, 0u);
231 uint32_t potential_bytes_received = bytes_received_ + num_bytes;
232 // bytes_received_ can overflow so we must consider two cases:
233 // 1. Both |bytes_received_| and |pending_error_->offset| have overflowed an
234 // equal number of times. In this case, |potential_bytes_received| must
235 // be in the range (|bytes_received|, |pending_error_->offset|]. Below
236 // this range can only occur if |bytes_received_| overflows before
237 // |pending_error_->offset|. Above can only occur if |bytes_received_|
238 // overtakes |pending_error_->offset|.
239 // 2. |pending_error_->offset| has overflowed once more than
240 // |bytes_received_|. In this case, |potential_bytes_received| must not
241 // be in the range (|pending_error_->offset|, |bytes_received_|].
242 if ((bytes_received_ < pending_error_->offset &&
243 (potential_bytes_received > pending_error_->offset ||
244 potential_bytes_received <= bytes_received_)) ||
245 (bytes_received_ > pending_error_->offset &&
246 potential_bytes_received > pending_error_->offset &&
247 potential_bytes_received <= bytes_received_)) {
248 return false;
249 }
250 return true;
251 } 204 }
252 205
253 void DataReceiver::ShutDown() { 206 void DataReceiver::ShutDown() {
254 shut_down_ = true; 207 shut_down_ = true;
255 if (pending_receive_) 208 if (pending_receive_)
256 pending_receive_->DispatchFatalError(); 209 pending_receive_->DispatchFatalError();
257 pending_error_.reset(); 210 pending_error_.reset();
258 waiter_.reset();
259 } 211 }
260 212
261 DataReceiver::PendingReceive::PendingReceive( 213 DataReceiver::PendingReceive::PendingReceive(
262 DataReceiver* receiver, 214 DataReceiver* receiver,
263 const ReceiveDataCallback& callback, 215 const ReceiveDataCallback& callback,
264 const ReceiveErrorCallback& error_callback, 216 const ReceiveErrorCallback& error_callback,
265 int32_t fatal_error_value) 217 int32_t fatal_error_value)
266 : receiver_(receiver), 218 : receiver_(receiver),
267 receive_callback_(callback), 219 receive_callback_(callback),
268 receive_error_callback_(error_callback), 220 receive_error_callback_(error_callback),
269 fatal_error_value_(fatal_error_value), 221 fatal_error_value_(fatal_error_value),
270 buffer_in_use_(false) { 222 buffer_in_use_(false) {
271 } 223 }
272 224
273 void DataReceiver::PendingReceive::DispatchData(const void* data, 225 void DataReceiver::PendingReceive::DispatchData(
274 uint32_t num_bytes) { 226 DataReceiver::PendingData* data) {
275 DCHECK(!buffer_in_use_); 227 DCHECK(!buffer_in_use_);
276 buffer_in_use_ = true; 228 buffer_in_use_ = true;
277 receive_callback_.Run(scoped_ptr<ReadOnlyBuffer>( 229 base::MessageLoop::current()->PostTask(
278 new Buffer(receiver_, this, static_cast<const char*>(data), num_bytes))); 230 FROM_HERE,
231 base::Bind(
232 receive_callback_,
233 base::Passed(scoped_ptr<ReadOnlyBuffer>(new Buffer(
234 receiver_,
235 this,
236 reinterpret_cast<char*>(&data->data[0]) + data->offset,
237 static_cast<uint32_t>(data->data.size() - data->offset))))));
279 } 238 }
280 239
281 bool DataReceiver::PendingReceive::DispatchError(PendingError* error, 240 bool DataReceiver::PendingReceive::DispatchError(PendingError* error) {
282 uint32_t bytes_received) {
283 DCHECK(!error->dispatched); 241 DCHECK(!error->dispatched);
284 if (buffer_in_use_ || bytes_received != error->offset) 242 if (buffer_in_use_ || error->queue_position != 0)
285 return false; 243 return false;
286 244
287 error->dispatched = true; 245 error->dispatched = true;
288 receive_error_callback_.Run(error->error); 246 base::MessageLoop::current()->PostTask(
247 FROM_HERE, base::Bind(receive_error_callback_, error->error));
289 return true; 248 return true;
290 } 249 }
291 250
292 void DataReceiver::PendingReceive::DispatchFatalError() { 251 void DataReceiver::PendingReceive::DispatchFatalError() {
293 receive_error_callback_.Run(fatal_error_value_); 252 receive_error_callback_.Run(fatal_error_value_);
294 } 253 }
295 254
296 void DataReceiver::PendingReceive::Done(uint32_t bytes_consumed) { 255 void DataReceiver::PendingReceive::Done(uint32_t bytes_consumed) {
297 DCHECK(buffer_in_use_); 256 DCHECK(buffer_in_use_);
298 buffer_in_use_ = false; 257 buffer_in_use_ = false;
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
331 buffer_size_ = 0; 290 buffer_size_ = 0;
332 } 291 }
333 292
334 void DataReceiver::PendingReceive::Buffer::DoneWithError( 293 void DataReceiver::PendingReceive::Buffer::DoneWithError(
335 uint32_t bytes_consumed, 294 uint32_t bytes_consumed,
336 int32_t error) { 295 int32_t error) {
337 Done(bytes_consumed); 296 Done(bytes_consumed);
338 } 297 }
339 298
340 } // namespace device 299 } // namespace device
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698