Chromium Code Reviews| 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/extension_system.h" | 8 #include "chrome/browser/extensions/extension_system.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 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 117 params_->port, | 117 params_->port, |
| 118 bitrate_, | 118 bitrate_, |
| 119 event_notifier_); | 119 event_notifier_); |
| 120 CHECK(serial_connection); | 120 CHECK(serial_connection); |
| 121 int id = manager_->Add(serial_connection); | 121 int id = manager_->Add(serial_connection); |
| 122 CHECK(id); | 122 CHECK(id); |
| 123 | 123 |
| 124 bool open_result = serial_connection->Open(); | 124 bool open_result = serial_connection->Open(); |
| 125 if (!open_result) { | 125 if (!open_result) { |
| 126 serial_connection->Close(); | 126 serial_connection->Close(); |
| 127 manager_->Remove(id); | 127 manager_->Remove(extension_->id(), id); |
|
Mihai Parparita -not on Chrome
2012/09/11 07:18:19
Don't feel too strongly about this, but rather tha
| |
| 128 id = -1; | 128 id = -1; |
| 129 } | 129 } |
| 130 | 130 |
| 131 DictionaryValue* result = new DictionaryValue(); | 131 DictionaryValue* result = new DictionaryValue(); |
| 132 result->SetInteger(kConnectionIdKey, id); | 132 result->SetInteger(kConnectionIdKey, id); |
| 133 SetResult(result); | 133 SetResult(result); |
| 134 AsyncWorkCompleted(); | 134 AsyncWorkCompleted(); |
| 135 } else { | 135 } else { |
| 136 DictionaryValue* result = new DictionaryValue(); | 136 DictionaryValue* result = new DictionaryValue(); |
| 137 result->SetInteger(kConnectionIdKey, -1); | 137 result->SetInteger(kConnectionIdKey, -1); |
| 138 SetResult(result); | 138 SetResult(result); |
| 139 AsyncWorkCompleted(); | 139 AsyncWorkCompleted(); |
| 140 } | 140 } |
| 141 } | 141 } |
| 142 | 142 |
| 143 SerialConnection* SerialOpenFunction::CreateSerialConnection( | 143 SerialConnection* SerialOpenFunction::CreateSerialConnection( |
| 144 const std::string& port, | 144 const std::string& port, |
| 145 int bitrate, | 145 int bitrate, |
| 146 ApiResourceEventNotifier* event_notifier) { | 146 ApiResourceEventNotifier* event_notifier) { |
| 147 return new SerialConnection(port, bitrate, event_notifier); | 147 return new SerialConnection(port, bitrate, extension_->id(), event_notifier); |
| 148 } | 148 } |
| 149 | 149 |
| 150 bool SerialOpenFunction::DoesPortExist(const std::string& port) { | 150 bool SerialOpenFunction::DoesPortExist(const std::string& port) { |
| 151 const SerialPortEnumerator::StringSet name_set( | 151 const SerialPortEnumerator::StringSet name_set( |
| 152 SerialPortEnumerator::GenerateValidSerialPortNames()); | 152 SerialPortEnumerator::GenerateValidSerialPortNames()); |
| 153 return SerialPortEnumerator::DoesPortExist(name_set, params_->port); | 153 return SerialPortEnumerator::DoesPortExist(name_set, params_->port); |
| 154 } | 154 } |
| 155 | 155 |
| 156 bool SerialOpenFunction::Respond() { | 156 bool SerialOpenFunction::Respond() { |
| 157 return true; | 157 return true; |
| 158 } | 158 } |
| 159 | 159 |
| 160 SerialCloseFunction::SerialCloseFunction() { | 160 SerialCloseFunction::SerialCloseFunction() { |
| 161 } | 161 } |
| 162 | 162 |
| 163 SerialCloseFunction::~SerialCloseFunction() { | 163 SerialCloseFunction::~SerialCloseFunction() { |
| 164 } | 164 } |
| 165 | 165 |
| 166 bool SerialCloseFunction::Prepare() { | 166 bool SerialCloseFunction::Prepare() { |
| 167 set_work_thread_id(BrowserThread::FILE); | 167 set_work_thread_id(BrowserThread::FILE); |
| 168 | 168 |
| 169 params_ = api::serial::Close::Params::Create(*args_); | 169 params_ = api::serial::Close::Params::Create(*args_); |
| 170 EXTENSION_FUNCTION_VALIDATE(params_.get()); | 170 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
| 171 | 171 |
| 172 return true; | 172 return true; |
| 173 } | 173 } |
| 174 | 174 |
| 175 void SerialCloseFunction::Work() { | 175 void SerialCloseFunction::Work() { |
| 176 bool close_result = false; | 176 bool close_result = false; |
| 177 SerialConnection* serial_connection = manager_->Get(params_->connection_id); | 177 SerialConnection* serial_connection = manager_->Get(extension_->id(), |
| 178 params_->connection_id); | |
| 178 if (serial_connection) { | 179 if (serial_connection) { |
| 179 serial_connection->Close(); | 180 serial_connection->Close(); |
| 180 manager_->Remove(params_->connection_id); | 181 manager_->Remove(extension_->id(), params_->connection_id); |
| 181 close_result = true; | 182 close_result = true; |
| 182 } | 183 } |
| 183 | 184 |
| 184 SetResult(Value::CreateBooleanValue(close_result)); | 185 SetResult(Value::CreateBooleanValue(close_result)); |
| 185 } | 186 } |
| 186 | 187 |
| 187 bool SerialCloseFunction::Respond() { | 188 bool SerialCloseFunction::Respond() { |
| 188 return true; | 189 return true; |
| 189 } | 190 } |
| 190 | 191 |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 204 return false; | 205 return false; |
| 205 } | 206 } |
| 206 | 207 |
| 207 return true; | 208 return true; |
| 208 } | 209 } |
| 209 | 210 |
| 210 void SerialReadFunction::Work() { | 211 void SerialReadFunction::Work() { |
| 211 int bytes_read = -1; | 212 int bytes_read = -1; |
| 212 scoped_refptr<net::IOBufferWithSize> io_buffer( | 213 scoped_refptr<net::IOBufferWithSize> io_buffer( |
| 213 new net::IOBufferWithSize(params_->bytes_to_read)); | 214 new net::IOBufferWithSize(params_->bytes_to_read)); |
| 214 SerialConnection* serial_connection(manager_->Get(params_->connection_id)); | 215 SerialConnection* serial_connection(manager_->Get(extension_->id(), |
| 216 params_->connection_id)); | |
| 215 | 217 |
| 216 if (serial_connection) | 218 if (serial_connection) |
| 217 bytes_read = serial_connection->Read(io_buffer); | 219 bytes_read = serial_connection->Read(io_buffer); |
| 218 | 220 |
| 219 DictionaryValue* result = new DictionaryValue(); | 221 DictionaryValue* result = new DictionaryValue(); |
| 220 | 222 |
| 221 // The API is defined to require a 'data' value, so we will always | 223 // The API is defined to require a 'data' value, so we will always |
| 222 // create a BinaryValue, even if it's zero-length. | 224 // create a BinaryValue, even if it's zero-length. |
| 223 if (bytes_read < 0) | 225 if (bytes_read < 0) |
| 224 bytes_read = 0; | 226 bytes_read = 0; |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 246 EXTENSION_FUNCTION_VALIDATE(params_.get()); | 248 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
| 247 | 249 |
| 248 io_buffer_size_ = params_->data.size(); | 250 io_buffer_size_ = params_->data.size(); |
| 249 io_buffer_ = new net::WrappedIOBuffer(params_->data.data()); | 251 io_buffer_ = new net::WrappedIOBuffer(params_->data.data()); |
| 250 | 252 |
| 251 return true; | 253 return true; |
| 252 } | 254 } |
| 253 | 255 |
| 254 void SerialWriteFunction::Work() { | 256 void SerialWriteFunction::Work() { |
| 255 int bytes_written = -1; | 257 int bytes_written = -1; |
| 256 SerialConnection* serial_connection = manager_->Get(params_->connection_id); | 258 SerialConnection* serial_connection = manager_->Get(extension_->id(), |
| 259 params_->connection_id); | |
| 257 if (serial_connection) | 260 if (serial_connection) |
| 258 bytes_written = serial_connection->Write(io_buffer_, io_buffer_size_); | 261 bytes_written = serial_connection->Write(io_buffer_, io_buffer_size_); |
| 259 else | 262 else |
| 260 error_ = kSerialConnectionNotFoundError; | 263 error_ = kSerialConnectionNotFoundError; |
| 261 | 264 |
| 262 DictionaryValue* result = new DictionaryValue(); | 265 DictionaryValue* result = new DictionaryValue(); |
| 263 result->SetInteger(kBytesWrittenKey, bytes_written); | 266 result->SetInteger(kBytesWrittenKey, bytes_written); |
| 264 SetResult(result); | 267 SetResult(result); |
| 265 } | 268 } |
| 266 | 269 |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 277 bool SerialFlushFunction::Prepare() { | 280 bool SerialFlushFunction::Prepare() { |
| 278 set_work_thread_id(BrowserThread::FILE); | 281 set_work_thread_id(BrowserThread::FILE); |
| 279 | 282 |
| 280 params_ = api::serial::Flush::Params::Create(*args_); | 283 params_ = api::serial::Flush::Params::Create(*args_); |
| 281 EXTENSION_FUNCTION_VALIDATE(params_.get()); | 284 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
| 282 return true; | 285 return true; |
| 283 } | 286 } |
| 284 | 287 |
| 285 void SerialFlushFunction::Work() { | 288 void SerialFlushFunction::Work() { |
| 286 bool flush_result = false; | 289 bool flush_result = false; |
| 287 SerialConnection* serial_connection = manager_->Get(params_->connection_id); | 290 SerialConnection* serial_connection = manager_->Get(extension_->id(), |
| 291 params_->connection_id); | |
| 288 if (serial_connection) { | 292 if (serial_connection) { |
| 289 serial_connection->Flush(); | 293 serial_connection->Flush(); |
| 290 flush_result = true; | 294 flush_result = true; |
| 291 } | 295 } |
| 292 | 296 |
| 293 SetResult(Value::CreateBooleanValue(flush_result)); | 297 SetResult(Value::CreateBooleanValue(flush_result)); |
| 294 } | 298 } |
| 295 | 299 |
| 296 bool SerialFlushFunction::Respond() { | 300 bool SerialFlushFunction::Respond() { |
| 297 return true; | 301 return true; |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 309 | 313 |
| 310 params_ = api::serial::GetControlSignals::Params::Create( | 314 params_ = api::serial::GetControlSignals::Params::Create( |
| 311 *args_); | 315 *args_); |
| 312 EXTENSION_FUNCTION_VALIDATE(params_.get()); | 316 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
| 313 | 317 |
| 314 return true; | 318 return true; |
| 315 } | 319 } |
| 316 | 320 |
| 317 void SerialGetControlSignalsFunction::Work() { | 321 void SerialGetControlSignalsFunction::Work() { |
| 318 DictionaryValue *result = new DictionaryValue(); | 322 DictionaryValue *result = new DictionaryValue(); |
| 319 SerialConnection* serial_connection = manager_->Get(params_->connection_id); | 323 SerialConnection* serial_connection = manager_->Get(extension_->id(), |
| 324 params_->connection_id); | |
| 320 if (serial_connection) { | 325 if (serial_connection) { |
| 321 SerialConnection::ControlSignals control_signals = { 0 }; | 326 SerialConnection::ControlSignals control_signals = { 0 }; |
| 322 if (serial_connection->GetControlSignals(control_signals)) { | 327 if (serial_connection->GetControlSignals(control_signals)) { |
| 323 api_response_ = true; | 328 api_response_ = true; |
| 324 result->SetBoolean(kDcdKey, control_signals.dcd); | 329 result->SetBoolean(kDcdKey, control_signals.dcd); |
| 325 result->SetBoolean(kCtsKey, control_signals.cts); | 330 result->SetBoolean(kCtsKey, control_signals.cts); |
| 326 } else { | 331 } else { |
| 327 error_ = kErrorGetControlSignalsFailed; | 332 error_ = kErrorGetControlSignalsFailed; |
| 328 } | 333 } |
| 329 } else { | 334 } else { |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 348 set_work_thread_id(BrowserThread::FILE); | 353 set_work_thread_id(BrowserThread::FILE); |
| 349 | 354 |
| 350 params_ = api::serial::SetControlSignals::Params::Create( | 355 params_ = api::serial::SetControlSignals::Params::Create( |
| 351 *args_); | 356 *args_); |
| 352 EXTENSION_FUNCTION_VALIDATE(params_.get()); | 357 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
| 353 | 358 |
| 354 return true; | 359 return true; |
| 355 } | 360 } |
| 356 | 361 |
| 357 void SerialSetControlSignalsFunction::Work() { | 362 void SerialSetControlSignalsFunction::Work() { |
| 358 SerialConnection* serial_connection = manager_->Get(params_->connection_id); | 363 SerialConnection* serial_connection = manager_->Get(extension_->id(), |
| 364 params_->connection_id); | |
| 359 if (serial_connection) { | 365 if (serial_connection) { |
| 360 SerialConnection::ControlSignals control_signals = { 0 }; | 366 SerialConnection::ControlSignals control_signals = { 0 }; |
| 361 control_signals.should_set_dtr = params_->options.dtr.get() != NULL; | 367 control_signals.should_set_dtr = params_->options.dtr.get() != NULL; |
| 362 if (control_signals.should_set_dtr) | 368 if (control_signals.should_set_dtr) |
| 363 control_signals.dtr = *(params_->options.dtr); | 369 control_signals.dtr = *(params_->options.dtr); |
| 364 control_signals.should_set_rts = params_->options.rts.get() != NULL; | 370 control_signals.should_set_rts = params_->options.rts.get() != NULL; |
| 365 if (control_signals.should_set_rts) | 371 if (control_signals.should_set_rts) |
| 366 control_signals.rts = *(params_->options.rts); | 372 control_signals.rts = *(params_->options.rts); |
| 367 if (serial_connection->SetControlSignals(control_signals)) { | 373 if (serial_connection->SetControlSignals(control_signals)) { |
| 368 SetResult(Value::CreateBooleanValue(true)); | 374 SetResult(Value::CreateBooleanValue(true)); |
| 369 } else { | 375 } else { |
| 370 error_ = kErrorSetControlSignalsFailed; | 376 error_ = kErrorSetControlSignalsFailed; |
| 371 SetResult(Value::CreateBooleanValue(false)); | 377 SetResult(Value::CreateBooleanValue(false)); |
| 372 } | 378 } |
| 373 } else { | 379 } else { |
| 374 error_ = kSerialConnectionNotFoundError; | 380 error_ = kSerialConnectionNotFoundError; |
| 375 SetResult(Value::CreateBooleanValue(false)); | 381 SetResult(Value::CreateBooleanValue(false)); |
| 376 } | 382 } |
| 377 } | 383 } |
| 378 | 384 |
| 379 bool SerialSetControlSignalsFunction::Respond() { | 385 bool SerialSetControlSignalsFunction::Respond() { |
| 380 return true; | 386 return true; |
| 381 } | 387 } |
| 382 | 388 |
| 383 } // namespace extensions | 389 } // namespace extensions |
| OLD | NEW |