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/serial_io_handler.h" | 5 #include "device/serial/serial_io_handler.h" |
6 | 6 |
| 7 #include <utility> |
| 8 |
7 #include "base/bind.h" | 9 #include "base/bind.h" |
8 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
9 #include "base/message_loop/message_loop.h" | 11 #include "base/message_loop/message_loop.h" |
10 #include "base/strings/string_util.h" | 12 #include "base/strings/string_util.h" |
11 #include "build/build_config.h" | 13 #include "build/build_config.h" |
12 | 14 |
13 #if defined(OS_CHROMEOS) | 15 #if defined(OS_CHROMEOS) |
14 #include "chromeos/dbus/dbus_thread_manager.h" | 16 #include "chromeos/dbus/dbus_thread_manager.h" |
15 #include "chromeos/dbus/permission_broker_client.h" | 17 #include "chromeos/dbus/permission_broker_client.h" |
16 #include "dbus/file_descriptor.h" // nogncheck | 18 #include "dbus/file_descriptor.h" // nogncheck |
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
139 OpenCompleteCallback callback = open_complete_; | 141 OpenCompleteCallback callback = open_complete_; |
140 open_complete_.Reset(); | 142 open_complete_.Reset(); |
141 | 143 |
142 if (!file.IsValid()) { | 144 if (!file.IsValid()) { |
143 LOG(ERROR) << "Failed to open serial port: " | 145 LOG(ERROR) << "Failed to open serial port: " |
144 << base::File::ErrorToString(file.error_details()); | 146 << base::File::ErrorToString(file.error_details()); |
145 callback.Run(false); | 147 callback.Run(false); |
146 return; | 148 return; |
147 } | 149 } |
148 | 150 |
149 file_ = file.Pass(); | 151 file_ = std::move(file); |
150 | 152 |
151 bool success = PostOpen() && ConfigurePortImpl(); | 153 bool success = PostOpen() && ConfigurePortImpl(); |
152 if (!success) { | 154 if (!success) { |
153 Close(); | 155 Close(); |
154 } | 156 } |
155 | 157 |
156 callback.Run(success); | 158 callback.Run(success); |
157 } | 159 } |
158 | 160 |
159 bool SerialIoHandler::PostOpen() { | 161 bool SerialIoHandler::PostOpen() { |
160 return true; | 162 return true; |
161 } | 163 } |
162 | 164 |
163 void SerialIoHandler::Close() { | 165 void SerialIoHandler::Close() { |
164 if (file_.IsValid()) { | 166 if (file_.IsValid()) { |
165 DCHECK(file_thread_task_runner_.get()); | 167 DCHECK(file_thread_task_runner_.get()); |
166 file_thread_task_runner_->PostTask( | 168 file_thread_task_runner_->PostTask( |
167 FROM_HERE, base::Bind(&SerialIoHandler::DoClose, Passed(file_.Pass()))); | 169 FROM_HERE, |
| 170 base::Bind(&SerialIoHandler::DoClose, Passed(std::move(file_)))); |
168 } | 171 } |
169 } | 172 } |
170 | 173 |
171 // static | 174 // static |
172 void SerialIoHandler::DoClose(base::File port) { | 175 void SerialIoHandler::DoClose(base::File port) { |
173 // port closed by destructor. | 176 // port closed by destructor. |
174 } | 177 } |
175 | 178 |
176 void SerialIoHandler::Read(scoped_ptr<WritableBuffer> buffer) { | 179 void SerialIoHandler::Read(scoped_ptr<WritableBuffer> buffer) { |
177 DCHECK(CalledOnValidThread()); | 180 DCHECK(CalledOnValidThread()); |
178 DCHECK(!IsReadPending()); | 181 DCHECK(!IsReadPending()); |
179 pending_read_buffer_ = buffer.Pass(); | 182 pending_read_buffer_ = std::move(buffer); |
180 read_canceled_ = false; | 183 read_canceled_ = false; |
181 AddRef(); | 184 AddRef(); |
182 ReadImpl(); | 185 ReadImpl(); |
183 } | 186 } |
184 | 187 |
185 void SerialIoHandler::Write(scoped_ptr<ReadOnlyBuffer> buffer) { | 188 void SerialIoHandler::Write(scoped_ptr<ReadOnlyBuffer> buffer) { |
186 DCHECK(CalledOnValidThread()); | 189 DCHECK(CalledOnValidThread()); |
187 DCHECK(!IsWritePending()); | 190 DCHECK(!IsWritePending()); |
188 pending_write_buffer_ = buffer.Pass(); | 191 pending_write_buffer_ = std::move(buffer); |
189 write_canceled_ = false; | 192 write_canceled_ = false; |
190 AddRef(); | 193 AddRef(); |
191 WriteImpl(); | 194 WriteImpl(); |
192 } | 195 } |
193 | 196 |
194 void SerialIoHandler::ReadCompleted(int bytes_read, | 197 void SerialIoHandler::ReadCompleted(int bytes_read, |
195 serial::ReceiveError error) { | 198 serial::ReceiveError error) { |
196 DCHECK(CalledOnValidThread()); | 199 DCHECK(CalledOnValidThread()); |
197 DCHECK(IsReadPending()); | 200 DCHECK(IsReadPending()); |
198 scoped_ptr<WritableBuffer> pending_read_buffer = pending_read_buffer_.Pass(); | 201 scoped_ptr<WritableBuffer> pending_read_buffer = |
| 202 std::move(pending_read_buffer_); |
199 if (error == serial::RECEIVE_ERROR_NONE) { | 203 if (error == serial::RECEIVE_ERROR_NONE) { |
200 pending_read_buffer->Done(bytes_read); | 204 pending_read_buffer->Done(bytes_read); |
201 } else { | 205 } else { |
202 pending_read_buffer->DoneWithError(bytes_read, error); | 206 pending_read_buffer->DoneWithError(bytes_read, error); |
203 } | 207 } |
204 Release(); | 208 Release(); |
205 } | 209 } |
206 | 210 |
207 void SerialIoHandler::WriteCompleted(int bytes_written, | 211 void SerialIoHandler::WriteCompleted(int bytes_written, |
208 serial::SendError error) { | 212 serial::SendError error) { |
209 DCHECK(CalledOnValidThread()); | 213 DCHECK(CalledOnValidThread()); |
210 DCHECK(IsWritePending()); | 214 DCHECK(IsWritePending()); |
211 scoped_ptr<ReadOnlyBuffer> pending_write_buffer = | 215 scoped_ptr<ReadOnlyBuffer> pending_write_buffer = |
212 pending_write_buffer_.Pass(); | 216 std::move(pending_write_buffer_); |
213 if (error == serial::SEND_ERROR_NONE) { | 217 if (error == serial::SEND_ERROR_NONE) { |
214 pending_write_buffer->Done(bytes_written); | 218 pending_write_buffer->Done(bytes_written); |
215 } else { | 219 } else { |
216 pending_write_buffer->DoneWithError(bytes_written, error); | 220 pending_write_buffer->DoneWithError(bytes_written, error); |
217 } | 221 } |
218 Release(); | 222 Release(); |
219 } | 223 } |
220 | 224 |
221 bool SerialIoHandler::IsReadPending() const { | 225 bool SerialIoHandler::IsReadPending() const { |
222 DCHECK(CalledOnValidThread()); | 226 DCHECK(CalledOnValidThread()); |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
259 } | 263 } |
260 | 264 |
261 void SerialIoHandler::QueueWriteCompleted(int bytes_written, | 265 void SerialIoHandler::QueueWriteCompleted(int bytes_written, |
262 serial::SendError error) { | 266 serial::SendError error) { |
263 base::MessageLoop::current()->PostTask( | 267 base::MessageLoop::current()->PostTask( |
264 FROM_HERE, | 268 FROM_HERE, |
265 base::Bind(&SerialIoHandler::WriteCompleted, this, bytes_written, error)); | 269 base::Bind(&SerialIoHandler::WriteCompleted, this, bytes_written, error)); |
266 } | 270 } |
267 | 271 |
268 } // namespace device | 272 } // namespace device |
OLD | NEW |