| 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/threading/non_thread_safe.h" | 12 #include "base/threading/non_thread_safe.h" |
| 13 #include "device/serial/buffer.h" | 13 #include "device/serial/buffer.h" |
| 14 #include "device/serial/serial.mojom.h" | 14 #include "device/serial/serial.mojom.h" |
| 15 | 15 |
| 16 namespace device { | 16 namespace device { |
| 17 | 17 |
| 18 // Provides a simplified interface for performing asynchronous I/O on serial | 18 // Provides a simplified interface for performing asynchronous I/O on serial |
| 19 // devices by hiding platform-specific MessageLoop interfaces. Pending I/O | 19 // devices by hiding platform-specific MessageLoop interfaces. Pending I/O |
| 20 // operations hold a reference to this object until completion so that memory | 20 // operations hold a reference to this object until completion so that memory |
| 21 // doesn't disappear out from under the OS. | 21 // doesn't disappear out from under the OS. |
| 22 class SerialIoHandler : public base::NonThreadSafe, | 22 class SerialIoHandler : public base::NonThreadSafe, |
| 23 public base::RefCounted<SerialIoHandler> { | 23 public base::RefCounted<SerialIoHandler> { |
| 24 public: | 24 public: |
| 25 // Constructs an instance of some platform-specific subclass. | 25 // Constructs an instance of some platform-specific subclass. |
| 26 static scoped_refptr<SerialIoHandler> Create( | 26 static scoped_refptr<SerialIoHandler> Create( |
| 27 scoped_refptr<base::MessageLoopProxy> file_thread_message_loop); | 27 scoped_refptr<base::MessageLoopProxy> file_thread_message_loop, |
| 28 scoped_refptr<base::MessageLoopProxy> ui_thread_message_loop); |
| 28 | 29 |
| 29 typedef base::Callback<void(bool success)> OpenCompleteCallback; | 30 typedef base::Callback<void(bool success)> OpenCompleteCallback; |
| 30 | 31 |
| 31 // Initiates an asynchronous Open of the device. | 32 // Initiates an asynchronous Open of the device. |
| 32 virtual void Open(const std::string& port, | 33 virtual void Open(const std::string& port, |
| 33 const OpenCompleteCallback& callback); | 34 const OpenCompleteCallback& callback); |
| 34 | 35 |
| 36 // Signals that the access request for |port| is complete. |
| 37 void OnRequestAccessComplete(const std::string& port, bool success); |
| 38 |
| 35 // Performs an async Read operation. Behavior is undefined if this is called | 39 // Performs an async Read operation. Behavior is undefined if this is called |
| 36 // while a Read is already pending. Otherwise, the Done or DoneWithError | 40 // while a Read is already pending. Otherwise, the Done or DoneWithError |
| 37 // method on |buffer| will eventually be called with a result. | 41 // method on |buffer| will eventually be called with a result. |
| 38 void Read(scoped_ptr<WritableBuffer> buffer); | 42 void Read(scoped_ptr<WritableBuffer> buffer); |
| 39 | 43 |
| 40 // Performs an async Write operation. Behavior is undefined if this is called | 44 // Performs an async Write operation. Behavior is undefined if this is called |
| 41 // while a Write is already pending. Otherwise, the Done or DoneWithError | 45 // while a Write is already pending. Otherwise, the Done or DoneWithError |
| 42 // method on |buffer| will eventually be called with a result. | 46 // method on |buffer| will eventually be called with a result. |
| 43 void Write(scoped_ptr<ReadOnlyBuffer> buffer); | 47 void Write(scoped_ptr<ReadOnlyBuffer> buffer); |
| 44 | 48 |
| (...skipping 27 matching lines...) Expand all Loading... |
| 72 // configuration was successful. | 76 // configuration was successful. |
| 73 virtual bool ConfigurePort(const serial::ConnectionOptions& options) = 0; | 77 virtual bool ConfigurePort(const serial::ConnectionOptions& options) = 0; |
| 74 | 78 |
| 75 // Performs a platform-specific port configuration query. Fills values in an | 79 // Performs a platform-specific port configuration query. Fills values in an |
| 76 // existing ConnectionInfo. Returns |true| iff port configuration was | 80 // existing ConnectionInfo. Returns |true| iff port configuration was |
| 77 // successfully retrieved. | 81 // successfully retrieved. |
| 78 virtual serial::ConnectionInfoPtr GetPortInfo() const = 0; | 82 virtual serial::ConnectionInfoPtr GetPortInfo() const = 0; |
| 79 | 83 |
| 80 protected: | 84 protected: |
| 81 explicit SerialIoHandler( | 85 explicit SerialIoHandler( |
| 82 scoped_refptr<base::MessageLoopProxy> file_thread_message_loop); | 86 scoped_refptr<base::MessageLoopProxy> file_thread_message_loop, |
| 87 scoped_refptr<base::MessageLoopProxy> ui_thread_message_loop); |
| 83 virtual ~SerialIoHandler(); | 88 virtual ~SerialIoHandler(); |
| 84 | 89 |
| 85 // Performs a platform-specific read operation. This must guarantee that | 90 // Performs a platform-specific read operation. This must guarantee that |
| 86 // ReadCompleted is called when the underlying async operation is completed | 91 // ReadCompleted is called when the underlying async operation is completed |
| 87 // or the SerialIoHandler instance will leak. | 92 // or the SerialIoHandler instance will leak. |
| 88 // NOTE: Implementations of ReadImpl should never call ReadCompleted directly. | 93 // NOTE: Implementations of ReadImpl should never call ReadCompleted directly. |
| 89 // Use QueueReadCompleted instead to avoid reentrancy. | 94 // Use QueueReadCompleted instead to avoid reentrancy. |
| 90 virtual void ReadImpl() = 0; | 95 virtual void ReadImpl() = 0; |
| 91 | 96 |
| 92 // Performs a platform-specific write operation. This must guarantee that | 97 // Performs a platform-specific write operation. This must guarantee that |
| 93 // WriteCompleted is called when the underlying async operation is completed | 98 // WriteCompleted is called when the underlying async operation is completed |
| 94 // or the SerialIoHandler instance will leak. | 99 // or the SerialIoHandler instance will leak. |
| 95 // NOTE: Implementations of Writempl should never call WriteCompleted | 100 // NOTE: Implementations of WriteImpl should never call WriteCompleted |
| 96 // directly. Use QueueWriteCompleted instead to avoid reentrancy. | 101 // directly. Use QueueWriteCompleted instead to avoid reentrancy. |
| 97 virtual void WriteImpl() = 0; | 102 virtual void WriteImpl() = 0; |
| 98 | 103 |
| 99 // Platform-specific read cancelation. | 104 // Platform-specific read cancelation. |
| 100 virtual void CancelReadImpl() = 0; | 105 virtual void CancelReadImpl() = 0; |
| 101 | 106 |
| 102 // Platform-specific write cancelation. | 107 // Platform-specific write cancelation. |
| 103 virtual void CancelWriteImpl() = 0; | 108 virtual void CancelWriteImpl() = 0; |
| 104 | 109 |
| 110 // Requests access to the underlying serial device, if needed. |
| 111 virtual void RequestAccess( |
| 112 const std::string& port, |
| 113 scoped_refptr<base::MessageLoopProxy> file_message_loop, |
| 114 scoped_refptr<base::MessageLoopProxy> ui_message_loop); |
| 115 |
| 105 // Performs platform-specific, one-time port configuration on open. | 116 // Performs platform-specific, one-time port configuration on open. |
| 106 virtual bool PostOpen(); | 117 virtual bool PostOpen(); |
| 107 | 118 |
| 108 // Called by the implementation to signal that the active read has completed. | 119 // Called by the implementation to signal that the active read has completed. |
| 109 // WARNING: Calling this method can destroy the SerialIoHandler instance | 120 // WARNING: Calling this method can destroy the SerialIoHandler instance |
| 110 // if the associated I/O operation was the only thing keeping it alive. | 121 // if the associated I/O operation was the only thing keeping it alive. |
| 111 void ReadCompleted(int bytes_read, serial::ReceiveError error); | 122 void ReadCompleted(int bytes_read, serial::ReceiveError error); |
| 112 | 123 |
| 113 // Called by the implementation to signal that the active write has completed. | 124 // Called by the implementation to signal that the active write has completed. |
| 114 // WARNING: Calling this method may destroy the SerialIoHandler instance | 125 // WARNING: Calling this method may destroy the SerialIoHandler instance |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 180 bool read_canceled_; | 191 bool read_canceled_; |
| 181 | 192 |
| 182 scoped_ptr<ReadOnlyBuffer> pending_write_buffer_; | 193 scoped_ptr<ReadOnlyBuffer> pending_write_buffer_; |
| 183 serial::SendError write_cancel_reason_; | 194 serial::SendError write_cancel_reason_; |
| 184 bool write_canceled_; | 195 bool write_canceled_; |
| 185 | 196 |
| 186 // Callback to handle the completion of a pending Open() request. | 197 // Callback to handle the completion of a pending Open() request. |
| 187 OpenCompleteCallback open_complete_; | 198 OpenCompleteCallback open_complete_; |
| 188 | 199 |
| 189 scoped_refptr<base::MessageLoopProxy> file_thread_message_loop_; | 200 scoped_refptr<base::MessageLoopProxy> file_thread_message_loop_; |
| 201 // On Chrome OS, PermissionBrokerClient should be called on the UI thread. |
| 202 scoped_refptr<base::MessageLoopProxy> ui_thread_message_loop_; |
| 190 | 203 |
| 191 DISALLOW_COPY_AND_ASSIGN(SerialIoHandler); | 204 DISALLOW_COPY_AND_ASSIGN(SerialIoHandler); |
| 192 }; | 205 }; |
| 193 | 206 |
| 194 } // namespace device | 207 } // namespace device |
| 195 | 208 |
| 196 #endif // DEVICE_SERIAL_SERIAL_IO_HANDLER_H_ | 209 #endif // DEVICE_SERIAL_SERIAL_IO_HANDLER_H_ |
| OLD | NEW |