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
3 // found in the LICENSE file.
5 #include "device/serial/serial_service_impl.h"
8 #include "base/location.h"
9 #include "device/serial/serial_io_handler.h"
13 SerialServiceImpl::SerialServiceImpl(
14 scoped_refptr
<SerialConnectionFactory
> connection_factory
)
15 : connection_factory_(connection_factory
) {
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
) {
25 SerialServiceImpl::~SerialServiceImpl() {
29 void SerialServiceImpl::Create(
30 scoped_refptr
<base::MessageLoopProxy
> io_message_loop
,
31 scoped_refptr
<base::MessageLoopProxy
> ui_message_loop
,
32 mojo::InterfaceRequest
<serial::SerialService
> request
) {
33 mojo::BindToRequest(new SerialServiceImpl(new SerialConnectionFactory(
34 base::Bind(SerialIoHandler::Create
,
35 base::MessageLoopProxy::current(),
42 void SerialServiceImpl::CreateOnMessageLoop(
43 scoped_refptr
<base::MessageLoopProxy
> message_loop
,
44 scoped_refptr
<base::MessageLoopProxy
> io_message_loop
,
45 scoped_refptr
<base::MessageLoopProxy
> ui_message_loop
,
46 mojo::InterfaceRequest
<serial::SerialService
> request
) {
47 message_loop
->PostTask(FROM_HERE
,
48 base::Bind(&SerialServiceImpl::Create
,
51 base::Passed(&request
)));
54 void SerialServiceImpl::GetDevices(
55 const mojo::Callback
<void(mojo::Array
<serial::DeviceInfoPtr
>)>& callback
) {
56 callback
.Run(GetDeviceEnumerator()->GetDevices());
59 void SerialServiceImpl::Connect(
60 const mojo::String
& path
,
61 serial::ConnectionOptionsPtr options
,
62 mojo::InterfaceRequest
<serial::Connection
> connection_request
,
63 mojo::InterfaceRequest
<serial::DataSink
> sink
,
64 mojo::InterfaceRequest
<serial::DataSource
> source
,
65 mojo::InterfacePtr
<serial::DataSourceClient
> source_client
) {
66 if (!IsValidPath(path
))
68 connection_factory_
->CreateConnection(path
, options
.Pass(),
69 connection_request
.Pass(), sink
.Pass(),
70 source
.Pass(), source_client
.Pass());
73 SerialDeviceEnumerator
* SerialServiceImpl::GetDeviceEnumerator() {
74 if (!device_enumerator_
)
75 device_enumerator_
= SerialDeviceEnumerator::Create();
76 return device_enumerator_
.get();
79 bool SerialServiceImpl::IsValidPath(const mojo::String
& path
) {
80 mojo::Array
<serial::DeviceInfoPtr
> devices(
81 GetDeviceEnumerator()->GetDevices());
82 for (size_t i
= 0; i
< devices
.size(); i
++) {
83 if (path
== devices
[i
]->path
)