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 mojo::InterfaceRequest
<serial::SerialService
> request
)
16 : connection_factory_(connection_factory
), binding_(this, request
.Pass()) {
19 SerialServiceImpl::SerialServiceImpl(
20 scoped_refptr
<SerialConnectionFactory
> connection_factory
,
21 scoped_ptr
<SerialDeviceEnumerator
> device_enumerator
,
22 mojo::InterfaceRequest
<serial::SerialService
> request
)
23 : device_enumerator_(device_enumerator
.Pass()),
24 connection_factory_(connection_factory
),
25 binding_(this, request
.Pass()) {
28 SerialServiceImpl::~SerialServiceImpl() {
32 void SerialServiceImpl::Create(
33 scoped_refptr
<base::SingleThreadTaskRunner
> io_task_runner
,
34 scoped_refptr
<base::SingleThreadTaskRunner
> ui_task_runner
,
35 mojo::InterfaceRequest
<serial::SerialService
> request
) {
36 new SerialServiceImpl(
37 new SerialConnectionFactory(
38 base::Bind(SerialIoHandler::Create
,
39 base::ThreadTaskRunnerHandle::Get(), ui_task_runner
),
45 void SerialServiceImpl::CreateOnMessageLoop(
46 scoped_refptr
<base::SingleThreadTaskRunner
> task_runner
,
47 scoped_refptr
<base::SingleThreadTaskRunner
> io_task_runner
,
48 scoped_refptr
<base::SingleThreadTaskRunner
> ui_task_runner
,
49 mojo::InterfaceRequest
<serial::SerialService
> request
) {
50 task_runner
->PostTask(FROM_HERE
,
51 base::Bind(&SerialServiceImpl::Create
, io_task_runner
,
52 ui_task_runner
, base::Passed(&request
)));
55 void SerialServiceImpl::GetDevices(
56 const mojo::Callback
<void(mojo::Array
<serial::DeviceInfoPtr
>)>& callback
) {
57 callback
.Run(GetDeviceEnumerator()->GetDevices());
60 void SerialServiceImpl::Connect(
61 const mojo::String
& path
,
62 serial::ConnectionOptionsPtr options
,
63 mojo::InterfaceRequest
<serial::Connection
> connection_request
,
64 mojo::InterfaceRequest
<serial::DataSink
> sink
,
65 mojo::InterfaceRequest
<serial::DataSource
> source
,
66 mojo::InterfacePtr
<serial::DataSourceClient
> source_client
) {
67 if (!IsValidPath(path
))
69 connection_factory_
->CreateConnection(path
, options
.Pass(),
70 connection_request
.Pass(), sink
.Pass(),
71 source
.Pass(), source_client
.Pass());
74 SerialDeviceEnumerator
* SerialServiceImpl::GetDeviceEnumerator() {
75 if (!device_enumerator_
)
76 device_enumerator_
= SerialDeviceEnumerator::Create();
77 return device_enumerator_
.get();
80 bool SerialServiceImpl::IsValidPath(const mojo::String
& path
) {
81 mojo::Array
<serial::DeviceInfoPtr
> devices(
82 GetDeviceEnumerator()->GetDevices());
83 for (size_t i
= 0; i
< devices
.size(); i
++) {
84 if (path
== devices
[i
]->path
)