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::SingleThreadTaskRunner
> io_task_runner
,
31 scoped_refptr
<base::SingleThreadTaskRunner
> ui_task_runner
,
32 mojo::InterfaceRequest
<serial::SerialService
> request
) {
34 new SerialServiceImpl(new SerialConnectionFactory(
35 base::Bind(SerialIoHandler::Create
,
36 base::ThreadTaskRunnerHandle::Get(), ui_task_runner
),
42 void SerialServiceImpl::CreateOnMessageLoop(
43 scoped_refptr
<base::SingleThreadTaskRunner
> task_runner
,
44 scoped_refptr
<base::SingleThreadTaskRunner
> io_task_runner
,
45 scoped_refptr
<base::SingleThreadTaskRunner
> ui_task_runner
,
46 mojo::InterfaceRequest
<serial::SerialService
> request
) {
47 task_runner
->PostTask(FROM_HERE
,
48 base::Bind(&SerialServiceImpl::Create
, io_task_runner
,
49 ui_task_runner
, base::Passed(&request
)));
52 void SerialServiceImpl::GetDevices(
53 const mojo::Callback
<void(mojo::Array
<serial::DeviceInfoPtr
>)>& callback
) {
54 callback
.Run(GetDeviceEnumerator()->GetDevices());
57 void SerialServiceImpl::Connect(
58 const mojo::String
& path
,
59 serial::ConnectionOptionsPtr options
,
60 mojo::InterfaceRequest
<serial::Connection
> connection_request
,
61 mojo::InterfaceRequest
<serial::DataSink
> sink
,
62 mojo::InterfaceRequest
<serial::DataSource
> source
,
63 mojo::InterfacePtr
<serial::DataSourceClient
> source_client
) {
64 if (!IsValidPath(path
))
66 connection_factory_
->CreateConnection(path
, options
.Pass(),
67 connection_request
.Pass(), sink
.Pass(),
68 source
.Pass(), source_client
.Pass());
71 SerialDeviceEnumerator
* SerialServiceImpl::GetDeviceEnumerator() {
72 if (!device_enumerator_
)
73 device_enumerator_
= SerialDeviceEnumerator::Create();
74 return device_enumerator_
.get();
77 bool SerialServiceImpl::IsValidPath(const mojo::String
& path
) {
78 mojo::Array
<serial::DeviceInfoPtr
> devices(
79 GetDeviceEnumerator()->GetDevices());
80 for (size_t i
= 0; i
< devices
.size(); i
++) {
81 if (path
== devices
[i
]->path
)