| 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 "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/files/file_path.h" | 8 #include "base/files/file_path.h" |
| 9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
| 10 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
| 11 | 11 |
| 12 namespace device { | 12 namespace device { |
| 13 | 13 |
| 14 SerialIoHandler::SerialIoHandler( | 14 SerialIoHandler::SerialIoHandler( |
| 15 scoped_refptr<base::MessageLoopProxy> file_thread_message_loop) | 15 scoped_refptr<base::MessageLoopProxy> file_thread_message_loop) |
| 16 : file_thread_message_loop_(file_thread_message_loop) { | 16 : file_thread_message_loop_(file_thread_message_loop) { |
| 17 } | 17 } |
| 18 | 18 |
| 19 SerialIoHandler::~SerialIoHandler() { | 19 SerialIoHandler::~SerialIoHandler() { |
| 20 DCHECK(CalledOnValidThread()); | 20 DCHECK(CalledOnValidThread()); |
| 21 Close(); | 21 Close(); |
| 22 } | 22 } |
| 23 | 23 |
| 24 void SerialIoHandler::Open(const std::string& port, | 24 void SerialIoHandler::Open(const std::string& port, |
| 25 const OpenCompleteCallback& callback) { | 25 const OpenCompleteCallback& callback) { |
| 26 DCHECK(CalledOnValidThread()); | 26 DCHECK(CalledOnValidThread()); |
| 27 DCHECK(open_complete_.is_null()); | 27 DCHECK(open_complete_.is_null()); |
| 28 open_complete_ = callback; | 28 open_complete_ = callback; |
| 29 DCHECK(file_thread_message_loop_); | 29 DCHECK(file_thread_message_loop_.get()); |
| 30 file_thread_message_loop_->PostTask( | 30 file_thread_message_loop_->PostTask( |
| 31 FROM_HERE, | 31 FROM_HERE, |
| 32 base::Bind(&SerialIoHandler::StartOpen, | 32 base::Bind(&SerialIoHandler::StartOpen, |
| 33 this, | 33 this, |
| 34 port, | 34 port, |
| 35 base::MessageLoopProxy::current())); | 35 base::MessageLoopProxy::current())); |
| 36 } | 36 } |
| 37 | 37 |
| 38 void SerialIoHandler::StartOpen( | 38 void SerialIoHandler::StartOpen( |
| 39 const std::string& port, | 39 const std::string& port, |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 74 Close(); | 74 Close(); |
| 75 callback.Run(success); | 75 callback.Run(success); |
| 76 } | 76 } |
| 77 | 77 |
| 78 bool SerialIoHandler::PostOpen() { | 78 bool SerialIoHandler::PostOpen() { |
| 79 return true; | 79 return true; |
| 80 } | 80 } |
| 81 | 81 |
| 82 void SerialIoHandler::Close() { | 82 void SerialIoHandler::Close() { |
| 83 if (file_.IsValid()) { | 83 if (file_.IsValid()) { |
| 84 DCHECK(file_thread_message_loop_); | 84 DCHECK(file_thread_message_loop_.get()); |
| 85 file_thread_message_loop_->PostTask( | 85 file_thread_message_loop_->PostTask( |
| 86 FROM_HERE, base::Bind(&SerialIoHandler::DoClose, Passed(file_.Pass()))); | 86 FROM_HERE, base::Bind(&SerialIoHandler::DoClose, Passed(file_.Pass()))); |
| 87 } | 87 } |
| 88 } | 88 } |
| 89 | 89 |
| 90 // static | 90 // static |
| 91 void SerialIoHandler::DoClose(base::File port) { | 91 void SerialIoHandler::DoClose(base::File port) { |
| 92 // port closed by destructor. | 92 // port closed by destructor. |
| 93 } | 93 } |
| 94 | 94 |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 172 } | 172 } |
| 173 | 173 |
| 174 void SerialIoHandler::QueueWriteCompleted(int bytes_written, | 174 void SerialIoHandler::QueueWriteCompleted(int bytes_written, |
| 175 serial::SendError error) { | 175 serial::SendError error) { |
| 176 base::MessageLoop::current()->PostTask( | 176 base::MessageLoop::current()->PostTask( |
| 177 FROM_HERE, | 177 FROM_HERE, |
| 178 base::Bind(&SerialIoHandler::WriteCompleted, this, bytes_written, error)); | 178 base::Bind(&SerialIoHandler::WriteCompleted, this, bytes_written, error)); |
| 179 } | 179 } |
| 180 | 180 |
| 181 } // namespace device | 181 } // namespace device |
| OLD | NEW |