1 // Copyright 2017 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef SERVICES_DEVICE_SERIAL_SERIAL_PORT_MANAGER_IMPL_H_
6 #define SERVICES_DEVICE_SERIAL_SERIAL_PORT_MANAGER_IMPL_H_
10 #include "base/memory/ref_counted.h"
11 #include "base/memory/weak_ptr.h"
12 #include "base/scoped_multi_source_observation.h"
13 #include "base/sequence_checker.h"
14 #include "mojo/public/cpp/bindings/pending_receiver.h"
15 #include "mojo/public/cpp/bindings/pending_remote.h"
16 #include "mojo/public/cpp/bindings/receiver_set.h"
17 #include "mojo/public/cpp/bindings/remote_set.h"
18 #include "services/device/public/mojom/serial.mojom.h"
19 #include "services/device/serial/bluetooth_serial_device_enumerator.h"
20 #include "services/device/serial/bluetooth_serial_port_impl.h"
21 #include "services/device/serial/serial_device_enumerator.h"
24 class SingleThreadTaskRunner;
25 class UnguessableToken;
32 // TODO(leonhsl): Merge this class with SerialDeviceEnumerator if/once
33 // SerialDeviceEnumerator is exposed only via the Device Service.
37 // 1. Created on the UI thread.
38 // 2. Used on the UI thread runner (macOS only), otherwise on a blocking task
40 // 3. Deleted on the same runner on which it is used *except* sometimes
41 // during shutdown when the runner threadpool is already shutdown.
42 // See crbug.com/1263149#c20 for details.
43 class SerialPortManagerImpl : public mojom::SerialPortManager,
44 public SerialDeviceEnumerator::Observer {
46 SerialPortManagerImpl(
47 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner,
48 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner);
50 SerialPortManagerImpl(const SerialPortManagerImpl&) = delete;
51 SerialPortManagerImpl& operator=(const SerialPortManagerImpl&) = delete;
53 ~SerialPortManagerImpl() override;
55 void Bind(mojo::PendingReceiver<mojom::SerialPortManager> receiver);
56 void SetSerialEnumeratorForTesting(
57 std::unique_ptr<SerialDeviceEnumerator> fake_enumerator);
58 void SetBluetoothSerialEnumeratorForTesting(
59 std::unique_ptr<BluetoothSerialDeviceEnumerator>
60 fake_bluetooth_enumerator);
63 // mojom::SerialPortManager methods:
65 mojo::PendingRemote<mojom::SerialPortManagerClient> client) override;
66 void GetDevices(GetDevicesCallback callback) override;
67 void OpenPort(const base::UnguessableToken& token,
68 bool use_alternate_path,
69 device::mojom::SerialConnectionOptionsPtr options,
70 mojo::PendingRemote<mojom::SerialPortClient> client,
71 mojo::PendingRemote<mojom::SerialPortConnectionWatcher> watcher,
72 OpenPortCallback callback) override;
74 // SerialDeviceEnumerator::Observer methods:
75 void OnPortAdded(const mojom::SerialPortInfo& port) override;
76 void OnPortRemoved(const mojom::SerialPortInfo& port) override;
78 void OpenBluetoothSerialPortOnUI(
79 const std::string& address,
80 const BluetoothUUID& service_class_id,
81 mojom::SerialConnectionOptionsPtr options,
82 mojo::PendingRemote<mojom::SerialPortClient> client,
83 mojo::PendingRemote<mojom::SerialPortConnectionWatcher> watcher,
84 BluetoothSerialPortImpl::OpenCallback callback);
86 std::unique_ptr<SerialDeviceEnumerator> enumerator_;
87 std::unique_ptr<BluetoothSerialDeviceEnumerator> bluetooth_enumerator_;
88 base::ScopedMultiSourceObservation<SerialDeviceEnumerator,
89 SerialDeviceEnumerator::Observer>
90 observed_enumerator_{this};
92 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_;
93 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner_;
95 mojo::ReceiverSet<SerialPortManager> receivers_;
96 mojo::RemoteSet<mojom::SerialPortManagerClient> clients_;
97 // See threading notes above for guidelines for checking sequence.
98 SEQUENCE_CHECKER(sequence_checker_);
99 base::WeakPtrFactory<SerialPortManagerImpl> weak_factory_{this};
102 } // namespace device
104 #endif // SERVICES_DEVICE_SERIAL_SERIAL_PORT_MANAGER_IMPL_H_