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_sink_receiver.h" | 5 #include "device/serial/data_sink_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 // A ReadOnlyBuffer implementation that provides a view of a buffer owned by a | 15 // A ReadOnlyBuffer implementation that provides a view of a buffer owned by a |
15 // DataSinkReceiver. | 16 // DataSinkReceiver. |
16 class DataSinkReceiver::Buffer : public ReadOnlyBuffer { | 17 class DataSinkReceiver::Buffer : public ReadOnlyBuffer { |
17 public: | 18 public: |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
64 mojo::Array<uint8_t> data_; | 65 mojo::Array<uint8_t> data_; |
65 uint32_t offset_; | 66 uint32_t offset_; |
66 const mojo::Callback<void(uint32_t, int32_t)> callback_; | 67 const mojo::Callback<void(uint32_t, int32_t)> callback_; |
67 }; | 68 }; |
68 | 69 |
69 DataSinkReceiver::DataSinkReceiver( | 70 DataSinkReceiver::DataSinkReceiver( |
70 mojo::InterfaceRequest<serial::DataSink> request, | 71 mojo::InterfaceRequest<serial::DataSink> request, |
71 const ReadyCallback& ready_callback, | 72 const ReadyCallback& ready_callback, |
72 const CancelCallback& cancel_callback, | 73 const CancelCallback& cancel_callback, |
73 const ErrorCallback& error_callback) | 74 const ErrorCallback& error_callback) |
74 : binding_(this, request.Pass()), | 75 : binding_(this, std::move(request)), |
75 ready_callback_(ready_callback), | 76 ready_callback_(ready_callback), |
76 cancel_callback_(cancel_callback), | 77 cancel_callback_(cancel_callback), |
77 error_callback_(error_callback), | 78 error_callback_(error_callback), |
78 current_error_(0), | 79 current_error_(0), |
79 buffer_in_use_(NULL), | 80 buffer_in_use_(NULL), |
80 shut_down_(false), | 81 shut_down_(false), |
81 weak_factory_(this) { | 82 weak_factory_(this) { |
82 binding_.set_connection_error_handler( | 83 binding_.set_connection_error_handler( |
83 base::Bind(&DataSinkReceiver::OnConnectionError, base::Unretained(this))); | 84 base::Bind(&DataSinkReceiver::OnConnectionError, base::Unretained(this))); |
84 } | 85 } |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
116 } | 117 } |
117 | 118 |
118 void DataSinkReceiver::OnData( | 119 void DataSinkReceiver::OnData( |
119 mojo::Array<uint8_t> data, | 120 mojo::Array<uint8_t> data, |
120 const mojo::Callback<void(uint32_t, int32_t)>& callback) { | 121 const mojo::Callback<void(uint32_t, int32_t)>& callback) { |
121 if (current_error_) { | 122 if (current_error_) { |
122 callback.Run(0, current_error_); | 123 callback.Run(0, current_error_); |
123 return; | 124 return; |
124 } | 125 } |
125 pending_data_buffers_.push( | 126 pending_data_buffers_.push( |
126 linked_ptr<DataFrame>(new DataFrame(data.Pass(), callback))); | 127 linked_ptr<DataFrame>(new DataFrame(std::move(data), callback))); |
127 if (!buffer_in_use_) | 128 if (!buffer_in_use_) |
128 RunReadyCallback(); | 129 RunReadyCallback(); |
129 } | 130 } |
130 | 131 |
131 void DataSinkReceiver::OnConnectionError() { | 132 void DataSinkReceiver::OnConnectionError() { |
132 DispatchFatalError(); | 133 DispatchFatalError(); |
133 } | 134 } |
134 | 135 |
135 void DataSinkReceiver::RunReadyCallback() { | 136 void DataSinkReceiver::RunReadyCallback() { |
136 DCHECK(!shut_down_ && !current_error_); | 137 DCHECK(!shut_down_ && !current_error_); |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
247 scoped_refptr<DataSinkReceiver> receiver = receiver_; | 248 scoped_refptr<DataSinkReceiver> receiver = receiver_; |
248 receiver_ = nullptr; | 249 receiver_ = nullptr; |
249 receiver->DoneWithError(bytes_read, error); | 250 receiver->DoneWithError(bytes_read, error); |
250 buffer_ = NULL; | 251 buffer_ = NULL; |
251 buffer_size_ = 0; | 252 buffer_size_ = 0; |
252 } | 253 } |
253 | 254 |
254 DataSinkReceiver::DataFrame::DataFrame( | 255 DataSinkReceiver::DataFrame::DataFrame( |
255 mojo::Array<uint8_t> data, | 256 mojo::Array<uint8_t> data, |
256 const mojo::Callback<void(uint32_t, int32_t)>& callback) | 257 const mojo::Callback<void(uint32_t, int32_t)>& callback) |
257 : data_(data.Pass()), offset_(0), callback_(callback) { | 258 : data_(std::move(data)), offset_(0), callback_(callback) { |
258 DCHECK_LT(0u, data_.size()); | 259 DCHECK_LT(0u, data_.size()); |
259 } | 260 } |
260 | 261 |
261 // Returns the number of uncomsumed bytes remaining of this data frame. | 262 // Returns the number of uncomsumed bytes remaining of this data frame. |
262 uint32_t DataSinkReceiver::DataFrame::GetRemainingBytes() { | 263 uint32_t DataSinkReceiver::DataFrame::GetRemainingBytes() { |
263 return static_cast<uint32_t>(data_.size() - offset_); | 264 return static_cast<uint32_t>(data_.size() - offset_); |
264 } | 265 } |
265 | 266 |
266 // Returns a pointer to the remaining data to be consumed. | 267 // Returns a pointer to the remaining data to be consumed. |
267 const char* DataSinkReceiver::DataFrame::GetData() { | 268 const char* DataSinkReceiver::DataFrame::GetData() { |
268 DCHECK_LT(offset_, data_.size()); | 269 DCHECK_LT(offset_, data_.size()); |
269 return reinterpret_cast<const char*>(&data_[0]) + offset_; | 270 return reinterpret_cast<const char*>(&data_[0]) + offset_; |
270 } | 271 } |
271 | 272 |
272 void DataSinkReceiver::DataFrame::OnDataConsumed(uint32_t bytes_read) { | 273 void DataSinkReceiver::DataFrame::OnDataConsumed(uint32_t bytes_read) { |
273 offset_ += bytes_read; | 274 offset_ += bytes_read; |
274 DCHECK_LE(offset_, data_.size()); | 275 DCHECK_LE(offset_, data_.size()); |
275 if (offset_ == data_.size()) | 276 if (offset_ == data_.size()) |
276 callback_.Run(offset_, 0); | 277 callback_.Run(offset_, 0); |
277 } | 278 } |
278 void DataSinkReceiver::DataFrame::ReportError(uint32_t bytes_read, | 279 void DataSinkReceiver::DataFrame::ReportError(uint32_t bytes_read, |
279 int32_t error) { | 280 int32_t error) { |
280 offset_ += bytes_read; | 281 offset_ += bytes_read; |
281 DCHECK_LE(offset_, data_.size()); | 282 DCHECK_LE(offset_, data_.size()); |
282 callback_.Run(offset_, error); | 283 callback_.Run(offset_, error); |
283 } | 284 } |
284 | 285 |
285 } // namespace device | 286 } // namespace device |
OLD | NEW |