1 // Copyright 2014 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_IO_HANDLER_WIN_H_
6 #define SERVICES_DEVICE_SERIAL_SERIAL_IO_HANDLER_WIN_H_
11 #include "base/memory/raw_ptr.h"
12 #include "base/message_loop/message_pump_for_io.h"
13 #include "base/task/single_thread_task_runner.h"
14 #include "base/threading/thread_task_runner_handle.h"
15 #include "services/device/serial/serial_io_handler.h"
19 class SerialIoHandlerWin : public SerialIoHandler,
20 public base::MessagePumpForIO::IOHandler {
22 SerialIoHandlerWin(const SerialIoHandlerWin&) = delete;
23 SerialIoHandlerWin& operator=(const SerialIoHandlerWin&) = delete;
26 // SerialIoHandler implementation.
27 void ReadImpl() override;
28 void WriteImpl() override;
29 void CancelReadImpl() override;
30 void CancelWriteImpl() override;
31 bool ConfigurePortImpl() override;
32 void Flush(mojom::SerialPortFlushMode mode) const override;
33 void Drain() override;
34 mojom::SerialPortControlSignalsPtr GetControlSignals() const override;
35 bool SetControlSignals(
36 const mojom::SerialHostControlSignals& control_signals) override;
37 mojom::SerialConnectionInfoPtr GetPortInfo() const override;
38 bool PostOpen() override;
42 friend class SerialIoHandler;
44 explicit SerialIoHandlerWin(
45 const base::FilePath& port,
46 scoped_refptr<base::SingleThreadTaskRunner> ui_thread_task_runner);
47 ~SerialIoHandlerWin() override;
49 // base::MessagePumpForIO::IOHandler implementation.
50 void OnIOCompleted(base::MessagePumpForIO::IOContext* context,
51 DWORD bytes_transfered,
52 DWORD error) override;
54 void ClearPendingError();
55 void OnDeviceRemoved(const std::wstring& device_path);
57 // Context used for overlapped reads.
58 std::unique_ptr<base::MessagePumpForIO::IOContext> read_context_;
60 // Context used for overlapped writes.
61 std::unique_ptr<base::MessagePumpForIO::IOContext> write_context_;
63 // The helper lives on the UI thread and holds a weak reference back to the
64 // handler that owns it.
65 raw_ptr<UiThreadHelper> helper_ = nullptr;
66 base::WeakPtrFactory<SerialIoHandlerWin> weak_factory_{this};
71 #endif // SERVICES_DEVICE_SERIAL_SERIAL_IO_HANDLER_WIN_H_