Upload upstream chromium 108.0.5359.1
[platform/framework/web/chromium-efl.git] / services / device / serial / serial_io_handler_win.h
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.
4
5 #ifndef SERVICES_DEVICE_SERIAL_SERIAL_IO_HANDLER_WIN_H_
6 #define SERVICES_DEVICE_SERIAL_SERIAL_IO_HANDLER_WIN_H_
7
8 #include <memory>
9 #include <string>
10
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"
16
17 namespace device {
18
19 class SerialIoHandlerWin : public SerialIoHandler,
20                            public base::MessagePumpForIO::IOHandler {
21  public:
22   SerialIoHandlerWin(const SerialIoHandlerWin&) = delete;
23   SerialIoHandlerWin& operator=(const SerialIoHandlerWin&) = delete;
24
25  protected:
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;
39
40  private:
41   class UiThreadHelper;
42   friend class SerialIoHandler;
43
44   explicit SerialIoHandlerWin(
45       const base::FilePath& port,
46       scoped_refptr<base::SingleThreadTaskRunner> ui_thread_task_runner);
47   ~SerialIoHandlerWin() override;
48
49   // base::MessagePumpForIO::IOHandler implementation.
50   void OnIOCompleted(base::MessagePumpForIO::IOContext* context,
51                      DWORD bytes_transfered,
52                      DWORD error) override;
53
54   void ClearPendingError();
55   void OnDeviceRemoved(const std::wstring& device_path);
56
57   // Context used for overlapped reads.
58   std::unique_ptr<base::MessagePumpForIO::IOContext> read_context_;
59
60   // Context used for overlapped writes.
61   std::unique_ptr<base::MessagePumpForIO::IOContext> write_context_;
62
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};
67 };
68
69 }  // namespace device
70
71 #endif  // SERVICES_DEVICE_SERIAL_SERIAL_IO_HANDLER_WIN_H_