1 // Copyright 2017 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 "services/device/serial/serial_port_manager_impl.h"
10 #include "base/sequenced_task_runner.h"
11 #include "mojo/public/cpp/bindings/strong_binding.h"
12 #include "services/device/serial/serial_device_enumerator.h"
13 #include "services/device/serial/serial_port_impl.h"
17 SerialPortManagerImpl::SerialPortManagerImpl(
18 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner,
19 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner)
20 : io_task_runner_(std::move(io_task_runner)),
21 ui_task_runner_(std::move(ui_task_runner)) {}
23 SerialPortManagerImpl::~SerialPortManagerImpl() = default;
25 void SerialPortManagerImpl::Bind(mojom::SerialPortManagerRequest request) {
26 bindings_.AddBinding(this, std::move(request));
29 void SerialPortManagerImpl::SetSerialEnumeratorForTesting(
30 std::unique_ptr<SerialDeviceEnumerator> fake_enumerator) {
31 DCHECK(fake_enumerator);
32 enumerator_ = std::move(fake_enumerator);
35 void SerialPortManagerImpl::GetDevices(GetDevicesCallback callback) {
37 enumerator_ = SerialDeviceEnumerator::Create();
38 std::move(callback).Run(enumerator_->GetDevices());
41 void SerialPortManagerImpl::GetPort(const base::UnguessableToken& token,
42 mojom::SerialPortRequest request) {
44 enumerator_ = SerialDeviceEnumerator::Create();
45 base::Optional<base::FilePath> path = enumerator_->GetPathFromToken(token);
47 io_task_runner_->PostTask(
48 FROM_HERE, base::BindOnce(&SerialPortImpl::Create, *path,
49 std::move(request), ui_task_runner_));