OLD | NEW |
---|---|
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 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 #include "chrome/browser/extensions/api/serial/serial_connection.h" | 5 #include "chrome/browser/extensions/api/serial/serial_connection.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
10 #include "base/lazy_instance.h" | 10 #include "base/lazy_instance.h" |
11 #include "chrome/common/extensions/api/serial.h" | 11 #include "chrome/common/extensions/api/serial.h" |
12 #include "extensions/browser/api/api_resource_manager.h" | 12 #include "extensions/browser/api/api_resource_manager.h" |
13 | 13 |
14 namespace extensions { | 14 namespace extensions { |
15 | 15 |
16 namespace { | 16 namespace { |
17 | 17 |
18 const int kDefaultBufferSize = 4096; | 18 const int kDefaultBufferSize = 4096; |
19 | |
20 api::serial::SendError ConvertSendErrorFromMojo(device::SendError input) { | |
21 switch (input) { | |
22 case device::SEND_ERROR_NONE: | |
23 default: | |
raymes
2014/07/02 00:57:48
It's often good to get rid of the "default" case s
Sam McNally
2014/07/02 04:50:49
Done.
| |
24 return api::serial::SEND_ERROR_NONE; | |
25 case device::SEND_ERROR_DISCONNECTED: | |
26 return api::serial::SEND_ERROR_DISCONNECTED; | |
27 case device::SEND_ERROR_PENDING: | |
28 return api::serial::SEND_ERROR_PENDING; | |
29 case device::SEND_ERROR_TIMEOUT: | |
30 return api::serial::SEND_ERROR_TIMEOUT; | |
31 case device::SEND_ERROR_SYSTEM_ERROR: | |
32 return api::serial::SEND_ERROR_SYSTEM_ERROR; | |
33 } | |
19 } | 34 } |
20 | 35 |
36 api::serial::ReceiveError ConvertReceiveErrorFromMojo( | |
37 device::ReceiveError input) { | |
38 switch (input) { | |
39 case device::RECEIVE_ERROR_NONE: | |
40 default: | |
41 return api::serial::RECEIVE_ERROR_NONE; | |
42 case device::RECEIVE_ERROR_DISCONNECTED: | |
43 return api::serial::RECEIVE_ERROR_DISCONNECTED; | |
44 case device::RECEIVE_ERROR_TIMEOUT: | |
45 return api::serial::RECEIVE_ERROR_TIMEOUT; | |
46 case device::RECEIVE_ERROR_DEVICE_LOST: | |
47 return api::serial::RECEIVE_ERROR_DEVICE_LOST; | |
48 case device::RECEIVE_ERROR_SYSTEM_ERROR: | |
49 return api::serial::RECEIVE_ERROR_SYSTEM_ERROR; | |
50 } | |
51 } | |
52 | |
53 api::serial::DataBits ConvertDataBitsFromMojo(device::DataBits input) { | |
54 switch (input) { | |
55 case device::DATA_BITS_NONE: | |
56 default: | |
57 return api::serial::DATA_BITS_NONE; | |
58 case device::DATA_BITS_SEVEN: | |
59 return api::serial::DATA_BITS_SEVEN; | |
60 case device::DATA_BITS_EIGHT: | |
61 return api::serial::DATA_BITS_EIGHT; | |
62 } | |
63 } | |
64 | |
65 device::DataBits ConvertDataBitsToMojo(api::serial::DataBits input) { | |
66 switch (input) { | |
67 case api::serial::DATA_BITS_NONE: | |
68 default: | |
69 return device::DATA_BITS_NONE; | |
70 case api::serial::DATA_BITS_SEVEN: | |
71 return device::DATA_BITS_SEVEN; | |
72 case api::serial::DATA_BITS_EIGHT: | |
73 return device::DATA_BITS_EIGHT; | |
74 } | |
75 } | |
76 | |
77 api::serial::ParityBit ConvertParityBitFromMojo(device::ParityBit input) { | |
78 switch (input) { | |
79 case device::PARITY_BIT_NONE: | |
80 default: | |
81 return api::serial::PARITY_BIT_NONE; | |
82 case device::PARITY_BIT_ODD: | |
83 return api::serial::PARITY_BIT_ODD; | |
84 case device::PARITY_BIT_NO: | |
85 return api::serial::PARITY_BIT_NO; | |
86 case device::PARITY_BIT_EVEN: | |
87 return api::serial::PARITY_BIT_EVEN; | |
88 } | |
89 } | |
90 | |
91 device::ParityBit ConvertParityBitToMojo(api::serial::ParityBit input) { | |
92 switch (input) { | |
93 case api::serial::PARITY_BIT_NONE: | |
94 default: | |
95 return device::PARITY_BIT_NONE; | |
96 case api::serial::PARITY_BIT_NO: | |
97 return device::PARITY_BIT_NO; | |
98 case api::serial::PARITY_BIT_ODD: | |
99 return device::PARITY_BIT_ODD; | |
100 case api::serial::PARITY_BIT_EVEN: | |
101 return device::PARITY_BIT_EVEN; | |
102 } | |
103 } | |
104 | |
105 api::serial::StopBits ConvertStopBitsFromMojo(device::StopBits input) { | |
106 switch (input) { | |
107 case device::STOP_BITS_NONE: | |
108 default: | |
109 return api::serial::STOP_BITS_NONE; | |
110 case device::STOP_BITS_ONE: | |
111 return api::serial::STOP_BITS_ONE; | |
112 case device::STOP_BITS_TWO: | |
113 return api::serial::STOP_BITS_TWO; | |
114 } | |
115 } | |
116 | |
117 device::StopBits ConvertStopBitsToMojo(api::serial::StopBits input) { | |
118 switch (input) { | |
119 case api::serial::STOP_BITS_NONE: | |
120 default: | |
121 return device::STOP_BITS_NONE; | |
122 case api::serial::STOP_BITS_ONE: | |
123 return device::STOP_BITS_ONE; | |
124 case api::serial::STOP_BITS_TWO: | |
125 return device::STOP_BITS_TWO; | |
126 } | |
127 } | |
128 | |
129 } // namespace | |
130 | |
21 static base::LazyInstance< | 131 static base::LazyInstance< |
22 BrowserContextKeyedAPIFactory<ApiResourceManager<SerialConnection> > > | 132 BrowserContextKeyedAPIFactory<ApiResourceManager<SerialConnection> > > |
23 g_factory = LAZY_INSTANCE_INITIALIZER; | 133 g_factory = LAZY_INSTANCE_INITIALIZER; |
24 | 134 |
25 // static | 135 // static |
26 template <> | 136 template <> |
27 BrowserContextKeyedAPIFactory<ApiResourceManager<SerialConnection> >* | 137 BrowserContextKeyedAPIFactory<ApiResourceManager<SerialConnection> >* |
28 ApiResourceManager<SerialConnection>::GetFactoryInstance() { | 138 ApiResourceManager<SerialConnection>::GetFactoryInstance() { |
29 return g_factory.Pointer(); | 139 return g_factory.Pointer(); |
30 } | 140 } |
31 | 141 |
32 SerialConnection::SerialConnection(const std::string& port, | 142 SerialConnection::SerialConnection(const std::string& port, |
33 const std::string& owner_extension_id) | 143 const std::string& owner_extension_id) |
34 : ApiResource(owner_extension_id), | 144 : ApiResource(owner_extension_id), |
35 port_(port), | 145 port_(port), |
36 persistent_(false), | 146 persistent_(false), |
37 buffer_size_(kDefaultBufferSize), | 147 buffer_size_(kDefaultBufferSize), |
38 receive_timeout_(0), | 148 receive_timeout_(0), |
39 send_timeout_(0), | 149 send_timeout_(0), |
40 paused_(false), | 150 paused_(false), |
41 io_handler_(SerialIoHandler::Create()) { | 151 io_handler_(SerialIoHandler::Create()) { |
42 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 152 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
43 io_handler_->Initialize( | 153 io_handler_->Initialize( |
44 base::Bind(&SerialConnection::OnAsyncReadComplete, AsWeakPtr()), | 154 base::Bind(&SerialConnection::OnAsyncReadComplete, AsWeakPtr()), |
45 base::Bind(&SerialConnection::OnAsyncWriteComplete, AsWeakPtr())); | 155 base::Bind(&SerialConnection::OnAsyncWriteComplete, AsWeakPtr())); |
46 } | 156 } |
47 | 157 |
48 SerialConnection::~SerialConnection() { | 158 SerialConnection::~SerialConnection() { |
49 io_handler_->CancelRead(api::serial::RECEIVE_ERROR_DISCONNECTED); | 159 io_handler_->CancelRead(device::RECEIVE_ERROR_DISCONNECTED); |
50 io_handler_->CancelWrite(api::serial::SEND_ERROR_DISCONNECTED); | 160 io_handler_->CancelWrite(device::SEND_ERROR_DISCONNECTED); |
51 } | 161 } |
52 | 162 |
53 bool SerialConnection::IsPersistent() const { return persistent(); } | 163 bool SerialConnection::IsPersistent() const { return persistent(); } |
54 | 164 |
55 void SerialConnection::set_buffer_size(int buffer_size) { | 165 void SerialConnection::set_buffer_size(int buffer_size) { |
56 buffer_size_ = buffer_size; | 166 buffer_size_ = buffer_size; |
57 } | 167 } |
58 | 168 |
59 void SerialConnection::set_receive_timeout(int receive_timeout) { | 169 void SerialConnection::set_receive_timeout(int receive_timeout) { |
60 receive_timeout_ = receive_timeout; | 170 receive_timeout_ = receive_timeout; |
61 } | 171 } |
62 | 172 |
63 void SerialConnection::set_send_timeout(int send_timeout) { | 173 void SerialConnection::set_send_timeout(int send_timeout) { |
64 send_timeout_ = send_timeout; | 174 send_timeout_ = send_timeout; |
65 } | 175 } |
66 | 176 |
67 void SerialConnection::set_paused(bool paused) { | 177 void SerialConnection::set_paused(bool paused) { |
68 paused_ = paused; | 178 paused_ = paused; |
69 if (paused) { | 179 if (paused) { |
70 io_handler_->CancelRead(api::serial::RECEIVE_ERROR_NONE); | 180 io_handler_->CancelRead(device::RECEIVE_ERROR_NONE); |
71 } | 181 } |
72 } | 182 } |
73 | 183 |
74 void SerialConnection::Open(const OpenCompleteCallback& callback) { | 184 void SerialConnection::Open(const OpenCompleteCallback& callback) { |
75 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 185 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
76 io_handler_->Open(port_, callback); | 186 io_handler_->Open(port_, callback); |
77 } | 187 } |
78 | 188 |
79 bool SerialConnection::Receive(const ReceiveCompleteCallback& callback) { | 189 bool SerialConnection::Receive(const ReceiveCompleteCallback& callback) { |
80 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 190 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
113 if (options.persistent.get()) | 223 if (options.persistent.get()) |
114 set_persistent(*options.persistent); | 224 set_persistent(*options.persistent); |
115 if (options.name.get()) | 225 if (options.name.get()) |
116 set_name(*options.name); | 226 set_name(*options.name); |
117 if (options.buffer_size.get()) | 227 if (options.buffer_size.get()) |
118 set_buffer_size(*options.buffer_size); | 228 set_buffer_size(*options.buffer_size); |
119 if (options.receive_timeout.get()) | 229 if (options.receive_timeout.get()) |
120 set_receive_timeout(*options.receive_timeout); | 230 set_receive_timeout(*options.receive_timeout); |
121 if (options.send_timeout.get()) | 231 if (options.send_timeout.get()) |
122 set_send_timeout(*options.send_timeout); | 232 set_send_timeout(*options.send_timeout); |
123 bool success = io_handler_->ConfigurePort(options); | 233 bool success = |
124 io_handler_->CancelRead(api::serial::RECEIVE_ERROR_NONE); | 234 io_handler_->ConfigurePort(*device::ConnectionOptions::From(options)); |
235 io_handler_->CancelRead(device::RECEIVE_ERROR_NONE); | |
125 return success; | 236 return success; |
126 } | 237 } |
127 | 238 |
128 void SerialConnection::SetIoHandlerForTest( | 239 void SerialConnection::SetIoHandlerForTest( |
129 scoped_refptr<SerialIoHandler> handler) { | 240 scoped_refptr<SerialIoHandler> handler) { |
130 io_handler_ = handler; | 241 io_handler_ = handler; |
131 io_handler_->Initialize( | 242 io_handler_->Initialize( |
132 base::Bind(&SerialConnection::OnAsyncReadComplete, AsWeakPtr()), | 243 base::Bind(&SerialConnection::OnAsyncReadComplete, AsWeakPtr()), |
133 base::Bind(&SerialConnection::OnAsyncWriteComplete, AsWeakPtr())); | 244 base::Bind(&SerialConnection::OnAsyncWriteComplete, AsWeakPtr())); |
134 } | 245 } |
135 | 246 |
136 bool SerialConnection::GetInfo(api::serial::ConnectionInfo* info) const { | 247 bool SerialConnection::GetInfo(api::serial::ConnectionInfo* info) const { |
137 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 248 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
138 info->paused = paused_; | 249 info->paused = paused_; |
139 info->persistent = persistent_; | 250 info->persistent = persistent_; |
140 info->name = name_; | 251 info->name = name_; |
141 info->buffer_size = buffer_size_; | 252 info->buffer_size = buffer_size_; |
142 info->receive_timeout = receive_timeout_; | 253 info->receive_timeout = receive_timeout_; |
143 info->send_timeout = send_timeout_; | 254 info->send_timeout = send_timeout_; |
144 return io_handler_->GetPortInfo(info); | 255 device::ConnectionInfoPtr port_info = io_handler_->GetPortInfo(); |
256 if (!port_info) | |
257 return false; | |
258 | |
259 info->bitrate.reset(new int(port_info->bitrate)); | |
260 info->data_bits = ConvertDataBitsFromMojo(port_info->data_bits); | |
261 info->parity_bit = ConvertParityBitFromMojo(port_info->parity_bit); | |
262 info->stop_bits = ConvertStopBitsFromMojo(port_info->stop_bits); | |
263 info->cts_flow_control.reset(new bool(port_info->cts_flow_control)); | |
264 return true; | |
145 } | 265 } |
146 | 266 |
147 bool SerialConnection::Flush() const { | 267 bool SerialConnection::Flush() const { |
148 return io_handler_->Flush(); | 268 return io_handler_->Flush(); |
149 } | 269 } |
150 | 270 |
151 bool SerialConnection::GetControlSignals( | 271 bool SerialConnection::GetControlSignals( |
152 api::serial::DeviceControlSignals* control_signals) const { | 272 api::serial::DeviceControlSignals* control_signals) const { |
153 return io_handler_->GetControlSignals(control_signals); | 273 device::DeviceControlSignalsPtr signals = io_handler_->GetControlSignals(); |
274 if (!signals) | |
275 return false; | |
276 | |
277 control_signals->dcd = signals->dcd; | |
278 control_signals->cts = signals->cts; | |
279 control_signals->ri = signals->ri; | |
280 control_signals->dsr = signals->dsr; | |
281 return true; | |
154 } | 282 } |
155 | 283 |
156 bool SerialConnection::SetControlSignals( | 284 bool SerialConnection::SetControlSignals( |
157 const api::serial::HostControlSignals& control_signals) { | 285 const api::serial::HostControlSignals& control_signals) { |
158 return io_handler_->SetControlSignals(control_signals); | 286 return io_handler_->SetControlSignals( |
287 *device::HostControlSignals::From(control_signals)); | |
159 } | 288 } |
160 | 289 |
161 void SerialConnection::OnReceiveTimeout() { | 290 void SerialConnection::OnReceiveTimeout() { |
162 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 291 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
163 io_handler_->CancelRead(api::serial::RECEIVE_ERROR_TIMEOUT); | 292 io_handler_->CancelRead(device::RECEIVE_ERROR_TIMEOUT); |
164 } | 293 } |
165 | 294 |
166 void SerialConnection::OnSendTimeout() { | 295 void SerialConnection::OnSendTimeout() { |
167 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 296 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
168 io_handler_->CancelWrite(api::serial::SEND_ERROR_TIMEOUT); | 297 io_handler_->CancelWrite(device::SEND_ERROR_TIMEOUT); |
169 } | 298 } |
170 | 299 |
171 void SerialConnection::OnAsyncReadComplete(const std::string& data, | 300 void SerialConnection::OnAsyncReadComplete(const std::string& data, |
172 api::serial::ReceiveError error) { | 301 device::ReceiveError error) { |
173 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 302 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
174 DCHECK(!receive_complete_.is_null()); | 303 DCHECK(!receive_complete_.is_null()); |
175 ReceiveCompleteCallback callback = receive_complete_; | 304 ReceiveCompleteCallback callback = receive_complete_; |
176 receive_complete_.Reset(); | 305 receive_complete_.Reset(); |
177 receive_timeout_task_.reset(); | 306 receive_timeout_task_.reset(); |
178 callback.Run(data, error); | 307 callback.Run(data, ConvertReceiveErrorFromMojo(error)); |
179 } | 308 } |
180 | 309 |
181 void SerialConnection::OnAsyncWriteComplete(int bytes_sent, | 310 void SerialConnection::OnAsyncWriteComplete(int bytes_sent, |
182 api::serial::SendError error) { | 311 device::SendError error) { |
183 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 312 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
184 DCHECK(!send_complete_.is_null()); | 313 DCHECK(!send_complete_.is_null()); |
185 SendCompleteCallback callback = send_complete_; | 314 SendCompleteCallback callback = send_complete_; |
186 send_complete_.Reset(); | 315 send_complete_.Reset(); |
187 send_timeout_task_.reset(); | 316 send_timeout_task_.reset(); |
188 callback.Run(bytes_sent, error); | 317 callback.Run(bytes_sent, ConvertSendErrorFromMojo(error)); |
189 } | 318 } |
190 | 319 |
191 SerialConnection::TimeoutTask::TimeoutTask(const base::Closure& closure, | 320 SerialConnection::TimeoutTask::TimeoutTask(const base::Closure& closure, |
192 const base::TimeDelta& delay) | 321 const base::TimeDelta& delay) |
193 : weak_factory_(this), closure_(closure), delay_(delay) { | 322 : weak_factory_(this), closure_(closure), delay_(delay) { |
194 base::MessageLoop::current()->PostDelayedTask( | 323 base::MessageLoop::current()->PostDelayedTask( |
195 FROM_HERE, | 324 FROM_HERE, |
196 base::Bind(&TimeoutTask::Run, weak_factory_.GetWeakPtr()), | 325 base::Bind(&TimeoutTask::Run, weak_factory_.GetWeakPtr()), |
197 delay_); | 326 delay_); |
198 } | 327 } |
199 | 328 |
200 SerialConnection::TimeoutTask::~TimeoutTask() {} | 329 SerialConnection::TimeoutTask::~TimeoutTask() {} |
201 | 330 |
202 void SerialConnection::TimeoutTask::Run() const { closure_.Run(); } | 331 void SerialConnection::TimeoutTask::Run() const { closure_.Run(); } |
203 | 332 |
204 } // namespace extensions | 333 } // namespace extensions |
334 | |
335 namespace mojo { | |
336 | |
337 // static | |
338 device::HostControlSignalsPtr | |
339 TypeConverter<device::HostControlSignalsPtr, | |
340 extensions::api::serial::HostControlSignals>:: | |
341 ConvertFrom(const extensions::api::serial::HostControlSignals& input) { | |
342 device::HostControlSignalsPtr output(device::HostControlSignals::New()); | |
343 if (input.dtr.get()) { | |
344 output->has_dtr = true; | |
345 output->dtr = *input.dtr; | |
346 } | |
347 if (input.rts.get()) { | |
348 output->has_rts = true; | |
349 output->rts = *input.rts; | |
350 } | |
351 return output.Pass(); | |
352 } | |
353 | |
354 // static | |
355 device::ConnectionOptionsPtr | |
356 TypeConverter<device::ConnectionOptionsPtr, | |
357 extensions::api::serial::ConnectionOptions>:: | |
358 ConvertFrom(const extensions::api::serial::ConnectionOptions& input) { | |
359 device::ConnectionOptionsPtr output(device::ConnectionOptions::New()); | |
360 if (input.bitrate.get() && *input.bitrate > 0) | |
361 output->bitrate = *input.bitrate; | |
362 output->data_bits = extensions::ConvertDataBitsToMojo(input.data_bits); | |
363 output->parity_bit = extensions::ConvertParityBitToMojo(input.parity_bit); | |
364 output->stop_bits = extensions::ConvertStopBitsToMojo(input.stop_bits); | |
365 if (input.cts_flow_control.get()) { | |
366 output->has_cts_flow_control = true; | |
367 output->cts_flow_control = *input.cts_flow_control; | |
368 } | |
369 return output.Pass(); | |
370 } | |
371 | |
372 } // namespace mojo | |
OLD | NEW |