Vectorize website settings icons in omnibox
[chromium-blink-merge.git] / device / serial / serial_service_impl.cc
blob98ae1e02756bafce6e7f4ba4b4f84c602b3a6491
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"
7 #include "base/bind.h"
8 #include "base/location.h"
9 #include "device/serial/serial_io_handler.h"
11 namespace device {
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() {
31 // static
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),
40 io_task_runner),
41 request.Pass());
44 // static
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))
68 return;
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)
85 return true;
87 return false;
90 } // namespace device