Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / device / serial / serial_service_impl.cc
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     scoped_refptr<base::MessageLoopProxy> ui_message_loop,
32     mojo::InterfaceRequest<serial::SerialService> request) {
33   mojo::BindToRequest(new SerialServiceImpl(new SerialConnectionFactory(
34                           base::Bind(SerialIoHandler::Create,
35                                      base::MessageLoopProxy::current(),
36                                      ui_message_loop),
37                           io_message_loop)),
38                       &request);
39 }
40
41 // static
42 void SerialServiceImpl::CreateOnMessageLoop(
43     scoped_refptr<base::MessageLoopProxy> message_loop,
44     scoped_refptr<base::MessageLoopProxy> io_message_loop,
45     scoped_refptr<base::MessageLoopProxy> ui_message_loop,
46     mojo::InterfaceRequest<serial::SerialService> request) {
47   message_loop->PostTask(FROM_HERE,
48                          base::Bind(&SerialServiceImpl::Create,
49                                     io_message_loop,
50                                     ui_message_loop,
51                                     base::Passed(&request)));
52 }
53
54 void SerialServiceImpl::GetDevices(
55     const mojo::Callback<void(mojo::Array<serial::DeviceInfoPtr>)>& callback) {
56   callback.Run(GetDeviceEnumerator()->GetDevices());
57 }
58
59 void SerialServiceImpl::Connect(
60     const mojo::String& path,
61     serial::ConnectionOptionsPtr options,
62     mojo::InterfaceRequest<serial::Connection> connection_request,
63     mojo::InterfaceRequest<serial::DataSink> sink,
64     mojo::InterfaceRequest<serial::DataSource> source) {
65   if (!IsValidPath(path))
66     return;
67   connection_factory_->CreateConnection(path,
68                                         options.Pass(),
69                                         connection_request.Pass(),
70                                         sink.Pass(),
71                                         source.Pass());
72 }
73
74 SerialDeviceEnumerator* SerialServiceImpl::GetDeviceEnumerator() {
75   if (!device_enumerator_)
76     device_enumerator_ = SerialDeviceEnumerator::Create();
77   return device_enumerator_.get();
78 }
79
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;
86   }
87   return false;
88 }
89
90 }  // namespace device