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.
5 #ifndef SERVICES_DEVICE_SERIAL_SERIAL_IO_HANDLER_WIN_H_
6 #define SERVICES_DEVICE_SERIAL_SERIAL_IO_HANDLER_WIN_H_
11 #include "base/macros.h"
12 #include "base/message_loop/message_pump_for_io.h"
13 #include "base/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 // SerialIoHandler implementation.
23 void ReadImpl() override;
24 void WriteImpl() override;
25 void CancelReadImpl() override;
26 void CancelWriteImpl() override;
27 bool ConfigurePortImpl() override;
28 bool Flush() const override;
29 mojom::SerialPortControlSignalsPtr GetControlSignals() const override;
30 bool SetControlSignals(
31 const mojom::SerialHostControlSignals& control_signals) override;
32 mojom::SerialConnectionInfoPtr GetPortInfo() const override;
33 bool SetBreak() override;
34 bool ClearBreak() override;
35 bool PostOpen() override;
39 friend class SerialIoHandler;
41 explicit SerialIoHandlerWin(
42 const base::FilePath& port,
43 scoped_refptr<base::SingleThreadTaskRunner> ui_thread_task_runner);
44 ~SerialIoHandlerWin() override;
46 // base::MessagePumpForIO::IOHandler implementation.
47 void OnIOCompleted(base::MessagePumpForIO::IOContext* context,
48 DWORD bytes_transfered,
49 DWORD error) override;
51 void OnDeviceRemoved(const std::string& device_path);
53 // Context used for asynchronous WaitCommEvent calls.
54 std::unique_ptr<base::MessagePumpForIO::IOContext> comm_context_;
56 // Context used for overlapped reads.
57 std::unique_ptr<base::MessagePumpForIO::IOContext> read_context_;
59 // Context used for overlapped writes.
60 std::unique_ptr<base::MessagePumpForIO::IOContext> write_context_;
62 // Asynchronous event mask state
65 // Indicates if a pending read is waiting on initial data arrival via
66 // WaitCommEvent, as opposed to waiting on actual ReadFile completion
67 // after a corresponding WaitCommEvent has completed.
68 bool is_comm_pending_;
70 // The helper lives on the UI thread and holds a weak reference back to the
71 // handler that owns it.
72 UiThreadHelper* helper_;
73 base::WeakPtrFactory<SerialIoHandlerWin> weak_factory_;
75 DISALLOW_COPY_AND_ASSIGN(SerialIoHandlerWin);
80 #endif // SERVICES_DEVICE_SERIAL_SERIAL_IO_HANDLER_WIN_H_