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_sender.h" | 5 #include "device/serial/data_sender.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/message_loop/message_loop.h" | 11 #include "base/location.h" |
| 12 #include "base/single_thread_task_runner.h" |
| 13 #include "base/threading/thread_task_runner_handle.h" |
12 | 14 |
13 namespace device { | 15 namespace device { |
14 | 16 |
15 // Represents a send that is not yet fulfilled. | 17 // Represents a send that is not yet fulfilled. |
16 class DataSender::PendingSend { | 18 class DataSender::PendingSend { |
17 public: | 19 public: |
18 PendingSend(const base::StringPiece& data, | 20 PendingSend(const base::StringPiece& data, |
19 const DataSentCallback& callback, | 21 const DataSentCallback& callback, |
20 const SendErrorCallback& error_callback, | 22 const SendErrorCallback& error_callback, |
21 DataSender* sender); | 23 DataSender* sender); |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
73 pending_send->SendData(); | 75 pending_send->SendData(); |
74 sends_awaiting_ack_.push(pending_send); | 76 sends_awaiting_ack_.push(pending_send); |
75 return true; | 77 return true; |
76 } | 78 } |
77 | 79 |
78 bool DataSender::Cancel(int32_t error, const CancelCallback& callback) { | 80 bool DataSender::Cancel(int32_t error, const CancelCallback& callback) { |
79 DCHECK(!callback.is_null()); | 81 DCHECK(!callback.is_null()); |
80 if (!pending_cancel_.is_null() || shut_down_) | 82 if (!pending_cancel_.is_null() || shut_down_) |
81 return false; | 83 return false; |
82 if (sends_awaiting_ack_.empty()) { | 84 if (sends_awaiting_ack_.empty()) { |
83 base::MessageLoop::current()->PostTask(FROM_HERE, callback); | 85 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, callback); |
84 return true; | 86 return true; |
85 } | 87 } |
86 | 88 |
87 pending_cancel_ = callback; | 89 pending_cancel_ = callback; |
88 sink_->Cancel(error); | 90 sink_->Cancel(error); |
89 return true; | 91 return true; |
90 } | 92 } |
91 | 93 |
92 void DataSender::SendComplete() { | 94 void DataSender::SendComplete() { |
93 if (shut_down_) | 95 if (shut_down_) |
(...skipping 19 matching lines...) Expand all Loading... |
113 | 115 |
114 void DataSender::OnConnectionError() { | 116 void DataSender::OnConnectionError() { |
115 ShutDown(); | 117 ShutDown(); |
116 } | 118 } |
117 | 119 |
118 void DataSender::RunCancelCallback() { | 120 void DataSender::RunCancelCallback() { |
119 DCHECK(sends_awaiting_ack_.empty()); | 121 DCHECK(sends_awaiting_ack_.empty()); |
120 if (pending_cancel_.is_null()) | 122 if (pending_cancel_.is_null()) |
121 return; | 123 return; |
122 | 124 |
123 base::MessageLoop::current()->PostTask(FROM_HERE, | 125 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
124 base::Bind(pending_cancel_)); | 126 base::Bind(pending_cancel_)); |
125 pending_cancel_.Reset(); | 127 pending_cancel_.Reset(); |
126 } | 128 } |
127 | 129 |
128 void DataSender::ShutDown() { | 130 void DataSender::ShutDown() { |
129 shut_down_ = true; | 131 shut_down_ = true; |
130 while (!sends_awaiting_ack_.empty()) { | 132 while (!sends_awaiting_ack_.empty()) { |
131 sends_awaiting_ack_.front()->DispatchFatalError(); | 133 sends_awaiting_ack_.front()->DispatchFatalError(); |
132 sends_awaiting_ack_.pop(); | 134 sends_awaiting_ack_.pop(); |
133 } | 135 } |
134 RunCancelCallback(); | 136 RunCancelCallback(); |
135 } | 137 } |
136 | 138 |
137 DataSender::PendingSend::PendingSend(const base::StringPiece& data, | 139 DataSender::PendingSend::PendingSend(const base::StringPiece& data, |
138 const DataSentCallback& callback, | 140 const DataSentCallback& callback, |
139 const SendErrorCallback& error_callback, | 141 const SendErrorCallback& error_callback, |
140 DataSender* sender) | 142 DataSender* sender) |
141 : data_(data), | 143 : data_(data), |
142 callback_(callback), | 144 callback_(callback), |
143 error_callback_(error_callback), | 145 error_callback_(error_callback), |
144 sender_(sender) { | 146 sender_(sender) { |
145 } | 147 } |
146 | 148 |
147 void DataSender::PendingSend::OnDataSent(uint32_t num_bytes, int32_t error) { | 149 void DataSender::PendingSend::OnDataSent(uint32_t num_bytes, int32_t error) { |
148 if (error) { | 150 if (error) { |
149 base::MessageLoop::current()->PostTask( | 151 base::ThreadTaskRunnerHandle::Get()->PostTask( |
150 FROM_HERE, base::Bind(error_callback_, num_bytes, error)); | 152 FROM_HERE, base::Bind(error_callback_, num_bytes, error)); |
151 sender_->SendFailed(error); | 153 sender_->SendFailed(error); |
152 } else { | 154 } else { |
153 DCHECK(num_bytes == data_.size()); | 155 DCHECK(num_bytes == data_.size()); |
154 base::MessageLoop::current()->PostTask(FROM_HERE, | 156 base::ThreadTaskRunnerHandle::Get()->PostTask( |
155 base::Bind(callback_, num_bytes)); | 157 FROM_HERE, base::Bind(callback_, num_bytes)); |
156 sender_->SendComplete(); | 158 sender_->SendComplete(); |
157 } | 159 } |
158 } | 160 } |
159 | 161 |
160 void DataSender::PendingSend::DispatchFatalError() { | 162 void DataSender::PendingSend::DispatchFatalError() { |
161 base::MessageLoop::current()->PostTask( | 163 base::ThreadTaskRunnerHandle::Get()->PostTask( |
162 FROM_HERE, base::Bind(error_callback_, 0, sender_->fatal_error_value_)); | 164 FROM_HERE, base::Bind(error_callback_, 0, sender_->fatal_error_value_)); |
163 } | 165 } |
164 | 166 |
165 void DataSender::PendingSend::SendData() { | 167 void DataSender::PendingSend::SendData() { |
166 uint32_t num_bytes_to_send = static_cast<uint32_t>(data_.size()); | 168 uint32_t num_bytes_to_send = static_cast<uint32_t>(data_.size()); |
167 mojo::Array<uint8_t> bytes(num_bytes_to_send); | 169 mojo::Array<uint8_t> bytes(num_bytes_to_send); |
168 memcpy(&bytes[0], data_.data(), num_bytes_to_send); | 170 memcpy(&bytes[0], data_.data(), num_bytes_to_send); |
169 sender_->sink_->OnData( | 171 sender_->sink_->OnData( |
170 std::move(bytes), | 172 std::move(bytes), |
171 base::Bind(&DataSender::PendingSend::OnDataSent, base::Unretained(this))); | 173 base::Bind(&DataSender::PendingSend::OnDataSent, base::Unretained(this))); |
172 } | 174 } |
173 | 175 |
174 } // namespace device | 176 } // namespace device |
OLD | NEW |