| 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 <memory> | 8 #include <memory> |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| 11 #include "base/bind.h" | 11 #include "base/bind.h" |
| 12 #include "base/message_loop/message_loop.h" | 12 #include "base/location.h" |
| 13 #include "base/single_thread_task_runner.h" |
| 14 #include "base/threading/thread_task_runner_handle.h" |
| 13 | 15 |
| 14 namespace device { | 16 namespace device { |
| 15 | 17 |
| 16 // A ReadOnlyBuffer implementation that provides a view of a buffer owned by a | 18 // A ReadOnlyBuffer implementation that provides a view of a buffer owned by a |
| 17 // DataSinkReceiver. | 19 // DataSinkReceiver. |
| 18 class DataSinkReceiver::Buffer : public ReadOnlyBuffer { | 20 class DataSinkReceiver::Buffer : public ReadOnlyBuffer { |
| 19 public: | 21 public: |
| 20 Buffer(scoped_refptr<DataSinkReceiver> receiver, | 22 Buffer(scoped_refptr<DataSinkReceiver> receiver, |
| 21 const char* buffer, | 23 const char* buffer, |
| 22 uint32_t buffer_size); | 24 uint32_t buffer_size); |
| (...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 147 ready_callback_.Run(std::unique_ptr<ReadOnlyBuffer>(buffer_in_use_)); | 149 ready_callback_.Run(std::unique_ptr<ReadOnlyBuffer>(buffer_in_use_)); |
| 148 } | 150 } |
| 149 | 151 |
| 150 void DataSinkReceiver::Done(uint32_t bytes_read) { | 152 void DataSinkReceiver::Done(uint32_t bytes_read) { |
| 151 if (!DoneInternal(bytes_read)) | 153 if (!DoneInternal(bytes_read)) |
| 152 return; | 154 return; |
| 153 pending_data_buffers_.front()->OnDataConsumed(bytes_read); | 155 pending_data_buffers_.front()->OnDataConsumed(bytes_read); |
| 154 if (pending_data_buffers_.front()->GetRemainingBytes() == 0) | 156 if (pending_data_buffers_.front()->GetRemainingBytes() == 0) |
| 155 pending_data_buffers_.pop(); | 157 pending_data_buffers_.pop(); |
| 156 if (!pending_data_buffers_.empty()) { | 158 if (!pending_data_buffers_.empty()) { |
| 157 base::MessageLoop::current()->PostTask( | 159 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 158 FROM_HERE, | 160 FROM_HERE, base::Bind(&DataSinkReceiver::RunReadyCallback, |
| 159 base::Bind(&DataSinkReceiver::RunReadyCallback, | 161 weak_factory_.GetWeakPtr())); |
| 160 weak_factory_.GetWeakPtr())); | |
| 161 } | 162 } |
| 162 } | 163 } |
| 163 | 164 |
| 164 void DataSinkReceiver::DoneWithError(uint32_t bytes_read, int32_t error) { | 165 void DataSinkReceiver::DoneWithError(uint32_t bytes_read, int32_t error) { |
| 165 if (!DoneInternal(bytes_read)) | 166 if (!DoneInternal(bytes_read)) |
| 166 return; | 167 return; |
| 167 ReportError(bytes_read, error); | 168 ReportError(bytes_read, error); |
| 168 } | 169 } |
| 169 | 170 |
| 170 bool DataSinkReceiver::DoneInternal(uint32_t bytes_read) { | 171 bool DataSinkReceiver::DoneInternal(uint32_t bytes_read) { |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 278 callback_.Run(offset_, 0); | 279 callback_.Run(offset_, 0); |
| 279 } | 280 } |
| 280 void DataSinkReceiver::DataFrame::ReportError(uint32_t bytes_read, | 281 void DataSinkReceiver::DataFrame::ReportError(uint32_t bytes_read, |
| 281 int32_t error) { | 282 int32_t error) { |
| 282 offset_ += bytes_read; | 283 offset_ += bytes_read; |
| 283 DCHECK_LE(offset_, data_.size()); | 284 DCHECK_LE(offset_, data_.size()); |
| 284 callback_.Run(offset_, error); | 285 callback_.Run(offset_, error); |
| 285 } | 286 } |
| 286 | 287 |
| 287 } // namespace device | 288 } // namespace device |
| OLD | NEW |