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" |
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 kBytesToReadKey[] = "bytesToRead"; | |
21 const char kBytesWrittenKey[] = "bytesWritten"; | 22 const char kBytesWrittenKey[] = "bytesWritten"; |
22 const char kBitrateKey[] = "bitrate"; | 23 const char kBitrateKey[] = "bitrate"; |
23 const char kOptionsKey[] = "options"; | 24 const char kOptionsKey[] = "options"; |
24 const char kSuccessKey[] = "success"; | 25 const char kSuccessKey[] = "success"; |
25 const char kDtrKey[] = "dtr"; | 26 const char kDtrKey[] = "dtr"; |
26 const char kRtsKey[] = "rts"; | 27 const char kRtsKey[] = "rts"; |
27 const char kDcdKey[] = "dcd"; | 28 const char kDcdKey[] = "dcd"; |
28 const char kCtsKey[] = "cts"; | 29 const char kCtsKey[] = "cts"; |
29 | 30 |
30 const char kErrorGetControlSignalsFailed[] = "Failed to get control signals."; | 31 const char kErrorGetControlSignalsFailed[] = "Failed to get control signals."; |
31 const char kErrorSetControlSignalsFailed[] = "Failed to set control signals."; | 32 const char kErrorSetControlSignalsFailed[] = "Failed to set control signals."; |
33 const char kSerialReadInvalidBytesToRead[] = "Number of bytes to read must " | |
34 "be a positive number."; | |
32 | 35 |
33 SerialAsyncApiFunction::SerialAsyncApiFunction() | 36 SerialAsyncApiFunction::SerialAsyncApiFunction() |
34 : manager_(NULL) { | 37 : manager_(NULL) { |
35 } | 38 } |
36 | 39 |
37 SerialAsyncApiFunction::~SerialAsyncApiFunction() { | 40 SerialAsyncApiFunction::~SerialAsyncApiFunction() { |
38 } | 41 } |
39 | 42 |
40 bool SerialAsyncApiFunction::PrePrepare() { | 43 bool SerialAsyncApiFunction::PrePrepare() { |
41 manager_ = ExtensionSystem::Get(profile())->serial_connection_manager(); | 44 manager_ = ExtensionSystem::Get(profile())->serial_connection_manager(); |
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
189 } | 192 } |
190 | 193 |
191 SerialReadFunction::~SerialReadFunction() { | 194 SerialReadFunction::~SerialReadFunction() { |
192 } | 195 } |
193 | 196 |
194 bool SerialReadFunction::Prepare() { | 197 bool SerialReadFunction::Prepare() { |
195 set_work_thread_id(BrowserThread::FILE); | 198 set_work_thread_id(BrowserThread::FILE); |
196 | 199 |
197 params_ = api::experimental_serial::Read::Params::Create(*args_); | 200 params_ = api::experimental_serial::Read::Params::Create(*args_); |
198 EXTENSION_FUNCTION_VALIDATE(params_.get()); | 201 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
202 if (params_->bytes_to_read <= 0) { | |
203 error_ = kSerialReadInvalidBytesToRead; | |
204 return false; | |
205 } | |
199 | 206 |
200 return true; | 207 return true; |
201 } | 208 } |
202 | 209 |
203 void SerialReadFunction::Work() { | 210 void SerialReadFunction::Work() { |
204 uint8 byte = '\0'; | |
205 int bytes_read = -1; | 211 int bytes_read = -1; |
206 SerialConnection* serial_connection = manager_->Get(params_->connection_id); | 212 scoped_refptr<net::IOBufferWithSize> io_buffer( |
213 new net::IOBufferWithSize(params_->bytes_to_read)); | |
asargent_no_longer_on_chrome
2012/08/02 19:58:07
nit: does IOBufferWithSize already properly handle
| |
214 SerialConnection* serial_connection(manager_->Get(params_->connection_id)); | |
215 | |
207 if (serial_connection) | 216 if (serial_connection) |
208 bytes_read = serial_connection->Read(&byte); | 217 bytes_read = serial_connection->Read(io_buffer); |
209 | 218 |
210 DictionaryValue* result = new DictionaryValue(); | 219 DictionaryValue* result = new DictionaryValue(); |
211 | 220 |
212 // The API is defined to require a 'data' value, so we will always | 221 // The API is defined to require a 'data' value, so we will always |
213 // create a BinaryValue, even if it's zero-length. | 222 // create a BinaryValue, even if it's zero-length. |
214 if (bytes_read < 0) | 223 if (bytes_read < 0) |
215 bytes_read = 0; | 224 bytes_read = 0; |
216 result->SetInteger(kBytesReadKey, bytes_read); | 225 result->SetInteger(kBytesReadKey, bytes_read); |
217 result->Set(kDataKey, base::BinaryValue::CreateWithCopiedBuffer( | 226 result->Set(kDataKey, base::BinaryValue::CreateWithCopiedBuffer( |
218 reinterpret_cast<char*>(&byte), bytes_read)); | 227 io_buffer->data(), bytes_read)); |
219 SetResult(result); | 228 SetResult(result); |
220 } | 229 } |
221 | 230 |
222 bool SerialReadFunction::Respond() { | 231 bool SerialReadFunction::Respond() { |
223 return true; | 232 return true; |
224 } | 233 } |
225 | 234 |
226 SerialWriteFunction::SerialWriteFunction() | 235 SerialWriteFunction::SerialWriteFunction() |
227 : io_buffer_(NULL), io_buffer_size_(0) { | 236 : io_buffer_(NULL), io_buffer_size_(0) { |
228 } | 237 } |
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
365 error_ = kSerialConnectionNotFoundError; | 374 error_ = kSerialConnectionNotFoundError; |
366 SetResult(Value::CreateBooleanValue(false)); | 375 SetResult(Value::CreateBooleanValue(false)); |
367 } | 376 } |
368 } | 377 } |
369 | 378 |
370 bool SerialSetControlSignalsFunction::Respond() { | 379 bool SerialSetControlSignalsFunction::Respond() { |
371 return true; | 380 return true; |
372 } | 381 } |
373 | 382 |
374 } // namespace extensions | 383 } // namespace extensions |
OLD | NEW |