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