Upload upstream chromium 69.0.3497
[platform/framework/web/chromium-efl.git] / device / serial / serial_io_handler_win.h
1 // Copyright 2014 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.
4
5 #ifndef DEVICE_SERIAL_SERIAL_IO_HANDLER_WIN_H_
6 #define DEVICE_SERIAL_SERIAL_IO_HANDLER_WIN_H_
7
8 #include <memory>
9
10 #include "base/macros.h"
11 #include "base/message_loop/message_pump_for_io.h"
12 #include "base/single_thread_task_runner.h"
13 #include "base/threading/thread_task_runner_handle.h"
14 #include "device/serial/serial_io_handler.h"
15
16 namespace device {
17
18 class SerialIoHandlerWin : public SerialIoHandler,
19                            public base::MessagePumpForIO::IOHandler {
20  protected:
21   // SerialIoHandler implementation.
22   void ReadImpl() override;
23   void WriteImpl() override;
24   void CancelReadImpl() override;
25   void CancelWriteImpl() override;
26   bool ConfigurePortImpl() override;
27   bool Flush() const override;
28   mojom::SerialDeviceControlSignalsPtr GetControlSignals() const override;
29   bool SetControlSignals(
30       const mojom::SerialHostControlSignals& control_signals) override;
31   mojom::SerialConnectionInfoPtr GetPortInfo() const override;
32   bool SetBreak() override;
33   bool ClearBreak() override;
34   bool PostOpen() override;
35
36  private:
37   class UiThreadHelper;
38   friend class SerialIoHandler;
39
40   explicit SerialIoHandlerWin(
41       scoped_refptr<base::SingleThreadTaskRunner> ui_thread_task_runner);
42   ~SerialIoHandlerWin() override;
43
44   // base::MessagePumpForIO::IOHandler implementation.
45   void OnIOCompleted(base::MessagePumpForIO::IOContext* context,
46                      DWORD bytes_transfered,
47                      DWORD error) override;
48
49   void OnDeviceRemoved(const std::string& device_path);
50
51   // Context used for asynchronous WaitCommEvent calls.
52   std::unique_ptr<base::MessagePumpForIO::IOContext> comm_context_;
53
54   // Context used for overlapped reads.
55   std::unique_ptr<base::MessagePumpForIO::IOContext> read_context_;
56
57   // Context used for overlapped writes.
58   std::unique_ptr<base::MessagePumpForIO::IOContext> write_context_;
59
60   // Asynchronous event mask state
61   DWORD event_mask_;
62
63   // Indicates if a pending read is waiting on initial data arrival via
64   // WaitCommEvent, as opposed to waiting on actual ReadFile completion
65   // after a corresponding WaitCommEvent has completed.
66   bool is_comm_pending_;
67
68   // The helper lives on the UI thread and holds a weak reference back to the
69   // handler that owns it.
70   UiThreadHelper* helper_;
71   base::WeakPtrFactory<SerialIoHandlerWin> weak_factory_;
72
73   DISALLOW_COPY_AND_ASSIGN(SerialIoHandlerWin);
74 };
75
76 }  // namespace device
77
78 #endif  // DEVICE_SERIAL_SERIAL_IO_HANDLER_WIN_H_