// Copyright 2014 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #include "device/serial/serial_service_impl.h" #include #include #include "base/bind.h" #include "base/location.h" #include "device/serial/serial_io_handler.h" namespace device { SerialServiceImpl::SerialServiceImpl( scoped_refptr connection_factory, mojo::InterfaceRequest request) : connection_factory_(connection_factory), binding_(this, std::move(request)) {} SerialServiceImpl::SerialServiceImpl( scoped_refptr connection_factory, scoped_ptr device_enumerator, mojo::InterfaceRequest request) : device_enumerator_(std::move(device_enumerator)), connection_factory_(connection_factory), binding_(this, std::move(request)) {} SerialServiceImpl::~SerialServiceImpl() { } // static void SerialServiceImpl::Create( scoped_refptr io_task_runner, scoped_refptr ui_task_runner, mojo::InterfaceRequest request) { new SerialServiceImpl( new SerialConnectionFactory( base::Bind(SerialIoHandler::Create, base::ThreadTaskRunnerHandle::Get(), ui_task_runner), io_task_runner), std::move(request)); } // static void SerialServiceImpl::CreateOnMessageLoop( scoped_refptr task_runner, scoped_refptr io_task_runner, scoped_refptr ui_task_runner, mojo::InterfaceRequest request) { task_runner->PostTask(FROM_HERE, base::Bind(&SerialServiceImpl::Create, io_task_runner, ui_task_runner, base::Passed(&request))); } void SerialServiceImpl::GetDevices( const mojo::Callback)>& callback) { callback.Run(GetDeviceEnumerator()->GetDevices()); } void SerialServiceImpl::Connect( const mojo::String& path, serial::ConnectionOptionsPtr options, mojo::InterfaceRequest connection_request, mojo::InterfaceRequest sink, mojo::InterfaceRequest source, mojo::InterfacePtr source_client) { if (!IsValidPath(path)) return; connection_factory_->CreateConnection( path, std::move(options), std::move(connection_request), std::move(sink), std::move(source), std::move(source_client)); } SerialDeviceEnumerator* SerialServiceImpl::GetDeviceEnumerator() { if (!device_enumerator_) device_enumerator_ = SerialDeviceEnumerator::Create(); return device_enumerator_.get(); } bool SerialServiceImpl::IsValidPath(const mojo::String& path) { mojo::Array devices( GetDeviceEnumerator()->GetDevices()); for (size_t i = 0; i < devices.size(); i++) { if (path == devices[i]->path) return true; } return false; } } // namespace device