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_.get()); | 29 DCHECK(file_thread_message_loop_.get()); |
30 file_thread_message_loop_->PostTask( | 30 RequestAccess(port, file_thread_message_loop_); |
31 FROM_HERE, | 31 } |
32 base::Bind(&SerialIoHandler::StartOpen, | 32 |
33 this, | 33 void SerialIoHandler::RequestAccess( |
34 port, | 34 const std::string& port, |
35 base::MessageLoopProxy::current())); | 35 scoped_refptr<base::MessageLoopProxy> io_message_loop) { |
| 36 OnRequestAccessComplete(port, true /* success */); |
| 37 } |
| 38 |
| 39 void SerialIoHandler::OnRequestAccessComplete(const std::string& port, |
| 40 bool success) { |
| 41 DCHECK(CalledOnValidThread()); |
| 42 if (success) { |
| 43 DCHECK(file_thread_message_loop_.get()); |
| 44 file_thread_message_loop_->PostTask( |
| 45 FROM_HERE, |
| 46 base::Bind(&SerialIoHandler::StartOpen, |
| 47 this, |
| 48 port, |
| 49 base::MessageLoopProxy::current())); |
| 50 return; |
| 51 } else { |
| 52 DCHECK(!open_complete_.is_null()); |
| 53 OpenCompleteCallback callback = open_complete_; |
| 54 open_complete_.Reset(); |
| 55 callback.Run(false); |
| 56 return; |
| 57 } |
36 } | 58 } |
37 | 59 |
38 void SerialIoHandler::StartOpen( | 60 void SerialIoHandler::StartOpen( |
39 const std::string& port, | 61 const std::string& port, |
40 scoped_refptr<base::MessageLoopProxy> io_message_loop) { | 62 scoped_refptr<base::MessageLoopProxy> io_message_loop) { |
41 DCHECK(!open_complete_.is_null()); | 63 DCHECK(!open_complete_.is_null()); |
42 DCHECK(file_thread_message_loop_->RunsTasksOnCurrentThread()); | 64 DCHECK(file_thread_message_loop_->RunsTasksOnCurrentThread()); |
43 DCHECK(!file_.IsValid()); | 65 DCHECK(!file_.IsValid()); |
44 // It's the responsibility of the API wrapper around SerialIoHandler to | 66 // It's the responsibility of the API wrapper around SerialIoHandler to |
45 // validate the supplied path against the set of valid port names, and | 67 // validate the supplied path against the set of valid port names, and |
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
173 } | 195 } |
174 | 196 |
175 void SerialIoHandler::QueueWriteCompleted(int bytes_written, | 197 void SerialIoHandler::QueueWriteCompleted(int bytes_written, |
176 serial::SendError error) { | 198 serial::SendError error) { |
177 base::MessageLoop::current()->PostTask( | 199 base::MessageLoop::current()->PostTask( |
178 FROM_HERE, | 200 FROM_HERE, |
179 base::Bind(&SerialIoHandler::WriteCompleted, this, bytes_written, error)); | 201 base::Bind(&SerialIoHandler::WriteCompleted, this, bytes_written, error)); |
180 } | 202 } |
181 | 203 |
182 } // namespace device | 204 } // namespace device |
OLD | NEW |