Update the ICU's default timezone in browser/renderer
[chromium-blink-merge.git] / device / serial / serial_service_impl.cc
blob22734c89c9273e03d760043623cd60e65222368d
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 : 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() {
28 // static
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) {
33 mojo::BindToRequest(
34 new SerialServiceImpl(new SerialConnectionFactory(
35 base::Bind(SerialIoHandler::Create,
36 base::ThreadTaskRunnerHandle::Get(), ui_task_runner),
37 io_task_runner)),
38 &request);
41 // static
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))
65 return;
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)
82 return true;
84 return false;
87 } // namespace device