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 | 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" |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
54 const char* GetData(); | 54 const char* GetData(); |
55 | 55 |
56 // Reports that |bytes_read| bytes have been consumed. | 56 // Reports that |bytes_read| bytes have been consumed. |
57 void OnDataConsumed(uint32_t bytes_read); | 57 void OnDataConsumed(uint32_t bytes_read); |
58 | 58 |
59 private: | 59 private: |
60 mojo::Array<uint8_t> data_; | 60 mojo::Array<uint8_t> data_; |
61 uint32_t offset_; | 61 uint32_t offset_; |
62 }; | 62 }; |
63 | 63 |
64 DataSinkReceiver::DataSinkReceiver( | 64 DataSinkReceiver::DataSinkReceiver(const ReadyCallback& ready_callback, |
65 mojo::InterfaceRequest<serial::DataSink> request, | 65 const CancelCallback& cancel_callback, |
66 mojo::InterfacePtr<serial::DataSinkClient> client, | 66 const ErrorCallback& error_callback) |
67 const ReadyCallback& ready_callback, | 67 : ready_callback_(ready_callback), |
68 const CancelCallback& cancel_callback, | |
69 const ErrorCallback& error_callback) | |
70 : binding_(this, request.Pass()), | |
71 client_(client.Pass()), | |
72 ready_callback_(ready_callback), | |
73 cancel_callback_(cancel_callback), | 68 cancel_callback_(cancel_callback), |
74 error_callback_(error_callback), | 69 error_callback_(error_callback), |
75 flush_pending_(false), | 70 flush_pending_(false), |
76 buffer_in_use_(NULL), | 71 buffer_in_use_(NULL), |
77 initialized_(false), | 72 initialized_(false), |
78 available_buffer_capacity_(0), | 73 available_buffer_capacity_(0), |
79 shut_down_(false), | 74 shut_down_(false), |
80 weak_factory_(this) { | 75 weak_factory_(this) { |
81 binding_.set_error_handler(this); | |
82 client_.set_error_handler(this); | |
83 } | 76 } |
84 | 77 |
85 void DataSinkReceiver::ShutDown() { | 78 void DataSinkReceiver::ShutDown() { |
86 shut_down_ = true; | 79 shut_down_ = true; |
87 } | 80 } |
88 | 81 |
89 DataSinkReceiver::~DataSinkReceiver() { | 82 DataSinkReceiver::~DataSinkReceiver() { |
90 } | 83 } |
91 | 84 |
92 void DataSinkReceiver::Init(uint32_t buffer_size) { | 85 void DataSinkReceiver::Init(uint32_t buffer_size) { |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
151 buffer_in_use_ = | 144 buffer_in_use_ = |
152 new Buffer(this, | 145 new Buffer(this, |
153 pending_data_buffers_.front()->GetData(), | 146 pending_data_buffers_.front()->GetData(), |
154 pending_data_buffers_.front()->GetRemainingBytes()); | 147 pending_data_buffers_.front()->GetRemainingBytes()); |
155 ready_callback_.Run(scoped_ptr<ReadOnlyBuffer>(buffer_in_use_)); | 148 ready_callback_.Run(scoped_ptr<ReadOnlyBuffer>(buffer_in_use_)); |
156 } | 149 } |
157 | 150 |
158 void DataSinkReceiver::Done(uint32_t bytes_read) { | 151 void DataSinkReceiver::Done(uint32_t bytes_read) { |
159 if (!DoneInternal(bytes_read)) | 152 if (!DoneInternal(bytes_read)) |
160 return; | 153 return; |
161 client_->ReportBytesSent(bytes_read); | 154 client()->ReportBytesSent(bytes_read); |
162 if (!pending_data_buffers_.empty()) { | 155 if (!pending_data_buffers_.empty()) { |
163 base::MessageLoop::current()->PostTask( | 156 base::MessageLoop::current()->PostTask( |
164 FROM_HERE, | 157 FROM_HERE, |
165 base::Bind(&DataSinkReceiver::RunReadyCallback, | 158 base::Bind(&DataSinkReceiver::RunReadyCallback, |
166 weak_factory_.GetWeakPtr())); | 159 weak_factory_.GetWeakPtr())); |
167 } | 160 } |
168 } | 161 } |
169 | 162 |
170 void DataSinkReceiver::DoneWithError(uint32_t bytes_read, int32_t error) { | 163 void DataSinkReceiver::DoneWithError(uint32_t bytes_read, int32_t error) { |
171 if (!DoneInternal(bytes_read)) | 164 if (!DoneInternal(bytes_read)) |
(...skipping 12 matching lines...) Expand all Loading... |
184 if (pending_data_buffers_.front()->GetRemainingBytes() == 0) | 177 if (pending_data_buffers_.front()->GetRemainingBytes() == 0) |
185 pending_data_buffers_.pop(); | 178 pending_data_buffers_.pop(); |
186 return true; | 179 return true; |
187 } | 180 } |
188 | 181 |
189 void DataSinkReceiver::ReportBytesSentAndError(uint32_t bytes_read, | 182 void DataSinkReceiver::ReportBytesSentAndError(uint32_t bytes_read, |
190 int32_t error) { | 183 int32_t error) { |
191 // When we encounter an error, we must discard the data from any send buffers | 184 // When we encounter an error, we must discard the data from any send buffers |
192 // transmitted by the DataSinkClient before it receives this error. | 185 // transmitted by the DataSinkClient before it receives this error. |
193 flush_pending_ = true; | 186 flush_pending_ = true; |
194 client_->ReportBytesSentAndError( | 187 client()->ReportBytesSentAndError( |
195 bytes_read, error, | 188 bytes_read, |
| 189 error, |
196 base::Bind(&DataSinkReceiver::DoFlush, weak_factory_.GetWeakPtr())); | 190 base::Bind(&DataSinkReceiver::DoFlush, weak_factory_.GetWeakPtr())); |
197 } | 191 } |
198 | 192 |
199 void DataSinkReceiver::DoFlush() { | 193 void DataSinkReceiver::DoFlush() { |
200 DCHECK(flush_pending_); | 194 DCHECK(flush_pending_); |
201 flush_pending_ = false; | 195 flush_pending_ = false; |
202 while (!pending_data_buffers_.empty()) { | 196 while (!pending_data_buffers_.empty()) { |
203 available_buffer_capacity_ += | 197 available_buffer_capacity_ += |
204 pending_data_buffers_.front()->GetRemainingBytes(); | 198 pending_data_buffers_.front()->GetRemainingBytes(); |
205 pending_data_buffers_.pop(); | 199 pending_data_buffers_.pop(); |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
282 DCHECK_LT(offset_, data_.size()); | 276 DCHECK_LT(offset_, data_.size()); |
283 return reinterpret_cast<const char*>(&data_[0]) + offset_; | 277 return reinterpret_cast<const char*>(&data_[0]) + offset_; |
284 } | 278 } |
285 | 279 |
286 void DataSinkReceiver::DataFrame::OnDataConsumed(uint32_t bytes_read) { | 280 void DataSinkReceiver::DataFrame::OnDataConsumed(uint32_t bytes_read) { |
287 offset_ += bytes_read; | 281 offset_ += bytes_read; |
288 DCHECK_LE(offset_, data_.size()); | 282 DCHECK_LE(offset_, data_.size()); |
289 } | 283 } |
290 | 284 |
291 } // namespace device | 285 } // namespace device |
OLD | NEW |