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_service_impl.h" | 5 #include "device/serial/serial_service_impl.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/location.h" | 8 #include "base/location.h" |
| 9 #include "device/serial/serial_io_handler.h" |
9 | 10 |
10 namespace device { | 11 namespace device { |
11 | 12 |
12 SerialServiceImpl::SerialServiceImpl() { | 13 SerialServiceImpl::SerialServiceImpl( |
| 14 scoped_refptr<SerialConnectionFactory> connection_factory) |
| 15 : connection_factory_(connection_factory) { |
| 16 } |
| 17 |
| 18 SerialServiceImpl::SerialServiceImpl( |
| 19 scoped_refptr<SerialConnectionFactory> connection_factory, |
| 20 scoped_ptr<SerialDeviceEnumerator> device_enumerator) |
| 21 : device_enumerator_(device_enumerator.Pass()), |
| 22 connection_factory_(connection_factory) { |
13 } | 23 } |
14 | 24 |
15 SerialServiceImpl::~SerialServiceImpl() { | 25 SerialServiceImpl::~SerialServiceImpl() { |
16 } | 26 } |
17 | 27 |
18 // static | 28 // static |
19 void SerialServiceImpl::Create( | 29 void SerialServiceImpl::Create( |
| 30 scoped_refptr<base::MessageLoopProxy> io_message_loop, |
20 mojo::InterfaceRequest<serial::SerialService> request) { | 31 mojo::InterfaceRequest<serial::SerialService> request) { |
21 mojo::BindToRequest(new SerialServiceImpl(), &request); | 32 mojo::BindToRequest(new SerialServiceImpl(new SerialConnectionFactory( |
| 33 base::Bind(SerialIoHandler::Create, |
| 34 base::MessageLoopProxy::current()), |
| 35 io_message_loop)), |
| 36 &request); |
22 } | 37 } |
23 | 38 |
24 // static | 39 // static |
25 void SerialServiceImpl::CreateOnMessageLoop( | 40 void SerialServiceImpl::CreateOnMessageLoop( |
26 scoped_refptr<base::MessageLoopProxy> message_loop, | 41 scoped_refptr<base::MessageLoopProxy> message_loop, |
| 42 scoped_refptr<base::MessageLoopProxy> io_message_loop, |
27 mojo::InterfaceRequest<serial::SerialService> request) { | 43 mojo::InterfaceRequest<serial::SerialService> request) { |
28 message_loop->PostTask( | 44 message_loop->PostTask( |
29 FROM_HERE, | 45 FROM_HERE, |
30 base::Bind(&SerialServiceImpl::Create, base::Passed(&request))); | 46 base::Bind( |
| 47 &SerialServiceImpl::Create, io_message_loop, base::Passed(&request))); |
31 } | 48 } |
32 | 49 |
33 void SerialServiceImpl::GetDevices( | 50 void SerialServiceImpl::GetDevices( |
34 const mojo::Callback<void(mojo::Array<serial::DeviceInfoPtr>)>& callback) { | 51 const mojo::Callback<void(mojo::Array<serial::DeviceInfoPtr>)>& callback) { |
35 if (!device_enumerator_) | 52 callback.Run(GetDeviceEnumerator()->GetDevices()); |
36 device_enumerator_ = SerialDeviceEnumerator::Create(); | 53 } |
37 callback.Run(device_enumerator_->GetDevices()); | 54 |
| 55 void SerialServiceImpl::Connect( |
| 56 const mojo::String& path, |
| 57 serial::ConnectionOptionsPtr options, |
| 58 mojo::InterfaceRequest<serial::Connection> connection_request) { |
| 59 if (!IsValidPath(path)) |
| 60 return; |
| 61 connection_factory_->CreateConnection( |
| 62 path, options.Pass(), connection_request.Pass()); |
38 } | 63 } |
39 | 64 |
40 void SerialServiceImpl::OnConnectionError() { | 65 void SerialServiceImpl::OnConnectionError() { |
41 delete this; | 66 delete this; |
42 } | 67 } |
43 | 68 |
| 69 SerialDeviceEnumerator* SerialServiceImpl::GetDeviceEnumerator() { |
| 70 if (!device_enumerator_) |
| 71 device_enumerator_ = SerialDeviceEnumerator::Create(); |
| 72 return device_enumerator_.get(); |
| 73 } |
| 74 |
| 75 bool SerialServiceImpl::IsValidPath(const mojo::String& path) { |
| 76 mojo::Array<serial::DeviceInfoPtr> devices( |
| 77 GetDeviceEnumerator()->GetDevices()); |
| 78 for (size_t i = 0; i < devices.size(); i++) { |
| 79 if (path == devices[i]->path) |
| 80 return true; |
| 81 } |
| 82 return false; |
| 83 } |
| 84 |
| 85 void SerialServiceImpl::OnConnected( |
| 86 const mojo::Callback<void(serial::ConnectionInfoPtr)>& callback, |
| 87 serial::ConnectionInfoPtr result) { |
| 88 callback.Run(result.Pass()); |
| 89 } |
| 90 |
44 } // namespace device | 91 } // namespace device |
OLD | NEW |