OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef DEVICE_SERIAL_SERIAL_IO_HANDLER_H_ | 5 #ifndef DEVICE_SERIAL_SERIAL_IO_HANDLER_H_ |
6 #define DEVICE_SERIAL_SERIAL_IO_HANDLER_H_ | 6 #define DEVICE_SERIAL_SERIAL_IO_HANDLER_H_ |
7 | 7 |
8 #include "base/callback.h" | 8 #include "base/callback.h" |
9 #include "base/files/file.h" | 9 #include "base/files/file.h" |
10 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
11 #include "base/message_loop/message_loop_proxy.h" | 11 #include "base/message_loop/message_loop_proxy.h" |
| 12 #include "base/single_thread_task_runner.h" |
| 13 #include "base/thread_task_runner_handle.h" |
12 #include "base/threading/non_thread_safe.h" | 14 #include "base/threading/non_thread_safe.h" |
13 #include "device/serial/buffer.h" | 15 #include "device/serial/buffer.h" |
14 #include "device/serial/serial.mojom.h" | 16 #include "device/serial/serial.mojom.h" |
15 | 17 |
16 namespace device { | 18 namespace device { |
17 | 19 |
18 // Provides a simplified interface for performing asynchronous I/O on serial | 20 // Provides a simplified interface for performing asynchronous I/O on serial |
19 // devices by hiding platform-specific MessageLoop interfaces. Pending I/O | 21 // devices by hiding platform-specific MessageLoop interfaces. Pending I/O |
20 // operations hold a reference to this object until completion so that memory | 22 // operations hold a reference to this object until completion so that memory |
21 // doesn't disappear out from under the OS. | 23 // doesn't disappear out from under the OS. |
22 class SerialIoHandler : public base::NonThreadSafe, | 24 class SerialIoHandler : public base::NonThreadSafe, |
23 public base::RefCounted<SerialIoHandler> { | 25 public base::RefCounted<SerialIoHandler> { |
24 public: | 26 public: |
25 // Constructs an instance of some platform-specific subclass. | 27 // Constructs an instance of some platform-specific subclass. |
26 static scoped_refptr<SerialIoHandler> Create( | 28 static scoped_refptr<SerialIoHandler> Create( |
27 scoped_refptr<base::MessageLoopProxy> file_thread_message_loop, | 29 scoped_refptr<base::SingleThreadTaskRunner> file_thread_message_loop, |
28 scoped_refptr<base::MessageLoopProxy> ui_thread_message_loop); | 30 scoped_refptr<base::SingleThreadTaskRunner> ui_thread_message_loop); |
29 | 31 |
30 typedef base::Callback<void(bool success)> OpenCompleteCallback; | 32 typedef base::Callback<void(bool success)> OpenCompleteCallback; |
31 | 33 |
32 // Initiates an asynchronous Open of the device. | 34 // Initiates an asynchronous Open of the device. |
33 virtual void Open(const std::string& port, | 35 virtual void Open(const std::string& port, |
34 const serial::ConnectionOptions& options, | 36 const serial::ConnectionOptions& options, |
35 const OpenCompleteCallback& callback); | 37 const OpenCompleteCallback& callback); |
36 | 38 |
37 // Signals that the access request for |port| is complete. | 39 // Signals that the access request for |port| is complete. |
38 void OnRequestAccessComplete(const std::string& port, bool success); | 40 void OnRequestAccessComplete(const std::string& port, bool success); |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
77 // configuration was successful. | 79 // configuration was successful. |
78 bool ConfigurePort(const serial::ConnectionOptions& options); | 80 bool ConfigurePort(const serial::ConnectionOptions& options); |
79 | 81 |
80 // Performs a platform-specific port configuration query. Fills values in an | 82 // Performs a platform-specific port configuration query. Fills values in an |
81 // existing ConnectionInfo. Returns |true| iff port configuration was | 83 // existing ConnectionInfo. Returns |true| iff port configuration was |
82 // successfully retrieved. | 84 // successfully retrieved. |
83 virtual serial::ConnectionInfoPtr GetPortInfo() const = 0; | 85 virtual serial::ConnectionInfoPtr GetPortInfo() const = 0; |
84 | 86 |
85 protected: | 87 protected: |
86 explicit SerialIoHandler( | 88 explicit SerialIoHandler( |
87 scoped_refptr<base::MessageLoopProxy> file_thread_message_loop, | 89 scoped_refptr<base::SingleThreadTaskRunner> file_thread_message_loop, |
88 scoped_refptr<base::MessageLoopProxy> ui_thread_message_loop); | 90 scoped_refptr<base::SingleThreadTaskRunner> ui_thread_message_loop); |
89 virtual ~SerialIoHandler(); | 91 virtual ~SerialIoHandler(); |
90 | 92 |
91 // Performs a platform-specific read operation. This must guarantee that | 93 // Performs a platform-specific read operation. This must guarantee that |
92 // ReadCompleted is called when the underlying async operation is completed | 94 // ReadCompleted is called when the underlying async operation is completed |
93 // or the SerialIoHandler instance will leak. | 95 // or the SerialIoHandler instance will leak. |
94 // NOTE: Implementations of ReadImpl should never call ReadCompleted directly. | 96 // NOTE: Implementations of ReadImpl should never call ReadCompleted directly. |
95 // Use QueueReadCompleted instead to avoid reentrancy. | 97 // Use QueueReadCompleted instead to avoid reentrancy. |
96 virtual void ReadImpl() = 0; | 98 virtual void ReadImpl() = 0; |
97 | 99 |
98 // Performs a platform-specific write operation. This must guarantee that | 100 // Performs a platform-specific write operation. This must guarantee that |
99 // WriteCompleted is called when the underlying async operation is completed | 101 // WriteCompleted is called when the underlying async operation is completed |
100 // or the SerialIoHandler instance will leak. | 102 // or the SerialIoHandler instance will leak. |
101 // NOTE: Implementations of WriteImpl should never call WriteCompleted | 103 // NOTE: Implementations of WriteImpl should never call WriteCompleted |
102 // directly. Use QueueWriteCompleted instead to avoid reentrancy. | 104 // directly. Use QueueWriteCompleted instead to avoid reentrancy. |
103 virtual void WriteImpl() = 0; | 105 virtual void WriteImpl() = 0; |
104 | 106 |
105 // Platform-specific read cancelation. | 107 // Platform-specific read cancelation. |
106 virtual void CancelReadImpl() = 0; | 108 virtual void CancelReadImpl() = 0; |
107 | 109 |
108 // Platform-specific write cancelation. | 110 // Platform-specific write cancelation. |
109 virtual void CancelWriteImpl() = 0; | 111 virtual void CancelWriteImpl() = 0; |
110 | 112 |
111 // Platform-specific port configuration applies options_ to the device. | 113 // Platform-specific port configuration applies options_ to the device. |
112 virtual bool ConfigurePortImpl() = 0; | 114 virtual bool ConfigurePortImpl() = 0; |
113 | 115 |
114 // Requests access to the underlying serial device, if needed. | 116 // Requests access to the underlying serial device, if needed. |
115 virtual void RequestAccess( | 117 virtual void RequestAccess( |
116 const std::string& port, | 118 const std::string& port, |
117 scoped_refptr<base::MessageLoopProxy> file_message_loop, | 119 scoped_refptr<base::SingleThreadTaskRunner> file_message_loop, |
118 scoped_refptr<base::MessageLoopProxy> ui_message_loop); | 120 scoped_refptr<base::SingleThreadTaskRunner> ui_message_loop); |
119 | 121 |
120 // Performs platform-specific, one-time port configuration on open. | 122 // Performs platform-specific, one-time port configuration on open. |
121 virtual bool PostOpen(); | 123 virtual bool PostOpen(); |
122 | 124 |
123 // Called by the implementation to signal that the active read has completed. | 125 // Called by the implementation to signal that the active read has completed. |
124 // WARNING: Calling this method can destroy the SerialIoHandler instance | 126 // WARNING: Calling this method can destroy the SerialIoHandler instance |
125 // if the associated I/O operation was the only thing keeping it alive. | 127 // if the associated I/O operation was the only thing keeping it alive. |
126 void ReadCompleted(int bytes_read, serial::ReceiveError error); | 128 void ReadCompleted(int bytes_read, serial::ReceiveError error); |
127 | 129 |
128 // Called by the implementation to signal that the active write has completed. | 130 // Called by the implementation to signal that the active write has completed. |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
173 // Possibly fixes up a serial port path name in a platform-specific manner. | 175 // Possibly fixes up a serial port path name in a platform-specific manner. |
174 static std::string MaybeFixUpPortName(const std::string& port_name); | 176 static std::string MaybeFixUpPortName(const std::string& port_name); |
175 | 177 |
176 private: | 178 private: |
177 friend class base::RefCounted<SerialIoHandler>; | 179 friend class base::RefCounted<SerialIoHandler>; |
178 | 180 |
179 void MergeConnectionOptions(const serial::ConnectionOptions& options); | 181 void MergeConnectionOptions(const serial::ConnectionOptions& options); |
180 | 182 |
181 // Continues an Open operation on the FILE thread. | 183 // Continues an Open operation on the FILE thread. |
182 void StartOpen(const std::string& port, | 184 void StartOpen(const std::string& port, |
183 scoped_refptr<base::MessageLoopProxy> io_message_loop); | 185 scoped_refptr<base::SingleThreadTaskRunner> io_message_loop); |
184 | 186 |
185 // Finalizes an Open operation (continued from StartOpen) on the IO thread. | 187 // Finalizes an Open operation (continued from StartOpen) on the IO thread. |
186 void FinishOpen(base::File file); | 188 void FinishOpen(base::File file); |
187 | 189 |
188 void Close(); | 190 void Close(); |
189 | 191 |
190 // Continues a Close operation on the FILE thread. | 192 // Continues a Close operation on the FILE thread. |
191 static void DoClose(base::File port); | 193 static void DoClose(base::File port); |
192 | 194 |
193 // File for the opened serial device. This value is only modified from the IO | 195 // File for the opened serial device. This value is only modified from the IO |
194 // thread. | 196 // thread. |
195 base::File file_; | 197 base::File file_; |
196 | 198 |
197 // Currently applied connection options. | 199 // Currently applied connection options. |
198 serial::ConnectionOptions options_; | 200 serial::ConnectionOptions options_; |
199 | 201 |
200 scoped_ptr<WritableBuffer> pending_read_buffer_; | 202 scoped_ptr<WritableBuffer> pending_read_buffer_; |
201 serial::ReceiveError read_cancel_reason_; | 203 serial::ReceiveError read_cancel_reason_; |
202 bool read_canceled_; | 204 bool read_canceled_; |
203 | 205 |
204 scoped_ptr<ReadOnlyBuffer> pending_write_buffer_; | 206 scoped_ptr<ReadOnlyBuffer> pending_write_buffer_; |
205 serial::SendError write_cancel_reason_; | 207 serial::SendError write_cancel_reason_; |
206 bool write_canceled_; | 208 bool write_canceled_; |
207 | 209 |
208 // Callback to handle the completion of a pending Open() request. | 210 // Callback to handle the completion of a pending Open() request. |
209 OpenCompleteCallback open_complete_; | 211 OpenCompleteCallback open_complete_; |
210 | 212 |
211 scoped_refptr<base::MessageLoopProxy> file_thread_message_loop_; | 213 scoped_refptr<base::SingleThreadTaskRunner> file_thread_message_loop_; |
212 // On Chrome OS, PermissionBrokerClient should be called on the UI thread. | 214 // On Chrome OS, PermissionBrokerClient should be called on the UI thread. |
213 scoped_refptr<base::MessageLoopProxy> ui_thread_message_loop_; | 215 scoped_refptr<base::SingleThreadTaskRunner> ui_thread_message_loop_; |
214 | 216 |
215 DISALLOW_COPY_AND_ASSIGN(SerialIoHandler); | 217 DISALLOW_COPY_AND_ASSIGN(SerialIoHandler); |
216 }; | 218 }; |
217 | 219 |
218 } // namespace device | 220 } // namespace device |
219 | 221 |
220 #endif // DEVICE_SERIAL_SERIAL_IO_HANDLER_H_ | 222 #endif // DEVICE_SERIAL_SERIAL_IO_HANDLER_H_ |
OLD | NEW |