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