| 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/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" |
| 11 #include "content/public/browser/browser_thread.h" | 11 #include "content/public/browser/browser_thread.h" |
| 12 | 12 |
| 13 using content::BrowserThread; | 13 using content::BrowserThread; |
| 14 | 14 |
| 15 namespace extensions { | 15 namespace extensions { |
| 16 | 16 |
| 17 const char kConnectionIdKey[] = "connectionId"; | 17 const char kConnectionIdKey[] = "connectionId"; |
| 18 const char kPortsKey[] = "ports"; | 18 const char kPortsKey[] = "ports"; |
| 19 const char kDataKey[] = "data"; | 19 const char kDataKey[] = "data"; |
| 20 const char kBytesReadKey[] = "bytesRead"; | 20 const char kBytesReadKey[] = "bytesRead"; |
| 21 const char kBytesWrittenKey[] = "bytesWritten"; | 21 const char kBytesWrittenKey[] = "bytesWritten"; |
| 22 const char kBitrateKey[] = "bitrate"; | 22 const char kBitrateKey[] = "bitrate"; |
| 23 const char kOptionsKey[] = "options"; | 23 const char kOptionsKey[] = "options"; |
| 24 const char kSuccessKey[] = "success"; | 24 const char kSuccessKey[] = "success"; |
| 25 const char kDtrKey[] = "dtr"; | 25 const char kDtrKey[] = "dtr"; |
| 26 const char kRtsKey[] = "rts"; | 26 const char kRtsKey[] = "rts"; |
| 27 const char kDcdKey[] = "dcd"; | 27 const char kDcdKey[] = "dcd"; |
| 28 const char kCtsKey[] = "cts"; | 28 const char kCtsKey[] = "cts"; |
| 29 | 29 |
| 30 const char kErrorGetControlSignalsFailed[] = "Failed to get control signals."; | 30 const char kErrorGetControlSignalsFailed[] = "Failed to get control signals."; |
| 31 const char kErrorSetControlSignalsFailed[] = "Failed to set control signals."; | 31 const char kErrorSetControlSignalsFailed[] = "Failed to set control signals."; |
| 32 | 32 |
| 33 SerialAsyncApiFunction::SerialAsyncApiFunction() |
| 34 : manager_(NULL) { |
| 35 } |
| 36 |
| 37 SerialAsyncApiFunction::~SerialAsyncApiFunction() { |
| 38 } |
| 39 |
| 40 bool SerialAsyncApiFunction::PrePrepare() { |
| 41 manager_ = ExtensionSystem::Get(profile())->serial_connection_manager(); |
| 42 return manager_ != NULL; |
| 43 } |
| 44 |
| 33 SerialGetPortsFunction::SerialGetPortsFunction() {} | 45 SerialGetPortsFunction::SerialGetPortsFunction() {} |
| 34 | 46 |
| 35 bool SerialGetPortsFunction::Prepare() { | 47 bool SerialGetPortsFunction::Prepare() { |
| 36 set_work_thread_id(BrowserThread::FILE); | 48 set_work_thread_id(BrowserThread::FILE); |
| 37 return true; | 49 return true; |
| 38 } | 50 } |
| 39 | 51 |
| 40 void SerialGetPortsFunction::Work() { | 52 void SerialGetPortsFunction::Work() { |
| 41 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 53 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 42 | 54 |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 96 void SerialOpenFunction::Work() { | 108 void SerialOpenFunction::Work() { |
| 97 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 109 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 98 const SerialPortEnumerator::StringSet name_set( | 110 const SerialPortEnumerator::StringSet name_set( |
| 99 SerialPortEnumerator::GenerateValidSerialPortNames()); | 111 SerialPortEnumerator::GenerateValidSerialPortNames()); |
| 100 if (DoesPortExist(params_->port)) { | 112 if (DoesPortExist(params_->port)) { |
| 101 SerialConnection* serial_connection = CreateSerialConnection( | 113 SerialConnection* serial_connection = CreateSerialConnection( |
| 102 params_->port, | 114 params_->port, |
| 103 bitrate_, | 115 bitrate_, |
| 104 event_notifier_); | 116 event_notifier_); |
| 105 CHECK(serial_connection); | 117 CHECK(serial_connection); |
| 106 int id = controller()->AddAPIResource(serial_connection); | 118 int id = manager_->Add(serial_connection); |
| 107 CHECK(id); | 119 CHECK(id); |
| 108 | 120 |
| 109 bool open_result = serial_connection->Open(); | 121 bool open_result = serial_connection->Open(); |
| 110 if (!open_result) { | 122 if (!open_result) { |
| 111 serial_connection->Close(); | 123 serial_connection->Close(); |
| 112 controller()->RemoveSerialConnection(id); | 124 manager_->Remove(id); |
| 113 id = -1; | 125 id = -1; |
| 114 } | 126 } |
| 115 | 127 |
| 116 DictionaryValue* result = new DictionaryValue(); | 128 DictionaryValue* result = new DictionaryValue(); |
| 117 result->SetInteger(kConnectionIdKey, id); | 129 result->SetInteger(kConnectionIdKey, id); |
| 118 SetResult(result); | 130 SetResult(result); |
| 119 AsyncWorkCompleted(); | 131 AsyncWorkCompleted(); |
| 120 } else { | 132 } else { |
| 121 DictionaryValue* result = new DictionaryValue(); | 133 DictionaryValue* result = new DictionaryValue(); |
| 122 result->SetInteger(kConnectionIdKey, -1); | 134 result->SetInteger(kConnectionIdKey, -1); |
| 123 SetResult(result); | 135 SetResult(result); |
| 124 AsyncWorkCompleted(); | 136 AsyncWorkCompleted(); |
| 125 } | 137 } |
| 126 } | 138 } |
| 127 | 139 |
| 128 SerialConnection* SerialOpenFunction::CreateSerialConnection( | 140 SerialConnection* SerialOpenFunction::CreateSerialConnection( |
| 129 const std::string& port, | 141 const std::string& port, |
| 130 int bitrate, | 142 int bitrate, |
| 131 APIResourceEventNotifier* event_notifier) { | 143 ApiResourceEventNotifier* event_notifier) { |
| 132 return new SerialConnection(port, bitrate, event_notifier); | 144 return new SerialConnection(port, bitrate, event_notifier); |
| 133 } | 145 } |
| 134 | 146 |
| 135 bool SerialOpenFunction::DoesPortExist(const std::string& port) { | 147 bool SerialOpenFunction::DoesPortExist(const std::string& port) { |
| 136 const SerialPortEnumerator::StringSet name_set( | 148 const SerialPortEnumerator::StringSet name_set( |
| 137 SerialPortEnumerator::GenerateValidSerialPortNames()); | 149 SerialPortEnumerator::GenerateValidSerialPortNames()); |
| 138 return SerialPortEnumerator::DoesPortExist(name_set, params_->port); | 150 return SerialPortEnumerator::DoesPortExist(name_set, params_->port); |
| 139 } | 151 } |
| 140 | 152 |
| 141 bool SerialOpenFunction::Respond() { | 153 bool SerialOpenFunction::Respond() { |
| 142 return true; | 154 return true; |
| 143 } | 155 } |
| 144 | 156 |
| 145 SerialCloseFunction::SerialCloseFunction() { | 157 SerialCloseFunction::SerialCloseFunction() { |
| 146 } | 158 } |
| 147 | 159 |
| 148 SerialCloseFunction::~SerialCloseFunction() { | 160 SerialCloseFunction::~SerialCloseFunction() { |
| 149 } | 161 } |
| 150 | 162 |
| 151 bool SerialCloseFunction::Prepare() { | 163 bool SerialCloseFunction::Prepare() { |
| 152 set_work_thread_id(BrowserThread::FILE); | 164 set_work_thread_id(BrowserThread::FILE); |
| 153 | 165 |
| 154 params_ = api::experimental_serial::Close::Params::Create(*args_); | 166 params_ = api::experimental_serial::Close::Params::Create(*args_); |
| 155 EXTENSION_FUNCTION_VALIDATE(params_.get()); | 167 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
| 168 |
| 156 return true; | 169 return true; |
| 157 } | 170 } |
| 158 | 171 |
| 159 void SerialCloseFunction::Work() { | 172 void SerialCloseFunction::Work() { |
| 160 bool close_result = false; | 173 bool close_result = false; |
| 161 SerialConnection* serial_connection = | 174 SerialConnection* serial_connection = manager_->Get(params_->connection_id); |
| 162 controller()->GetSerialConnection(params_->connection_id); | |
| 163 if (serial_connection) { | 175 if (serial_connection) { |
| 164 serial_connection->Close(); | 176 serial_connection->Close(); |
| 165 controller()->RemoveSerialConnection(params_->connection_id); | 177 manager_->Remove(params_->connection_id); |
| 166 close_result = true; | 178 close_result = true; |
| 167 } | 179 } |
| 168 | 180 |
| 169 SetResult(Value::CreateBooleanValue(close_result)); | 181 SetResult(Value::CreateBooleanValue(close_result)); |
| 170 } | 182 } |
| 171 | 183 |
| 172 bool SerialCloseFunction::Respond() { | 184 bool SerialCloseFunction::Respond() { |
| 173 return true; | 185 return true; |
| 174 } | 186 } |
| 175 | 187 |
| 176 SerialReadFunction::SerialReadFunction() { | 188 SerialReadFunction::SerialReadFunction() { |
| 177 } | 189 } |
| 178 | 190 |
| 179 SerialReadFunction::~SerialReadFunction() { | 191 SerialReadFunction::~SerialReadFunction() { |
| 180 } | 192 } |
| 181 | 193 |
| 182 bool SerialReadFunction::Prepare() { | 194 bool SerialReadFunction::Prepare() { |
| 183 set_work_thread_id(BrowserThread::FILE); | 195 set_work_thread_id(BrowserThread::FILE); |
| 184 | 196 |
| 185 params_ = api::experimental_serial::Read::Params::Create(*args_); | 197 params_ = api::experimental_serial::Read::Params::Create(*args_); |
| 186 EXTENSION_FUNCTION_VALIDATE(params_.get()); | 198 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
| 199 |
| 187 return true; | 200 return true; |
| 188 } | 201 } |
| 189 | 202 |
| 190 void SerialReadFunction::Work() { | 203 void SerialReadFunction::Work() { |
| 191 uint8 byte = '\0'; | 204 uint8 byte = '\0'; |
| 192 int bytes_read = -1; | 205 int bytes_read = -1; |
| 193 SerialConnection* serial_connection = | 206 SerialConnection* serial_connection = manager_->Get(params_->connection_id); |
| 194 controller()->GetSerialConnection(params_->connection_id); | |
| 195 if (serial_connection) | 207 if (serial_connection) |
| 196 bytes_read = serial_connection->Read(&byte); | 208 bytes_read = serial_connection->Read(&byte); |
| 197 | 209 |
| 198 DictionaryValue* result = new DictionaryValue(); | 210 DictionaryValue* result = new DictionaryValue(); |
| 199 | 211 |
| 200 // The API is defined to require a 'data' value, so we will always | 212 // The API is defined to require a 'data' value, so we will always |
| 201 // create a BinaryValue, even if it's zero-length. | 213 // create a BinaryValue, even if it's zero-length. |
| 202 if (bytes_read < 0) | 214 if (bytes_read < 0) |
| 203 bytes_read = 0; | 215 bytes_read = 0; |
| 204 result->SetInteger(kBytesReadKey, bytes_read); | 216 result->SetInteger(kBytesReadKey, bytes_read); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 225 EXTENSION_FUNCTION_VALIDATE(params_.get()); | 237 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
| 226 | 238 |
| 227 io_buffer_size_ = params_->data->GetSize(); | 239 io_buffer_size_ = params_->data->GetSize(); |
| 228 io_buffer_ = new net::WrappedIOBuffer(params_->data->GetBuffer()); | 240 io_buffer_ = new net::WrappedIOBuffer(params_->data->GetBuffer()); |
| 229 | 241 |
| 230 return true; | 242 return true; |
| 231 } | 243 } |
| 232 | 244 |
| 233 void SerialWriteFunction::Work() { | 245 void SerialWriteFunction::Work() { |
| 234 int bytes_written = -1; | 246 int bytes_written = -1; |
| 235 SerialConnection* serial_connection = | 247 SerialConnection* serial_connection = manager_->Get(params_->connection_id); |
| 236 controller()->GetSerialConnection(params_->connection_id); | |
| 237 if (serial_connection) | 248 if (serial_connection) |
| 238 bytes_written = serial_connection->Write(io_buffer_, io_buffer_size_); | 249 bytes_written = serial_connection->Write(io_buffer_, io_buffer_size_); |
| 239 else | 250 else |
| 240 error_ = kSerialConnectionNotFoundError; | 251 error_ = kSerialConnectionNotFoundError; |
| 241 | 252 |
| 242 DictionaryValue* result = new DictionaryValue(); | 253 DictionaryValue* result = new DictionaryValue(); |
| 243 result->SetInteger(kBytesWrittenKey, bytes_written); | 254 result->SetInteger(kBytesWrittenKey, bytes_written); |
| 244 SetResult(result); | 255 SetResult(result); |
| 245 } | 256 } |
| 246 | 257 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 257 bool SerialFlushFunction::Prepare() { | 268 bool SerialFlushFunction::Prepare() { |
| 258 set_work_thread_id(BrowserThread::FILE); | 269 set_work_thread_id(BrowserThread::FILE); |
| 259 | 270 |
| 260 params_ = api::experimental_serial::Flush::Params::Create(*args_); | 271 params_ = api::experimental_serial::Flush::Params::Create(*args_); |
| 261 EXTENSION_FUNCTION_VALIDATE(params_.get()); | 272 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
| 262 return true; | 273 return true; |
| 263 } | 274 } |
| 264 | 275 |
| 265 void SerialFlushFunction::Work() { | 276 void SerialFlushFunction::Work() { |
| 266 bool flush_result = false; | 277 bool flush_result = false; |
| 267 SerialConnection* serial_connection = | 278 SerialConnection* serial_connection = manager_->Get(params_->connection_id); |
| 268 controller()->GetSerialConnection(params_->connection_id); | |
| 269 if (serial_connection) { | 279 if (serial_connection) { |
| 270 serial_connection->Flush(); | 280 serial_connection->Flush(); |
| 271 flush_result = true; | 281 flush_result = true; |
| 272 } | 282 } |
| 273 | 283 |
| 274 SetResult(Value::CreateBooleanValue(flush_result)); | 284 SetResult(Value::CreateBooleanValue(flush_result)); |
| 275 } | 285 } |
| 276 | 286 |
| 277 bool SerialFlushFunction::Respond() { | 287 bool SerialFlushFunction::Respond() { |
| 278 return true; | 288 return true; |
| 279 } | 289 } |
| 280 | 290 |
| 281 SerialGetControlSignalsFunction::SerialGetControlSignalsFunction() | 291 SerialGetControlSignalsFunction::SerialGetControlSignalsFunction() |
| 282 : api_response_(false) { | 292 : api_response_(false) { |
| 283 } | 293 } |
| 284 | 294 |
| 285 SerialGetControlSignalsFunction::~SerialGetControlSignalsFunction() { | 295 SerialGetControlSignalsFunction::~SerialGetControlSignalsFunction() { |
| 286 } | 296 } |
| 287 | 297 |
| 288 bool SerialGetControlSignalsFunction::Prepare() { | 298 bool SerialGetControlSignalsFunction::Prepare() { |
| 289 set_work_thread_id(BrowserThread::FILE); | 299 set_work_thread_id(BrowserThread::FILE); |
| 290 | 300 |
| 291 params_ = api::experimental_serial::GetControlSignals::Params::Create( | 301 params_ = api::experimental_serial::GetControlSignals::Params::Create( |
| 292 *args_); | 302 *args_); |
| 293 EXTENSION_FUNCTION_VALIDATE(params_.get()); | 303 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
| 304 |
| 294 return true; | 305 return true; |
| 295 } | 306 } |
| 296 | 307 |
| 297 void SerialGetControlSignalsFunction::Work() { | 308 void SerialGetControlSignalsFunction::Work() { |
| 298 DictionaryValue *result = new DictionaryValue(); | 309 DictionaryValue *result = new DictionaryValue(); |
| 299 SerialConnection* serial_connection = | 310 SerialConnection* serial_connection = manager_->Get(params_->connection_id); |
| 300 controller()->GetSerialConnection(params_->connection_id); | |
| 301 if (serial_connection) { | 311 if (serial_connection) { |
| 302 SerialConnection::ControlSignals control_signals = { 0 }; | 312 SerialConnection::ControlSignals control_signals = { 0 }; |
| 303 if (serial_connection->GetControlSignals(control_signals)) { | 313 if (serial_connection->GetControlSignals(control_signals)) { |
| 304 api_response_ = true; | 314 api_response_ = true; |
| 305 result->SetBoolean(kDcdKey, control_signals.dcd); | 315 result->SetBoolean(kDcdKey, control_signals.dcd); |
| 306 result->SetBoolean(kCtsKey, control_signals.cts); | 316 result->SetBoolean(kCtsKey, control_signals.cts); |
| 307 } else { | 317 } else { |
| 308 error_ = kErrorGetControlSignalsFailed; | 318 error_ = kErrorGetControlSignalsFailed; |
| 309 } | 319 } |
| 310 } else { | 320 } else { |
| (...skipping 13 matching lines...) Expand all Loading... |
| 324 | 334 |
| 325 SerialSetControlSignalsFunction::~SerialSetControlSignalsFunction() { | 335 SerialSetControlSignalsFunction::~SerialSetControlSignalsFunction() { |
| 326 } | 336 } |
| 327 | 337 |
| 328 bool SerialSetControlSignalsFunction::Prepare() { | 338 bool SerialSetControlSignalsFunction::Prepare() { |
| 329 set_work_thread_id(BrowserThread::FILE); | 339 set_work_thread_id(BrowserThread::FILE); |
| 330 | 340 |
| 331 params_ = api::experimental_serial::SetControlSignals::Params::Create( | 341 params_ = api::experimental_serial::SetControlSignals::Params::Create( |
| 332 *args_); | 342 *args_); |
| 333 EXTENSION_FUNCTION_VALIDATE(params_.get()); | 343 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
| 344 |
| 334 return true; | 345 return true; |
| 335 } | 346 } |
| 336 | 347 |
| 337 void SerialSetControlSignalsFunction::Work() { | 348 void SerialSetControlSignalsFunction::Work() { |
| 338 SerialConnection* serial_connection = | 349 SerialConnection* serial_connection = manager_->Get(params_->connection_id); |
| 339 controller()->GetSerialConnection(params_->connection_id); | |
| 340 if (serial_connection) { | 350 if (serial_connection) { |
| 341 SerialConnection::ControlSignals control_signals = { 0 }; | 351 SerialConnection::ControlSignals control_signals = { 0 }; |
| 342 control_signals.should_set_dtr = params_->options.dtr.get() != NULL; | 352 control_signals.should_set_dtr = params_->options.dtr.get() != NULL; |
| 343 if (control_signals.should_set_dtr) | 353 if (control_signals.should_set_dtr) |
| 344 control_signals.dtr = *(params_->options.dtr); | 354 control_signals.dtr = *(params_->options.dtr); |
| 345 control_signals.should_set_rts = params_->options.rts.get() != NULL; | 355 control_signals.should_set_rts = params_->options.rts.get() != NULL; |
| 346 if (control_signals.should_set_rts) | 356 if (control_signals.should_set_rts) |
| 347 control_signals.rts = *(params_->options.rts); | 357 control_signals.rts = *(params_->options.rts); |
| 348 if (serial_connection->SetControlSignals(control_signals)) { | 358 if (serial_connection->SetControlSignals(control_signals)) { |
| 349 SetResult(Value::CreateBooleanValue(true)); | 359 SetResult(Value::CreateBooleanValue(true)); |
| 350 } else { | 360 } else { |
| 351 error_ = kErrorSetControlSignalsFailed; | 361 error_ = kErrorSetControlSignalsFailed; |
| 352 SetResult(Value::CreateBooleanValue(false)); | 362 SetResult(Value::CreateBooleanValue(false)); |
| 353 } | 363 } |
| 354 } else { | 364 } else { |
| 355 error_ = kSerialConnectionNotFoundError; | 365 error_ = kSerialConnectionNotFoundError; |
| 356 SetResult(Value::CreateBooleanValue(false)); | 366 SetResult(Value::CreateBooleanValue(false)); |
| 357 } | 367 } |
| 358 } | 368 } |
| 359 | 369 |
| 360 bool SerialSetControlSignalsFunction::Respond() { | 370 bool SerialSetControlSignalsFunction::Respond() { |
| 361 return true; | 371 return true; |
| 362 } | 372 } |
| 363 | 373 |
| 364 } // namespace extensions | 374 } // namespace extensions |
| OLD | NEW |