OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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_api.h" | 5 #include "chrome/browser/extensions/api/serial/serial_api.h" |
6 | 6 |
7 #include "base/values.h" | 7 #include "base/values.h" |
8 #include "chrome/browser/extensions/api/api_resource_controller.h" | 8 #include "chrome/browser/extensions/api/api_resource_controller.h" |
9 #include "chrome/browser/extensions/api/serial/serial_connection.h" | 9 #include "chrome/browser/extensions/api/serial/serial_connection.h" |
10 #include "chrome/browser/extensions/api/serial/serial_port_enumerator.h" | 10 #include "chrome/browser/extensions/api/serial/serial_port_enumerator.h" |
(...skipping 30 matching lines...) Expand all Loading... |
41 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 41 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
42 | 42 |
43 ListValue* ports = new ListValue(); | 43 ListValue* ports = new ListValue(); |
44 SerialPortEnumerator::StringSet port_names = | 44 SerialPortEnumerator::StringSet port_names = |
45 SerialPortEnumerator::GenerateValidSerialPortNames(); | 45 SerialPortEnumerator::GenerateValidSerialPortNames(); |
46 SerialPortEnumerator::StringSet::const_iterator i = port_names.begin(); | 46 SerialPortEnumerator::StringSet::const_iterator i = port_names.begin(); |
47 while (i != port_names.end()) { | 47 while (i != port_names.end()) { |
48 ports->Append(Value::CreateStringValue(*i++)); | 48 ports->Append(Value::CreateStringValue(*i++)); |
49 } | 49 } |
50 | 50 |
51 result_.reset(ports); | 51 SetResult(ports); |
52 } | 52 } |
53 | 53 |
54 bool SerialGetPortsFunction::Respond() { | 54 bool SerialGetPortsFunction::Respond() { |
55 return true; | 55 return true; |
56 } | 56 } |
57 | 57 |
58 // It's a fool's errand to come up with a default bitrate, because we don't get | 58 // It's a fool's errand to come up with a default bitrate, because we don't get |
59 // to control both sides of the communication. Unless the other side has | 59 // to control both sides of the communication. Unless the other side has |
60 // implemented auto-bitrate detection (rare), if we pick the wrong rate, then | 60 // implemented auto-bitrate detection (rare), if we pick the wrong rate, then |
61 // you're gonna have a bad time. Close doesn't count. | 61 // you're gonna have a bad time. Close doesn't count. |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
108 | 108 |
109 bool open_result = serial_connection->Open(); | 109 bool open_result = serial_connection->Open(); |
110 if (!open_result) { | 110 if (!open_result) { |
111 serial_connection->Close(); | 111 serial_connection->Close(); |
112 controller()->RemoveSerialConnection(id); | 112 controller()->RemoveSerialConnection(id); |
113 id = -1; | 113 id = -1; |
114 } | 114 } |
115 | 115 |
116 DictionaryValue* result = new DictionaryValue(); | 116 DictionaryValue* result = new DictionaryValue(); |
117 result->SetInteger(kConnectionIdKey, id); | 117 result->SetInteger(kConnectionIdKey, id); |
118 result_.reset(result); | 118 SetResult(result); |
119 AsyncWorkCompleted(); | 119 AsyncWorkCompleted(); |
120 } else { | 120 } else { |
121 DictionaryValue* result = new DictionaryValue(); | 121 DictionaryValue* result = new DictionaryValue(); |
122 result->SetInteger(kConnectionIdKey, -1); | 122 result->SetInteger(kConnectionIdKey, -1); |
123 result_.reset(result); | 123 SetResult(result); |
124 AsyncWorkCompleted(); | 124 AsyncWorkCompleted(); |
125 } | 125 } |
126 } | 126 } |
127 | 127 |
128 SerialConnection* SerialOpenFunction::CreateSerialConnection( | 128 SerialConnection* SerialOpenFunction::CreateSerialConnection( |
129 const std::string& port, | 129 const std::string& port, |
130 int bitrate, | 130 int bitrate, |
131 APIResourceEventNotifier* event_notifier) { | 131 APIResourceEventNotifier* event_notifier) { |
132 return new SerialConnection(port, bitrate, event_notifier); | 132 return new SerialConnection(port, bitrate, event_notifier); |
133 } | 133 } |
(...skipping 25 matching lines...) Expand all Loading... |
159 void SerialCloseFunction::Work() { | 159 void SerialCloseFunction::Work() { |
160 bool close_result = false; | 160 bool close_result = false; |
161 SerialConnection* serial_connection = | 161 SerialConnection* serial_connection = |
162 controller()->GetSerialConnection(params_->connection_id); | 162 controller()->GetSerialConnection(params_->connection_id); |
163 if (serial_connection) { | 163 if (serial_connection) { |
164 serial_connection->Close(); | 164 serial_connection->Close(); |
165 controller()->RemoveSerialConnection(params_->connection_id); | 165 controller()->RemoveSerialConnection(params_->connection_id); |
166 close_result = true; | 166 close_result = true; |
167 } | 167 } |
168 | 168 |
169 result_.reset(Value::CreateBooleanValue(close_result)); | 169 SetResult(Value::CreateBooleanValue(close_result)); |
170 } | 170 } |
171 | 171 |
172 bool SerialCloseFunction::Respond() { | 172 bool SerialCloseFunction::Respond() { |
173 return true; | 173 return true; |
174 } | 174 } |
175 | 175 |
176 SerialReadFunction::SerialReadFunction() { | 176 SerialReadFunction::SerialReadFunction() { |
177 } | 177 } |
178 | 178 |
179 SerialReadFunction::~SerialReadFunction() { | 179 SerialReadFunction::~SerialReadFunction() { |
(...skipping 17 matching lines...) Expand all Loading... |
197 | 197 |
198 DictionaryValue* result = new DictionaryValue(); | 198 DictionaryValue* result = new DictionaryValue(); |
199 | 199 |
200 // The API is defined to require a 'data' value, so we will always | 200 // The API is defined to require a 'data' value, so we will always |
201 // create a BinaryValue, even if it's zero-length. | 201 // create a BinaryValue, even if it's zero-length. |
202 if (bytes_read < 0) | 202 if (bytes_read < 0) |
203 bytes_read = 0; | 203 bytes_read = 0; |
204 result->SetInteger(kBytesReadKey, bytes_read); | 204 result->SetInteger(kBytesReadKey, bytes_read); |
205 result->Set(kDataKey, base::BinaryValue::CreateWithCopiedBuffer( | 205 result->Set(kDataKey, base::BinaryValue::CreateWithCopiedBuffer( |
206 reinterpret_cast<char*>(&byte), bytes_read)); | 206 reinterpret_cast<char*>(&byte), bytes_read)); |
207 result_.reset(result); | 207 SetResult(result); |
208 } | 208 } |
209 | 209 |
210 bool SerialReadFunction::Respond() { | 210 bool SerialReadFunction::Respond() { |
211 return true; | 211 return true; |
212 } | 212 } |
213 | 213 |
214 SerialWriteFunction::SerialWriteFunction() | 214 SerialWriteFunction::SerialWriteFunction() |
215 : io_buffer_(NULL), io_buffer_size_(0) { | 215 : io_buffer_(NULL), io_buffer_size_(0) { |
216 } | 216 } |
217 | 217 |
(...skipping 16 matching lines...) Expand all Loading... |
234 int bytes_written = -1; | 234 int bytes_written = -1; |
235 SerialConnection* serial_connection = | 235 SerialConnection* serial_connection = |
236 controller()->GetSerialConnection(params_->connection_id); | 236 controller()->GetSerialConnection(params_->connection_id); |
237 if (serial_connection) | 237 if (serial_connection) |
238 bytes_written = serial_connection->Write(io_buffer_, io_buffer_size_); | 238 bytes_written = serial_connection->Write(io_buffer_, io_buffer_size_); |
239 else | 239 else |
240 error_ = kSerialConnectionNotFoundError; | 240 error_ = kSerialConnectionNotFoundError; |
241 | 241 |
242 DictionaryValue* result = new DictionaryValue(); | 242 DictionaryValue* result = new DictionaryValue(); |
243 result->SetInteger(kBytesWrittenKey, bytes_written); | 243 result->SetInteger(kBytesWrittenKey, bytes_written); |
244 result_.reset(result); | 244 SetResult(result); |
245 } | 245 } |
246 | 246 |
247 bool SerialWriteFunction::Respond() { | 247 bool SerialWriteFunction::Respond() { |
248 return true; | 248 return true; |
249 } | 249 } |
250 | 250 |
251 SerialFlushFunction::SerialFlushFunction() { | 251 SerialFlushFunction::SerialFlushFunction() { |
252 } | 252 } |
253 | 253 |
254 SerialFlushFunction::~SerialFlushFunction() { | 254 SerialFlushFunction::~SerialFlushFunction() { |
255 } | 255 } |
256 | 256 |
257 bool SerialFlushFunction::Prepare() { | 257 bool SerialFlushFunction::Prepare() { |
258 set_work_thread_id(BrowserThread::FILE); | 258 set_work_thread_id(BrowserThread::FILE); |
259 | 259 |
260 params_ = api::experimental_serial::Flush::Params::Create(*args_); | 260 params_ = api::experimental_serial::Flush::Params::Create(*args_); |
261 EXTENSION_FUNCTION_VALIDATE(params_.get()); | 261 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
262 return true; | 262 return true; |
263 } | 263 } |
264 | 264 |
265 void SerialFlushFunction::Work() { | 265 void SerialFlushFunction::Work() { |
266 bool flush_result = false; | 266 bool flush_result = false; |
267 SerialConnection* serial_connection = | 267 SerialConnection* serial_connection = |
268 controller()->GetSerialConnection(params_->connection_id); | 268 controller()->GetSerialConnection(params_->connection_id); |
269 if (serial_connection) { | 269 if (serial_connection) { |
270 serial_connection->Flush(); | 270 serial_connection->Flush(); |
271 flush_result = true; | 271 flush_result = true; |
272 } | 272 } |
273 | 273 |
274 result_.reset(Value::CreateBooleanValue(flush_result)); | 274 SetResult(Value::CreateBooleanValue(flush_result)); |
275 } | 275 } |
276 | 276 |
277 bool SerialFlushFunction::Respond() { | 277 bool SerialFlushFunction::Respond() { |
278 return true; | 278 return true; |
279 } | 279 } |
280 | 280 |
281 SerialGetControlSignalsFunction::SerialGetControlSignalsFunction() | 281 SerialGetControlSignalsFunction::SerialGetControlSignalsFunction() |
282 : api_response_(false) { | 282 : api_response_(false) { |
283 } | 283 } |
284 | 284 |
(...skipping 20 matching lines...) Expand all Loading... |
305 result->SetBoolean(kDcdKey, control_signals.dcd); | 305 result->SetBoolean(kDcdKey, control_signals.dcd); |
306 result->SetBoolean(kCtsKey, control_signals.cts); | 306 result->SetBoolean(kCtsKey, control_signals.cts); |
307 } else { | 307 } else { |
308 error_ = kErrorGetControlSignalsFailed; | 308 error_ = kErrorGetControlSignalsFailed; |
309 } | 309 } |
310 } else { | 310 } else { |
311 error_ = kSerialConnectionNotFoundError; | 311 error_ = kSerialConnectionNotFoundError; |
312 result->SetBoolean(kSuccessKey, false); | 312 result->SetBoolean(kSuccessKey, false); |
313 } | 313 } |
314 | 314 |
315 result_.reset(result); | 315 SetResult(result); |
316 } | 316 } |
317 | 317 |
318 bool SerialGetControlSignalsFunction::Respond() { | 318 bool SerialGetControlSignalsFunction::Respond() { |
319 return api_response_; | 319 return api_response_; |
320 } | 320 } |
321 | 321 |
322 SerialSetControlSignalsFunction::SerialSetControlSignalsFunction() { | 322 SerialSetControlSignalsFunction::SerialSetControlSignalsFunction() { |
323 } | 323 } |
324 | 324 |
325 SerialSetControlSignalsFunction::~SerialSetControlSignalsFunction() { | 325 SerialSetControlSignalsFunction::~SerialSetControlSignalsFunction() { |
(...skipping 13 matching lines...) Expand all Loading... |
339 controller()->GetSerialConnection(params_->connection_id); | 339 controller()->GetSerialConnection(params_->connection_id); |
340 if (serial_connection) { | 340 if (serial_connection) { |
341 SerialConnection::ControlSignals control_signals = { 0 }; | 341 SerialConnection::ControlSignals control_signals = { 0 }; |
342 control_signals.should_set_dtr = params_->options.dtr.get() != NULL; | 342 control_signals.should_set_dtr = params_->options.dtr.get() != NULL; |
343 if (control_signals.should_set_dtr) | 343 if (control_signals.should_set_dtr) |
344 control_signals.dtr = *(params_->options.dtr); | 344 control_signals.dtr = *(params_->options.dtr); |
345 control_signals.should_set_rts = params_->options.rts.get() != NULL; | 345 control_signals.should_set_rts = params_->options.rts.get() != NULL; |
346 if (control_signals.should_set_rts) | 346 if (control_signals.should_set_rts) |
347 control_signals.rts = *(params_->options.rts); | 347 control_signals.rts = *(params_->options.rts); |
348 if (serial_connection->SetControlSignals(control_signals)) { | 348 if (serial_connection->SetControlSignals(control_signals)) { |
349 result_.reset(Value::CreateBooleanValue(true)); | 349 SetResult(Value::CreateBooleanValue(true)); |
350 } else { | 350 } else { |
351 error_ = kErrorSetControlSignalsFailed; | 351 error_ = kErrorSetControlSignalsFailed; |
352 result_.reset(Value::CreateBooleanValue(false)); | 352 SetResult(Value::CreateBooleanValue(false)); |
353 } | 353 } |
354 } else { | 354 } else { |
355 error_ = kSerialConnectionNotFoundError; | 355 error_ = kSerialConnectionNotFoundError; |
356 result_.reset(Value::CreateBooleanValue(false)); | 356 SetResult(Value::CreateBooleanValue(false)); |
357 } | 357 } |
358 } | 358 } |
359 | 359 |
360 bool SerialSetControlSignalsFunction::Respond() { | 360 bool SerialSetControlSignalsFunction::Respond() { |
361 return true; | 361 return true; |
362 } | 362 } |
363 | 363 |
364 } // namespace extensions | 364 } // namespace extensions |
OLD | NEW |