summaryrefslogtreecommitdiffstats
path: root/device/serial/serial_service_impl.cc
blob: 14a8033f4ed48e06e22c4e1f03abb6341ac9990c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// 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 "base/bind.h"
#include "base/location.h"
#include "device/serial/serial_io_handler.h"

namespace device {

SerialServiceImpl::SerialServiceImpl(
    scoped_refptr<SerialConnectionFactory> connection_factory)
    : connection_factory_(connection_factory) {
}

SerialServiceImpl::SerialServiceImpl(
    scoped_refptr<SerialConnectionFactory> connection_factory,
    scoped_ptr<SerialDeviceEnumerator> device_enumerator)
    : device_enumerator_(device_enumerator.Pass()),
      connection_factory_(connection_factory) {
}

SerialServiceImpl::~SerialServiceImpl() {
}

// static
void SerialServiceImpl::Create(
    scoped_refptr<base::MessageLoopProxy> io_message_loop,
    mojo::InterfaceRequest<serial::SerialService> request) {
  mojo::BindToRequest(new SerialServiceImpl(new SerialConnectionFactory(
                          base::Bind(SerialIoHandler::Create,
                                     base::MessageLoopProxy::current()),
                          io_message_loop)),
                      &request);
}

// static
void SerialServiceImpl::CreateOnMessageLoop(
    scoped_refptr<base::MessageLoopProxy> message_loop,
    scoped_refptr<base::MessageLoopProxy> io_message_loop,
    mojo::InterfaceRequest<serial::SerialService> request) {
  message_loop->PostTask(
      FROM_HERE,
      base::Bind(
          &SerialServiceImpl::Create, io_message_loop, base::Passed(&request)));
}

void SerialServiceImpl::GetDevices(
    const mojo::Callback<void(mojo::Array<serial::DeviceInfoPtr>)>& callback) {
  callback.Run(GetDeviceEnumerator()->GetDevices());
}

void SerialServiceImpl::Connect(
    const mojo::String& path,
    serial::ConnectionOptionsPtr options,
    mojo::InterfaceRequest<serial::Connection> connection_request) {
  if (!IsValidPath(path))
    return;
  connection_factory_->CreateConnection(
      path, options.Pass(), connection_request.Pass());
}

void SerialServiceImpl::OnConnectionError() {
  delete this;
}

SerialDeviceEnumerator* SerialServiceImpl::GetDeviceEnumerator() {
  if (!device_enumerator_)
    device_enumerator_ = SerialDeviceEnumerator::Create();
  return device_enumerator_.get();
}

bool SerialServiceImpl::IsValidPath(const mojo::String& path) {
  mojo::Array<serial::DeviceInfoPtr> devices(
      GetDeviceEnumerator()->GetDevices());
  for (size_t i = 0; i < devices.size(); i++) {
    if (path == devices[i]->path)
      return true;
  }
  return false;
}

void SerialServiceImpl::OnConnected(
    const mojo::Callback<void(serial::ConnectionInfoPtr)>& callback,
    serial::ConnectionInfoPtr result) {
  callback.Run(result.Pass());
}

}  // namespace device