ChildAccountService[Java] delegates everything to native side.
[chromium-blink-merge.git] / device / serial / serial_io_handler.h
blob81d70282a00188dce960ca65308b6d3e119707ce
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 DEVICE_SERIAL_SERIAL_IO_HANDLER_H_
6 #define DEVICE_SERIAL_SERIAL_IO_HANDLER_H_
8 #include "base/callback.h"
9 #include "base/files/file.h"
10 #include "base/memory/ref_counted.h"
11 #include "base/single_thread_task_runner.h"
12 #include "base/thread_task_runner_handle.h"
13 #include "base/threading/non_thread_safe.h"
14 #include "device/serial/buffer.h"
15 #include "device/serial/serial.mojom.h"
17 namespace dbus {
18 class FileDescriptor;
21 namespace device {
23 // Provides a simplified interface for performing asynchronous I/O on serial
24 // devices by hiding platform-specific MessageLoop interfaces. Pending I/O
25 // operations hold a reference to this object until completion so that memory
26 // doesn't disappear out from under the OS.
27 class SerialIoHandler : public base::NonThreadSafe,
28 public base::RefCounted<SerialIoHandler> {
29 public:
30 // Constructs an instance of some platform-specific subclass.
31 static scoped_refptr<SerialIoHandler> Create(
32 scoped_refptr<base::SingleThreadTaskRunner> file_thread_task_runner,
33 scoped_refptr<base::SingleThreadTaskRunner> ui_thread_task_runner);
35 typedef base::Callback<void(bool success)> OpenCompleteCallback;
37 // Initiates an asynchronous Open of the device.
38 virtual void Open(const std::string& port,
39 const serial::ConnectionOptions& options,
40 const OpenCompleteCallback& callback);
42 #if defined(OS_CHROMEOS)
43 // Signals that the port has been opened.
44 void OnPathOpened(
45 scoped_refptr<base::SingleThreadTaskRunner> file_thread_task_runner,
46 scoped_refptr<base::SingleThreadTaskRunner> io_thread_task_runner,
47 dbus::FileDescriptor fd);
49 // Validates the file descriptor provided by the permission broker.
50 void ValidateOpenPort(
51 scoped_refptr<base::SingleThreadTaskRunner> io_thread_task_runner,
52 dbus::FileDescriptor fd);
53 #endif // defined(OS_CHROMEOS)
55 // Performs an async Read operation. Behavior is undefined if this is called
56 // while a Read is already pending. Otherwise, the Done or DoneWithError
57 // method on |buffer| will eventually be called with a result.
58 void Read(scoped_ptr<WritableBuffer> buffer);
60 // Performs an async Write operation. Behavior is undefined if this is called
61 // while a Write is already pending. Otherwise, the Done or DoneWithError
62 // method on |buffer| will eventually be called with a result.
63 void Write(scoped_ptr<ReadOnlyBuffer> buffer);
65 // Indicates whether or not a read is currently pending.
66 bool IsReadPending() const;
68 // Indicates whether or not a write is currently pending.
69 bool IsWritePending() const;
71 // Attempts to cancel a pending read operation.
72 void CancelRead(serial::ReceiveError reason);
74 // Attempts to cancel a pending write operation.
75 void CancelWrite(serial::SendError reason);
77 // Flushes input and output buffers.
78 virtual bool Flush() const = 0;
80 // Reads current control signals (DCD, CTS, etc.) into an existing
81 // DeviceControlSignals structure. Returns |true| iff the signals were
82 // successfully read.
83 virtual serial::DeviceControlSignalsPtr GetControlSignals() const = 0;
85 // Sets one or more control signals (DTR and/or RTS). Returns |true| iff
86 // the signals were successfully set. Unininitialized flags in the
87 // HostControlSignals structure are left unchanged.
88 virtual bool SetControlSignals(
89 const serial::HostControlSignals& control_signals) = 0;
91 // Performs platform-specific port configuration. Returns |true| iff
92 // configuration was successful.
93 bool ConfigurePort(const serial::ConnectionOptions& options);
95 // Performs a platform-specific port configuration query. Fills values in an
96 // existing ConnectionInfo. Returns |true| iff port configuration was
97 // successfully retrieved.
98 virtual serial::ConnectionInfoPtr GetPortInfo() const = 0;
100 // Initiates a BREAK signal. Places the transmission line in a break state
101 // until the |ClearBreak| is called.
102 virtual bool SetBreak() = 0;
104 // Terminates the BREAK signal. Places the transmission line in a nonbreak
105 // state.
106 virtual bool ClearBreak() = 0;
108 protected:
109 explicit SerialIoHandler(
110 scoped_refptr<base::SingleThreadTaskRunner> file_thread_task_runner,
111 scoped_refptr<base::SingleThreadTaskRunner> ui_thread_task_runner);
112 virtual ~SerialIoHandler();
114 // Performs a platform-specific read operation. This must guarantee that
115 // ReadCompleted is called when the underlying async operation is completed
116 // or the SerialIoHandler instance will leak.
117 // NOTE: Implementations of ReadImpl should never call ReadCompleted directly.
118 // Use QueueReadCompleted instead to avoid reentrancy.
119 virtual void ReadImpl() = 0;
121 // Performs a platform-specific write operation. This must guarantee that
122 // WriteCompleted is called when the underlying async operation is completed
123 // or the SerialIoHandler instance will leak.
124 // NOTE: Implementations of WriteImpl should never call WriteCompleted
125 // directly. Use QueueWriteCompleted instead to avoid reentrancy.
126 virtual void WriteImpl() = 0;
128 // Platform-specific read cancelation.
129 virtual void CancelReadImpl() = 0;
131 // Platform-specific write cancelation.
132 virtual void CancelWriteImpl() = 0;
134 // Platform-specific port configuration applies options_ to the device.
135 virtual bool ConfigurePortImpl() = 0;
137 // Performs platform-specific, one-time port configuration on open.
138 virtual bool PostOpen();
140 // Called by the implementation to signal that the active read has completed.
141 // WARNING: Calling this method can destroy the SerialIoHandler instance
142 // if the associated I/O operation was the only thing keeping it alive.
143 void ReadCompleted(int bytes_read, serial::ReceiveError error);
145 // Called by the implementation to signal that the active write has completed.
146 // WARNING: Calling this method may destroy the SerialIoHandler instance
147 // if the associated I/O operation was the only thing keeping it alive.
148 void WriteCompleted(int bytes_written, serial::SendError error);
150 // Queues a ReadCompleted call on the current thread. This is used to allow
151 // ReadImpl to immediately signal completion with 0 bytes and an error,
152 // without being reentrant.
153 void QueueReadCompleted(int bytes_read, serial::ReceiveError error);
155 // Queues a WriteCompleted call on the current thread. This is used to allow
156 // WriteImpl to immediately signal completion with 0 bytes and an error,
157 // without being reentrant.
158 void QueueWriteCompleted(int bytes_written, serial::SendError error);
160 const base::File& file() const { return file_; }
162 char* pending_read_buffer() const {
163 return pending_read_buffer_ ? pending_read_buffer_->GetData() : NULL;
166 uint32_t pending_read_buffer_len() const {
167 return pending_read_buffer_ ? pending_read_buffer_->GetSize() : 0;
170 serial::ReceiveError read_cancel_reason() const {
171 return read_cancel_reason_;
174 bool read_canceled() const { return read_canceled_; }
176 const char* pending_write_buffer() const {
177 return pending_write_buffer_ ? pending_write_buffer_->GetData() : NULL;
180 uint32_t pending_write_buffer_len() const {
181 return pending_write_buffer_ ? pending_write_buffer_->GetSize() : 0;
184 serial::SendError write_cancel_reason() const { return write_cancel_reason_; }
186 bool write_canceled() const { return write_canceled_; }
188 const serial::ConnectionOptions& options() const { return options_; }
190 // Possibly fixes up a serial port path name in a platform-specific manner.
191 static std::string MaybeFixUpPortName(const std::string& port_name);
193 private:
194 friend class base::RefCounted<SerialIoHandler>;
196 void MergeConnectionOptions(const serial::ConnectionOptions& options);
198 // Continues an Open operation on the FILE thread.
199 void StartOpen(const std::string& port,
200 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner);
202 // Finalizes an Open operation (continued from StartOpen) on the IO thread.
203 void FinishOpen(base::File file);
205 void Close();
207 // Continues a Close operation on the FILE thread.
208 static void DoClose(base::File port);
210 // File for the opened serial device. This value is only modified from the IO
211 // thread.
212 base::File file_;
214 // Currently applied connection options.
215 serial::ConnectionOptions options_;
217 scoped_ptr<WritableBuffer> pending_read_buffer_;
218 serial::ReceiveError read_cancel_reason_;
219 bool read_canceled_;
221 scoped_ptr<ReadOnlyBuffer> pending_write_buffer_;
222 serial::SendError write_cancel_reason_;
223 bool write_canceled_;
225 // Callback to handle the completion of a pending Open() request.
226 OpenCompleteCallback open_complete_;
228 scoped_refptr<base::SingleThreadTaskRunner> file_thread_task_runner_;
229 // On Chrome OS, PermissionBrokerClient should be called on the UI thread.
230 scoped_refptr<base::SingleThreadTaskRunner> ui_thread_task_runner_;
232 DISALLOW_COPY_AND_ASSIGN(SerialIoHandler);
235 } // namespace device
237 #endif // DEVICE_SERIAL_SERIAL_IO_HANDLER_H_