| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 CHROME_BROWSER_EXTENSIONS_API_SERIAL_SERIAL_IO_HANDLER_H_ | 5 #ifndef CHROME_BROWSER_EXTENSIONS_API_SERIAL_SERIAL_IO_HANDLER_H_ |
| 6 #define CHROME_BROWSER_EXTENSIONS_API_SERIAL_SERIAL_IO_HANDLER_H_ | 6 #define CHROME_BROWSER_EXTENSIONS_API_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/threading/non_thread_safe.h" | 11 #include "base/threading/non_thread_safe.h" |
| 12 #include "chrome/common/extensions/api/serial.h" | 12 #include "device/serial/serial.mojom.h" |
| 13 #include "net/base/io_buffer.h" | 13 #include "net/base/io_buffer.h" |
| 14 | 14 |
| 15 namespace extensions { | 15 namespace extensions { |
| 16 | 16 |
| 17 // Provides a simplified interface for performing asynchronous I/O on serial | 17 // Provides a simplified interface for performing asynchronous I/O on serial |
| 18 // devices by hiding platform-specific MessageLoop interfaces. Pending I/O | 18 // devices by hiding platform-specific MessageLoop interfaces. Pending I/O |
| 19 // operations hold a reference to this object until completion so that memory | 19 // operations hold a reference to this object until completion so that memory |
| 20 // doesn't disappear out from under the OS. | 20 // doesn't disappear out from under the OS. |
| 21 class SerialIoHandler : public base::NonThreadSafe, | 21 class SerialIoHandler : public base::NonThreadSafe, |
| 22 public base::RefCounted<SerialIoHandler> { | 22 public base::RefCounted<SerialIoHandler> { |
| 23 public: | 23 public: |
| 24 // Constructs an instance of some platform-specific subclass. | 24 // Constructs an instance of some platform-specific subclass. |
| 25 static scoped_refptr<SerialIoHandler> Create(); | 25 static scoped_refptr<SerialIoHandler> Create(); |
| 26 | 26 |
| 27 typedef base::Callback<void(bool success)> OpenCompleteCallback; | 27 typedef base::Callback<void(bool success)> OpenCompleteCallback; |
| 28 | 28 |
| 29 // Called with a string of bytes read, and a result code. Note that an error | 29 // Called with a string of bytes read, and a result code. Note that an error |
| 30 // result does not necessarily imply 0 bytes read. | 30 // result does not necessarily imply 0 bytes read. |
| 31 typedef base::Callback<void(const std::string& data, | 31 typedef base::Callback< |
| 32 api::serial::ReceiveError error)> | 32 void(const std::string& data, device::serial::ReceiveError error)> |
| 33 ReadCompleteCallback; | 33 ReadCompleteCallback; |
| 34 | 34 |
| 35 // Called with the number of bytes written and a result code. Note that an | 35 // Called with the number of bytes written and a result code. Note that an |
| 36 // error result does not necessarily imply 0 bytes written. | 36 // error result does not necessarily imply 0 bytes written. |
| 37 typedef base::Callback<void(int bytes_written, api::serial::SendError error)> | 37 typedef base::Callback< |
| 38 void(int bytes_written, device::serial::SendError error)> |
| 38 WriteCompleteCallback; | 39 WriteCompleteCallback; |
| 39 | 40 |
| 40 // Initializes the handler on the current message loop. Must be called exactly | 41 // Initializes the handler on the current message loop. Must be called exactly |
| 41 // once before performing any I/O through the handler. | 42 // once before performing any I/O through the handler. |
| 42 virtual void Initialize(const ReadCompleteCallback& read_callback, | 43 virtual void Initialize(const ReadCompleteCallback& read_callback, |
| 43 const WriteCompleteCallback& write_callback); | 44 const WriteCompleteCallback& write_callback); |
| 44 | 45 |
| 45 // Initiates an asynchronous Open of the device. | 46 // Initiates an asynchronous Open of the device. |
| 46 virtual void Open(const std::string& port, | 47 virtual void Open(const std::string& port, |
| 47 const OpenCompleteCallback& callback); | 48 const OpenCompleteCallback& callback); |
| 48 | 49 |
| 49 // Performs an async Read operation. Behavior is undefined if this is called | 50 // Performs an async Read operation. Behavior is undefined if this is called |
| 50 // while a Read is already pending. Otherwise, the ReadCompleteCallback | 51 // while a Read is already pending. Otherwise, the ReadCompleteCallback |
| 51 // (see above) will eventually be called with a result. | 52 // (see above) will eventually be called with a result. |
| 52 void Read(int max_bytes); | 53 void Read(int max_bytes); |
| 53 | 54 |
| 54 // Performs an async Write operation. Behavior is undefined if this is called | 55 // Performs an async Write operation. Behavior is undefined if this is called |
| 55 // while a Write is already pending. Otherwise, the WriteCompleteCallback | 56 // while a Write is already pending. Otherwise, the WriteCompleteCallback |
| 56 // (see above) will eventually be called with a result. | 57 // (see above) will eventually be called with a result. |
| 57 void Write(const std::string& data); | 58 void Write(const std::string& data); |
| 58 | 59 |
| 59 // Indicates whether or not a read is currently pending. | 60 // Indicates whether or not a read is currently pending. |
| 60 bool IsReadPending() const; | 61 bool IsReadPending() const; |
| 61 | 62 |
| 62 // Indicates whether or not a write is currently pending. | 63 // Indicates whether or not a write is currently pending. |
| 63 bool IsWritePending() const; | 64 bool IsWritePending() const; |
| 64 | 65 |
| 65 // Attempts to cancel a pending read operation. | 66 // Attempts to cancel a pending read operation. |
| 66 void CancelRead(api::serial::ReceiveError reason); | 67 void CancelRead(device::serial::ReceiveError reason); |
| 67 | 68 |
| 68 // Attempts to cancel a pending write operation. | 69 // Attempts to cancel a pending write operation. |
| 69 void CancelWrite(api::serial::SendError reason); | 70 void CancelWrite(device::serial::SendError reason); |
| 70 | 71 |
| 71 // Flushes input and output buffers. | 72 // Flushes input and output buffers. |
| 72 virtual bool Flush() const = 0; | 73 virtual bool Flush() const = 0; |
| 73 | 74 |
| 74 // Reads current control signals (DCD, CTS, etc.) into an existing | 75 // Reads current control signals (DCD, CTS, etc.) into an existing |
| 75 // DeviceControlSignals structure. Returns |true| iff the signals were | 76 // DeviceControlSignals structure. Returns |true| iff the signals were |
| 76 // successfully read. | 77 // successfully read. |
| 77 virtual bool GetControlSignals( | 78 virtual device::serial::DeviceControlSignalsPtr GetControlSignals() const = 0; |
| 78 api::serial::DeviceControlSignals* control_signals) const = 0; | |
| 79 | 79 |
| 80 // Sets one or more control signals (DTR and/or RTS). Returns |true| iff | 80 // Sets one or more control signals (DTR and/or RTS). Returns |true| iff |
| 81 // the signals were successfully set. Unininitialized flags in the | 81 // the signals were successfully set. Unininitialized flags in the |
| 82 // HostControlSignals structure are left unchanged. | 82 // HostControlSignals structure are left unchanged. |
| 83 virtual bool SetControlSignals( | 83 virtual bool SetControlSignals( |
| 84 const api::serial::HostControlSignals& control_signals) = 0; | 84 const device::serial::HostControlSignals& control_signals) = 0; |
| 85 | 85 |
| 86 // Performs platform-specific port configuration. Returns |true| iff | 86 // Performs platform-specific port configuration. Returns |true| iff |
| 87 // configuration was successful. | 87 // configuration was successful. |
| 88 virtual bool ConfigurePort(const api::serial::ConnectionOptions& options) = 0; | 88 virtual bool ConfigurePort( |
| 89 const device::serial::ConnectionOptions& options) = 0; |
| 89 | 90 |
| 90 // Performs a platform-specific port configuration query. Fills values in an | 91 // Performs a platform-specific port configuration query. Fills values in an |
| 91 // existing ConnectionInfo. Returns |true| iff port configuration was | 92 // existing ConnectionInfo. Returns |true| iff port configuration was |
| 92 // successfully retrieved. | 93 // successfully retrieved. |
| 93 virtual bool GetPortInfo(api::serial::ConnectionInfo* info) const = 0; | 94 virtual device::serial::ConnectionInfoPtr GetPortInfo() const = 0; |
| 94 | 95 |
| 95 protected: | 96 protected: |
| 96 SerialIoHandler(); | 97 SerialIoHandler(); |
| 97 virtual ~SerialIoHandler(); | 98 virtual ~SerialIoHandler(); |
| 98 | 99 |
| 99 // Performs a platform-specific read operation. This must guarantee that | 100 // Performs a platform-specific read operation. This must guarantee that |
| 100 // ReadCompleted is called when the underlying async operation is completed | 101 // ReadCompleted is called when the underlying async operation is completed |
| 101 // or the SerialIoHandler instance will leak. | 102 // or the SerialIoHandler instance will leak. |
| 102 // NOTE: Implementations of ReadImpl should never call ReadCompleted directly. | 103 // NOTE: Implementations of ReadImpl should never call ReadCompleted directly. |
| 103 // Use QueueReadCompleted instead to avoid reentrancy. | 104 // Use QueueReadCompleted instead to avoid reentrancy. |
| (...skipping 11 matching lines...) Expand all Loading... |
| 115 | 116 |
| 116 // Platform-specific write cancelation. | 117 // Platform-specific write cancelation. |
| 117 virtual void CancelWriteImpl() = 0; | 118 virtual void CancelWriteImpl() = 0; |
| 118 | 119 |
| 119 // Performs platform-specific, one-time port configuration on open. | 120 // Performs platform-specific, one-time port configuration on open. |
| 120 virtual bool PostOpen(); | 121 virtual bool PostOpen(); |
| 121 | 122 |
| 122 // Called by the implementation to signal that the active read has completed. | 123 // Called by the implementation to signal that the active read has completed. |
| 123 // WARNING: Calling this method can destroy the SerialIoHandler instance | 124 // WARNING: Calling this method can destroy the SerialIoHandler instance |
| 124 // if the associated I/O operation was the only thing keeping it alive. | 125 // if the associated I/O operation was the only thing keeping it alive. |
| 125 void ReadCompleted(int bytes_read, api::serial::ReceiveError error); | 126 void ReadCompleted(int bytes_read, device::serial::ReceiveError error); |
| 126 | 127 |
| 127 // Called by the implementation to signal that the active write has completed. | 128 // Called by the implementation to signal that the active write has completed. |
| 128 // WARNING: Calling this method may destroy the SerialIoHandler instance | 129 // WARNING: Calling this method may destroy the SerialIoHandler instance |
| 129 // if the associated I/O operation was the only thing keeping it alive. | 130 // if the associated I/O operation was the only thing keeping it alive. |
| 130 void WriteCompleted(int bytes_written, api::serial::SendError error); | 131 void WriteCompleted(int bytes_written, device::serial::SendError error); |
| 131 | 132 |
| 132 // Queues a ReadCompleted call on the current thread. This is used to allow | 133 // Queues a ReadCompleted call on the current thread. This is used to allow |
| 133 // ReadImpl to immediately signal completion with 0 bytes and an error, | 134 // ReadImpl to immediately signal completion with 0 bytes and an error, |
| 134 // without being reentrant. | 135 // without being reentrant. |
| 135 void QueueReadCompleted(int bytes_read, api::serial::ReceiveError error); | 136 void QueueReadCompleted(int bytes_read, device::serial::ReceiveError error); |
| 136 | 137 |
| 137 // Queues a WriteCompleted call on the current thread. This is used to allow | 138 // Queues a WriteCompleted call on the current thread. This is used to allow |
| 138 // WriteImpl to immediately signal completion with 0 bytes and an error, | 139 // WriteImpl to immediately signal completion with 0 bytes and an error, |
| 139 // without being reentrant. | 140 // without being reentrant. |
| 140 void QueueWriteCompleted(int bytes_written, api::serial::SendError error); | 141 void QueueWriteCompleted(int bytes_written, device::serial::SendError error); |
| 141 | 142 |
| 142 const base::File& file() const { return file_; } | 143 const base::File& file() const { return file_; } |
| 143 | 144 |
| 144 net::IOBuffer* pending_read_buffer() const { | 145 net::IOBuffer* pending_read_buffer() const { |
| 145 return pending_read_buffer_.get(); | 146 return pending_read_buffer_.get(); |
| 146 } | 147 } |
| 147 | 148 |
| 148 int pending_read_buffer_len() const { | 149 int pending_read_buffer_len() const { |
| 149 return pending_read_buffer_len_; | 150 return pending_read_buffer_len_; |
| 150 } | 151 } |
| 151 | 152 |
| 152 api::serial::ReceiveError read_cancel_reason() const { | 153 device::serial::ReceiveError read_cancel_reason() const { |
| 153 return read_cancel_reason_; | 154 return read_cancel_reason_; |
| 154 } | 155 } |
| 155 | 156 |
| 156 bool read_canceled() const { | 157 bool read_canceled() const { |
| 157 return read_canceled_; | 158 return read_canceled_; |
| 158 } | 159 } |
| 159 | 160 |
| 160 net::IOBuffer* pending_write_buffer() const { | 161 net::IOBuffer* pending_write_buffer() const { |
| 161 return pending_write_buffer_.get(); | 162 return pending_write_buffer_.get(); |
| 162 } | 163 } |
| 163 | 164 |
| 164 int pending_write_buffer_len() const { | 165 int pending_write_buffer_len() const { |
| 165 return pending_write_buffer_len_; | 166 return pending_write_buffer_len_; |
| 166 } | 167 } |
| 167 | 168 |
| 168 api::serial::SendError write_cancel_reason() const { | 169 device::serial::SendError write_cancel_reason() const { |
| 169 return write_cancel_reason_; | 170 return write_cancel_reason_; |
| 170 } | 171 } |
| 171 | 172 |
| 172 bool write_canceled() const { | 173 bool write_canceled() const { |
| 173 return write_canceled_; | 174 return write_canceled_; |
| 174 } | 175 } |
| 175 | 176 |
| 176 // Possibly fixes up a serial port path name in a platform-specific manner. | 177 // Possibly fixes up a serial port path name in a platform-specific manner. |
| 177 static std::string MaybeFixUpPortName(const std::string& port_name); | 178 static std::string MaybeFixUpPortName(const std::string& port_name); |
| 178 | 179 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 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 scoped_refptr<net::IOBuffer> pending_read_buffer_; | 198 scoped_refptr<net::IOBuffer> pending_read_buffer_; |
| 198 int pending_read_buffer_len_; | 199 int pending_read_buffer_len_; |
| 199 api::serial::ReceiveError read_cancel_reason_; | 200 device::serial::ReceiveError read_cancel_reason_; |
| 200 bool read_canceled_; | 201 bool read_canceled_; |
| 201 | 202 |
| 202 scoped_refptr<net::IOBuffer> pending_write_buffer_; | 203 scoped_refptr<net::IOBuffer> pending_write_buffer_; |
| 203 int pending_write_buffer_len_; | 204 int pending_write_buffer_len_; |
| 204 api::serial::SendError write_cancel_reason_; | 205 device::serial::SendError write_cancel_reason_; |
| 205 bool write_canceled_; | 206 bool write_canceled_; |
| 206 | 207 |
| 207 ReadCompleteCallback read_complete_; | 208 ReadCompleteCallback read_complete_; |
| 208 WriteCompleteCallback write_complete_; | 209 WriteCompleteCallback write_complete_; |
| 209 | 210 |
| 210 // Callback to handle the completion of a pending Open() request. | 211 // Callback to handle the completion of a pending Open() request. |
| 211 OpenCompleteCallback open_complete_; | 212 OpenCompleteCallback open_complete_; |
| 212 | 213 |
| 213 DISALLOW_COPY_AND_ASSIGN(SerialIoHandler); | 214 DISALLOW_COPY_AND_ASSIGN(SerialIoHandler); |
| 214 }; | 215 }; |
| 215 | 216 |
| 216 } // namespace extensions | 217 } // namespace extensions |
| 217 | 218 |
| 218 #endif // CHROME_BROWSER_EXTENSIONS_API_SERIAL_SERIAL_IO_HANDLER_H_ | 219 #endif // CHROME_BROWSER_EXTENSIONS_API_SERIAL_SERIAL_IO_HANDLER_H_ |
| OLD | NEW |