Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(133)

Side by Side Diff: chrome/browser/extensions/api/serial/serial_io_handler.h

Issue 363583002: Convert SerialIoHandler to use Mojo types. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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<void(const std::string& data,
32 api::serial::ReceiveError error)> 32 device::ReceiveError error)> ReadCompleteCallback;
33 ReadCompleteCallback;
34 33
35 // Called with the number of bytes written and a result code. Note that an 34 // Called with the number of bytes written and a result code. Note that an
36 // error result does not necessarily imply 0 bytes written. 35 // error result does not necessarily imply 0 bytes written.
37 typedef base::Callback<void(int bytes_written, api::serial::SendError error)> 36 typedef base::Callback<void(int bytes_written, device::SendError error)>
38 WriteCompleteCallback; 37 WriteCompleteCallback;
39 38
40 // Initializes the handler on the current message loop. Must be called exactly 39 // Initializes the handler on the current message loop. Must be called exactly
41 // once before performing any I/O through the handler. 40 // once before performing any I/O through the handler.
42 virtual void Initialize(const ReadCompleteCallback& read_callback, 41 virtual void Initialize(const ReadCompleteCallback& read_callback,
43 const WriteCompleteCallback& write_callback); 42 const WriteCompleteCallback& write_callback);
44 43
45 // Initiates an asynchronous Open of the device. 44 // Initiates an asynchronous Open of the device.
46 virtual void Open(const std::string& port, 45 virtual void Open(const std::string& port,
47 const OpenCompleteCallback& callback); 46 const OpenCompleteCallback& callback);
48 47
49 // Performs an async Read operation. Behavior is undefined if this is called 48 // Performs an async Read operation. Behavior is undefined if this is called
50 // while a Read is already pending. Otherwise, the ReadCompleteCallback 49 // while a Read is already pending. Otherwise, the ReadCompleteCallback
51 // (see above) will eventually be called with a result. 50 // (see above) will eventually be called with a result.
52 void Read(int max_bytes); 51 void Read(int max_bytes);
53 52
54 // Performs an async Write operation. Behavior is undefined if this is called 53 // Performs an async Write operation. Behavior is undefined if this is called
55 // while a Write is already pending. Otherwise, the WriteCompleteCallback 54 // while a Write is already pending. Otherwise, the WriteCompleteCallback
56 // (see above) will eventually be called with a result. 55 // (see above) will eventually be called with a result.
57 void Write(const std::string& data); 56 void Write(const std::string& data);
58 57
59 // Indicates whether or not a read is currently pending. 58 // Indicates whether or not a read is currently pending.
60 bool IsReadPending() const; 59 bool IsReadPending() const;
61 60
62 // Indicates whether or not a write is currently pending. 61 // Indicates whether or not a write is currently pending.
63 bool IsWritePending() const; 62 bool IsWritePending() const;
64 63
65 // Attempts to cancel a pending read operation. 64 // Attempts to cancel a pending read operation.
66 void CancelRead(api::serial::ReceiveError reason); 65 void CancelRead(device::ReceiveError reason);
67 66
68 // Attempts to cancel a pending write operation. 67 // Attempts to cancel a pending write operation.
69 void CancelWrite(api::serial::SendError reason); 68 void CancelWrite(device::SendError reason);
70 69
71 // Flushes input and output buffers. 70 // Flushes input and output buffers.
72 virtual bool Flush() const = 0; 71 virtual bool Flush() const = 0;
73 72
74 // Reads current control signals (DCD, CTS, etc.) into an existing 73 // Reads current control signals (DCD, CTS, etc.) into an existing
75 // DeviceControlSignals structure. Returns |true| iff the signals were 74 // DeviceControlSignals structure. Returns |true| iff the signals were
76 // successfully read. 75 // successfully read.
77 virtual bool GetControlSignals( 76 virtual device::DeviceControlSignalsPtr GetControlSignals() const = 0;
78 api::serial::DeviceControlSignals* control_signals) const = 0;
79 77
80 // Sets one or more control signals (DTR and/or RTS). Returns |true| iff 78 // Sets one or more control signals (DTR and/or RTS). Returns |true| iff
81 // the signals were successfully set. Unininitialized flags in the 79 // the signals were successfully set. Unininitialized flags in the
82 // HostControlSignals structure are left unchanged. 80 // HostControlSignals structure are left unchanged.
83 virtual bool SetControlSignals( 81 virtual bool SetControlSignals(
84 const api::serial::HostControlSignals& control_signals) = 0; 82 const device::HostControlSignals& control_signals) = 0;
85 83
86 // Performs platform-specific port configuration. Returns |true| iff 84 // Performs platform-specific port configuration. Returns |true| iff
87 // configuration was successful. 85 // configuration was successful.
88 virtual bool ConfigurePort(const api::serial::ConnectionOptions& options) = 0; 86 virtual bool ConfigurePort(const device::ConnectionOptions& options) = 0;
89 87
90 // Performs a platform-specific port configuration query. Fills values in an 88 // Performs a platform-specific port configuration query. Fills values in an
91 // existing ConnectionInfo. Returns |true| iff port configuration was 89 // existing ConnectionInfo. Returns |true| iff port configuration was
92 // successfully retrieved. 90 // successfully retrieved.
93 virtual bool GetPortInfo(api::serial::ConnectionInfo* info) const = 0; 91 virtual device::ConnectionInfoPtr GetPortInfo() const = 0;
94 92
95 protected: 93 protected:
96 SerialIoHandler(); 94 SerialIoHandler();
97 virtual ~SerialIoHandler(); 95 virtual ~SerialIoHandler();
98 96
99 // Performs a platform-specific read operation. This must guarantee that 97 // Performs a platform-specific read operation. This must guarantee that
100 // ReadCompleted is called when the underlying async operation is completed 98 // ReadCompleted is called when the underlying async operation is completed
101 // or the SerialIoHandler instance will leak. 99 // or the SerialIoHandler instance will leak.
102 // NOTE: Implementations of ReadImpl should never call ReadCompleted directly. 100 // NOTE: Implementations of ReadImpl should never call ReadCompleted directly.
103 // Use QueueReadCompleted instead to avoid reentrancy. 101 // Use QueueReadCompleted instead to avoid reentrancy.
(...skipping 11 matching lines...) Expand all
115 113
116 // Platform-specific write cancelation. 114 // Platform-specific write cancelation.
117 virtual void CancelWriteImpl() = 0; 115 virtual void CancelWriteImpl() = 0;
118 116
119 // Performs platform-specific, one-time port configuration on open. 117 // Performs platform-specific, one-time port configuration on open.
120 virtual bool PostOpen(); 118 virtual bool PostOpen();
121 119
122 // Called by the implementation to signal that the active read has completed. 120 // Called by the implementation to signal that the active read has completed.
123 // WARNING: Calling this method can destroy the SerialIoHandler instance 121 // WARNING: Calling this method can destroy the SerialIoHandler instance
124 // if the associated I/O operation was the only thing keeping it alive. 122 // if the associated I/O operation was the only thing keeping it alive.
125 void ReadCompleted(int bytes_read, api::serial::ReceiveError error); 123 void ReadCompleted(int bytes_read, device::ReceiveError error);
126 124
127 // Called by the implementation to signal that the active write has completed. 125 // Called by the implementation to signal that the active write has completed.
128 // WARNING: Calling this method may destroy the SerialIoHandler instance 126 // WARNING: Calling this method may destroy the SerialIoHandler instance
129 // 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.
130 void WriteCompleted(int bytes_written, api::serial::SendError error); 128 void WriteCompleted(int bytes_written, device::SendError error);
131 129
132 // Queues a ReadCompleted call on the current thread. This is used to allow 130 // 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, 131 // ReadImpl to immediately signal completion with 0 bytes and an error,
134 // without being reentrant. 132 // without being reentrant.
135 void QueueReadCompleted(int bytes_read, api::serial::ReceiveError error); 133 void QueueReadCompleted(int bytes_read, device::ReceiveError error);
136 134
137 // Queues a WriteCompleted call on the current thread. This is used to allow 135 // 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, 136 // WriteImpl to immediately signal completion with 0 bytes and an error,
139 // without being reentrant. 137 // without being reentrant.
140 void QueueWriteCompleted(int bytes_written, api::serial::SendError error); 138 void QueueWriteCompleted(int bytes_written, device::SendError error);
141 139
142 const base::File& file() const { return file_; } 140 const base::File& file() const { return file_; }
143 141
144 net::IOBuffer* pending_read_buffer() const { 142 net::IOBuffer* pending_read_buffer() const {
145 return pending_read_buffer_.get(); 143 return pending_read_buffer_.get();
146 } 144 }
147 145
148 int pending_read_buffer_len() const { 146 int pending_read_buffer_len() const {
149 return pending_read_buffer_len_; 147 return pending_read_buffer_len_;
150 } 148 }
151 149
152 api::serial::ReceiveError read_cancel_reason() const { 150 device::ReceiveError read_cancel_reason() const {
153 return read_cancel_reason_; 151 return read_cancel_reason_;
154 } 152 }
155 153
156 bool read_canceled() const { 154 bool read_canceled() const {
157 return read_canceled_; 155 return read_canceled_;
158 } 156 }
159 157
160 net::IOBuffer* pending_write_buffer() const { 158 net::IOBuffer* pending_write_buffer() const {
161 return pending_write_buffer_.get(); 159 return pending_write_buffer_.get();
162 } 160 }
163 161
164 int pending_write_buffer_len() const { 162 int pending_write_buffer_len() const {
165 return pending_write_buffer_len_; 163 return pending_write_buffer_len_;
166 } 164 }
167 165
168 api::serial::SendError write_cancel_reason() const { 166 device::SendError write_cancel_reason() const { return write_cancel_reason_; }
169 return write_cancel_reason_;
170 }
171 167
172 bool write_canceled() const { 168 bool write_canceled() const {
173 return write_canceled_; 169 return write_canceled_;
174 } 170 }
175 171
176 // Possibly fixes up a serial port path name in a platform-specific manner. 172 // Possibly fixes up a serial port path name in a platform-specific manner.
177 static std::string MaybeFixUpPortName(const std::string& port_name); 173 static std::string MaybeFixUpPortName(const std::string& port_name);
178 174
179 private: 175 private:
180 friend class base::RefCounted<SerialIoHandler>; 176 friend class base::RefCounted<SerialIoHandler>;
181 177
182 // Continues an Open operation on the FILE thread. 178 // Continues an Open operation on the FILE thread.
183 void StartOpen(const std::string& port); 179 void StartOpen(const std::string& port);
184 180
185 // Finalizes an Open operation (continued from StartOpen) on the IO thread. 181 // Finalizes an Open operation (continued from StartOpen) on the IO thread.
186 void FinishOpen(base::File file); 182 void FinishOpen(base::File file);
187 183
188 void Close(); 184 void Close();
189 185
190 // Continues a Close operation on the FILE thread. 186 // Continues a Close operation on the FILE thread.
191 static void DoClose(base::File port); 187 static void DoClose(base::File port);
192 188
193 // File for the opened serial device. This value is only modified from the IO 189 // File for the opened serial device. This value is only modified from the IO
194 // thread. 190 // thread.
195 base::File file_; 191 base::File file_;
196 192
197 scoped_refptr<net::IOBuffer> pending_read_buffer_; 193 scoped_refptr<net::IOBuffer> pending_read_buffer_;
198 int pending_read_buffer_len_; 194 int pending_read_buffer_len_;
199 api::serial::ReceiveError read_cancel_reason_; 195 device::ReceiveError read_cancel_reason_;
200 bool read_canceled_; 196 bool read_canceled_;
201 197
202 scoped_refptr<net::IOBuffer> pending_write_buffer_; 198 scoped_refptr<net::IOBuffer> pending_write_buffer_;
203 int pending_write_buffer_len_; 199 int pending_write_buffer_len_;
204 api::serial::SendError write_cancel_reason_; 200 device::SendError write_cancel_reason_;
205 bool write_canceled_; 201 bool write_canceled_;
206 202
207 ReadCompleteCallback read_complete_; 203 ReadCompleteCallback read_complete_;
208 WriteCompleteCallback write_complete_; 204 WriteCompleteCallback write_complete_;
209 205
210 // Callback to handle the completion of a pending Open() request. 206 // Callback to handle the completion of a pending Open() request.
211 OpenCompleteCallback open_complete_; 207 OpenCompleteCallback open_complete_;
212 208
213 DISALLOW_COPY_AND_ASSIGN(SerialIoHandler); 209 DISALLOW_COPY_AND_ASSIGN(SerialIoHandler);
214 }; 210 };
215 211
216 } // namespace extensions 212 } // namespace extensions
217 213
218 #endif // CHROME_BROWSER_EXTENSIONS_API_SERIAL_SERIAL_IO_HANDLER_H_ 214 #endif // CHROME_BROWSER_EXTENSIONS_API_SERIAL_SERIAL_IO_HANDLER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698